Close memory leaks.

pull/1/head
Nicholas Marriott 2007-10-24 11:42:03 +00:00
parent 810a8846b7
commit 1f10f6ea8b
2 changed files with 31 additions and 14 deletions

View File

@ -1,4 +1,4 @@
/* $Id: client.c,v 1.17 2007-10-23 10:25:03 nicm Exp $ */ /* $Id: client.c,v 1.18 2007-10-24 11:42:02 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -49,24 +49,25 @@ client_init(char *path, struct client_ctx *cctx, int start_server)
if (path == NULL) { if (path == NULL) {
xasprintf(&path, xasprintf(&path,
"%s/%s-%lu", _PATH_TMP, __progname, (u_long) getuid()); "%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)
return (-1); goto error;
usleep(10000); usleep(10000);
retries++; retries++;
goto retry; goto retry;
} }
log_warn("%s: stat", path); log_warn("%s: stat", path);
return (-1); goto error;
} }
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));
return (-1); goto error;
} }
memset(&sa, 0, sizeof sa); memset(&sa, 0, sizeof sa);
@ -74,35 +75,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));
return (-1); goto error;
} }
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);
return (-1); goto error;
} }
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);
return (-1); goto error;
} }
usleep(10000); usleep(10000);
retries++; retries++;
goto retry; goto retry;
} }
log_warn("%s: connect", path); log_warn("%s: connect", path);
return (-1); goto error;
} }
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);
return (-1); goto error;
} }
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);
return (-1); goto error;
} }
cctx->srv_in = buffer_create(BUFSIZ); cctx->srv_in = buffer_create(BUFSIZ);
cctx->srv_out = buffer_create(BUFSIZ); cctx->srv_out = buffer_create(BUFSIZ);
@ -110,7 +111,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)");
return (-1); goto error;
} }
data.sx = ws.ws_col; data.sx = ws.ws_col;
@ -120,7 +121,12 @@ 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

15
tmux.c
View File

@ -1,4 +1,4 @@
/* $Id: tmux.c,v 1.37 2007-10-24 11:21:29 nicm Exp $ */ /* $Id: tmux.c,v 1.38 2007-10-24 11:42:03 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -238,7 +238,12 @@ main(int argc, char **argv)
client_write_server2(&cctx, client_write_server2(&cctx,
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)
xfree(name);
for (;;) { for (;;) {
pfd.fd = cctx.srv_fd; pfd.fd = cctx.srv_fd;
pfd.events = POLLIN; pfd.events = POLLIN;
@ -290,6 +295,12 @@ main(int argc, char **argv)
} }
out: out:
xfree(default_command);
close(cctx.srv_fd);
buffer_destroy(cctx.srv_in);
buffer_destroy(cctx.srv_out);
#ifdef DEBUG #ifdef DEBUG
xmalloc_report(getpid(), "client"); xmalloc_report(getpid(), "client");
#endif #endif