blob: 02a6bedb69a31ffd9c539bdcd8e8f678decd14f6 [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>
13
Namhyung Kim12864b32012-04-26 14:15:22 +090014
Namhyung Kim60bbdda2012-05-07 14:09:00 +090015enum perf_target_errno perf_target__validate(struct perf_target *target)
Namhyung Kim12864b32012-04-26 14:15:22 +090016{
Namhyung Kim60bbdda2012-05-07 14:09:00 +090017 enum perf_target_errno ret = PERF_ERRNO_TARGET__SUCCESS;
18
Namhyung Kim12864b32012-04-26 14:15:22 +090019 if (target->pid)
20 target->tid = target->pid;
21
22 /* CPU and PID are mutually exclusive */
23 if (target->tid && target->cpu_list) {
Namhyung Kim12864b32012-04-26 14:15:22 +090024 target->cpu_list = NULL;
Namhyung Kim60bbdda2012-05-07 14:09:00 +090025 if (ret == PERF_ERRNO_TARGET__SUCCESS)
26 ret = PERF_ERRNO_TARGET__PID_OVERRIDE_CPU;
Namhyung Kim12864b32012-04-26 14:15:22 +090027 }
28
29 /* UID and PID are mutually exclusive */
30 if (target->tid && target->uid_str) {
Namhyung Kim12864b32012-04-26 14:15:22 +090031 target->uid_str = NULL;
Namhyung Kim60bbdda2012-05-07 14:09:00 +090032 if (ret == PERF_ERRNO_TARGET__SUCCESS)
33 ret = PERF_ERRNO_TARGET__PID_OVERRIDE_UID;
Namhyung Kim12864b32012-04-26 14:15:22 +090034 }
35
36 /* UID and CPU are mutually exclusive */
37 if (target->uid_str && target->cpu_list) {
Namhyung Kim12864b32012-04-26 14:15:22 +090038 target->cpu_list = NULL;
Namhyung Kim60bbdda2012-05-07 14:09:00 +090039 if (ret == PERF_ERRNO_TARGET__SUCCESS)
40 ret = PERF_ERRNO_TARGET__UID_OVERRIDE_CPU;
Namhyung Kim12864b32012-04-26 14:15:22 +090041 }
42
Namhyung Kim60bbdda2012-05-07 14:09:00 +090043 /* PID and SYSTEM are mutually exclusive */
44 if (target->tid && target->system_wide) {
Namhyung Kim12864b32012-04-26 14:15:22 +090045 target->system_wide = false;
Namhyung Kim60bbdda2012-05-07 14:09:00 +090046 if (ret == PERF_ERRNO_TARGET__SUCCESS)
47 ret = PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM;
Namhyung Kim12864b32012-04-26 14:15:22 +090048 }
Namhyung Kim60bbdda2012-05-07 14:09:00 +090049
50 /* UID and SYSTEM are mutually exclusive */
51 if (target->uid_str && target->system_wide) {
52 target->system_wide = false;
53 if (ret == PERF_ERRNO_TARGET__SUCCESS)
54 ret = PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM;
55 }
56
57 return ret;
Namhyung Kim12864b32012-04-26 14:15:22 +090058}
Namhyung Kimdfe78ad2012-05-07 14:09:01 +090059
60enum 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}