-
Notifications
You must be signed in to change notification settings - Fork 0
/
pty.c
820 lines (691 loc) · 15.9 KB
/
pty.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
/*
* This code was shamelessly stolen from the Emu project, whose notice is
* reproduced below.
*/
/*
* This file is part of the Emu system.
*
* Copyright 1990 by PCS Computer Systeme, GmbH. Munich, West Germany.
*
* Copyright 1994 by Jordan K. Hubbard and Michael W. Elbel
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL PCS, THE AUTHORS, OR THEIR HOUSEPETS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SO DON'T SUE US.
* THANK YOU.
*/
#ifdef SERIAL
/*#include "QLtypes.h"*/
#include "QL68000.h"
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include <errno.h>
#include <setjmp.h>
#include <fcntl.h>
#include <termios.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
//#include <sys/termios.h>
#ifdef HPUX
#include <bsdtty.h>
#endif
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#ifdef __linux__
#include <sys/cdefs.h>
#endif
#include <sys/wait.h>
#include <sys/time.h> /* for timeval */
#include <stdarg.h>
#ifdef BSD44
#define BSD
#endif
#ifdef NEWPTY
#include "QL.h"
#include "driver.h"
#include "QSerial.h"
#endif
#include "qx_proto.h"
#if !defined(NFILES)
#define NFILES 64 /* take a wild guess */
#endif
#ifdef _NO_PID_T
typedef int pid_t;
#endif
#if defined(__GNUC__)
# ifdef _USE_STDARGS
# define _VARARGS(start) start, ...
# define _VA_DCL(fname, arg) fname(arg, ...)
# else
# define _VARARGS(start) ...
# define _VA_DCL(fname, arg) fname(va_alist) va_dcl
# endif
#else
# define _VARARGS(start)
# define _VA_DCL(fname, arg) fname(va_alist) va_dcl
#endif
/* predefined subprocess exit codes */
#define PROC_EXEC_FAILED -4
#define PROC_TTYOPEN_FAILED -5
#if !defined (SIGCHLD) && defined (SIGCLD)
#define SIGCHLD SIGCLD
#endif /* SIGCLD */
#if !defined(PTYDEV)
#define PTYDEV "/dev/pty%c%c"
#endif /* !PTYDEV */
#if !defined(TTYDEV)
#define TTYDEV "/dev/tty%c%c"
#endif /* !TTYDEV */
#if !defined(PTYCHAR1)
#define PTYCHAR1 "pqrstuvwxyz"
#endif /* !PTYCHAR1 */
#if !defined(PTYCHAR2)
#define PTYCHAR2 "0123456789abcdef"
#endif /* !PTYCHAR2 */
#define DEFAULTCOMMAND "/bin/sh"
#define DEFAULTTERMTYPE "vt100"
/* moved to QSerial.h */
#if 0
typedef struct _FakeRec *FakeTerm;
typedef struct _FakeRec
{
char *command; /* command name */
char **command_args; /* command arguments */
char *term_type; /* terminal type */
char utmp_inhibit; /* don't add utmp entries */
char login_shell; /* invoke shell as login shell */
char *tname; /* name of allocated PTY */
pid_t pid; /* process ID of child process */
uid_t uid; /* uid of invoking user */
gid_t gid; /* gid of invoking user */
int master; /* pty master file descriptor */
int slave; /* pty slave file descriptor */
} FakeRec;
#endif
/*static FakeTerm w;*/
void fake_tty_close(FakeTerm);
FakeTerm fake_tty_open(char *);
void pty_cleanup(pid_t pid, int id);
#ifdef NEWPTY
open_arg pty_par[4];
int pty_init(int idx){return 0;}
int pty_test(int id, char *name)
{
return decode_name(name,Drivers[id].namep,&pty_par);
}
FakeTerm pty_list=NULL;
int pty_open(int id, void **priv)
{
serdev_t *p;
char *cmd;
FakeTerm res;
/*int unit=ser_par[0];*/
/* if (unit<1 || unit>MAXSERIAL)
return -1;*/
*priv=p=malloc(sizeof(serdev_t));
p->parity=0;
p->hshake=-1;
p->xlate=pty_par[1].i;
p->baud=115200;
#ifdef NO_FIONREAD
p->bfc_valid=0;
#endif
p->killed=0;
p->w=NULL;
p->teof=0;
cmd=pty_par[2].s;
if (!strcmp(cmd,""))
cmd=NULL;
p->w = fake_tty_open(cmd);
if (p->w && (p->fd = p->w->master)>0)
{
/*link into list*/
if (pty_list) {p->w->next=pty_list;pty_list=p->w;}
else pty_list=p->w;
p->w->sd=p;
p->w->job_control=pty_par[0].i;
return 0;
}
/* failure */
free(p);
return -1;
}
void pty_close(int id,void *priv)
{
FakeTerm tp;
char *fb;
serdev_t *p=priv;
/*printf("pty_close\n");*/
ser_write(p,&fb,0); /* attempt to generate a EOF/EOT */
fake_tty_close(p->w);
if (p->w->job_control==2)
kill(p->w->pid, 2);
if (pty_list==p->w)
{
pty_list=pty_list->next;
return;
}
tp=pty_list;
while(tp)
{
if (tp->next==p->w)
{
tp->next=tp->next->next;
free(p->w);
return;
}
tp=tp->next;
}
/*printf("WARNING: closed pty not in list\n");*/
}
#endif
static void fork_process(FakeTerm w)
{
int i;
sigset_t mask,omask;
/* Make sure we don't get the signal just yet - do it the BSD way,
linux doesn't support sighold */
#if 0
omask = sigblock(sigmask(SIGCHLD));
#else
sigemptyset(&mask);
sigaddset(&mask,SIGCHLD);
sigprocmask(SIG_BLOCK,&mask,&omask);
#endif
if (!(w->pid = qm_fork(pty_cleanup,0))) {
/* Now in child process */
char **argp;
int fd;
struct group *gr;
/* We're not particularly interested */
signal(SIGCHLD, SIG_DFL);
if (setsid() < 0)
puts ("failed to set process group");
#if 0
printf("execing prog: %x %s\n",w->command,w->command);
printf("args: %x %s\n",w->command_args);
for (i=0;w->command_args[i];i++)
printf("\t%s\n",w->command_args[i]);
#endif
#if 1
/* Close everything in sight */
for (i = 0; i < NFILES; i++)
close(i);
#else
close(0);
close(1); /* leave stderr */
for (i = 3; i < NFILES; i++)
close(i);
#endif
/*
* Open the slave side for stdin and dup twice for stdout and stderr.
* We need to open all fd's read/write since some weird applications
* (like "more") READ from stdout/stderr or WRITE to stdin!
* Ack, bletch, barf.
*
* This should also take care of the controlling TTY. I hope.
*/
fd = open(w->tname, O_RDWR, 0); /* 0 */
dup(fd); /* 1 */
dup(fd); /* 2 */
#if 0
fprintf(stderr,"opened tty: %s, result %d\n",w->tname,fd);
#endif
#ifndef __USE_XOPEN /* XOPEN does this for us */
/* It's MINE, I tell you! Mine! Mine! Mine! */
fchown(fd, w->uid, w->gid);
fchmod(fd, 0622);
#endif
#if 0
/* set various signals to non annoying (for SYSV) values */
if (getpgrp() == getpid()) {
signal(SIGINT, exit);
signal(SIGQUIT, exit);
signal(SIGTERM, exit);
}
else {
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
}
signal(SIGPIPE, exit);
#endif
/* since we may be setuid root (for utmp), be safe */
setuid(w->uid);
setgid(w->gid);
argp = w->command_args ;
if (w->login_shell)
argp[0] = "-";
else
argp[0] = w->command;
/*printf("*******\n");*/
execvp(w->command, argp);
/*
* This will show up in the client window (hopefully), so
* sleep for some time before exiting to let the user read
* the message
*/
printf ("### pty error message: Couldn't exec %s\t ###\n", argp[0]);
sleep(5);
exit(PROC_EXEC_FAILED);
}
/*
* Now that we have stored the widget ID, we can enable SIGCHLD
* and deal with a possible early failure.
*/
sigprocmask(SIG_SETMASK,&omask,NULL);
}
static void process_init(FakeTerm w)
{
char envterm[1024], *tmp;
char * res;
w->uid = getuid();
w->gid = getgid();
sprintf(envterm, "TERM=%s", w->term_type);
tmp = strdup (envterm);
putenv(tmp);
fork_process(w);
}
static void process_cleanup(FakeTerm w)
{
/*printf("process cleanup\n");*/
}
#define WAIT_STATUS_TYPE int
static char * tty_find_pty(FakeTerm w)
{
#ifdef __USE_XOPEN
char *pty;
if((w->master = open("/dev/ptmx",O_RDWR,0)) >= 0)
{
grantpt(w->master);
unlockpt(w->master);
pty = ptsname(w->master);
fprintf(stderr, "Using XOPEN pty %s\n", pty);
if((w->slave = open(pty, O_RDWR,0)) >= 0)
{
return strdup(pty);
}
else
{
close(w->master);
}
}
#else
char ttyname[80];
struct stat st;
register char *c1, *c2;
/* Use user provided search loop or our own to grub through ptys */
for (c1 = PTYCHAR1; *c1; ++c1) {
for (c2 = PTYCHAR2; *c2; ++c2) {
/* Go for the master side */
sprintf(ttyname, PTYDEV, *c1, *c2);
if (stat(ttyname, &st) < 0)
continue;
if ((w->master = open(ttyname, O_RDWR, 0)) >= 0) {
/*
* Now make sure that the slave side is available.
* This allows for a bug in some versions of rlogind
* and gives us a handle for ioctl() ops.
*/
sprintf(ttyname, TTYDEV, *c1, *c2);
if ((w->slave = open(ttyname, O_RDWR, 0)) >= 0) {
return strdup(ttyname);
}
else
close(w->master);
}
}
}
#endif
return NULL;
}
typedef struct termios TtyStuff;
/* File descriptors to try */
static int Fd_try[] = { 0, 1, 2, -1, 0, -1 };
/* If w is NULL, get tty values for invoking tty, not pty */
static void * tty_get_values(FakeTerm w)
{
int i, *try;
static TtyStuff ret;
extern int errno;
if (w == NULL)
try = Fd_try;
else {
try = Fd_try + 4;
try[0] = w->slave;
}
for (i = 0; try[i] >= 0; i++) {
int r;
r = tcgetattr(try[i], &ret);
if (!r) /* success? */
break;
}
return (void *)&ret;
}
/* Must return some "sane" set of hardwired values */
static void * tty_get_sane()
{
static struct termios sane;
static char cchars[] = {
VINTR, VQUIT, VERASE, VKILL, VEOF, VEOL, VSTART, VSTOP, VSUSP,
};
/*
* load values individually rather than in static struct since
* the order of the members *doesn't* seem to be something that
* all posix systems agree on.
*/
sane.c_iflag = IGNPAR|ICRNL|BRKINT|ISTRIP|IXON;
sane.c_oflag = OPOST|ONLCR;
sane.c_cflag = B9600|CS7|PARENB|CREAD;
sane.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHONL;
#ifdef __linux__
sane.c_line = 2;
#endif
bcopy(cchars, sane.c_cc, sizeof(cchars));
return (void *)&sane;
}
/* Whap some saved values onto the tty */
static void tty_set_values(void *val, FakeTerm w)
{
int i, *try;
extern int errno;
if (w == NULL)
try = Fd_try;
else {
try = Fd_try + 4;
try[0] = w->slave;
}
for (i = 0; try[i] >= 0; i++) {
int r;
r = tcsetattr(try[i], TCSADRAIN, (struct termios *)val);
if (!r) /* success? */
break;
}
}
static void tty_set_size(FakeTerm w, int rows, int cols, int width, int height)
{
struct winsize wz;
int pgrp;
wz.ws_row = rows;
wz.ws_col = cols;
wz.ws_xpixel = width;
wz.ws_ypixel = height;
ioctl(w->slave, TIOCSWINSZ, &wz);
#ifdef TIOCGPGRP
if (ioctl(w->slave, TIOCGPGRP, &pgrp) != -1)
killpg(pgrp, SIGWINCH);
#endif
}
void pty_cleanup(pid_t pid, int id)
{
FakeTerm tp;
tp=pty_list;
while(tp)
{
if (pid==tp->pid)
{
if (tp->job_control != 1)
tp->sd->killed=1;
return;
}
tp=tp->next;
}
}
void fake_process_create(FakeTerm w)
{
/*signal(SIGCHLD, process_reaper);*/
process_init(w);
}
static int doargs(char *p, char **cmdp, char ***argp)
{
int n,ff,qq,q,issp;
char *p1,*p2,*p3,*pp,*pc;
char **x,**xav;
#if 1
p3=malloc(strlen(p)*2);
issp=q=qq=0;
for(p1=p,pp=p3;*p1;)
{
if (*p1=='\'') q=(!q),qq=1;
else qq=0;
if (q || qq)
{
*pp++=*p1++;
continue;
}
if ((*p1!='<') && (*p1!='>') && !(isdigit(*p1) && (*(p1+1)=='<' || *(p1+1)=='>')))
{
if (!issp || *p1!=' ')
*pp++=*p1++;
else p1++;
if (*(p1-1)==' ') issp=1;
else issp=0;
}
else /* insert spaces around redirection form */
{
*pp++=' ';
ff=0;
if (!isdigit(*p1)) ff++;
*pp++=*p1++;
while(ff<2 && (*p1=='<' || *p1=='>'))
{
*pp++=*p1++;
ff++;
}
*pp++=' ';
}
}
*pp=0;
pp=p3; /* free !!!! */
/*printf("pty args are : %s\n",pp);*/
#endif
#if 1
p1=pp;
n=1;
while (p3)
{
p3=strpbrk(p3," '\t");
if (p3)
{
if (*p3=='\'')
{
p3=strpbrk(p3+1,"'");
if (p3)p3+=1;
continue;
}
*p3++=0;
}
n++;
}
xav = x = malloc((n+1) * sizeof(char *));
ff = 0;
p3=p1;
while(--n>0)
{
/*printf("setting arg %d to %s\n",x-xav,p3);*/
*x=p3;
if(ff == 0)
{
*cmdp = *x;ff=1;
}
x++;
p3=p3+strlen(p3)+1;
}
*x = NULL;
*argp = xav;
return n;
#else
p3 = strdup(pp);
for(n = 0, p1 = p3; (p2 = strtok(p1, " \t")); p1 = NULL, n++)
;
free(p3);
xav = x = malloc((n+1) * sizeof(char *));
n = 0;
for(p1 = p; (p2 = strtok(p1, " \t")); n++, p1 = NULL)
{
*x = strdup(p2);
if(n == 0)
{
*cmdp = *x;
}
x++;
}
*x = NULL;
*argp = xav;
return n;
#endif
}
/* convert UQLX-> unix filenames and prepare redirection a'la 'sh' */
void conv_fnames(char ***cmd, int *redir)
{
char **p=*cmd;
char *c;
#if 0
for(;*p,p++)
{
c=*p+strspn(*p," \t");
if (*c!='#') continue;
c++;
*p=ql2uxname(c); /* alloc/dealloc !!!! */
}
#endif
}
FakeTerm fake_tty_open(char *cmd)
{
FakeTerm w;
int redir[20];
if((w = calloc(1,sizeof(FakeRec))))
{
if(cmd)
{
doargs(cmd, &w->command, &w->command_args);
conv_fnames(&w->command_args,redir);
}
else
{
w->command = DEFAULTCOMMAND;
w->command_args = NULL;
}
w->term_type = DEFAULTTERMTYPE;
if (!(w->tname = tty_find_pty(w)))
{
free(w);
return 0;
}
else
{
tty_set_size (w, 24, 80, 480, 240);
fake_process_create(w);
/*printf("pty child process %d\n",w->pid);*/
return w;
}
}
}
void fake_tty_close(FakeTerm w)
{
/*printf("fake_tty_close\n");*/
if(w)
{
char **x;
if (w->master >= 0)
close(w->master);
if (w->slave >= 0)
close(w->slave);
if(w->tname)
free(w->tname);
x = w->command_args;
if (x && *x) free(x);
/* for(x = w->command_args; x && *x; x++)
{
if(*x) free(*x);
}*/
/*if(w->command_args) free (w->command_args);*/
/*free(w);*/
/*w = NULL;*/
process_cleanup(w);
}
}
#ifdef TEST
int check(int f1, int f2, fd_set *rfds)
{
if(FD_ISSET(f1, rfds))
{
int nb;
int nn;
char buf[80];
if(ioctl(f1, FIONREAD, &nn) == 0)
{
if(nn > 80) nn = 80;
if((nb = read(f1, buf, nn)) > 0)
{
write(f2, buf, nb);
}
}
}
}
void fake_handle_process()
{
int n, nc,nr ;
short done = 0;
fd_set rfds;
FD_ZERO(&rfds);
nr = 0;
while ( !done )
{
FD_SET(0, &rfds);
FD_SET(w->master, &rfds);
n = select ( FD_SETSIZE, &rfds, 0, 0, NULL);
switch(n)
{
case 0:
nr = 0;
done = 1;
break;
case -1:
nr = -1;
done = 1;
break;
default:
check(0, w->master, &rfds);
check(w->master, 1, &rfds);
break;
}
}
}
int main(int ac, char *av)
{
if(fake_tty_open () > 0)
{
printf("fds are %d %d\n", w->master, w->slave);
fake_handle_process();
fake_tty_close();
}
}
#endif
#endif /* SERIAL */