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>
36 extern char *malloc_options
;
40 struct options global_options
; /* server options */
41 struct options global_s_options
; /* session options */
42 struct options global_w_options
; /* window options */
43 struct environ global_environ
;
45 struct event_base
*ev_base
;
58 __dead
void usage(void);
59 void parse_env(struct env_data
*);
60 char *makesockpath(const char *);
61 __dead
void shell_exec(const char *, const char *);
63 struct imsgbuf
*main_ibuf
;
65 void main_signal(int, short, unused
void *);
66 void main_callback(int, short, void *);
67 void main_dispatch(const char *);
73 "usage: %s [-28lquv] [-c shell-command] [-f file] [-L socket-name]\n"
74 " [-S socket-path] [command [flags]]\n",
80 logfile(const char *name
)
85 if (debug_level
> 0) {
86 xasprintf(&path
, "tmux-%s-%ld.log", name
, (long) getpid());
87 log_open_file(debug_level
, path
);
98 shell
= getenv("SHELL");
99 if (checkshell(shell
))
102 pw
= getpwuid(getuid());
103 if (pw
!= NULL
&& checkshell(pw
->pw_shell
))
104 return (pw
->pw_shell
);
106 return (_PATH_BSHELL
);
110 checkshell(const char *shell
)
112 if (shell
== NULL
|| *shell
== '\0' || areshell(shell
))
114 if (access(shell
, X_OK
) != 0)
120 areshell(const char *shell
)
122 const char *progname
, *ptr
;
124 if ((ptr
= strrchr(shell
, '/')) != NULL
)
128 progname
= __progname
;
129 if (*progname
== '-')
131 if (strcmp(ptr
, progname
) == 0)
137 parse_env(struct env_data
*data
)
139 char *env
, *path_pid
, *pid_idx
, buf
[256];
145 if ((env
= getenv("TMUX")) == NULL
)
148 if ((path_pid
= strchr(env
, ',')) == NULL
|| path_pid
== env
)
150 if ((pid_idx
= strchr(path_pid
+ 1, ',')) == NULL
)
152 if ((pid_idx
== path_pid
+ 1 || pid_idx
[1] == '\0'))
156 len
= path_pid
- env
;
157 data
->path
= xmalloc (len
+ 1);
158 memcpy(data
->path
, env
, len
);
159 data
->path
[len
] = '\0';
162 len
= pid_idx
- path_pid
- 1;
163 if (len
> (sizeof buf
) - 1)
165 memcpy(buf
, path_pid
+ 1, len
);
168 ll
= strtonum(buf
, 0, LONG_MAX
, &errstr
);
174 ll
= strtonum(pid_idx
+1, 0, UINT_MAX
, &errstr
);
181 makesockpath(const char *label
)
183 char base
[MAXPATHLEN
], *path
;
188 xsnprintf(base
, MAXPATHLEN
, "%s/tmux-%d", _PATH_TMP
, uid
);
190 if (mkdir(base
, S_IRWXU
) != 0 && errno
!= EEXIST
)
193 if (lstat(base
, &sb
) != 0)
195 if (!S_ISDIR(sb
.st_mode
)) {
199 if (sb
.st_uid
!= uid
|| (sb
.st_mode
& (S_IRWXG
|S_IRWXO
)) != 0) {
204 xasprintf(&path
, "%s/%s", base
, label
);
209 shell_exec(const char *shell
, const char *shellcmd
)
211 const char *shellname
, *ptr
;
214 ptr
= strrchr(shell
, '/');
215 if (ptr
!= NULL
&& *(ptr
+ 1) != '\0')
220 xasprintf(&argv0
, "-%s", shellname
);
222 xasprintf(&argv0
, "%s", shellname
);
223 setenv("SHELL", shell
, 1);
225 execl(shell
, argv0
, "-c", shellcmd
, (char *) NULL
);
226 fatal("execl failed");
230 main(int argc
, char **argv
)
232 struct cmd_list
*cmdlist
;
236 struct options
*oo
, *so
, *wo
;
237 struct keylist
*keylist
;
238 struct env_data envdata
;
239 struct msg_command_data cmddata
;
240 char *s
, *shellcmd
, *path
, *label
, *home
, *cause
;
244 int opt
, flags
, quiet
= 0, cmdflags
= 0;
248 malloc_options
= (char *) "AFGJPX";
252 shellcmd
= label
= path
= NULL
;
254 login_shell
= (**argv
== '-');
255 while ((opt
= getopt(argc
, argv
, "28c:df:lL:qS:uUv")) != -1) {
258 flags
|= IDENTIFY_256COLOURS
;
259 flags
&= ~IDENTIFY_88COLOURS
;
262 flags
|= IDENTIFY_88COLOURS
;
263 flags
&= ~IDENTIFY_256COLOURS
;
266 if (shellcmd
!= NULL
)
268 shellcmd
= xstrdup(optarg
);
271 if (cfg_file
!= NULL
)
273 cfg_file
= xstrdup(optarg
);
281 label
= xstrdup(optarg
);
289 path
= xstrdup(optarg
);
292 flags
|= IDENTIFY_UTF8
;
304 if (shellcmd
!= NULL
&& argc
!= 0)
307 log_open_tty(debug_level
);
309 if (!(flags
& IDENTIFY_UTF8
)) {
311 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG
312 * exist (in that order) to contain UTF-8, it is a safe
313 * assumption that either they are using a UTF-8 terminal, or
314 * if not they know that output from UTF-8-capable programs may
317 if ((s
= getenv("LC_ALL")) == NULL
) {
318 if ((s
= getenv("LC_CTYPE")) == NULL
)
321 if (s
!= NULL
&& (strcasestr(s
, "UTF-8") != NULL
||
322 strcasestr(s
, "UTF8") != NULL
))
323 flags
|= IDENTIFY_UTF8
;
326 environ_init(&global_environ
);
327 for (var
= environ
; *var
!= NULL
; var
++)
328 environ_put(&global_environ
, *var
);
330 options_init(&global_options
, NULL
);
331 oo
= &global_options
;
332 options_set_number(oo
, "quiet", quiet
);
333 options_set_number(oo
, "escape-time", 500);
335 options_init(&global_s_options
, NULL
);
336 so
= &global_s_options
;
337 options_set_number(so
, "base-index", 0);
338 options_set_number(so
, "bell-action", BELL_ANY
);
339 options_set_number(so
, "buffer-limit", 9);
340 options_set_string(so
, "default-command", "%s", "");
341 options_set_string(so
, "default-path", "%s", "");
342 options_set_string(so
, "default-shell", "%s", getshell());
343 options_set_string(so
, "default-terminal", "screen");
344 options_set_number(so
, "detach-on-destroy", 1);
345 options_set_number(so
, "display-panes-active-colour", 1);
346 options_set_number(so
, "display-panes-colour", 4);
347 options_set_number(so
, "display-panes-time", 1000);
348 options_set_number(so
, "display-time", 750);
349 options_set_number(so
, "history-limit", 2000);
350 options_set_number(so
, "lock-after-time", 0);
351 options_set_string(so
, "lock-command", "lock -np");
352 options_set_number(so
, "lock-server", 1);
353 options_set_number(so
, "message-attr", 0);
354 options_set_number(so
, "message-bg", 3);
355 options_set_number(so
, "message-fg", 0);
356 options_set_number(so
, "message-limit", 20);
357 options_set_number(so
, "mouse-select-pane", 0);
358 options_set_number(so
, "pane-active-border-bg", 8);
359 options_set_number(so
, "pane-active-border-fg", 2);
360 options_set_number(so
, "pane-border-bg", 8);
361 options_set_number(so
, "pane-border-fg", 8);
362 options_set_number(so
, "repeat-time", 500);
363 options_set_number(so
, "set-remain-on-exit", 0);
364 options_set_number(so
, "set-titles", 0);
365 options_set_string(so
, "set-titles-string", "#S:#I:#W - \"#T\"");
366 options_set_number(so
, "status", 1);
367 options_set_number(so
, "status-attr", 0);
368 options_set_number(so
, "status-bg", 2);
369 options_set_number(so
, "status-fg", 0);
370 options_set_number(so
, "status-interval", 15);
371 options_set_number(so
, "status-justify", 0);
372 options_set_number(so
, "status-keys", MODEKEY_EMACS
);
373 options_set_string(so
, "status-left", "[#S]");
374 options_set_number(so
, "status-left-attr", 0);
375 options_set_number(so
, "status-left-bg", 8);
376 options_set_number(so
, "status-left-fg", 8);
377 options_set_number(so
, "status-left-length", 10);
378 options_set_string(so
, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y");
379 options_set_number(so
, "status-right-attr", 0);
380 options_set_number(so
, "status-right-bg", 8);
381 options_set_number(so
, "status-right-fg", 8);
382 options_set_number(so
, "status-right-length", 40);
383 options_set_string(so
, "terminal-overrides",
384 "*88col*:colors=88,*256col*:colors=256");
385 options_set_string(so
, "update-environment", "DISPLAY "
386 "WINDOWID SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION");
387 options_set_number(so
, "visual-activity", 0);
388 options_set_number(so
, "visual-bell", 0);
389 options_set_number(so
, "visual-content", 0);
391 keylist
= xmalloc(sizeof *keylist
);
393 ARRAY_ADD(keylist
, '\002');
394 options_set_data(so
, "prefix", keylist
, xfree
);
396 options_init(&global_w_options
, NULL
);
397 wo
= &global_w_options
;
398 options_set_number(wo
, "aggressive-resize", 0);
399 options_set_number(wo
, "alternate-screen", 1);
400 options_set_number(wo
, "automatic-rename", 1);
401 options_set_number(wo
, "clock-mode-colour", 4);
402 options_set_number(wo
, "clock-mode-style", 1);
403 options_set_number(wo
, "force-height", 0);
404 options_set_number(wo
, "force-width", 0);
405 options_set_number(wo
, "main-pane-height", 24);
406 options_set_number(wo
, "main-pane-width", 81);
407 options_set_number(wo
, "mode-attr", 0);
408 options_set_number(wo
, "mode-bg", 3);
409 options_set_number(wo
, "mode-fg", 0);
410 options_set_number(wo
, "mode-keys", MODEKEY_EMACS
);
411 options_set_number(wo
, "mode-mouse", 0);
412 options_set_number(wo
, "monitor-activity", 0);
413 options_set_string(wo
, "monitor-content", "%s", "");
414 options_set_number(wo
, "window-status-attr", 0);
415 options_set_number(wo
, "window-status-bg", 8);
416 options_set_number(wo
, "window-status-current-attr", 0);
417 options_set_number(wo
, "window-status-current-bg", 8);
418 options_set_number(wo
, "window-status-current-fg", 8);
419 options_set_number(wo
, "window-status-fg", 8);
420 options_set_number(wo
, "window-status-alert-attr", GRID_ATTR_REVERSE
);
421 options_set_number(wo
, "window-status-alert-bg", 8);
422 options_set_number(wo
, "window-status-alert-fg", 8);
423 options_set_string(wo
, "window-status-format", "#I:#W#F");
424 options_set_string(wo
, "window-status-current-format", "#I:#W#F");
425 options_set_string(wo
, "word-separators", " -_@");
426 options_set_number(wo
, "xterm-keys", 0);
427 options_set_number(wo
, "remain-on-exit", 0);
428 options_set_number(wo
, "synchronize-panes", 0);
430 if (flags
& IDENTIFY_UTF8
) {
431 options_set_number(so
, "status-utf8", 1);
432 options_set_number(wo
, "utf8", 1);
434 options_set_number(so
, "status-utf8", 0);
435 options_set_number(wo
, "utf8", 0);
438 if (cfg_file
== NULL
) {
439 home
= getenv("HOME");
440 if (home
== NULL
|| *home
== '\0') {
441 pw
= getpwuid(getuid());
445 xasprintf(&cfg_file
, "%s/%s", home
, DEFAULT_CFG
);
446 if (access(cfg_file
, R_OK
) != 0 && errno
== ENOENT
) {
453 * Figure out the socket path. If specified on the command-line with
454 * -S or -L, use it, otherwise try $TMUX or assume -L default.
458 /* No -L. Try $TMUX, or default. */
462 label
= xstrdup("default");
465 /* -L or default set. */
467 if ((path
= makesockpath(label
)) == NULL
) {
468 log_warn("can't create socket");
476 if (shellcmd
!= NULL
) {
481 cmddata
.pid
= envdata
.pid
;
482 cmddata
.idx
= envdata
.idx
;
484 /* Prepare command for server. */
487 argc
, argv
, cmddata
.argv
, sizeof cmddata
.argv
) != 0) {
488 log_warnx("command too long");
494 len
= sizeof cmddata
;
497 if (shellcmd
!= NULL
)
498 cmdflags
|= CMD_STARTSERVER
;
499 else if (argc
== 0) /* new-session is the default */
500 cmdflags
|= CMD_STARTSERVER
|CMD_SENDENVIRON
|CMD_CANTNEST
;
503 * It sucks parsing the command string twice (in client and
504 * later in server) but it is necessary to get the start server
507 if ((cmdlist
= cmd_list_parse(argc
, argv
, &cause
)) == NULL
) {
508 log_warnx("%s", cause
);
511 cmdflags
&= ~CMD_STARTSERVER
;
512 TAILQ_FOREACH(cmd
, &cmdlist
->list
, qentry
) {
513 if (cmd
->entry
->flags
& CMD_STARTSERVER
)
514 cmdflags
|= CMD_STARTSERVER
;
515 if (cmd
->entry
->flags
& CMD_SENDENVIRON
)
516 cmdflags
|= CMD_SENDENVIRON
;
517 if (cmd
->entry
->flags
& CMD_CANTNEST
)
518 cmdflags
|= CMD_CANTNEST
;
520 cmd_list_free(cmdlist
);
524 * Check if this could be a nested session, if the command can't nest:
525 * if the socket path matches $TMUX, this is probably the same server.
527 if (shellcmd
== NULL
&& envdata
.path
!= NULL
&&
528 cmdflags
& CMD_CANTNEST
&&
529 (path
== envdata
.path
|| strcmp(path
, envdata
.path
) == 0)) {
530 log_warnx("sessions should be nested with care. "
531 "unset $TMUX to force.");
535 ev_base
= event_init();
536 set_signals(main_signal
);
538 /* Initialise the client socket/start the server. */
539 if ((main_ibuf
= client_init(path
, cmdflags
, flags
)) == NULL
)
543 imsg_compose(main_ibuf
, msg
, PROTOCOL_VERSION
, -1, -1, buf
, len
);
546 if (main_ibuf
->w
.queued
> 0)
548 event_once(main_ibuf
->fd
, events
, main_callback
, shellcmd
, NULL
);
554 client_main(); /* doesn't return */
559 main_signal(int sig
, unused
short events
, unused
void *data
)
567 waitpid(WAIT_ANY
, &status
, WNOHANG
);
574 main_callback(unused
int fd
, short events
, void *data
)
576 char *shellcmd
= data
;
578 if (events
& EV_READ
)
579 main_dispatch(shellcmd
);
581 if (events
& EV_WRITE
) {
582 if (msgbuf_write(&main_ibuf
->w
) < 0)
583 fatalx("msgbuf_write failed");
587 if (main_ibuf
->w
.queued
> 0)
589 event_once(main_ibuf
->fd
, events
, main_callback
, shellcmd
, NULL
);
593 main_dispatch(const char *shellcmd
)
597 struct msg_shell_data shelldata
;
598 struct msg_exit_data exitdata
;
600 if ((n
= imsg_read(main_ibuf
)) == -1 || n
== 0)
601 fatalx("imsg_read failed");
604 if ((n
= imsg_get(main_ibuf
, &imsg
)) == -1)
605 fatalx("imsg_get failed");
608 datalen
= imsg
.hdr
.len
- IMSG_HEADER_SIZE
;
610 switch (imsg
.hdr
.type
) {
613 if (datalen
!= sizeof exitdata
) {
615 fatalx("bad MSG_EXIT size");
618 memcpy(&exitdata
, imsg
.data
, sizeof exitdata
);
619 exit(exitdata
.retcode
);
622 fatalx("bad MSG_READY size");
624 event_loopexit(NULL
); /* move to client_main() */
628 fatalx("bad MSG_VERSION size");
630 log_warnx("protocol version mismatch (client %u, "
631 "server %u)", PROTOCOL_VERSION
, imsg
.hdr
.peerid
);
634 if (datalen
!= sizeof shelldata
)
635 fatalx("bad MSG_SHELL size");
636 memcpy(&shelldata
, imsg
.data
, sizeof shelldata
);
637 shelldata
.shell
[(sizeof shelldata
.shell
) - 1] = '\0';
641 shell_exec(shelldata
.shell
, shellcmd
);
643 fatalx("unexpected message");