386BSD 0.1 development
[unix-history] / usr / othersrc / contrib / isode / acsap / acserver2.c
CommitLineData
48435ab0
WJ
1/* acserver2.c - generic server dispatch */
2
3#ifndef lint
4static char *rcsid = "$Header: /f/osi/acsap/RCS/acserver2.c,v 7.4 91/02/22 09:14:24 mrose Interim $";
5#endif
6
7/*
8 * $Header: /f/osi/acsap/RCS/acserver2.c,v 7.4 91/02/22 09:14:24 mrose Interim $
9 *
10 *
11 * $Log: acserver2.c,v $
12 * Revision 7.4 91/02/22 09:14:24 mrose
13 * Interim 6.8
14 *
15 * Revision 7.3 90/10/29 18:37:51 mrose
16 * updates
17 *
18 * Revision 7.2 90/07/09 14:30:46 mrose
19 * sync
20 *
21 * Revision 7.1 90/02/19 13:07:07 mrose
22 * update
23 *
24 * Revision 7.0 89/11/23 21:22:03 mrose
25 * Release 6.0
26 *
27 */
28
29/*
30 * NOTICE
31 *
32 * Acquisition, use, and distribution of this module and related
33 * materials are subject to the restrictions of a license agreement.
34 * Consult the Preface in the User's Manual for the full terms of
35 * this agreement.
36 *
37 */
38
39
40/* LINTLIBRARY */
41
42#include <signal.h>
43#include "psap.h"
44#include "tsap.h"
45#include <sys/ioctl.h>
46#ifdef BSD42
47#include <sys/file.h>
48#endif
49#ifdef SYS5
50#include <fcntl.h>
51#endif
52#include "tailor.h"
53
54/* \f */
55
56static int is_nfds;
57static fd_set is_mask;
58static char is_single;
59
60/* \f */
61
62int iserver_init (argc, argv, aei, initfnx, td)
63int argc;
64char **argv;
65AEI aei;
66IFP initfnx;
67struct TSAPdisconnect *td;
68{
69 int fd;
70
71 isodetailor (NULLCP, 0);
72
73 is_nfds = 0;
74 FD_ZERO (&is_mask);
75
76 if (argc > 1) {
77 is_single = 1;
78 if ((fd = (*initfnx) (argc, argv)) == NOTOK)
79 return tsaplose (td, DR_NETWORK, NULLCP, "initialization failed");
80
81 is_nfds = fd + 1;
82 FD_SET (fd, &is_mask);
83 }
84 else {
85 struct PSAPaddr *pa;
86 is_single = 0;
87
88 if ((pa = aei2addr (aei)) == NULLPA)
89 return tsaplose (td, DR_ADDRESS, NULLCP,
90 "address translation failed");
91
92 if (TNetListen (&pa -> pa_addr.sa_addr, td) == NOTOK)
93 return NOTOK;
94
95 if (!isatty (2)) {
96 int i;
97
98 for (i = 0; i < 5; i++) {
99 switch (fork ()) {
100 case NOTOK:
101 sleep (5);
102 continue;
103
104 case OK:
105 break;
106
107 default:
108 _exit (0);
109 }
110 break;
111 }
112
113 (void) chdir ("/");
114
115 if ((fd = open ("/dev/null", O_RDWR)) != NOTOK) {
116 if (fd != 0)
117 (void) dup2 (fd, 0), (void) close (fd);
118 (void) dup2 (0, 1);
119 (void) dup2 (0, 2);
120 }
121
122#ifdef SETSID
123 (void) setsid ();
124#endif
125#ifdef TIOCNOTTY
126 if ((fd = open ("/dev/tty", O_RDWR)) != NOTOK) {
127 (void) ioctl (fd, TIOCNOTTY, NULLCP);
128 (void) close (fd);
129 }
130#else
131#ifdef SYS5
132 (void) setpgrp ();
133 (void) signal (SIGINT, SIG_IGN);
134 (void) signal (SIGQUIT, SIG_IGN);
135#endif
136#endif
137 isodexport (NULLCP); /* re-initialize logfiles */
138 }
139 }
140
141 return OK;
142}
143
144/* \f */
145
146int iserver_wait (initfnx, workfnx, losefnx, nfds, rfds, wfds, efds, secs,
147 td)
148IFP initfnx,
149 workfnx,
150 losefnx;
151int nfds;
152fd_set *rfds,
153 *wfds,
154 *efds;
155int secs;
156struct TSAPdisconnect *td;
157{
158 int fd,
159 vecp;
160 fd_set ifds,
161 ofds,
162 xfds;
163 char *vec[4];
164
165 ifds = is_mask; /* struct copy */
166 FD_ZERO (&ofds);
167 FD_ZERO (&xfds);
168 if (is_nfds > nfds)
169 nfds = is_nfds + 1;
170
171 if (rfds)
172 for (fd = 0; fd < nfds; fd++)
173 if (FD_ISSET (fd, rfds))
174 FD_SET (fd, &ifds);
175 if (wfds)
176 ofds = *wfds;
177 if (efds)
178 xfds = *efds;
179
180 if (TNetAccept (&vecp, vec, nfds, &ifds, &ofds, &xfds, secs, td)
181 == NOTOK) {
182 (void) (*losefnx) (td);
183
184 return OK;
185 }
186 if (wfds)
187 *wfds = ofds;
188 if (efds)
189 *efds = xfds;
190
191 if (vecp > 0 && (fd = (*initfnx) (vecp, vec)) != NOTOK) {
192 if (fd >= is_nfds)
193 is_nfds = fd + 1;
194 FD_SET (fd, &is_mask);
195 }
196
197 for (fd = 0; fd < nfds; fd++)
198 if (FD_ISSET (fd, &is_mask) && FD_ISSET (fd, &ifds)) {
199 if (workfnx == NULLIFP) {
200 (void) TNetClose (NULLTA, td);
201 return tsaplose (td, DR_OPERATION, NULLCP,
202 "no worker routine for connected fd");
203 }
204 FD_CLR (fd, &ifds);
205 if ((*workfnx) (fd) == NOTOK) {
206 FD_CLR (fd, &is_mask);
207 if (is_nfds == fd + 1)
208 is_nfds--;
209
210 if (is_single) {
211 int xd;
212
213 for (xd = 0; xd < nfds; xd++)
214 if (FD_ISSET (xd, &is_mask))
215 break;
216 if (rfds)
217 FD_ZERO (rfds);
218 if (xd >= is_nfds)
219 return DONE;
220 }
221 }
222 }
223 if (rfds)
224 *rfds = ifds;
225 return OK;
226}
227
228/* \f */
229
230fd_set iserver_mask ()
231{
232 return is_mask;
233}