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