blob: 21c4d9b23c249e6e59db3ca8738b69444de2e8c8 [file] [log] [blame]
Namhyung Kim12864b32012-04-26 14:15:22 +09001/*
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"
Arnaldo Carvalho de Melo175729f2016-07-07 11:38:09 -030010#include "util.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090011#include "debug.h"
12
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090013#include <pwd.h>
Namhyung Kim16ad2ff2012-05-07 14:09:02 +090014#include <string.h>
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090015
Namhyung Kim12864b32012-04-26 14:15:22 +090016
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030017enum target_errno target__validate(struct target *target)
Namhyung Kim12864b32012-04-26 14:15:22 +090018{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030019 enum target_errno ret = TARGET_ERRNO__SUCCESS;
Namhyung Kim60bbdda2012-05-07 14:09:00 +090020
Namhyung Kim12864b32012-04-26 14:15:22 +090021 if (target->pid)
22 target->tid = target->pid;
23
24 /* CPU and PID are mutually exclusive */
25 if (target->tid && target->cpu_list) {
Namhyung Kim12864b32012-04-26 14:15:22 +090026 target->cpu_list = NULL;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030027 if (ret == TARGET_ERRNO__SUCCESS)
28 ret = TARGET_ERRNO__PID_OVERRIDE_CPU;
Namhyung Kim12864b32012-04-26 14:15:22 +090029 }
30
31 /* UID and PID are mutually exclusive */
32 if (target->tid && target->uid_str) {
Namhyung Kim12864b32012-04-26 14:15:22 +090033 target->uid_str = NULL;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030034 if (ret == TARGET_ERRNO__SUCCESS)
35 ret = TARGET_ERRNO__PID_OVERRIDE_UID;
Namhyung Kim12864b32012-04-26 14:15:22 +090036 }
37
38 /* UID and CPU are mutually exclusive */
39 if (target->uid_str && target->cpu_list) {
Namhyung Kim12864b32012-04-26 14:15:22 +090040 target->cpu_list = NULL;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030041 if (ret == TARGET_ERRNO__SUCCESS)
42 ret = TARGET_ERRNO__UID_OVERRIDE_CPU;
Namhyung Kim12864b32012-04-26 14:15:22 +090043 }
44
Namhyung Kim60bbdda2012-05-07 14:09:00 +090045 /* PID and SYSTEM are mutually exclusive */
46 if (target->tid && target->system_wide) {
Namhyung Kim12864b32012-04-26 14:15:22 +090047 target->system_wide = false;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030048 if (ret == TARGET_ERRNO__SUCCESS)
49 ret = TARGET_ERRNO__PID_OVERRIDE_SYSTEM;
Namhyung Kim12864b32012-04-26 14:15:22 +090050 }
Namhyung Kim60bbdda2012-05-07 14:09:00 +090051
52 /* UID and SYSTEM are mutually exclusive */
53 if (target->uid_str && target->system_wide) {
54 target->system_wide = false;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030055 if (ret == TARGET_ERRNO__SUCCESS)
56 ret = TARGET_ERRNO__UID_OVERRIDE_SYSTEM;
Namhyung Kim60bbdda2012-05-07 14:09:00 +090057 }
58
Adrian Hunter3aa59392013-11-15 15:52:29 +020059 /* 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
Namhyung Kim60bbdda2012-05-07 14:09:00 +090066 return ret;
Namhyung Kim12864b32012-04-26 14:15:22 +090067}
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090068
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030069enum target_errno target__parse_uid(struct target *target)
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090070{
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)
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030077 return TARGET_ERRNO__SUCCESS;
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090078
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')
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030090 return TARGET_ERRNO__INVALID_UID;
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090091
92 getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
93
94 if (result == NULL)
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030095 return TARGET_ERRNO__USER_NOT_FOUND;
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090096 }
97
98 target->uid = result->pw_uid;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030099 return TARGET_ERRNO__SUCCESS;
Namhyung Kimdfe78ad2012-05-07 14:09:01 +0900100}
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900101
102/*
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300103 * This must have a same ordering as the enum target_errno.
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900104 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300105static const char *target__error_str[] = {
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900106 "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",
Adrian Hunter3aa59392013-11-15 15:52:29 +0200111 "SYSTEM/CPU switch overriding PER-THREAD",
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900112 "Invalid User: %s",
113 "Problems obtaining information for user %s",
114};
115
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300116int target__strerror(struct target *target, int errnum,
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900117 char *buf, size_t buflen)
118{
119 int idx;
120 const char *msg;
121
Namhyung Kim0ecf4f02012-07-26 10:50:10 +0900122 BUG_ON(buflen == 0);
Kirill A. Shutemov4cc49d42012-07-24 00:06:54 +0300123
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900124 if (errnum >= 0) {
Arnaldo Carvalho de Meloce928342016-07-29 11:54:35 -0300125 str_error_r(errnum, buf, buflen);
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900126 return 0;
127 }
128
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300129 if (errnum < __TARGET_ERRNO__START || errnum >= __TARGET_ERRNO__END)
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900130 return -1;
131
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300132 idx = errnum - __TARGET_ERRNO__START;
133 msg = target__error_str[idx];
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900134
135 switch (errnum) {
Adrian Hunter3aa59392013-11-15 15:52:29 +0200136 case TARGET_ERRNO__PID_OVERRIDE_CPU ...
137 TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD:
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900138 snprintf(buf, buflen, "%s", msg);
139 break;
140
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300141 case TARGET_ERRNO__INVALID_UID:
142 case TARGET_ERRNO__USER_NOT_FOUND:
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900143 snprintf(buf, buflen, msg, target->uid_str);
144 break;
145
146 default:
147 /* cannot reach here */
148 break;
149 }
150
151 return 0;
152}