2009-11-02 21:41:16 +00:00
|
|
|
/* $Id: client.c,v 1.84 2009-11-02 21:41:16 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-10-23 17:32:26 +00:00
|
|
|
struct imsgbuf client_ibuf;
|
|
|
|
const char *client_exitmsg;
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2009-10-23 17:37:41 +00:00
|
|
|
void client_send_identify(int);
|
2009-10-23 17:32:26 +00:00
|
|
|
void client_send_environ(void);
|
|
|
|
void client_write_server(enum msgtype, void *, size_t);
|
|
|
|
int client_dispatch(void);
|
|
|
|
void client_suspend(void);
|
|
|
|
|
|
|
|
struct imsgbuf *
|
|
|
|
client_init(char *path, int cmdflags, int flags)
|
2007-09-26 13:43:15 +00:00
|
|
|
{
|
2009-10-23 17:37:41 +00:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
size_t size;
|
|
|
|
int fd, mode;
|
2009-06-25 15:58:33 +00:00
|
|
|
#ifdef HAVE_SETPROCTITLE
|
2009-10-23 17:37:41 +00:00
|
|
|
char rpathbuf[MAXPATHLEN];
|
2009-06-25 15:58:33 +00:00
|
|
|
#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
|
|
|
|
|
|
|
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)
|
2009-09-20 22:11:27 +00:00
|
|
|
fatal("socket failed");
|
2007-12-01 11:10:33 +00:00
|
|
|
|
2009-08-09 17:43:00 +00:00
|
|
|
if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
|
2009-11-02 21:40:44 +00:00
|
|
|
if (!(cmdflags & CMD_STARTSERVER))
|
|
|
|
goto not_found;
|
|
|
|
switch (errno) {
|
|
|
|
case ECONNREFUSED:
|
|
|
|
if (unlink(path) != 0)
|
2009-01-19 17:16:09 +00:00
|
|
|
goto not_found;
|
2009-11-02 21:40:44 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 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-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-09-23 15:18:56 +00:00
|
|
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
fatal("fcntl failed");
|
2009-10-23 17:32:26 +00:00
|
|
|
imsg_init(&client_ibuf, fd);
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2009-08-09 17:48:55 +00:00
|
|
|
if (cmdflags & CMD_SENDENVIRON)
|
2009-10-23 17:32:26 +00:00
|
|
|
client_send_environ();
|
2009-10-23 17:37:41 +00:00
|
|
|
if (isatty(STDIN_FILENO))
|
|
|
|
client_send_identify(flags);
|
2007-10-03 21:31:07 +00:00
|
|
|
|
2009-10-23 17:32:26 +00:00
|
|
|
return (&client_ibuf);
|
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");
|
2009-10-23 17:32:26 +00:00
|
|
|
return (NULL);
|
2008-06-07 07:27:28 +00:00
|
|
|
|
2009-01-19 17:16:09 +00:00
|
|
|
not_found:
|
2007-12-01 11:10:33 +00:00
|
|
|
log_warn("server not found");
|
2009-10-23 17:32:26 +00:00
|
|
|
return (NULL);
|
2007-09-28 19:04:21 +00:00
|
|
|
}
|
|
|
|
|
2009-10-23 17:37:41 +00:00
|
|
|
void
|
|
|
|
client_send_identify(int flags)
|
|
|
|
{
|
|
|
|
struct msg_identify_data data;
|
|
|
|
struct winsize ws;
|
|
|
|
char *term;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
|
|
|
|
fatal("ioctl(TIOCGWINSZ)");
|
|
|
|
data.flags = flags;
|
|
|
|
|
|
|
|
if (getcwd(data.cwd, sizeof data.cwd) == NULL)
|
|
|
|
*data.cwd = '\0';
|
|
|
|
|
|
|
|
term = getenv("TERM");
|
|
|
|
if (term == NULL ||
|
|
|
|
strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
|
|
|
|
*data.term = '\0';
|
|
|
|
|
|
|
|
if ((fd = dup(STDIN_FILENO)) == -1)
|
|
|
|
fatal("dup failed");
|
|
|
|
imsg_compose(&client_ibuf,
|
|
|
|
MSG_IDENTIFY, PROTOCOL_VERSION, -1, fd, &data, sizeof data);
|
|
|
|
}
|
|
|
|
|
2009-08-09 17:48:55 +00:00
|
|
|
void
|
2009-10-23 17:32:26 +00:00
|
|
|
client_send_environ(void)
|
2009-08-09 17:48:55 +00:00
|
|
|
{
|
|
|
|
struct msg_environ_data data;
|
2009-10-23 17:37:41 +00:00
|
|
|
char **var;
|
2009-08-09 17:48:55 +00:00
|
|
|
|
|
|
|
for (var = environ; *var != NULL; var++) {
|
|
|
|
if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
|
|
|
|
continue;
|
2009-10-23 17:32:26 +00:00
|
|
|
client_write_server(MSG_ENVIRON, &data, sizeof data);
|
2009-08-09 17:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-23 17:32:26 +00:00
|
|
|
void
|
|
|
|
client_write_server(enum msgtype type, void *buf, size_t len)
|
|
|
|
{
|
|
|
|
imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
__dead void
|
|
|
|
client_main(void)
|
2007-09-26 13:43:15 +00:00
|
|
|
{
|
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.
|
|
|
|
*/
|
2009-10-23 17:32:26 +00:00
|
|
|
if (client_dispatch() != 0)
|
2009-09-03 21:06:30 +00:00
|
|
|
goto out;
|
|
|
|
|
2009-07-30 20:50:10 +00:00
|
|
|
for (;;) {
|
2009-10-23 17:32:26 +00:00
|
|
|
if (sigterm) {
|
|
|
|
client_exitmsg = "terminated";
|
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
}
|
2009-02-08 16:11:26 +00:00
|
|
|
if (sigchld) {
|
|
|
|
sigchld = 0;
|
2009-10-28 23:11:07 +00:00
|
|
|
waitpid(WAIT_ANY, NULL, WNOHANG);
|
|
|
|
continue;
|
2009-02-08 16:11:26 +00:00
|
|
|
}
|
2009-09-23 14:44:02 +00:00
|
|
|
if (sigwinch) {
|
2009-10-28 23:11:07 +00:00
|
|
|
sigwinch = 0;
|
2009-10-23 17:32:26 +00:00
|
|
|
client_write_server(MSG_RESIZE, NULL, 0);
|
2009-10-28 23:11:07 +00:00
|
|
|
continue;
|
2009-09-23 14:44:02 +00:00
|
|
|
}
|
2009-01-18 12:09:42 +00:00
|
|
|
if (sigcont) {
|
2009-10-28 23:11:07 +00:00
|
|
|
sigcont = 0;
|
2009-01-18 12:09:42 +00:00
|
|
|
siginit();
|
2009-10-23 17:32:26 +00:00
|
|
|
client_write_server(MSG_WAKEUP, NULL, 0);
|
2009-10-28 23:11:07 +00:00
|
|
|
continue;
|
2009-01-18 12:09:42 +00:00
|
|
|
}
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2009-10-23 17:32:26 +00:00
|
|
|
pfd.fd = client_ibuf.fd;
|
2007-11-27 19:23:34 +00:00
|
|
|
pfd.events = POLLIN;
|
2009-10-23 17:32:26 +00:00
|
|
|
if (client_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-10-23 17:32:26 +00:00
|
|
|
if ((n = imsg_read(&client_ibuf)) == -1 || n == 0) {
|
|
|
|
client_exitmsg = "lost server";
|
2009-09-03 21:06:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-10-23 17:32:26 +00:00
|
|
|
if (client_dispatch() != 0)
|
2009-08-14 21:04:04 +00:00
|
|
|
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) {
|
2009-10-23 17:32:26 +00:00
|
|
|
if (msgbuf_write(&client_ibuf.w) < 0) {
|
|
|
|
client_exitmsg = "lost server";
|
2009-08-14 21:04:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-09-26 18:50:49 +00:00
|
|
|
}
|
2009-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
out:
|
2009-10-23 17:32:26 +00:00
|
|
|
/* Print the exit message, if any, and exit. */
|
|
|
|
if (client_exitmsg != NULL) {
|
2009-10-15 01:48:24 +00:00
|
|
|
if (!login_shell)
|
2009-10-23 17:32:26 +00:00
|
|
|
printf("[%s]\n", client_exitmsg);
|
|
|
|
exit(1);
|
2007-10-05 14:23:28 +00:00
|
|
|
}
|
2009-10-23 17:32:26 +00:00
|
|
|
exit(0);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 20:57:39 +00:00
|
|
|
int
|
2009-10-23 17:32:26 +00:00
|
|
|
client_dispatch(void)
|
2009-07-30 20:57:39 +00:00
|
|
|
{
|
2009-08-14 21:04:04 +00:00
|
|
|
struct imsg imsg;
|
2009-09-23 15:00:09 +00:00
|
|
|
struct msg_lock_data lockdata;
|
2009-08-14 21:04:04 +00:00
|
|
|
ssize_t n, datalen;
|
|
|
|
|
2009-07-30 20:57:39 +00:00
|
|
|
for (;;) {
|
2009-10-23 17:32:26 +00:00
|
|
|
if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
|
2009-08-14 21:04:04 +00:00
|
|
|
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");
|
|
|
|
|
2009-10-23 17:32:26 +00:00
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
client_exitmsg = "detached";
|
2009-07-30 20:57:39 +00:00
|
|
|
break;
|
|
|
|
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-10-23 17:32:26 +00:00
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
client_exitmsg = "exited";
|
2009-07-30 20:57:39 +00:00
|
|
|
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");
|
|
|
|
|
2009-10-23 17:32:26 +00:00
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
client_exitmsg = "server exited";
|
2009-07-30 20:57:39 +00:00
|
|
|
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;
|
2009-09-23 15:00:09 +00:00
|
|
|
case MSG_LOCK:
|
|
|
|
if (datalen != sizeof lockdata)
|
|
|
|
fatalx("bad MSG_LOCK size");
|
|
|
|
memcpy(&lockdata, imsg.data, sizeof lockdata);
|
|
|
|
|
|
|
|
lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
|
|
|
|
system(lockdata.cmd);
|
2009-10-23 17:32:26 +00:00
|
|
|
client_write_server(MSG_UNLOCK, NULL, 0);
|
2009-09-23 15:00:09 +00:00
|
|
|
break;
|
2009-07-30 20:57:39 +00:00
|
|
|
default:
|
|
|
|
fatalx("unexpected message");
|
|
|
|
}
|
2009-08-14 21:04:04 +00:00
|
|
|
|
|
|
|
imsg_free(&imsg);
|
2009-07-30 20:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-23 17:32:26 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
client_suspend(void)
|
|
|
|
{
|
|
|
|
struct sigaction act;
|
|
|
|
|
|
|
|
memset(&act, 0, sizeof act);
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = SA_RESTART;
|
|
|
|
|
|
|
|
act.sa_handler = SIG_DFL;
|
|
|
|
if (sigaction(SIGTSTP, &act, NULL) != 0)
|
|
|
|
fatal("sigaction failed");
|
|
|
|
|
|
|
|
act.sa_handler = sighandler;
|
|
|
|
if (sigaction(SIGCONT, &act, NULL) != 0)
|
|
|
|
fatal("sigaction failed");
|
|
|
|
|
|
|
|
kill(getpid(), SIGTSTP);
|
|
|
|
}
|