1
0
mirror of https://github.com/tmux/tmux.git synced 2025-04-21 20:08:48 +00:00

realpath the socket path; also sprinkle some const.

This commit is contained in:
Nicholas Marriott 2007-11-12 15:12:08 +00:00
parent e4a6cdefda
commit 4309d65475
4 changed files with 32 additions and 36 deletions

View File

@ -1,4 +1,4 @@
/* $Id: client.c,v 1.20 2007-11-08 10:39:52 nicm Exp $ */ /* $Id: client.c,v 1.21 2007-11-12 15:12:08 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -36,7 +36,7 @@ void client_handle_winch(struct client_ctx *);
int client_process_local(struct client_ctx *, char **); int client_process_local(struct client_ctx *, char **);
int int
client_init(char *path, struct client_ctx *cctx, int start_server) client_init(const char *path, struct client_ctx *cctx, int start_server)
{ {
struct sockaddr_un sa; struct sockaddr_un sa;
struct stat sb; struct stat sb;
@ -46,28 +46,22 @@ client_init(char *path, struct client_ctx *cctx, int start_server)
int mode; int mode;
u_int retries; u_int retries;
if (path == NULL) {
xasprintf(&path,
"%s/%s-%lu", _PATH_TMP, __progname, (u_long) getuid());
} else
path = xstrdup(path);
retries = 0; retries = 0;
retry: retry:
if (stat(path, &sb) != 0) { if (stat(path, &sb) != 0) {
if (start_server && errno == ENOENT && retries < 10) { if (start_server && errno == ENOENT && retries < 10) {
if (server_start(path) != 0) if (server_start(path) != 0)
goto error; return (-1);
usleep(10000); usleep(10000);
retries++; retries++;
goto retry; goto retry;
} }
log_warn("%s: stat", path); log_warn("%s: stat", path);
goto error; return (-1);
} }
if (!S_ISSOCK(sb.st_mode)) { if (!S_ISSOCK(sb.st_mode)) {
log_warnx("%s: %s", path, strerror(ENOTSOCK)); log_warnx("%s: %s", path, strerror(ENOTSOCK));
goto error; return (-1);
} }
memset(&sa, 0, sizeof sa); memset(&sa, 0, sizeof sa);
@ -75,35 +69,35 @@ retry:
size = strlcpy(sa.sun_path, path, sizeof sa.sun_path); size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
if (size >= sizeof sa.sun_path) { if (size >= sizeof sa.sun_path) {
log_warnx("%s: %s", path, strerror(ENAMETOOLONG)); log_warnx("%s: %s", path, strerror(ENAMETOOLONG));
goto error; return (-1);
} }
if ((cctx->srv_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { if ((cctx->srv_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
log_warn("%s: socket", path); log_warn("%s: socket", path);
goto error; return (-1);
} }
if (connect( if (connect(
cctx->srv_fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) { cctx->srv_fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
if (start_server && errno == ECONNREFUSED && retries < 10) { if (start_server && errno == ECONNREFUSED && retries < 10) {
if (unlink(path) != 0) { if (unlink(path) != 0) {
log_warn("%s: unlink", path); log_warn("%s: unlink", path);
goto error; return (-1);
} }
usleep(10000); usleep(10000);
retries++; retries++;
goto retry; goto retry;
} }
log_warn("%s: connect", path); log_warn("%s: connect", path);
goto error; return (-1);
} }
if ((mode = fcntl(cctx->srv_fd, F_GETFL)) == -1) { if ((mode = fcntl(cctx->srv_fd, F_GETFL)) == -1) {
log_warn("%s: fcntl", path); log_warn("%s: fcntl", path);
goto error; return (-1);
} }
if (fcntl(cctx->srv_fd, F_SETFL, mode|O_NONBLOCK) == -1) { if (fcntl(cctx->srv_fd, F_SETFL, mode|O_NONBLOCK) == -1) {
log_warn("%s: fcntl", path); log_warn("%s: fcntl", path);
goto error; return (-1);
} }
cctx->srv_in = buffer_create(BUFSIZ); cctx->srv_in = buffer_create(BUFSIZ);
cctx->srv_out = buffer_create(BUFSIZ); cctx->srv_out = buffer_create(BUFSIZ);
@ -111,7 +105,7 @@ retry:
if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) { if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) {
log_warn("ioctl(TIOCGWINSZ)"); log_warn("ioctl(TIOCGWINSZ)");
goto error; return (-1);
} }
data.sx = ws.ws_col; data.sx = ws.ws_col;
@ -121,12 +115,7 @@ retry:
client_write_server(cctx, MSG_IDENTIFY, &data, sizeof data); client_write_server(cctx, MSG_IDENTIFY, &data, sizeof data);
} }
xfree(path);
return (0); return (0);
error:
xfree(path);
return (-1);
} }
int int

View File

@ -1,4 +1,4 @@
/* $Id: server.c,v 1.37 2007-10-31 14:26:26 nicm Exp $ */ /* $Id: server.c,v 1.38 2007-11-12 15:12:08 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -43,7 +43,7 @@
/* Client list. */ /* Client list. */
struct clients clients; struct clients clients;
int server_main(char *, int); int server_main(const char *, int);
void server_fill_windows(struct pollfd **); void server_fill_windows(struct pollfd **);
void server_handle_windows(struct pollfd **); void server_handle_windows(struct pollfd **);
void server_fill_clients(struct pollfd **); void server_fill_clients(struct pollfd **);
@ -55,7 +55,7 @@ void server_lost_window(struct window *);
/* Fork new server. */ /* Fork new server. */
int int
server_start(char *path) server_start(const char *path)
{ {
struct sockaddr_un sa; struct sockaddr_un sa;
size_t size; size_t size;
@ -122,7 +122,7 @@ server_start(char *path)
/* Main server loop. */ /* Main server loop. */
int int
server_main(char *srv_path, int srv_fd) server_main(const char *srv_path, int srv_fd)
{ {
struct pollfd *pfds, *pfd; struct pollfd *pfds, *pfd;
int nfds; int nfds;

19
tmux.c
View File

@ -1,4 +1,4 @@
/* $Id: tmux.c,v 1.39 2007-11-09 11:03:35 nicm Exp $ */ /* $Id: tmux.c,v 1.40 2007-11-12 15:12:08 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -19,7 +19,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <err.h>
#include <errno.h> #include <errno.h>
#include <paths.h> #include <paths.h>
#include <poll.h> #include <poll.h>
@ -174,7 +173,7 @@ main(int argc, char **argv)
struct hdr hdr; struct hdr hdr;
const char *shell; const char *shell;
struct passwd *pw; struct passwd *pw;
char *path, *cause, *name; char *path, rpath[MAXPATHLEN], *cause, *name;
int n, opt; int n, opt;
path = name = NULL; path = name = NULL;
@ -209,6 +208,16 @@ main(int argc, char **argv)
bell_action = BELL_ANY; bell_action = BELL_ANY;
if (path == NULL) {
xasprintf(&path,
"%s/%s-%lu", _PATH_TMP, __progname, (u_long) getuid());
}
if (realpath(path, rpath) == NULL) {
log_warn("%s", path);
exit(1);
}
xfree(path);
shell = getenv("SHELL"); shell = getenv("SHELL");
if (shell == NULL || *shell == '\0') { if (shell == NULL || *shell == '\0') {
pw = getpwuid(getuid()); pw = getpwuid(getuid());
@ -231,7 +240,7 @@ main(int argc, char **argv)
if (!(cmd->entry->flags & CMD_NOSESSION) || if (!(cmd->entry->flags & CMD_NOSESSION) ||
(cmd->entry->flags & CMD_CANTNEST)) (cmd->entry->flags & CMD_CANTNEST))
client_fill_session(&data); client_fill_session(&data);
if (client_init(path, &cctx, cmd->entry->flags & CMD_STARTSERVER) != 0) if (client_init(rpath, &cctx, cmd->entry->flags & CMD_STARTSERVER) != 0)
exit(1); exit(1);
b = buffer_create(BUFSIZ); b = buffer_create(BUFSIZ);
cmd_send_string(b, name); cmd_send_string(b, name);
@ -242,8 +251,6 @@ main(int argc, char **argv)
MSG_COMMAND, &data, sizeof data, BUFFER_OUT(b), BUFFER_USED(b)); MSG_COMMAND, &data, sizeof data, BUFFER_OUT(b), BUFFER_USED(b));
buffer_destroy(b); buffer_destroy(b);
if (path != NULL)
xfree(path);
if (name != NULL) if (name != NULL)
xfree(name); xfree(name);

6
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.78 2007-11-12 14:21:41 nicm Exp $ */ /* $Id: tmux.h,v 1.79 2007-11-12 15:12:08 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -595,7 +595,7 @@ extern const struct cmd_entry cmd_unlink_window_entry;
void cmd_select_window_default(void **, int); void cmd_select_window_default(void **, int);
/* client.c */ /* client.c */
int client_init(char *, struct client_ctx *, int); int client_init(const char *, struct client_ctx *, int);
int client_flush(struct client_ctx *); int client_flush(struct client_ctx *);
int client_main(struct client_ctx *); int client_main(struct client_ctx *);
@ -622,7 +622,7 @@ const char *key_string_lookup_key(int);
/* server.c */ /* server.c */
extern struct clients clients; extern struct clients clients;
int server_start(char *); int server_start(const char *);
/* server-msg.c */ /* server-msg.c */
int server_msg_dispatch(struct client *); int server_msg_dispatch(struct client *);