2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2007-09-26 13:43:15 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2020-05-08 14:15:11 +00:00
|
|
|
#include <sys/uio.h>
|
2007-09-26 13:43:15 +00:00
|
|
|
#include <sys/un.h>
|
2009-02-08 16:11:26 +00:00
|
|
|
#include <sys/wait.h>
|
2018-06-07 08:17:05 +00:00
|
|
|
#include <sys/file.h>
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
#include <errno.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"
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static struct tmuxproc *client_proc;
|
|
|
|
static struct tmuxpeer *client_peer;
|
2020-06-18 08:34:22 +00:00
|
|
|
static uint64_t client_flags;
|
2020-10-30 18:54:23 +00:00
|
|
|
static int client_suspended;
|
2016-10-03 22:52:11 +00:00
|
|
|
static enum {
|
2012-04-24 16:24:03 +00:00
|
|
|
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,
|
2020-06-10 07:27:10 +00:00
|
|
|
CLIENT_EXIT_MESSAGE_PROVIDED
|
2012-04-24 16:24:03 +00:00
|
|
|
} client_exitreason = CLIENT_EXIT_NONE;
|
2019-12-13 06:55:12 +00:00
|
|
|
static int client_exitflag;
|
2016-10-03 22:52:11 +00:00
|
|
|
static int client_exitval;
|
|
|
|
static enum msgtype client_exittype;
|
|
|
|
static const char *client_exitsession;
|
2020-06-10 07:27:10 +00:00
|
|
|
static char *client_exitmessage;
|
2017-01-13 10:12:12 +00:00
|
|
|
static const char *client_execshell;
|
|
|
|
static const char *client_execcmd;
|
2016-10-03 22:52:11 +00:00
|
|
|
static int client_attached;
|
2019-12-12 11:39:56 +00:00
|
|
|
static struct client_files client_files = RB_INITIALIZER(&client_files);
|
2016-10-03 22:52:11 +00:00
|
|
|
|
|
|
|
static __dead void client_exec(const char *,const char *);
|
|
|
|
static int client_get_lock(char *);
|
2020-09-22 05:23:34 +00:00
|
|
|
static int client_connect(struct event_base *, const char *,
|
|
|
|
uint64_t);
|
2021-02-17 07:18:36 +00:00
|
|
|
static void client_send_identify(const char *, const char *,
|
|
|
|
char **, u_int, const char *, int);
|
2016-10-03 22:52:11 +00:00
|
|
|
static void client_signal(int);
|
|
|
|
static void client_dispatch(struct imsg *, void *);
|
|
|
|
static void client_dispatch_attached(struct imsg *);
|
2017-07-12 09:21:25 +00:00
|
|
|
static void client_dispatch_wait(struct imsg *);
|
2016-10-03 22:52:11 +00:00
|
|
|
static 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
|
2015-11-24 23:01:51 +00:00
|
|
|
* another client, so block until the lock is released and return -2 to
|
|
|
|
* retry. Return -1 on failure to continue and start the server anyway.
|
2012-03-18 01:28:10 +00:00
|
|
|
*/
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2012-03-18 01:28:10 +00:00
|
|
|
client_get_lock(char *lockfile)
|
|
|
|
{
|
|
|
|
int lockfd;
|
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("lock file is %s", lockfile);
|
2012-03-18 01:28:10 +00:00
|
|
|
|
2015-11-24 22:45:44 +00:00
|
|
|
if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
|
2015-11-24 23:01:51 +00:00
|
|
|
log_debug("open failed: %s", strerror(errno));
|
|
|
|
return (-1);
|
2015-11-24 22:45:44 +00:00
|
|
|
}
|
2012-03-09 09:57:40 +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);
|
2015-11-24 23:01:51 +00:00
|
|
|
return (-2);
|
2012-03-18 01:28:10 +00:00
|
|
|
}
|
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. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2020-09-22 05:23:34 +00:00
|
|
|
client_connect(struct event_base *base, const char *path, uint64_t flags)
|
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)
|
2015-11-24 22:45:44 +00:00
|
|
|
return (-1);
|
2007-12-01 11:10:33 +00:00
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("trying connect");
|
2015-11-24 22:45:44 +00:00
|
|
|
if (connect(fd, (struct sockaddr *)&sa, sizeof 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;
|
2021-01-17 16:17:41 +00:00
|
|
|
if (flags & CLIENT_NOSTARTSERVER)
|
|
|
|
goto failed;
|
2020-03-12 09:26:34 +00:00
|
|
|
if (~flags & CLIENT_STARTSERVER)
|
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);
|
2015-11-24 23:01:51 +00:00
|
|
|
if ((lockfd = client_get_lock(lockfile)) < 0) {
|
|
|
|
log_debug("didn't get lock (%d)", lockfd);
|
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
free(lockfile);
|
2015-11-24 23:01:51 +00:00
|
|
|
lockfile = NULL;
|
|
|
|
|
|
|
|
if (lockfd == -2)
|
|
|
|
goto retry;
|
2014-07-21 10:52:48 +00:00
|
|
|
}
|
2015-11-24 23:01:51 +00:00
|
|
|
log_debug("got lock (%d)", lockfd);
|
2014-07-21 10:52:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2015-11-24 23:01:51 +00:00
|
|
|
if (lockfd >= 0 && unlink(path) != 0 && errno != ENOENT) {
|
2014-01-09 21:20:45 +00:00
|
|
|
free(lockfile);
|
|
|
|
close(lockfd);
|
2012-03-18 01:28:10 +00:00
|
|
|
return (-1);
|
2014-01-09 21:20:45 +00:00
|
|
|
}
|
2020-03-12 09:26:34 +00:00
|
|
|
fd = server_start(client_proc, flags, base, lockfd, lockfile);
|
2014-07-21 10:52:48 +00:00
|
|
|
}
|
2015-09-24 12:06:20 +00:00
|
|
|
|
2015-11-24 23:01:51 +00:00
|
|
|
if (locked && lockfd >= 0) {
|
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:
|
2015-09-24 12:06:20 +00:00
|
|
|
if (locked) {
|
|
|
|
free(lockfile);
|
|
|
|
close(lockfd);
|
|
|
|
}
|
2010-10-24 01:31:08 +00:00
|
|
|
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:
|
2019-07-26 20:08:40 +00:00
|
|
|
return ("server exited unexpectedly");
|
2012-04-24 16:24:03 +00:00
|
|
|
case CLIENT_EXIT_EXITED:
|
|
|
|
return ("exited");
|
|
|
|
case CLIENT_EXIT_SERVER_EXITED:
|
|
|
|
return ("server exited");
|
2020-06-10 07:27:10 +00:00
|
|
|
case CLIENT_EXIT_MESSAGE_PROVIDED:
|
|
|
|
return (client_exitmessage);
|
2012-04-24 16:24:03 +00:00
|
|
|
}
|
|
|
|
return ("unknown reason");
|
|
|
|
}
|
|
|
|
|
2019-12-13 06:55:12 +00:00
|
|
|
/* Exit if all streams flushed. */
|
|
|
|
static void
|
|
|
|
client_exit(void)
|
|
|
|
{
|
2021-02-11 09:39:29 +00:00
|
|
|
if (!file_write_left(&client_files))
|
2019-12-13 06:55:12 +00:00
|
|
|
proc_exit(client_proc);
|
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Client main loop. */
|
|
|
|
int
|
2020-09-22 05:23:34 +00:00
|
|
|
client_main(struct event_base *base, int argc, char **argv, uint64_t flags,
|
|
|
|
int feat)
|
2010-10-24 01:31:08 +00:00
|
|
|
{
|
2019-05-25 07:18:20 +00:00
|
|
|
struct cmd_parse_result *pr;
|
2019-12-12 11:39:56 +00:00
|
|
|
struct msg_command *data;
|
2020-03-12 09:26:34 +00:00
|
|
|
int fd, i;
|
2021-02-17 07:18:36 +00:00
|
|
|
const char *ttynam, *termname, *cwd;
|
2011-03-19 23:27:35 +00:00
|
|
|
pid_t ppid;
|
2010-10-24 01:31:08 +00:00
|
|
|
enum msgtype msg;
|
2012-06-18 15:23:01 +00:00
|
|
|
struct termios tio, saved_tio;
|
2020-06-18 08:34:22 +00:00
|
|
|
size_t size, linesize = 0;
|
|
|
|
ssize_t linelen;
|
2021-02-17 07:18:36 +00:00
|
|
|
char *line = NULL, **caps = NULL, *cause;
|
|
|
|
u_int ncaps = 0;
|
2010-10-24 01:31:08 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
/* Ignore SIGCHLD now or daemon() in the server will leave a zombie. */
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Set up the initial command. */
|
2017-07-12 09:21:25 +00:00
|
|
|
if (shell_command != NULL) {
|
2010-10-24 01:31:08 +00:00
|
|
|
msg = MSG_SHELL;
|
2020-03-30 15:49:23 +00:00
|
|
|
flags |= CLIENT_STARTSERVER;
|
2010-10-24 01:31:08 +00:00
|
|
|
} else if (argc == 0) {
|
|
|
|
msg = MSG_COMMAND;
|
2020-03-12 09:26:34 +00:00
|
|
|
flags |= CLIENT_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.
|
|
|
|
*/
|
2019-05-25 07:18:20 +00:00
|
|
|
pr = cmd_parse_from_arguments(argc, argv, NULL);
|
|
|
|
if (pr->status == CMD_PARSE_SUCCESS) {
|
2020-04-13 08:26:27 +00:00
|
|
|
if (cmd_list_any_have(pr->cmdlist, CMD_STARTSERVER))
|
|
|
|
flags |= CLIENT_STARTSERVER;
|
2019-05-25 07:18:20 +00:00
|
|
|
cmd_list_free(pr->cmdlist);
|
|
|
|
} else
|
|
|
|
free(pr->error);
|
2010-10-24 01:31:08 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 22:45:44 +00:00
|
|
|
/* Create client process structure (starts logging). */
|
2017-07-12 09:24:17 +00:00
|
|
|
client_proc = proc_start("client");
|
|
|
|
proc_set_signals(client_proc, client_signal);
|
2015-11-24 22:45:44 +00:00
|
|
|
|
2020-06-18 08:34:22 +00:00
|
|
|
/* Save the flags. */
|
|
|
|
client_flags = flags;
|
2020-09-22 05:23:34 +00:00
|
|
|
log_debug("flags are %#llx", (unsigned long long)client_flags);
|
2020-06-18 08:34:22 +00:00
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
/* Initialize the client socket and start the server. */
|
2020-03-12 09:26:34 +00:00
|
|
|
fd = client_connect(base, socket_path, client_flags);
|
2010-10-24 01:31:08 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-07-12 09:21:25 +00:00
|
|
|
client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL);
|
2015-10-27 13:23:24 +00:00
|
|
|
|
2015-10-17 18:30:43 +00:00
|
|
|
/* Save these before pledge(). */
|
2018-11-22 10:36:40 +00:00
|
|
|
if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL)
|
2018-01-01 11:19:08 +00:00
|
|
|
cwd = "/";
|
2015-10-17 18:30:43 +00:00
|
|
|
if ((ttynam = ttyname(STDIN_FILENO)) == NULL)
|
|
|
|
ttynam = "";
|
2021-02-17 07:18:36 +00:00
|
|
|
if ((termname = getenv("TERM")) == NULL)
|
|
|
|
termname = "";
|
2015-10-17 18:30:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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().
|
|
|
|
*/
|
2019-12-12 11:39:56 +00:00
|
|
|
if (pledge(
|
|
|
|
"stdio rpath wpath cpath unix sendfd proc exec tty",
|
|
|
|
NULL) != 0)
|
2015-10-17 18:30:43 +00:00
|
|
|
fatal("pledge failed");
|
|
|
|
|
2021-02-17 07:18:36 +00:00
|
|
|
/* Load terminfo entry if any. */
|
|
|
|
if (isatty(STDIN_FILENO) &&
|
|
|
|
*termname != '\0' &&
|
|
|
|
tty_term_read_list(termname, STDIN_FILENO, &caps, &ncaps,
|
|
|
|
&cause) != 0) {
|
|
|
|
fprintf(stderr, "%s\n", cause);
|
|
|
|
free(cause);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2015-10-17 18:30:43 +00:00
|
|
|
/* Free stuff that is not used in the client. */
|
2017-01-23 10:09:43 +00:00
|
|
|
if (ptm_fd != -1)
|
|
|
|
close(ptm_fd);
|
2015-10-27 15:58:42 +00:00
|
|
|
options_free(global_options);
|
|
|
|
options_free(global_s_options);
|
|
|
|
options_free(global_w_options);
|
2015-10-28 09:51:55 +00:00
|
|
|
environ_free(global_environ);
|
2010-10-24 01:31:08 +00:00
|
|
|
|
2019-12-12 11:39:56 +00:00
|
|
|
/* Set up control mode. */
|
2015-08-30 22:40:25 +00:00
|
|
|
if (client_flags & CLIENT_CONTROLCONTROL) {
|
2017-01-20 14:02:33 +00:00
|
|
|
if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) {
|
|
|
|
fprintf(stderr, "tcgetattr failed: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (1);
|
|
|
|
}
|
2012-06-18 15:23:01 +00:00
|
|
|
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. */
|
2021-02-17 07:18:36 +00:00
|
|
|
client_send_identify(ttynam, termname, caps, ncaps, cwd, feat);
|
|
|
|
tty_term_free_list(caps, ncaps);
|
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;
|
2017-12-18 22:13:36 +00:00
|
|
|
if (size > MAX_IMSGSIZE - (sizeof *data)) {
|
|
|
|
fprintf(stderr, "command too long\n");
|
|
|
|
return (1);
|
|
|
|
}
|
2013-10-05 23:48:24 +00:00
|
|
|
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. */
|
2015-10-27 13:23:24 +00:00
|
|
|
if (proc_send(client_peer, msg, -1, data, size) != 0) {
|
2013-10-05 23:48:24 +00:00
|
|
|
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)
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, msg, -1, NULL, 0);
|
2007-12-01 11:10:33 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
/* Start main loop. */
|
|
|
|
proc_loop(client_proc, NULL);
|
2008-06-07 07:27:28 +00:00
|
|
|
|
2017-01-13 10:12:12 +00:00
|
|
|
/* Run command if user requested exec, instead of exiting. */
|
|
|
|
if (client_exittype == MSG_EXEC) {
|
|
|
|
if (client_flags & CLIENT_CONTROLCONTROL)
|
|
|
|
tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
|
|
|
|
client_exec(client_execshell, client_execcmd);
|
|
|
|
}
|
|
|
|
|
2020-06-01 09:43:00 +00:00
|
|
|
/* Restore streams to blocking. */
|
|
|
|
setblocking(STDIN_FILENO, 1);
|
|
|
|
setblocking(STDOUT_FILENO, 1);
|
|
|
|
setblocking(STDERR_FILENO, 1);
|
|
|
|
|
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) {
|
2015-08-30 22:40:25 +00:00
|
|
|
if (client_exitreason != CLIENT_EXIT_NONE)
|
2012-04-24 16:24:03 +00:00
|
|
|
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);
|
2020-06-01 09:43:00 +00:00
|
|
|
} else if (client_flags & CLIENT_CONTROL) {
|
2013-10-05 23:10:40 +00:00
|
|
|
if (client_exitreason != CLIENT_EXIT_NONE)
|
|
|
|
printf("%%exit %s\n", client_exit_message());
|
|
|
|
else
|
|
|
|
printf("%%exit\n");
|
2020-06-18 08:34:22 +00:00
|
|
|
fflush(stdout);
|
|
|
|
if (client_flags & CLIENT_CONTROL_WAITEXIT) {
|
|
|
|
setvbuf(stdin, NULL, _IOLBF, 0);
|
|
|
|
for (;;) {
|
|
|
|
linelen = getline(&line, &linesize, stdin);
|
|
|
|
if (linelen <= 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
}
|
2020-06-01 09:43:00 +00:00
|
|
|
if (client_flags & CLIENT_CONTROLCONTROL) {
|
|
|
|
printf("\033\\");
|
2020-06-18 08:34:22 +00:00
|
|
|
fflush(stdout);
|
2020-06-01 09:43:00 +00:00
|
|
|
tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
|
|
|
|
}
|
2015-11-25 07:58:55 +00:00
|
|
|
} else if (client_exitreason != CLIENT_EXIT_NONE)
|
2015-11-24 22:45:44 +00:00
|
|
|
fprintf(stderr, "%s\n", client_exit_message());
|
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. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2021-02-17 07:18:36 +00:00
|
|
|
client_send_identify(const char *ttynam, const char *termname, char **caps,
|
|
|
|
u_int ncaps, const char *cwd, int feat)
|
2009-10-23 17:37:41 +00:00
|
|
|
{
|
2021-02-17 07:18:36 +00:00
|
|
|
char **ss;
|
|
|
|
size_t sslen;
|
|
|
|
int fd, flags = client_flags;
|
|
|
|
pid_t pid;
|
|
|
|
u_int i;
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
|
2020-09-22 05:23:34 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_LONGFLAGS, -1, &client_flags,
|
|
|
|
sizeof client_flags);
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2021-02-17 07:18:36 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_TERM, -1, termname,
|
|
|
|
strlen(termname) + 1);
|
2020-04-20 13:25:36 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_FEATURES, -1, &feat, sizeof feat);
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2015-11-24 22:04:36 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam,
|
|
|
|
strlen(ttynam) + 1);
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1);
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2021-02-17 07:18:36 +00:00
|
|
|
for (i = 0; i < ncaps; i++) {
|
|
|
|
proc_send(client_peer, MSG_IDENTIFY_TERMINFO, -1,
|
|
|
|
caps[i], strlen(caps[i]) + 1);
|
|
|
|
}
|
|
|
|
|
2012-03-29 21:02:34 +00:00
|
|
|
if ((fd = dup(STDIN_FILENO)) == -1)
|
|
|
|
fatal("dup failed");
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0);
|
2020-05-26 08:41:47 +00:00
|
|
|
if ((fd = dup(STDOUT_FILENO)) == -1)
|
|
|
|
fatal("dup failed");
|
|
|
|
proc_send(client_peer, MSG_IDENTIFY_STDOUT, fd, NULL, 0);
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2015-06-14 10:07:44 +00:00
|
|
|
pid = getpid();
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid);
|
2015-06-14 10:07:44 +00:00
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
for (ss = environ; *ss != NULL; ss++) {
|
|
|
|
sslen = strlen(*ss) + 1;
|
2015-11-24 22:04:36 +00:00
|
|
|
if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
|
|
|
|
continue;
|
|
|
|
proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen);
|
2009-11-08 22:40:36 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0);
|
2009-11-08 22:40:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 22:40:25 +00:00
|
|
|
/* Run command in shell; used for -c. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static __dead void
|
2015-11-24 23:22:51 +00:00
|
|
|
client_exec(const char *shell, const char *shellcmd)
|
2015-08-30 22:40:25 +00:00
|
|
|
{
|
|
|
|
const char *name, *ptr;
|
|
|
|
char *argv0;
|
|
|
|
|
2015-11-24 23:22:51 +00:00
|
|
|
log_debug("shell %s, command %s", shell, shellcmd);
|
2015-08-30 22:40:25 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-07-14 18:49:07 +00:00
|
|
|
proc_clear_signals(client_proc, 1);
|
|
|
|
|
2015-08-30 22:40:25 +00:00
|
|
|
setblocking(STDIN_FILENO, 1);
|
|
|
|
setblocking(STDOUT_FILENO, 1);
|
|
|
|
setblocking(STDERR_FILENO, 1);
|
|
|
|
closefrom(STDERR_FILENO + 1);
|
|
|
|
|
2015-11-24 23:22:51 +00:00
|
|
|
execl(shell, argv0, "-c", shellcmd, (char *) NULL);
|
2015-08-30 22:40:25 +00:00
|
|
|
fatal("execl failed");
|
|
|
|
}
|
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
/* Callback to handle signals in the client. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2015-10-27 13:23:24 +00:00
|
|
|
client_signal(int sig)
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
int status;
|
|
|
|
|
2020-10-30 18:54:23 +00:00
|
|
|
log_debug("%s: %s", __func__, strsignal(sig));
|
2015-10-27 13:23:24 +00:00
|
|
|
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:
|
2020-10-30 18:54:23 +00:00
|
|
|
if (!client_suspended)
|
|
|
|
client_exitreason = CLIENT_EXIT_TERMINATED;
|
2015-10-27 13:23:24 +00:00
|
|
|
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);
|
2020-10-30 18:54:23 +00:00
|
|
|
client_suspended = 0;
|
2015-10-27 13:23:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 08:28:45 +00:00
|
|
|
/* Callback for file write error or close. */
|
|
|
|
static void
|
|
|
|
client_file_check_cb(__unused struct client *c, __unused const char *path,
|
|
|
|
__unused int error, __unused int closed, __unused struct evbuffer *buffer,
|
|
|
|
__unused void *data)
|
|
|
|
{
|
|
|
|
if (client_exitflag)
|
|
|
|
client_exit();
|
|
|
|
}
|
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
/* Callback for client read events. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2017-07-12 09:21:25 +00:00
|
|
|
client_dispatch(struct imsg *imsg, __unused void *arg)
|
2015-10-27 13:23:24 +00:00
|
|
|
{
|
|
|
|
if (imsg == NULL) {
|
2021-02-12 06:52:48 +00:00
|
|
|
if (!client_exitflag) {
|
|
|
|
client_exitreason = CLIENT_EXIT_LOST_SERVER;
|
|
|
|
client_exitval = 1;
|
|
|
|
}
|
2015-11-24 22:45:44 +00:00
|
|
|
proc_exit(client_proc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client_attached)
|
2015-10-27 13:23:24 +00:00
|
|
|
client_dispatch_attached(imsg);
|
|
|
|
else
|
2017-07-12 09:21:25 +00:00
|
|
|
client_dispatch_wait(imsg);
|
2015-10-27 13:23:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 07:27:10 +00:00
|
|
|
/* Process an exit message. */
|
|
|
|
static void
|
|
|
|
client_dispatch_exit_message(char *data, size_t datalen)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (datalen < sizeof retval && datalen != 0)
|
|
|
|
fatalx("bad MSG_EXIT size");
|
|
|
|
|
|
|
|
if (datalen >= sizeof retval) {
|
|
|
|
memcpy(&retval, data, sizeof retval);
|
|
|
|
client_exitval = retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (datalen > sizeof retval) {
|
|
|
|
datalen -= sizeof retval;
|
|
|
|
data += sizeof retval;
|
|
|
|
|
|
|
|
client_exitmessage = xmalloc(datalen);
|
|
|
|
memcpy(client_exitmessage, data, datalen);
|
|
|
|
client_exitmessage[datalen - 1] = '\0';
|
|
|
|
|
|
|
|
client_exitreason = CLIENT_EXIT_MESSAGE_PROVIDED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Dispatch imsgs when in wait state (before MSG_READY). */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2017-07-12 09:21:25 +00:00
|
|
|
client_dispatch_wait(struct imsg *imsg)
|
2010-10-24 01:31:08 +00:00
|
|
|
{
|
2019-12-12 11:39:56 +00:00
|
|
|
char *data;
|
|
|
|
ssize_t datalen;
|
|
|
|
static int pledge_applied;
|
2015-10-17 18:30:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* "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) {
|
2019-12-12 11:39:56 +00:00
|
|
|
if (pledge(
|
|
|
|
"stdio rpath wpath cpath unix proc exec tty",
|
|
|
|
NULL) != 0)
|
2015-10-17 18:30:43 +00:00
|
|
|
fatal("pledge failed");
|
|
|
|
pledge_applied = 1;
|
2019-12-12 11:39:56 +00:00
|
|
|
}
|
2010-10-24 01:31:08 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
data = imsg->data;
|
|
|
|
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
|
|
|
|
|
|
|
|
switch (imsg->hdr.type) {
|
|
|
|
case MSG_EXIT:
|
|
|
|
case MSG_SHUTDOWN:
|
2020-06-10 07:27:10 +00:00
|
|
|
client_dispatch_exit_message(data, datalen);
|
2019-12-13 06:55:12 +00:00
|
|
|
client_exitflag = 1;
|
|
|
|
client_exit();
|
2015-10-27 13:23:24 +00:00
|
|
|
break;
|
|
|
|
case MSG_READY:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_READY size");
|
2012-05-22 20:56:35 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
client_attached = 1;
|
|
|
|
proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
|
|
|
|
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 13:43:38 +00:00
|
|
|
imsg->hdr.peerid & 0xff);
|
2015-10-27 13:23:24 +00:00
|
|
|
client_exitval = 1;
|
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
2020-06-18 08:34:22 +00:00
|
|
|
case MSG_FLAGS:
|
|
|
|
if (datalen != sizeof client_flags)
|
|
|
|
fatalx("bad MSG_FLAGS string");
|
|
|
|
|
|
|
|
memcpy(&client_flags, data, sizeof client_flags);
|
2020-09-22 05:23:34 +00:00
|
|
|
log_debug("new flags are %#llx",
|
|
|
|
(unsigned long long)client_flags);
|
2020-06-18 08:34:22 +00:00
|
|
|
break;
|
2015-10-27 13:23:24 +00:00
|
|
|
case MSG_SHELL:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
|
|
|
fatalx("bad MSG_SHELL string");
|
|
|
|
|
2017-07-12 09:21:25 +00:00
|
|
|
client_exec(data, shell_command);
|
2015-10-27 13:23:24 +00:00
|
|
|
/* 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;
|
2019-12-12 11:39:56 +00:00
|
|
|
case MSG_READ_OPEN:
|
2021-02-11 08:28:45 +00:00
|
|
|
file_read_open(&client_files, client_peer, imsg, 1,
|
|
|
|
!(client_flags & CLIENT_CONTROL), client_file_check_cb,
|
|
|
|
NULL);
|
2019-12-12 11:39:56 +00:00
|
|
|
break;
|
|
|
|
case MSG_WRITE_OPEN:
|
2021-02-11 08:28:45 +00:00
|
|
|
file_write_open(&client_files, client_peer, imsg, 1,
|
|
|
|
!(client_flags & CLIENT_CONTROL), client_file_check_cb,
|
|
|
|
NULL);
|
2019-12-12 11:39:56 +00:00
|
|
|
break;
|
|
|
|
case MSG_WRITE:
|
2021-02-11 08:28:45 +00:00
|
|
|
file_write_data(&client_files, imsg);
|
2019-12-12 11:39:56 +00:00
|
|
|
break;
|
|
|
|
case MSG_WRITE_CLOSE:
|
2021-02-11 08:28:45 +00:00
|
|
|
file_write_close(&client_files, imsg);
|
2019-12-12 11:39:56 +00:00
|
|
|
break;
|
2020-01-29 16:22:32 +00:00
|
|
|
case MSG_OLDSTDERR:
|
|
|
|
case MSG_OLDSTDIN:
|
|
|
|
case MSG_OLDSTDOUT:
|
|
|
|
fprintf(stderr, "server version is too old for client\n");
|
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
2010-10-24 01:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dispatch imsgs in attached state (after MSG_READY). */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2015-10-27 13:23:24 +00:00
|
|
|
client_dispatch_attached(struct imsg *imsg)
|
2009-07-30 20:57:39 +00:00
|
|
|
{
|
2013-10-05 23:28:35 +00:00
|
|
|
struct sigaction sigact;
|
|
|
|
char *data;
|
2015-10-27 13:23:24 +00:00
|
|
|
ssize_t datalen;
|
2009-08-14 21:04:04 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
data = imsg->data;
|
|
|
|
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
|
2009-07-30 20:57:39 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
switch (imsg->hdr.type) {
|
2020-06-18 08:34:22 +00:00
|
|
|
case MSG_FLAGS:
|
|
|
|
if (datalen != sizeof client_flags)
|
|
|
|
fatalx("bad MSG_FLAGS string");
|
|
|
|
|
|
|
|
memcpy(&client_flags, data, sizeof client_flags);
|
2020-09-22 05:23:34 +00:00
|
|
|
log_debug("new flags are %#llx",
|
|
|
|
(unsigned long long)client_flags);
|
2020-06-18 08:34:22 +00:00
|
|
|
break;
|
2015-10-27 13:23:24 +00:00
|
|
|
case MSG_DETACH:
|
|
|
|
case MSG_DETACHKILL:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
|
|
|
fatalx("bad MSG_DETACH string");
|
2009-07-30 20:57:39 +00:00
|
|
|
|
2015-10-27 13:23:24 +00: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;
|
2017-01-13 10:12:12 +00:00
|
|
|
case MSG_EXEC:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0' ||
|
|
|
|
strlen(data) + 1 == (size_t)datalen)
|
|
|
|
fatalx("bad MSG_EXEC string");
|
|
|
|
client_execcmd = xstrdup(data);
|
|
|
|
client_execshell = xstrdup(data + strlen(data) + 1);
|
|
|
|
|
|
|
|
client_exittype = imsg->hdr.type;
|
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
2015-10-27 13:23:24 +00:00
|
|
|
case MSG_EXIT:
|
2020-06-10 07:27:10 +00:00
|
|
|
client_dispatch_exit_message(data, datalen);
|
|
|
|
if (client_exitreason == CLIENT_EXIT_NONE)
|
|
|
|
client_exitreason = CLIENT_EXIT_EXITED;
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case MSG_EXITED:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_EXITED size");
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
|
|
|
case MSG_SHUTDOWN:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_SHUTDOWN size");
|
2009-08-14 21:04:04 +00:00
|
|
|
|
2015-10-27 13:23:24 +00: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");
|
2020-10-30 18:54:23 +00:00
|
|
|
client_suspended = 1;
|
2015-10-27 13:23:24 +00:00
|
|
|
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 20:57:39 +00:00
|
|
|
}
|
|
|
|
}
|