NULL needs to be cast to void * when passed to a variadic function like
[u/mdw/putty] / unix / uxpty.c
1 /*
2 * Pseudo-tty backend for pterm.
3 */
4
5 #define _XOPEN_SOURCE 600
6 #define _XOPEN_SOURCE_EXTENDED
7 #define _GNU_SOURCE
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <signal.h>
14 #include <fcntl.h>
15 #include <termios.h>
16 #include <grp.h>
17 #include <utmp.h>
18 #include <pwd.h>
19 #include <time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/wait.h>
23 #include <sys/ioctl.h>
24 #include <errno.h>
25
26 #include "putty.h"
27 #include "tree234.h"
28
29 #ifndef FALSE
30 #define FALSE 0
31 #endif
32 #ifndef TRUE
33 #define TRUE 1
34 #endif
35
36 #ifndef UTMP_FILE
37 #define UTMP_FILE "/var/run/utmp"
38 #endif
39 #ifndef WTMP_FILE
40 #define WTMP_FILE "/var/log/wtmp"
41 #endif
42 #ifndef LASTLOG_FILE
43 #ifdef _PATH_LASTLOG
44 #define LASTLOG_FILE _PATH_LASTLOG
45 #else
46 #define LASTLOG_FILE "/var/log/lastlog"
47 #endif
48 #endif
49
50 /*
51 * Set up a default for vaguely sane systems. The idea is that if
52 * OMIT_UTMP is not defined, then at least one of the symbols which
53 * enable particular forms of utmp processing should be, if only so
54 * that a link error can warn you that you should have defined
55 * OMIT_UTMP if you didn't want any. Currently HAVE_PUTUTLINE is
56 * the only such symbol.
57 */
58 #ifndef OMIT_UTMP
59 #if !defined HAVE_PUTUTLINE
60 #define HAVE_PUTUTLINE
61 #endif
62 #endif
63
64 typedef struct pty_tag *Pty;
65
66 /*
67 * The pty_signal_pipe, along with the SIGCHLD handler, must be
68 * process-global rather than session-specific.
69 */
70 static int pty_signal_pipe[2] = { -1, -1 }; /* obviously bogus initial val */
71
72 struct pty_tag {
73 Config cfg;
74 int master_fd, slave_fd;
75 void *frontend;
76 char name[FILENAME_MAX];
77 int child_pid;
78 int term_width, term_height;
79 int child_dead, finished;
80 int exit_code;
81 };
82
83 /*
84 * We store our pty backends in a tree sorted by master fd, so that
85 * when we get an uxsel notification we know which backend instance
86 * is the owner of the pty that caused it.
87 */
88 static int pty_compare_by_fd(void *av, void *bv)
89 {
90 Pty a = (Pty)av;
91 Pty b = (Pty)bv;
92
93 if (a->master_fd < b->master_fd)
94 return -1;
95 else if (a->master_fd > b->master_fd)
96 return +1;
97 return 0;
98 }
99
100 static int pty_find_by_fd(void *av, void *bv)
101 {
102 int a = *(int *)av;
103 Pty b = (Pty)bv;
104
105 if (a < b->master_fd)
106 return -1;
107 else if (a > b->master_fd)
108 return +1;
109 return 0;
110 }
111
112 static tree234 *ptys_by_fd = NULL;
113
114 /*
115 * We also have a tree sorted by child pid, so that when we wait()
116 * in response to the signal we know which backend instance is the
117 * owner of the process that caused the signal.
118 */
119 static int pty_compare_by_pid(void *av, void *bv)
120 {
121 Pty a = (Pty)av;
122 Pty b = (Pty)bv;
123
124 if (a->child_pid < b->child_pid)
125 return -1;
126 else if (a->child_pid > b->child_pid)
127 return +1;
128 return 0;
129 }
130
131 static int pty_find_by_pid(void *av, void *bv)
132 {
133 int a = *(int *)av;
134 Pty b = (Pty)bv;
135
136 if (a < b->child_pid)
137 return -1;
138 else if (a > b->child_pid)
139 return +1;
140 return 0;
141 }
142
143 static tree234 *ptys_by_pid = NULL;
144
145 /*
146 * If we are using pty_pre_init(), it will need to have already
147 * allocated a pty structure, which we must then return from
148 * pty_init() rather than allocating a new one. Here we store that
149 * structure between allocation and use.
150 *
151 * Note that although most of this module is entirely capable of
152 * handling multiple ptys in a single process, pty_pre_init() is
153 * fundamentally _dependent_ on there being at most one pty per
154 * process, so the normal static-data constraints don't apply.
155 *
156 * Likewise, since utmp is only used via pty_pre_init, it too must
157 * be single-instance, so we can declare utmp-related variables
158 * here.
159 */
160 static Pty single_pty = NULL;
161
162 #ifndef OMIT_UTMP
163 static int pty_utmp_helper_pid, pty_utmp_helper_pipe;
164 static int pty_stamped_utmp;
165 static struct utmp utmp_entry;
166 #endif
167
168 /*
169 * pty_argv is a grievous hack to allow a proper argv to be passed
170 * through from the Unix command line. Again, it doesn't really
171 * make sense outside a one-pty-per-process setup.
172 */
173 char **pty_argv;
174
175 static void pty_close(Pty pty);
176
177 #ifndef OMIT_UTMP
178 static void setup_utmp(char *ttyname, char *location)
179 {
180 #ifdef HAVE_LASTLOG
181 struct lastlog lastlog_entry;
182 FILE *lastlog;
183 #endif
184 struct passwd *pw;
185 FILE *wtmp;
186 time_t uttime;
187
188 pw = getpwuid(getuid());
189 memset(&utmp_entry, 0, sizeof(utmp_entry));
190 utmp_entry.ut_type = USER_PROCESS;
191 utmp_entry.ut_pid = getpid();
192 strncpy(utmp_entry.ut_line, ttyname+5, lenof(utmp_entry.ut_line));
193 strncpy(utmp_entry.ut_id, ttyname+8, lenof(utmp_entry.ut_id));
194 strncpy(utmp_entry.ut_user, pw->pw_name, lenof(utmp_entry.ut_user));
195 strncpy(utmp_entry.ut_host, location, lenof(utmp_entry.ut_host));
196 /* Apparently there are some architectures where (struct utmp).ut_time
197 * is not essentially time_t (e.g. Linux amd64). Hence the temporary. */
198 time(&uttime);
199 utmp_entry.ut_time = uttime; /* may truncate */
200
201 #if defined HAVE_PUTUTLINE
202 utmpname(UTMP_FILE);
203 setutent();
204 pututline(&utmp_entry);
205 endutent();
206 #endif
207
208 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
209 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
210 fclose(wtmp);
211 }
212
213 #ifdef HAVE_LASTLOG
214 memset(&lastlog_entry, 0, sizeof(lastlog_entry));
215 strncpy(lastlog_entry.ll_line, ttyname+5, lenof(lastlog_entry.ll_line));
216 strncpy(lastlog_entry.ll_host, location, lenof(lastlog_entry.ll_host));
217 time(&lastlog_entry.ll_time);
218 if ((lastlog = fopen(LASTLOG_FILE, "r+")) != NULL) {
219 fseek(lastlog, sizeof(lastlog_entry) * getuid(), SEEK_SET);
220 fwrite(&lastlog_entry, 1, sizeof(lastlog_entry), lastlog);
221 fclose(lastlog);
222 }
223 #endif
224
225 pty_stamped_utmp = 1;
226
227 }
228
229 static void cleanup_utmp(void)
230 {
231 FILE *wtmp;
232 time_t uttime;
233
234 if (!pty_stamped_utmp)
235 return;
236
237 utmp_entry.ut_type = DEAD_PROCESS;
238 memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
239 time(&uttime);
240 utmp_entry.ut_time = uttime;
241
242 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
243 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
244 fclose(wtmp);
245 }
246
247 memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line));
248 utmp_entry.ut_time = 0;
249
250 #if defined HAVE_PUTUTLINE
251 utmpname(UTMP_FILE);
252 setutent();
253 pututline(&utmp_entry);
254 endutent();
255 #endif
256
257 pty_stamped_utmp = 0; /* ensure we never double-cleanup */
258 }
259 #endif
260
261 static void sigchld_handler(int signum)
262 {
263 write(pty_signal_pipe[1], "x", 1);
264 }
265
266 #ifndef OMIT_UTMP
267 static void fatal_sig_handler(int signum)
268 {
269 putty_signal(signum, SIG_DFL);
270 cleanup_utmp();
271 setuid(getuid());
272 raise(signum);
273 }
274 #endif
275
276 static int pty_open_slave(Pty pty)
277 {
278 if (pty->slave_fd < 0)
279 pty->slave_fd = open(pty->name, O_RDWR);
280
281 return pty->slave_fd;
282 }
283
284 static void pty_open_master(Pty pty)
285 {
286 #ifdef BSD_PTYS
287 const char chars1[] = "pqrstuvwxyz";
288 const char chars2[] = "0123456789abcdef";
289 const char *p1, *p2;
290 char master_name[20];
291 struct group *gp;
292
293 for (p1 = chars1; *p1; p1++)
294 for (p2 = chars2; *p2; p2++) {
295 sprintf(master_name, "/dev/pty%c%c", *p1, *p2);
296 pty->master_fd = open(master_name, O_RDWR);
297 if (pty->master_fd >= 0) {
298 if (geteuid() == 0 ||
299 access(master_name, R_OK | W_OK) == 0) {
300 /*
301 * We must also check at this point that we are
302 * able to open the slave side of the pty. We
303 * wouldn't want to allocate the wrong master,
304 * get all the way down to forking, and _then_
305 * find we're unable to open the slave.
306 */
307 strcpy(pty->name, master_name);
308 pty->name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
309
310 if (pty_open_slave(pty) >= 0 &&
311 access(pty->name, R_OK | W_OK) == 0)
312 goto got_one;
313 if (pty->slave_fd > 0)
314 close(pty->slave_fd);
315 pty->slave_fd = -1;
316 }
317 close(pty->master_fd);
318 }
319 }
320
321 /* If we get here, we couldn't get a tty at all. */
322 fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n");
323 exit(1);
324
325 got_one:
326
327 /* We need to chown/chmod the /dev/ttyXX device. */
328 gp = getgrnam("tty");
329 chown(pty->name, getuid(), gp ? gp->gr_gid : -1);
330 chmod(pty->name, 0600);
331 #else
332 pty->master_fd = open("/dev/ptmx", O_RDWR);
333
334 if (pty->master_fd < 0) {
335 perror("/dev/ptmx: open");
336 exit(1);
337 }
338
339 if (grantpt(pty->master_fd) < 0) {
340 perror("grantpt");
341 exit(1);
342 }
343
344 if (unlockpt(pty->master_fd) < 0) {
345 perror("unlockpt");
346 exit(1);
347 }
348
349 pty->name[FILENAME_MAX-1] = '\0';
350 strncpy(pty->name, ptsname(pty->master_fd), FILENAME_MAX-1);
351 #endif
352
353 if (!ptys_by_fd)
354 ptys_by_fd = newtree234(pty_compare_by_fd);
355 add234(ptys_by_fd, pty);
356 }
357
358 /*
359 * Pre-initialisation. This is here to get around the fact that GTK
360 * doesn't like being run in setuid/setgid programs (probably
361 * sensibly). So before we initialise GTK - and therefore before we
362 * even process the command line - we check to see if we're running
363 * set[ug]id. If so, we open our pty master _now_, chown it as
364 * necessary, and drop privileges. We can always close it again
365 * later. If we're potentially going to be doing utmp as well, we
366 * also fork off a utmp helper process and communicate with it by
367 * means of a pipe; the utmp helper will keep privileges in order
368 * to clean up utmp when we exit (i.e. when its end of our pipe
369 * closes).
370 */
371 void pty_pre_init(void)
372 {
373 Pty pty;
374
375 #ifndef OMIT_UTMP
376 pid_t pid;
377 int pipefd[2];
378 #endif
379
380 pty = single_pty = snew(struct pty_tag);
381
382 /* set the child signal handler straight away; it needs to be set
383 * before we ever fork. */
384 putty_signal(SIGCHLD, sigchld_handler);
385 pty->master_fd = pty->slave_fd = -1;
386 #ifndef OMIT_UTMP
387 pty_stamped_utmp = FALSE;
388 #endif
389
390 if (geteuid() != getuid() || getegid() != getgid()) {
391 pty_open_master(pty);
392 }
393
394 #ifndef OMIT_UTMP
395 /*
396 * Fork off the utmp helper.
397 */
398 if (pipe(pipefd) < 0) {
399 perror("pterm: pipe");
400 exit(1);
401 }
402 pid = fork();
403 if (pid < 0) {
404 perror("pterm: fork");
405 exit(1);
406 } else if (pid == 0) {
407 char display[128], buffer[128];
408 int dlen, ret;
409
410 close(pipefd[1]);
411 /*
412 * Now sit here until we receive a display name from the
413 * other end of the pipe, and then stamp utmp. Unstamp utmp
414 * again, and exit, when the pipe closes.
415 */
416
417 dlen = 0;
418 while (1) {
419
420 ret = read(pipefd[0], buffer, lenof(buffer));
421 if (ret <= 0) {
422 cleanup_utmp();
423 _exit(0);
424 } else if (!pty_stamped_utmp) {
425 if (dlen < lenof(display))
426 memcpy(display+dlen, buffer,
427 min(ret, lenof(display)-dlen));
428 if (buffer[ret-1] == '\0') {
429 /*
430 * Now we have a display name. NUL-terminate
431 * it, and stamp utmp.
432 */
433 display[lenof(display)-1] = '\0';
434 /*
435 * Trap as many fatal signals as we can in the
436 * hope of having the best possible chance to
437 * clean up utmp before termination. We are
438 * unfortunately unprotected against SIGKILL,
439 * but that's life.
440 */
441 putty_signal(SIGHUP, fatal_sig_handler);
442 putty_signal(SIGINT, fatal_sig_handler);
443 putty_signal(SIGQUIT, fatal_sig_handler);
444 putty_signal(SIGILL, fatal_sig_handler);
445 putty_signal(SIGABRT, fatal_sig_handler);
446 putty_signal(SIGFPE, fatal_sig_handler);
447 putty_signal(SIGPIPE, fatal_sig_handler);
448 putty_signal(SIGALRM, fatal_sig_handler);
449 putty_signal(SIGTERM, fatal_sig_handler);
450 putty_signal(SIGSEGV, fatal_sig_handler);
451 putty_signal(SIGUSR1, fatal_sig_handler);
452 putty_signal(SIGUSR2, fatal_sig_handler);
453 #ifdef SIGBUS
454 putty_signal(SIGBUS, fatal_sig_handler);
455 #endif
456 #ifdef SIGPOLL
457 putty_signal(SIGPOLL, fatal_sig_handler);
458 #endif
459 #ifdef SIGPROF
460 putty_signal(SIGPROF, fatal_sig_handler);
461 #endif
462 #ifdef SIGSYS
463 putty_signal(SIGSYS, fatal_sig_handler);
464 #endif
465 #ifdef SIGTRAP
466 putty_signal(SIGTRAP, fatal_sig_handler);
467 #endif
468 #ifdef SIGVTALRM
469 putty_signal(SIGVTALRM, fatal_sig_handler);
470 #endif
471 #ifdef SIGXCPU
472 putty_signal(SIGXCPU, fatal_sig_handler);
473 #endif
474 #ifdef SIGXFSZ
475 putty_signal(SIGXFSZ, fatal_sig_handler);
476 #endif
477 #ifdef SIGIO
478 putty_signal(SIGIO, fatal_sig_handler);
479 #endif
480 setup_utmp(pty->name, display);
481 }
482 }
483 }
484 } else {
485 close(pipefd[0]);
486 pty_utmp_helper_pid = pid;
487 pty_utmp_helper_pipe = pipefd[1];
488 }
489 #endif
490
491 /* Drop privs. */
492 {
493 #ifndef HAVE_NO_SETRESUID
494 int gid = getgid(), uid = getuid();
495 int setresgid(gid_t, gid_t, gid_t);
496 int setresuid(uid_t, uid_t, uid_t);
497 setresgid(gid, gid, gid);
498 setresuid(uid, uid, uid);
499 #else
500 setgid(getgid());
501 setuid(getuid());
502 #endif
503 }
504 }
505
506 int pty_real_select_result(Pty pty, int event, int status)
507 {
508 char buf[4096];
509 int ret;
510 int finished = FALSE;
511
512 if (event < 0) {
513 /*
514 * We've been called because our child process did
515 * something. `status' tells us what.
516 */
517 if ((WIFEXITED(status) || WIFSIGNALED(status))) {
518 /*
519 * The primary child process died. We could keep
520 * the terminal open for remaining subprocesses to
521 * output to, but conventional wisdom seems to feel
522 * that that's the Wrong Thing for an xterm-alike,
523 * so we bail out now (though we don't necessarily
524 * _close_ the window, depending on the state of
525 * Close On Exit). This would be easy enough to
526 * change or make configurable if necessary.
527 */
528 pty->exit_code = status;
529 pty->child_dead = TRUE;
530 del234(ptys_by_pid, pty);
531 finished = TRUE;
532 }
533 } else {
534 if (event == 1) {
535
536 ret = read(pty->master_fd, buf, sizeof(buf));
537
538 /*
539 * Clean termination condition is that either ret == 0, or ret
540 * < 0 and errno == EIO. Not sure why the latter, but it seems
541 * to happen. Boo.
542 */
543 if (ret == 0 || (ret < 0 && errno == EIO)) {
544 /*
545 * We assume a clean exit if the pty has closed but the
546 * actual child process hasn't. The only way I can
547 * imagine this happening is if it detaches itself from
548 * the pty and goes daemonic - in which case the
549 * expected usage model would precisely _not_ be for
550 * the pterm window to hang around!
551 */
552 finished = TRUE;
553 if (!pty->child_dead)
554 pty->exit_code = 0;
555 } else if (ret < 0) {
556 perror("read pty master");
557 exit(1);
558 } else if (ret > 0) {
559 from_backend(pty->frontend, 0, buf, ret);
560 }
561 }
562 }
563
564 if (finished && !pty->finished) {
565 uxsel_del(pty->master_fd);
566 pty_close(pty);
567 pty->master_fd = -1;
568
569 pty->finished = TRUE;
570
571 /*
572 * This is a slight layering-violation sort of hack: only
573 * if we're not closing on exit (COE is set to Never, or to
574 * Only On Clean and it wasn't a clean exit) do we output a
575 * `terminated' message.
576 */
577 if (pty->cfg.close_on_exit == FORCE_OFF ||
578 (pty->cfg.close_on_exit == AUTO && pty->exit_code != 0)) {
579 char message[512];
580 if (WIFEXITED(pty->exit_code))
581 sprintf(message, "\r\n[pterm: process terminated with exit"
582 " code %d]\r\n", WEXITSTATUS(pty->exit_code));
583 else if (WIFSIGNALED(pty->exit_code))
584 #ifdef HAVE_NO_STRSIGNAL
585 sprintf(message, "\r\n[pterm: process terminated on signal"
586 " %d]\r\n", WTERMSIG(pty->exit_code));
587 #else
588 sprintf(message, "\r\n[pterm: process terminated on signal"
589 " %d (%.400s)]\r\n", WTERMSIG(pty->exit_code),
590 strsignal(WTERMSIG(pty->exit_code)));
591 #endif
592 from_backend(pty->frontend, 0, message, strlen(message));
593 }
594
595 notify_remote_exit(pty->frontend);
596 }
597
598 return !finished;
599 }
600
601 int pty_select_result(int fd, int event)
602 {
603 int ret = TRUE;
604 Pty pty;
605
606 if (fd == pty_signal_pipe[0]) {
607 pid_t pid;
608 int ipid;
609 int status;
610 char c[1];
611
612 read(pty_signal_pipe[0], c, 1); /* ignore its value; it'll be `x' */
613
614 do {
615 pid = waitpid(-1, &status, WNOHANG);
616
617 ipid = pid;
618 pty = find234(ptys_by_pid, &pid, pty_find_by_pid);
619
620 if (pty)
621 ret = ret && pty_real_select_result(pty, -1, status);
622 } while (pid > 0);
623 } else {
624 pty = find234(ptys_by_fd, &fd, pty_find_by_fd);
625
626 if (pty)
627 ret = ret && pty_real_select_result(pty, event, 0);
628 }
629
630 return ret;
631 }
632
633 static void pty_uxsel_setup(Pty pty)
634 {
635 uxsel_set(pty->master_fd, 1, pty_select_result);
636
637 /*
638 * In principle this only needs calling once for all pty
639 * backend instances, but it's simplest just to call it every
640 * time; uxsel won't mind.
641 */
642 uxsel_set(pty_signal_pipe[0], 1, pty_select_result);
643 }
644
645 /*
646 * Called to set up the pty.
647 *
648 * Returns an error message, or NULL on success.
649 *
650 * Also places the canonical host name into `realhost'. It must be
651 * freed by the caller.
652 */
653 static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
654 char *host, int port, char **realhost, int nodelay,
655 int keepalive)
656 {
657 int slavefd;
658 pid_t pid, pgrp;
659 #ifndef NOT_X_WINDOWS /* for Mac OS X native compilation */
660 long windowid;
661 #endif
662 Pty pty;
663
664 if (single_pty) {
665 pty = single_pty;
666 } else {
667 pty = snew(struct pty_tag);
668 pty->master_fd = pty->slave_fd = -1;
669 #ifndef OMIT_UTMP
670 pty_stamped_utmp = FALSE;
671 #endif
672 }
673
674 pty->frontend = frontend;
675 *backend_handle = NULL; /* we can't sensibly use this, sadly */
676
677 pty->cfg = *cfg; /* structure copy */
678 pty->term_width = cfg->width;
679 pty->term_height = cfg->height;
680
681 if (pty->master_fd < 0)
682 pty_open_master(pty);
683
684 /*
685 * Set the backspace character to be whichever of ^H and ^? is
686 * specified by bksp_is_delete.
687 */
688 {
689 struct termios attrs;
690 tcgetattr(pty->master_fd, &attrs);
691 attrs.c_cc[VERASE] = cfg->bksp_is_delete ? '\177' : '\010';
692 tcsetattr(pty->master_fd, TCSANOW, &attrs);
693 }
694
695 #ifndef OMIT_UTMP
696 /*
697 * Stamp utmp (that is, tell the utmp helper process to do so),
698 * or not.
699 */
700 if (!cfg->stamp_utmp) {
701 close(pty_utmp_helper_pipe); /* just let the child process die */
702 pty_utmp_helper_pipe = -1;
703 } else {
704 char *location = get_x_display(pty->frontend);
705 int len = strlen(location)+1, pos = 0; /* +1 to include NUL */
706 while (pos < len) {
707 int ret = write(pty_utmp_helper_pipe, location+pos, len - pos);
708 if (ret < 0) {
709 perror("pterm: writing to utmp helper process");
710 close(pty_utmp_helper_pipe); /* arrgh, just give up */
711 pty_utmp_helper_pipe = -1;
712 break;
713 }
714 pos += ret;
715 }
716 }
717 #endif
718
719 #ifndef NOT_X_WINDOWS /* for Mac OS X native compilation */
720 windowid = get_windowid(pty->frontend);
721 #endif
722
723 /*
724 * Fork and execute the command.
725 */
726 pid = fork();
727 if (pid < 0) {
728 perror("fork");
729 exit(1);
730 }
731
732 if (pid == 0) {
733 int i;
734 /*
735 * We are the child.
736 */
737
738 slavefd = pty_open_slave(pty);
739 if (slavefd < 0) {
740 perror("slave pty: open");
741 _exit(1);
742 }
743
744 close(pty->master_fd);
745 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
746 dup2(slavefd, 0);
747 dup2(slavefd, 1);
748 dup2(slavefd, 2);
749 setsid();
750 ioctl(slavefd, TIOCSCTTY, 1);
751 pgrp = getpid();
752 tcsetpgrp(slavefd, pgrp);
753 setpgid(pgrp, pgrp);
754 close(open(pty->name, O_WRONLY, 0));
755 setpgid(pgrp, pgrp);
756 /* Close everything _else_, for tidiness. */
757 for (i = 3; i < 1024; i++)
758 close(i);
759 {
760 char term_env_var[10 + sizeof(cfg->termtype)];
761 sprintf(term_env_var, "TERM=%s", cfg->termtype);
762 putenv(term_env_var);
763 }
764 #ifndef NOT_X_WINDOWS /* for Mac OS X native compilation */
765 {
766 char windowid_env_var[40];
767 sprintf(windowid_env_var, "WINDOWID=%ld", windowid);
768 putenv(windowid_env_var);
769 }
770 #endif
771 {
772 char *e = cfg->environmt;
773 char *var, *varend, *val, *varval;
774 while (*e) {
775 var = e;
776 while (*e && *e != '\t') e++;
777 varend = e;
778 if (*e == '\t') e++;
779 val = e;
780 while (*e) e++;
781 e++;
782
783 varval = dupprintf("%.*s=%s", varend-var, var, val);
784 putenv(varval);
785 /*
786 * We must not free varval, since putenv links it
787 * into the environment _in place_. Weird, but
788 * there we go. Memory usage will be rationalised
789 * as soon as we exec anyway.
790 */
791 }
792 }
793
794 /*
795 * SIGINT and SIGQUIT may have been set to ignored by our
796 * parent, particularly by things like sh -c 'pterm &' and
797 * some window managers. SIGCHLD, meanwhile, was blocked
798 * during pt_main() startup. Reverse all this for our child
799 * process.
800 */
801 putty_signal(SIGINT, SIG_DFL);
802 putty_signal(SIGQUIT, SIG_DFL);
803 block_signal(SIGCHLD, 0);
804 if (pty_argv)
805 execvp(pty_argv[0], pty_argv);
806 else {
807 char *shell = getenv("SHELL");
808 char *shellname;
809 if (cfg->login_shell) {
810 char *p = strrchr(shell, '/');
811 shellname = snewn(2+strlen(shell), char);
812 p = p ? p+1 : shell;
813 sprintf(shellname, "-%s", p);
814 } else
815 shellname = shell;
816 execl(getenv("SHELL"), shellname, (void *)NULL);
817 }
818
819 /*
820 * If we're here, exec has gone badly foom.
821 */
822 perror("exec");
823 _exit(127);
824 } else {
825 pty->child_pid = pid;
826 pty->child_dead = FALSE;
827 pty->finished = FALSE;
828 if (pty->slave_fd > 0)
829 close(pty->slave_fd);
830 if (!ptys_by_pid)
831 ptys_by_pid = newtree234(pty_compare_by_pid);
832 add234(ptys_by_pid, pty);
833 }
834
835 if (pty_signal_pipe[0] < 0 && pipe(pty_signal_pipe) < 0) {
836 perror("pipe");
837 exit(1);
838 }
839 pty_uxsel_setup(pty);
840
841 *backend_handle = pty;
842
843 return NULL;
844 }
845
846 static void pty_reconfig(void *handle, Config *cfg)
847 {
848 Pty pty = (Pty)handle;
849 /*
850 * We don't have much need to reconfigure this backend, but
851 * unfortunately we do need to pick up the setting of Close On
852 * Exit so we know whether to give a `terminated' message.
853 */
854 pty->cfg = *cfg; /* structure copy */
855 }
856
857 /*
858 * Stub routine (never called in pterm).
859 */
860 static void pty_free(void *handle)
861 {
862 Pty pty = (Pty)handle;
863
864 /* Either of these may fail `not found'. That's fine with us. */
865 del234(ptys_by_pid, pty);
866 del234(ptys_by_fd, pty);
867
868 sfree(pty);
869 }
870
871 /*
872 * Called to send data down the pty.
873 */
874 static int pty_send(void *handle, char *buf, int len)
875 {
876 Pty pty = (Pty)handle;
877
878 if (pty->master_fd < 0)
879 return 0; /* ignore all writes if fd closed */
880
881 while (len > 0) {
882 int ret = write(pty->master_fd, buf, len);
883 if (ret < 0) {
884 perror("write pty master");
885 exit(1);
886 }
887 buf += ret;
888 len -= ret;
889 }
890 return 0;
891 }
892
893 static void pty_close(Pty pty)
894 {
895 if (pty->master_fd >= 0) {
896 close(pty->master_fd);
897 pty->master_fd = -1;
898 }
899 #ifndef OMIT_UTMP
900 if (pty_utmp_helper_pipe >= 0) {
901 close(pty_utmp_helper_pipe); /* this causes utmp to be cleaned up */
902 pty_utmp_helper_pipe = -1;
903 }
904 #endif
905 }
906
907 /*
908 * Called to query the current socket sendability status.
909 */
910 static int pty_sendbuffer(void *handle)
911 {
912 /* Pty pty = (Pty)handle; */
913 return 0;
914 }
915
916 /*
917 * Called to set the size of the window
918 */
919 static void pty_size(void *handle, int width, int height)
920 {
921 Pty pty = (Pty)handle;
922 struct winsize size;
923
924 pty->term_width = width;
925 pty->term_height = height;
926
927 size.ws_row = (unsigned short)pty->term_height;
928 size.ws_col = (unsigned short)pty->term_width;
929 size.ws_xpixel = (unsigned short) pty->term_width *
930 font_dimension(pty->frontend, 0);
931 size.ws_ypixel = (unsigned short) pty->term_height *
932 font_dimension(pty->frontend, 1);
933 ioctl(pty->master_fd, TIOCSWINSZ, (void *)&size);
934 return;
935 }
936
937 /*
938 * Send special codes.
939 */
940 static void pty_special(void *handle, Telnet_Special code)
941 {
942 /* Pty pty = (Pty)handle; */
943 /* Do nothing! */
944 return;
945 }
946
947 /*
948 * Return a list of the special codes that make sense in this
949 * protocol.
950 */
951 static const struct telnet_special *pty_get_specials(void *handle)
952 {
953 /* Pty pty = (Pty)handle; */
954 /*
955 * Hmm. When I get round to having this actually usable, it
956 * might be quite nice to have the ability to deliver a few
957 * well chosen signals to the child process - SIGINT, SIGTERM,
958 * SIGKILL at least.
959 */
960 return NULL;
961 }
962
963 static Socket pty_socket(void *handle)
964 {
965 /* Pty pty = (Pty)handle; */
966 return NULL; /* shouldn't ever be needed */
967 }
968
969 static int pty_sendok(void *handle)
970 {
971 /* Pty pty = (Pty)handle; */
972 return 1;
973 }
974
975 static void pty_unthrottle(void *handle, int backlog)
976 {
977 /* Pty pty = (Pty)handle; */
978 /* do nothing */
979 }
980
981 static int pty_ldisc(void *handle, int option)
982 {
983 /* Pty pty = (Pty)handle; */
984 return 0; /* neither editing nor echoing */
985 }
986
987 static void pty_provide_ldisc(void *handle, void *ldisc)
988 {
989 /* Pty pty = (Pty)handle; */
990 /* This is a stub. */
991 }
992
993 static void pty_provide_logctx(void *handle, void *logctx)
994 {
995 /* Pty pty = (Pty)handle; */
996 /* This is a stub. */
997 }
998
999 static int pty_exitcode(void *handle)
1000 {
1001 Pty pty = (Pty)handle;
1002 if (!pty->finished)
1003 return -1; /* not dead yet */
1004 else
1005 return pty->exit_code;
1006 }
1007
1008 static int pty_cfg_info(void *handle)
1009 {
1010 /* Pty pty = (Pty)handle; */
1011 return 0;
1012 }
1013
1014 Backend pty_backend = {
1015 pty_init,
1016 pty_free,
1017 pty_reconfig,
1018 pty_send,
1019 pty_sendbuffer,
1020 pty_size,
1021 pty_special,
1022 pty_get_specials,
1023 pty_socket,
1024 pty_exitcode,
1025 pty_sendok,
1026 pty_ldisc,
1027 pty_provide_ldisc,
1028 pty_provide_logctx,
1029 pty_unthrottle,
1030 pty_cfg_info,
1031 1
1032 };