tmux.c 7.8 KB

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