diff options
author | Namhyung Kim <namhyung.kim@lge.com> | 2012-05-07 01:09:01 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2012-05-07 15:46:48 -0400 |
commit | dfe78adaaca90417ece98edbd3eb1c9661334406 (patch) | |
tree | 537576730ce54f2ddee4d6266aad7c8f72258a45 /tools | |
parent | 60bbddaaa33865633efa2800702e3b02495a0e94 (diff) |
perf target: Introduce perf_target__parse_uid()
Add and use the modern perf_target__parse_uid() and get rid of the old
parse_target_uid().
Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>
Reviewed-by: David Ahern <dsahern@gmail.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1336367344-28071-5-git-send-email-namhyung.kim@lge.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/perf/builtin-record.c | 4 | ||||
-rw-r--r-- | tools/perf/builtin-top.c | 3 | ||||
-rw-r--r-- | tools/perf/util/target.c | 35 | ||||
-rw-r--r-- | tools/perf/util/target.h | 5 | ||||
-rw-r--r-- | tools/perf/util/usage.c | 31 | ||||
-rw-r--r-- | tools/perf/util/util.h | 3 |
6 files changed, 42 insertions, 39 deletions
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index d16590942cec..d26a279796d9 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c | |||
@@ -886,9 +886,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) | |||
886 | 886 | ||
887 | perf_target__validate(&rec->opts.target); | 887 | perf_target__validate(&rec->opts.target); |
888 | 888 | ||
889 | rec->opts.target.uid = parse_target_uid(rec->opts.target.uid_str); | 889 | if (perf_target__parse_uid(&rec->opts.target) < 0) |
890 | if (rec->opts.target.uid_str != NULL && | ||
891 | rec->opts.target.uid == UINT_MAX - 1) | ||
892 | goto out_free_fd; | 890 | goto out_free_fd; |
893 | 891 | ||
894 | if (perf_evlist__create_maps(evsel_list, &rec->opts.target) < 0) | 892 | if (perf_evlist__create_maps(evsel_list, &rec->opts.target) < 0) |
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index e40f86ea3641..c9137ba580d9 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c | |||
@@ -1254,8 +1254,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) | |||
1254 | 1254 | ||
1255 | perf_target__validate(&top.target); | 1255 | perf_target__validate(&top.target); |
1256 | 1256 | ||
1257 | top.target.uid = parse_target_uid(top.target.uid_str); | 1257 | if (perf_target__parse_uid(&top.target) < 0) |
1258 | if (top.target.uid_str != NULL && top.target.uid == UINT_MAX - 1) | ||
1259 | goto out_delete_evlist; | 1258 | goto out_delete_evlist; |
1260 | 1259 | ||
1261 | if (top.target.tid == 0 && top.target.pid == 0 && | 1260 | if (top.target.tid == 0 && top.target.pid == 0 && |
diff --git a/tools/perf/util/target.c b/tools/perf/util/target.c index 5c59dcfc8f8d..02a6bedb69a3 100644 --- a/tools/perf/util/target.c +++ b/tools/perf/util/target.c | |||
@@ -9,6 +9,8 @@ | |||
9 | #include "target.h" | 9 | #include "target.h" |
10 | #include "debug.h" | 10 | #include "debug.h" |
11 | 11 | ||
12 | #include <pwd.h> | ||
13 | |||
12 | 14 | ||
13 | enum perf_target_errno perf_target__validate(struct perf_target *target) | 15 | enum perf_target_errno perf_target__validate(struct perf_target *target) |
14 | { | 16 | { |
@@ -54,3 +56,36 @@ enum perf_target_errno perf_target__validate(struct perf_target *target) | |||
54 | 56 | ||
55 | return ret; | 57 | return ret; |
56 | } | 58 | } |
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 | } | ||
diff --git a/tools/perf/util/target.h b/tools/perf/util/target.h index eb0d2101154a..d4aabdaba42a 100644 --- a/tools/perf/util/target.h +++ b/tools/perf/util/target.h | |||
@@ -33,9 +33,14 @@ enum perf_target_errno { | |||
33 | PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM, | 33 | PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM, |
34 | PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM, | 34 | PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM, |
35 | 35 | ||
36 | /* for perf_target__parse_uid() */ | ||
37 | PERF_ERRNO_TARGET__INVALID_UID, | ||
38 | PERF_ERRNO_TARGET__USER_NOT_FOUND, | ||
39 | |||
36 | __PERF_ERRNO_TARGET__END, | 40 | __PERF_ERRNO_TARGET__END, |
37 | }; | 41 | }; |
38 | 42 | ||
39 | enum perf_target_errno perf_target__validate(struct perf_target *target); | 43 | enum perf_target_errno perf_target__validate(struct perf_target *target); |
44 | enum perf_target_errno perf_target__parse_uid(struct perf_target *target); | ||
40 | 45 | ||
41 | #endif /* _PERF_TARGET_H */ | 46 | #endif /* _PERF_TARGET_H */ |
diff --git a/tools/perf/util/usage.c b/tools/perf/util/usage.c index e851abc22ccc..4007aca8e0ca 100644 --- a/tools/perf/util/usage.c +++ b/tools/perf/util/usage.c | |||
@@ -82,34 +82,3 @@ void warning(const char *warn, ...) | |||
82 | warn_routine(warn, params); | 82 | warn_routine(warn, params); |
83 | va_end(params); | 83 | va_end(params); |
84 | } | 84 | } |
85 | |||
86 | uid_t parse_target_uid(const char *str) | ||
87 | { | ||
88 | struct passwd pwd, *result; | ||
89 | char buf[1024]; | ||
90 | |||
91 | if (str == NULL) | ||
92 | return UINT_MAX; | ||
93 | |||
94 | getpwnam_r(str, &pwd, buf, sizeof(buf), &result); | ||
95 | |||
96 | if (result == NULL) { | ||
97 | char *endptr; | ||
98 | int uid = strtol(str, &endptr, 10); | ||
99 | |||
100 | if (*endptr != '\0') { | ||
101 | ui__error("Invalid user %s\n", str); | ||
102 | return UINT_MAX - 1; | ||
103 | } | ||
104 | |||
105 | getpwuid_r(uid, &pwd, buf, sizeof(buf), &result); | ||
106 | |||
107 | if (result == NULL) { | ||
108 | ui__error("Problems obtaining information for user %s\n", | ||
109 | str); | ||
110 | return UINT_MAX - 1; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | return result->pw_uid; | ||
115 | } | ||
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 52be74c359d3..27a11a78ad39 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h | |||
@@ -74,7 +74,6 @@ | |||
74 | #include <netinet/tcp.h> | 74 | #include <netinet/tcp.h> |
75 | #include <arpa/inet.h> | 75 | #include <arpa/inet.h> |
76 | #include <netdb.h> | 76 | #include <netdb.h> |
77 | #include <pwd.h> | ||
78 | #include <inttypes.h> | 77 | #include <inttypes.h> |
79 | #include "../../../include/linux/magic.h" | 78 | #include "../../../include/linux/magic.h" |
80 | #include "types.h" | 79 | #include "types.h" |
@@ -249,8 +248,6 @@ struct perf_event_attr; | |||
249 | 248 | ||
250 | void event_attr_init(struct perf_event_attr *attr); | 249 | void event_attr_init(struct perf_event_attr *attr); |
251 | 250 | ||
252 | uid_t parse_target_uid(const char *str); | ||
253 | |||
254 | #define _STR(x) #x | 251 | #define _STR(x) #x |
255 | #define STR(x) _STR(x) | 252 | #define STR(x) _STR(x) |
256 | 253 | ||