Skip to content

Commit 5ddf4c3

Browse files
captain5050namhyung
authored andcommitted
perf target: Separate parse_uid into its own function
Allow parse_uid to be called without a struct target. Rather than have two errors, remove TARGET_ERRNO__USER_NOT_FOUND and use TARGET_ERRNO__INVALID_UID as the handling is identical. Signed-off-by: Ian Rogers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent 8b99e2f commit 5ddf4c3

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

tools/perf/util/target.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,13 @@ enum target_errno target__validate(struct target *target)
9494
return ret;
9595
}
9696

97-
enum target_errno target__parse_uid(struct target *target)
97+
uid_t parse_uid(const char *str)
9898
{
9999
struct passwd pwd, *result;
100100
char buf[1024];
101-
const char *str = target->uid_str;
102101

103-
target->uid = UINT_MAX;
104102
if (str == NULL)
105-
return TARGET_ERRNO__SUCCESS;
103+
return UINT_MAX;
106104

107105
/* Try user name first */
108106
getpwnam_r(str, &pwd, buf, sizeof(buf), &result);
@@ -115,16 +113,22 @@ enum target_errno target__parse_uid(struct target *target)
115113
int uid = strtol(str, &endptr, 10);
116114

117115
if (*endptr != '\0')
118-
return TARGET_ERRNO__INVALID_UID;
116+
return UINT_MAX;
119117

120118
getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
121119

122120
if (result == NULL)
123-
return TARGET_ERRNO__USER_NOT_FOUND;
121+
return UINT_MAX;
124122
}
125123

126-
target->uid = result->pw_uid;
127-
return TARGET_ERRNO__SUCCESS;
124+
return result->pw_uid;
125+
}
126+
127+
enum target_errno target__parse_uid(struct target *target)
128+
{
129+
target->uid = parse_uid(target->uid_str);
130+
131+
return target->uid != UINT_MAX ? TARGET_ERRNO__SUCCESS : TARGET_ERRNO__INVALID_UID;
128132
}
129133

130134
/*
@@ -142,7 +146,6 @@ static const char *target__error_str[] = {
142146
"BPF switch overriding UID",
143147
"BPF switch overriding THREAD",
144148
"Invalid User: %s",
145-
"Problems obtaining information for user %s",
146149
};
147150

148151
int target__strerror(struct target *target, int errnum,
@@ -171,7 +174,6 @@ int target__strerror(struct target *target, int errnum,
171174
break;
172175

173176
case TARGET_ERRNO__INVALID_UID:
174-
case TARGET_ERRNO__USER_NOT_FOUND:
175177
snprintf(buf, buflen, msg, target->uid_str);
176178
break;
177179

tools/perf/util/target.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ enum target_errno {
4848

4949
/* for target__parse_uid() */
5050
TARGET_ERRNO__INVALID_UID,
51-
TARGET_ERRNO__USER_NOT_FOUND,
5251

5352
__TARGET_ERRNO__END,
5453
};
5554

5655
enum target_errno target__validate(struct target *target);
56+
57+
uid_t parse_uid(const char *str);
5758
enum target_errno target__parse_uid(struct target *target);
5859

5960
int target__strerror(struct target *target, int errnum, char *buf, size_t buflen);

0 commit comments

Comments
 (0)