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