perf target: Separate parse_uid into its own function
authorIan Rogers <irogers@google.com>
Wed, 4 Jun 2025 17:45:36 +0000 (10:45 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Mon, 9 Jun 2025 18:18:17 +0000 (11:18 -0700)
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>
tools/perf/util/target.c
tools/perf/util/target.h

index 0f383418e3df5e568fe8459253f851ddf32e947a..f3ad59ccfa998cb21133297e79c505ab982f9de3 100644 (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;
 
index 2ee2cc30340f0f1cc89018e508a6a8e64dbd5b4a..e082bda990fbc4fbcf5ae6536a8a3eb11e633a22 100644 (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);