2009-06-02 00:58:49 +02: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>
|
2012-03-24 12:23:19 +01:00
|
|
|
#include <sys/file.h>
|
2009-06-02 00:58:49 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2009-11-04 21:50:11 +01:00
|
|
|
#include <event.h>
|
2014-04-30 00:31:22 +02:00
|
|
|
#include <fcntl.h>
|
2015-10-27 14:23:24 +01:00
|
|
|
#include <imsg.h>
|
2014-10-21 01:27:14 +02:00
|
|
|
#include <signal.h>
|
2009-06-02 00:58:49 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
struct tmuxproc *client_proc;
|
|
|
|
struct tmuxpeer *client_peer;
|
|
|
|
int client_flags;
|
|
|
|
struct event client_stdin;
|
2012-04-24 01:18:43 +02: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;
|
2015-10-27 14:23:24 +01:00
|
|
|
int client_exitval;
|
|
|
|
enum msgtype client_exittype;
|
|
|
|
const char *client_exitsession;
|
|
|
|
int client_attached;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2015-08-31 00:40:25 +02:00
|
|
|
__dead void client_exec(const char *);
|
2012-03-09 10:57:40 +01:00
|
|
|
int client_get_lock(char *);
|
2015-08-31 00:19:07 +02:00
|
|
|
int client_connect(struct event_base *, char *, int);
|
2015-10-18 22:42:42 +02:00
|
|
|
void client_send_identify(const char *, const char *);
|
2012-05-21 20:27:42 +02:00
|
|
|
void client_stdin_callback(int, short, void *);
|
2012-06-18 15:34:56 +02:00
|
|
|
void client_write(int, const char *, size_t);
|
2015-10-27 14:23:24 +01:00
|
|
|
void client_signal(int);
|
|
|
|
void client_dispatch(struct imsg *, void *);
|
|
|
|
void client_dispatch_attached(struct imsg *);
|
|
|
|
void client_dispatch_wait(struct imsg *);
|
2012-04-24 01:25:02 +02:00
|
|
|
const char *client_exit_message(void);
|
2009-10-21 22:11:47 +02:00
|
|
|
|
2012-03-09 10:57:40 +01: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 12:52:48 +02:00
|
|
|
log_debug("lock file is %s", lockfile);
|
2012-03-09 10:57:40 +01:00
|
|
|
|
2014-07-21 12:52:48 +02: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-09 10:57:40 +01:00
|
|
|
/* nothing */;
|
|
|
|
close(lockfd);
|
|
|
|
return (-1);
|
|
|
|
}
|
2014-07-21 12:52:48 +02:00
|
|
|
log_debug("flock succeeded");
|
2012-03-09 10:57:40 +01:00
|
|
|
|
|
|
|
return (lockfd);
|
|
|
|
}
|
|
|
|
|
2010-10-18 22:00:02 +02:00
|
|
|
/* Connect client to server. */
|
|
|
|
int
|
2015-08-31 00:19:07 +02:00
|
|
|
client_connect(struct event_base *base, char *path, int start_server)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
2009-10-21 23:11:55 +02:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
size_t size;
|
2014-07-21 12:52:48 +02:00
|
|
|
int fd, lockfd = -1, locked = 0;
|
|
|
|
char *lockfile = NULL;
|
2009-06-02 00:58:49 +02: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;
|
2010-10-18 22:00:02 +02:00
|
|
|
return (-1);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
2014-07-21 12:52:48 +02:00
|
|
|
log_debug("socket is %s", path);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2012-03-09 10:57:40 +01:00
|
|
|
retry:
|
2009-08-08 23:18:23 +02:00
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
2009-09-20 16:58:12 +02:00
|
|
|
fatal("socket failed");
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2014-07-21 12:52:48 +02:00
|
|
|
log_debug("trying connect");
|
2015-10-11 02:26:23 +02:00
|
|
|
if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
|
2014-07-21 12:52:48 +02:00
|
|
|
log_debug("connect failed: %s", strerror(errno));
|
2012-03-09 10:57:40 +01:00
|
|
|
if (errno != ECONNREFUSED && errno != ENOENT)
|
2010-10-18 22:00:02 +02:00
|
|
|
goto failed;
|
2012-03-09 10:57:40 +01:00
|
|
|
if (!start_server)
|
2010-10-18 22:00:02 +02:00
|
|
|
goto failed;
|
2012-03-09 10:57:40 +01:00
|
|
|
close(fd);
|
|
|
|
|
2014-07-21 12:52:48 +02: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-09 10:57:40 +01:00
|
|
|
goto retry;
|
2014-01-09 22:20:45 +01:00
|
|
|
}
|
2014-07-21 12:52:48 +02:00
|
|
|
|
2014-01-09 22:20:45 +01:00
|
|
|
if (unlink(path) != 0 && errno != ENOENT) {
|
|
|
|
free(lockfile);
|
|
|
|
close(lockfd);
|
2012-03-09 10:57:40 +01:00
|
|
|
return (-1);
|
2014-01-09 22:20:45 +01:00
|
|
|
}
|
2015-08-31 00:19:07 +02:00
|
|
|
fd = server_start(base, lockfd, lockfile);
|
2014-07-21 12:52:48 +02:00
|
|
|
}
|
2015-09-24 14:06:20 +02:00
|
|
|
|
2014-07-21 12:52:48 +02:00
|
|
|
if (locked) {
|
2012-07-10 13:53:01 +02:00
|
|
|
free(lockfile);
|
2012-03-09 10:57:40 +01:00
|
|
|
close(lockfd);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
2011-01-08 02:52:36 +01:00
|
|
|
setblocking(fd, 0);
|
2010-10-18 22:00:02 +02:00
|
|
|
return (fd);
|
|
|
|
|
|
|
|
failed:
|
2015-09-24 14:06:20 +02:00
|
|
|
if (locked) {
|
|
|
|
free(lockfile);
|
|
|
|
close(lockfd);
|
|
|
|
}
|
2010-10-18 22:00:02 +02:00
|
|
|
close(fd);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2012-04-24 01:18:43 +02:00
|
|
|
/* Get exit string from reason number. */
|
|
|
|
const char *
|
|
|
|
client_exit_message(void)
|
|
|
|
{
|
2013-10-10 14:28:08 +02:00
|
|
|
static char msg[256];
|
|
|
|
|
2012-04-24 01:18:43 +02:00
|
|
|
switch (client_exitreason) {
|
|
|
|
case CLIENT_EXIT_NONE:
|
|
|
|
break;
|
|
|
|
case CLIENT_EXIT_DETACHED:
|
2013-10-10 14:28:08 +02:00
|
|
|
if (client_exitsession != NULL) {
|
|
|
|
xsnprintf(msg, sizeof msg, "detached "
|
|
|
|
"(from session %s)", client_exitsession);
|
|
|
|
return (msg);
|
|
|
|
}
|
2012-04-24 01:18:43 +02:00
|
|
|
return ("detached");
|
|
|
|
case CLIENT_EXIT_DETACHED_HUP:
|
2013-10-10 14:28:08 +02:00
|
|
|
if (client_exitsession != NULL) {
|
|
|
|
xsnprintf(msg, sizeof msg, "detached and SIGHUP "
|
|
|
|
"(from session %s)", client_exitsession);
|
|
|
|
return (msg);
|
|
|
|
}
|
2012-04-24 01:18:43 +02: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-18 22:00:02 +02:00
|
|
|
/* Client main loop. */
|
|
|
|
int
|
2015-08-31 00:19:07 +02:00
|
|
|
client_main(struct event_base *base, int argc, char **argv, int flags)
|
2010-10-18 22:00:02 +02:00
|
|
|
{
|
|
|
|
struct cmd *cmd;
|
|
|
|
struct cmd_list *cmdlist;
|
2013-10-10 14:13:56 +02:00
|
|
|
struct msg_command_data *data;
|
2015-10-18 22:42:42 +02:00
|
|
|
int cmdflags, fd, i;
|
|
|
|
const char *ttynam, *cwd;
|
2011-03-03 09:51:47 +01:00
|
|
|
pid_t ppid;
|
2010-10-18 22:00:02 +02:00
|
|
|
enum msgtype msg;
|
2015-10-18 22:42:42 +02:00
|
|
|
char *cause, path[PATH_MAX];
|
2012-06-18 15:16:42 +02:00
|
|
|
struct termios tio, saved_tio;
|
2013-10-10 14:13:56 +02:00
|
|
|
size_t size;
|
2010-10-18 22:00:02 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
/* Ignore SIGCHLD now or daemon() in the server will leave a zombie. */
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
|
2015-08-31 00:40:25 +02:00
|
|
|
/* Save the flags. */
|
|
|
|
client_flags = flags;
|
|
|
|
|
2010-10-18 22:00:02 +02: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-05 01:27:51 +02:00
|
|
|
cmdflags = CMD_STARTSERVER;
|
2010-10-18 22:00:02 +02: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-03-24 10:54:10 +01:00
|
|
|
cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause);
|
|
|
|
if (cmdlist == NULL) {
|
2012-08-27 23:35:11 +02:00
|
|
|
fprintf(stderr, "%s\n", cause);
|
2010-10-18 22:00:02 +02: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);
|
|
|
|
}
|
|
|
|
|
2014-07-21 12:52:48 +02:00
|
|
|
/* Initialize the client socket and start the server. */
|
2015-08-31 00:19:07 +02:00
|
|
|
fd = client_connect(base, socket_path, cmdflags & CMD_STARTSERVER);
|
2010-10-18 22:00:02 +02:00
|
|
|
if (fd == -1) {
|
2015-04-22 00:21:41 +02: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-18 22:00:02 +02:00
|
|
|
return (1);
|
|
|
|
}
|
2015-10-17 20:30:43 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
/* Build process state. */
|
|
|
|
client_proc = proc_start("client", base, 0, client_signal);
|
|
|
|
client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL);
|
|
|
|
|
2015-10-17 20:30:43 +02:00
|
|
|
/* Save these before pledge(). */
|
2015-10-31 09:13:58 +01:00
|
|
|
if ((cwd = getcwd(path, sizeof path)) == NULL) {
|
|
|
|
if ((cwd = find_home()) == NULL)
|
|
|
|
cwd = "/";
|
|
|
|
}
|
2015-10-17 20:30:43 +02:00
|
|
|
if ((ttynam = ttyname(STDIN_FILENO)) == NULL)
|
|
|
|
ttynam = "";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Drop privileges for client. "proc exec" is needed for -c and for
|
|
|
|
* locking (which uses system(3)).
|
|
|
|
*
|
|
|
|
* "tty" is needed to restore termios(4) and also for some reason -CC
|
|
|
|
* does not work properly without it (input is not recognised).
|
|
|
|
*
|
|
|
|
* "sendfd" is dropped later in client_dispatch_wait().
|
|
|
|
*/
|
2015-11-14 10:41:06 +01:00
|
|
|
if (0 && pledge("stdio unix sendfd proc exec tty", NULL) != 0)
|
2015-10-17 20:30:43 +02:00
|
|
|
fatal("pledge failed");
|
|
|
|
|
|
|
|
/* Free stuff that is not used in the client. */
|
2015-10-27 16:58:42 +01:00
|
|
|
options_free(global_options);
|
|
|
|
options_free(global_s_options);
|
|
|
|
options_free(global_w_options);
|
2015-10-28 10:51:55 +01:00
|
|
|
environ_free(global_environ);
|
2010-10-18 22:00:02 +02:00
|
|
|
|
2012-05-21 20:27:42 +02:00
|
|
|
/* Create stdin handler. */
|
|
|
|
setblocking(STDIN_FILENO, 0);
|
|
|
|
event_set(&client_stdin, STDIN_FILENO, EV_READ|EV_PERSIST,
|
|
|
|
client_stdin_callback, NULL);
|
2015-08-31 00:40:25 +02:00
|
|
|
if (client_flags & CLIENT_CONTROLCONTROL) {
|
2012-06-18 15:16:42 +02: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;
|
|
|
|
tio.c_lflag = NOKERNINFO;
|
|
|
|
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-21 20:27:42 +02:00
|
|
|
|
2013-10-10 14:26:34 +02:00
|
|
|
/* Send identify messages. */
|
2015-10-18 22:42:42 +02:00
|
|
|
client_send_identify(ttynam, cwd);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2010-10-18 22:00:02 +02:00
|
|
|
/* Send first command. */
|
|
|
|
if (msg == MSG_COMMAND) {
|
2013-10-10 14:13:56 +02: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-18 22:00:02 +02:00
|
|
|
/* Prepare command for server. */
|
2013-10-10 14:13:56 +02:00
|
|
|
data->argc = argc;
|
2014-09-01 23:50:18 +02:00
|
|
|
if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) {
|
2012-05-25 10:28:10 +02:00
|
|
|
fprintf(stderr, "command too long\n");
|
2013-10-10 14:13:56 +02:00
|
|
|
free(data);
|
2010-10-18 22:00:02 +02:00
|
|
|
return (1);
|
|
|
|
}
|
2013-10-10 14:13:56 +02:00
|
|
|
size += sizeof *data;
|
2010-10-18 22:00:02 +02:00
|
|
|
|
2013-10-10 14:13:56 +02:00
|
|
|
/* Send the command. */
|
2015-10-27 14:23:24 +01:00
|
|
|
if (proc_send(client_peer, msg, -1, data, size) != 0) {
|
2013-10-10 14:13:56 +02:00
|
|
|
fprintf(stderr, "failed to send command\n");
|
|
|
|
free(data);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
free(data);
|
2010-10-18 22:00:02 +02:00
|
|
|
} else if (msg == MSG_SHELL)
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, msg, -1, NULL, 0);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
/* Start main loop. */
|
|
|
|
proc_loop(client_proc, NULL);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2010-10-18 22:00:02 +02:00
|
|
|
/* Print the exit message, if any, and exit. */
|
2011-03-03 09:51:47 +01:00
|
|
|
if (client_attached) {
|
2015-08-31 00:40:25 +02:00
|
|
|
if (client_exitreason != CLIENT_EXIT_NONE)
|
2012-04-24 01:18:43 +02:00
|
|
|
printf("[%s]\n", client_exit_message());
|
2011-03-03 09:51:47 +01:00
|
|
|
|
|
|
|
ppid = getppid();
|
|
|
|
if (client_exittype == MSG_DETACHKILL && ppid > 1)
|
|
|
|
kill(ppid, SIGHUP);
|
2015-08-31 00:40:25 +02:00
|
|
|
} else if (client_flags & CLIENT_CONTROLCONTROL) {
|
2013-10-10 14:13:56 +02:00
|
|
|
if (client_exitreason != CLIENT_EXIT_NONE)
|
|
|
|
printf("%%exit %s\n", client_exit_message());
|
|
|
|
else
|
|
|
|
printf("%%exit\n");
|
|
|
|
printf("\033\\");
|
2012-06-18 15:16:42 +02:00
|
|
|
tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
|
2013-03-25 11:03:24 +01:00
|
|
|
}
|
2012-05-21 20:27:42 +02:00
|
|
|
setblocking(STDIN_FILENO, 1);
|
2010-10-18 22:00:02 +02:00
|
|
|
return (client_exitval);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
2013-10-10 14:26:34 +02:00
|
|
|
/* Send identify messages to server. */
|
2009-10-21 23:11:55 +02:00
|
|
|
void
|
2015-10-18 22:42:42 +02:00
|
|
|
client_send_identify(const char *ttynam, const char *cwd)
|
2009-10-21 23:11:55 +02:00
|
|
|
{
|
2015-06-14 12:07:44 +02:00
|
|
|
const char *s;
|
2013-10-10 14:26:34 +02:00
|
|
|
char **ss;
|
2015-07-13 20:10:26 +02:00
|
|
|
size_t sslen;
|
2015-08-31 00:40:25 +02:00
|
|
|
int fd, flags = client_flags;
|
2015-06-14 12:07:44 +02:00
|
|
|
pid_t pid;
|
2009-10-21 23:11:55 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
|
2009-10-21 23:11:55 +02:00
|
|
|
|
2013-10-10 14:26:34 +02:00
|
|
|
if ((s = getenv("TERM")) == NULL)
|
|
|
|
s = "";
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1);
|
2009-12-03 23:50:09 +01:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam, strlen(ttynam) + 1);
|
|
|
|
proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1);
|
2009-10-21 23:11:55 +02:00
|
|
|
|
2012-03-19 09:42:06 +01:00
|
|
|
if ((fd = dup(STDIN_FILENO)) == -1)
|
|
|
|
fatal("dup failed");
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0);
|
2009-10-21 23:11:55 +02:00
|
|
|
|
2015-06-14 12:07:44 +02:00
|
|
|
pid = getpid();
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid);
|
2015-06-14 12:07:44 +02:00
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
for (ss = environ; *ss != NULL; ss++) {
|
|
|
|
sslen = strlen(*ss) + 1;
|
|
|
|
if (sslen <= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen);
|
2015-07-13 20:10:26 +02:00
|
|
|
}
|
2013-10-10 14:35:30 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0);
|
2009-11-04 21:50:11 +01:00
|
|
|
}
|
|
|
|
|
2012-05-21 20:27:42 +02:00
|
|
|
/* Callback for client stdin read events. */
|
|
|
|
void
|
2015-08-31 00:40:25 +02:00
|
|
|
client_stdin_callback(unused int fd, unused short events, unused void *arg)
|
2012-05-21 20:27:42 +02:00
|
|
|
{
|
|
|
|
struct msg_stdin_data data;
|
|
|
|
|
|
|
|
data.size = read(STDIN_FILENO, data.data, sizeof data.data);
|
|
|
|
if (data.size < 0 && (errno == EINTR || errno == EAGAIN))
|
|
|
|
return;
|
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, MSG_STDIN, -1, &data, sizeof data);
|
2012-05-21 20:27:42 +02:00
|
|
|
if (data.size <= 0)
|
|
|
|
event_del(&client_stdin);
|
|
|
|
}
|
|
|
|
|
2012-06-18 15:34:56 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 00:40:25 +02:00
|
|
|
/* Run command in shell; used for -c. */
|
|
|
|
__dead void
|
|
|
|
client_exec(const char *shell)
|
|
|
|
{
|
|
|
|
const char *name, *ptr;
|
|
|
|
char *argv0;
|
|
|
|
|
|
|
|
log_debug("shell %s, command %s", shell, shell_cmd);
|
|
|
|
|
|
|
|
ptr = strrchr(shell, '/');
|
|
|
|
if (ptr != NULL && *(ptr + 1) != '\0')
|
|
|
|
name = ptr + 1;
|
|
|
|
else
|
|
|
|
name = shell;
|
|
|
|
if (client_flags & CLIENT_LOGIN)
|
|
|
|
xasprintf(&argv0, "-%s", name);
|
|
|
|
else
|
|
|
|
xasprintf(&argv0, "%s", name);
|
|
|
|
setenv("SHELL", shell, 1);
|
|
|
|
|
|
|
|
setblocking(STDIN_FILENO, 1);
|
|
|
|
setblocking(STDOUT_FILENO, 1);
|
|
|
|
setblocking(STDERR_FILENO, 1);
|
|
|
|
closefrom(STDERR_FILENO + 1);
|
|
|
|
|
|
|
|
execl(shell, argv0, "-c", shell_cmd, (char *) NULL);
|
|
|
|
fatal("execl failed");
|
|
|
|
}
|
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
/* Callback to handle signals in the client. */
|
|
|
|
void
|
|
|
|
client_signal(int sig)
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (sig == SIGCHLD)
|
|
|
|
waitpid(WAIT_ANY, &status, WNOHANG);
|
|
|
|
else if (!client_attached) {
|
|
|
|
if (sig == SIGTERM)
|
|
|
|
proc_exit(client_proc);
|
|
|
|
} else {
|
|
|
|
switch (sig) {
|
|
|
|
case SIGHUP:
|
|
|
|
client_exitreason = CLIENT_EXIT_LOST_TTY;
|
|
|
|
client_exitval = 1;
|
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case SIGTERM:
|
|
|
|
client_exitreason = CLIENT_EXIT_TERMINATED;
|
|
|
|
client_exitval = 1;
|
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case SIGWINCH:
|
|
|
|
proc_send(client_peer, MSG_RESIZE, -1, 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");
|
|
|
|
proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client read events. */
|
|
|
|
void
|
|
|
|
client_dispatch(struct imsg *imsg, unused void *arg)
|
|
|
|
{
|
|
|
|
if (imsg == NULL) {
|
|
|
|
client_exitreason = CLIENT_EXIT_LOST_SERVER;
|
|
|
|
client_exitval = 1;
|
|
|
|
} else if (client_attached)
|
|
|
|
client_dispatch_attached(imsg);
|
|
|
|
else
|
|
|
|
client_dispatch_wait(imsg);
|
|
|
|
}
|
|
|
|
|
2010-10-18 22:00:02 +02:00
|
|
|
/* Dispatch imsgs when in wait state (before MSG_READY). */
|
2015-10-27 14:23:24 +01:00
|
|
|
void
|
|
|
|
client_dispatch_wait(struct imsg *imsg)
|
2010-10-18 22:00:02 +02:00
|
|
|
{
|
2013-10-10 14:13:56 +02:00
|
|
|
char *data;
|
2015-10-27 14:23:24 +01:00
|
|
|
ssize_t datalen;
|
2013-10-10 14:13:56 +02:00
|
|
|
struct msg_stdout_data stdoutdata;
|
|
|
|
struct msg_stderr_data stderrdata;
|
|
|
|
int retval;
|
2015-10-17 20:30:43 +02:00
|
|
|
static int pledge_applied;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "sendfd" is no longer required once all of the identify messages
|
|
|
|
* have been sent. We know the server won't send us anything until that
|
|
|
|
* point (because we don't ask it to), so we can drop "sendfd" once we
|
|
|
|
* get the first message from the server.
|
|
|
|
*/
|
|
|
|
if (!pledge_applied) {
|
2015-11-14 10:41:06 +01:00
|
|
|
if (0 && pledge("stdio unix proc exec tty", NULL) != 0)
|
2015-10-17 20:30:43 +02:00
|
|
|
fatal("pledge failed");
|
|
|
|
pledge_applied = 1;
|
|
|
|
};
|
2010-10-18 22:00:02 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
data = imsg->data;
|
|
|
|
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
|
|
|
|
|
|
|
|
switch (imsg->hdr.type) {
|
|
|
|
case MSG_EXIT:
|
|
|
|
case MSG_SHUTDOWN:
|
|
|
|
if (datalen != sizeof retval && datalen != 0)
|
|
|
|
fatalx("bad MSG_EXIT size");
|
|
|
|
if (datalen == sizeof retval) {
|
|
|
|
memcpy(&retval, data, sizeof retval);
|
|
|
|
client_exitval = retval;
|
|
|
|
}
|
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
|
|
|
case MSG_READY:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_READY size");
|
2012-05-21 20:27:42 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
event_del(&client_stdin);
|
|
|
|
client_attached = 1;
|
|
|
|
proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case MSG_STDIN:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_STDIN size");
|
2010-10-18 22:00:02 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
event_add(&client_stdin, NULL);
|
|
|
|
break;
|
|
|
|
case MSG_STDOUT:
|
|
|
|
if (datalen != sizeof stdoutdata)
|
|
|
|
fatalx("bad MSG_STDOUT size");
|
|
|
|
memcpy(&stdoutdata, data, sizeof stdoutdata);
|
2010-10-18 22:00:02 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
client_write(STDOUT_FILENO, stdoutdata.data,
|
|
|
|
stdoutdata.size);
|
|
|
|
break;
|
|
|
|
case MSG_STDERR:
|
|
|
|
if (datalen != sizeof stderrdata)
|
|
|
|
fatalx("bad MSG_STDERR size");
|
|
|
|
memcpy(&stderrdata, data, sizeof stderrdata);
|
2010-10-18 22:00:02 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
client_write(STDERR_FILENO, stderrdata.data,
|
|
|
|
stderrdata.size);
|
|
|
|
break;
|
|
|
|
case MSG_VERSION:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_VERSION size");
|
|
|
|
|
|
|
|
fprintf(stderr, "protocol version mismatch "
|
|
|
|
"(client %d, server %u)\n", PROTOCOL_VERSION,
|
2015-10-31 14:43:38 +01:00
|
|
|
imsg->hdr.peerid & 0xff);
|
2015-10-27 14:23:24 +01:00
|
|
|
client_exitval = 1;
|
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
|
|
|
case MSG_SHELL:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
|
|
|
fatalx("bad MSG_SHELL string");
|
|
|
|
|
|
|
|
clear_signals(0);
|
|
|
|
client_exec(data);
|
|
|
|
/* NOTREACHED */
|
|
|
|
case MSG_DETACH:
|
|
|
|
case MSG_DETACHKILL:
|
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case MSG_EXITED:
|
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
2010-10-18 22:00:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dispatch imsgs in attached state (after MSG_READY). */
|
2015-10-27 14:23:24 +01:00
|
|
|
void
|
|
|
|
client_dispatch_attached(struct imsg *imsg)
|
2009-07-30 18:32:12 +02:00
|
|
|
{
|
2013-10-10 14:13:56 +02:00
|
|
|
struct sigaction sigact;
|
|
|
|
char *data;
|
2015-10-27 14:23:24 +01:00
|
|
|
ssize_t datalen;
|
2009-08-11 19:18:35 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
data = imsg->data;
|
|
|
|
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
|
2009-07-30 18:32:12 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
switch (imsg->hdr.type) {
|
|
|
|
case MSG_DETACH:
|
|
|
|
case MSG_DETACHKILL:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
|
|
|
fatalx("bad MSG_DETACH string");
|
2009-07-30 18:32:12 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
client_exitsession = xstrdup(data);
|
|
|
|
client_exittype = imsg->hdr.type;
|
|
|
|
if (imsg->hdr.type == MSG_DETACHKILL)
|
|
|
|
client_exitreason = CLIENT_EXIT_DETACHED_HUP;
|
|
|
|
else
|
|
|
|
client_exitreason = CLIENT_EXIT_DETACHED;
|
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case MSG_EXIT:
|
|
|
|
if (datalen != 0 && datalen != sizeof (int))
|
|
|
|
fatalx("bad MSG_EXIT size");
|
2009-07-30 18:32:12 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
client_exitreason = CLIENT_EXIT_EXITED;
|
|
|
|
break;
|
|
|
|
case MSG_EXITED:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_EXITED size");
|
2009-12-03 23:50:09 +01:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
|
|
|
case MSG_SHUTDOWN:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_SHUTDOWN size");
|
2009-08-11 19:18:35 +02:00
|
|
|
|
2015-10-27 14:23:24 +01:00
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
client_exitreason = CLIENT_EXIT_SERVER_EXITED;
|
|
|
|
client_exitval = 1;
|
|
|
|
break;
|
|
|
|
case MSG_SUSPEND:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_SUSPEND size");
|
|
|
|
|
|
|
|
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);
|
|
|
|
break;
|
|
|
|
case MSG_LOCK:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
|
|
|
fatalx("bad MSG_LOCK string");
|
|
|
|
|
|
|
|
system(data);
|
|
|
|
proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0);
|
|
|
|
break;
|
2009-07-30 18:32:12 +02:00
|
|
|
}
|
|
|
|
}
|