Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Helper functions for handling target threads/cpus |
| 3 | * |
| 4 | * Copyright (C) 2012, LG Electronics, Namhyung Kim <namhyung.kim@lge.com> |
| 5 | * |
| 6 | * Released under the GPL v2. |
| 7 | */ |
| 8 | |
| 9 | #include "target.h" |
Arnaldo Carvalho de Melo | 175729f | 2016-07-07 11:38:09 -0300 | [diff] [blame] | 10 | #include "util.h" |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 11 | #include "debug.h" |
| 12 | |
Namhyung Kim | dfe78ad | 2012-05-07 14:09:01 +0900 | [diff] [blame] | 13 | #include <pwd.h> |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 14 | #include <string.h> |
Namhyung Kim | dfe78ad | 2012-05-07 14:09:01 +0900 | [diff] [blame] | 15 | |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 17 | enum target_errno target__validate(struct target *target) |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 18 | { |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 19 | enum target_errno ret = TARGET_ERRNO__SUCCESS; |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 20 | |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 21 | if (target->pid) |
| 22 | target->tid = target->pid; |
| 23 | |
| 24 | /* CPU and PID are mutually exclusive */ |
| 25 | if (target->tid && target->cpu_list) { |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 26 | target->cpu_list = NULL; |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 27 | if (ret == TARGET_ERRNO__SUCCESS) |
| 28 | ret = TARGET_ERRNO__PID_OVERRIDE_CPU; |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | /* UID and PID are mutually exclusive */ |
| 32 | if (target->tid && target->uid_str) { |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 33 | target->uid_str = NULL; |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 34 | if (ret == TARGET_ERRNO__SUCCESS) |
| 35 | ret = TARGET_ERRNO__PID_OVERRIDE_UID; |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* UID and CPU are mutually exclusive */ |
| 39 | if (target->uid_str && target->cpu_list) { |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 40 | target->cpu_list = NULL; |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 41 | if (ret == TARGET_ERRNO__SUCCESS) |
| 42 | ret = TARGET_ERRNO__UID_OVERRIDE_CPU; |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 43 | } |
| 44 | |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 45 | /* PID and SYSTEM are mutually exclusive */ |
| 46 | if (target->tid && target->system_wide) { |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 47 | target->system_wide = false; |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 48 | if (ret == TARGET_ERRNO__SUCCESS) |
| 49 | ret = TARGET_ERRNO__PID_OVERRIDE_SYSTEM; |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 50 | } |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 51 | |
| 52 | /* UID and SYSTEM are mutually exclusive */ |
| 53 | if (target->uid_str && target->system_wide) { |
| 54 | target->system_wide = false; |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 55 | if (ret == TARGET_ERRNO__SUCCESS) |
| 56 | ret = TARGET_ERRNO__UID_OVERRIDE_SYSTEM; |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 57 | } |
| 58 | |
Adrian Hunter | 3aa5939 | 2013-11-15 15:52:29 +0200 | [diff] [blame] | 59 | /* THREAD and SYSTEM/CPU are mutually exclusive */ |
| 60 | if (target->per_thread && (target->system_wide || target->cpu_list)) { |
| 61 | target->per_thread = false; |
| 62 | if (ret == TARGET_ERRNO__SUCCESS) |
| 63 | ret = TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD; |
| 64 | } |
| 65 | |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 66 | return ret; |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 67 | } |
Namhyung Kim | dfe78ad | 2012-05-07 14:09:01 +0900 | [diff] [blame] | 68 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 69 | enum target_errno target__parse_uid(struct target *target) |
Namhyung Kim | dfe78ad | 2012-05-07 14:09:01 +0900 | [diff] [blame] | 70 | { |
| 71 | struct passwd pwd, *result; |
| 72 | char buf[1024]; |
| 73 | const char *str = target->uid_str; |
| 74 | |
| 75 | target->uid = UINT_MAX; |
| 76 | if (str == NULL) |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 77 | return TARGET_ERRNO__SUCCESS; |
Namhyung Kim | dfe78ad | 2012-05-07 14:09:01 +0900 | [diff] [blame] | 78 | |
| 79 | /* Try user name first */ |
| 80 | getpwnam_r(str, &pwd, buf, sizeof(buf), &result); |
| 81 | |
| 82 | if (result == NULL) { |
| 83 | /* |
| 84 | * The user name not found. Maybe it's a UID number. |
| 85 | */ |
| 86 | char *endptr; |
| 87 | int uid = strtol(str, &endptr, 10); |
| 88 | |
| 89 | if (*endptr != '\0') |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 90 | return TARGET_ERRNO__INVALID_UID; |
Namhyung Kim | dfe78ad | 2012-05-07 14:09:01 +0900 | [diff] [blame] | 91 | |
| 92 | getpwuid_r(uid, &pwd, buf, sizeof(buf), &result); |
| 93 | |
| 94 | if (result == NULL) |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 95 | return TARGET_ERRNO__USER_NOT_FOUND; |
Namhyung Kim | dfe78ad | 2012-05-07 14:09:01 +0900 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | target->uid = result->pw_uid; |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 99 | return TARGET_ERRNO__SUCCESS; |
Namhyung Kim | dfe78ad | 2012-05-07 14:09:01 +0900 | [diff] [blame] | 100 | } |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 101 | |
| 102 | /* |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 103 | * This must have a same ordering as the enum target_errno. |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 104 | */ |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 105 | static const char *target__error_str[] = { |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 106 | "PID/TID switch overriding CPU", |
| 107 | "PID/TID switch overriding UID", |
| 108 | "UID switch overriding CPU", |
| 109 | "PID/TID switch overriding SYSTEM", |
| 110 | "UID switch overriding SYSTEM", |
Adrian Hunter | 3aa5939 | 2013-11-15 15:52:29 +0200 | [diff] [blame] | 111 | "SYSTEM/CPU switch overriding PER-THREAD", |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 112 | "Invalid User: %s", |
| 113 | "Problems obtaining information for user %s", |
| 114 | }; |
| 115 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 116 | int target__strerror(struct target *target, int errnum, |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 117 | char *buf, size_t buflen) |
| 118 | { |
| 119 | int idx; |
| 120 | const char *msg; |
| 121 | |
Namhyung Kim | 0ecf4f0 | 2012-07-26 10:50:10 +0900 | [diff] [blame] | 122 | BUG_ON(buflen == 0); |
Kirill A. Shutemov | 4cc49d4 | 2012-07-24 00:06:54 +0300 | [diff] [blame] | 123 | |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 124 | if (errnum >= 0) { |
Arnaldo Carvalho de Melo | ce92834 | 2016-07-29 11:54:35 -0300 | [diff] [blame] | 125 | str_error_r(errnum, buf, buflen); |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 129 | if (errnum < __TARGET_ERRNO__START || errnum >= __TARGET_ERRNO__END) |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 130 | return -1; |
| 131 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 132 | idx = errnum - __TARGET_ERRNO__START; |
| 133 | msg = target__error_str[idx]; |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 134 | |
| 135 | switch (errnum) { |
Adrian Hunter | 3aa5939 | 2013-11-15 15:52:29 +0200 | [diff] [blame] | 136 | case TARGET_ERRNO__PID_OVERRIDE_CPU ... |
| 137 | TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD: |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 138 | snprintf(buf, buflen, "%s", msg); |
| 139 | break; |
| 140 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 141 | case TARGET_ERRNO__INVALID_UID: |
| 142 | case TARGET_ERRNO__USER_NOT_FOUND: |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 143 | snprintf(buf, buflen, msg, target->uid_str); |
| 144 | break; |
| 145 | |
| 146 | default: |
| 147 | /* cannot reach here */ |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |