Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Helper functions for handling target threads/cpus
4 *
5 * Copyright (C) 2012, LG Electronics, Namhyung Kim <namhyung.kim@lge.com>
6 */
7
8#include "target.h"
9
10#include <pwd.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <linux/kernel.h>
15#include <linux/string.h>
16
17enum target_errno target__validate(struct target *target)
18{
19 enum target_errno ret = TARGET_ERRNO__SUCCESS;
20
21 if (target->pid)
22 target->tid = target->pid;
23
24 /* CPU and PID are mutually exclusive */
25 if (target->tid && target->cpu_list) {
26 target->cpu_list = NULL;
27 if (ret == TARGET_ERRNO__SUCCESS)
28 ret = TARGET_ERRNO__PID_OVERRIDE_CPU;
29 }
30
31 /* UID and PID are mutually exclusive */
32 if (target->tid && target->uid_str) {
33 target->uid_str = NULL;
34 if (ret == TARGET_ERRNO__SUCCESS)
35 ret = TARGET_ERRNO__PID_OVERRIDE_UID;
36 }
37
38 /* UID and CPU are mutually exclusive */
39 if (target->uid_str && target->cpu_list) {
40 target->cpu_list = NULL;
41 if (ret == TARGET_ERRNO__SUCCESS)
42 ret = TARGET_ERRNO__UID_OVERRIDE_CPU;
43 }
44
45 /* PID and SYSTEM are mutually exclusive */
46 if (target->tid && target->system_wide) {
47 target->system_wide = false;
48 if (ret == TARGET_ERRNO__SUCCESS)
49 ret = TARGET_ERRNO__PID_OVERRIDE_SYSTEM;
50 }
51
52 /* UID and SYSTEM are mutually exclusive */
53 if (target->uid_str && target->system_wide) {
54 target->system_wide = false;
55 if (ret == TARGET_ERRNO__SUCCESS)
56 ret = TARGET_ERRNO__UID_OVERRIDE_SYSTEM;
57 }
58
59 /* THREAD and SYSTEM/CPU are mutually exclusive */
60 if (target->per_thread && (target->system_wide || target->cpu_list)) {
61 target->per_thread = false;
62 if (ret == TARGET_ERRNO__SUCCESS)
63 ret = TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD;
64 }
65
66 return ret;
67}
68
69enum target_errno target__parse_uid(struct target *target)
70{
71 struct passwd pwd, *result;
72 char buf[1024];
73 const char *str = target->uid_str;
74
75 target->uid = UINT_MAX;
76 if (str == NULL)
77 return TARGET_ERRNO__SUCCESS;
78
79 /* Try user name first */
80 getpwnam_r(str, &pwd, buf, sizeof(buf), &result);
81
82 if (result == NULL) {
83 /*
84 * The user name not found. Maybe it's a UID number.
85 */
86 char *endptr;
87 int uid = strtol(str, &endptr, 10);
88
89 if (*endptr != '\0')
90 return TARGET_ERRNO__INVALID_UID;
91
92 getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
93
94 if (result == NULL)
95 return TARGET_ERRNO__USER_NOT_FOUND;
96 }
97
98 target->uid = result->pw_uid;
99 return TARGET_ERRNO__SUCCESS;
100}
101
102/*
103 * This must have a same ordering as the enum target_errno.
104 */
105static const char *target__error_str[] = {
106 "PID/TID switch overriding CPU",
107 "PID/TID switch overriding UID",
108 "UID switch overriding CPU",
109 "PID/TID switch overriding SYSTEM",
110 "UID switch overriding SYSTEM",
111 "SYSTEM/CPU switch overriding PER-THREAD",
112 "Invalid User: %s",
113 "Problems obtaining information for user %s",
114};
115
116int target__strerror(struct target *target, int errnum,
117 char *buf, size_t buflen)
118{
119 int idx;
120 const char *msg;
121
122 BUG_ON(buflen == 0);
123
124 if (errnum >= 0) {
125 str_error_r(errnum, buf, buflen);
126 return 0;
127 }
128
129 if (errnum < __TARGET_ERRNO__START || errnum >= __TARGET_ERRNO__END)
130 return -1;
131
132 idx = errnum - __TARGET_ERRNO__START;
133 msg = target__error_str[idx];
134
135 switch (errnum) {
136 case TARGET_ERRNO__PID_OVERRIDE_CPU ...
137 TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD:
138 snprintf(buf, buflen, "%s", msg);
139 break;
140
141 case TARGET_ERRNO__INVALID_UID:
142 case TARGET_ERRNO__USER_NOT_FOUND:
143 snprintf(buf, buflen, msg, target->uid_str);
144 break;
145
146 default:
147 /* cannot reach here */
148 break;
149 }
150
151 return 0;
152}
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
12#include <pwd.h>
13#include <string.h>
14
15
16enum perf_target_errno perf_target__validate(struct perf_target *target)
17{
18 enum perf_target_errno ret = PERF_ERRNO_TARGET__SUCCESS;
19
20 if (target->pid)
21 target->tid = target->pid;
22
23 /* CPU and PID are mutually exclusive */
24 if (target->tid && target->cpu_list) {
25 target->cpu_list = NULL;
26 if (ret == PERF_ERRNO_TARGET__SUCCESS)
27 ret = PERF_ERRNO_TARGET__PID_OVERRIDE_CPU;
28 }
29
30 /* UID and PID are mutually exclusive */
31 if (target->tid && target->uid_str) {
32 target->uid_str = NULL;
33 if (ret == PERF_ERRNO_TARGET__SUCCESS)
34 ret = PERF_ERRNO_TARGET__PID_OVERRIDE_UID;
35 }
36
37 /* UID and CPU are mutually exclusive */
38 if (target->uid_str && target->cpu_list) {
39 target->cpu_list = NULL;
40 if (ret == PERF_ERRNO_TARGET__SUCCESS)
41 ret = PERF_ERRNO_TARGET__UID_OVERRIDE_CPU;
42 }
43
44 /* PID and SYSTEM are mutually exclusive */
45 if (target->tid && target->system_wide) {
46 target->system_wide = false;
47 if (ret == PERF_ERRNO_TARGET__SUCCESS)
48 ret = PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM;
49 }
50
51 /* UID and SYSTEM are mutually exclusive */
52 if (target->uid_str && target->system_wide) {
53 target->system_wide = false;
54 if (ret == PERF_ERRNO_TARGET__SUCCESS)
55 ret = PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM;
56 }
57
58 return ret;
59}
60
61enum perf_target_errno perf_target__parse_uid(struct perf_target *target)
62{
63 struct passwd pwd, *result;
64 char buf[1024];
65 const char *str = target->uid_str;
66
67 target->uid = UINT_MAX;
68 if (str == NULL)
69 return PERF_ERRNO_TARGET__SUCCESS;
70
71 /* Try user name first */
72 getpwnam_r(str, &pwd, buf, sizeof(buf), &result);
73
74 if (result == NULL) {
75 /*
76 * The user name not found. Maybe it's a UID number.
77 */
78 char *endptr;
79 int uid = strtol(str, &endptr, 10);
80
81 if (*endptr != '\0')
82 return PERF_ERRNO_TARGET__INVALID_UID;
83
84 getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
85
86 if (result == NULL)
87 return PERF_ERRNO_TARGET__USER_NOT_FOUND;
88 }
89
90 target->uid = result->pw_uid;
91 return PERF_ERRNO_TARGET__SUCCESS;
92}
93
94/*
95 * This must have a same ordering as the enum perf_target_errno.
96 */
97static const char *perf_target__error_str[] = {
98 "PID/TID switch overriding CPU",
99 "PID/TID switch overriding UID",
100 "UID switch overriding CPU",
101 "PID/TID switch overriding SYSTEM",
102 "UID switch overriding SYSTEM",
103 "Invalid User: %s",
104 "Problems obtaining information for user %s",
105};
106
107int perf_target__strerror(struct perf_target *target, int errnum,
108 char *buf, size_t buflen)
109{
110 int idx;
111 const char *msg;
112
113 if (errnum >= 0) {
114 strerror_r(errnum, buf, buflen);
115 return 0;
116 }
117
118 if (errnum < __PERF_ERRNO_TARGET__START ||
119 errnum >= __PERF_ERRNO_TARGET__END)
120 return -1;
121
122 idx = errnum - __PERF_ERRNO_TARGET__START;
123 msg = perf_target__error_str[idx];
124
125 switch (errnum) {
126 case PERF_ERRNO_TARGET__PID_OVERRIDE_CPU
127 ... PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM:
128 snprintf(buf, buflen, "%s", msg);
129 break;
130
131 case PERF_ERRNO_TARGET__INVALID_UID:
132 case PERF_ERRNO_TARGET__USER_NOT_FOUND:
133 snprintf(buf, buflen, msg, target->uid_str);
134 break;
135
136 default:
137 /* cannot reach here */
138 break;
139 }
140
141 return 0;
142}