Cache user from getpwuid because it can be very expensive on some

platforms. From Ben Maurer in GitHub issue 4973.
This commit is contained in:
nicm
2026-04-04 17:13:07 +00:00
parent 7f2ac9c871
commit cad282ebb7
3 changed files with 13 additions and 4 deletions

View File

@@ -1616,9 +1616,13 @@ format_cb_client_user(struct format_tree *ft)
struct passwd *pw;
if (ft->c != NULL) {
if (ft->c->user != NULL)
return (xstrdup(ft->c->user));
uid = proc_get_peer_uid(ft->c->peer);
if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL)
return (xstrdup(pw->pw_name));
if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL) {
ft->c->user = xstrdup(pw->pw_name);
return (xstrdup(ft->c->user));
}
}
return (NULL);
}
@@ -3076,10 +3080,13 @@ format_cb_uid(__unused struct format_tree *ft)
static void *
format_cb_user(__unused struct format_tree *ft)
{
static char *cached;
struct passwd *pw;
if ((pw = getpwuid(getuid())) != NULL)
return (xstrdup(pw->pw_name));
if (cached == NULL && (pw = getpwuid(getuid())) != NULL)
cached = xstrdup(pw->pw_name);
if (cached != NULL)
return (xstrdup(cached));
return (NULL);
}

View File

@@ -537,6 +537,7 @@ server_client_free(__unused int fd, __unused short events, void *arg)
if (c->references == 0) {
free((void *)c->name);
free((void *)c->user);
free(c);
}
}

1
tmux.h
View File

@@ -1958,6 +1958,7 @@ typedef void (*overlay_resize_cb)(struct client *, void *);
struct client {
const char *name;
struct tmuxpeer *peer;
const char *user;
struct cmdq_list *queue;
struct client_windows windows;