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