2009-09-03 21:06:30 +00:00
|
|
|
/* $Id: client.c,v 1.70 2009-09-03 21:06:30 tcunha Exp $ */
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
2009-02-08 16:11:26 +00:00
|
|
|
#include <sys/wait.h>
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2009-01-11 00:48:42 +00:00
|
|
|
#include <pwd.h>
|
2007-09-26 13:43:15 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-08-09 17:48:55 +00:00
|
|
|
void client_send_environ(struct client_ctx *);
|
2007-09-26 13:43:15 +00:00
|
|
|
void client_handle_winch(struct client_ctx *);
|
|
|
|
|
|
|
|
int
|
2009-07-23 13:15:41 +00:00
|
|
|
client_init(char *path, struct client_ctx *cctx, int cmdflags, int flags)
|
2007-09-26 13:43:15 +00:00
|
|
|
{
|
2007-10-03 21:31:07 +00:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
struct stat sb;
|
|
|
|
struct msg_identify_data data;
|
|
|
|
struct winsize ws;
|
2007-10-23 10:25:03 +00:00
|
|
|
size_t size;
|
2009-08-14 21:26:07 +00:00
|
|
|
int fd, fd2, mode;
|
2009-07-28 22:12:16 +00:00
|
|
|
char *name, *term;
|
2009-06-25 15:58:33 +00:00
|
|
|
#ifdef HAVE_SETPROCTITLE
|
|
|
|
char rpathbuf[MAXPATHLEN];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SETPROCTITLE
|
|
|
|
if (realpath(path, rpathbuf) == NULL)
|
|
|
|
strlcpy(rpathbuf, path, sizeof rpathbuf);
|
|
|
|
setproctitle("client (%s)", rpathbuf);
|
|
|
|
#endif
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2009-03-27 08:58:28 +00:00
|
|
|
if (lstat(path, &sb) != 0) {
|
2009-07-23 13:15:41 +00:00
|
|
|
if (cmdflags & CMD_STARTSERVER && errno == ENOENT) {
|
2009-08-09 17:43:00 +00:00
|
|
|
if ((fd = server_start(path)) == -1)
|
2009-01-19 17:16:09 +00:00
|
|
|
goto start_failed;
|
|
|
|
goto server_started;
|
2007-09-27 20:53:13 +00:00
|
|
|
}
|
2009-01-19 17:16:09 +00:00
|
|
|
goto not_found;
|
2007-09-27 20:53:13 +00:00
|
|
|
}
|
|
|
|
if (!S_ISSOCK(sb.st_mode)) {
|
2007-12-01 11:10:33 +00:00
|
|
|
errno = ENOTSOCK;
|
2009-01-19 17:16:09 +00:00
|
|
|
goto not_found;
|
2007-09-27 20:53:13 +00:00
|
|
|
}
|
|
|
|
|
2007-09-26 13:43:15 +00:00
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
sa.sun_family = AF_UNIX;
|
2007-10-23 10:25:03 +00:00
|
|
|
size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
|
|
|
|
if (size >= sizeof sa.sun_path) {
|
2007-12-01 11:10:33 +00:00
|
|
|
errno = ENAMETOOLONG;
|
2009-01-19 17:16:09 +00:00
|
|
|
goto not_found;
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 17:43:00 +00:00
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
2007-12-01 11:10:33 +00:00
|
|
|
fatal("socket");
|
|
|
|
|
2009-08-09 17:43:00 +00:00
|
|
|
if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
|
2009-01-19 17:16:09 +00:00
|
|
|
if (errno == ECONNREFUSED) {
|
2009-07-23 13:15:41 +00:00
|
|
|
if (unlink(path) != 0 || !(cmdflags & CMD_STARTSERVER))
|
2009-01-19 17:16:09 +00:00
|
|
|
goto not_found;
|
2009-08-09 17:43:00 +00:00
|
|
|
if ((fd = server_start(path)) == -1)
|
2009-01-19 17:16:09 +00:00
|
|
|
goto start_failed;
|
|
|
|
goto server_started;
|
2007-09-26 18:12:19 +00:00
|
|
|
}
|
2009-01-19 17:16:09 +00:00
|
|
|
goto not_found;
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
2009-01-19 17:16:09 +00:00
|
|
|
server_started:
|
2009-08-09 17:43:00 +00:00
|
|
|
if ((mode = fcntl(fd, F_GETFL)) == -1)
|
2009-03-31 22:20:42 +00:00
|
|
|
fatal("fcntl failed");
|
2009-08-09 17:43:00 +00:00
|
|
|
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
|
2009-03-31 22:20:42 +00:00
|
|
|
fatal("fcntl failed");
|
2009-08-14 21:04:04 +00:00
|
|
|
imsg_init(&cctx->ibuf, fd);
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2009-08-09 17:48:55 +00:00
|
|
|
if (cmdflags & CMD_SENDENVIRON)
|
|
|
|
client_send_environ(cctx);
|
2007-11-26 20:36:30 +00:00
|
|
|
if (isatty(STDIN_FILENO)) {
|
2007-12-01 11:10:33 +00:00
|
|
|
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
|
|
|
|
fatal("ioctl(TIOCGWINSZ)");
|
2008-09-09 22:16:37 +00:00
|
|
|
data.flags = flags;
|
2007-10-03 21:31:07 +00:00
|
|
|
data.sx = ws.ws_col;
|
|
|
|
data.sy = ws.ws_row;
|
2009-07-28 22:12:16 +00:00
|
|
|
|
2009-01-10 19:37:35 +00:00
|
|
|
if (getcwd(data.cwd, sizeof data.cwd) == NULL)
|
|
|
|
*data.cwd = '\0';
|
2008-06-18 20:58:03 +00:00
|
|
|
|
2009-07-28 22:12:16 +00:00
|
|
|
*data.term = '\0';
|
|
|
|
if ((term = getenv("TERM")) != NULL) {
|
|
|
|
if (strlcpy(data.term,
|
|
|
|
term, sizeof data.term) >= sizeof data.term)
|
|
|
|
*data.term = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
*data.tty = '\0';
|
2008-06-18 20:58:03 +00:00
|
|
|
if ((name = ttyname(STDIN_FILENO)) == NULL)
|
|
|
|
fatal("ttyname failed");
|
|
|
|
if (strlcpy(data.tty, name, sizeof data.tty) >= sizeof data.tty)
|
|
|
|
fatalx("ttyname failed");
|
2008-06-01 21:24:33 +00:00
|
|
|
|
2009-08-14 21:26:07 +00:00
|
|
|
fd2 = dup(STDIN_FILENO);
|
2009-08-14 21:23:20 +00:00
|
|
|
imsg_compose(&cctx->ibuf, MSG_IDENTIFY,
|
2009-08-14 21:26:07 +00:00
|
|
|
PROTOCOL_VERSION, -1, fd2, &data, sizeof data);
|
2007-09-29 13:22:15 +00:00
|
|
|
}
|
2007-10-03 21:31:07 +00:00
|
|
|
|
|
|
|
return (0);
|
2007-12-01 11:10:33 +00:00
|
|
|
|
2009-01-19 17:16:09 +00:00
|
|
|
start_failed:
|
2008-06-07 07:27:28 +00:00
|
|
|
log_warnx("server failed to start");
|
|
|
|
return (1);
|
|
|
|
|
2009-01-19 17:16:09 +00:00
|
|
|
not_found:
|
2007-12-01 11:10:33 +00:00
|
|
|
log_warn("server not found");
|
2008-06-02 21:08:36 +00:00
|
|
|
return (1);
|
2007-09-28 19:04:21 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 17:48:55 +00:00
|
|
|
void
|
|
|
|
client_send_environ(struct client_ctx *cctx)
|
|
|
|
{
|
|
|
|
char **var;
|
|
|
|
struct msg_environ_data data;
|
|
|
|
|
|
|
|
for (var = environ; *var != NULL; var++) {
|
|
|
|
if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
|
|
|
|
continue;
|
|
|
|
client_write_server(cctx, MSG_ENVIRON, &data, sizeof data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-26 13:43:15 +00:00
|
|
|
int
|
|
|
|
client_main(struct client_ctx *cctx)
|
|
|
|
{
|
2007-11-27 19:23:34 +00:00
|
|
|
struct pollfd pfd;
|
2009-09-03 21:06:30 +00:00
|
|
|
int n, nfds;
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2007-11-08 10:39:52 +00:00
|
|
|
siginit();
|
|
|
|
|
2007-09-26 13:43:15 +00:00
|
|
|
logfile("client");
|
|
|
|
|
2009-09-03 21:06:30 +00:00
|
|
|
/*
|
|
|
|
* imsg_read in the first client poll loop (before the terminal has
|
|
|
|
* been initialiased) may have read messages into the buffer after the
|
|
|
|
* MSG_READY switched to here. Process anything outstanding now so poll
|
|
|
|
* doesn't hang waiting for messages that have already arrived.
|
|
|
|
*/
|
|
|
|
if (client_msg_dispatch(cctx) != 0)
|
|
|
|
goto out;
|
|
|
|
|
2009-07-30 20:50:10 +00:00
|
|
|
for (;;) {
|
|
|
|
if (sigterm)
|
|
|
|
client_write_server(cctx, MSG_EXITING, NULL, 0);
|
2009-02-08 16:11:26 +00:00
|
|
|
if (sigchld) {
|
|
|
|
waitpid(WAIT_ANY, NULL, WNOHANG);
|
|
|
|
sigchld = 0;
|
|
|
|
}
|
2007-09-26 13:43:15 +00:00
|
|
|
if (sigwinch)
|
|
|
|
client_handle_winch(cctx);
|
2009-01-18 12:09:42 +00:00
|
|
|
if (sigcont) {
|
|
|
|
siginit();
|
2009-05-04 17:58:27 +00:00
|
|
|
client_write_server(cctx, MSG_WAKEUP, NULL, 0);
|
2009-01-18 12:09:42 +00:00
|
|
|
sigcont = 0;
|
|
|
|
}
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2009-08-14 21:04:04 +00:00
|
|
|
pfd.fd = cctx->ibuf.fd;
|
2007-11-27 19:23:34 +00:00
|
|
|
pfd.events = POLLIN;
|
2009-08-14 21:04:04 +00:00
|
|
|
if (cctx->ibuf.w.queued > 0)
|
2007-11-27 19:23:34 +00:00
|
|
|
pfd.events |= POLLOUT;
|
2007-12-06 09:46:23 +00:00
|
|
|
|
2009-08-14 21:04:04 +00:00
|
|
|
if ((nfds = poll(&pfd, 1, INFTIM)) == -1) {
|
2007-09-26 13:43:15 +00:00
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
|
|
|
fatal("poll failed");
|
|
|
|
}
|
2009-08-14 21:04:04 +00:00
|
|
|
if (nfds == 0)
|
|
|
|
continue;
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2009-08-14 21:04:04 +00:00
|
|
|
if (pfd.revents & (POLLERR|POLLHUP|POLLNVAL))
|
|
|
|
fatalx("socket error");
|
|
|
|
|
|
|
|
if (pfd.revents & POLLIN) {
|
2009-09-03 21:06:30 +00:00
|
|
|
if ((n = imsg_read(&cctx->ibuf)) == -1 || n == 0) {
|
|
|
|
cctx->exittype = CCTX_DIED;
|
|
|
|
break;
|
|
|
|
}
|
2009-08-14 21:04:04 +00:00
|
|
|
if (client_msg_dispatch(cctx) != 0)
|
|
|
|
break;
|
2009-07-23 23:42:59 +00:00
|
|
|
}
|
2009-07-30 20:57:39 +00:00
|
|
|
|
2009-08-14 21:04:04 +00:00
|
|
|
if (pfd.revents & POLLOUT) {
|
|
|
|
if (msgbuf_write(&cctx->ibuf.w) < 0) {
|
|
|
|
cctx->exittype = CCTX_DIED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-09-26 18:50:49 +00:00
|
|
|
}
|
2009-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
out:
|
2007-10-05 14:23:28 +00:00
|
|
|
if (sigterm) {
|
|
|
|
printf("[terminated]\n");
|
|
|
|
return (1);
|
|
|
|
}
|
2009-07-23 23:42:59 +00:00
|
|
|
switch (cctx->exittype) {
|
|
|
|
case CCTX_DIED:
|
|
|
|
printf("[lost server]\n");
|
|
|
|
return (0);
|
|
|
|
case CCTX_SHUTDOWN:
|
2009-01-21 22:47:31 +00:00
|
|
|
printf("[server exited]\n");
|
|
|
|
return (0);
|
2009-07-23 23:42:59 +00:00
|
|
|
case CCTX_EXIT:
|
2009-09-02 20:16:29 +00:00
|
|
|
if (cctx->errstr != NULL) {
|
|
|
|
printf("[error: %s]\n", cctx->errstr);
|
|
|
|
return (1);
|
|
|
|
}
|
2007-10-05 14:23:28 +00:00
|
|
|
printf("[exited]\n");
|
|
|
|
return (0);
|
2009-07-23 23:42:59 +00:00
|
|
|
case CCTX_DETACH:
|
2007-10-05 14:23:28 +00:00
|
|
|
printf("[detached]\n");
|
|
|
|
return (0);
|
2009-09-02 20:01:22 +00:00
|
|
|
default:
|
2009-09-02 20:16:29 +00:00
|
|
|
printf("[unknown error]\n");
|
2009-09-02 20:01:22 +00:00
|
|
|
return (1);
|
2007-10-05 14:23:28 +00:00
|
|
|
}
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
client_handle_winch(struct client_ctx *cctx)
|
|
|
|
{
|
2007-10-03 21:31:07 +00:00
|
|
|
struct msg_resize_data data;
|
|
|
|
struct winsize ws;
|
2007-12-06 09:46:23 +00:00
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
|
2007-09-26 13:43:15 +00:00
|
|
|
fatal("ioctl failed");
|
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
data.sx = ws.ws_col;
|
|
|
|
data.sy = ws.ws_row;
|
|
|
|
client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
|
2007-12-06 09:46:23 +00:00
|
|
|
|
2007-09-26 13:43:15 +00:00
|
|
|
sigwinch = 0;
|
|
|
|
}
|
2009-07-30 20:57:39 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
client_msg_dispatch(struct client_ctx *cctx)
|
|
|
|
{
|
2009-08-14 21:04:04 +00:00
|
|
|
struct imsg imsg;
|
2009-07-30 20:57:39 +00:00
|
|
|
struct msg_print_data printdata;
|
2009-08-14 21:04:04 +00:00
|
|
|
ssize_t n, datalen;
|
|
|
|
|
2009-07-30 20:57:39 +00:00
|
|
|
for (;;) {
|
2009-08-14 21:04:04 +00:00
|
|
|
if ((n = imsg_get(&cctx->ibuf, &imsg)) == -1)
|
|
|
|
fatalx("imsg_get failed");
|
|
|
|
if (n == 0)
|
2009-07-30 20:57:39 +00:00
|
|
|
return (0);
|
2009-08-14 21:04:04 +00:00
|
|
|
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
|
2009-07-30 20:57:39 +00:00
|
|
|
|
2009-08-14 21:04:04 +00:00
|
|
|
switch (imsg.hdr.type) {
|
2009-07-30 20:57:39 +00:00
|
|
|
case MSG_DETACH:
|
2009-08-14 21:04:04 +00:00
|
|
|
if (datalen != 0)
|
2009-07-30 20:57:39 +00:00
|
|
|
fatalx("bad MSG_DETACH size");
|
|
|
|
|
|
|
|
client_write_server(cctx, MSG_EXITING, NULL, 0);
|
|
|
|
cctx->exittype = CCTX_DETACH;
|
|
|
|
break;
|
|
|
|
case MSG_ERROR:
|
2009-08-14 21:04:04 +00:00
|
|
|
if (datalen != sizeof printdata)
|
|
|
|
fatalx("bad MSG_ERROR size");
|
|
|
|
memcpy(&printdata, imsg.data, sizeof printdata);
|
2009-07-30 20:57:39 +00:00
|
|
|
|
2009-08-09 17:48:55 +00:00
|
|
|
printdata.msg[(sizeof printdata.msg) - 1] = '\0';
|
2009-09-02 20:16:29 +00:00
|
|
|
/* Error string used after exit message from server. */
|
2009-07-30 20:57:39 +00:00
|
|
|
cctx->errstr = xstrdup(printdata.msg);
|
2009-08-14 21:04:04 +00:00
|
|
|
imsg_free(&imsg);
|
2009-07-30 20:57:39 +00:00
|
|
|
return (-1);
|
|
|
|
case MSG_EXIT:
|
2009-08-14 21:04:04 +00:00
|
|
|
if (datalen != 0)
|
2009-07-30 20:57:39 +00:00
|
|
|
fatalx("bad MSG_EXIT size");
|
2009-08-14 21:04:04 +00:00
|
|
|
|
2009-07-30 20:57:39 +00:00
|
|
|
client_write_server(cctx, MSG_EXITING, NULL, 0);
|
|
|
|
cctx->exittype = CCTX_EXIT;
|
|
|
|
break;
|
|
|
|
case MSG_EXITED:
|
2009-08-14 21:04:04 +00:00
|
|
|
if (datalen != 0)
|
2009-07-30 20:57:39 +00:00
|
|
|
fatalx("bad MSG_EXITED size");
|
|
|
|
|
2009-08-14 21:04:04 +00:00
|
|
|
imsg_free(&imsg);
|
2009-07-30 20:57:39 +00:00
|
|
|
return (-1);
|
|
|
|
case MSG_SHUTDOWN:
|
2009-08-14 21:04:04 +00:00
|
|
|
if (datalen != 0)
|
2009-07-30 20:57:39 +00:00
|
|
|
fatalx("bad MSG_SHUTDOWN size");
|
|
|
|
|
|
|
|
client_write_server(cctx, MSG_EXITING, NULL, 0);
|
|
|
|
cctx->exittype = CCTX_SHUTDOWN;
|
|
|
|
break;
|
|
|
|
case MSG_SUSPEND:
|
2009-08-14 21:04:04 +00:00
|
|
|
if (datalen != 0)
|
2009-07-30 20:57:39 +00:00
|
|
|
fatalx("bad MSG_SUSPEND size");
|
|
|
|
|
|
|
|
client_suspend();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatalx("unexpected message");
|
|
|
|
}
|
2009-08-14 21:04:04 +00:00
|
|
|
|
|
|
|
imsg_free(&imsg);
|
2009-07-30 20:57:39 +00:00
|
|
|
}
|
|
|
|
}
|