-
Notifications
You must be signed in to change notification settings - Fork 27
/
xread.c
654 lines (531 loc) · 14.7 KB
/
xread.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
/*
* Changes for use with CM11A copyright 1996, 1997 - 2003 Daniel B. Suthers,
* Pleasanton Ca, 94588 USA
* E-mail [email protected]
*
*/
/*
* Copyright 1986 by Larry Campbell, 73 Concord Street, Maynard MA 01754 USA
* (maynard!campbell).
*
* John Chmielewski (tesla!jlc until 9/1/86, then rogue!jlc) assisted
* by doing the System V port and adding some nice features. Thanks!
*/
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <signal.h>
#include <setjmp.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_SYS_ERRNO_H
#include <sys/errno.h>
#endif
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "x10.h"
#ifdef HAVE_STRING_H
#include <string.h> /* char *strerror(); */
#endif
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "process.h"
extern int verbose;
extern int i_am_relay, i_am_aux;
extern int tty;
unsigned alarm();
void sigtimer( int );
#ifdef HAVE_SETITIMER
struct itimerval iold, icurrent;
#endif
int xread ( int, unsigned char *, int, int );
/*
* xread(fd, buf, count, timeout)
*
* Timed read. Works just like read(2) but gives up after
* timeout seconds, returning whatever's been read so far.
*/
/* NOTE: The CM11A will pop out a poll message every second when it wants
* to send an update. Alarm(1) will go off when the next second starts.
* This may be any amount of time up to 1 second. This makes a normal
* alarm(1) unsuitable. Thus the use of setitimer. Other OSes will have
* similar capabilities that assure a 1 second alarm is actually one second.
*/
jmp_buf jb;
int xread(fd, buf, count, timeout)
int fd, count, timeout;
unsigned char *buf;
{
static int total; /* so setjump won't clobber it */
extern int i_am_relay;
int counter;
unsigned char *cbuf;
char RCSID[]= "@(#) $Id: xread.c,v 1.10 2003/03/17 01:40:32 dbs Exp dbs $\n";
display(RCSID);
if (verbose && (! i_am_relay) ) {
fprintf(stderr, "xread() called, count=%d, timeout = %d\n", count, timeout);
fflush(stderr);
}
total = 0;
if (setjmp(jb))
{
if (verbose && (! i_am_relay) ) {
fprintf(stderr, "xread() returning %d after timeout\n", total);
fflush(stderr);
}
(void) alarm(0);
(void) signal(SIGALRM, SIG_IGN);
return (total);
}
/* The compiler would complain that
* "argument `buf' and count might be clobbered by `longjmp'"
* This is bacause we change them after the setjmp.
* To prevent this, we use copies of the arguements.
*/
cbuf = buf;
counter = count;
(void)alarm(0);
(void) signal(SIGALRM, sigtimer);
#ifdef HAVE_SETITIMER
icurrent.it_interval.tv_sec = 0;
icurrent.it_interval.tv_usec = 0;
icurrent.it_value.tv_sec = timeout;
icurrent.it_value.tv_usec = 30;
if (setitimer(ITIMER_REAL, &icurrent, &iold) == -1)
if( !i_am_relay )
perror ("setitimer");
#else
(void) alarm((unsigned) timeout);
#endif
errno = 0;
while (counter--) { /* loop till all characters are read */
int i;
if ((i = read(fd, (char *)cbuf, 1)) < 1) {
if (i == 0) {
/* loop around on EOF */
counter++; /* reverse the decrement in the while loop. */
if( errno == EINTR) /* alarm was triggered */
{
break;
}
continue;
}
if( (i < 0) && (i_am_relay != 1) )
perror("read");
else
if( (i < 0) && (i_am_relay == 1) )
{
syslog(LOG_ERR,"Relay Xread read error");
syslog(LOG_ERR, "%s", strerror(errno));
}
(void) alarm(0);
(void) signal(SIGALRM, SIG_IGN);
if (verbose) {
fprintf(stderr, "xread() returning %d bytes after error\n", total);
fflush(stderr);
}
return (total);
}
cbuf++;
total++;
}
(void) alarm(0);
(void) signal(SIGALRM, SIG_IGN);
if (verbose && (i_am_relay != 1) ) {
if ( total == 0 )
fprintf(stderr, "xread() returning 0 bytes\n");
else
fprintf(stderr, "xread() returning %d byte(s). The first is 0x%02x\n", total, buf[0] );
fflush(stderr);
}
return (total);
}
void sigtimer( int signo )
{
if (verbose && (! i_am_relay) ){
fprintf(stderr, "Alarm - timeout\n");
fflush(stderr);
}
(void) signal(SIGALRM, sigtimer);
errno = EINTR;
return;
/* the jmp temporarily disabled (version 1.34)
... It was causing problems when triggered under linux 2.2.20
using USB. The second trap would not behave properly.
I don't know why.
*/
/* longjmp(jb, 1); */
}
/* expect xread sends a code to the spool file that specifys how many
* incoming characters are to be discarded.
* The code is 3 0xff followed by (the number of bytes to expect +127)
*/
int exread(fd, buf, count, timeout)
int fd;
char *buf;
int count;
int timeout;
{
unsigned char lbuf[160];
extern int sptty;
lbuf[1] = lbuf[2] = lbuf[0] = 0xff;
lbuf[3] = count+127;
if ( (!i_am_relay || i_am_aux) && write(sptty, (char *)lbuf , 4) != 4 )
{
/* if( i_am_relay )
{
syslog (LOG_ERR, "exread(): write failed\n");
}
else */
if ( !i_am_relay && !i_am_aux )
{
fprintf (stderr, "exread(): write failed\n");
}
}
return(xread( fd, (unsigned char *)buf, count, timeout));
}
int is_blocking ( int tty )
{
int flags;
flags = fcntl(tty, F_GETFL);
if ( flags & O_NONBLOCK )
return 0;
return 1;
}
int file_flags ( void )
{
return fcntl(tty, F_GETFL);
}
int setblocking ( int fd, int code )
{
int flags;
flags = fcntl(fd, F_GETFL);
if ( code == 0 )
flags |= O_NONBLOCK;
else
flags &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
return flags;
}
void sxread_isr_1( int signo )
{
return;
}
#if defined(HAVE_SIGACTION)
/*--------------------------------------------------------+
| An interruptable read() like xread(), but from a |
| serial port file descriptor. Needs sigaction() |
+--------------------------------------------------------*/
int sxread ( int fd, unsigned char *buffer, int size, int timeout )
{
extern void cleanup_files();
extern void cleanup_aux(void);
int count = 0, nread = 0, passes = 0;
struct sigaction act;
if ( fd == TTY_DUMMY ) {
sleep(timeout);
return 0;
}
errno = 0;
setblocking(fd, 1);
sigaction(SIGALRM, NULL, &act);
act.sa_handler = sxread_isr_1;
act.sa_flags &= ~SA_RESTART;
sigaction(SIGALRM, &act, NULL);
alarm(timeout);
while ( nread < size && passes++ < 100 ) {
count = read(fd, (char *)(buffer + nread), size - nread);
if ( count < 0 )
break;
nread += count;
}
alarm(0);
act.sa_handler = SIG_IGN;
act.sa_flags |= SA_RESTART;
sigaction(SIGALRM, &act, NULL);
if ( count >= 0 || (errno == EINTR) )
return nread;
syslog(LOG_ERR, "sxread() failed, error = %s\n", strerror(errno));
if ( i_am_aux )
cleanup_aux();
cleanup_files();
exit(1);
return -1;
}
#elif defined(HAVE_SIGINTERRUPT)
/*--------------------------------------------------------+
| An interruptable read() like xread(), but from a |
| serial port file descriptor. Needs siginterrupt() |
+--------------------------------------------------------*/
int sxread ( int fd, unsigned char *buffer, int size, int timeout )
{
extern void cleanup_files();
extern void cleanup_aux(void);
int count = 0, nread = 0, passes = 0;
if ( fd == TTY_DUMMY ) {
sleep(timeout);
return 0;
}
errno = 0;
setblocking(fd, 1);
siginterrupt(SIGALRM, 1);
signal(SIGALRM, sxread_isr_1);
alarm(timeout);
while ( size > nread && passes++ < 100 ) {
count = read(fd, (char *)(buffer + nread), size - nread);
if ( count < 0 )
break;
nread += count;
}
alarm(0);
siginterrupt(SIGALRM, 0);
if ( count >= 0 || (count < 0 && errno == EINTR) )
return nread;
syslog(LOG_ERR, "sxread() failed, error = %s\n", strerror(errno));
if ( i_am_aux )
cleanup_aux();
cleanup_files();
exit(1);
return -1;
}
#elif defined(SIGKLUGE)
static int s_fd;
void sxread_isr_2( int signo )
{
setblocking(s_fd, 0);
return;
}
/*--------------------------------------------------------+
| An interruptable read() like xread(), but from a |
| serial port file descriptor. This one uses a kluge |
| whereby the port is changed from blocking to non- |
| blocking after the alarm timeout. |
+--------------------------------------------------------*/
int sxread ( int fd, unsigned char *buffer, int size, int timeout )
{
extern void cleanup_files();
extern void cleanup_aux(void);
int count = 0, nread = 0, passes = 0;
if ( fd == TTY_DUMMY ) {
sleep(timeout);
return 0;
}
s_fd = fd;
alarm(0);
signal(SIGALRM, sxread_isr_2);
alarm(timeout);
while ( size > nread && passes++ < 100 ) {
errno = 0;
setblocking(fd, 1);
count = read(fd, (char *)(buffer + nread), size - nread);
if ( count < 0 ) {
if ( errno == EINTR || errno == EAGAIN ) {
break;
}
else {
continue;
}
}
nread += count;
}
alarm(0);
setblocking(fd, 1);
if ( count >= 0 || (errno == EINTR || errno == EAGAIN) )
return nread;
syslog(LOG_ERR, "sxread() failed, error = %s\n", strerror(errno));
if ( i_am_aux )
cleanup_aux();
cleanup_files();
exit(1);
return -1;
}
#else
/*--------------------------------------------------------+
| This version of sxread polls until the characters are |
| read or timeout. Timeout is not very accurate - can |
| take twice as long as specified with older kernels. |
+--------------------------------------------------------*/
int sxread ( int fd, unsigned char *buffer, int size, int timeout )
{
extern void cleanup_files();
extern void cleanup_aux();
int count = 0, nread = 0, passes = 0;
if ( fd == TTY_DUMMY ) {
sleep(timeout);
return 0;
}
setblocking(fd, 0);
passes = 100 * timeout;
while ( size > nread && --passes > 0 ) {
errno = 0;
count = read(fd, (char *)(buffer + nread), size - nread);
if ( count <= 0 ) {
millisleep(10);
continue;
}
nread += count;
}
setblocking(fd, 1);
if ( count >= 0 || errno == EAGAIN )
return nread;
syslog(LOG_ERR, "sxread() failed, error = %s\n", strerror(errno));
if ( i_am_aux )
cleanup_aux();
cleanup_files();
exit(1);
return -1;
}
#endif
#if 0
int timeout_flag = 0;
void xread_sigtimer ( int signo )
{
timeout_flag = 1;
errno = EINTR;
return;
}
/*
* An experimental version of xread()
*/
int xread ( int fd, unsigned char *buffer, int count, int timeout )
{
int nread, ntot = 0;
if ( fd == TTY_DUMMY ) {
sleep(timeout);
return 0;
}
if ( verbose && (! i_am_relay) ) {
fprintf(stderr, "xread() called, count=%d, timeout = %d\n", count, timeout);
fflush(stderr);
}
timeout_flag = 0;
(void)alarm(0);
(void) signal(SIGALRM, xread_sigtimer);
#ifdef HAVE_SETITIMER
icurrent.it_interval.tv_sec = 0;
icurrent.it_interval.tv_usec = 0;
icurrent.it_value.tv_sec = timeout;
icurrent.it_value.tv_usec = 30;
if ( setitimer(ITIMER_REAL, &icurrent, &iold) == -1 ) {
if( !i_am_relay )
perror ("setitimer");
}
#else
(void) alarm((unsigned) timeout);
#endif
errno = 0;
while ( ntot < count && timeout_flag == 0 ) {
nread = read(fd, (char *)(buffer + ntot), count - ntot);
if ( nread < 0 ) {
syslog(LOG_ERR, "xread: read() error %s\n", strerror(errno));
if ( verbose && !i_am_relay ) {
fprintf(stderr, "xread() returning %d bytes after error\n", ntot);
fflush(stderr);
}
alarm(0);
signal(SIGALRM, SIG_IGN);
return ntot;
}
ntot += nread;
if ( nread == 0 )
millisleep(10);
}
if ( verbose && !i_am_relay ) {
if ( ntot == 0 )
fprintf(stderr, "xread() returning 0 bytes\n");
else
fprintf(stderr, "xread() returning %d byte(s). The first is 0x%02x\n", ntot, buffer[0]);
fflush(stderr);
}
alarm(0);
signal(SIGALRM, SIG_IGN);
return ntot;
}
#endif
#if 0
/*
* This is an alternate experimental version of xread which does not use
* a signal for timeout.
*/
int xread ( int fd, unsigned char *buffer, int count, int timeout )
{
int nread, ntot = 0;
long maxpasses, passes = 0;
time_t tquit;
if ( fd == TTY_DUMMY ) {
sleep(timeout);
return 0;
}
if ( verbose && (! i_am_relay) ) {
fprintf(stderr, "xread() called, count=%d, timeout = %d\n", count, timeout);
fflush(stderr);
}
maxpasses = timeout * 100; /* at nominal 10 msec/pass */
/* System clock is checked since the timing resolution with older kernels */
/* is such that the timout interval could otherwise be twice as long as */
/* specified */
tquit = time(NULL) + (time_t)(timeout + 1);
errno = 0;
while ( ntot < count && passes < maxpasses && time(NULL) < tquit ) {
nread = read(fd, (char *)(buffer + ntot), count - ntot);
if ( nread < 0 ) {
syslog(LOG_ERR, "xread: read() error %s\n", strerror(errno));
if ( verbose ) {
fprintf(stderr, "xread() returning %d bytes after error\n", ntot);
fflush(stderr);
}
return ntot;
}
ntot += nread;
passes++;
if ( nread == 0 )
millisleep(10);
}
if ( verbose && (i_am_relay != 1) ) {
if ( ntot == 0 )
fprintf(stderr, "xread() returning 0 bytes\n");
else
fprintf(stderr, "xread() returning %d byte(s). The first is 0x%02x\n", ntot, buffer[0]);
fflush(stderr);
}
return ntot;
}
#endif