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