Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/talarm.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1999-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "sftest.h"
21
22
#define HANDLER "Handler"
23
char Buf[16];
24
int Except;
25
26
#if __STD_C
27
void alrmhandler(int sig)
28
#else
29
void alrmhandler(sig)
30
int sig;
31
#endif
32
{
33
strcpy(Buf,HANDLER);
34
35
if(Except == 0)
36
signal(sig,alrmhandler);
37
else if(Except == 1) /* testing return on interrupt */
38
{ Except = 2;
39
signal(sig,alrmhandler);
40
alarm(2);
41
}
42
else if(Except == 2)
43
{ twarn("System call was automatically resumed by the OS");
44
texit(0);
45
}
46
else terror("Unexpected Except(%d) state", Except);
47
}
48
49
#if __STD_C
50
int exceptf(Sfio_t* f, int type, Void_t* data, Sfdisc_t* disc)
51
#else
52
int exceptf(f, type, data, disc)
53
Sfio_t* f;
54
int type;
55
Void_t* data;
56
Sfdisc_t* disc;
57
#endif
58
{
59
if(type == SF_ATEXIT || type == SF_DPOP)
60
return 0;
61
62
if(type != SF_READ)
63
terror("Bad Io type %0o", type);
64
if(errno != EINTR)
65
terror("Bad exception %d", errno);
66
Except = -1;
67
68
return -1;
69
}
70
71
Sfdisc_t Disc = {NIL(Sfread_f), NIL(Sfwrite_f), NIL(Sfseek_f), exceptf};
72
73
tmain()
74
{
75
int fd[2];
76
ssize_t n;
77
char buf[128];
78
79
if(pipe(fd) < 0)
80
terror("Can't make pipe");
81
if(sfnew(sfstdin,NIL(Void_t*),(size_t)SF_UNBOUND,fd[0],SF_READ) != sfstdin)
82
terror("Can't renew stdin");
83
sfdisc(sfstdin,&Disc);
84
sfset(sfstdin,SF_SHARE,1);
85
86
Except = 0;
87
signal(SIGALRM,alrmhandler);
88
alarm(2);
89
if(sfreserve(sfstdin,1,SF_LOCKR))
90
terror("Unexpected data");
91
if(strcmp(Buf,HANDLER) != 0)
92
terror("Handler wasn't called");
93
if(Except >= 0)
94
terror("Exception handler wasn't called1");
95
96
Buf[0] = 0;
97
Except = 0;
98
signal(SIGALRM,alrmhandler);
99
alarm(2);
100
if(sfgetr(sfstdin,'\n',0))
101
terror("Unexpected data2");
102
if(strcmp(Buf,HANDLER) != 0)
103
terror("Handler wasn't called2");
104
if(Except >= 0)
105
terror("Exception handler wasn't called2");
106
107
Buf[0] = 0;
108
Except = 1; /* testing return-on-interrupt feature */
109
sfdisc(sfstdin, NIL(Sfdisc_t*)); /* pop discipline */
110
sfset(sfstdin, SF_IOINTR, 1); /* set to return on interrupt */
111
signal(SIGALRM,alrmhandler);
112
if(write(fd[1],"0123456789",10) != 10)
113
terror("Writing to pipe");
114
alarm(2);
115
if((n = sfread(sfstdin,buf,sizeof(buf))) != 10)
116
twarn("Wrong read size(%d) after an interrupt\n", n);
117
118
texit(0);
119
}
120
121