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>
35 extern char *malloc_options
;
39 struct options global_options
; /* server options */
40 struct options global_s_options
; /* session options */
41 struct options global_w_options
; /* window options */
42 struct environ global_environ
;
49 __dead
void usage(void);
50 void fill_session(struct msg_command_data
*);
51 char *makesockpath(const char *);
52 __dead
void shell_exec(const char *, const char *);
54 struct imsgbuf
*main_ibuf
;
55 struct event main_ev_sigterm
;
58 void main_set_signals(void);
59 void main_clear_signals(void);
60 void main_signal(int, short, unused
void *);
61 void main_callback(int, short, void *);
62 void main_dispatch(const char *);
68 "usage: %s [-28lquv] [-c shell-command] [-f file] [-L socket-name]\n"
69 " [-S socket-path] [command [flags]]\n",
75 logfile(const char *name
)
80 if (debug_level
> 0) {
81 xasprintf(&path
, "tmux-%s-%ld.log", name
, (long) getpid());
82 log_open_file(debug_level
, path
);
93 shell
= getenv("SHELL");
94 if (checkshell(shell
))
97 pw
= getpwuid(getuid());
98 if (pw
!= NULL
&& checkshell(pw
->pw_shell
))
99 return (pw
->pw_shell
);
101 return (_PATH_BSHELL
);
105 checkshell(const char *shell
)
107 if (shell
== NULL
|| *shell
== '\0' || areshell(shell
))
109 if (access(shell
, X_OK
) != 0)
115 areshell(const char *shell
)
117 const char *progname
, *ptr
;
119 if ((ptr
= strrchr(shell
, '/')) != NULL
)
123 progname
= __progname
;
124 if (*progname
== '-')
126 if (strcmp(ptr
, progname
) == 0)
132 fill_session(struct msg_command_data
*data
)
134 char *env
, *ptr1
, *ptr2
, buf
[256];
140 if ((env
= getenv("TMUX")) == NULL
)
143 if ((ptr2
= strrchr(env
, ',')) == NULL
|| ptr2
== env
)
145 for (ptr1
= ptr2
- 1; ptr1
> env
&& *ptr1
!= ','; ptr1
--)
152 len
= ptr2
- ptr1
- 1;
153 if (len
> (sizeof buf
) - 1)
155 memcpy(buf
, ptr1
, len
);
158 ll
= strtonum(buf
, 0, LONG_MAX
, &errstr
);
163 ll
= strtonum(ptr2
, 0, UINT_MAX
, &errstr
);
170 makesockpath(const char *label
)
172 char base
[MAXPATHLEN
], *path
;
177 xsnprintf(base
, MAXPATHLEN
, "%s/tmux-%d", _PATH_TMP
, uid
);
179 if (mkdir(base
, S_IRWXU
) != 0 && errno
!= EEXIST
)
182 if (lstat(base
, &sb
) != 0)
184 if (!S_ISDIR(sb
.st_mode
)) {
188 if (sb
.st_uid
!= uid
|| (sb
.st_mode
& (S_IRWXG
|S_IRWXO
)) != 0) {
193 xasprintf(&path
, "%s/%s", base
, label
);
198 shell_exec(const char *shell
, const char *shellcmd
)
200 const char *shellname
, *ptr
;
203 ptr
= strrchr(shell
, '/');
204 if (ptr
!= NULL
&& *(ptr
+ 1) != '\0')
209 xasprintf(&argv0
, "-%s", shellname
);
211 xasprintf(&argv0
, "%s", shellname
);
212 setenv("SHELL", shell
, 1);
214 execl(shell
, argv0
, "-c", shellcmd
, (char *) NULL
);
215 fatal("execl failed");
219 main(int argc
, char **argv
)
221 struct cmd_list
*cmdlist
;
225 struct options
*oo
, *so
, *wo
;
226 struct keylist
*keylist
;
227 struct msg_command_data cmddata
;
228 char *s
, *shellcmd
, *path
, *label
, *home
, *cause
;
229 char cwd
[MAXPATHLEN
], **var
;
232 int opt
, flags
, quiet
= 0, cmdflags
= 0;
236 malloc_options
= (char *) "AFGJPX";
240 shellcmd
= label
= path
= NULL
;
241 login_shell
= (**argv
== '-');
242 while ((opt
= getopt(argc
, argv
, "28c:df:lL:qS:uUv")) != -1) {
245 flags
|= IDENTIFY_256COLOURS
;
246 flags
&= ~IDENTIFY_88COLOURS
;
249 flags
|= IDENTIFY_88COLOURS
;
250 flags
&= ~IDENTIFY_256COLOURS
;
253 if (shellcmd
!= NULL
)
255 shellcmd
= xstrdup(optarg
);
258 if (cfg_file
!= NULL
)
260 cfg_file
= xstrdup(optarg
);
268 label
= xstrdup(optarg
);
276 path
= xstrdup(optarg
);
279 flags
|= IDENTIFY_UTF8
;
291 if (shellcmd
!= NULL
&& argc
!= 0)
294 log_open_tty(debug_level
);
296 if (!(flags
& IDENTIFY_UTF8
)) {
298 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG
299 * exist (in that order) to contain UTF-8, it is a safe
300 * assumption that either they are using a UTF-8 terminal, or
301 * if not they know that output from UTF-8-capable programs may
304 if ((s
= getenv("LC_ALL")) == NULL
) {
305 if ((s
= getenv("LC_CTYPE")) == NULL
)
308 if (s
!= NULL
&& (strcasestr(s
, "UTF-8") != NULL
||
309 strcasestr(s
, "UTF8") != NULL
))
310 flags
|= IDENTIFY_UTF8
;
313 environ_init(&global_environ
);
314 for (var
= environ
; *var
!= NULL
; var
++)
315 environ_put(&global_environ
, *var
);
317 options_init(&global_options
, NULL
);
318 oo
= &global_options
;
319 options_set_number(oo
, "quiet", quiet
);
320 options_set_number(oo
, "escape-time", 500);
322 options_init(&global_s_options
, NULL
);
323 so
= &global_s_options
;
324 options_set_number(so
, "base-index", 0);
325 options_set_number(so
, "bell-action", BELL_ANY
);
326 options_set_number(so
, "buffer-limit", 9);
327 options_set_string(so
, "default-command", "%s", "");
328 options_set_string(so
, "default-shell", "%s", getshell());
329 options_set_string(so
, "default-terminal", "screen");
330 options_set_number(so
, "display-panes-colour", 4);
331 options_set_number(so
, "display-panes-time", 1000);
332 options_set_number(so
, "display-time", 750);
333 options_set_number(so
, "history-limit", 2000);
334 options_set_number(so
, "lock-after-time", 0);
335 options_set_string(so
, "lock-command", "lock -np");
336 options_set_number(so
, "lock-server", 1);
337 options_set_number(so
, "message-attr", 0);
338 options_set_number(so
, "message-bg", 3);
339 options_set_number(so
, "message-fg", 0);
340 options_set_number(so
, "message-limit", 20);
341 options_set_number(so
, "mouse-select-pane", 0);
342 options_set_number(so
, "pane-active-border-bg", 2);
343 options_set_number(so
, "pane-active-border-fg", 8);
344 options_set_number(so
, "pane-border-bg", 8);
345 options_set_number(so
, "pane-border-fg", 8);
346 options_set_number(so
, "repeat-time", 500);
347 options_set_number(so
, "set-remain-on-exit", 0);
348 options_set_number(so
, "set-titles", 0);
349 options_set_string(so
, "set-titles-string", "#S:#I:#W - \"#T\"");
350 options_set_number(so
, "status", 1);
351 options_set_number(so
, "status-attr", 0);
352 options_set_number(so
, "status-bg", 2);
353 options_set_number(so
, "status-fg", 0);
354 options_set_number(so
, "status-interval", 15);
355 options_set_number(so
, "status-justify", 0);
356 options_set_number(so
, "status-keys", MODEKEY_EMACS
);
357 options_set_string(so
, "status-left", "[#S]");
358 options_set_number(so
, "status-left-attr", 0);
359 options_set_number(so
, "status-left-bg", 8);
360 options_set_number(so
, "status-left-fg", 8);
361 options_set_number(so
, "status-left-length", 10);
362 options_set_string(so
, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y");
363 options_set_number(so
, "status-right-attr", 0);
364 options_set_number(so
, "status-right-bg", 8);
365 options_set_number(so
, "status-right-fg", 8);
366 options_set_number(so
, "status-right-length", 40);
367 options_set_string(so
, "terminal-overrides",
368 "*88col*:colors=88,*256col*:colors=256");
369 options_set_string(so
, "update-environment", "DISPLAY "
370 "WINDOWID SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION");
371 options_set_number(so
, "visual-activity", 0);
372 options_set_number(so
, "visual-bell", 0);
373 options_set_number(so
, "visual-content", 0);
375 keylist
= xmalloc(sizeof *keylist
);
377 ARRAY_ADD(keylist
, '\002');
378 options_set_data(so
, "prefix", keylist
, xfree
);
380 options_init(&global_w_options
, NULL
);
381 wo
= &global_w_options
;
382 options_set_number(wo
, "aggressive-resize", 0);
383 options_set_number(wo
, "automatic-rename", 1);
384 options_set_number(wo
, "clock-mode-colour", 4);
385 options_set_number(wo
, "clock-mode-style", 1);
386 options_set_number(wo
, "force-height", 0);
387 options_set_number(wo
, "force-width", 0);
388 options_set_number(wo
, "main-pane-height", 24);
389 options_set_number(wo
, "main-pane-width", 81);
390 options_set_number(wo
, "mode-attr", 0);
391 options_set_number(wo
, "mode-bg", 3);
392 options_set_number(wo
, "mode-fg", 0);
393 options_set_number(wo
, "mode-keys", MODEKEY_EMACS
);
394 options_set_number(wo
, "mode-mouse", 0);
395 options_set_number(wo
, "monitor-activity", 0);
396 options_set_string(wo
, "monitor-content", "%s", "");
397 options_set_number(wo
, "window-status-attr", 0);
398 options_set_number(wo
, "window-status-bg", 8);
399 options_set_number(wo
, "window-status-current-attr", 0);
400 options_set_number(wo
, "window-status-current-bg", 8);
401 options_set_number(wo
, "window-status-current-fg", 8);
402 options_set_number(wo
, "window-status-fg", 8);
403 options_set_string(wo
, "window-status-format", "#I:#W#F");
404 options_set_string(wo
, "window-status-current-format", "#I:#W#F");
405 options_set_number(wo
, "xterm-keys", 0);
406 options_set_number(wo
, "remain-on-exit", 0);
407 options_set_number(wo
, "synchronize-panes", 0);
409 if (flags
& IDENTIFY_UTF8
) {
410 options_set_number(so
, "status-utf8", 1);
411 options_set_number(wo
, "utf8", 1);
413 options_set_number(so
, "status-utf8", 0);
414 options_set_number(wo
, "utf8", 0);
417 if (getcwd(cwd
, sizeof cwd
) == NULL
) {
418 pw
= getpwuid(getuid());
419 if (pw
->pw_dir
!= NULL
&& *pw
->pw_dir
!= '\0')
420 strlcpy(cwd
, pw
->pw_dir
, sizeof cwd
);
422 strlcpy(cwd
, "/", sizeof cwd
);
424 options_set_string(so
, "default-path", "%s", cwd
);
426 if (cfg_file
== NULL
) {
427 home
= getenv("HOME");
428 if (home
== NULL
|| *home
== '\0') {
429 pw
= getpwuid(getuid());
433 xasprintf(&cfg_file
, "%s/%s", home
, DEFAULT_CFG
);
434 if (access(cfg_file
, R_OK
) != 0) {
439 if (access(cfg_file
, R_OK
) != 0) {
440 log_warn("%s", cfg_file
);
446 label
= xstrdup("default");
447 if (path
== NULL
&& (path
= makesockpath(label
)) == NULL
) {
448 log_warn("can't create socket");
453 if (shellcmd
!= NULL
) {
458 fill_session(&cmddata
);
462 argc
, argv
, cmddata
.argv
, sizeof cmddata
.argv
) != 0) {
463 log_warnx("command too long");
469 len
= sizeof cmddata
;
472 if (shellcmd
!= NULL
)
473 cmdflags
|= CMD_STARTSERVER
;
474 else if (argc
== 0) /* new-session is the default */
475 cmdflags
|= CMD_STARTSERVER
|CMD_SENDENVIRON
;
478 * It sucks parsing the command string twice (in client and
479 * later in server) but it is necessary to get the start server
482 if ((cmdlist
= cmd_list_parse(argc
, argv
, &cause
)) == NULL
) {
483 log_warnx("%s", cause
);
486 cmdflags
&= ~CMD_STARTSERVER
;
487 TAILQ_FOREACH(cmd
, cmdlist
, qentry
) {
488 if (cmd
->entry
->flags
& CMD_STARTSERVER
)
489 cmdflags
|= CMD_STARTSERVER
;
490 if (cmd
->entry
->flags
& CMD_SENDENVIRON
)
491 cmdflags
|= CMD_SENDENVIRON
;
493 cmd_list_free(cmdlist
);
496 if ((main_ibuf
= client_init(path
, cmdflags
, flags
)) == NULL
)
502 imsg_compose(main_ibuf
, msg
, PROTOCOL_VERSION
, -1, -1, buf
, len
);
507 if (main_ibuf
->w
.queued
> 0)
509 event_once(main_ibuf
->fd
, events
, main_callback
, shellcmd
, NULL
);
514 main_clear_signals();
516 client_main(); /* doesn't return */
520 main_set_signals(void)
522 struct sigaction sigact
;
524 memset(&sigact
, 0, sizeof sigact
);
525 sigemptyset(&sigact
.sa_mask
);
526 sigact
.sa_flags
= SA_RESTART
;
527 sigact
.sa_handler
= SIG_IGN
;
528 if (sigaction(SIGINT
, &sigact
, NULL
) != 0)
529 fatal("sigaction failed");
530 if (sigaction(SIGPIPE
, &sigact
, NULL
) != 0)
531 fatal("sigaction failed");
532 if (sigaction(SIGUSR1
, &sigact
, NULL
) != 0)
533 fatal("sigaction failed");
534 if (sigaction(SIGUSR2
, &sigact
, NULL
) != 0)
535 fatal("sigaction failed");
536 if (sigaction(SIGTSTP
, &sigact
, NULL
) != 0)
537 fatal("sigaction failed");
539 signal_set(&main_ev_sigterm
, SIGTERM
, main_signal
, NULL
);
540 signal_add(&main_ev_sigterm
, NULL
);
544 main_clear_signals(void)
546 struct sigaction sigact
;
548 memset(&sigact
, 0, sizeof sigact
);
549 sigemptyset(&sigact
.sa_mask
);
550 sigact
.sa_flags
= SA_RESTART
;
551 sigact
.sa_handler
= SIG_DFL
;
552 if (sigaction(SIGINT
, &sigact
, NULL
) != 0)
553 fatal("sigaction failed");
554 if (sigaction(SIGPIPE
, &sigact
, NULL
) != 0)
555 fatal("sigaction failed");
556 if (sigaction(SIGUSR1
, &sigact
, NULL
) != 0)
557 fatal("sigaction failed");
558 if (sigaction(SIGUSR2
, &sigact
, NULL
) != 0)
559 fatal("sigaction failed");
560 if (sigaction(SIGTSTP
, &sigact
, NULL
) != 0)
561 fatal("sigaction failed");
563 event_del(&main_ev_sigterm
);
568 main_signal(int sig
, unused
short events
, unused
void *data
)
578 main_callback(unused
int fd
, short events
, void *data
)
580 char *shellcmd
= data
;
582 if (events
& EV_READ
)
583 main_dispatch(shellcmd
);
585 if (events
& EV_WRITE
) {
586 if (msgbuf_write(&main_ibuf
->w
) < 0)
587 fatalx("msgbuf_write failed");
591 if (main_ibuf
->w
.queued
> 0)
593 event_once(main_ibuf
->fd
, events
, main_callback
, shellcmd
, NULL
);
597 main_dispatch(const char *shellcmd
)
601 struct msg_print_data printdata
;
602 struct msg_shell_data shelldata
;
604 if ((n
= imsg_read(main_ibuf
)) == -1 || n
== 0)
605 fatalx("imsg_read failed");
608 if ((n
= imsg_get(main_ibuf
, &imsg
)) == -1)
609 fatalx("imsg_get failed");
612 datalen
= imsg
.hdr
.len
- IMSG_HEADER_SIZE
;
614 switch (imsg
.hdr
.type
) {
618 fatalx("bad MSG_EXIT size");
623 if (datalen
!= sizeof printdata
)
624 fatalx("bad MSG_PRINT size");
625 memcpy(&printdata
, imsg
.data
, sizeof printdata
);
626 printdata
.msg
[(sizeof printdata
.msg
) - 1] = '\0';
628 log_info("%s", printdata
.msg
);
629 if (imsg
.hdr
.type
== MSG_ERROR
)
634 fatalx("bad MSG_READY size");
636 event_loopexit(NULL
); /* move to client_main() */
640 fatalx("bad MSG_VERSION size");
642 log_warnx("protocol version mismatch (client %u, "
643 "server %u)", PROTOCOL_VERSION
, imsg
.hdr
.peerid
);
646 if (datalen
!= sizeof shelldata
)
647 fatalx("bad MSG_SHELL size");
648 memcpy(&shelldata
, imsg
.data
, sizeof shelldata
);
649 shelldata
.shell
[(sizeof shelldata
.shell
) - 1] = '\0';
651 main_clear_signals();
653 shell_exec(shelldata
.shell
, shellcmd
);
655 fatalx("unexpected message");