2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2009-11-04 20:50:11 +00:00
|
|
|
#include <event.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-10-21 20:11:47 +00:00
|
|
|
struct imsgbuf client_ibuf;
|
2009-11-04 20:50:11 +00:00
|
|
|
struct event client_event;
|
2009-10-21 20:11:47 +00:00
|
|
|
const char *client_exitmsg;
|
2009-11-10 15:47:48 +00:00
|
|
|
int client_exitval;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
void client_send_identify(int);
|
|
|
|
void client_send_environ(void);
|
|
|
|
void client_write_server(enum msgtype, void *, size_t);
|
2009-11-04 22:57:49 +00:00
|
|
|
void client_update_event(void);
|
2009-11-04 20:50:11 +00:00
|
|
|
void client_signal(int, short, void *);
|
|
|
|
void client_callback(int, short, void *);
|
|
|
|
int client_dispatch(void);
|
2009-10-21 20:11:47 +00:00
|
|
|
|
|
|
|
struct imsgbuf *
|
|
|
|
client_init(char *path, int cmdflags, int flags)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-10-21 21:11:55 +00:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
size_t size;
|
|
|
|
int fd, mode;
|
|
|
|
char rpathbuf[MAXPATHLEN];
|
2009-06-05 07:15:58 +00:00
|
|
|
|
|
|
|
if (realpath(path, rpathbuf) == NULL)
|
|
|
|
strlcpy(rpathbuf, path, sizeof rpathbuf);
|
|
|
|
setproctitle("client (%s)", rpathbuf);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
sa.sun_family = AF_UNIX;
|
|
|
|
size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
|
|
|
|
if (size >= sizeof sa.sun_path) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
goto not_found;
|
|
|
|
}
|
|
|
|
|
2009-08-08 21:18:23 +00:00
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
2009-09-20 14:58:12 +00:00
|
|
|
fatal("socket failed");
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-08 21:18:23 +00:00
|
|
|
if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
|
2009-11-02 13:41:25 +00:00
|
|
|
if (!(cmdflags & CMD_STARTSERVER))
|
|
|
|
goto not_found;
|
|
|
|
switch (errno) {
|
|
|
|
case ECONNREFUSED:
|
|
|
|
if (unlink(path) != 0)
|
2009-06-01 22:58:49 +00:00
|
|
|
goto not_found;
|
2009-11-02 13:41:25 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case ENOENT:
|
2009-08-08 21:18:23 +00:00
|
|
|
if ((fd = server_start(path)) == -1)
|
2009-06-01 22:58:49 +00:00
|
|
|
goto start_failed;
|
|
|
|
goto server_started;
|
|
|
|
}
|
|
|
|
goto not_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_started:
|
2009-08-08 21:18:23 +00:00
|
|
|
if ((mode = fcntl(fd, F_GETFL)) == -1)
|
2009-06-01 22:58:49 +00:00
|
|
|
fatal("fcntl failed");
|
2009-08-08 21:18:23 +00:00
|
|
|
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
|
2009-06-01 22:58:49 +00:00
|
|
|
fatal("fcntl failed");
|
2009-09-23 12:03:30 +00:00
|
|
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
fatal("fcntl failed");
|
2009-10-21 20:11:47 +00:00
|
|
|
imsg_init(&client_ibuf, fd);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-08 21:52:43 +00:00
|
|
|
if (cmdflags & CMD_SENDENVIRON)
|
2009-10-21 20:11:47 +00:00
|
|
|
client_send_environ();
|
2010-06-28 22:10:42 +00:00
|
|
|
client_send_identify(flags);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-10-21 20:11:47 +00:00
|
|
|
return (&client_ibuf);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
start_failed:
|
|
|
|
log_warnx("server failed to start");
|
2009-10-21 20:11:47 +00:00
|
|
|
return (NULL);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
not_found:
|
|
|
|
log_warn("server not found");
|
2009-10-21 20:11:47 +00:00
|
|
|
return (NULL);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 21:11:55 +00:00
|
|
|
void
|
|
|
|
client_send_identify(int flags)
|
|
|
|
{
|
|
|
|
struct msg_identify_data data;
|
|
|
|
char *term;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
data.flags = flags;
|
|
|
|
|
|
|
|
if (getcwd(data.cwd, sizeof data.cwd) == NULL)
|
|
|
|
*data.cwd = '\0';
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-10-21 21:11:55 +00:00
|
|
|
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);
|
2010-06-28 22:10:42 +00:00
|
|
|
|
|
|
|
if ((fd = dup(STDOUT_FILENO)) == -1)
|
|
|
|
fatal("dup failed");
|
|
|
|
imsg_compose(&client_ibuf, MSG_STDOUT, PROTOCOL_VERSION, -1, fd, NULL, 0);
|
|
|
|
|
|
|
|
if ((fd = dup(STDERR_FILENO)) == -1)
|
|
|
|
fatal("dup failed");
|
|
|
|
imsg_compose(&client_ibuf, MSG_STDERR, PROTOCOL_VERSION, -1, fd, NULL, 0);
|
2009-10-21 21:11:55 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 21:52:43 +00:00
|
|
|
void
|
2009-10-21 20:11:47 +00:00
|
|
|
client_send_environ(void)
|
2009-08-08 21:52:43 +00:00
|
|
|
{
|
|
|
|
struct msg_environ_data data;
|
2009-10-21 21:11:55 +00:00
|
|
|
char **var;
|
2009-08-08 21:52:43 +00:00
|
|
|
|
2009-12-03 22:50:09 +00:00
|
|
|
for (var = environ; *var != NULL; var++) {
|
2009-08-08 21:52:43 +00:00
|
|
|
if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
|
|
|
|
continue;
|
2009-10-21 20:11:47 +00:00
|
|
|
client_write_server(MSG_ENVIRON, &data, sizeof data);
|
2009-08-08 21:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-21 20:11:47 +00:00
|
|
|
void
|
|
|
|
client_write_server(enum msgtype type, void *buf, size_t len)
|
|
|
|
{
|
2009-12-03 22:50:09 +00:00
|
|
|
imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
|
2009-10-21 20:11:47 +00:00
|
|
|
}
|
|
|
|
|
2009-11-04 22:57:49 +00:00
|
|
|
void
|
|
|
|
client_update_event(void)
|
|
|
|
{
|
|
|
|
short events;
|
|
|
|
|
|
|
|
event_del(&client_event);
|
|
|
|
events = EV_READ;
|
|
|
|
if (client_ibuf.w.queued > 0)
|
|
|
|
events |= EV_WRITE;
|
|
|
|
event_set(&client_event, client_ibuf.fd, events, client_callback, NULL);
|
|
|
|
event_add(&client_event, NULL);
|
|
|
|
}
|
|
|
|
|
2009-10-21 20:11:47 +00:00
|
|
|
__dead void
|
|
|
|
client_main(void)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
|
|
|
logfile("client");
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
/* Note: event_init() has already been called. */
|
|
|
|
|
2009-12-03 22:50:09 +00:00
|
|
|
/* Set up signals. */
|
2010-05-04 17:28:16 +00:00
|
|
|
set_signals(client_signal);
|
2009-11-04 20:50:11 +00:00
|
|
|
|
2010-06-05 16:47:11 +00:00
|
|
|
/*
|
|
|
|
* Send a resize message immediately in case the terminal size has
|
|
|
|
* changed between the identify message to the server and the MSG_READY
|
|
|
|
* telling us to move into the client code.
|
|
|
|
*/
|
|
|
|
client_write_server(MSG_RESIZE, NULL, 0);
|
|
|
|
|
2009-09-02 23:49:25 +00:00
|
|
|
/*
|
|
|
|
* imsg_read in the first client poll loop (before the terminal has
|
2009-11-04 20:50:11 +00:00
|
|
|
* been initialised) may have read messages into the buffer after the
|
|
|
|
* MSG_READY switched to here. Process anything outstanding now to
|
|
|
|
* avoid hanging waiting for messages that have already arrived.
|
2009-09-02 23:49:25 +00:00
|
|
|
*/
|
2009-10-21 20:11:47 +00:00
|
|
|
if (client_dispatch() != 0)
|
2009-09-02 23:49:25 +00:00
|
|
|
goto out;
|
|
|
|
|
2009-11-04 22:57:49 +00:00
|
|
|
/* Set the event and dispatch. */
|
2009-11-10 15:47:48 +00:00
|
|
|
client_update_event();
|
2009-11-04 20:50:11 +00:00
|
|
|
event_dispatch();
|
2009-09-02 23:49:25 +00:00
|
|
|
|
|
|
|
out:
|
2009-10-21 20:11:47 +00:00
|
|
|
/* Print the exit message, if any, and exit. */
|
2009-11-10 15:47:48 +00:00
|
|
|
if (client_exitmsg != NULL && !login_shell)
|
|
|
|
printf("[%s]\n", client_exitmsg);
|
|
|
|
exit(client_exitval);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-26 21:37:13 +00:00
|
|
|
/* ARGSUSED */
|
2009-11-04 20:50:11 +00:00
|
|
|
void
|
2009-11-04 22:57:49 +00:00
|
|
|
client_signal(int sig, unused short events, unused void *data)
|
2009-11-04 20:50:11 +00:00
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
switch (sig) {
|
2010-05-12 15:05:39 +00:00
|
|
|
case SIGHUP:
|
|
|
|
client_exitmsg = "lost tty";
|
|
|
|
client_exitval = 1;
|
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
break;
|
2009-11-04 20:50:11 +00:00
|
|
|
case SIGTERM:
|
|
|
|
client_exitmsg = "terminated";
|
2009-11-10 15:47:48 +00:00
|
|
|
client_exitval = 1;
|
2009-11-04 20:50:11 +00:00
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
break;
|
|
|
|
case SIGWINCH:
|
|
|
|
client_write_server(MSG_RESIZE, NULL, 0);
|
|
|
|
break;
|
|
|
|
case SIGCONT:
|
|
|
|
memset(&sigact, 0, sizeof sigact);
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = SA_RESTART;
|
|
|
|
sigact.sa_handler = SIG_IGN;
|
|
|
|
if (sigaction(SIGTSTP, &sigact, NULL) != 0)
|
|
|
|
fatal("sigaction failed");
|
|
|
|
client_write_server(MSG_WAKEUP, NULL, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-04 22:57:49 +00:00
|
|
|
client_update_event();
|
2009-11-04 20:50:11 +00:00
|
|
|
}
|
|
|
|
|
2009-11-26 21:37:13 +00:00
|
|
|
/* ARGSUSED */
|
2009-11-04 20:50:11 +00:00
|
|
|
void
|
|
|
|
client_callback(unused int fd, short events, unused void *data)
|
|
|
|
{
|
2009-11-13 18:07:52 +00:00
|
|
|
ssize_t n;
|
2009-11-04 20:50:11 +00:00
|
|
|
|
|
|
|
if (events & EV_READ) {
|
|
|
|
if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
|
|
|
|
goto lost_server;
|
|
|
|
if (client_dispatch() != 0) {
|
|
|
|
event_loopexit(NULL);
|
|
|
|
return;
|
2009-12-03 22:50:09 +00:00
|
|
|
}
|
2009-11-04 20:50:11 +00:00
|
|
|
}
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
if (events & EV_WRITE) {
|
|
|
|
if (msgbuf_write(&client_ibuf.w) < 0)
|
|
|
|
goto lost_server;
|
|
|
|
}
|
|
|
|
|
2009-11-04 22:57:49 +00:00
|
|
|
client_update_event();
|
2009-11-04 20:50:11 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
lost_server:
|
|
|
|
client_exitmsg = "lost server";
|
2009-11-10 15:47:48 +00:00
|
|
|
client_exitval = 1;
|
2009-11-04 20:50:11 +00:00
|
|
|
event_loopexit(NULL);
|
|
|
|
}
|
|
|
|
|
2009-07-30 16:32:12 +00:00
|
|
|
int
|
2009-10-21 20:11:47 +00:00
|
|
|
client_dispatch(void)
|
2009-07-30 16:32:12 +00:00
|
|
|
{
|
2009-11-04 20:50:11 +00:00
|
|
|
struct imsg imsg;
|
|
|
|
struct msg_lock_data lockdata;
|
|
|
|
struct sigaction sigact;
|
|
|
|
ssize_t n, datalen;
|
2009-08-11 17:18:35 +00:00
|
|
|
|
2009-07-30 16:32:12 +00:00
|
|
|
for (;;) {
|
2009-10-21 20:11:47 +00:00
|
|
|
if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
|
2009-08-11 17:18:35 +00:00
|
|
|
fatalx("imsg_get failed");
|
|
|
|
if (n == 0)
|
2009-07-30 16:32:12 +00:00
|
|
|
return (0);
|
2009-08-11 17:18:35 +00:00
|
|
|
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
|
2009-07-30 16:32:12 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
log_debug("client got %d", imsg.hdr.type);
|
2009-08-11 17:18:35 +00:00
|
|
|
switch (imsg.hdr.type) {
|
2009-07-30 16:32:12 +00:00
|
|
|
case MSG_DETACH:
|
2009-08-11 17:18:35 +00:00
|
|
|
if (datalen != 0)
|
2009-07-30 16:32:12 +00:00
|
|
|
fatalx("bad MSG_DETACH size");
|
|
|
|
|
2009-10-21 20:11:47 +00:00
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
client_exitmsg = "detached";
|
2009-07-30 16:32:12 +00:00
|
|
|
break;
|
|
|
|
case MSG_EXIT:
|
2009-08-11 17:18:35 +00:00
|
|
|
if (datalen != 0)
|
2009-07-30 16:32:12 +00:00
|
|
|
fatalx("bad MSG_EXIT size");
|
2009-08-11 17:18:35 +00:00
|
|
|
|
2009-10-21 20:11:47 +00:00
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
client_exitmsg = "exited";
|
2009-07-30 16:32:12 +00:00
|
|
|
break;
|
|
|
|
case MSG_EXITED:
|
2009-08-11 17:18:35 +00:00
|
|
|
if (datalen != 0)
|
2009-07-30 16:32:12 +00:00
|
|
|
fatalx("bad MSG_EXITED size");
|
|
|
|
|
2009-08-11 17:18:35 +00:00
|
|
|
imsg_free(&imsg);
|
2009-07-30 16:32:12 +00:00
|
|
|
return (-1);
|
|
|
|
case MSG_SHUTDOWN:
|
2009-08-11 17:18:35 +00:00
|
|
|
if (datalen != 0)
|
2009-07-30 16:32:12 +00:00
|
|
|
fatalx("bad MSG_SHUTDOWN size");
|
|
|
|
|
2009-10-21 20:11:47 +00:00
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
client_exitmsg = "server exited";
|
2009-11-10 15:47:48 +00:00
|
|
|
client_exitval = 1;
|
2009-07-30 16:32:12 +00:00
|
|
|
break;
|
|
|
|
case MSG_SUSPEND:
|
2009-08-11 17:18:35 +00:00
|
|
|
if (datalen != 0)
|
2009-07-30 16:32:12 +00:00
|
|
|
fatalx("bad MSG_SUSPEND size");
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
memset(&sigact, 0, sizeof sigact);
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = SA_RESTART;
|
|
|
|
sigact.sa_handler = SIG_DFL;
|
|
|
|
if (sigaction(SIGTSTP, &sigact, NULL) != 0)
|
|
|
|
fatal("sigaction failed");
|
|
|
|
kill(getpid(), SIGTSTP);
|
2009-07-30 16:32:12 +00:00
|
|
|
break;
|
2009-09-23 06:18:47 +00:00
|
|
|
case MSG_LOCK:
|
|
|
|
if (datalen != sizeof lockdata)
|
|
|
|
fatalx("bad MSG_LOCK size");
|
|
|
|
memcpy(&lockdata, imsg.data, sizeof lockdata);
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-09-23 06:18:47 +00:00
|
|
|
lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
|
|
|
|
system(lockdata.cmd);
|
2009-10-21 20:11:47 +00:00
|
|
|
client_write_server(MSG_UNLOCK, NULL, 0);
|
2009-09-23 06:18:47 +00:00
|
|
|
break;
|
2009-07-30 16:32:12 +00:00
|
|
|
default:
|
|
|
|
fatalx("unexpected message");
|
|
|
|
}
|
2009-08-11 17:18:35 +00:00
|
|
|
|
|
|
|
imsg_free(&imsg);
|
2009-07-30 16:32:12 +00:00
|
|
|
}
|
|
|
|
}
|