-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckupty.c
1988 lines (1764 loc) · 49.4 KB
/
ckupty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
char *ckptyv = "Pseudoterminal support, 9.0.104, 18 Sep 2020";
/* C K U P T Y -- C-Kermit pseudoterminal control functions for UNIX */
/* Last update: Sat Sep 19 15:25:13 2020 */
/*
Copyright 1995 by the Massachusetts Institute of Technology.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation, and that the name of M.I.T. not be used in advertising or
publicity pertaining to distribution of the software without specific,
written prior permission. Furthermore if you modify this software you must
label your software as modified software and not distribute it in such a
fashion that it might be confused with the original M.I.T. software.
M.I.T. makes no representations about the suitability of this software for
any purpose. It is provided "as is" without express or implied warranty.
Modified for use in C-Kermit, and new material added, by:
Jeffrey Altman <jaltman@secure-endpoints.com>
Secure Endpoints Inc., New York City
November 1999
Parameterized for pty file descriptor and function code,
Dec 2006 - Sep 2009, plus some minor "compliance" nits addressed in 2020.
See "HAVE_OPENPTY" section of ckcdeb.h.
Frank da Cruz, The Kermit Project, New York City
*/
/*
Built and tested successully on:
. 4.4BSD, including BSDI/OS, NetBSD, FreeBSD, OpenBSD, Mac OS X
. AIX 4.1 and later
. DG/UX 5.4R4.11
. Digital UNIX 3.2 and 4.0
. HP-UX 9.00 and later
. IRIX 6.0 and later
. Linux
. Mac OS X 10.4
. NeXTSTEP 3.x
. OpenBSD
. QNX 4.25 (except PTY process termination not detected)
. SCO OSR5.0.5
. SCO Unixware 7
. SINIX 5.42
. Solaris 2.x and 7
. SunOS 4.1.3
Failures include:
. SCO UNIX 3.2v4.2 (compile fails with syntax error in <memory.h>)
. HP-UX 8.00 and earlier (no vhangup or ptsname routines)
*/
#ifndef __FreeBSD__ /* bs 20151224 */
#define _XOPEN_SOURCE 500 /* mdw 20140223 */
#endif /* __FreeBSD__ */ /* bs 20151224 */
#include <stdlib.h> /* mdw 20140223 */
#include "ckcsym.h"
#include "ckcdeb.h" /* To pick up NETPTY definition */
#ifndef NETPTY /* Selector for PTY support */
char * ptyver = "No PTY support";
#else /* (rest of this module...) */
char * ptyver = "PTY support 8.0.017, 18 Sep 2020";
/* These will no doubt need adjustment... */
#ifndef NEXT
#define HAVE_SETSID
#endif /* NEXT */
#define HAVE_KILLPG
#define HAVE_TTYNAME
#define HAVE_WAITPID
#ifdef SUNOS41
#define BSD44ORPOSIX
#endif /* SUNOS41 */
#ifndef USE_TERMIO
#ifdef LINUX
#define USE_TERMIO
#else
#ifdef ATTSV
#define USE_TERMIO
#else
#ifdef HPUX
#define USE_TERMIO
#else
#ifdef AIX
#define USE_TERMIO
#else
#ifdef BSD44ORPOSIX
#define USE_TERMIO
#else
#ifdef IRIX60
#define USE_TERMIO
#else
#ifdef QNX
#define USE_TERMIO
#endif /* QNX */
#endif /* IRIX60 */
#endif /* BSD44ORPOSIX */
#endif /* AIX */
#endif /* HPUX */
#endif /* ATTSV */
#endif /* LINUX */
#endif /* USE_TERMIO */
#ifdef QNX
#include <fcntl.h>
#endif /* QNX */
#ifdef USE_TERMIO
#define POSIX_TERMIOS /* Seems to be a misnomer */
#endif /* USE_TERMIO */
#ifdef NEXT
#ifndef GETPGRP_ONEARG
#define GETPGRP_ONEARG
#endif /* GETPGRP_ONEARG */
#endif /* NEXT */
#ifdef WANT_UTMP /* See ckupty.h */
/*
WANT_UTMP is not defined because (a) the utmp/wtmp junk is the most
nonportable part of this module, and (b) we're not logging anybody
in, we're just running a process, and don't need to write utmp/wtmp records.
*/
#ifndef HAVE_SETUTXENT /* Who has <utmpx.h> */
#ifdef SOLARIS
#define HAVE_SETUTXENT
#else
#ifdef IRIX60
#define HAVE_SETUTXENT
#else
#ifdef CK_SCOV5
#define HAVE_SETUTXENT
#else
#ifdef HPUX10
#define HAVE_SETUTXENT
#else
#ifdef UNIXWARE
#define HAVE_SETUTXENT
#else
#ifdef IRIX60
#define HAVE_SETUTXENT
#endif /* IRIX60 */
#endif /* UNIXWARE */
#endif /* HPUX10 */
#endif /* CK_SCOV5 */
#endif /* IRIX60 */
#endif /* SOLARIS */
#endif /* HAVE_SETUTXENT */
#ifndef HAVE_UTHOST /* Does utmp include ut_host[]? */
#ifdef HAVE_SETUTXENT /* utmpx always does */
#define HAVE_UTHOST
#else
#ifdef LINUX /* Linux does */
#define HAVE_UTHOST
#else
#ifdef SUNOS4 /* SunOS does */
#define HAVE_UTHOST
#else
#ifdef AIX41 /* AIX 4.1 and later do */
#define HAVE_UTHOST
#endif /* AIX41 */
#endif /* SUNOS4 */
#endif /* LINUX */
#endif /* HAVE_SETUTXENT */
#endif /* HAVE_UTHOST */
#ifndef HAVE_UT_HOST
#ifndef NO_UT_HOST
#define NO_UT_HOST
#endif /* NO_UT_HOST */
#endif /* HAVE_UT_HOST */
#endif /* WANT_UTMP */
#ifdef LINUX
#define CK_VHANGUP
#define HAVE_SYS_SELECT_H
#define HAVE_GETUTENT
#define HAVE_SETUTENT
#define HAVE_UPDWTMP
#endif /* LINUX */
#ifdef HPUX10
#define CK_VHANGUP
#define VHANG_FIRST
#define HAVE_PTSNAME
#ifndef HAVE_PTYTRAP
#define HAVE_PTYTRAP
#endif /* HAVE_PTYTRAP */
#else
#ifdef HPUX9
#define CK_VHANGUP
#define VHANG_FIRST
#define HAVE_PTSNAME
#ifndef HAVE_PTYTRAP
#define HAVE_PTYTRAP
#endif /* HAVE_PTYTRAP */
#endif /* HPUX9 */
#endif /* HPUX10 */
#ifdef SUNOS4
#define CK_VHANGUP
#define NO_UT_PID
#define VHANG_FIRST
#endif /* SUNOS4 */
#ifdef IRIX60
#define CK_VHANGUP
#define HAVE__GETPTY
#endif /* IRIX60 */
#ifdef SINIX
#define HAVE_STREAMS
#define HAVE_GRANTPT
#define HAVE_PTSNAME
#define PUSH_PTEM
#define PUSH_LDTERM
#define PUSH_TTCOMPAT
#endif /* SINIX */
#ifdef ultrix
#define MUST_SETPGRP
#endif /* ultrix */
#ifdef QNX
#define MUST_SETPGRP
#define NO_DEVTTY
#define INIT_SPTY
#endif /* QNX */
#ifdef LINUX
#ifdef HAVE_PTMX
#define HAVE_GRANTPT
#define HAVE_PTSNAME
#endif /* HAVE_PTMX */
#else
#ifdef HAVE_STREAMS
#define HAVE_PTMX
#endif /* HAVE_STREAMS */
#endif /* LINUX */
#include "ckupty.h"
#ifdef PTYNOBLOCK
#ifndef O_NDELAY
#ifdef O_NONBLOCK
#define O_NDELAY O_NONBLOCK
#endif /* O_NONBLOCK */
#endif /* O_NDELAY */
#else /* PTYNOBLOCK */
#ifdef O_NDELAY
#undef O_NDELAY
#endif /* O_NDELAY */
#define O_NDELAY 0
#endif /* PTYNOBLOCK */
#ifndef ONLCR
#define ONLCR 0
#endif /* ONLCR */
#ifdef CK_WAIT_H
#include <sys/wait.h>
#endif /* CK_WAIT_H */
#ifdef STREAMSPTY
#ifndef INIT_SPTY
#define INIT_SPTY
#endif /* INIT_SPTY */
#include <sys/stream.h>
#include <stropts.h>
#include <termio.h>
/* Make sure we don't get the BSD version */
#ifdef HAVE_SYS_TTY_H
#include "/usr/include/sys/tty.h"
#endif /* HAVE_SYS_TTY_H */
#ifdef HAS_PTYVAR /* Where is this set? */
#include <sys/ptyvar.h>
#else /* HAS_PTYVAR */
#ifndef TIOCPKT_FLUSHWRITE
#define TIOCPKT_FLUSHWRITE 0x02
#define TIOCPKT_NOSTOP 0x10
#define TIOCPKT_DOSTOP 0x20
#define TIOCPKT_IOCTL 0x40
#endif /* TIOCPKT_FLUSHWRITE */
#endif /* HAS_PTYVAR */
#ifdef HAVE_TTY_H
#include <tty.h>
#endif /* HAVE_TTY_H */
/*
Because of the way ptyibuf is used with streams messages, we need
ptyibuf+1 to be on a full-word boundary. The following weirdness
is simply to make that happen.
*/
long ptyibufbuf[BUFSIZ/sizeof(long)+1];
char *ptyibuf = ((char *)&ptyibufbuf[1])-1;
char *ptyip = ((char *)&ptyibufbuf[1])-1;
char ptyibuf2[BUFSIZ];
unsigned char ctlbuf[BUFSIZ];
struct strbuf strbufc, strbufd;
int readstream();
#else /* ! STREAMSPTY */
/* I/O data buffers, pointers, and counters. */
char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
char ptyibuf2[BUFSIZ];
#endif /* ! STREAMSPTY */
#ifndef USE_TERMIO
struct termbuf {
struct sgttyb sg;
struct tchars tc;
struct ltchars ltc;
int state;
int lflags;
} termbuf, termbuf2;
#define cfsetospeed(tp,val) (tp)->sg.sg_ospeed = (val)
#define cfsetispeed(tp,val) (tp)->sg.sg_ispeed = (val)
#define cfgetospeed(tp) (tp)->sg.sg_ospeed
#define cfgetispeed(tp) (tp)->sg.sg_ispeed
#else /* USE_TERMIO */
#ifdef SYSV_TERMIO
#define termios termio
#endif /* SYSV_TERMIO */
#ifndef TCSANOW
#ifdef TCSETS
#define TCSANOW TCSETS
#define TCSADRAIN TCSETSW
#define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
#else /* TCSETS */
#ifdef TCSETA
#define TCSANOW TCSETA
#define TCSADRAIN TCSETAW
#define tcgetattr(f,t) ioctl(f,TCGETA,(char *)t)
#else /* TCSETA */
#define TCSANOW TIOCSETA
#define TCSADRAIN TIOCSETAW
#define tcgetattr(f,t) ioctl(f,TIOCGETA,(char *)t)
#endif /* TCSETA */
#endif /* TCSETS */
#define tcsetattr(f,a,t) ioctl(f,a,t)
#define cfsetospeed(tp,val) (tp)->c_cflag &= ~CBAUD;(tp)->c_cflag|=(val)
#define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
#ifdef CIBAUD
#define cfsetispeed(tp,val) \
(tp)->c_cflag &= ~CIBAUD; (tp)->c_cflag |= ((val)<<IBSHIFT)
#define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
#else /* CIBAUD */
#define cfsetispeed(tp,val) (tp)->c_cflag &= ~CBAUD; (tp)->c_cflag|=(val)
#define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
#endif /* CIBAUD */
#endif /* TCSANOW */
struct termios termbuf, termbuf2; /* pty control structure */
#ifdef INIT_SPTY
static int spty = -1;
#endif /* INIT_SPTY */
#endif /* USE_TERMIO */
#ifndef IXANY /* This was in #ifdef QNX.. */
#define IXANY 0 /* but because of _XOPEN_SOURCE */
#endif /* IXANY */ /* must be universal - does no harm */
static int msg = 0;
/* Variables available to other modules */
int pty_fork_active = 0; /* pty fork is active */
PID_T pty_fork_pid = -1; /* pty fork pid */
int pty_slave_fd = -1; /* pty slave file descriptor */
int pty_master_fd = -1; /* pty master file descriptor */
/* termbuf routines (begin) */
/*
init_termbuf()
copy_termbuf(cp)
set_termbuf()
These three routines are used to get and set the "termbuf" structure
to and from the kernel. init_termbuf() gets the current settings.
copy_termbuf() hands in a new "termbuf" to write to the kernel, and
set_termbuf() writes the structure into the kernel.
*/
VOID
init_termbuf(fd) int fd; {
int ttyfd;
int rc = 0;
ttyfd = fd;
#ifdef HAVE_STREAMS
debug(F100,"init_termbuf HAVE_STREAMS","",0);
#else
debug(F100,"init_termbuf HAVE_STREAMS NOT DEFINED","",0);
#endif /* HAVE_STREAMS */
#ifdef STREAMSPTY
debug(F100,"init_termbuf STREAMSPTY","",0);
#else
debug(F100,"init_termbuf STREAMSPTY NOT DEFINED","",0);
#endif /* STREAMSPTY */
#ifdef INIT_SPTY
debug(F100,"init_termbuf INIT_SPTY","",0);
#else
debug(F100,"init_termbuf INIT_SPTY NOT DEFINED","",0);
#endif /* INIT_SPTY */
debug(F101,"init_termbuf ttyfd","",ttyfd);
#ifdef INIT_SPTY
debug(F101,"init_termbuf spty","",spty);
#endif /* INIT_SPTY */
memset(&termbuf,0,sizeof(termbuf));
memset(&termbuf2,0,sizeof(termbuf2));
#ifndef USE_TERMIO
rc = ioctl(ttyfd, TIOCGETP, (char *)&termbuf.sg);
rc |= ioctl(ttyfd, TIOCGETC, (char *)&termbuf.tc);
rc |= ioctl(ttyfd, TIOCGLTC, (char *)&termbuf.ltc);
#ifdef TIOCGSTATE
rc |= ioctl(ttyfd, TIOCGSTATE, (char *)&termbuf.state);
#endif /* TIOCGSTATE */
#else /* USE_TERMIO */
errno = 0;
#ifdef INIT_SPTY
rc = tcgetattr(spty, &termbuf);
debug(F111,"init_termbuf() tcgetattr(spty)",ckitoa(rc),errno);
#else
rc = tcgetattr(ttyfd, &termbuf);
debug(F111,"init_termbuf() tcgetattr(ttyfd)",ckitoa(rc),errno);
#endif /* INIT_SPTY */
#endif /* USE_TERMIO */
if (!rc)
termbuf2 = termbuf;
}
#ifdef TIOCPKT_IOCTL
VOID
copy_termbuf(cp, len) char *cp; int len; {
if (len > sizeof(termbuf))
len = sizeof(termbuf);
memcpy((char *)&termbuf, cp, len);
termbuf2 = termbuf;
}
#endif /* TIOCPKT_IOCTL */
VOID
set_termbuf(fd) int fd; { /* Only make the necessary changes. */
int x;
int ttyfd;
ttyfd = fd;
debug(F101,"set_termbuf ttyfd","",ttyfd);
#ifdef INIT_SPTY
debug(F101,"set_termbuf spty","",spty);
#endif /* INIT_SPTY */
#ifndef USE_TERMIO
debug(F100,"set_termbuf USE_TERMIO","",0);
if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg, sizeof(termbuf.sg)))
ioctl(ttyfd, TIOCSETN, (char *)&termbuf.sg);
if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc, sizeof(termbuf.tc)))
ioctl(ttyfd, TIOCSETC, (char *)&termbuf.tc);
if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
sizeof(termbuf.ltc)))
ioctl(ttyfd, TIOCSLTC, (char *)&termbuf.ltc);
if (termbuf.lflags != termbuf2.lflags)
ioctl(ttyfd, TIOCLSET, (char *)&termbuf.lflags);
#else /* USE_TERMIO */
x = memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf));
debug(F101,"set_termbuf !USE_TERMIO memcmp","",x);
x = 1; /* Force this */
if (x) {
int x;
errno = 0;
#ifdef INIT_SPTY
debug(F100,"set_termbuf INIT_SPTY","",0);
x = tcsetattr(spty, TCSANOW, &termbuf);
debug(F111,"set_termbuf tcsetattr(spty)",ckitoa(x),errno);
#else
debug(F100,"set_termbuf !INIT_SPTY","",0);
x = tcsetattr(ttyfd, TCSANOW, &termbuf);
debug(F111,"set_termbuf tcsetattr(ttyfd)",ckitoa(x),errno);
#endif /* INIT_SPTY */
}
#endif /* USE_TERMIO */
}
/* termbuf routines (end) */
VOID
ptyint_vhangup() {
#ifdef CK_VHANGUP
_PROTOTYP( int vhangup, (void) );
#ifdef CK_POSIX_SIG
struct sigaction sa;
/* Initialize "sa" structure. */
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = SIG_IGN;
sigaction(SIGHUP, &sa, (struct sigaction *)0);
vhangup();
sa.sa_handler = SIG_DFL;
sigaction(SIGHUP, &sa, (struct sigaction *)0);
#else /* CK_POSIX_SIG */
signal(SIGHUP,SIG_IGN);
vhangup();
signal(SIGHUP,SIG_DFL);
#endif /* CK_POSIX_SIG */
#endif /* CK_VHANGUP */
}
/*
This routine is called twice. It's not particularly important that the
setsid() or TIOCSCTTY ioctls succeed (they may not the second time), but
rather that we have a controlling terminal at the end. It is assumed that
vhangup doesn't exist and confuse the process's notion of controlling
terminal on any system without TIOCNOTTY. That is, either vhangup() leaves
the controlling terminal in tact, breaks the association completely, or the
system provides TIOCNOTTY to get things back into a reasonable state. In
practice, vhangup() either breaks the association completely or doesn't
effect controlling terminals, so this condition is met.
*/
long
ptyint_void_association() {
int con_fd;
#ifdef HAVE_SETSID
debug(F110,
"ptyint_void_association()",
"setsid()",
0
);
setsid();
#endif /* HAVE_SETSID */
#ifndef NO_DEVTTY
/* Void tty association first */
#ifdef TIOCNOTTY
con_fd = open("/dev/tty", O_RDWR);
debug(F111,
"ptyint_void_association() open(/dev/tty,O_RDWR)",
"/dev/tty",
con_fd);
if (con_fd >= 0) {
ioctl(con_fd, TIOCNOTTY, 0);
close(con_fd);
}
#ifdef DEBUG
else debug(F101, "ptyint_void_association() open() errno","",errno);
#endif /* DEBUG */
#endif /* TIOCNOTTY */
#endif /* NO_DEVTTY */
return(0);
}
/* PID may be zero for unknown.*/
long
pty_cleanup(slave, pid, update_utmp) char *slave; int pid; int update_utmp; {
#ifdef VHANG_LAST
int retval, fd;
#endif /* VHANG_LAST */
debug(F111,"pty_cleanup()",slave,pid);
#ifdef WANT_UTMP
if (update_utmp)
pty_update_utmp(PTY_DEAD_PROCESS,
0,
"",
slave,
(char *)0,
PTY_UTMP_USERNAME_VALID
);
#endif /* WANT_UTMP */
#ifdef SETUID
chmod(slave, 0666);
chown(slave, 0, 0);
#endif /* SETUID */
#ifdef HAVE_REVOKE
revoke(slave);
/*
Revoke isn't guaranteed to send a SIGHUP to the processes it
dissociates from the terminal. The best solution without a Posix
mechanism for forcing a hangup is to killpg() the process group of the
pty. This will at least kill the shell and hopefully, the child
processes. This is not always the case, however. If the shell puts
each job in a process group and doesn't pass along SIGHUP, all
processes may not die.
*/
if (pid > 0) {
#ifdef HAVE_KILLPG
killpg(pid, SIGHUP);
#else
kill(-(pid), SIGHUP);
#endif /*HAVE_KILLPG*/
}
#else /* HAVE_REVOKE*/
#ifdef VHANG_LAST
{
int status;
#ifdef CK_POSIX_SIG
sigset_t old, new;
sigemptyset(&new);
sigaddset(&new, SIGCHLD);
sigprocmask(SIG_BLOCK, &new, &old);
#else /*CK_POSIX_SIG*/
int mask = sigblock(sigmask(SIGCHLD));
#endif /*CK_POSIX_SIG*/
switch (retval = fork()) {
case -1:
#ifdef CK_POSIX_SIG
sigprocmask(SIG_SETMASK, &old, 0);
#else /*CK_POSIX_SIG*/
sigsetmask(mask);
#endif /*CK_POSIX_SIG*/
return errno;
case 0:
ptyint_void_association();
if (retval = (pty_open_ctty(slave, &fd, -1)))
exit(retval);
ptyint_vhangup();
exit(0);
break;
default:
#ifdef HAVE_WAITPID
waitpid(retval, &status, 0);
#else /*HAVE_WAITPID*/
wait(&status);
#endif /* HAVE_WAITPID */
#ifdef CK_POSIX_SIG
sigprocmask(SIG_SETMASK, &old, 0);
#else /*CK_POSIX_SIG*/
sigsetmask(mask);
#endif /*CK_POSIX_SIG*/
break;
}
}
#endif /*VHANG_LAST*/
#endif /* HAVE_REVOKE*/
#ifndef HAVE_STREAMS
slave[strlen("/dev/")] = 'p';
#ifdef SETUID
chmod(slave, 0666);
chown(slave, 0, 0);
#endif /* SETUID */
#endif /* HAVE_STREAMS */
return(0);
}
#ifdef HAVE_OPENPTY
#include <pty.h>
#endif
long
pty_getpty(fd, slave, slavelength) int slavelength; int *fd; char *slave; {
char *cp;
char *p;
int i, ptynum;
struct stat stb;
#ifndef HAVE_OPENPTY
#ifndef HAVE__GETPTY
char slavebuf[1024];
#endif /* HAVE__GETPTY */
#endif /* HAVE_OPENPTY */
#ifdef HAVE__GETPTY
char *slaveret; /* Temp to hold pointer to slave */
#endif /*HAVE__GETPTY*/
#ifdef HAVE_OPENPTY
int slavefd;
pty_master_fd = -1;
debug(F100,"HAVE_OPENPTY","",0);
if (openpty(fd,
&slavefd,
slave,
(struct termios *)0,
(struct winsize *)0
)
) {
pty_master_fd = *fd;
return(1);
}
close(slavefd);
return(0);
#else /* HAVE_OPENPTY */
#ifdef HAVE__GETPTY
/*
This code is included for Irix; as of version 5.3, Irix has /dev/ptmx, but
it fails to work properly; even after calling unlockpt, root gets permission
denied opening the pty. The code to support _getpty should be removed if
Irix gets working streams ptys in favor of maintaining the least needed code
paths.
*/
debug(F100,"HAVE__GETPTY","",0);
if ((slaveret = _getpty(fd, O_RDWR | O_NDELAY, 0600, 0)) == 0) {
*fd = -1;
return(PTY_GETPTY_NOPTY);
}
if (strlen(slaveret) > slavelength - 1) {
close(*fd);
*fd = -1;
return(PTY_GETPTY_SLAVE_TOOLONG);
} else {
ckstrncpy(slave, slaveret, slavelength);
}
return(0);
#else /* HAVE__GETPTY */
*fd = open("/dev/ptym/clone", O_RDWR|O_NDELAY); /* HPUX */
if (*fd >= 0) {
debug(F110,"pty_getpty()","open(/dev/ptym/clone) success",0);
goto have_fd;
}
#ifdef HAVE_PTMX
debug(F100,"HAVE_PTMX","",0);
*fd = open("/dev/ptmx",O_RDWR|O_NDELAY);
if (*fd >= 0) {
debug(F110,"pty_getpty()","open(/dev/ptmx) success",0);
goto have_fd;
}
#endif /* HAVE_PTMX */
*fd = open("/dev/ptc", O_RDWR|O_NDELAY); /* AIX */
if (*fd >= 0) {
debug(F110,"pty_getpty()","open(/dev/ptc) success",0);
goto have_fd;
}
*fd = open("/dev/pty", O_RDWR|O_NDELAY); /* sysvimp */
if (*fd >= 0)
debug(F110,"pty_getpty()","open(/dev/pty) success",0);
have_fd:
/* This would be the pty master */
debug(F101,"pty_getpty fd(A)","",*fd);
if (*fd >= 0) {
pty_master_fd = *fd;
#ifdef HAVE_GRANTPT
#ifdef HAVE_PTMX
debug(F100,"HAVE_GRANTPT","",0);
if (grantpt(*fd) || unlockpt(*fd))
return(PTY_GETPTY_STREAMS);
#endif /* HAVE_PTMX */
#endif /* HAVE_GRANTPT */
#ifdef HAVE_PTSNAME
debug(F100,"HAVE_PTSNAME","",0);
p = (char *)ptsname(*fd);
debug(F110,"pty_getpty() ptsname()",p,0);
#else
#ifdef HAVE_TTYNAME
debug(F100,"HAVE_TTYNAME","",0);
p = ttyname(*fd);
debug(F110,"pty_getpty() ttyname()",p,0);
#else
/* If we don't have either what do we do? */
return(PTY_GETPTY_NOPTY); /* punt */
#endif /* HAVE_TTYNAME */
#endif /* HAVE_PTSNAME */
if (p) {
if (strlen(p) > slavelength - 1) {
close (*fd);
*fd = -1;
return(PTY_GETPTY_SLAVE_TOOLONG);
}
ckstrncpy(slave, p, slavelength);
return(0);
}
if (fstat(*fd, &stb) < 0) {
close(*fd);
return(PTY_GETPTY_FSTAT);
}
ptynum = (int)(stb.st_rdev&0xFF);
sprintf(slavebuf, "/dev/ttyp%x", ptynum); /* safe */
if (strlen(slavebuf) > slavelength - 1) {
close(*fd);
*fd = -1;
return(PTY_GETPTY_SLAVE_TOOLONG);
}
debug(F110,"pty_getpty() slavebuf",slavebuf,0);
ckstrncpy(slave, slavebuf, slavelength);
return(0);
} else {
for (cp = "pqrstuvwxyzPQRST";*cp; cp++) {
sprintf(slavebuf,"/dev/ptyXX"); /* safe */
slavebuf[sizeof("/dev/pty") - 1] = *cp;
slavebuf[sizeof("/dev/ptyp") - 1] = '0';
if (stat(slavebuf, &stb) < 0)
break;
for (i = 0; i < 16; i++) {
slavebuf[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i];
errno = 0;
*fd = open(slavebuf, O_RDWR|O_NDELAY);
if (*fd < 0) {
debug(F111,"pty_getpty() pty master open error",
slavebuf,errno);
continue;
}
debug(F111,"pty_getpty() found pty master",slavebuf,*fd);
slavebuf[sizeof("/dev/") - 1] = 't'; /* got pty */
if (strlen(slavebuf) > slavelength -1) {
close(*fd);
*fd = -1;
return(PTY_GETPTY_SLAVE_TOOLONG);
}
ckstrncpy(slave, slavebuf, slavelength);
debug(F110,"pty_getpty slave name",slave,0);
pty_master_fd = *fd;
return(0);
}
}
return(PTY_GETPTY_NOPTY);
}
#endif /*HAVE__GETPTY*/
#endif /* HAVE_OPENPTY */
}
long
pty_init() {
#ifdef HAVE_PTYM
static char dummy;
debug(F100,"HAVE_PTYM","",0);
tty_bank = &master_name[strlen("/dev/ptym/pty")];
tty_num = &master_name[strlen("/dev/ptym/ptyX")];
slave_bank = &slave_name[strlen("/dev/pty/tty")];
slave_num = &slave_name[strlen("/dev/pty/ttyX")];
#endif
return(0L);
}
/*
The following is an array of modules that should be pushed on the stream.
See configure.in for caviats and notes about when this array is used and not
used.
*/
#ifdef HAVE_STREAMS
#ifndef HAVE_LINE_PUSH
static char *push_list[] = {
#ifdef PUSH_PTEM
"ptem",
#endif
#ifdef PUSH_LDTERM
"ldterm",
#endif
#ifdef PUSH_TTCOMPAT
"ttcompat",
#endif
0
};
#endif /* HAVE_LINE_PUSH */
#endif /* HAVE_STREAMS */
long
pty_initialize_slave (fd) int fd; {
#ifdef POSIX_TERMIOS
#ifndef ultrix
struct termios new_termio;
#else
struct sgttyb b;
#endif /* ultrix */
#else
struct sgttyb b;
#endif /* POSIX_TERMIOS */
int pid;
#ifdef POSIX_TERMIOS
#ifndef ultrix
int rc;
#endif /* ultrix */
#endif /* POSIX_TERMIOS */
debug(F111,"pty_initialize_slave()","fd",fd);
#ifdef HAVE_STREAMS
#ifdef HAVE_LINE_PUSH
while (ioctl(fd,I_POP,0) == 0) ; /* Clear out any old lined's */
if (line_push(fd) < 0) {
debug(F110,"pty_initialize_slave()","line_push() failed",0);
close(fd);
fd = -1;
return(PTY_OPEN_SLAVE_LINE_PUSHFAIL);
}
#else /*No line_push */
{
char **module = &push_list[0];
while (*module) {
if (ioctl(fd, I_PUSH, *(module++)) < 0) {
debug(F110,"pty_initialize_slave()","ioctl(I_PUSH) failed",0);
return(PTY_OPEN_SLAVE_PUSH_FAIL);
}
}
}
#endif /*LINE_PUSH*/
#endif /*HAVE_STREAMS*/
/*
Under Ultrix 3.0, the pgrp of the slave pty terminal needs to be set
explicitly. Why rlogind works at all without this on 4.3BSD is a mystery.
*/
#ifdef GETPGRP_ONEARG
pid = getpgrp(getpid());
#else
pid = getpgrp();
#endif /* GETPGRP_ONEARG */
debug(F111,"pty_initialize_slave()","pid",pid);
#ifdef TIOCSPGRP
ioctl(fd, TIOCSPGRP, &pid);
#endif /* TIOCSPGRP */
#ifdef POSIX_TERMIOS
#ifndef ultrix
tcsetpgrp(fd, pid);
errno = 0;
rc = tcgetattr(fd,&new_termio);
debug(F111,"pty_initialize_slave tcgetattr(fd)",ckitoa(rc),errno);
if (rc == 0) {
new_termio.c_cc[VMIN] = 1;
new_termio.c_cc[VTIME] = 0;
rc = tcsetattr(fd,TCSANOW,&new_termio);
debug(F111,"pty_initialize_slave tcsetattr(fd)",ckitoa(rc),errno);
}
#endif /* ultrix */
#endif /* POSIX_TERMIOS */
return(0L);
}
#ifdef WANT_UTMP
long
pty_logwtmp (tty, user, host) char *user, *tty, *host; {
#ifdef HAVE_LOGWTMP
logwtmp(tty,user,host);
return(0);
#else
struct utmp ut;
char *tmpx;
char utmp_id[5];
int loggingin = user[0]; /* Will be empty for logout */
#ifndef NO_UT_HOST
strncpy(ut.ut_host, host, sizeof(ut.ut_host));
#endif /* NO_UT_HOST */
strncpy(ut.ut_line, tty, sizeof(ut.ut_line));
ut.ut_time = time(0);
#ifndef NO_UT_PID
ut.ut_pid = getpid();
strncpy(ut.ut_user, user, sizeof(ut.ut_user));
tmpx = tty + strlen(tty) - 2;
ckmakmsg(utmp_id,5,"kr",tmpx,NULL,NULL);
strncpy(ut.ut_id, utmp_id, sizeof(ut.ut_id));
ut.ut_pid = (loggingin ? getpid() : 0);
ut.ut_type = (loggingin ? USER_PROCESS : DEAD_PROCESS);
#else