Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libcs/csserve.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1990-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
#pragma prototyped
21
22
/*
23
* Glenn Fowler
24
* AT&T Research
25
*
26
* obsolete generic server state machine
27
* <css.h> provides a discipline interface with multiple servers
28
*/
29
30
#include "csslib.h"
31
32
struct Server_s
33
{
34
Cssdisc_t disc;
35
Css_t* css;
36
void* handle;
37
int (*done)(void*, int);
38
int (*con)(void*, int, Csid_t*, int, char**);
39
int (*rd)(void*, int);
40
int (*wr)(void*, int);
41
int (*to)(void*);
42
};
43
44
static int
45
acceptf(Css_t* css, Cssfd_t* fp, Csid_t* ip, char** av, Cssdisc_t* disc)
46
{
47
register Server_t* server = (Server_t*)disc;
48
49
return (*server->con)(server->handle, fp->fd, ip, 0, av) ? -1 : fp->fd;
50
}
51
52
static int
53
actionf(Css_t* css, Cssfd_t* fp, Cssdisc_t* disc)
54
{
55
register Server_t* server = (Server_t*)disc;
56
57
switch (fp->status)
58
{
59
case CS_POLL_READ:
60
if (server->rd)
61
return (*server->rd)(server->handle, fp->fd) < 0 ? -1 : 1;
62
break;
63
case CS_POLL_WRITE:
64
if (server->wr)
65
return (*server->wr)(server->handle, fp->fd) < 0 ? -1 : 1;
66
break;
67
}
68
return 0;
69
}
70
71
static int
72
exceptf(Css_t* css, unsigned long op, unsigned long arg, Cssdisc_t* disc)
73
{
74
register Server_t* server = (Server_t*)disc;
75
76
switch (op)
77
{
78
case CSS_CLOSE:
79
if (server->done)
80
(*server->done)(server->handle, 0);
81
return 0;
82
case CSS_INTERRUPT:
83
if (server->done && !(*server->done)(server->handle, EXIT_TERM(arg)))
84
return 1;
85
error(ERROR_SYSTEM|3, "%s: interrupt exit", fmtsignal(arg));
86
return -1;
87
case CSS_DORMANT:
88
case CSS_TIMEOUT:
89
case CSS_WAKEUP:
90
return !server->to ? 0 : (*server->to)(server->handle) < 0 ? -1 : 1;
91
}
92
error(ERROR_SYSTEM|3, "poll error [op=%lu arg=%lu]", op, arg);
93
return -1;
94
}
95
96
int
97
csfd(Cs_t* state, int fd, int op)
98
{
99
return !state->server || cssfd(state->server->css, fd, op) ? -1 : 0;
100
}
101
102
/*
103
* csserve() wakeup and timeout are mutually exclusive
104
*/
105
106
unsigned long
107
cstimeout(register Cs_t* state, unsigned long ms)
108
{
109
unsigned long rv;
110
111
if (!state->server)
112
return CS_NEVER;
113
state->server->css->disc->wakeup = 0;
114
rv = state->server->css->disc->timeout;
115
state->server->css->disc->timeout = ms;
116
return rv;
117
}
118
119
/*
120
* csserve() wakeup and timeout are mutually exclusive
121
*/
122
123
unsigned long
124
cswakeup(Cs_t* state, unsigned long ms)
125
{
126
unsigned long rv;
127
128
if (!state->server)
129
return CS_NEVER;
130
state->server->css->disc->timeout = 0;
131
rv = state->server->css->disc->wakeup;
132
state->server->css->disc->wakeup = ms;
133
return rv;
134
}
135
136
/*
137
* server state machine
138
*/
139
140
void
141
csserve(Cs_t* state, void* handle, const char* path, void* (*init)(void*, int), int (*done)(void*, int), int (*con)(void*, int, Csid_t*, int, char**), int (*rd)(void*, int), int (*wr)(void*, int), int (*to)(void*))
142
{
143
register Server_t* server;
144
145
if (!con && !rd)
146
error(ERROR_PANIC, "a connect or read handler must be supplied");
147
if (!(server = newof(0, Server_t, 1, 0)))
148
error(ERROR_SYSTEM|3, "out of space");
149
state->server = server;
150
server->disc.version = CSS_VERSION;
151
server->disc.flags = CSS_DAEMON|CSS_LOG|CSS_CLOSE|CSS_ERROR|CSS_INTERRUPT|CSS_TIMEOUT|CSS_WAKEUP;
152
server->handle = handle;
153
server->con = con;
154
if (server->con)
155
server->disc.acceptf = acceptf;
156
server->rd = rd;
157
server->wr = wr;
158
if (server->rd || server->wr)
159
server->disc.actionf = actionf;
160
server->to = to;
161
server->disc.errorf = errorf;
162
server->disc.exceptf = exceptf;
163
server->done = done;
164
close(0);
165
if (!(server->css = cssopen(path, (Cssdisc_t*)server)))
166
exit(1);
167
error_info.id = server->css->service;
168
state->id = server->css->id;
169
state->cs = server->css->path;
170
state->control = state->mount + (server->css->control - server->css->mount);
171
strcpy(state->mount, server->css->mount);
172
#ifdef SIGCHLD
173
if (!done)
174
signal(SIGCHLD, SIG_DFL);
175
#endif
176
177
/*
178
* we the
179
* are
180
* ser
181
* ver
182
*
183
* in background
184
* no controlling tty
185
* pwd is the service data directory
186
* stdin is the connect stream
187
* stdout is /dev/null
188
* stderr is CS_MNT_LOG in the service data directory
189
* umask matches service mode
190
*/
191
192
if (init)
193
server->handle = (*init)(server->handle, server->css->fdmax);
194
csspoll(CS_NEVER, 0);
195
}
196
197
int
198
_cs_fd(int fd, int op)
199
{
200
return csfd(&cs, fd, op);
201
}
202
203
void
204
_cs_serve(void* handle, const char* path, void* (*init)(void*, int), int (*done)(void*, int), int (*con)(void*, int, Csid_t*, int, char**), int (*rd)(void*, int), int (*wr)(void*, int), int (*to)(void*))
205
{
206
csserve(&cs, handle, path, init, done, con, rd, wr, to);
207
}
208
209
unsigned long
210
_cs_timeout(unsigned long ms)
211
{
212
return cstimeout(&cs, ms);
213
}
214
215
unsigned long
216
_cs_wakeup(unsigned long ms)
217
{
218
return cswakeup(&cs, ms);
219
}
220
221