Skip to content
Newer
Older
100644 666 lines (585 sloc) 16.6 KB
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
1 /* $Id: tmux.c,v 1.214 2010/07/17 14:36:41 tcunha Exp $ */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
2
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5 *
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.
9 *
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.
17 */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
21 #include <sys/wait.h>
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
22
23 #include <errno.h>
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
24 #include <event.h>
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
25 #include <pwd.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <syslog.h>
30 #include <unistd.h>
31
32 #include "tmux.h"
33
34 #if defined(DEBUG) && defined(__OpenBSD__)
35 extern char *malloc_options;
36 #endif
37
38 char *cfg_file;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
39 struct options global_options; /* server options */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
40 struct options global_s_options; /* session options */
41 struct options global_w_options; /* window options */
42 struct environ global_environ;
43
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
44 struct event_base *ev_base;
45
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
46 int debug_level;
47 time_t start_time;
48 char *socket_path;
49 int login_shell;
50
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
51 struct env_data {
52 char *path;
53 pid_t pid;
54 u_int idx;
55 };
56
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
57 __dead void usage(void);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
58 void parse_env(struct env_data *);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
59 char *makesockpath(const char *);
60 __dead void shell_exec(const char *, const char *);
61
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
62 struct imsgbuf *main_ibuf;
63
64 void main_signal(int, short, unused void *);
65 void main_callback(int, short, void *);
66 void main_dispatch(const char *);
67
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
68 #ifndef HAVE_PROGNAME
69 char *__progname = (char *) "tmux";
70 #endif
71
72 __dead void
73 usage(void)
74 {
75 fprintf(stderr,
76 "usage: %s [-28lquv] [-c shell-command] [-f file] [-L socket-name]\n"
77 " [-S socket-path] [command [flags]]\n",
78 __progname);
79 exit(1);
80 }
81
82 void
83 logfile(const char *name)
84 {
85 char *path;
86
87 log_close();
88 if (debug_level > 0) {
89 xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid());
90 log_open_file(debug_level, path);
91 xfree(path);
92 }
93 }
94
95 const char *
96 getshell(void)
97 {
98 struct passwd *pw;
99 const char *shell;
100
101 shell = getenv("SHELL");
102 if (checkshell(shell))
103 return (shell);
104
105 pw = getpwuid(getuid());
106 if (pw != NULL && checkshell(pw->pw_shell))
107 return (pw->pw_shell);
108
109 return (_PATH_BSHELL);
110 }
111
112 int
113 checkshell(const char *shell)
114 {
115 if (shell == NULL || *shell == '\0' || areshell(shell))
116 return (0);
117 if (access(shell, X_OK) != 0)
118 return (0);
119 return (1);
120 }
121
122 int
123 areshell(const char *shell)
124 {
125 const char *progname, *ptr;
126
127 if ((ptr = strrchr(shell, '/')) != NULL)
128 ptr++;
129 else
130 ptr = shell;
131 progname = __progname;
132 if (*progname == '-')
133 progname++;
134 if (strcmp(ptr, progname) == 0)
135 return (1);
136 return (0);
137 }
138
139 void
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
140 parse_env(struct env_data *data)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
141 {
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
142 char *env, *path_pid, *pid_idx, buf[256];
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
143 size_t len;
144 const char *errstr;
145 long long ll;
146
147 data->pid = -1;
148 if ((env = getenv("TMUX")) == NULL)
149 return;
150
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
151 if ((path_pid = strchr(env, ',')) == NULL || path_pid == env)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
152 return;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
153 if ((pid_idx = strchr(path_pid + 1, ',')) == NULL)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
154 return;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
155 if ((pid_idx == path_pid + 1 || pid_idx[1] == '\0'))
156 return;
157
158 /* path */
159 len = path_pid - env;
160 data->path = xmalloc (len + 1);
161 memcpy(data->path, env, len);
162 data->path[len] = '\0';
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
163
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
164 /* pid */
165 len = pid_idx - path_pid - 1;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
166 if (len > (sizeof buf) - 1)
167 return;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
168 memcpy(buf, path_pid + 1, len);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
169 buf[len] = '\0';
170
171 ll = strtonum(buf, 0, LONG_MAX, &errstr);
172 if (errstr != NULL)
173 return;
174 data->pid = ll;
175
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
176 /* idx */
177 ll = strtonum(pid_idx+1, 0, UINT_MAX, &errstr);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
178 if (errstr != NULL)
179 return;
180 data->idx = ll;
181 }
182
183 char *
184 makesockpath(const char *label)
185 {
186 char base[MAXPATHLEN], *path;
187 struct stat sb;
188 u_int uid;
189
190 uid = getuid();
191 xsnprintf(base, MAXPATHLEN, "%s/tmux-%d", _PATH_TMP, uid);
192
193 if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
194 return (NULL);
195
196 if (lstat(base, &sb) != 0)
197 return (NULL);
198 if (!S_ISDIR(sb.st_mode)) {
199 errno = ENOTDIR;
200 return (NULL);
201 }
202 if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) {
203 errno = EACCES;
204 return (NULL);
205 }
206
207 xasprintf(&path, "%s/%s", base, label);
208 return (path);
209 }
210
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
211 __dead void
212 shell_exec(const char *shell, const char *shellcmd)
213 {
214 const char *shellname, *ptr;
215 char *argv0;
216
217 ptr = strrchr(shell, '/');
218 if (ptr != NULL && *(ptr + 1) != '\0')
219 shellname = ptr + 1;
220 else
221 shellname = shell;
222 if (login_shell)
223 xasprintf(&argv0, "-%s", shellname);
224 else
225 xasprintf(&argv0, "%s", shellname);
226 setenv("SHELL", shell, 1);
227
228 execl(shell, argv0, "-c", shellcmd, (char *) NULL);
229 fatal("execl failed");
230 }
231
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
232 int
233 main(int argc, char **argv)
234 {
235 struct cmd_list *cmdlist;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
236 struct cmd *cmd;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
237 enum msgtype msg;
238 struct passwd *pw;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
239 struct options *oo, *so, *wo;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
240 struct keylist *keylist;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
241 struct env_data envdata;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
242 struct msg_command_data cmddata;
243 char *s, *shellcmd, *path, *label, *home, *cause;
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
244 char **var;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
245 void *buf;
246 size_t len;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
247 int opt, flags, quiet = 0, cmdflags = 0;
248 short events;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
249
250 #if defined(DEBUG) && defined(__OpenBSD__)
251 malloc_options = (char *) "AFGJPX";
252 #endif
253
254 flags = 0;
255 shellcmd = label = path = NULL;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
256 envdata.path = NULL;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
257 login_shell = (**argv == '-');
258 while ((opt = getopt(argc, argv, "28c:df:lL:qS:uUv")) != -1) {
259 switch (opt) {
260 case '2':
261 flags |= IDENTIFY_256COLOURS;
262 flags &= ~IDENTIFY_88COLOURS;
263 break;
264 case '8':
265 flags |= IDENTIFY_88COLOURS;
266 flags &= ~IDENTIFY_256COLOURS;
267 break;
268 case 'c':
269 if (shellcmd != NULL)
270 xfree(shellcmd);
271 shellcmd = xstrdup(optarg);
272 break;
273 case 'f':
274 if (cfg_file != NULL)
275 xfree(cfg_file);
276 cfg_file = xstrdup(optarg);
277 break;
278 case 'l':
279 login_shell = 1;
280 break;
281 case 'L':
282 if (label != NULL)
283 xfree(label);
284 label = xstrdup(optarg);
285 break;
286 case 'q':
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
287 quiet = 1;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
288 break;
289 case 'S':
290 if (path != NULL)
291 xfree(path);
292 path = xstrdup(optarg);
293 break;
294 case 'u':
295 flags |= IDENTIFY_UTF8;
296 break;
297 case 'v':
298 debug_level++;
299 break;
300 default:
301 usage();
302 }
303 }
304 argc -= optind;
305 argv += optind;
306
307 if (shellcmd != NULL && argc != 0)
308 usage();
309
310 log_open_tty(debug_level);
311
312 if (!(flags & IDENTIFY_UTF8)) {
313 /*
314 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG
315 * exist (in that order) to contain UTF-8, it is a safe
316 * assumption that either they are using a UTF-8 terminal, or
317 * if not they know that output from UTF-8-capable programs may
318 * be wrong.
319 */
320 if ((s = getenv("LC_ALL")) == NULL) {
321 if ((s = getenv("LC_CTYPE")) == NULL)
322 s = getenv("LANG");
323 }
324 if (s != NULL && (strcasestr(s, "UTF-8") != NULL ||
325 strcasestr(s, "UTF8") != NULL))
326 flags |= IDENTIFY_UTF8;
327 }
328
329 environ_init(&global_environ);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
330 for (var = environ; *var != NULL; var++)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
331 environ_put(&global_environ, *var);
332
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
333 options_init(&global_options, NULL);
334 oo = &global_options;
335 options_set_number(oo, "quiet", quiet);
336 options_set_number(oo, "escape-time", 500);
337
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
338 options_init(&global_s_options, NULL);
339 so = &global_s_options;
340 options_set_number(so, "base-index", 0);
341 options_set_number(so, "bell-action", BELL_ANY);
342 options_set_number(so, "buffer-limit", 9);
343 options_set_string(so, "default-command", "%s", "");
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
344 options_set_string(so, "default-path", "%s", "");
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
345 options_set_string(so, "default-shell", "%s", getshell());
346 options_set_string(so, "default-terminal", "screen");
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
347 options_set_number(so, "detach-on-destroy", 1);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
348 options_set_number(so, "display-panes-active-colour", 1);
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
349 options_set_number(so, "display-panes-colour", 4);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
350 options_set_number(so, "display-panes-time", 1000);
351 options_set_number(so, "display-time", 750);
352 options_set_number(so, "history-limit", 2000);
353 options_set_number(so, "lock-after-time", 0);
354 options_set_string(so, "lock-command", "lock -np");
355 options_set_number(so, "lock-server", 1);
356 options_set_number(so, "message-attr", 0);
357 options_set_number(so, "message-bg", 3);
358 options_set_number(so, "message-fg", 0);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
359 options_set_number(so, "message-limit", 20);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
360 options_set_number(so, "mouse-select-pane", 0);
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
361 options_set_number(so, "pane-active-border-bg", 8);
362 options_set_number(so, "pane-active-border-fg", 2);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
363 options_set_number(so, "pane-border-bg", 8);
364 options_set_number(so, "pane-border-fg", 8);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
365 options_set_number(so, "repeat-time", 500);
366 options_set_number(so, "set-remain-on-exit", 0);
367 options_set_number(so, "set-titles", 0);
368 options_set_string(so, "set-titles-string", "#S:#I:#W - \"#T\"");
369 options_set_number(so, "status", 1);
370 options_set_number(so, "status-attr", 0);
371 options_set_number(so, "status-bg", 2);
372 options_set_number(so, "status-fg", 0);
373 options_set_number(so, "status-interval", 15);
374 options_set_number(so, "status-justify", 0);
375 options_set_number(so, "status-keys", MODEKEY_EMACS);
376 options_set_string(so, "status-left", "[#S]");
377 options_set_number(so, "status-left-attr", 0);
378 options_set_number(so, "status-left-bg", 8);
379 options_set_number(so, "status-left-fg", 8);
380 options_set_number(so, "status-left-length", 10);
381 options_set_string(so, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y");
382 options_set_number(so, "status-right-attr", 0);
383 options_set_number(so, "status-right-bg", 8);
384 options_set_number(so, "status-right-fg", 8);
385 options_set_number(so, "status-right-length", 40);
386 options_set_string(so, "terminal-overrides",
387 "*88col*:colors=88,*256col*:colors=256");
388 options_set_string(so, "update-environment", "DISPLAY "
389 "WINDOWID SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION");
390 options_set_number(so, "visual-activity", 0);
391 options_set_number(so, "visual-bell", 0);
392 options_set_number(so, "visual-content", 0);
393
394 keylist = xmalloc(sizeof *keylist);
395 ARRAY_INIT(keylist);
396 ARRAY_ADD(keylist, '\002');
397 options_set_data(so, "prefix", keylist, xfree);
398
399 options_init(&global_w_options, NULL);
400 wo = &global_w_options;
401 options_set_number(wo, "aggressive-resize", 0);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
402 options_set_number(wo, "alternate-screen", 1);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
403 options_set_number(wo, "automatic-rename", 1);
404 options_set_number(wo, "clock-mode-colour", 4);
405 options_set_number(wo, "clock-mode-style", 1);
406 options_set_number(wo, "force-height", 0);
407 options_set_number(wo, "force-width", 0);
408 options_set_number(wo, "main-pane-height", 24);
409 options_set_number(wo, "main-pane-width", 81);
410 options_set_number(wo, "mode-attr", 0);
411 options_set_number(wo, "mode-bg", 3);
412 options_set_number(wo, "mode-fg", 0);
413 options_set_number(wo, "mode-keys", MODEKEY_EMACS);
414 options_set_number(wo, "mode-mouse", 0);
415 options_set_number(wo, "monitor-activity", 0);
416 options_set_string(wo, "monitor-content", "%s", "");
417 options_set_number(wo, "window-status-attr", 0);
418 options_set_number(wo, "window-status-bg", 8);
419 options_set_number(wo, "window-status-current-attr", 0);
420 options_set_number(wo, "window-status-current-bg", 8);
421 options_set_number(wo, "window-status-current-fg", 8);
422 options_set_number(wo, "window-status-fg", 8);
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
423 options_set_number(wo, "window-status-alert-attr", GRID_ATTR_REVERSE);
424 options_set_number(wo, "window-status-alert-bg", 8);
425 options_set_number(wo, "window-status-alert-fg", 8);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
426 options_set_string(wo, "window-status-format", "#I:#W#F");
427 options_set_string(wo, "window-status-current-format", "#I:#W#F");
428 options_set_string(wo, "word-separators", " -_@");
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
429 options_set_number(wo, "xterm-keys", 0);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
430 options_set_number(wo, "remain-on-exit", 0);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
431 options_set_number(wo, "synchronize-panes", 0);
432
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
433 if (flags & IDENTIFY_UTF8) {
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
434 options_set_number(so, "status-utf8", 1);
435 options_set_number(wo, "utf8", 1);
436 } else {
437 options_set_number(so, "status-utf8", 0);
438 options_set_number(wo, "utf8", 0);
439 }
440
441 if (cfg_file == NULL) {
442 home = getenv("HOME");
443 if (home == NULL || *home == '\0') {
444 pw = getpwuid(getuid());
445 if (pw != NULL)
446 home = pw->pw_dir;
447 }
448 xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
449 if (access(cfg_file, R_OK) != 0 && errno == ENOENT) {
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
450 xfree(cfg_file);
451 cfg_file = NULL;
452 }
453 }
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
454
455 /*
456 * Figure out the socket path. If specified on the command-line with
457 * -S or -L, use it, otherwise try $TMUX or assume -L default.
458 */
459 parse_env(&envdata);
460 if (path == NULL) {
461 /* No -L. Try $TMUX, or default. */
462 if (label == NULL) {
463 path = envdata.path;
464 if (path == NULL)
465 label = xstrdup("default");
466 }
467
468 /* -L or default set. */
469 if (label != NULL) {
470 if ((path = makesockpath(label)) == NULL) {
471 log_warn("can't create socket");
472 exit(1);
473 }
474 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
475 }
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
476 if (label != NULL)
477 xfree(label);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
478
479 if (shellcmd != NULL) {
480 msg = MSG_SHELL;
481 buf = NULL;
482 len = 0;
483 } else {
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
484 cmddata.pid = envdata.pid;
485 cmddata.idx = envdata.idx;
486
487 /* Prepare command for server. */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
488 cmddata.argc = argc;
489 if (cmd_pack_argv(
490 argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
491 log_warnx("command too long");
492 exit(1);
493 }
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
494
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
495 msg = MSG_COMMAND;
496 buf = &cmddata;
497 len = sizeof cmddata;
498 }
499
500 if (shellcmd != NULL)
501 cmdflags |= CMD_STARTSERVER;
502 else if (argc == 0) /* new-session is the default */
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
503 cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON|CMD_CANTNEST;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
504 else {
505 /*
506 * It sucks parsing the command string twice (in client and
507 * later in server) but it is necessary to get the start server
508 * flag.
509 */
510 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
511 log_warnx("%s", cause);
512 exit(1);
513 }
514 cmdflags &= ~CMD_STARTSERVER;
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
515 TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
516 if (cmd->entry->flags & CMD_STARTSERVER)
517 cmdflags |= CMD_STARTSERVER;
518 if (cmd->entry->flags & CMD_SENDENVIRON)
519 cmdflags |= CMD_SENDENVIRON;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
520 if (cmd->entry->flags & CMD_CANTNEST)
521 cmdflags |= CMD_CANTNEST;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
522 }
523 cmd_list_free(cmdlist);
524 }
525
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
526 /*
527 * Check if this could be a nested session, if the command can't nest:
528 * if the socket path matches $TMUX, this is probably the same server.
529 */
530 if (shellcmd == NULL && envdata.path != NULL &&
531 cmdflags & CMD_CANTNEST &&
532 (path == envdata.path || strcmp(path, envdata.path) == 0)) {
533 log_warnx("sessions should be nested with care. "
534 "unset $TMUX to force.");
535 exit(1);
536 }
537
538 #ifdef HAVE_BROKEN_KQUEUE
539 if (setenv("EVENT_NOKQUEUE", "1", 1) != 0)
540 fatal("setenv failed");
541 #endif
542 #ifdef HAVE_BROKEN_POLL
543 if (setenv("EVENT_NOPOLL", "1", 1) != 0)
544 fatal("setenv failed");
545 #endif
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
546 ev_base = event_init();
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
547 #ifdef HAVE_BROKEN_KQUEUE
548 unsetenv("EVENT_NOKQUEUE");
549 #endif
550 #ifdef HAVE_BROKEN_POLL
551 unsetenv("EVENT_NOPOLL");
552 #endif
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
553 set_signals(main_signal);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
554
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
555 /* Initialise the client socket/start the server. */
556 if ((main_ibuf = client_init(path, cmdflags, flags)) == NULL)
557 exit(1);
558 xfree(path);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
559
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
560 imsg_compose(main_ibuf, msg, PROTOCOL_VERSION, -1, -1, buf, len);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
561
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
562 events = EV_READ;
563 if (main_ibuf->w.queued > 0)
564 events |= EV_WRITE;
565 event_once(main_ibuf->fd, events, main_callback, shellcmd, NULL);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
566
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
567 event_dispatch();
568
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
569 clear_signals();
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
570
571 client_main(); /* doesn't return */
572 }
573
574 /* ARGSUSED */
575 void
576 main_signal(int sig, unused short events, unused void *data)
577 {
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
578 int status;
579
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
580 switch (sig) {
581 case SIGTERM:
582 exit(1);
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
583 case SIGCHLD:
584 waitpid(WAIT_ANY, &status, WNOHANG);
585 break;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
586 }
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
587 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
588
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
589 /* ARGSUSED */
590 void
591 main_callback(unused int fd, short events, void *data)
592 {
593 char *shellcmd = data;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
594
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
595 if (events & EV_READ)
596 main_dispatch(shellcmd);
597
598 if (events & EV_WRITE) {
599 if (msgbuf_write(&main_ibuf->w) < 0)
600 fatalx("msgbuf_write failed");
601 }
602
603 events = EV_READ;
604 if (main_ibuf->w.queued > 0)
605 events |= EV_WRITE;
606 event_once(main_ibuf->fd, events, main_callback, shellcmd, NULL);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
607 }
608
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
609 void
610 main_dispatch(const char *shellcmd)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
611 {
612 struct imsg imsg;
613 ssize_t n, datalen;
614 struct msg_shell_data shelldata;
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
615 struct msg_exit_data exitdata;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
616
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
617 if ((n = imsg_read(main_ibuf)) == -1 || n == 0)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
618 fatalx("imsg_read failed");
619
620 for (;;) {
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
621 if ((n = imsg_get(main_ibuf, &imsg)) == -1)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
622 fatalx("imsg_get failed");
623 if (n == 0)
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
624 return;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
625 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
626
627 switch (imsg.hdr.type) {
628 case MSG_EXIT:
629 case MSG_SHUTDOWN:
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
630 if (datalen != sizeof exitdata) {
631 if (datalen != 0)
632 fatalx("bad MSG_EXIT size");
633 exit(0);
634 }
635 memcpy(&exitdata, imsg.data, sizeof exitdata);
636 exit(exitdata.retcode);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
637 case MSG_READY:
638 if (datalen != 0)
639 fatalx("bad MSG_READY size");
640
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
641 event_loopexit(NULL); /* move to client_main() */
642 break;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
643 case MSG_VERSION:
644 if (datalen != 0)
645 fatalx("bad MSG_VERSION size");
646
647 log_warnx("protocol version mismatch (client %u, "
648 "server %u)", PROTOCOL_VERSION, imsg.hdr.peerid);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
649 exit(1);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
650 case MSG_SHELL:
651 if (datalen != sizeof shelldata)
652 fatalx("bad MSG_SHELL size");
653 memcpy(&shelldata, imsg.data, sizeof shelldata);
654 shelldata.shell[(sizeof shelldata.shell) - 1] = '\0';
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
655
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
656 clear_signals();
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
657
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
658 shell_exec(shelldata.shell, shellcmd);
659 default:
660 fatalx("unexpected message");
661 }
662
663 imsg_free(&imsg);
664 }
665 }
Something went wrong with that request. Please try again.