Add TMUX_SOCK like TMUX_PATH for the socket directory.

This commit is contained in:
Nicholas Marriott 2020-04-23 18:15:02 +01:00
parent 0d3fdae7b6
commit d53e1fedd5
2 changed files with 23 additions and 19 deletions

33
tmux.c
View File

@ -180,31 +180,30 @@ expand_paths(const char *s, char ***paths, u_int *n)
static char * static char *
make_label(const char *label, char **cause) make_label(const char *label, char **cause)
{ {
char *base, resolved[PATH_MAX], *path, *s; char **paths, *path, *base;
u_int i, n;
struct stat sb; struct stat sb;
uid_t uid; uid_t uid;
*cause = NULL; *cause = NULL;
if (label == NULL) if (label == NULL)
label = "default"; label = "default";
uid = getuid(); uid = getuid();
if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0') expand_paths(TMUX_SOCK, &paths, &n);
xasprintf(&base, "%s/tmux-%ld", s, (long)uid); if (n == 0) {
else xasprintf(cause, "no suitable socket path");
xasprintf(&base, "%s/tmux-%ld", _PATH_TMP, (long)uid); return (NULL);
if (realpath(base, resolved) == NULL &&
strlcpy(resolved, base, sizeof resolved) >= sizeof resolved) {
errno = ERANGE;
free(base);
goto fail;
} }
free(base); path = paths[0]; /* can only have one socket! */
for (i = 1; i < n; i++)
free(paths[i]);
free(paths);
if (mkdir(resolved, S_IRWXU) != 0 && errno != EEXIST) xasprintf(&base, "%s/tmux-%ld", path, (long)uid);
if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
goto fail; goto fail;
if (lstat(resolved, &sb) != 0) if (lstat(base, &sb) != 0)
goto fail; goto fail;
if (!S_ISDIR(sb.st_mode)) { if (!S_ISDIR(sb.st_mode)) {
errno = ENOTDIR; errno = ENOTDIR;
@ -214,11 +213,13 @@ make_label(const char *label, char **cause)
errno = EACCES; errno = EACCES;
goto fail; goto fail;
} }
xasprintf(&path, "%s/%s", resolved, label); xasprintf(&path, "%s/%s", base, label);
free(base);
return (path); return (path);
fail: fail:
xasprintf(cause, "error creating %s (%s)", resolved, strerror(errno)); xasprintf(cause, "error creating %s (%s)", base, strerror(errno));
free(base);
return (NULL); return (NULL);
} }

5
tmux.h
View File

@ -65,10 +65,13 @@ struct winlink;
/* Client-server protocol version. */ /* Client-server protocol version. */
#define PROTOCOL_VERSION 8 #define PROTOCOL_VERSION 8
/* Default configuration file. */ /* Default configuration files and socket paths. */
#ifndef TMUX_CONF #ifndef TMUX_CONF
#define TMUX_CONF "/etc/tmux.conf:~/.tmux.conf" #define TMUX_CONF "/etc/tmux.conf:~/.tmux.conf"
#endif #endif
#ifndef TMUX_SOCK
#define TMUX_SOCK "$TMUX_TMPDIR:" _PATH_TMP
#endif
/* Minimum layout cell size, NOT including border lines. */ /* Minimum layout cell size, NOT including border lines. */
#define PANE_MINIMUM 1 #define PANE_MINIMUM 1