blob: 3c778a07b7cc78de8d92ec188e9bb446c40798bd [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"
10#include "debug.h"
11
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090012#include <pwd.h>
Namhyung Kim16ad2ff2012-05-07 14:09:02 +090013#include <string.h>
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090014
Namhyung Kim12864b32012-04-26 14:15:22 +090015
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030016enum target_errno target__validate(struct target *target)
Namhyung Kim12864b32012-04-26 14:15:22 +090017{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030018 enum target_errno ret = TARGET_ERRNO__SUCCESS;
Namhyung Kim60bbdda2012-05-07 14:09:00 +090019
Namhyung Kim12864b32012-04-26 14:15:22 +090020 if (target->pid)
21 target->tid = target->pid;
22
23 /* CPU and PID are mutually exclusive */
24 if (target->tid && target->cpu_list) {
Namhyung Kim12864b32012-04-26 14:15:22 +090025 target->cpu_list = NULL;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030026 if (ret == TARGET_ERRNO__SUCCESS)
27 ret = TARGET_ERRNO__PID_OVERRIDE_CPU;
Namhyung Kim12864b32012-04-26 14:15:22 +090028 }
29
30 /* UID and PID are mutually exclusive */
31 if (target->tid && target->uid_str) {
Namhyung Kim12864b32012-04-26 14:15:22 +090032 target->uid_str = NULL;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030033 if (ret == TARGET_ERRNO__SUCCESS)
34 ret = TARGET_ERRNO__PID_OVERRIDE_UID;
Namhyung Kim12864b32012-04-26 14:15:22 +090035 }
36
37 /* UID and CPU are mutually exclusive */
38 if (target->uid_str && target->cpu_list) {
Namhyung Kim12864b32012-04-26 14:15:22 +090039 target->cpu_list = NULL;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030040 if (ret == TARGET_ERRNO__SUCCESS)
41 ret = TARGET_ERRNO__UID_OVERRIDE_CPU;
Namhyung Kim12864b32012-04-26 14:15:22 +090042 }
43
Namhyung Kim60bbdda2012-05-07 14:09:00 +090044 /* PID and SYSTEM are mutually exclusive */
45 if (target->tid && target->system_wide) {
Namhyung Kim12864b32012-04-26 14:15:22 +090046 target->system_wide = false;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030047 if (ret == TARGET_ERRNO__SUCCESS)
48 ret = TARGET_ERRNO__PID_OVERRIDE_SYSTEM;
Namhyung Kim12864b32012-04-26 14:15:22 +090049 }
Namhyung Kim60bbdda2012-05-07 14:09:00 +090050
51 /* UID and SYSTEM are mutually exclusive */
52 if (target->uid_str && target->system_wide) {
53 target->system_wide = false;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030054 if (ret == TARGET_ERRNO__SUCCESS)
55 ret = TARGET_ERRNO__UID_OVERRIDE_SYSTEM;
Namhyung Kim60bbdda2012-05-07 14:09:00 +090056 }
57
58 return ret;
Namhyung Kim12864b32012-04-26 14:15:22 +090059}
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090060
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030061enum target_errno target__parse_uid(struct target *target)
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090062{
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)
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030069 return TARGET_ERRNO__SUCCESS;
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090070
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')
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030082 return TARGET_ERRNO__INVALID_UID;
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090083
84 getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
85
86 if (result == NULL)
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030087 return TARGET_ERRNO__USER_NOT_FOUND;
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090088 }
89
90 target->uid = result->pw_uid;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030091 return TARGET_ERRNO__SUCCESS;
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090092}
Namhyung Kim16ad2ff2012-05-07 14:09:02 +090093
94/*
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030095 * This must have a same ordering as the enum target_errno.
Namhyung Kim16ad2ff2012-05-07 14:09:02 +090096 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030097static const char *target__error_str[] = {
Namhyung Kim16ad2ff2012-05-07 14:09:02 +090098 "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
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300107int target__strerror(struct target *target, int errnum,
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900108 char *buf, size_t buflen)
109{
110 int idx;
111 const char *msg;
112
Namhyung Kim0ecf4f02012-07-26 10:50:10 +0900113 BUG_ON(buflen == 0);
Kirill A. Shutemov4cc49d42012-07-24 00:06:54 +0300114
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900115 if (errnum >= 0) {
Kirill A. Shutemov4cc49d42012-07-24 00:06:54 +0300116 const char *err = strerror_r(errnum, buf, buflen);
117
118 if (err != buf) {
119 size_t len = strlen(err);
Irina Tirdea60ff92f2012-08-29 01:22:16 +0300120 memcpy(buf, err, min(buflen - 1, len));
121 *(buf + min(buflen - 1, len)) = '\0';
Kirill A. Shutemov4cc49d42012-07-24 00:06:54 +0300122 }
123
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900124 return 0;
125 }
126
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300127 if (errnum < __TARGET_ERRNO__START || errnum >= __TARGET_ERRNO__END)
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900128 return -1;
129
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300130 idx = errnum - __TARGET_ERRNO__START;
131 msg = target__error_str[idx];
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900132
133 switch (errnum) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300134 case TARGET_ERRNO__PID_OVERRIDE_CPU ... TARGET_ERRNO__UID_OVERRIDE_SYSTEM:
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900135 snprintf(buf, buflen, "%s", msg);
136 break;
137
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300138 case TARGET_ERRNO__INVALID_UID:
139 case TARGET_ERRNO__USER_NOT_FOUND:
Namhyung Kim16ad2ff2012-05-07 14:09:02 +0900140 snprintf(buf, buflen, msg, target->uid_str);
141 break;
142
143 default:
144 /* cannot reach here */
145 break;
146 }
147
148 return 0;
149}