For initial session, use size of command client even if detached.
[tmux-openbsd.git] / client.c
blobeea45177212fe33abe56227b1af0485e00b407d6
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/un.h>
23 #include <sys/wait.h>
25 #include <errno.h>
26 #include <event.h>
27 #include <fcntl.h>
28 #include <pwd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "tmux.h"
35 struct imsgbuf client_ibuf;
36 struct event client_event;
37 const char *client_exitmsg;
38 int client_exitval;
39 enum msgtype client_exittype;
40 int client_attached;
42 int client_connect(char *, int);
43 void client_send_identify(int);
44 void client_send_environ(void);
45 void client_write_server(enum msgtype, void *, size_t);
46 void client_update_event(void);
47 void client_signal(int, short, void *);
48 void client_callback(int, short, void *);
49 int client_dispatch_attached(void);
50 int client_dispatch_wait(void *);
52 /* Connect client to server. */
53 int
54 client_connect(char *path, int start_server)
56 struct sockaddr_un sa;
57 size_t size;
58 int fd;
60 memset(&sa, 0, sizeof sa);
61 sa.sun_family = AF_UNIX;
62 size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
63 if (size >= sizeof sa.sun_path) {
64 errno = ENAMETOOLONG;
65 return (-1);
68 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
69 fatal("socket failed");
71 if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
72 if (!start_server)
73 goto failed;
74 switch (errno) {
75 case ECONNREFUSED:
76 if (unlink(path) != 0)
77 goto failed;
78 /* FALLTHROUGH */
79 case ENOENT:
80 if ((fd = server_start()) == -1)
81 goto failed;
82 break;
83 default:
84 goto failed;
88 setblocking(fd, 0);
89 return (fd);
91 failed:
92 close(fd);
93 return (-1);
96 /* Client main loop. */
97 int
98 client_main(int argc, char **argv, int flags)
100 struct cmd *cmd;
101 struct cmd_list *cmdlist;
102 struct msg_command_data cmddata;
103 int cmdflags, fd;
104 pid_t ppid;
105 enum msgtype msg;
106 char *cause;
108 /* Set up the initial command. */
109 cmdflags = 0;
110 if (shell_cmd != NULL) {
111 msg = MSG_SHELL;
112 cmdflags = CMD_STARTSERVER;
113 } else if (argc == 0) {
114 msg = MSG_COMMAND;
115 cmdflags = CMD_STARTSERVER|CMD_SENDENVIRON|CMD_CANTNEST;
116 } else {
117 msg = MSG_COMMAND;
120 * It sucks parsing the command string twice (in client and
121 * later in server) but it is necessary to get the start server
122 * flag.
124 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
125 log_warnx("%s", cause);
126 return (1);
128 cmdflags &= ~CMD_STARTSERVER;
129 TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
130 if (cmd->entry->flags & CMD_STARTSERVER)
131 cmdflags |= CMD_STARTSERVER;
132 if (cmd->entry->flags & CMD_SENDENVIRON)
133 cmdflags |= CMD_SENDENVIRON;
134 if (cmd->entry->flags & CMD_CANTNEST)
135 cmdflags |= CMD_CANTNEST;
137 cmd_list_free(cmdlist);
141 * Check if this could be a nested session, if the command can't nest:
142 * if the socket path matches $TMUX, this is probably the same server.
144 if (shell_cmd == NULL && environ_path != NULL &&
145 cmdflags & CMD_CANTNEST && strcmp(socket_path, environ_path) == 0) {
146 log_warnx("sessions should be nested with care. "
147 "unset $TMUX to force.");
148 return (1);
151 /* Initialise the client socket and start the server. */
152 fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER);
153 if (fd == -1) {
154 log_warn("failed to connect to server");
155 return (1);
158 /* Set process title, log and signals now this is the client. */
159 setproctitle("client (%s)", socket_path);
160 logfile("client");
162 /* Create imsg. */
163 imsg_init(&client_ibuf, fd);
164 event_set(&client_event, fd, EV_READ, client_callback, shell_cmd);
166 /* Establish signal handlers. */
167 set_signals(client_signal);
169 /* Send initial environment. */
170 if (cmdflags & CMD_SENDENVIRON)
171 client_send_environ();
172 client_send_identify(flags);
174 /* Send first command. */
175 if (msg == MSG_COMMAND) {
176 /* Fill in command line arguments. */
177 cmddata.pid = environ_pid;
178 cmddata.idx = environ_idx;
180 /* Prepare command for server. */
181 cmddata.argc = argc;
182 if (cmd_pack_argv(
183 argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
184 log_warnx("command too long");
185 return (1);
188 client_write_server(msg, &cmddata, sizeof cmddata);
189 } else if (msg == MSG_SHELL)
190 client_write_server(msg, NULL, 0);
192 /* Set the event and dispatch. */
193 client_update_event();
194 event_dispatch();
196 /* Print the exit message, if any, and exit. */
197 if (client_attached) {
198 if (client_exitmsg != NULL && !login_shell)
199 printf("[%s]\n", client_exitmsg);
201 ppid = getppid();
202 if (client_exittype == MSG_DETACHKILL && ppid > 1)
203 kill(ppid, SIGHUP);
205 return (client_exitval);
208 /* Send identify message to server with the file descriptors. */
209 void
210 client_send_identify(int flags)
212 struct msg_identify_data data;
213 char *term;
214 int fd;
216 data.flags = flags;
218 if (getcwd(data.cwd, sizeof data.cwd) == NULL)
219 *data.cwd = '\0';
221 term = getenv("TERM");
222 if (term == NULL ||
223 strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
224 *data.term = '\0';
226 if ((fd = dup(STDIN_FILENO)) == -1)
227 fatal("dup failed");
228 imsg_compose(&client_ibuf,
229 MSG_IDENTIFY, PROTOCOL_VERSION, -1, fd, &data, sizeof data);
231 if ((fd = dup(STDOUT_FILENO)) == -1)
232 fatal("dup failed");
233 imsg_compose(&client_ibuf,
234 MSG_STDOUT, PROTOCOL_VERSION, -1, fd, NULL, 0);
236 if ((fd = dup(STDERR_FILENO)) == -1)
237 fatal("dup failed");
238 imsg_compose(&client_ibuf,
239 MSG_STDERR, PROTOCOL_VERSION, -1, fd, NULL, 0);
242 /* Forward entire environment to server. */
243 void
244 client_send_environ(void)
246 struct msg_environ_data data;
247 char **var;
249 for (var = environ; *var != NULL; var++) {
250 if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
251 continue;
252 client_write_server(MSG_ENVIRON, &data, sizeof data);
256 /* Write a message to the server without a file descriptor. */
257 void
258 client_write_server(enum msgtype type, void *buf, size_t len)
260 imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
263 /* Update client event based on whether it needs to read or read and write. */
264 void
265 client_update_event(void)
267 short events;
269 event_del(&client_event);
270 events = EV_READ;
271 if (client_ibuf.w.queued > 0)
272 events |= EV_WRITE;
273 event_set(
274 &client_event, client_ibuf.fd, events, client_callback, shell_cmd);
275 event_add(&client_event, NULL);
278 /* Callback to handle signals in the client. */
279 /* ARGSUSED */
280 void
281 client_signal(int sig, unused short events, unused void *data)
283 struct sigaction sigact;
284 int status;
286 if (!client_attached) {
287 switch (sig) {
288 case SIGCHLD:
289 waitpid(WAIT_ANY, &status, WNOHANG);
290 break;
291 case SIGTERM:
292 event_loopexit(NULL);
293 break;
295 } else {
296 switch (sig) {
297 case SIGHUP:
298 client_exitmsg = "lost tty";
299 client_exitval = 1;
300 client_write_server(MSG_EXITING, NULL, 0);
301 break;
302 case SIGTERM:
303 client_exitmsg = "terminated";
304 client_exitval = 1;
305 client_write_server(MSG_EXITING, NULL, 0);
306 break;
307 case SIGWINCH:
308 client_write_server(MSG_RESIZE, NULL, 0);
309 break;
310 case SIGCONT:
311 memset(&sigact, 0, sizeof sigact);
312 sigemptyset(&sigact.sa_mask);
313 sigact.sa_flags = SA_RESTART;
314 sigact.sa_handler = SIG_IGN;
315 if (sigaction(SIGTSTP, &sigact, NULL) != 0)
316 fatal("sigaction failed");
317 client_write_server(MSG_WAKEUP, NULL, 0);
318 break;
322 client_update_event();
325 /* Callback for client imsg read events. */
326 /* ARGSUSED */
327 void
328 client_callback(unused int fd, short events, void *data)
330 ssize_t n;
331 int retval;
333 if (events & EV_READ) {
334 if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
335 goto lost_server;
336 if (client_attached)
337 retval = client_dispatch_attached();
338 else
339 retval = client_dispatch_wait(data);
340 if (retval != 0) {
341 event_loopexit(NULL);
342 return;
346 if (events & EV_WRITE) {
347 if (msgbuf_write(&client_ibuf.w) < 0)
348 goto lost_server;
351 client_update_event();
352 return;
354 lost_server:
355 client_exitmsg = "lost server";
356 client_exitval = 1;
357 event_loopexit(NULL);
360 /* Dispatch imsgs when in wait state (before MSG_READY). */
362 client_dispatch_wait(void *data)
364 struct imsg imsg;
365 ssize_t n, datalen;
366 struct msg_shell_data shelldata;
367 struct msg_exit_data exitdata;
368 const char *shellcmd = data;
370 if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
371 fatalx("imsg_read failed");
373 for (;;) {
374 if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
375 fatalx("imsg_get failed");
376 if (n == 0)
377 return (0);
378 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
380 switch (imsg.hdr.type) {
381 case MSG_EXIT:
382 case MSG_SHUTDOWN:
383 if (datalen != sizeof exitdata) {
384 if (datalen != 0)
385 fatalx("bad MSG_EXIT size");
386 } else {
387 memcpy(&exitdata, imsg.data, sizeof exitdata);
388 client_exitval = exitdata.retcode;
390 imsg_free(&imsg);
391 return (-1);
392 case MSG_READY:
393 if (datalen != 0)
394 fatalx("bad MSG_READY size");
396 client_attached = 1;
397 break;
398 case MSG_VERSION:
399 if (datalen != 0)
400 fatalx("bad MSG_VERSION size");
402 log_warnx("protocol version mismatch (client %u, "
403 "server %u)", PROTOCOL_VERSION, imsg.hdr.peerid);
404 client_exitval = 1;
406 imsg_free(&imsg);
407 return (-1);
408 case MSG_SHELL:
409 if (datalen != sizeof shelldata)
410 fatalx("bad MSG_SHELL size");
411 memcpy(&shelldata, imsg.data, sizeof shelldata);
412 shelldata.shell[(sizeof shelldata.shell) - 1] = '\0';
414 clear_signals(0);
416 shell_exec(shelldata.shell, shellcmd);
417 /* NOTREACHED */
418 default:
419 fatalx("unexpected message");
422 imsg_free(&imsg);
426 /* Dispatch imsgs in attached state (after MSG_READY). */
427 /* ARGSUSED */
429 client_dispatch_attached(void)
431 struct imsg imsg;
432 struct msg_lock_data lockdata;
433 struct sigaction sigact;
434 ssize_t n, datalen;
436 for (;;) {
437 if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
438 fatalx("imsg_get failed");
439 if (n == 0)
440 return (0);
441 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
443 log_debug("client got %d", imsg.hdr.type);
444 switch (imsg.hdr.type) {
445 case MSG_DETACHKILL:
446 case MSG_DETACH:
447 if (datalen != 0)
448 fatalx("bad MSG_DETACH size");
450 client_exittype = imsg.hdr.type;
451 if (imsg.hdr.type == MSG_DETACHKILL)
452 client_exitmsg = "detached and SIGHUP";
453 else
454 client_exitmsg = "detached";
455 client_write_server(MSG_EXITING, NULL, 0);
456 break;
457 case MSG_EXIT:
458 if (datalen != 0 &&
459 datalen != sizeof (struct msg_exit_data))
460 fatalx("bad MSG_EXIT size");
462 client_write_server(MSG_EXITING, NULL, 0);
463 client_exitmsg = "exited";
464 break;
465 case MSG_EXITED:
466 if (datalen != 0)
467 fatalx("bad MSG_EXITED size");
469 imsg_free(&imsg);
470 return (-1);
471 case MSG_SHUTDOWN:
472 if (datalen != 0)
473 fatalx("bad MSG_SHUTDOWN size");
475 client_write_server(MSG_EXITING, NULL, 0);
476 client_exitmsg = "server exited";
477 client_exitval = 1;
478 break;
479 case MSG_SUSPEND:
480 if (datalen != 0)
481 fatalx("bad MSG_SUSPEND size");
483 memset(&sigact, 0, sizeof sigact);
484 sigemptyset(&sigact.sa_mask);
485 sigact.sa_flags = SA_RESTART;
486 sigact.sa_handler = SIG_DFL;
487 if (sigaction(SIGTSTP, &sigact, NULL) != 0)
488 fatal("sigaction failed");
489 kill(getpid(), SIGTSTP);
490 break;
491 case MSG_LOCK:
492 if (datalen != sizeof lockdata)
493 fatalx("bad MSG_LOCK size");
494 memcpy(&lockdata, imsg.data, sizeof lockdata);
496 lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
497 system(lockdata.cmd);
498 client_write_server(MSG_UNLOCK, NULL, 0);
499 break;
500 default:
501 fatalx("unexpected message");
504 imsg_free(&imsg);