diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2015-11-24 22:27:23 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2015-11-24 22:27:23 +0000 |
commit | d8423aef2cf174dc8a9c68303172ee9ecda52782 (patch) | |
tree | 524147ad4166ba6e2050f2b4d627ad6507197e10 | |
parent | a98518a8100f7d1b9c027e790ec5a81aa6a4bd79 (diff) |
Tidy the code that works out the socket path, and just use the full path
in the global socket_path rather than copying it.
-rw-r--r-- | usr.bin/tmux/client.c | 6 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.c | 88 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 4 |
3 files changed, 45 insertions, 53 deletions
diff --git a/usr.bin/tmux/client.c b/usr.bin/tmux/client.c index 85d773ab811..49a98b59a14 100644 --- a/usr.bin/tmux/client.c +++ b/usr.bin/tmux/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.107 2015/11/24 22:04:36 nicm Exp $ */ +/* $OpenBSD: client.c,v 1.108 2015/11/24 22:27:22 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -55,7 +55,7 @@ int client_attached; __dead void client_exec(const char *); int client_get_lock(char *); -int client_connect(struct event_base *, char *, int); +int client_connect(struct event_base *, const char *, int); void client_send_identify(const char *, const char *); void client_stdin_callback(int, short, void *); void client_write(int, const char *, size_t); @@ -96,7 +96,7 @@ client_get_lock(char *lockfile) /* Connect client to server. */ int -client_connect(struct event_base *base, char *path, int start_server) +client_connect(struct event_base *base, const char *path, int start_server) { struct sockaddr_un sa; size_t size; diff --git a/usr.bin/tmux/tmux.c b/usr.bin/tmux/tmux.c index a3b1e596df1..9690f9e86e5 100644 --- a/usr.bin/tmux/tmux.c +++ b/usr.bin/tmux/tmux.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.c,v 1.159 2015/11/24 22:09:53 nicm Exp $ */ +/* $OpenBSD: tmux.c,v 1.160 2015/11/24 22:27:22 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -41,10 +41,10 @@ struct environ *global_environ; char *shell_cmd; struct timeval start_time; -char socket_path[PATH_MAX]; +const char *socket_path; __dead void usage(void); -char *makesocketpath(const char *); +static char *make_label(const char *); __dead void usage(void) @@ -102,38 +102,48 @@ areshell(const char *shell) return (0); } -char * -makesocketpath(const char *label) +static char * +make_label(const char *label) { - char base[PATH_MAX], realbase[PATH_MAX], *path, *s; - struct stat sb; - u_int uid; + char *base, resolved[PATH_MAX], *path, *s; + struct stat sb; + u_int uid; + int saved_errno; + + if (label == NULL) + label = "default"; uid = getuid(); + if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0') - xsnprintf(base, sizeof base, "%s/tmux-%u", s, uid); + xasprintf(&base, "%s/tmux-%u", s, uid); else - xsnprintf(base, sizeof base, "%s/tmux-%u", _PATH_TMP, uid); + xasprintf(&base, "%s/tmux-%u", _PATH_TMP, uid); if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST) - return (NULL); + goto fail; if (lstat(base, &sb) != 0) - return (NULL); + goto fail; if (!S_ISDIR(sb.st_mode)) { errno = ENOTDIR; - return (NULL); + goto fail; } if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) { errno = EACCES; - return (NULL); + goto fail; } - if (realpath(base, realbase) == NULL) - strlcpy(realbase, base, sizeof realbase); - - xasprintf(&path, "%s/%s", realbase, label); + if (realpath(base, resolved) == NULL) + strlcpy(resolved, base, sizeof resolved); + xasprintf(&path, "%s/%s", resolved, label); return (path); + +fail: + saved_errno = errno; + free(base); + errno = saved_errno; + return (NULL); } void @@ -289,41 +299,23 @@ main(int argc, char **argv) } /* - * Figure out the socket path. If specified on the command-line with -S - * or -L, use it, otherwise try $TMUX or assume -L default. + * If socket is specified on the command-line with -S or -L, it is + * used. Otherwise, $TMUX is checked and if that fails "default" is + * used. */ - if (path == NULL) { - /* If no -L, use the environment. */ - if (label == NULL) { - s = getenv("TMUX"); - if (s != NULL) { - path = xstrdup(s); - path[strcspn (path, ",")] = '\0'; - if (*path == '\0') { - free(path); - label = xstrdup("default"); - } - } else - label = xstrdup("default"); - } - - /* -L or default set. */ - if (label != NULL) { - if ((path = makesocketpath(label)) == NULL) { - fprintf(stderr, "can't create socket: %s\n", - strerror(errno)); - exit(1); - } + if (path == NULL && label == NULL) { + s = getenv("TMUX"); + if (s != NULL && *s != '\0' && *s != ',') { + path = xstrdup(s); + path[strcspn (path, ",")] = '\0'; } } - free(label); - - if (strlcpy(socket_path, path, sizeof socket_path) >= - sizeof socket_path) { - fprintf(stderr, "socket path too long: %s\n", path); + if (path == NULL && (path = make_label(label)) == NULL) { + fprintf(stderr, "can't create socket: %s\n", strerror(errno)); exit(1); } - free(path); + socket_path = path; + free(label); /* Pass control to the client. */ exit(client_main(event_init(), argc, argv, flags)); diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 35d5e84a8bd..e2924f53ac4 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.589 2015/11/24 21:52:06 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.590 2015/11/24 22:27:22 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -1433,7 +1433,7 @@ extern struct options *global_w_options; extern struct environ *global_environ; extern char *shell_cmd; extern struct timeval start_time; -extern char socket_path[PATH_MAX]; +extern const char *socket_path; const char *getshell(void); int checkshell(const char *); int areshell(const char *); |