-
Notifications
You must be signed in to change notification settings - Fork 3
/
linux-dev.c
753 lines (643 loc) · 13.5 KB
/
linux-dev.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
/*
*
* Copyright (C) Cyclades Corporation, 1999-1999. All rights reserved.
*
*
* dev.c
* Unix Pty Device routines
*
* History
* 08/17/1999 V.1.0.0 Initial revision
*
* 12/17/1999 V.1.0.1
* Fixes: a copy in parse_message () now uses the correct data pointer
*
*/
/* Open / stat includes */
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include <stdlib.h>
/* Errno */
# include <stdio.h>
# include <errno.h>
# include <string.h>
/* Termio */
# ifdef USE_TERMIO
# include <termio.h>
# endif
/* Termios */
# ifdef USE_TERMIOS
# include <termios.h>
# endif
/* Get process group id of controlling pty */
# include <dirent.h>
/* Signalling slave (kill) */
# include <signal.h>
# define _TSR_DEV_
#include "inc/cyclades-ser-cli.h"
#include "inc/system.h"
#include "inc/tsrio.h"
#include "inc/telnet.h"
#include "inc/dev.h"
#include "inc/misc.h"
#include "inc/port_speed.h"
#ifdef UNIX98
#include <pty.h> /* for openpty and forkpty */
#include <utmp.h>
#endif
#ifdef TSR_MEASURE
#include "inc/tsrmeasure.h"
#endif
/*
* Packet mode routines
*/
void parse_message(unsigned char type, char *buf, int size);
void parse_packet(int type);
/*
* Termio / Termios routines
*/
void portconfig_to_termios(struct portconfig *pcp, struct termios *tp);
/*
* Argh => Get the process group id associated with slave pty
*/
int get_slave_controlling(dev_t device);
/*
* Debug routines
*/
void print_msg(int type, unsigned char *buf, int size);
#if 0
char *ioctl_name(int type, void *unused);
#endif
/*
* Internal Variables
*/
char P_sname[NAMESIZE];
dev_t P_devnumber;
char Databuf[DEV_MAXIOSZ];
# define NTRIES 10
# define PTYDELAY 600 /* one minute */
#ifndef UNIX98
# define CONTROL_PREFIX "/dev/pty"
# define SLAVE_PREFIX "/dev/tty"
const char *const letters = "pqrstuvwxyzabcde";
const char *const ports = "0123456789abcdef";
#endif
int
dev_getaddr(char *dname)
{
int fd = -1;
#ifndef UNIX98
int found;
char ctty[16];
int i, j;
int tries;
#else
int slave_fd;
#endif
int mode;
char stty[16];
struct stat statb;
if (lstat(dname, &statb) >= 0) { /* File exists */
if (S_ISLNK(statb.st_mode)) {
sysmessage(MSG_WARNING, "Removing old sym-link \"%s\".\n", dname);
unlink(dname);
}
else if (!S_ISCHR(statb.st_mode)) {
sysmessage(MSG_ERR, "%s already exists\n", dname);
return (E_PARMINVAL);
}
}
else if (errno != ENOENT) { /* generic stat error */
sysmessage(MSG_ERR, "Can't lstat %s : %s\n", dname, strerror(errno));
return (E_FILEIO);
}
#ifndef UNIX98
mode = O_RDWR;
# ifdef USE_POSIX_NONBLOCK
mode |= O_NONBLOCK;
# elif defined USE_STD_NDELAY
mode |= O_NDELAY;
# endif
/*
* Warning: most PTY implementation puts master side as controlling terminal if
* O_NOCTTY is not set !!!
*/
mode |= O_NOCTTY;
found = FALSE;
for (tries = 0; tries < NTRIES; tries++) {
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
sprintf(ctty, "%s%c%c", CONTROL_PREFIX, letters[i], ports[j]);
if ((fd = open(ctty, mode)) >= 0) {
sysmessage(MSG_NOTICE, "open %s pseudo-tty\n", ctty);
found = TRUE;
goto out;
}
}
}
sysdelay(PTYDELAY);
}
out:
if (found == FALSE) {
sysmessage(MSG_ERR, "Can't get a free pseudo-tty :\n");
(void) close(fd);
return (E_FILEIO);
}
#else
if (openpty(&fd, &slave_fd, stty, NULL, NULL)) {
sysmessage(MSG_ERR, "Can't get a free pseudo-tty :\n");
return (E_FILEIO);
}
#endif
# ifdef USE_FIONBIO
mode = 1;
if (ioctl(fd, FIONBIO, &mode) == -1) {
sysmessage(MSG_ERR,
"Can't set non-block on master pty : %s\n",
strerror(errno));
close(fd);
return (E_FILEIO);
}
# endif
mode = 1;
if (ioctl(fd, TIOCPKT, &mode) == -1) {
sysmessage(MSG_ERR,
"Can't put master pty in packet mode: %s\n",
strerror(errno));
close(fd);
return (E_FILEIO);
}
#ifndef UNIX98
sprintf(stty, "%s%c%c", SLAVE_PREFIX, letters[i], ports[j]);
#endif
if (lstat(stty, &statb) == -1) { /* File does not exists */
sysmessage(MSG_ERR, "Can't stat slave pty: %s\n", strerror(errno));
close(fd);
return (E_FILEIO);
}
P_devnumber = statb.st_rdev;
if (symlink(stty, dname) == -1) {
sysmessage(MSG_ERR, "Can't link dev : %s\n", strerror(errno));
return (E_FILEIO);
}
else {
sysmessage(MSG_NOTICE, "Using %s pseudo-tty\n", stty);
}
P_mfd = fd;
strcpy(P_sname, stty);
strcpy(P_devname, dname);
return (E_NORMAL);
}
void
dev_free(void)
{
(void) close(P_sfd);
P_sfd = -1;
return;
}
int
dev_init(int iosize, int devmodem, int closemode, struct buffer *ibp,
struct buffer *obp, struct comport *cp)
{
Pty.portmodes = 0;
if (devmodem == DEV_LOCAL) {
Pty.portmodes = PORT_CLOCAL;
}
if (closemode == CLOSE_HANG) {
Pty.portmodes |= PORT_HUPCL;
}
Pty.portmodes |= PORT_IGNBRK | PORT_IGNPAR;
Pty.iosize = iosize;
Pty.inbuff = ibp;
Pty.outbuff = obp;
Pty.comport = cp;
return (E_NORMAL);
}
int
dev_config(void)
{
int sfd;
struct termios tios;
struct portconfig *pcp = &Pty.comport->portconfig;
int modes = Pty.portmodes;
sysmessage(MSG_NOTICE, "Opening %s pseudo-tty\n", P_sname);
if ((sfd = open(P_sname, O_RDWR | O_NOCTTY)) == -1) {
sysmessage(MSG_ERR, "Can't open slave device : %s\n",
strerror(errno));
return (E_FILEIO);
}
memset((void *) &tios, 0, sizeof(struct termios));
portconfig_to_termios(pcp, &tios);
tios.c_cflag |= CREAD;
tios.c_lflag |= NOFLSH;
/* PTY modes */
if (modes & PORT_HUPCL)
tios.c_cflag |= HUPCL;
if (modes & PORT_CLOCAL)
tios.c_cflag |= CLOCAL;
if (modes & PORT_IGNBRK)
tios.c_iflag |= IGNBRK;
if (modes & PORT_IGNPAR)
tios.c_iflag |= IGNPAR;
tios.c_iflag &= !IXOFF; /* turn off ixon */
tios.c_cc[VMIN] = 1;
if (tcsetattr(sfd, TCSANOW, &tios) == -1) {
sysmessage(MSG_ERR, "Can't set termios : %s\n", strerror(errno));
(void) close(sfd);
return (E_FILEIO);
}
P_sfd = sfd;
return (E_NORMAL);
}
int
dev_closeslave(void)
{
int mode;
if (Pty.state == PTY_OPER && P_sfd != -1) {
if (Debug > 1) {
sysmessage(MSG_DEBUG, "Closing %s pseudo-tty \n", P_sname);
}
sysmessage(MSG_NOTICE, "Closing %s pseudo-tty \n", P_sname);
(void) close(P_sfd);
P_sfd = -1;
mode = 1;
if (ioctl(P_mfd, TIOCPKT, &mode) == -1) {
sysmessage(MSG_ERR,
"Can't put master pty in packet mode: %s\n",
strerror(errno));
return (E_FILEIO);
}
}
return (E_NORMAL);
}
unsigned char Holdbuf[4];
int Hold = FALSE;
int
dev_probe(void)
{
int retc;
int retmsg;
unsigned char type;
if ((retc = read(P_mfd, Holdbuf, 1)) == -1) {
if (errno == EIO) { /* PTY WAS CLOSED */
retc = 0;
}
else {
sysmessage(MSG_ERR,
"Can't read from master pty: %s\n", strerror(errno));
return (retc);
}
}
if (Debug > 2) {
sysmessage(MSG_DEBUG, "PROBE: %d bytes: %d", retc, Holdbuf[0]);
}
if (retc != 0) {
type = Holdbuf[0];
if (type == TIOCPKT_DATA) {
retmsg = PROBE_DATA;
}
else if (type & (TIOCPKT_FLUSHREAD | TIOCPKT_FLUSHWRITE)) {
retmsg = PROBE_FLUSH;
}
else {
retmsg = PROBE_GENERIC;
}
Hold = TRUE;
}
else {
retmsg = PROBE_EOF;
}
if (Debug > 1) {
sysmessage(MSG_DEBUG, "PROBE: msg %d\n", retmsg);
}
return (retmsg);
}
int
dev_getdata(void)
{
int retc;
int size;
int mode;
unsigned char type;
size = Pty.iosize;
if (Hold == TRUE) {
Hold = FALSE;
retc = 1;
Databuf[0] = Holdbuf[0];
}
else {
if ((retc = read(P_mfd, Databuf, size)) == -1) {
if (errno == EIO) { /* PTY WAS CLOSED */
retc = 0;
}
else {
sysmessage(MSG_ERR,
"Can't read from master pty: %s\n",
strerror(errno));
# ifdef TSR_MEASURE
devnreads++;
# endif
return (retc);
}
}
}
if (Debug > 2) {
sysmessage(MSG_DEBUG, " DATA: %d bytes: ", retc);
}
if (Debug > 2) {
int i;
char debbuf[128];
char oct[8];
sprintf(debbuf, "DAT: ");
for (i = 0; i < retc && i < 8; i++) {
sprintf(oct, "%02X ", (unsigned char) Databuf[i]);
strcat(debbuf, oct);
}
sysmessage(MSG_DEBUG, "%s\n", debbuf);
}
/*
* Kernel 2.2.x => Closing slave also disables packet mode.
* Not all closes are detected, thus I restore packet mode at all events
*/
mode = 1;
if (ioctl(P_mfd, TIOCPKT, &mode) == -1) {
sysmessage(MSG_ERR,
"Can't put master pty in packet mode: %s\n",
strerror(errno));
return (-1);
}
type = Databuf[0];
parse_message(type, Databuf, retc);
return (0);
}
/*
* Packet mode routines
*/
void
parse_message(unsigned char type, char *buf, int size)
{
struct buffer *bp = Pty.inbuff;
if (size != 0) {
switch (Pty.state) {
case PTY_CLOSED:
case PTY_OPERRONLY:
SET_EVENT(EV_UP, EV_UPOPEN, 0, 0);
break;
}
}
else {
SET_EVENT(EV_UP, EV_UPCLOSE, 0, 0);
return;
}
# ifdef TSR_MEASURE
devreads++;
devrbytes += retc;
# endif
if (type == TIOCPKT_DATA) {
if (size == 0) {
SET_EVENT(EV_UP, EV_UPCLOSE, 0, 0);
}
else {
buf++;
size--;
/* V.1.0.1 fix: use buf instead of Dataptr */
COPY_TO_BUFFER(bp, buf, size);
SET_EVENT(EV_UP, EV_UPDATA, 0, 0);
}
}
else {
parse_packet((int) type);
}
}
int
dev_putdata(struct buffer *bp)
{
struct pty *pty = &Pty;
int ret;
int size;
/* XXXX TSR_MEASURES */
while (bp->b_hold) {
size = min(bp->b_hold, pty->iosize);
if ((ret = write(P_mfd, bp->b_rem, size)) == -1) {
if (errno == EAGAIN) {
ret = 0;
}
else {
sysmessage(MSG_ERR,
"Can't write on master pty: %s\n",
strerror(errno));
}
# ifdef TSR_MEASURE
devnwrites++;
# endif
return (ret);
}
# ifdef TSR_MEASURE
devwrites++;
devwbytes += ret;
# endif
FORWARD_BUFFER(bp, ret);
}
if (bp->b_hold == 0) {
RESET_BUFFER(bp);
}
return (0);
}
void
dev_interrupt(void)
{
int procid;
if ((procid = get_slave_controlling(P_devnumber)) > 0) {
(void) kill((pid_t) - procid, SIGINT);
}
}
void
dev_hangup(void)
{
int procid;
if ((procid = get_slave_controlling(P_devnumber)) > 0) {
(void) kill((pid_t) - procid, SIGHUP);
}
}
void
parse_packet(int type)
{
int flushbits;
int flushmode;
flushbits = type & (TIOCPKT_FLUSHREAD | TIOCPKT_FLUSHWRITE);
if (flushbits) {
switch (flushbits) {
case TIOCPKT_FLUSHREAD:
flushmode = OPFLUSH_IN;
break;
case TIOCPKT_FLUSHWRITE:
flushmode = OPFLUSH_OUT;
break;
default:
flushmode = OPFLUSH_IO;
break;
}
SET_EVENT(EV_UP, EV_UPFLUSH, (void *) &flushmode, sizeof(int));
}
}
/*
* Termio / Termios routines
*/
/* Termios must be clean */
void
portconfig_to_termios(struct portconfig *pcp, struct termios *tp)
{
/* Speed */
speed_t speed;
speed = int_to_baud_index(pcp->speed);
if (speed == B0)
speed = B115200;
cfsetospeed(tp, (speed_t) speed);
cfsetispeed(tp, (speed_t) B0);
/* Datasize */
switch (pcp->datasize) {
case 5:
tp->c_cflag |= CS5;
break;
case 6:
tp->c_cflag |= CS6;
break;
case 7:
tp->c_cflag |= CS7;
break;
case 8:
tp->c_cflag |= CS8;
break;
}
/* Stopsize */
if (pcp->stopsize == COM_SSIZE_TWO) {
tp->c_cflag |= CSTOPB;
} /* else one stop bit */
/* Parity */
switch (pcp->parity) {
case COM_PARITY_EVEN:
tp->c_cflag |= PARENB;
break;
case COM_PARITY_ODD:
tp->c_cflag |= PARENB | PARODD;
break;
case COM_PARITY_NONE:
default:
break;
}
/* Flow Control */
switch (pcp->flowc) {
case COM_FLOW_SOFT:
tp->c_iflag |= IXON;
break;
default:
break;
}
}
/*
* Argh => Get the process group id associated with slave pty
*/
int
get_slave_controlling(dev_t device)
{
DIR *dip;
struct dirent *dep;
int process;
char procfile[128];
char procbuf[512];
int procfd;
char dummybuf[512];
int dummyint;
int tty;
gid_t tpgid;
if ((dip = opendir("/proc")) == (DIR *) 0) {
sysmessage(MSG_ERR, "Can't open /proc: %s", strerror(errno));
exit(1);
}
while ((dep = readdir(dip)) != (struct dirent *) 0) {
process = atoi(dep->d_name);
if (process > 0) {
sprintf(procfile, "/proc/%d/stat", process);
if ((procfd = open(procfile, 0)) == -1) {
sysmessage(MSG_ERR, "Can't open %s:%s\n",
procfile, strerror(errno));
break;
}
if (read(procfd, procbuf, 512) <= 0) {
sysmessage(MSG_ERR, "Can't read %s:%s",
procfile, strerror(errno));
break;
}
(void) close(procfd);
if (sscanf(procbuf, "%d %s %c %d %d %d %d %u %s",
&dummyint, &dummybuf[0], (char *) &dummyint, &dummyint,
&dummyint, &dummyint, &tty, &tpgid,
&dummybuf[0]) != 0) {
if ((dev_t) tty == device) {
break;
}
}
tpgid = 0; /* not found */
}
}
(void) closedir(dip);
return (tpgid);
}
/*
* Debug routines
*/
char ioctlbuf[32];
#if 0
char *
ioctl_name(int type, void *unused)
{
const char *msgt;
if (Debug > 2) {
switch (type) {
case TCGETA:
msgt = "TCGETA";
break;
case TCSETA:
msgt = "TCSETA";
break;
case TCSETAW:
msgt = "TCSETAW";
break;
case TCSETAF:
msgt = "TCSETAF";
break;
case TCSBRK:
msgt = "TCSBRK";
break;
case TCXONC:
msgt = "TCXONC";
break;
case TCFLSH:
msgt = "TCFLSH";
break;
case TCGETS:
msgt = "TCGETS";
break;
case TCSETS:
msgt = "TCSETS";
break;
case TCSETSW:
msgt = "TCSETSW";
break;
case TCSETSF:
msgt = "TCSETSF";
break;
default:
msgt = "UNKNOWN";
break;
}
sprintf(ioctlbuf, "%s", msgt);
return (ioctlbuf);
}
else {
return ((char *) 0);
}
}
#endif