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 <irogers@google.com>
Link: https://lore.kernel.org/r/20250604174545.2853620-3-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Ian Rogers
2025-06-04 10:45:36 -07:00
committed by Namhyung Kim
parent 8b99e2f7a9
commit 5ddf4c3a17
2 changed files with 14 additions and 11 deletions

View File

@@ -94,15 +94,13 @@ enum target_errno target__validate(struct target *target)
return ret;
}
enum target_errno target__parse_uid(struct target *target)
uid_t parse_uid(const char *str)
{
struct passwd pwd, *result;
char buf[1024];
const char *str = target->uid_str;
target->uid = UINT_MAX;
if (str == NULL)
return TARGET_ERRNO__SUCCESS;
return UINT_MAX;
/* Try user name first */
getpwnam_r(str, &pwd, buf, sizeof(buf), &result);
@@ -115,16 +113,22 @@ enum target_errno target__parse_uid(struct target *target)
int uid = strtol(str, &endptr, 10);
if (*endptr != '\0')
return TARGET_ERRNO__INVALID_UID;
return UINT_MAX;
getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
if (result == NULL)
return TARGET_ERRNO__USER_NOT_FOUND;
return UINT_MAX;
}
target->uid = result->pw_uid;
return TARGET_ERRNO__SUCCESS;
return result->pw_uid;
}
enum target_errno target__parse_uid(struct target *target)
{
target->uid = parse_uid(target->uid_str);
return target->uid != UINT_MAX ? TARGET_ERRNO__SUCCESS : TARGET_ERRNO__INVALID_UID;
}
/*
@@ -142,7 +146,6 @@ static const char *target__error_str[] = {
"BPF switch overriding UID",
"BPF switch overriding THREAD",
"Invalid User: %s",
"Problems obtaining information for user %s",
};
int target__strerror(struct target *target, int errnum,
@@ -171,7 +174,6 @@ int target__strerror(struct target *target, int errnum,
break;
case TARGET_ERRNO__INVALID_UID:
case TARGET_ERRNO__USER_NOT_FOUND:
snprintf(buf, buflen, msg, target->uid_str);
break;

View File

@@ -48,12 +48,13 @@ enum target_errno {
/* for target__parse_uid() */
TARGET_ERRNO__INVALID_UID,
TARGET_ERRNO__USER_NOT_FOUND,
__TARGET_ERRNO__END,
};
enum target_errno target__validate(struct target *target);
uid_t parse_uid(const char *str);
enum target_errno target__parse_uid(struct target *target);
int target__strerror(struct target *target, int errnum, char *buf, size_t buflen);