#ifdef out expensive combining-character/scrollback diagnostics (for now).
[u/mdw/putty] / unix / uxpty.c
CommitLineData
28d00fe4 1/*
2 * Pseudo-tty backend for pterm.
28d00fe4 3 */
4
140745a8 5#define _XOPEN_SOURCE 600
88e6b9ca 6#define _XOPEN_SOURCE_EXTENDED
74aca06d 7#define _GNU_SOURCE
054d8535 8
1709795f 9#include <stdio.h>
10#include <stdlib.h>
054d8535 11#include <string.h>
12#include <unistd.h>
a0e16eb1 13#include <signal.h>
054d8535 14#include <fcntl.h>
88e6b9ca 15#include <termios.h>
be4f86de 16#include <grp.h>
755a6d84 17#include <utmp.h>
18#include <pwd.h>
19#include <time.h>
a0e16eb1 20#include <sys/types.h>
d4e1d591 21#include <sys/stat.h>
a0e16eb1 22#include <sys/wait.h>
88e6b9ca 23#include <sys/ioctl.h>
0f33f9d1 24#include <errno.h>
1709795f 25
26#include "putty.h"
d6fa1947 27#include "tree234.h"
1709795f 28
29#ifndef FALSE
30#define FALSE 0
31#endif
32#ifndef TRUE
33#define TRUE 1
34#endif
35
755a6d84 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
f51dc031 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
d6fa1947 64typedef 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 */
70static int pty_signal_pipe[2] = { -1, -1 }; /* obviously bogus initial val */
71
72struct 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 */
88static 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
100static 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}
054d8535 111
d6fa1947 112static 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 */
119static 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
131static 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
143static 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 */
160static Pty single_pty = NULL;
74aca06d 161
d4e1d591 162#ifndef OMIT_UTMP
163static int pty_utmp_helper_pid, pty_utmp_helper_pipe;
d6fa1947 164static int pty_stamped_utmp;
d4e1d591 165static struct utmp utmp_entry;
d6fa1947 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 */
173char **pty_argv;
d6fa1947 174
175static void pty_close(Pty pty);
d4e1d591 176
d6fa1947 177#ifndef OMIT_UTMP
95c47834 178static void setup_utmp(char *ttyname, char *location)
755a6d84 179{
755a6d84 180#ifdef HAVE_LASTLOG
181 struct lastlog lastlog_entry;
182 FILE *lastlog;
183#endif
184 struct passwd *pw;
755a6d84 185 FILE *wtmp;
4764563f 186 time_t uttime;
755a6d84 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));
4764563f 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 */
755a6d84 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
f51dc031 225 pty_stamped_utmp = 1;
226
755a6d84 227}
228
229static void cleanup_utmp(void)
230{
755a6d84 231 FILE *wtmp;
4764563f 232 time_t uttime;
755a6d84 233
95c47834 234 if (!pty_stamped_utmp)
f51dc031 235 return;
236
755a6d84 237 utmp_entry.ut_type = DEAD_PROCESS;
238 memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
4764563f 239 time(&uttime);
240 utmp_entry.ut_time = uttime;
755a6d84 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
f51dc031 257 pty_stamped_utmp = 0; /* ensure we never double-cleanup */
755a6d84 258}
d4e1d591 259#endif
755a6d84 260
a0e16eb1 261static void sigchld_handler(int signum)
262{
74aca06d 263 write(pty_signal_pipe[1], "x", 1);
a0e16eb1 264}
265
d4e1d591 266#ifndef OMIT_UTMP
755a6d84 267static void fatal_sig_handler(int signum)
268{
0f33f9d1 269 putty_signal(signum, SIG_DFL);
755a6d84 270 cleanup_utmp();
271 setuid(getuid());
272 raise(signum);
273}
d4e1d591 274#endif
755a6d84 275
d6fa1947 276static int pty_open_slave(Pty pty)
270d2e8d 277{
d6fa1947 278 if (pty->slave_fd < 0)
279 pty->slave_fd = open(pty->name, O_RDWR);
270d2e8d 280
d6fa1947 281 return pty->slave_fd;
270d2e8d 282}
283
d6fa1947 284static void pty_open_master(Pty pty)
1709795f 285{
be4f86de 286#ifdef BSD_PTYS
95c47834 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);
d6fa1947 296 pty->master_fd = open(master_name, O_RDWR);
297 if (pty->master_fd >= 0) {
95c47834 298 if (geteuid() == 0 ||
270d2e8d 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 */
d6fa1947 307 strcpy(pty->name, master_name);
308 pty->name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
270d2e8d 309
d6fa1947 310 if (pty_open_slave(pty) >= 0 &&
311 access(pty->name, R_OK | W_OK) == 0)
270d2e8d 312 goto got_one;
d6fa1947 313 if (pty->slave_fd > 0)
314 close(pty->slave_fd);
315 pty->slave_fd = -1;
270d2e8d 316 }
d6fa1947 317 close(pty->master_fd);
be4f86de 318 }
95c47834 319 }
be4f86de 320
95c47834 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);
be4f86de 324
95c47834 325 got_one:
95c47834 326
327 /* We need to chown/chmod the /dev/ttyXX device. */
328 gp = getgrnam("tty");
d6fa1947 329 chown(pty->name, getuid(), gp ? gp->gr_gid : -1);
330 chmod(pty->name, 0600);
be4f86de 331#else
d6fa1947 332 pty->master_fd = open("/dev/ptmx", O_RDWR);
054d8535 333
d6fa1947 334 if (pty->master_fd < 0) {
054d8535 335 perror("/dev/ptmx: open");
336 exit(1);
337 }
338
d6fa1947 339 if (grantpt(pty->master_fd) < 0) {
054d8535 340 perror("grantpt");
341 exit(1);
342 }
343
d6fa1947 344 if (unlockpt(pty->master_fd) < 0) {
054d8535 345 perror("unlockpt");
346 exit(1);
347 }
348
d6fa1947 349 pty->name[FILENAME_MAX-1] = '\0';
350 strncpy(pty->name, ptsname(pty->master_fd), FILENAME_MAX-1);
be4f86de 351#endif
d6fa1947 352
353 if (!ptys_by_fd)
354 ptys_by_fd = newtree234(pty_compare_by_fd);
355 add234(ptys_by_fd, pty);
95c47834 356}
054d8535 357
95c47834 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 */
371void pty_pre_init(void)
372{
d6fa1947 373 Pty pty;
374
d4e1d591 375#ifndef OMIT_UTMP
95c47834 376 pid_t pid;
377 int pipefd[2];
d4e1d591 378#endif
95c47834 379
d6fa1947 380 pty = single_pty = snew(struct pty_tag);
381
0f33f9d1 382 /* set the child signal handler straight away; it needs to be set
383 * before we ever fork. */
384 putty_signal(SIGCHLD, sigchld_handler);
d6fa1947 385 pty->master_fd = pty->slave_fd = -1;
386#ifndef OMIT_UTMP
387 pty_stamped_utmp = FALSE;
388#endif
95c47834 389
390 if (geteuid() != getuid() || getegid() != getgid()) {
d6fa1947 391 pty_open_master(pty);
39baeaa4 392 }
393
95c47834 394#ifndef OMIT_UTMP
39baeaa4 395 /*
95c47834 396 * Fork off the utmp helper.
755a6d84 397 */
95c47834 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();
0f33f9d1 423 _exit(0);
95c47834 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 */
0f33f9d1 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);
755a6d84 453#ifdef SIGBUS
0f33f9d1 454 putty_signal(SIGBUS, fatal_sig_handler);
755a6d84 455#endif
456#ifdef SIGPOLL
0f33f9d1 457 putty_signal(SIGPOLL, fatal_sig_handler);
755a6d84 458#endif
459#ifdef SIGPROF
0f33f9d1 460 putty_signal(SIGPROF, fatal_sig_handler);
755a6d84 461#endif
462#ifdef SIGSYS
0f33f9d1 463 putty_signal(SIGSYS, fatal_sig_handler);
755a6d84 464#endif
465#ifdef SIGTRAP
0f33f9d1 466 putty_signal(SIGTRAP, fatal_sig_handler);
755a6d84 467#endif
468#ifdef SIGVTALRM
0f33f9d1 469 putty_signal(SIGVTALRM, fatal_sig_handler);
755a6d84 470#endif
471#ifdef SIGXCPU
0f33f9d1 472 putty_signal(SIGXCPU, fatal_sig_handler);
755a6d84 473#endif
474#ifdef SIGXFSZ
0f33f9d1 475 putty_signal(SIGXFSZ, fatal_sig_handler);
755a6d84 476#endif
477#ifdef SIGIO
0f33f9d1 478 putty_signal(SIGIO, fatal_sig_handler);
95c47834 479#endif
d6fa1947 480 setup_utmp(pty->name, display);
95c47834 481 }
482 }
483 }
484 } else {
485 close(pipefd[0]);
486 pty_utmp_helper_pid = pid;
487 pty_utmp_helper_pipe = pipefd[1];
488 }
755a6d84 489#endif
95c47834 490
491 /* Drop privs. */
492 {
95c47834 493#ifndef HAVE_NO_SETRESUID
d4e1d591 494 int gid = getgid(), uid = getuid();
95c47834 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
d6fa1947 506int pty_real_select_result(Pty pty, int event, int status)
74aca06d 507{
508 char buf[4096];
509 int ret;
510 int finished = FALSE;
511
d6fa1947 512 if (event < 0) {
74aca06d 513 /*
d6fa1947 514 * We've been called because our child process did
515 * something. `status' tells us what.
74aca06d 516 */
d6fa1947 517 if ((WIFEXITED(status) || WIFSIGNALED(status))) {
74aca06d 518 /*
d6fa1947 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.
74aca06d 527 */
d6fa1947 528 pty->exit_code = status;
529 pty->child_dead = TRUE;
530 del234(ptys_by_pid, pty);
74aca06d 531 finished = TRUE;
74aca06d 532 }
d6fa1947 533 } else {
534 if (event == 1) {
74aca06d 535
d6fa1947 536 ret = read(pty->master_fd, buf, sizeof(buf));
74aca06d 537
d6fa1947 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)) {
74aca06d 544 /*
d6fa1947 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!
74aca06d 551 */
74aca06d 552 finished = TRUE;
d6fa1947 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);
74aca06d 560 }
d6fa1947 561 }
74aca06d 562 }
563
d6fa1947 564 if (finished && !pty->finished) {
565 uxsel_del(pty->master_fd);
566 pty_close(pty);
567 pty->master_fd = -1;
74aca06d 568
d6fa1947 569 pty->finished = TRUE;
74aca06d 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 */
d6fa1947 577 if (pty->cfg.close_on_exit == FORCE_OFF ||
578 (pty->cfg.close_on_exit == AUTO && pty->exit_code != 0)) {
74aca06d 579 char message[512];
d6fa1947 580 if (WIFEXITED(pty->exit_code))
74aca06d 581 sprintf(message, "\r\n[pterm: process terminated with exit"
d6fa1947 582 " code %d]\r\n", WEXITSTATUS(pty->exit_code));
583 else if (WIFSIGNALED(pty->exit_code))
74aca06d 584#ifdef HAVE_NO_STRSIGNAL
585 sprintf(message, "\r\n[pterm: process terminated on signal"
d6fa1947 586 " %d]\r\n", WTERMSIG(pty->exit_code));
74aca06d 587#else
588 sprintf(message, "\r\n[pterm: process terminated on signal"
d6fa1947 589 " %d (%.400s)]\r\n", WTERMSIG(pty->exit_code),
590 strsignal(WTERMSIG(pty->exit_code)));
74aca06d 591#endif
d6fa1947 592 from_backend(pty->frontend, 0, message, strlen(message));
74aca06d 593 }
39934deb 594
d6fa1947 595 notify_remote_exit(pty->frontend);
74aca06d 596 }
d6fa1947 597
74aca06d 598 return !finished;
599}
600
d6fa1947 601int pty_select_result(int fd, int event)
74aca06d 602{
d6fa1947 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
633static 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 */
74aca06d 642 uxsel_set(pty_signal_pipe[0], 1, pty_select_result);
643}
644
95c47834 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 */
cbe2d68f 653static const char *pty_init(void *frontend, void **backend_handle, Config *cfg,
79bf227b 654 char *host, int port, char **realhost, int nodelay,
655 int keepalive)
95c47834 656{
657 int slavefd;
658 pid_t pid, pgrp;
c13772b1 659#ifndef NOT_X_WINDOWS /* for Mac OS X native compilation */
7af753e6 660 long windowid;
c13772b1 661#endif
d6fa1947 662 Pty pty;
95c47834 663
d6fa1947 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;
28d00fe4 675 *backend_handle = NULL; /* we can't sensibly use this, sadly */
676
d6fa1947 677 pty->cfg = *cfg; /* structure copy */
678 pty->term_width = cfg->width;
679 pty->term_height = cfg->height;
f278d6f8 680
d6fa1947 681 if (pty->master_fd < 0)
682 pty_open_master(pty);
95c47834 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;
d6fa1947 690 tcgetattr(pty->master_fd, &attrs);
86916870 691 attrs.c_cc[VERASE] = cfg->bksp_is_delete ? '\177' : '\010';
d6fa1947 692 tcsetattr(pty->master_fd, TCSANOW, &attrs);
95c47834 693 }
694
d4e1d591 695#ifndef OMIT_UTMP
95c47834 696 /*
697 * Stamp utmp (that is, tell the utmp helper process to do so),
698 * or not.
699 */
58dcb876 700 if (!cfg->stamp_utmp) {
95c47834 701 close(pty_utmp_helper_pipe); /* just let the child process die */
58dcb876 702 pty_utmp_helper_pipe = -1;
703 } else {
d6fa1947 704 char *location = get_x_display(pty->frontend);
95c47834 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 */
58dcb876 711 pty_utmp_helper_pipe = -1;
95c47834 712 break;
713 }
714 pos += ret;
715 }
716 }
d4e1d591 717#endif
755a6d84 718
c13772b1 719#ifndef NOT_X_WINDOWS /* for Mac OS X native compilation */
d6fa1947 720 windowid = get_windowid(pty->frontend);
c13772b1 721#endif
7af753e6 722
755a6d84 723 /*
054d8535 724 * Fork and execute the command.
725 */
726 pid = fork();
727 if (pid < 0) {
728 perror("fork");
88e6b9ca 729 exit(1);
054d8535 730 }
731
732 if (pid == 0) {
733 int i;
734 /*
735 * We are the child.
736 */
d37c2d81 737
d6fa1947 738 slavefd = pty_open_slave(pty);
d37c2d81 739 if (slavefd < 0) {
740 perror("slave pty: open");
0f33f9d1 741 _exit(1);
d37c2d81 742 }
743
d6fa1947 744 close(pty->master_fd);
054d8535 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();
d37c2d81 750 ioctl(slavefd, TIOCSCTTY, 1);
751 pgrp = getpid();
752 tcsetpgrp(slavefd, pgrp);
ea0716c6 753 setpgid(pgrp, pgrp);
d6fa1947 754 close(open(pty->name, O_WRONLY, 0));
ea0716c6 755 setpgid(pgrp, pgrp);
054d8535 756 /* Close everything _else_, for tidiness. */
757 for (i = 3; i < 1024; i++)
758 close(i);
fe9548aa 759 {
86916870 760 char term_env_var[10 + sizeof(cfg->termtype)];
761 sprintf(term_env_var, "TERM=%s", cfg->termtype);
fe9548aa 762 putenv(term_env_var);
763 }
c13772b1 764#ifndef NOT_X_WINDOWS /* for Mac OS X native compilation */
7af753e6 765 {
766 char windowid_env_var[40];
767 sprintf(windowid_env_var, "WINDOWID=%ld", windowid);
768 putenv(windowid_env_var);
769 }
c13772b1 770#endif
73feed4f 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
8bf5c419 794 /*
795 * SIGINT and SIGQUIT may have been set to ignored by our
796 * parent, particularly by things like sh -c 'pterm &' and
15b5d988 797 * some window managers. SIGCHLD, meanwhile, was blocked
798 * during pt_main() startup. Reverse all this for our child
799 * process.
8bf5c419 800 */
0f33f9d1 801 putty_signal(SIGINT, SIG_DFL);
802 putty_signal(SIGQUIT, SIG_DFL);
15b5d988 803 block_signal(SIGCHLD, 0);
6169c758 804 if (pty_argv)
805 execvp(pty_argv[0], pty_argv);
c8ee61b9 806 else {
807 char *shell = getenv("SHELL");
808 char *shellname;
86916870 809 if (cfg->login_shell) {
c8ee61b9 810 char *p = strrchr(shell, '/');
3d88e64d 811 shellname = snewn(2+strlen(shell), char);
c8ee61b9 812 p = p ? p+1 : shell;
813 sprintf(shellname, "-%s", p);
814 } else
815 shellname = shell;
816 execl(getenv("SHELL"), shellname, NULL);
817 }
818
054d8535 819 /*
820 * If we're here, exec has gone badly foom.
821 */
822 perror("exec");
0f33f9d1 823 _exit(127);
054d8535 824 } else {
d6fa1947 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);
270d2e8d 833 }
054d8535 834
d6fa1947 835 if (pty_signal_pipe[0] < 0 && pipe(pty_signal_pipe) < 0) {
74aca06d 836 perror("pipe");
837 exit(1);
838 }
d6fa1947 839 pty_uxsel_setup(pty);
840
841 *backend_handle = pty;
74aca06d 842
1709795f 843 return NULL;
844}
845
86916870 846static void pty_reconfig(void *handle, Config *cfg)
847{
d6fa1947 848 Pty pty = (Pty)handle;
6b0eeb4e 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 */
d6fa1947 854 pty->cfg = *cfg; /* structure copy */
86916870 855}
856
857/*
6b0eeb4e 858 * Stub routine (never called in pterm).
cb60dcb3 859 */
860static void pty_free(void *handle)
861{
d6fa1947 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);
cb60dcb3 869}
870
cb60dcb3 871/*
1709795f 872 * Called to send data down the pty.
873 */
28d00fe4 874static int pty_send(void *handle, char *buf, int len)
1709795f 875{
d6fa1947 876 Pty pty = (Pty)handle;
877
878 if (pty->master_fd < 0)
90cfd8f4 879 return 0; /* ignore all writes if fd closed */
880
054d8535 881 while (len > 0) {
d6fa1947 882 int ret = write(pty->master_fd, buf, len);
054d8535 883 if (ret < 0) {
884 perror("write pty master");
885 exit(1);
886 }
887 buf += ret;
888 len -= ret;
889 }
1709795f 890 return 0;
891}
892
d6fa1947 893static void pty_close(Pty pty)
90cfd8f4 894{
d6fa1947 895 if (pty->master_fd >= 0) {
896 close(pty->master_fd);
897 pty->master_fd = -1;
90cfd8f4 898 }
d4e1d591 899#ifndef OMIT_UTMP
58dcb876 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 }
d4e1d591 904#endif
90cfd8f4 905}
906
1709795f 907/*
908 * Called to query the current socket sendability status.
909 */
28d00fe4 910static int pty_sendbuffer(void *handle)
1709795f 911{
d6fa1947 912 /* Pty pty = (Pty)handle; */
1709795f 913 return 0;
914}
915
916/*
917 * Called to set the size of the window
918 */
28d00fe4 919static void pty_size(void *handle, int width, int height)
1709795f 920{
d6fa1947 921 Pty pty = (Pty)handle;
88e6b9ca 922 struct winsize size;
923
d6fa1947 924 pty->term_width = width;
925 pty->term_height = height;
f278d6f8 926
d6fa1947 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);
1709795f 934 return;
935}
936
937/*
938 * Send special codes.
939 */
28d00fe4 940static void pty_special(void *handle, Telnet_Special code)
1709795f 941{
d6fa1947 942 /* Pty pty = (Pty)handle; */
1709795f 943 /* Do nothing! */
944 return;
945}
946
125105d1 947/*
948 * Return a list of the special codes that make sense in this
949 * protocol.
950 */
951static const struct telnet_special *pty_get_specials(void *handle)
952{
d6fa1947 953 /* Pty pty = (Pty)handle; */
125105d1 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
28d00fe4 963static Socket pty_socket(void *handle)
1709795f 964{
d6fa1947 965 /* Pty pty = (Pty)handle; */
1709795f 966 return NULL; /* shouldn't ever be needed */
967}
968
28d00fe4 969static int pty_sendok(void *handle)
1709795f 970{
d6fa1947 971 /* Pty pty = (Pty)handle; */
1709795f 972 return 1;
973}
974
28d00fe4 975static void pty_unthrottle(void *handle, int backlog)
1709795f 976{
d6fa1947 977 /* Pty pty = (Pty)handle; */
1709795f 978 /* do nothing */
979}
980
28d00fe4 981static int pty_ldisc(void *handle, int option)
1709795f 982{
d6fa1947 983 /* Pty pty = (Pty)handle; */
1709795f 984 return 0; /* neither editing nor echoing */
985}
986
b9d7bcad 987static void pty_provide_ldisc(void *handle, void *ldisc)
988{
d6fa1947 989 /* Pty pty = (Pty)handle; */
b9d7bcad 990 /* This is a stub. */
991}
992
a8327734 993static void pty_provide_logctx(void *handle, void *logctx)
994{
d6fa1947 995 /* Pty pty = (Pty)handle; */
a8327734 996 /* This is a stub. */
997}
998
28d00fe4 999static int pty_exitcode(void *handle)
1709795f 1000{
d6fa1947 1001 Pty pty = (Pty)handle;
1002 if (!pty->finished)
90cfd8f4 1003 return -1; /* not dead yet */
1004 else
d6fa1947 1005 return pty->exit_code;
1709795f 1006}
1007
f89c3294 1008static int pty_cfg_info(void *handle)
1009{
d6fa1947 1010 /* Pty pty = (Pty)handle; */
f89c3294 1011 return 0;
1012}
1013
1709795f 1014Backend pty_backend = {
1015 pty_init,
cb60dcb3 1016 pty_free,
86916870 1017 pty_reconfig,
1709795f 1018 pty_send,
1019 pty_sendbuffer,
1020 pty_size,
1021 pty_special,
125105d1 1022 pty_get_specials,
1709795f 1023 pty_socket,
1024 pty_exitcode,
1025 pty_sendok,
1026 pty_ldisc,
b9d7bcad 1027 pty_provide_ldisc,
a8327734 1028 pty_provide_logctx,
1709795f 1029 pty_unthrottle,
f89c3294 1030 pty_cfg_info,
1709795f 1031 1
1032};