Commit | Line | Data |
---|---|---|
12864b31 NK |
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 | ||
dfe78ada NK |
12 | #include <pwd.h> |
13 | ||
12864b31 | 14 | |
60bbddaa | 15 | enum perf_target_errno perf_target__validate(struct perf_target *target) |
12864b31 | 16 | { |
60bbddaa NK |
17 | enum perf_target_errno ret = PERF_ERRNO_TARGET__SUCCESS; |
18 | ||
12864b31 NK |
19 | if (target->pid) |
20 | target->tid = target->pid; | |
21 | ||
22 | /* CPU and PID are mutually exclusive */ | |
23 | if (target->tid && target->cpu_list) { | |
12864b31 | 24 | target->cpu_list = NULL; |
60bbddaa NK |
25 | if (ret == PERF_ERRNO_TARGET__SUCCESS) |
26 | ret = PERF_ERRNO_TARGET__PID_OVERRIDE_CPU; | |
12864b31 NK |
27 | } |
28 | ||
29 | /* UID and PID are mutually exclusive */ | |
30 | if (target->tid && target->uid_str) { | |
12864b31 | 31 | target->uid_str = NULL; |
60bbddaa NK |
32 | if (ret == PERF_ERRNO_TARGET__SUCCESS) |
33 | ret = PERF_ERRNO_TARGET__PID_OVERRIDE_UID; | |
12864b31 NK |
34 | } |
35 | ||
36 | /* UID and CPU are mutually exclusive */ | |
37 | if (target->uid_str && target->cpu_list) { | |
12864b31 | 38 | target->cpu_list = NULL; |
60bbddaa NK |
39 | if (ret == PERF_ERRNO_TARGET__SUCCESS) |
40 | ret = PERF_ERRNO_TARGET__UID_OVERRIDE_CPU; | |
12864b31 NK |
41 | } |
42 | ||
60bbddaa NK |
43 | /* PID and SYSTEM are mutually exclusive */ |
44 | if (target->tid && target->system_wide) { | |
12864b31 | 45 | target->system_wide = false; |
60bbddaa NK |
46 | if (ret == PERF_ERRNO_TARGET__SUCCESS) |
47 | ret = PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM; | |
12864b31 | 48 | } |
60bbddaa NK |
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; | |
12864b31 | 58 | } |
dfe78ada NK |
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 | } |