Commit 5ddf4c3a authored by Ian Rogers's avatar Ian Rogers Committed by Namhyung Kim
Browse files

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: default avatarIan Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250604174545.2853620-3-irogers@google.com


Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
parent 8b99e2f7
Loading
Loading
Loading
Loading
+12 −10
Original line number Diff line number Diff line
@@ -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;
	}

	return result->pw_uid;
}

	target->uid = result->pw_uid;
	return TARGET_ERRNO__SUCCESS;
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;

+2 −1
Original line number Diff line number Diff line
@@ -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);