Build list of paths and weed out duplicates before loading configs, and

add TMUX_SOCK like TMUX_PATH for the socket directory.
This commit is contained in:
nicm 2020-05-16 14:26:33 +00:00
parent 4e0a718666
commit a29196ca6a
4 changed files with 105 additions and 67 deletions

55
cfg.c
View File

@ -67,45 +67,12 @@ set_cfg_file(const char *path)
cfg_file = xstrdup(path);
}
static char *
expand_cfg_file(const char *path, const char *home)
{
char *expanded, *name;
const char *end;
struct environ_entry *value;
if (strncmp(path, "~/", 2) == 0) {
if (home == NULL)
return (NULL);
xasprintf(&expanded, "%s%s", home, path + 1);
return (expanded);
}
if (*path == '$') {
end = strchr(path, '/');
if (end == NULL)
name = xstrdup(path + 1);
else
name = xstrndup(path + 1, end - path - 1);
value = environ_find(global_environ, name);
free(name);
if (value == NULL)
return (NULL);
if (end == NULL)
end = "";
xasprintf(&expanded, "%s%s", value->value, end);
return (expanded);
}
return (xstrdup(path));
}
void
start_cfg(void)
{
const char *home = find_home();
struct client *c;
char *path, *copy, *next, *expanded;
struct client *c;
char **paths;
u_int i, n;
/*
* Configuration files are loaded without a client, so commands are run
@ -124,18 +91,12 @@ start_cfg(void)
}
if (cfg_file == NULL) {
path = copy = xstrdup(TMUX_CONF);
while ((next = strsep(&path, ":")) != NULL) {
expanded = expand_cfg_file(next, home);
if (expanded == NULL) {
log_debug("couldn't expand %s", next);
continue;
}
log_debug("expanded %s to %s", next, expanded);
load_cfg(expanded, c, NULL, CMD_PARSE_QUIET, NULL);
free(expanded);
expand_paths(TMUX_CONF, &paths, &n);
for (i = 0; i < n; i++) {
load_cfg(paths[i], c, NULL, CMD_PARSE_QUIET, NULL);
free(paths[i]);
}
free(copy);
free(paths);
} else
load_cfg(cfg_file, c, NULL, 0, NULL);

108
tmux.c
View File

@ -109,34 +109,104 @@ areshell(const char *shell)
return (0);
}
static char *
expand_path(const char *path, const char *home)
{
char *expanded, *name;
const char *end;
struct environ_entry *value;
if (strncmp(path, "~/", 2) == 0) {
if (home == NULL)
return (NULL);
xasprintf(&expanded, "%s%s", home, path + 1);
return (expanded);
}
if (*path == '$') {
end = strchr(path, '/');
if (end == NULL)
name = xstrdup(path + 1);
else
name = xstrndup(path + 1, end - path - 1);
value = environ_find(global_environ, name);
free(name);
if (value == NULL)
return (NULL);
if (end == NULL)
end = "";
xasprintf(&expanded, "%s%s", value->value, end);
return (expanded);
}
return (xstrdup(path));
}
void
expand_paths(const char *s, char ***paths, u_int *n)
{
const char *home = find_home();
char *copy, *next, *tmp, resolved[PATH_MAX], *expanded;
u_int i;
*paths = NULL;
*n = 0;
copy = tmp = xstrdup(s);
while ((next = strsep(&tmp, ":")) != NULL) {
expanded = expand_path(next, home);
if (expanded == NULL) {
log_debug("%s: invalid path: %s", __func__, next);
continue;
}
if (realpath(expanded, resolved) == NULL) {
log_debug("%s: realpath(\"%s\") failed: %s", __func__,
expanded, strerror(errno));
free(expanded);
continue;
}
free(expanded);
for (i = 0; i < *n; i++) {
if (strcmp(resolved, (*paths)[i]) == 0)
break;
}
if (i != *n) {
log_debug("%s: duplicate path: %s", __func__, resolved);
continue;
}
*paths = xreallocarray(*paths, (*n) + 1, sizeof *paths);
(*paths)[(*n)++] = xstrdup(resolved);
}
free(copy);
}
static char *
make_label(const char *label, char **cause)
{
char *base, resolved[PATH_MAX], *path, *s;
struct stat sb;
uid_t uid;
char **paths, *path, *base;
u_int i, n;
struct stat sb;
uid_t uid;
*cause = NULL;
if (label == NULL)
label = "default";
uid = getuid();
if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0')
xasprintf(&base, "%s/tmux-%ld", s, (long)uid);
else
xasprintf(&base, "%s/tmux-%ld", _PATH_TMP, (long)uid);
if (realpath(base, resolved) == NULL &&
strlcpy(resolved, base, sizeof resolved) >= sizeof resolved) {
errno = ERANGE;
free(base);
goto fail;
expand_paths(TMUX_SOCK, &paths, &n);
if (n == 0) {
xasprintf(cause, "no suitable socket path");
return (NULL);
}
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;
if (lstat(resolved, &sb) != 0)
if (lstat(base, &sb) != 0)
goto fail;
if (!S_ISDIR(sb.st_mode)) {
errno = ENOTDIR;
@ -146,11 +216,13 @@ make_label(const char *label, char **cause)
errno = EACCES;
goto fail;
}
xasprintf(&path, "%s/%s", resolved, label);
xasprintf(&path, "%s/%s", base, label);
free(base);
return (path);
fail:
xasprintf(cause, "error creating %s (%s)", resolved, strerror(errno));
xasprintf(cause, "error creating %s (%s)", base, strerror(errno));
free(base);
return (NULL);
}

7
tmux.h
View File

@ -63,10 +63,13 @@ struct winlink;
/* Client-server protocol version. */
#define PROTOCOL_VERSION 8
/* Default configuration files. */
/* Default configuration files and socket paths. */
#ifndef TMUX_CONF
#define TMUX_CONF "/etc/tmux.conf:~/.tmux.conf"
#endif
#ifndef TMUX_SOCK
#define TMUX_SOCK "$TMUX_TMPDIR:" _PATH_TMP
#endif
/* Minimum layout cell size, NOT including border lines. */
#define PANE_MINIMUM 1
@ -1746,6 +1749,8 @@ const char *sig2name(int);
const char *find_cwd(void);
const char *find_home(void);
const char *getversion(void);
void expand_paths(const char *, char ***, u_int *);
/* proc.c */
struct imsg;

View File

@ -27,7 +27,7 @@
* Still hardcoded:
* - bracket paste (sent if application asks for it);
* - mouse (under kmous capability);
* - focus events (under focus-events option);
* - focus events (under XT and focus-events option);
* - default colours (under AX or op capabilities);
* - AIX colours (under colors >= 16);
* - alternate escape (under XT).