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> |
| 13 | |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 14 | |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 15 | enum perf_target_errno perf_target__validate(struct perf_target *target) |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 16 | { |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 17 | enum perf_target_errno ret = PERF_ERRNO_TARGET__SUCCESS; |
| 18 | |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 19 | if (target->pid) |
| 20 | target->tid = target->pid; |
| 21 | |
| 22 | /* CPU and PID are mutually exclusive */ |
| 23 | if (target->tid && target->cpu_list) { |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 24 | target->cpu_list = NULL; |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 25 | if (ret == PERF_ERRNO_TARGET__SUCCESS) |
| 26 | ret = PERF_ERRNO_TARGET__PID_OVERRIDE_CPU; |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /* UID and PID are mutually exclusive */ |
| 30 | if (target->tid && target->uid_str) { |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 31 | target->uid_str = NULL; |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 32 | if (ret == PERF_ERRNO_TARGET__SUCCESS) |
| 33 | ret = PERF_ERRNO_TARGET__PID_OVERRIDE_UID; |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* UID and CPU are mutually exclusive */ |
| 37 | if (target->uid_str && target->cpu_list) { |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 38 | target->cpu_list = NULL; |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 39 | if (ret == PERF_ERRNO_TARGET__SUCCESS) |
| 40 | ret = PERF_ERRNO_TARGET__UID_OVERRIDE_CPU; |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 41 | } |
| 42 | |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 43 | /* PID and SYSTEM are mutually exclusive */ |
| 44 | if (target->tid && target->system_wide) { |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 45 | target->system_wide = false; |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 46 | if (ret == PERF_ERRNO_TARGET__SUCCESS) |
| 47 | ret = PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM; |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 48 | } |
Namhyung Kim | 60bbdda | 2012-05-07 14:09:00 +0900 | [diff] [blame] | 49 | |
| 50 | /* UID and SYSTEM are mutually exclusive */ |
| 51 | if (target->uid_str && target->system_wide) { |
| 52 | target->system_wide = false; |
| 53 | if (ret == PERF_ERRNO_TARGET__SUCCESS) |
| 54 | ret = PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM; |
| 55 | } |
| 56 | |
| 57 | return ret; |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 58 | } |
Namhyung Kim | dfe78ad | 2012-05-07 14:09:01 +0900 | [diff] [blame^] | 59 | |
| 60 | enum perf_target_errno perf_target__parse_uid(struct perf_target *target) |
| 61 | { |
| 62 | struct passwd pwd, *result; |
| 63 | char buf[1024]; |
| 64 | const char *str = target->uid_str; |
| 65 | |
| 66 | target->uid = UINT_MAX; |
| 67 | if (str == NULL) |
| 68 | return PERF_ERRNO_TARGET__SUCCESS; |
| 69 | |
| 70 | /* Try user name first */ |
| 71 | getpwnam_r(str, &pwd, buf, sizeof(buf), &result); |
| 72 | |
| 73 | if (result == NULL) { |
| 74 | /* |
| 75 | * The user name not found. Maybe it's a UID number. |
| 76 | */ |
| 77 | char *endptr; |
| 78 | int uid = strtol(str, &endptr, 10); |
| 79 | |
| 80 | if (*endptr != '\0') |
| 81 | return PERF_ERRNO_TARGET__INVALID_UID; |
| 82 | |
| 83 | getpwuid_r(uid, &pwd, buf, sizeof(buf), &result); |
| 84 | |
| 85 | if (result == NULL) |
| 86 | return PERF_ERRNO_TARGET__USER_NOT_FOUND; |
| 87 | } |
| 88 | |
| 89 | target->uid = result->pw_uid; |
| 90 | return PERF_ERRNO_TARGET__SUCCESS; |
| 91 | } |