tmux.c revision 1.1.1.10 1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) gmail.com>
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>
21
22 #include <errno.h>
23 #include <event.h>
24 #include <fcntl.h>
25 #include <langinfo.h>
26 #include <locale.h>
27 #include <pwd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31 #include <unistd.h>
32
33 #include "tmux.h"
34
35 struct options *global_options; /* server options */
36 struct options *global_s_options; /* session options */
37 struct options *global_w_options; /* window options */
38 struct environ *global_environ;
39 struct hooks *global_hooks;
40
41 struct timeval start_time;
42 const char *socket_path;
43 int ptm_fd = -1;
44 const char *shell_command;
45
46 static __dead void usage(void);
47 static char *make_label(const char *, char **);
48
49 static const char *getshell(void);
50 static int checkshell(const char *);
51
52 static __dead void
53 usage(void)
54 {
55 fprintf(stderr,
56 "usage: %s [-2CluvV] [-c shell-command] [-f file] [-L socket-name]\n"
57 " [-S socket-path] [command [flags]]\n",
58 getprogname());
59 exit(1);
60 }
61
62 static const char *
63 getshell(void)
64 {
65 struct passwd *pw;
66 const char *shell;
67
68 shell = getenv("SHELL");
69 if (checkshell(shell))
70 return (shell);
71
72 pw = getpwuid(getuid());
73 if (pw != NULL && checkshell(pw->pw_shell))
74 return (pw->pw_shell);
75
76 return (_PATH_BSHELL);
77 }
78
79 static int
80 checkshell(const char *shell)
81 {
82 if (shell == NULL || *shell != '/')
83 return (0);
84 if (areshell(shell))
85 return (0);
86 if (access(shell, X_OK) != 0)
87 return (0);
88 return (1);
89 }
90
91 int
92 areshell(const char *shell)
93 {
94 const char *progname, *ptr;
95
96 if ((ptr = strrchr(shell, '/')) != NULL)
97 ptr++;
98 else
99 ptr = shell;
100 progname = getprogname();
101 if (*progname == '-')
102 progname++;
103 if (strcmp(ptr, progname) == 0)
104 return (1);
105 return (0);
106 }
107
108 static char *
109 make_label(const char *label, char **cause)
110 {
111 char *base, resolved[PATH_MAX], *path, *s;
112 struct stat sb;
113 uid_t uid;
114
115 *cause = NULL;
116
117 if (label == NULL)
118 label = "default";
119 uid = getuid();
120
121 if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0')
122 xasprintf(&base, "%s/tmux-%ld", s, (long)uid);
123 else
124 xasprintf(&base, "%s/tmux-%ld", _PATH_TMP, (long)uid);
125 if (realpath(base, resolved) == NULL &&
126 strlcpy(resolved, base, sizeof resolved) >= sizeof resolved) {
127 errno = ERANGE;
128 free(base);
129 goto fail;
130 }
131
132 if (mkdir(resolved, S_IRWXU) != 0 && errno != EEXIST)
133 goto fail;
134 if (lstat(resolved, &sb) != 0)
135 goto fail;
136 if (!S_ISDIR(sb.st_mode)) {
137 errno = ENOTDIR;
138 goto fail;
139 }
140 if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) {
141 errno = EACCES;
142 goto fail;
143 }
144 xasprintf(&path, "%s/%s", resolved, label);
145 return (path);
146
147 fail:
148 xasprintf(cause, "error creating %s (%s)", resolved, strerror(errno));
149 return (NULL);
150 }
151
152 void
153 setblocking(int fd, int state)
154 {
155 int mode;
156
157 if ((mode = fcntl(fd, F_GETFL)) != -1) {
158 if (!state)
159 mode |= O_NONBLOCK;
160 else
161 mode &= ~O_NONBLOCK;
162 fcntl(fd, F_SETFL, mode);
163 }
164 }
165
166 const char *
167 find_cwd(void)
168 {
169 char resolved1[PATH_MAX], resolved2[PATH_MAX];
170 static char cwd[PATH_MAX];
171 const char *pwd;
172
173 if (getcwd(cwd, sizeof cwd) == NULL)
174 return (NULL);
175 if ((pwd = getenv("PWD")) == NULL || *pwd == '\0')
176 return (cwd);
177
178 /*
179 * We want to use PWD so that symbolic links are maintained,
180 * but only if it matches the actual working directory.
181 */
182 if (realpath(pwd, resolved1) == NULL)
183 return (cwd);
184 if (realpath(cwd, resolved2) == NULL)
185 return (cwd);
186 if (strcmp(resolved1, resolved2) != 0)
187 return (cwd);
188 return (pwd);
189 }
190
191 const char *
192 find_home(void)
193 {
194 struct passwd *pw;
195 static const char *home;
196
197 if (home != NULL)
198 return (home);
199
200 home = getenv("HOME");
201 if (home == NULL || *home == '\0') {
202 pw = getpwuid(getuid());
203 if (pw != NULL)
204 home = pw->pw_dir;
205 else
206 home = NULL;
207 }
208
209 return (home);
210 }
211
212 int
213 main(int argc, char **argv)
214 {
215 char *path, *label, *cause, **var;
216 const char *s, *shell, *cwd;
217 int opt, flags, keys;
218 const struct options_table_entry *oe;
219
220 if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL &&
221 setlocale(LC_CTYPE, "C.UTF-8") == NULL) {
222 if (setlocale(LC_CTYPE, "") == NULL)
223 errx(1, "invalid LC_ALL, LC_CTYPE or LANG");
224 s = nl_langinfo(CODESET);
225 if (strcasecmp(s, "UTF-8") != 0 && strcasecmp(s, "UTF8") != 0)
226 errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s);
227 }
228
229 setlocale(LC_TIME, "");
230 tzset();
231
232 if (**argv == '-')
233 flags = CLIENT_LOGIN;
234 else
235 flags = 0;
236
237 label = path = NULL;
238 while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUVv")) != -1) {
239 switch (opt) {
240 case '2':
241 flags |= CLIENT_256COLOURS;
242 break;
243 case 'c':
244 shell_command = optarg;
245 break;
246 case 'C':
247 if (flags & CLIENT_CONTROL)
248 flags |= CLIENT_CONTROLCONTROL;
249 else
250 flags |= CLIENT_CONTROL;
251 break;
252 case 'V':
253 printf("%s %s\n", getprogname(), VERSION);
254 exit(0);
255 case 'f':
256 set_cfg_file(optarg);
257 break;
258 case 'l':
259 flags |= CLIENT_LOGIN;
260 break;
261 case 'L':
262 free(label);
263 label = xstrdup(optarg);
264 break;
265 case 'q':
266 break;
267 case 'S':
268 free(path);
269 path = xstrdup(optarg);
270 break;
271 case 'u':
272 flags |= CLIENT_UTF8;
273 break;
274 case 'v':
275 log_add_level();
276 break;
277 default:
278 usage();
279 }
280 }
281 argc -= optind;
282 argv += optind;
283
284 if (shell_command != NULL && argc != 0)
285 usage();
286
287 if ((ptm_fd = getptmfd()) == -1)
288 err(1, "getptmfd");
289 if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd "
290 "recvfd proc exec tty ps", NULL) != 0)
291 err(1, "pledge");
292
293 /*
294 * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8.
295 * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain
296 * UTF-8, it is a safe assumption that either they are using a UTF-8
297 * terminal, or if not they know that output from UTF-8-capable
298 * programs may be wrong.
299 */
300 if (getenv("TMUX") != NULL)
301 flags |= CLIENT_UTF8;
302 else {
303 s = getenv("LC_ALL");
304 if (s == NULL || *s == '\0')
305 s = getenv("LC_CTYPE");
306 if (s == NULL || *s == '\0')
307 s = getenv("LANG");
308 if (s == NULL || *s == '\0')
309 s = "";
310 if (strcasestr(s, "UTF-8") != NULL ||
311 strcasestr(s, "UTF8") != NULL)
312 flags |= CLIENT_UTF8;
313 }
314
315 global_hooks = hooks_create(NULL);
316
317 global_environ = environ_create();
318 for (var = environ; *var != NULL; var++)
319 environ_put(global_environ, *var);
320 if ((cwd = find_cwd()) != NULL)
321 environ_set(global_environ, "PWD", "%s", cwd);
322
323 global_options = options_create(NULL);
324 global_s_options = options_create(NULL);
325 global_w_options = options_create(NULL);
326 for (oe = options_table; oe->name != NULL; oe++) {
327 if (oe->scope == OPTIONS_TABLE_SERVER)
328 options_default(global_options, oe);
329 if (oe->scope == OPTIONS_TABLE_SESSION)
330 options_default(global_s_options, oe);
331 if (oe->scope == OPTIONS_TABLE_WINDOW)
332 options_default(global_w_options, oe);
333 }
334
335 /*
336 * The default shell comes from SHELL or from the user's passwd entry
337 * if available.
338 */
339 shell = getshell();
340 options_set_string(global_s_options, "default-shell", 0, "%s", shell);
341
342 /* Override keys to vi if VISUAL or EDITOR are set. */
343 if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
344 if (strrchr(s, '/') != NULL)
345 s = strrchr(s, '/') + 1;
346 if (strstr(s, "vi") != NULL)
347 keys = MODEKEY_VI;
348 else
349 keys = MODEKEY_EMACS;
350 options_set_number(global_s_options, "status-keys", keys);
351 options_set_number(global_w_options, "mode-keys", keys);
352 }
353
354 /*
355 * If socket is specified on the command-line with -S or -L, it is
356 * used. Otherwise, $TMUX is checked and if that fails "default" is
357 * used.
358 */
359 if (path == NULL && label == NULL) {
360 s = getenv("TMUX");
361 if (s != NULL && *s != '\0' && *s != ',') {
362 path = xstrdup(s);
363 path[strcspn(path, ",")] = '\0';
364 }
365 }
366 if (path == NULL && (path = make_label(label, &cause)) == NULL) {
367 if (cause != NULL) {
368 fprintf(stderr, "%s\n", cause);
369 free(cause);
370 }
371 exit(1);
372 }
373 socket_path = path;
374 free(label);
375
376 /* Pass control to the client. */
377 exit(client_main(osdep_event_init(), argc, argv, flags));
378 }
379