-
Notifications
You must be signed in to change notification settings - Fork 0
/
SePort.c
981 lines (846 loc) · 20.5 KB
/
SePort.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
/*
* This file is part of the Seyon, Copyright (c) 1992-1993 by Muhammad M.
* Saggaf. All rights reserved.
*
* See the file COPYING (1-COPYING) or the manual page seyon(1) for a full
* statement of rights and permissions for this program.
*/
/* -*- Mode: C -*-
* SePort.c --- Modem port I/O routines
* Author : Muhammad M. Saggaf
* Created On : sometime in 1992
* Last Modified By: system admin
* Last Modified On: Mon Jun 28 19:25:32 1993
* Update Count : 10
* Status : Mostly OK, needs some cleaning up
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <ttylock.h>
#include <X11/Intrinsic.h>
#if HAVE_TERMIOS
#include <termios.h>
#else
#if HAVE_TERMIO
#include <termio.h>
#else
#if HAVE_SGTTYB
#include <sys/ioctl.h>
#endif
#endif
#endif
#if LF_USE_DEV_NUMBERS
#include <sys/stat.h>
#ifdef SVR4
#include <sys/mkdev.h>
#else
#include <sys/sysmacros.h>
#endif /* SVR4 */
#endif /* LF_USE_DEV_NUMBERS */
#include "SeDecl.h"
#include "seyon.h"
#if !defined(O_NDELAY) && defined(O_NONBLOCK)
#define O_NDELAY O_NONBLOCK
#endif
#if !defined(MAX_INPUT) && defined(_POSIX_MAX_INPUT)
#define MAX_INPUT _POSIX_MAX_INPUT
#endif
extern char TtyReadChar();
extern int TtyReadStr(), TtyTimedReadChar(), TtyReadLine(), TtyTimedWaitFor(),
IoGetModemStat();
extern speed_t io_get_speed();
/*
* MDELAY is the delay in the output because (on my modem) the command would be
* ignored if sent at full speed. Change for other setups. (This setting is
* for U.S. Robotics Password Modem).
*/
#if HAVE_TERMIOS
static struct termios pmode; /* modem device control structure */
#else
#if HAVE_TERMIO
static struct termio pmode; /* modem device control structure */
#else
#if HAVE_SGTTYB
static struct sgttyb pmode;
#endif
#endif
#endif
char modem_port[REG_BUF]; /* modem port device file string */
static int mfd = -1; /* modem port file descriptor */
int baudrate = B9600; /* baud rate */
void MdmIFlush() {
int TtyIFlush();
TtyIFlush(mfd);
}
void MdmOFlush() {
int TtyOFlush();
TtyOFlush(mfd);
}
void MdmIOFlush() {
int TtyIOFlush();
TtyIOFlush(mfd);
}
void send_break() { io_send_break(mfd); }
void MdmPutString(s) char *s;
{
char c;
usleep(MDELAY);
for (; (c = *s); s++) {
if (*s == '^' && *(s + 1)) {
if (*(++s) == '^')
c = *s;
else
c = *s & 0x1f;
}
if (c == '~')
sleep(1);
else
send_tbyte(c);
usleep(MDELAY);
}
}
void mprintf(fmt, a, b, c) char *fmt, *a, *b, *c;
{ MdmPutString(FmtString(fmt, a, b, c)); }
void get_modem_attr() {
io_get_attr(mfd, &pmode);
baudrate = io_get_speed(&pmode);
}
void set_modem_attr() {
io_set_speed(&pmode, baudrate);
io_set_attr(mfd, &pmode);
}
int get_modem_fio() { return fcntl(mfd, F_GETFL); }
void set_modem_fio(fio) int fio;
{ fcntl(mfd, F_SETFL, fio); }
/*---------------------------------------------------------------------------+
| MdmSaveRestoreAttr - saves or restores the modem attributes.
+---------------------------------------------------------------------------*/
int MdmSaveRestoreAttr(action) int action;
{
#if HAVE_TERMIOS
static struct termios savedAttr;
#else
#if HAVE_TERMIO
static struct termio savedAttr;
#else
#if HAVE_SGTTYB
static struct sgttyb savedAttr;
#endif
#endif
#endif
if (mfd == -1)
return -1;
if (action == ATTR_SAVE)
return io_get_attr(mfd, &savedAttr);
else if (action == ATTR_RESTORE) {
/* Not sure how to do this with sgttyb */
#if HAVE_TERMIOS || HAVE_TERMIO
savedAttr.c_cflag &= ~HUPCL; /* don't hangup on closing */
#endif
return io_set_attr(mfd, &savedAttr);
}
return -1;
}
int GetModemStat(newModem) int newModem;
{
static Boolean useModemControl = True;
int retStat = 0;
if (newModem)
useModemControl = True;
if (useModemControl && ((retStat = IoGetModemStat(mfd)) < 0)) {
SeErrorF("Could not get control line status for device %s", modem_port, "",
"");
SeNotice("Disabling status toggles for that device");
useModemControl = False;
qres.ignoreModemDCD = True;
PopupError("errModemControl", NULL);
}
return useModemControl ? retStat : 0;
}
int Online() { return (GetModemStat(0) & MDM_DCD) ? 1 : 0; }
void cancel_dial(verbose) int verbose;
{
MdmPutString(qres.dialCancelString);
MdmPurge();
if (verbose)
SeyonMessage("Canceled");
}
void SetInitialModemAttr() {
#if HAVE_TERMIOS || HAVE_TERMIO
pmode.c_iflag |= (IGNBRK | IGNPAR);
pmode.c_iflag &= ~(ISTRIP | BRKINT);
pmode.c_lflag = 0;
#ifdef XCLUDE
pmode.c_lflag |= XCLUDE;
#endif
pmode.c_oflag &= ~OPOST; /* transparent output */
pmode.c_cflag = baudrate | CREAD | CLOCAL;
/* this many characters satisfy reads */
pmode.c_cc[VMIN] = min(qres.modemVMin, MAX_INPUT);
pmode.c_cc[VTIME] = 1; /* or in this many tenths of a second */
#else
#if HAVE_SGTTYB
pmode.sg_flags = RAW;
#endif
#endif
xc_setflow();
set_rtscts();
}
void set_rtscts() {
#ifdef CRTSCTS
if (qres.rtsctsFlowControl) {
pmode.c_cflag |= CRTSCTS;
#ifdef CNORTSCTS
pmode.c_cflag &= ~CNORTSCTS;
#endif
} else {
pmode.c_cflag &= ~CRTSCTS;
#ifdef CNORTSCTS
pmode.c_cflag |= CNORTSCTS;
#endif
}
#endif
if (mfd != -1)
io_set_attr(mfd, &pmode);
}
void xc_setflow() {
if (qres.xonxoffFlowControl) {
#if HAVE_TERMIOS
pmode.c_iflag |= IXON | IXOFF;
#else
#if HAVE_TERMIO
pmode.c_iflag |= IXON | IXOFF;
pmode.c_iflag &= ~IXANY;
#else
#if HAVE_SGTOBTYB
pmode.sg_flags |= TANDEM;
#endif
#endif
#endif
} else
#if HAVE_TERMIOS
pmode.c_iflag &= ~(IXON | IXOFF);
#else
#if HAVE_TERMIO
pmode.c_iflag &= ~(IXON | IXOFF);
pmode.c_iflag &= ~IXANY;
#else
#if HAVE_SGTTYB
pmode.sg_flags &= ~TANDEM;
#endif
#endif
#endif
if (mfd != -1)
io_set_attr(mfd, &pmode);
}
char *mport(s) /* get/set port string */
char *s;
{
if (s != NULL)
strncpy(modem_port, s, sizeof(modem_port));
return modem_port;
}
int MdmSetGetBaud(baudIndex) int baudIndex;
{
static char *baud[] = {"0", "300", "1200", "2400", "4800",
"9600", "19200", "38400", "57600", "115200",
"230400", "460800", NULL};
long retBaud;
if (baudIndex != -1)
retBaud = mbaud(baud[baudIndex]);
else
retBaud = mbaud(NULL);
for (baudIndex = 0; baud[baudIndex]; baudIndex++)
if (atol(baud[baudIndex]) == retBaud)
return baudIndex;
return -1;
}
int MdmSetGetCSize(bits) int bits;
{
if (bits != -1) {
switch (bits) {
#if HAVE_TERMIOS || HAVE_TERMIO
case 5:
pmode.c_cflag &= ~CSIZE;
pmode.c_cflag |= CS5;
MdmSetGetIStrip(1);
break;
case 6:
pmode.c_cflag &= ~CSIZE;
pmode.c_cflag |= CS6;
MdmSetGetIStrip(1);
break;
case 7:
pmode.c_cflag &= ~CSIZE;
pmode.c_cflag |= CS7;
MdmSetGetIStrip(1);
break;
case 8:
pmode.c_cflag &= ~CSIZE;
pmode.c_cflag |= CS8;
MdmSetGetIStrip((int)qres.stripHighBit);
break;
#else
#if HAVE_SGTTYB
case 5:
case 6:
case 7:
pmode.sg_flags |= CBREAK;
pmode.sg_flags &= ~RAW;
MdmSetGetIStrip(0);
break;
case 8:
pmode.sg_flags |= RAW;
pmode.sg_flags &= ~CBREAK;
MdmSetGetIStrip(0);
break;
#endif
#endif
default:
SeErrorF("invalid number of bits: %d", bits, "", "");
return -1;
}
/* io_set_attr is called in MdmSetGetIStrip */
}
if (mfd != -1)
io_get_attr(mfd, &pmode);
#if HAVE_TERMIOS || HAVE_TERMIO
switch (pmode.c_cflag & CSIZE) {
case CS5:
return 5;
case CS6:
return 6;
case CS7:
return 7;
case CS8:
return 8;
default:
SeError("Consistency error in number of bits");
return -1;
#else
#if HAVE_SGTTYB
switch (pmode.sg_flags & CBREAK) {
case 0:
return 8;
default:
return 7;
#endif
#endif
}
}
int MdmSetGetParity(parity) int parity;
{
if (parity != -1) {
switch (parity) {
#if HAVE_TERMIOS || HAVE_TERMIO
case 0:
pmode.c_cflag &= ~PARENB;
break;
case 1:
pmode.c_cflag |= (PARENB | PARODD);
break;
case 2:
pmode.c_cflag |= PARENB;
pmode.c_cflag &= ~PARODD;
break;
#else
#if HAVE_SGTTYB
case 0:
pmode.sg_flags &= ~ANYP;
break;
case 1:
pmode.sg_flags |= ODDP;
pmode.sg_flags &= ~EVENP;
break;
case 2:
pmode.sg_flags |= EVENP;
pmode.sg_flags &= ~ODDP;
break;
#endif
#endif
default:
SeErrorF("Invalid parity: %d", parity, "", "");
return -1;
}
if (mfd != -1)
io_set_attr(mfd, &pmode);
}
if (mfd != -1)
io_get_attr(mfd, &pmode);
#if HAVE_TERMIOS || HAVE_TERMIO
if (!(pmode.c_cflag & PARENB))
return 0;
else if (pmode.c_cflag & PARODD)
return 1;
else
return 2;
#else
#if HAVE_SGTTYB
switch (pmode.sg_flags & ANYP) {
case ODDP:
return 1;
case EVENP:
return 2;
default:
return 0;
}
#endif
#endif
}
int MdmSetGetIStrip(flag) int flag;
{
#if HAVE_TERMIOS || HAVE_TERMIO
if (flag != -1) {
if (flag)
pmode.c_iflag |= ISTRIP;
else
pmode.c_iflag &= ~ISTRIP;
if (mfd != -1)
io_set_attr(mfd, &pmode);
}
if (mfd != -1)
io_get_attr(mfd, &pmode);
if (pmode.c_iflag & ISTRIP)
return 1;
else
return 0;
#else
#if !HAVE_SGTTYB
if (mfd != -1)
io_set_attr(mfd, &pmode);
return 0;
#endif
#endif
}
int MdmSetGetStopBits(bits) int bits;
{
#if HAVE_TERMIOS || HAVE_TERMIO
if (bits != -1) {
switch (bits) {
case 1:
pmode.c_cflag &= ~CSTOPB;
break;
case 2:
pmode.c_cflag |= CSTOPB;
break;
default:
SeErrorF("invalid number of stop bits: %d", bits, "", "");
return -1;
}
if (mfd != -1)
io_set_attr(mfd, &pmode);
}
if (mfd != -1)
io_get_attr(mfd, &pmode);
if (pmode.c_cflag & CSTOPB)
return 2;
else
return 1;
#else
#if !HAVE_SGTTYB
if (mfd != -1)
io_set_attr(mfd, &pmode);
return 1;
#endif
#endif
}
/*
* Get/set the baud rate of the modem port. If the port hasn't been opened
* yet, just store in pmode for mopen() to use when it opens the port.
*/
long mbaud(s) char *s;
{
if (s != NULL) {
/* this gives a more limited, realistic */
switch (atol(s)) { /* range than in sgtty.h */
case 300:
baudrate = B300;
break;
case 1200:
baudrate = B1200;
break;
case 2400:
baudrate = B2400;
break;
case 4800:
baudrate = B4800;
break;
case 9600:
baudrate = B9600;
break;
case 19200:
baudrate = B19200;
break;
case 38400:
baudrate = B38400;
break;
case 57600:
baudrate = B57600;
break;
case 115200:
baudrate = B115200;
break;
case 230400:
baudrate = B230400;
break;
case 460800:
baudrate = B460800;
break;
default:
return -1;
}
io_set_speed(&pmode, baudrate);
if (mfd != -1)
io_set_attr(mfd, &pmode);
}
if (mfd != -1)
io_get_attr(mfd, &pmode);
switch (io_get_speed(&pmode)) {
case B300:
return 300;
case B1200:
return 1200;
case B2400:
return 2400;
case B4800:
return 4800;
case B9600:
return 9600;
case B19200:
return 19200;
case B38400:
return 38400;
case B57600:
return 57600;
case B115200:
return 115200;
case B230400:
return 230400;
case B460800:
return 460800;
}
SeError("Consistency error in baud rate");
return -1;
}
/*
* The following routine is used to hang up the modem. This is accomplished
* by setting the baud rate to 0. According to my documentation on termio,
* setting the baud rate to zero will result in DTR not being asserted. This
* hangs up some (most?) modems. If not, the second part of the routine
* sends the Hayes modem "escape" and then a hangup command.
*/
void MdmHangup() {
int terminalWasActive;
if (mfd == -1)
return;
terminalWasActive = SuspContTerminal(TERM_SUSPEND);
if (qres.hangupViaDTR) {
io_set_speed(&pmode, B0); /* set baud 0 (drop DTR) */
io_set_attr(mfd, &pmode);
sleep(1); /* wait a second */
io_set_speed(&pmode, baudrate); /* reset baud rate */
io_set_attr(mfd, &pmode);
} else { /* use Hayes command */
sleep(2); /* allow for "escape guard time" */
MdmPutString(qres.modemAttentionString); /* send modem escape command */
sleep(3); /* more "escape guard time" */
MdmPutString(qres.modemHangupString); /* send hangup command */
}
MdmPurge();
if (terminalWasActive)
SuspContTerminal(TERM_CONTINUE);
UpdateStatusBox(NULL);
}
/*
* Opens the modem port and configures it. Returns 0 for success or -1 on
* error.
*/
int OpenModem(modem) String modem;
{
int LockModem(), UnlockModem();
void MdmIOFlush();
int oldFlags;
if (modem == NULL || modem[0] == '\0')
return ERR_MDM_NOMODEM;
if (LockModem(modem))
return ERR_MDM_LOCKED;
/* Need O_NDELAY to get the file open before we have carrier */
if ((mfd = open(modem, O_RDWR | O_NDELAY)) < 0) {
UnlockModem(modem);
return ERR_MDM_OPENFAILED;
}
/* Now, we must reset the O_NDELAY mode so that read() works correctly */
if (((oldFlags = fcntl(mfd, F_GETFL, 0)) == -1) ||
(fcntl(mfd, F_SETFL, oldFlags & ~O_NDELAY) == -1)) {
UnlockModem(modem);
return ERR_MDM_RESETFLAGSFAILED;
}
#if HAVE_SGTTYB
if (ioctl(mfd, TIOCEXCL) < 0)
SePError("exclusive-use");
#endif
mport(modem);
MdmIOFlush();
MdmSaveRestoreAttr(ATTR_SAVE);
SetInitialModemAttr();
io_set_attr(mfd, &pmode);
GetModemStat(1);
if (mbaud(qres.defaultBPS) == -1)
se_warningf("invalid default BPS value: %s", qres.defaultBPS, "", "");
if (MdmSetGetCSize(qres.defaultBits) < 0)
se_warningf("invalid default number of bits: %s", qres.defaultBits, "", "");
if (MdmSetGetParity(qres.defaultParity) < 0)
se_warningf("invalid default parity value: %s", qres.defaultParity, "", "");
if (MdmSetGetStopBits(qres.defaultStopBits) < 0)
se_warningf("invalid default number of stop bits: %s", qres.defaultStopBits,
"", "");
return 0;
}
void ShowOpenModemErrMsg(modemName, retStatus) String modemName;
int retStatus;
{
switch (retStatus) {
case ERR_MDM_NOMODEM:
SeError("No Modem Specified");
break;
case ERR_MDM_LOCKED:
SeError(FmtString("Modem ``%s'' is Locked", modemName, "", ""));
break;
case ERR_MDM_OPENFAILED:
SePError(FmtString("Unable to Open Modem ``%s''", modemName, "", ""));
break;
case ERR_MDM_RESETFLAGSFAILED:
SePError(
FmtString("Unable to Reset Flags for Modem ``%s''", modemName, "", ""));
break;
default:
SeError(FmtString("Unknown Error While Opening Modem ``%s''", modemName, "",
""));
break;
}
}
int CloseModem() {
if (mfd == -1)
return -1;
MdmSaveRestoreAttr(ATTR_RESTORE);
close(mfd);
return 0;
}
/*
* Attach standard input and output to the modem port. This only gets called
* after a fork by the child process; which then exec's a program that uses
* standard i/o for some data transfer protocol. (To put this here is
* actually a kludge, but I wanted to keep the modem-specific stuff in a
* black box.)
*/
void mattach() {
extern int dup2();
/*
* attach standard i/o to port
*/
dup2(mfd, 0); /* close local stdin and connect to port */
dup2(mfd, 1); /* close local stdout and connect to port */
close(mfd); /* close the old port descriptor */
}
/* ------------------------------------------------------------
* Routines to read from the modem.
*/
/*
* MdmReadStr: reads a bunch of characters from the modem.
*/
int MdmReadStr(buf) char *buf;
{ return TtyReadStr(mfd, buf); }
/*
* MdmReadChar: reads one character from the modem.
*/
char MdmReadChar(readChar) char *readChar;
{ return TtyReadChar(mfd, readChar); }
int MdmTimedReadChar(readChar, expireTime) char *readChar;
int expireTime;
{ return TtyTimedReadChar(mfd, readChar, expireTime); }
/*
* MdmReadLine: reads one line from the modem.
*/
int MdmReadLine(buf) char *buf;
{ return TtyReadLine(mfd, buf); }
int MdmTimedWaitFor(expectedString, waitTime) char *expectedString;
int waitTime;
{ return TtyTimedWaitFor(mfd, expectedString, waitTime); }
/*
* MdmPurge: throws away all incoming characters until no more are sent.
*/
void MdmPurge() {
char c;
while (MdmTimedReadChar(&c, 1) >= 0)
;
}
#ifdef retired
int readbyte(seconds) int seconds;
{ return trminp(mfd, seconds); }
#endif
/*
* Output a byte to the modem port. All data sent to the modem
* is output through this routine.
*/
void sendbyte(ch) int ch;
{
char c = ch & 0xff;
if (write(mfd, &c, 1) < 0)
SePError("character write");
}
void sendf_slowly(fmt, a, b, c) char *fmt, *a, *b, *c;
{ send_slowly(FmtString(fmt, a, b, c)); }
void send_slowly(s) char *s;
{
while (*s) {
sendbyte(*s++);
/*
* avoid busy waiting by using usleep if available. adjust MDELAY if
* you're having trouble
*/
usleep(MDELAY);
}
}
/*
* lock_tty() returns non-zero if the lock file exists (prevents Seyon from
* running).
*
* unlock_tty() deletes the lock file.
*
* Simple, eh?
*/
char lckf[SM_BUF];
char ltmp[SM_BUF];
pid_t lockPid;
int LockModem(modem) String modem;
{
int res = ttylock(modem);
if (res) {
SePErrorF("Modem locked: PID %d\n", res);
return 1;
} else
return 0;
}
int UnlockModem(modem) String modem;
{
int res = ttyunlock(modem);
if (res) {
SePErrorF("Modem unlocked: PID %d\n", res);
return 1;
} else
return 0;
}
int lock_tty() {
int lfd;
pid_t pid, lckpid;
char *modemname;
#if LF_USE_ASCII_PID
char pidstr[20], lckpidstr[20];
int nb;
#endif
#if LF_USE_DEV_NUMBERS
struct stat mbuf;
#endif
/* Get our PID, and initialize the filename strings */
pid = getpid();
#if !LF_USE_DEV_NUMBERS
modemname = strrchr(modem_port, '/');
if (modemname) {
if (SM_BUF > (1 + strlen(LF_PATH) + strlen(LF_PREFIX) + strlen(modemname)))
sprintf(lckf, "%s/%s%s", LF_PATH, LF_PREFIX, (modemname + 1));
else {
SePErrorF("Buffer too small for lock filename in lock_tty(): %s",
modemname, "", "");
return -1;
}
} else {
if (SM_BUF > (1 + strlen(LF_PATH) + strlen(LF_PREFIX) + strlen(modem_port)))
sprintf(lckf, "%s/%s%s", LF_PATH, LF_PREFIX, (modem_port));
else {
SePErrorF("Buffer too small for lock filename in lock_tty(): %s",
modem_port, "", "");
return -1;
}
}
#else
if (stat(modem_port, &mbuf) < 0) {
SePErrorF("could not stat modem port %s", modem_port, "", "");
return -1;
}
if (SM_BUF > (10 + strlen(LF_PATH) + strlen(LF_PREFIX)))
sprintf(lckf, "%s/%s%03u.%03u.%03u", LF_PATH, LF_PREFIX, major(mbuf.st_dev),
major(mbuf.st_rdev), minor(mbuf.st_rdev));
else {
SePErrorF("Buffer too small for lock filename in lock_tty():", "", "", "");
return -1;
}
#endif /* LF_USE_DEV_NUMBERS */
if (SM_BUF > (11 + strlen(LF_PATH)))
sprintf(ltmp, "%s/%s%d", LF_PATH, "LTMP.", pid);
else {
SePErrorF("Buffer too small for ltmp filename in lock_tty():", "", "", "");
return -1;
}
/* Create the LTMP.<pid> file and scribble our PID in it */
unlink(ltmp);
if ((lfd = creat(ltmp, 0644)) == -1) {
SePErrorF("Could not create temporary lock file %s", ltmp, "", "");
return -1;
}
#if LF_USE_ASCII_PID
/* pidstr is easily large enough */
sprintf(pidstr, "%10d\n", pid);
write(lfd, pidstr, 11);
#else
write(lfd, (char *)&pid, sizeof(pid));
#endif
close(lfd);
/*
* Attempt to link directly - if it works, we're done.
*/
relink:
if (link(ltmp, lckf) == 0) {
unlink(ltmp);
lockPid = pid;
return 0;
}
/*
* Oh brother, there's a LCK..* file already; we must now expend effort to
* learn if it's stale or not.
*/
if ((lfd = open(lckf, O_RDONLY)) != -1) {
#if LF_USE_ASCII_PID
for (nb = 0; nb < 20 && read(lfd, lckpidstr + nb, sizeof(char)); nb++)
;
if (nb) {
lckpid = atol(lckpidstr);
#else
if (read(lfd, (char *)&lckpid, sizeof(lckpid)) == sizeof(lckpid)) {
#endif
lockPid = (pid_t)lckpid;
if (kill(lckpid, 0) == 0 || errno != ESRCH) {
SeErrorF("Device %s is locked by process %d", modem_port, lckpid, "");
unlink(ltmp);
return -1;
}
}
}
/*
* The LCK..* file was stale. Remove and retry.
*/
if (unlink(lckf)) {
SePErrorF("Unable to unlink stale lock file \"%s\"", lckf, "", "");
unlink(ltmp);
return -1;
}
goto relink;
/* NOTREACHED */
}
void unlock_tty() {
/* Don't remove the lock file unless it's the one we created */
if (getpid() == lockPid)
unlink(lckf);
}