186470930SIngo Molnar /* 25f9273d6SNamhyung Kim * usage.c 35f9273d6SNamhyung Kim * 45f9273d6SNamhyung Kim * Various reporting routines. 55f9273d6SNamhyung Kim * Originally copied from GIT source. 686470930SIngo Molnar * 786470930SIngo Molnar * Copyright (C) Linus Torvalds, 2005 886470930SIngo Molnar */ 986470930SIngo Molnar #include "util.h" 100d37aa34SArnaldo Carvalho de Melo #include "debug.h" 1186470930SIngo Molnar 1286470930SIngo Molnar static void report(const char *prefix, const char *err, va_list params) 1386470930SIngo Molnar { 1486470930SIngo Molnar char msg[1024]; 1586470930SIngo Molnar vsnprintf(msg, sizeof(msg), err, params); 1686470930SIngo Molnar fprintf(stderr, " %s%s\n", prefix, msg); 1786470930SIngo Molnar } 1886470930SIngo Molnar 1986470930SIngo Molnar static NORETURN void usage_builtin(const char *err) 2086470930SIngo Molnar { 21716c69feSIngo Molnar fprintf(stderr, "\n Usage: %s\n", err); 2286470930SIngo Molnar exit(129); 2386470930SIngo Molnar } 2486470930SIngo Molnar 2586470930SIngo Molnar static NORETURN void die_builtin(const char *err, va_list params) 2686470930SIngo Molnar { 27716c69feSIngo Molnar report(" Fatal: ", err, params); 2886470930SIngo Molnar exit(128); 2986470930SIngo Molnar } 3086470930SIngo Molnar 3186470930SIngo Molnar static void error_builtin(const char *err, va_list params) 3286470930SIngo Molnar { 33716c69feSIngo Molnar report(" Error: ", err, params); 3486470930SIngo Molnar } 3586470930SIngo Molnar 3686470930SIngo Molnar static void warn_builtin(const char *warn, va_list params) 3786470930SIngo Molnar { 38716c69feSIngo Molnar report(" Warning: ", warn, params); 3986470930SIngo Molnar } 4086470930SIngo Molnar 4186470930SIngo Molnar /* If we are in a dlopen()ed .so write to a global variable would segfault 4286470930SIngo Molnar * (ugh), so keep things static. */ 4386470930SIngo Molnar static void (*usage_routine)(const char *err) NORETURN = usage_builtin; 4486470930SIngo Molnar static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin; 4586470930SIngo Molnar static void (*error_routine)(const char *err, va_list params) = error_builtin; 4686470930SIngo Molnar static void (*warn_routine)(const char *err, va_list params) = warn_builtin; 4786470930SIngo Molnar 4886470930SIngo Molnar void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN) 4986470930SIngo Molnar { 5086470930SIngo Molnar die_routine = routine; 5186470930SIngo Molnar } 5286470930SIngo Molnar 5386470930SIngo Molnar void usage(const char *err) 5486470930SIngo Molnar { 5586470930SIngo Molnar usage_routine(err); 5686470930SIngo Molnar } 5786470930SIngo Molnar 5886470930SIngo Molnar void die(const char *err, ...) 5986470930SIngo Molnar { 6086470930SIngo Molnar va_list params; 6186470930SIngo Molnar 6286470930SIngo Molnar va_start(params, err); 6386470930SIngo Molnar die_routine(err, params); 6486470930SIngo Molnar va_end(params); 6586470930SIngo Molnar } 6686470930SIngo Molnar 6786470930SIngo Molnar int error(const char *err, ...) 6886470930SIngo Molnar { 6986470930SIngo Molnar va_list params; 7086470930SIngo Molnar 7186470930SIngo Molnar va_start(params, err); 7286470930SIngo Molnar error_routine(err, params); 7386470930SIngo Molnar va_end(params); 7486470930SIngo Molnar return -1; 7586470930SIngo Molnar } 7686470930SIngo Molnar 7786470930SIngo Molnar void warning(const char *warn, ...) 7886470930SIngo Molnar { 7986470930SIngo Molnar va_list params; 8086470930SIngo Molnar 8186470930SIngo Molnar va_start(params, warn); 8286470930SIngo Molnar warn_routine(warn, params); 8386470930SIngo Molnar va_end(params); 8486470930SIngo Molnar } 850d37aa34SArnaldo Carvalho de Melo 860d37aa34SArnaldo Carvalho de Melo uid_t parse_target_uid(const char *str, pid_t tid, pid_t pid) 870d37aa34SArnaldo Carvalho de Melo { 880d37aa34SArnaldo Carvalho de Melo struct passwd pwd, *result; 890d37aa34SArnaldo Carvalho de Melo char buf[1024]; 900d37aa34SArnaldo Carvalho de Melo 910d37aa34SArnaldo Carvalho de Melo if (str == NULL) 920d37aa34SArnaldo Carvalho de Melo return UINT_MAX; 930d37aa34SArnaldo Carvalho de Melo 940d37aa34SArnaldo Carvalho de Melo /* CPU and PID are mutually exclusive */ 950d37aa34SArnaldo Carvalho de Melo if (tid > 0 || pid > 0) { 960d37aa34SArnaldo Carvalho de Melo ui__warning("PID/TID switch overriding UID\n"); 970d37aa34SArnaldo Carvalho de Melo sleep(1); 980d37aa34SArnaldo Carvalho de Melo return UINT_MAX; 990d37aa34SArnaldo Carvalho de Melo } 1000d37aa34SArnaldo Carvalho de Melo 1010d37aa34SArnaldo Carvalho de Melo getpwnam_r(str, &pwd, buf, sizeof(buf), &result); 1020d37aa34SArnaldo Carvalho de Melo 1030d37aa34SArnaldo Carvalho de Melo if (result == NULL) { 1040d37aa34SArnaldo Carvalho de Melo char *endptr; 1050d37aa34SArnaldo Carvalho de Melo int uid = strtol(str, &endptr, 10); 1060d37aa34SArnaldo Carvalho de Melo 1070d37aa34SArnaldo Carvalho de Melo if (*endptr != '\0') { 1080d37aa34SArnaldo Carvalho de Melo ui__error("Invalid user %s\n", str); 1090d37aa34SArnaldo Carvalho de Melo return UINT_MAX - 1; 1100d37aa34SArnaldo Carvalho de Melo } 1110d37aa34SArnaldo Carvalho de Melo 1120d37aa34SArnaldo Carvalho de Melo getpwuid_r(uid, &pwd, buf, sizeof(buf), &result); 1130d37aa34SArnaldo Carvalho de Melo 1140d37aa34SArnaldo Carvalho de Melo if (result == NULL) { 1150d37aa34SArnaldo Carvalho de Melo ui__error("Problems obtaining information for user %s\n", 1160d37aa34SArnaldo Carvalho de Melo str); 1170d37aa34SArnaldo Carvalho de Melo return UINT_MAX - 1; 1180d37aa34SArnaldo Carvalho de Melo } 1190d37aa34SArnaldo Carvalho de Melo } 1200d37aa34SArnaldo Carvalho de Melo 1210d37aa34SArnaldo Carvalho de Melo return result->pw_uid; 1220d37aa34SArnaldo Carvalho de Melo } 123