fix a kmap leak in virtio_console
[linux/fpc-iii.git] / tools / perf / util / target.c
blobe74c5963dc7a9db7038e4ad3f5f7de1ed490f20f
1 /*
2 * Helper functions for handling target threads/cpus
4 * Copyright (C) 2012, LG Electronics, Namhyung Kim <namhyung.kim@lge.com>
6 * Released under the GPL v2.
7 */
9 #include "target.h"
10 #include "debug.h"
12 #include <pwd.h>
13 #include <string.h>
16 enum target_errno target__validate(struct target *target)
18 enum target_errno ret = TARGET_ERRNO__SUCCESS;
20 if (target->pid)
21 target->tid = target->pid;
23 /* CPU and PID are mutually exclusive */
24 if (target->tid && target->cpu_list) {
25 target->cpu_list = NULL;
26 if (ret == TARGET_ERRNO__SUCCESS)
27 ret = TARGET_ERRNO__PID_OVERRIDE_CPU;
30 /* UID and PID are mutually exclusive */
31 if (target->tid && target->uid_str) {
32 target->uid_str = NULL;
33 if (ret == TARGET_ERRNO__SUCCESS)
34 ret = TARGET_ERRNO__PID_OVERRIDE_UID;
37 /* UID and CPU are mutually exclusive */
38 if (target->uid_str && target->cpu_list) {
39 target->cpu_list = NULL;
40 if (ret == TARGET_ERRNO__SUCCESS)
41 ret = TARGET_ERRNO__UID_OVERRIDE_CPU;
44 /* PID and SYSTEM are mutually exclusive */
45 if (target->tid && target->system_wide) {
46 target->system_wide = false;
47 if (ret == TARGET_ERRNO__SUCCESS)
48 ret = TARGET_ERRNO__PID_OVERRIDE_SYSTEM;
51 /* UID and SYSTEM are mutually exclusive */
52 if (target->uid_str && target->system_wide) {
53 target->system_wide = false;
54 if (ret == TARGET_ERRNO__SUCCESS)
55 ret = TARGET_ERRNO__UID_OVERRIDE_SYSTEM;
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;
65 return ret;
68 enum target_errno target__parse_uid(struct target *target)
70 struct passwd pwd, *result;
71 char buf[1024];
72 const char *str = target->uid_str;
74 target->uid = UINT_MAX;
75 if (str == NULL)
76 return TARGET_ERRNO__SUCCESS;
78 /* Try user name first */
79 getpwnam_r(str, &pwd, buf, sizeof(buf), &result);
81 if (result == NULL) {
83 * The user name not found. Maybe it's a UID number.
85 char *endptr;
86 int uid = strtol(str, &endptr, 10);
88 if (*endptr != '\0')
89 return TARGET_ERRNO__INVALID_UID;
91 getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
93 if (result == NULL)
94 return TARGET_ERRNO__USER_NOT_FOUND;
97 target->uid = result->pw_uid;
98 return TARGET_ERRNO__SUCCESS;
102 * This must have a same ordering as the enum target_errno.
104 static const char *target__error_str[] = {
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",
110 "SYSTEM/CPU switch overriding PER-THREAD",
111 "Invalid User: %s",
112 "Problems obtaining information for user %s",
115 int target__strerror(struct target *target, int errnum,
116 char *buf, size_t buflen)
118 int idx;
119 const char *msg;
121 BUG_ON(buflen == 0);
123 if (errnum >= 0) {
124 const char *err = strerror_r(errnum, buf, buflen);
126 if (err != buf) {
127 size_t len = strlen(err);
128 memcpy(buf, err, min(buflen - 1, len));
129 *(buf + min(buflen - 1, len)) = '\0';
132 return 0;
135 if (errnum < __TARGET_ERRNO__START || errnum >= __TARGET_ERRNO__END)
136 return -1;
138 idx = errnum - __TARGET_ERRNO__START;
139 msg = target__error_str[idx];
141 switch (errnum) {
142 case TARGET_ERRNO__PID_OVERRIDE_CPU ...
143 TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD:
144 snprintf(buf, buflen, "%s", msg);
145 break;
147 case TARGET_ERRNO__INVALID_UID:
148 case TARGET_ERRNO__USER_NOT_FOUND:
149 snprintf(buf, buflen, msg, target->uid_str);
150 break;
152 default:
153 /* cannot reach here */
154 break;
157 return 0;