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