2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
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>
|
2012-03-29 21:10:41 +00:00
|
|
|
#include <sys/file.h>
|
2007-09-26 13:43:15 +00:00
|
|
|
#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>
|
2009-11-08 22:40:36 +00:00
|
|
|
#include <event.h>
|
2014-04-29 22:31:22 +00:00
|
|
|
#include <fcntl.h>
|
2014-10-20 23:27:14 +00:00
|
|
|
#include <signal.h>
|
2007-09-26 13:43:15 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-10-23 17:32:26 +00:00
|
|
|
struct imsgbuf client_ibuf;
|
2009-11-08 22:40:36 +00:00
|
|
|
struct event client_event;
|
2012-05-22 20:56:35 +00:00
|
|
|
struct event client_stdin;
|
2012-04-24 16:24:03 +00:00
|
|
|
enum {
|
|
|
|
CLIENT_EXIT_NONE,
|
|
|
|
CLIENT_EXIT_DETACHED,
|
|
|
|
CLIENT_EXIT_DETACHED_HUP,
|
|
|
|
CLIENT_EXIT_LOST_TTY,
|
|
|
|
CLIENT_EXIT_TERMINATED,
|
|
|
|
CLIENT_EXIT_LOST_SERVER,
|
|
|
|
CLIENT_EXIT_EXITED,
|
|
|
|
CLIENT_EXIT_SERVER_EXITED,
|
|
|
|
} client_exitreason = CLIENT_EXIT_NONE;
|
2009-11-10 23:28:53 +00:00
|
|
|
int client_exitval;
|
2011-03-19 23:27:35 +00:00
|
|
|
enum msgtype client_exittype;
|
2013-10-06 20:21:52 +00:00
|
|
|
const char *client_exitsession;
|
2010-10-24 01:31:08 +00:00
|
|
|
int client_attached;
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2012-03-18 01:28:10 +00:00
|
|
|
int client_get_lock(char *);
|
2010-10-24 01:31:08 +00:00
|
|
|
int client_connect(char *, int);
|
2009-11-08 22:40:36 +00:00
|
|
|
void client_send_identify(int);
|
2013-10-05 23:48:24 +00:00
|
|
|
int client_write_one(enum msgtype, int, const void *, size_t);
|
|
|
|
int client_write_server(enum msgtype, const void *, size_t);
|
2009-11-08 23:07:14 +00:00
|
|
|
void client_update_event(void);
|
2009-11-08 22:40:36 +00:00
|
|
|
void client_signal(int, short, void *);
|
2012-05-22 20:56:35 +00:00
|
|
|
void client_stdin_callback(int, short, void *);
|
2012-06-18 15:25:23 +00:00
|
|
|
void client_write(int, const char *, size_t);
|
2009-11-08 22:40:36 +00:00
|
|
|
void client_callback(int, short, void *);
|
2010-10-24 01:31:08 +00:00
|
|
|
int client_dispatch_attached(void);
|
|
|
|
int client_dispatch_wait(void *);
|
2012-04-24 16:24:37 +00:00
|
|
|
const char *client_exit_message(void);
|
2009-10-23 17:32:26 +00:00
|
|
|
|
2012-03-18 01:28:10 +00:00
|
|
|
/*
|
|
|
|
* Get server create lock. If already held then server start is happening in
|
|
|
|
* another client, so block until the lock is released and return -1 to
|
|
|
|
* retry. Ignore other errors - just continue and start the server without the
|
|
|
|
* lock.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
client_get_lock(char *lockfile)
|
|
|
|
{
|
|
|
|
int lockfd;
|
|
|
|
|
|
|
|
if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1)
|
|
|
|
fatal("open failed");
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("lock file is %s", lockfile);
|
2012-03-18 01:28:10 +00:00
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
|
|
|
|
log_debug("flock failed: %s", strerror(errno));
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
return (lockfd);
|
|
|
|
while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
|
2012-03-18 01:28:10 +00:00
|
|
|
/* nothing */;
|
|
|
|
close(lockfd);
|
|
|
|
return (-1);
|
|
|
|
}
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("flock succeeded");
|
2012-03-18 01:28:10 +00:00
|
|
|
|
|
|
|
return (lockfd);
|
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Connect client to server. */
|
|
|
|
int
|
|
|
|
client_connect(char *path, int start_server)
|
2007-09-26 13:43:15 +00:00
|
|
|
{
|
2009-10-23 17:37:41 +00:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
size_t size;
|
2014-07-21 10:52:48 +00:00
|
|
|
int fd, lockfd = -1, locked = 0;
|
|
|
|
char *lockfile = NULL;
|
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;
|
2010-10-24 01:31:08 +00:00
|
|
|
return (-1);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("socket is %s", path);
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2012-03-18 01:28:10 +00:00
|
|
|
retry:
|
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
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("trying connect");
|
2009-08-09 17:43:00 +00:00
|
|
|
if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("connect failed: %s", strerror(errno));
|
2012-03-18 01:28:10 +00:00
|
|
|
if (errno != ECONNREFUSED && errno != ENOENT)
|
2010-10-24 01:31:08 +00:00
|
|
|
goto failed;
|
2012-03-18 01:28:10 +00:00
|
|
|
if (!start_server)
|
2010-10-24 01:31:08 +00:00
|
|
|
goto failed;
|
2012-03-18 01:28:10 +00:00
|
|
|
close(fd);
|
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
if (!locked) {
|
|
|
|
xasprintf(&lockfile, "%s.lock", path);
|
|
|
|
if ((lockfd = client_get_lock(lockfile)) == -1) {
|
|
|
|
log_debug("didn't get lock");
|
|
|
|
free(lockfile);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
log_debug("got lock");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Always retry at least once, even if we got the lock,
|
|
|
|
* because another client could have taken the lock,
|
|
|
|
* started the server and released the lock between our
|
|
|
|
* connect() and flock().
|
|
|
|
*/
|
|
|
|
locked = 1;
|
2012-03-18 01:28:10 +00:00
|
|
|
goto retry;
|
2014-01-09 21:20:45 +00:00
|
|
|
}
|
2014-07-21 10:52:48 +00:00
|
|
|
|
2014-01-09 21:20:45 +00:00
|
|
|
if (unlink(path) != 0 && errno != ENOENT) {
|
|
|
|
free(lockfile);
|
|
|
|
close(lockfd);
|
2012-03-18 01:28:10 +00:00
|
|
|
return (-1);
|
2014-01-09 21:20:45 +00:00
|
|
|
}
|
2012-03-18 01:28:10 +00:00
|
|
|
fd = server_start(lockfd, lockfile);
|
2014-07-21 10:52:48 +00:00
|
|
|
}
|
|
|
|
if (locked) {
|
2012-07-11 19:34:16 +00:00
|
|
|
free(lockfile);
|
2012-03-18 01:28:10 +00:00
|
|
|
close(lockfd);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
2011-01-21 23:44:13 +00:00
|
|
|
setblocking(fd, 0);
|
2010-10-24 01:31:08 +00:00
|
|
|
return (fd);
|
|
|
|
|
|
|
|
failed:
|
|
|
|
close(fd);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2012-04-24 16:24:03 +00:00
|
|
|
/* Get exit string from reason number. */
|
|
|
|
const char *
|
|
|
|
client_exit_message(void)
|
|
|
|
{
|
2013-10-06 20:21:52 +00:00
|
|
|
static char msg[256];
|
|
|
|
|
2012-04-24 16:24:03 +00:00
|
|
|
switch (client_exitreason) {
|
|
|
|
case CLIENT_EXIT_NONE:
|
|
|
|
break;
|
|
|
|
case CLIENT_EXIT_DETACHED:
|
2013-10-06 20:21:52 +00:00
|
|
|
if (client_exitsession != NULL) {
|
|
|
|
xsnprintf(msg, sizeof msg, "detached "
|
|
|
|
"(from session %s)", client_exitsession);
|
|
|
|
return (msg);
|
|
|
|
}
|
2012-04-24 16:24:03 +00:00
|
|
|
return ("detached");
|
|
|
|
case CLIENT_EXIT_DETACHED_HUP:
|
2013-10-06 20:21:52 +00:00
|
|
|
if (client_exitsession != NULL) {
|
|
|
|
xsnprintf(msg, sizeof msg, "detached and SIGHUP "
|
|
|
|
"(from session %s)", client_exitsession);
|
|
|
|
return (msg);
|
|
|
|
}
|
2012-04-24 16:24:03 +00:00
|
|
|
return ("detached and SIGHUP");
|
|
|
|
case CLIENT_EXIT_LOST_TTY:
|
|
|
|
return ("lost tty");
|
|
|
|
case CLIENT_EXIT_TERMINATED:
|
|
|
|
return ("terminated");
|
|
|
|
case CLIENT_EXIT_LOST_SERVER:
|
|
|
|
return ("lost server");
|
|
|
|
case CLIENT_EXIT_EXITED:
|
|
|
|
return ("exited");
|
|
|
|
case CLIENT_EXIT_SERVER_EXITED:
|
|
|
|
return ("server exited");
|
|
|
|
}
|
|
|
|
return ("unknown reason");
|
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Client main loop. */
|
|
|
|
int
|
|
|
|
client_main(int argc, char **argv, int flags)
|
|
|
|
{
|
|
|
|
struct cmd *cmd;
|
|
|
|
struct cmd_list *cmdlist;
|
2013-10-05 23:48:24 +00:00
|
|
|
struct msg_command_data *data;
|
|
|
|
int cmdflags, fd, i;
|
2011-03-19 23:27:35 +00:00
|
|
|
pid_t ppid;
|
2010-10-24 01:31:08 +00:00
|
|
|
enum msgtype msg;
|
|
|
|
char *cause;
|
2012-06-18 15:23:01 +00:00
|
|
|
struct termios tio, saved_tio;
|
2013-10-05 23:48:24 +00:00
|
|
|
size_t size;
|
2010-10-24 01:31:08 +00:00
|
|
|
|
|
|
|
/* Set up the initial command. */
|
|
|
|
cmdflags = 0;
|
|
|
|
if (shell_cmd != NULL) {
|
|
|
|
msg = MSG_SHELL;
|
|
|
|
cmdflags = CMD_STARTSERVER;
|
|
|
|
} else if (argc == 0) {
|
|
|
|
msg = MSG_COMMAND;
|
2015-06-04 23:27:51 +00:00
|
|
|
cmdflags = CMD_STARTSERVER;
|
2010-10-24 01:31:08 +00:00
|
|
|
} else {
|
|
|
|
msg = MSG_COMMAND;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It sucks parsing the command string twice (in client and
|
|
|
|
* later in server) but it is necessary to get the start server
|
|
|
|
* flag.
|
|
|
|
*/
|
2013-02-23 22:25:58 +00:00
|
|
|
cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause);
|
|
|
|
if (cmdlist == NULL) {
|
2012-08-31 09:25:19 +00:00
|
|
|
fprintf(stderr, "%s\n", cause);
|
2010-10-24 01:31:08 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
cmdflags &= ~CMD_STARTSERVER;
|
|
|
|
TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
|
|
|
|
if (cmd->entry->flags & CMD_STARTSERVER)
|
|
|
|
cmdflags |= CMD_STARTSERVER;
|
|
|
|
}
|
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
}
|
|
|
|
|
2015-04-24 20:58:44 +00:00
|
|
|
/* Establish signal handlers. */
|
|
|
|
set_signals(client_signal);
|
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
/* Initialize the client socket and start the server. */
|
2010-10-24 01:31:08 +00:00
|
|
|
fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER);
|
|
|
|
if (fd == -1) {
|
2015-04-21 22:21:41 +00:00
|
|
|
if (errno == ECONNREFUSED) {
|
|
|
|
fprintf(stderr, "no server running on %s\n",
|
|
|
|
socket_path);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error connecting to %s (%s)\n",
|
|
|
|
socket_path, strerror(errno));
|
|
|
|
}
|
2010-10-24 01:31:08 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set process title, log and signals now this is the client. */
|
2010-10-24 19:54:41 +00:00
|
|
|
#ifdef HAVE_SETPROCTITLE
|
2010-10-24 01:31:08 +00:00
|
|
|
setproctitle("client (%s)", socket_path);
|
2010-10-24 19:54:41 +00:00
|
|
|
#endif
|
2010-10-24 01:31:08 +00:00
|
|
|
logfile("client");
|
|
|
|
|
|
|
|
/* Create imsg. */
|
2009-10-23 17:32:26 +00:00
|
|
|
imsg_init(&client_ibuf, fd);
|
2010-10-24 01:31:08 +00:00
|
|
|
event_set(&client_event, fd, EV_READ, client_callback, shell_cmd);
|
|
|
|
|
2012-05-22 20:56:35 +00:00
|
|
|
/* Create stdin handler. */
|
|
|
|
setblocking(STDIN_FILENO, 0);
|
|
|
|
event_set(&client_stdin, STDIN_FILENO, EV_READ|EV_PERSIST,
|
|
|
|
client_stdin_callback, NULL);
|
2013-10-05 23:10:40 +00:00
|
|
|
if (flags & CLIENT_CONTROLCONTROL) {
|
2012-06-18 15:23:01 +00:00
|
|
|
if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) {
|
|
|
|
fprintf(stderr, "tcgetattr failed: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
cfmakeraw(&tio);
|
|
|
|
tio.c_iflag = ICRNL|IXANY;
|
|
|
|
tio.c_oflag = OPOST|ONLCR;
|
2012-06-18 16:23:42 +00:00
|
|
|
#ifdef NOKERNINFO
|
2012-06-18 15:23:01 +00:00
|
|
|
tio.c_lflag = NOKERNINFO;
|
2012-06-18 16:23:42 +00:00
|
|
|
#endif
|
2012-06-18 15:23:01 +00:00
|
|
|
tio.c_cflag = CREAD|CS8|HUPCL;
|
|
|
|
tio.c_cc[VMIN] = 1;
|
|
|
|
tio.c_cc[VTIME] = 0;
|
|
|
|
cfsetispeed(&tio, cfgetispeed(&saved_tio));
|
|
|
|
cfsetospeed(&tio, cfgetospeed(&saved_tio));
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &tio);
|
|
|
|
}
|
2012-05-22 20:56:35 +00:00
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
/* Send identify messages. */
|
2010-07-02 02:52:13 +00:00
|
|
|
client_send_identify(flags);
|
2007-10-03 21:31:07 +00:00
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Send first command. */
|
|
|
|
if (msg == MSG_COMMAND) {
|
2013-10-05 23:48:24 +00:00
|
|
|
/* How big is the command? */
|
|
|
|
size = 0;
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
size += strlen(argv[i]) + 1;
|
|
|
|
data = xmalloc((sizeof *data) + size);
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Prepare command for server. */
|
2013-10-05 23:48:24 +00:00
|
|
|
data->argc = argc;
|
2014-09-01 21:50:18 +00:00
|
|
|
if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) {
|
2012-05-30 13:42:57 +00:00
|
|
|
fprintf(stderr, "command too long\n");
|
2013-10-05 23:48:24 +00:00
|
|
|
free(data);
|
2010-10-24 01:31:08 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2013-10-05 23:48:24 +00:00
|
|
|
size += sizeof *data;
|
2010-10-24 01:31:08 +00:00
|
|
|
|
2013-10-05 23:48:24 +00:00
|
|
|
/* Send the command. */
|
|
|
|
if (client_write_server(msg, data, size) != 0) {
|
|
|
|
fprintf(stderr, "failed to send command\n");
|
|
|
|
free(data);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
free(data);
|
2010-10-24 01:31:08 +00:00
|
|
|
} else if (msg == MSG_SHELL)
|
|
|
|
client_write_server(msg, NULL, 0);
|
2007-12-01 11:10:33 +00:00
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Set the event and dispatch. */
|
|
|
|
client_update_event();
|
|
|
|
event_dispatch();
|
2008-06-07 07:27:28 +00:00
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Print the exit message, if any, and exit. */
|
2011-03-19 23:27:35 +00:00
|
|
|
if (client_attached) {
|
2012-04-24 16:24:03 +00:00
|
|
|
if (client_exitreason != CLIENT_EXIT_NONE && !login_shell)
|
|
|
|
printf("[%s]\n", client_exit_message());
|
2011-03-19 23:27:35 +00:00
|
|
|
|
|
|
|
ppid = getppid();
|
|
|
|
if (client_exittype == MSG_DETACHKILL && ppid > 1)
|
|
|
|
kill(ppid, SIGHUP);
|
2013-10-05 23:10:40 +00:00
|
|
|
} else if (flags & CLIENT_CONTROLCONTROL) {
|
|
|
|
if (client_exitreason != CLIENT_EXIT_NONE)
|
|
|
|
printf("%%exit %s\n", client_exit_message());
|
|
|
|
else
|
|
|
|
printf("%%exit\n");
|
|
|
|
printf("\033\\");
|
2012-06-18 15:23:01 +00:00
|
|
|
tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
|
2013-03-04 08:52:41 +00:00
|
|
|
}
|
2012-05-22 20:56:35 +00:00
|
|
|
setblocking(STDIN_FILENO, 1);
|
2010-10-24 01:31:08 +00:00
|
|
|
return (client_exitval);
|
2007-09-28 19:04:21 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
/* Send identify messages to server. */
|
2009-10-23 17:37:41 +00:00
|
|
|
void
|
|
|
|
client_send_identify(int flags)
|
|
|
|
{
|
2015-06-14 10:07:44 +00:00
|
|
|
const char *s;
|
2013-10-06 20:02:23 +00:00
|
|
|
char **ss;
|
2015-06-14 10:07:44 +00:00
|
|
|
int fd;
|
|
|
|
pid_t pid;
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
client_write_one(MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
if ((s = getenv("TERM")) == NULL)
|
|
|
|
s = "";
|
|
|
|
client_write_one(MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1);
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
if ((s = ttyname(STDIN_FILENO)) == NULL)
|
|
|
|
s = "";
|
|
|
|
client_write_one(MSG_IDENTIFY_TTYNAME, -1, s, strlen(s) + 1);
|
|
|
|
|
|
|
|
if ((fd = open(".", O_RDONLY)) == -1)
|
|
|
|
fd = open("/", O_RDONLY);
|
|
|
|
client_write_one(MSG_IDENTIFY_CWD, fd, NULL, 0);
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2012-03-29 21:02:34 +00:00
|
|
|
if ((fd = dup(STDIN_FILENO)) == -1)
|
|
|
|
fatal("dup failed");
|
2013-10-06 20:02:23 +00:00
|
|
|
client_write_one(MSG_IDENTIFY_STDIN, fd, NULL, 0);
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2015-06-14 10:07:44 +00:00
|
|
|
pid = getpid();
|
|
|
|
client_write_one(MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid);
|
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
for (ss = environ; *ss != NULL; ss++)
|
|
|
|
client_write_one(MSG_IDENTIFY_ENVIRON, -1, *ss, strlen(*ss) + 1);
|
2009-08-09 17:48:55 +00:00
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
client_write_one(MSG_IDENTIFY_DONE, -1, NULL, 0);
|
|
|
|
|
|
|
|
client_update_event();
|
2009-08-09 17:48:55 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 23:48:24 +00:00
|
|
|
/* Helper to send one message. */
|
|
|
|
int
|
|
|
|
client_write_one(enum msgtype type, int fd, const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, fd,
|
2014-09-01 21:50:18 +00:00
|
|
|
(void *)buf, len);
|
2013-10-05 23:48:24 +00:00
|
|
|
if (retval != 1)
|
|
|
|
return (-1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Write a message to the server without a file descriptor. */
|
2013-10-05 23:48:24 +00:00
|
|
|
int
|
|
|
|
client_write_server(enum msgtype type, const void *buf, size_t len)
|
2009-10-23 17:32:26 +00:00
|
|
|
{
|
2013-10-05 23:48:24 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = client_write_one(type, -1, buf, len);
|
|
|
|
if (retval == 0)
|
|
|
|
client_update_event();
|
|
|
|
return (retval);
|
2009-10-23 17:32:26 +00:00
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Update client event based on whether it needs to read or read and write. */
|
2009-11-08 23:07:14 +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;
|
2010-10-24 01:31:08 +00:00
|
|
|
event_set(
|
|
|
|
&client_event, client_ibuf.fd, events, client_callback, shell_cmd);
|
2009-11-08 23:07:14 +00:00
|
|
|
event_add(&client_event, NULL);
|
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Callback to handle signals in the client. */
|
2009-11-08 22:40:36 +00:00
|
|
|
void
|
2009-11-08 23:07:14 +00:00
|
|
|
client_signal(int sig, unused short events, unused void *data)
|
2009-11-08 22:40:36 +00:00
|
|
|
{
|
2010-10-24 01:31:08 +00:00
|
|
|
struct sigaction sigact;
|
|
|
|
int status;
|
2009-11-08 22:40:36 +00:00
|
|
|
|
2014-10-01 23:23:19 +00:00
|
|
|
if (sig == SIGCHLD)
|
|
|
|
waitpid(WAIT_ANY, &status, WNOHANG);
|
|
|
|
else if (!client_attached) {
|
|
|
|
if (sig == SIGTERM)
|
2010-10-24 01:31:08 +00:00
|
|
|
event_loopexit(NULL);
|
|
|
|
} else {
|
|
|
|
switch (sig) {
|
|
|
|
case SIGHUP:
|
2012-04-24 16:24:03 +00:00
|
|
|
client_exitreason = CLIENT_EXIT_LOST_TTY;
|
2010-10-24 01:31:08 +00:00
|
|
|
client_exitval = 1;
|
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
break;
|
|
|
|
case SIGTERM:
|
2012-04-24 16:24:03 +00:00
|
|
|
client_exitreason = CLIENT_EXIT_TERMINATED;
|
2010-10-24 01:31:08 +00:00
|
|
|
client_exitval = 1;
|
|
|
|
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-08 22:40:36 +00:00
|
|
|
}
|
|
|
|
|
2009-11-08 23:07:14 +00:00
|
|
|
client_update_event();
|
2009-11-08 22:40:36 +00:00
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Callback for client imsg read events. */
|
2009-11-08 22:40:36 +00:00
|
|
|
void
|
2010-10-24 01:31:08 +00:00
|
|
|
client_callback(unused int fd, short events, void *data)
|
2009-11-08 22:40:36 +00:00
|
|
|
{
|
2009-11-14 17:49:37 +00:00
|
|
|
ssize_t n;
|
2010-10-24 01:31:08 +00:00
|
|
|
int retval;
|
2009-11-08 22:40:36 +00:00
|
|
|
|
|
|
|
if (events & EV_READ) {
|
|
|
|
if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
|
|
|
|
goto lost_server;
|
2010-10-24 01:31:08 +00:00
|
|
|
if (client_attached)
|
|
|
|
retval = client_dispatch_attached();
|
|
|
|
else
|
|
|
|
retval = client_dispatch_wait(data);
|
|
|
|
if (retval != 0) {
|
2009-11-08 22:40:36 +00:00
|
|
|
event_loopexit(NULL);
|
|
|
|
return;
|
2009-12-04 22:14:47 +00:00
|
|
|
}
|
2009-11-08 22:40:36 +00:00
|
|
|
}
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2009-11-08 22:40:36 +00:00
|
|
|
if (events & EV_WRITE) {
|
2014-07-13 20:51:08 +00:00
|
|
|
if (msgbuf_write(&client_ibuf.w) <= 0 && errno != EAGAIN)
|
2009-11-08 22:40:36 +00:00
|
|
|
goto lost_server;
|
|
|
|
}
|
|
|
|
|
2009-11-08 23:07:14 +00:00
|
|
|
client_update_event();
|
2009-11-08 22:40:36 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
lost_server:
|
2012-04-24 16:24:03 +00:00
|
|
|
client_exitreason = CLIENT_EXIT_LOST_SERVER;
|
2009-11-10 23:28:53 +00:00
|
|
|
client_exitval = 1;
|
2009-11-08 22:40:36 +00:00
|
|
|
event_loopexit(NULL);
|
|
|
|
}
|
|
|
|
|
2012-05-22 20:56:35 +00:00
|
|
|
/* Callback for client stdin read events. */
|
|
|
|
void
|
|
|
|
client_stdin_callback(unused int fd, unused short events, unused void *data1)
|
|
|
|
{
|
|
|
|
struct msg_stdin_data data;
|
|
|
|
|
|
|
|
data.size = read(STDIN_FILENO, data.data, sizeof data.data);
|
|
|
|
if (data.size < 0 && (errno == EINTR || errno == EAGAIN))
|
|
|
|
return;
|
|
|
|
|
|
|
|
client_write_server(MSG_STDIN, &data, sizeof data);
|
|
|
|
if (data.size <= 0)
|
|
|
|
event_del(&client_stdin);
|
|
|
|
client_update_event();
|
|
|
|
}
|
|
|
|
|
2012-06-18 15:25:23 +00:00
|
|
|
/* Force write to file descriptor. */
|
|
|
|
void
|
|
|
|
client_write(int fd, const char *data, size_t size)
|
|
|
|
{
|
|
|
|
ssize_t used;
|
|
|
|
|
|
|
|
while (size != 0) {
|
|
|
|
used = write(fd, data, size);
|
|
|
|
if (used == -1) {
|
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data += used;
|
|
|
|
size -= used;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Dispatch imsgs when in wait state (before MSG_READY). */
|
|
|
|
int
|
2013-10-05 23:28:35 +00:00
|
|
|
client_dispatch_wait(void *data0)
|
2010-10-24 01:31:08 +00:00
|
|
|
{
|
2013-10-05 23:28:35 +00:00
|
|
|
struct imsg imsg;
|
|
|
|
char *data;
|
|
|
|
ssize_t n, datalen;
|
|
|
|
struct msg_stdout_data stdoutdata;
|
|
|
|
struct msg_stderr_data stderrdata;
|
|
|
|
int retval;
|
2010-10-24 01:31:08 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
|
|
|
|
fatalx("imsg_get failed");
|
|
|
|
if (n == 0)
|
|
|
|
return (0);
|
2013-10-05 23:28:35 +00:00
|
|
|
|
|
|
|
data = imsg.data;
|
2010-10-24 01:31:08 +00:00
|
|
|
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
|
|
|
|
|
2015-03-31 17:45:10 +00:00
|
|
|
log_debug("got %u from server", imsg.hdr.type);
|
2010-10-24 01:31:08 +00:00
|
|
|
switch (imsg.hdr.type) {
|
|
|
|
case MSG_EXIT:
|
|
|
|
case MSG_SHUTDOWN:
|
2013-10-05 23:28:35 +00:00
|
|
|
if (datalen != sizeof retval && datalen != 0)
|
|
|
|
fatalx("bad MSG_EXIT size");
|
|
|
|
if (datalen == sizeof retval) {
|
|
|
|
memcpy(&retval, data, sizeof retval);
|
|
|
|
client_exitval = retval;
|
2010-10-24 01:31:08 +00:00
|
|
|
}
|
|
|
|
imsg_free(&imsg);
|
|
|
|
return (-1);
|
|
|
|
case MSG_READY:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_READY size");
|
|
|
|
|
2012-05-22 20:56:35 +00:00
|
|
|
event_del(&client_stdin);
|
2010-10-24 01:31:08 +00:00
|
|
|
client_attached = 1;
|
2013-03-11 21:30:48 +00:00
|
|
|
client_write_server(MSG_RESIZE, NULL, 0);
|
2010-10-24 01:31:08 +00:00
|
|
|
break;
|
2012-09-03 08:48:57 +00:00
|
|
|
case MSG_STDIN:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_STDIN size");
|
|
|
|
|
|
|
|
event_add(&client_stdin, NULL);
|
|
|
|
break;
|
2012-05-22 20:56:35 +00:00
|
|
|
case MSG_STDOUT:
|
|
|
|
if (datalen != sizeof stdoutdata)
|
2013-10-05 23:28:35 +00:00
|
|
|
fatalx("bad MSG_STDOUT size");
|
|
|
|
memcpy(&stdoutdata, data, sizeof stdoutdata);
|
2012-05-22 20:56:35 +00:00
|
|
|
|
2013-10-05 23:48:24 +00:00
|
|
|
client_write(STDOUT_FILENO, stdoutdata.data,
|
|
|
|
stdoutdata.size);
|
2012-05-22 20:56:35 +00:00
|
|
|
break;
|
|
|
|
case MSG_STDERR:
|
|
|
|
if (datalen != sizeof stderrdata)
|
2013-10-05 23:28:35 +00:00
|
|
|
fatalx("bad MSG_STDERR size");
|
|
|
|
memcpy(&stderrdata, data, sizeof stderrdata);
|
2012-05-22 20:56:35 +00:00
|
|
|
|
2013-10-05 23:48:24 +00:00
|
|
|
client_write(STDERR_FILENO, stderrdata.data,
|
|
|
|
stderrdata.size);
|
2012-05-22 20:56:35 +00:00
|
|
|
break;
|
2010-10-24 01:31:08 +00:00
|
|
|
case MSG_VERSION:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_VERSION size");
|
|
|
|
|
2012-05-22 20:56:35 +00:00
|
|
|
fprintf(stderr, "protocol version mismatch "
|
2015-03-31 17:45:10 +00:00
|
|
|
"(client %d, server %u)\n", PROTOCOL_VERSION,
|
2012-05-22 20:56:35 +00:00
|
|
|
imsg.hdr.peerid);
|
2010-10-24 01:31:08 +00:00
|
|
|
client_exitval = 1;
|
|
|
|
|
|
|
|
imsg_free(&imsg);
|
|
|
|
return (-1);
|
|
|
|
case MSG_SHELL:
|
2013-10-06 20:20:11 +00:00
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
2013-10-05 23:28:35 +00:00
|
|
|
fatalx("bad MSG_SHELL string");
|
2010-10-24 01:31:08 +00:00
|
|
|
|
|
|
|
clear_signals(0);
|
2013-10-05 23:28:35 +00:00
|
|
|
shell_exec(data, data0);
|
2010-10-24 01:31:08 +00:00
|
|
|
/* NOTREACHED */
|
2012-06-18 15:23:01 +00:00
|
|
|
case MSG_DETACH:
|
2013-10-06 20:21:52 +00:00
|
|
|
case MSG_DETACHKILL:
|
2012-06-18 15:23:01 +00:00
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
|
|
|
break;
|
|
|
|
case MSG_EXITED:
|
|
|
|
imsg_free(&imsg);
|
|
|
|
return (-1);
|
2010-10-24 01:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
imsg_free(&imsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dispatch imsgs in attached state (after MSG_READY). */
|
2009-07-30 20:57:39 +00:00
|
|
|
int
|
2010-10-24 01:31:08 +00:00
|
|
|
client_dispatch_attached(void)
|
2009-07-30 20:57:39 +00:00
|
|
|
{
|
2013-10-05 23:28:35 +00:00
|
|
|
struct imsg imsg;
|
|
|
|
struct sigaction sigact;
|
|
|
|
char *data;
|
|
|
|
ssize_t n, datalen;
|
2009-08-14 21:04:04 +00:00
|
|
|
|
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);
|
2013-10-05 23:28:35 +00:00
|
|
|
|
|
|
|
data = imsg.data;
|
2009-08-14 21:04:04 +00:00
|
|
|
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
|
2009-07-30 20:57:39 +00:00
|
|
|
|
2015-03-31 17:45:10 +00:00
|
|
|
log_debug("got %u from server", imsg.hdr.type);
|
2009-08-14 21:04:04 +00:00
|
|
|
switch (imsg.hdr.type) {
|
2009-07-30 20:57:39 +00:00
|
|
|
case MSG_DETACH:
|
2013-10-06 20:21:52 +00:00
|
|
|
case MSG_DETACHKILL:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
|
|
|
fatalx("bad MSG_DETACH string");
|
2009-07-30 20:57:39 +00:00
|
|
|
|
2013-10-06 20:21:52 +00:00
|
|
|
client_exitsession = xstrdup(data);
|
2011-03-19 23:27:35 +00:00
|
|
|
client_exittype = imsg.hdr.type;
|
|
|
|
if (imsg.hdr.type == MSG_DETACHKILL)
|
2012-04-24 16:24:03 +00:00
|
|
|
client_exitreason = CLIENT_EXIT_DETACHED_HUP;
|
2011-03-19 23:27:35 +00:00
|
|
|
else
|
2012-04-24 16:24:03 +00:00
|
|
|
client_exitreason = CLIENT_EXIT_DETACHED;
|
2009-10-23 17:32:26 +00:00
|
|
|
client_write_server(MSG_EXITING, NULL, 0);
|
2009-07-30 20:57:39 +00:00
|
|
|
break;
|
|
|
|
case MSG_EXIT:
|
2013-10-05 23:28:35 +00:00
|
|
|
if (datalen != 0 && datalen != sizeof (int))
|
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);
|
2012-04-24 16:24:03 +00:00
|
|
|
client_exitreason = CLIENT_EXIT_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);
|
2012-04-24 16:24:03 +00:00
|
|
|
client_exitreason = CLIENT_EXIT_SERVER_EXITED;
|
2009-11-10 23:28:53 +00:00
|
|
|
client_exitval = 1;
|
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");
|
|
|
|
|
2009-11-08 22:40:36 +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 20:57:39 +00:00
|
|
|
break;
|
2009-09-23 15:00:09 +00:00
|
|
|
case MSG_LOCK:
|
2013-10-06 20:20:11 +00:00
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
2013-10-05 23:28:35 +00:00
|
|
|
fatalx("bad MSG_LOCK string");
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2013-10-05 23:28:35 +00:00
|
|
|
system(data);
|
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
|
|
|
}
|
2009-08-14 21:04:04 +00:00
|
|
|
|
|
|
|
imsg_free(&imsg);
|
2009-07-30 20:57:39 +00:00
|
|
|
}
|
|
|
|
}
|