]>
Commit | Line | Data |
---|---|---|
0fa91a0a | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
7 | * | |
8 | * | |
9 | * SMS server process. | |
10 | * | |
11 | * Most of this is stolen from ../gdb/tsr.c | |
12 | * | |
13 | * You are in a maze of twisty little finite automata, all different. | |
14 | * Let the reader beware. | |
15 | * | |
0fa91a0a | 16 | */ |
17 | ||
0fa91a0a | 18 | static char *rcsid_sms_main_c = "$Header$"; |
0fa91a0a | 19 | |
70d54e43 | 20 | #include <strings.h> |
21 | #include <sys/errno.h> | |
70d54e43 | 22 | #include <sys/signal.h> |
0fa91a0a | 23 | #include "sms_server.h" |
70d54e43 | 24 | |
0ec57336 | 25 | extern CONNECTION newconn, listencon; |
0fa91a0a | 26 | |
0ec57336 | 27 | extern int nclients; |
28 | extern client **clients, *cur_client; | |
0fa91a0a | 29 | |
0ec57336 | 30 | extern OPERATION listenop; |
31 | extern LIST_OF_OPERATIONS op_list; | |
0fa91a0a | 32 | |
0ec57336 | 33 | extern struct sockaddr_in client_addr; |
34 | extern int client_addrlen; | |
35 | extern TUPLE client_tuple; | |
0fa91a0a | 36 | |
0ec57336 | 37 | extern char *whoami; |
38 | extern char buf1[BUFSIZ]; | |
39 | extern char *takedown; | |
40 | extern int errno; | |
2f4ff9cd | 41 | extern FILE *journal; |
42 | #define JOURNAL "/u1/sms/journal" | |
70d54e43 | 43 | |
0ec57336 | 44 | extern char *malloc(); |
f5c72293 | 45 | extern int free(); |
0ec57336 | 46 | extern char *inet_ntoa(); |
47 | extern void sms_com_err(); | |
48 | extern void do_client(); | |
70d54e43 | 49 | |
0ec57336 | 50 | extern int sigshut(); |
51 | void clist_append(); | |
52 | void oplist_append(); | |
0fa91a0a | 53 | |
0eb9f52f | 54 | extern time_t now; |
55 | ||
0ec57336 | 56 | /* |
57 | * Main SMS server loop. | |
58 | * | |
59 | * Initialize the world, then start accepting connections and | |
60 | * making progress on current connections. | |
61 | */ | |
0fa91a0a | 62 | |
0ec57336 | 63 | /*ARGSUSED*/ |
64 | int | |
0fa91a0a | 65 | main(argc, argv) |
70d54e43 | 66 | int argc; |
67 | char **argv; | |
0fa91a0a | 68 | { |
0ec57336 | 69 | int status; |
0eb9f52f | 70 | time_t tardy; |
0ec57336 | 71 | |
72 | whoami = argv[0]; | |
73 | /* | |
74 | * Error handler init. | |
75 | */ | |
76 | init_sms_err_tbl(); | |
77 | init_krb_err_tbl(); | |
78 | set_com_err_hook(sms_com_err); | |
b4182127 | 79 | setlinebuf(stderr); |
80 | ||
0ec57336 | 81 | if (argc != 1) { |
82 | com_err(whoami, 0, "Usage: smsd"); | |
83 | exit(1); | |
84 | } | |
f5c72293 | 85 | |
86 | /* Profiling implies that getting rid of one level of call | |
87 | * indirection here wins us maybe 1% on the VAX. | |
88 | */ | |
89 | gdb_amv = malloc; | |
90 | gdb_fmv = free; | |
0ec57336 | 91 | |
92 | /* | |
93 | * GDB initialization. | |
94 | */ | |
5dbd09a0 | 95 | if(gdb_init() != 0) { |
96 | com_err(whoami, 0, "GDB initialization failed."); | |
97 | exit(1); | |
98 | } | |
7f024a70 | 99 | gdb_debug(0); /* this can be patched, if necessary, to enable */ |
100 | /* GDB level debugging .. */ | |
4a06eb54 | 101 | krb_realm = malloc(REALM_SZ); |
102 | get_krbrlm(krb_realm, 1); | |
7f024a70 | 103 | |
f6cc38de | 104 | /* |
105 | * Database initialization. | |
106 | */ | |
107 | ||
108 | if ((status = sms_open_database()) != 0) { | |
2f4ff9cd | 109 | com_err(whoami, status, " when trying to open database."); |
f6cc38de | 110 | exit(1); |
111 | } | |
112 | ||
2f4ff9cd | 113 | sanity_check_queries(); |
114 | sanity_check_database(); | |
115 | ||
0ec57336 | 116 | /* |
117 | * Set up client array handler. | |
118 | */ | |
0fa91a0a | 119 | nclients = 0; |
120 | clients = (client **) malloc(0); | |
70d54e43 | 121 | |
0ec57336 | 122 | /* |
123 | * Signal handlers | |
f6cc38de | 124 | * There should probably be a few more of these. |
0ec57336 | 125 | */ |
70d54e43 | 126 | |
0ec57336 | 127 | if ((((int)signal (SIGTERM, sigshut)) < 0) || |
128 | (((int)signal (SIGHUP, sigshut)) < 0)) { | |
2f4ff9cd | 129 | com_err(whoami, errno, " Unable to establish signal handler."); |
0ec57336 | 130 | exit(1); |
131 | } | |
132 | ||
2f4ff9cd | 133 | journal = fopen(JOURNAL, "a"); |
134 | if (journal == NULL) { | |
135 | com_err(whoami, errno, " while opening journal file"); | |
136 | exit(1); | |
137 | } | |
138 | ||
0ec57336 | 139 | /* |
140 | * Establish template connection. | |
141 | */ | |
142 | if ((status = do_listen()) != 0) { | |
143 | com_err(whoami, status, | |
2f4ff9cd | 144 | " while trying to create listening connection"); |
0ec57336 | 145 | exit(1); |
146 | } | |
0fa91a0a | 147 | |
0fa91a0a | 148 | op_list = create_list_of_operations(1, listenop); |
0fa91a0a | 149 | |
b4182127 | 150 | com_err(whoami, 0, "started (pid %d)", getpid()); |
0ec57336 | 151 | com_err(whoami, 0, rcsid_sms_main_c); |
49700f8b | 152 | send_zgram("SMS", "server started"); |
0ec57336 | 153 | |
154 | /* | |
155 | * Run until shut down. | |
156 | */ | |
70d54e43 | 157 | while(!takedown) { |
0ec57336 | 158 | register int i; |
159 | /* | |
160 | * Block until something happens. | |
161 | */ | |
5dbd09a0 | 162 | #ifdef notdef |
163 | com_err(whoami, 0, "tick"); | |
164 | #endif notdef | |
165 | errno = 0; | |
166 | status = op_select_any(op_list, 0, | |
167 | (fd_set *)NULL, (fd_set *)NULL, | |
168 | (fd_set *)NULL, (struct timeval *)NULL); | |
169 | ||
170 | if (status == -1) { | |
2f4ff9cd | 171 | com_err(whoami, errno, " error from op_select"); |
5dbd09a0 | 172 | continue; |
173 | } else if (status != -2) { | |
2f4ff9cd | 174 | com_err(whoami, 0, " wrong return from op_select_any"); |
5dbd09a0 | 175 | continue; |
176 | } | |
70d54e43 | 177 | if (takedown) break; |
0eb9f52f | 178 | time(&now); |
70d54e43 | 179 | #ifdef notdef |
180 | fprintf(stderr, " tick\n"); | |
181 | #endif notdef | |
b4182127 | 182 | /* |
183 | * Handle any new connections; this comes first so | |
184 | * errno isn't tromped on. | |
185 | */ | |
186 | if (OP_DONE(listenop)) { | |
187 | if (OP_STATUS(listenop) == OP_CANCELLED) { | |
188 | if (errno == EWOULDBLOCK) { | |
189 | do_reset_listen(); | |
190 | } else { | |
191 | com_err(whoami, errno, | |
2f4ff9cd | 192 | " error on listen"); |
b4182127 | 193 | exit(1); |
194 | } | |
195 | } else if ((status = new_connection()) != 0) { | |
196 | com_err(whoami, errno, | |
2f4ff9cd | 197 | " Error on listening operation."); |
b4182127 | 198 | /* |
199 | * Sleep here to prevent hosing? | |
200 | */ | |
201 | } | |
202 | } | |
0ec57336 | 203 | /* |
204 | * Handle any existing connections. | |
205 | */ | |
0eb9f52f | 206 | tardy = now - 30*60; |
207 | ||
0fa91a0a | 208 | for (i=0; i<nclients; i++) { |
0eb9f52f | 209 | cur_client = clients[i]; |
0fa91a0a | 210 | if (OP_DONE(clients[i]->pending_op)) { |
0eb9f52f | 211 | cur_client->last_time_used = now; |
70d54e43 | 212 | do_client(cur_client); |
0eb9f52f | 213 | } else if (clients[i]->last_time_used < tardy) { |
214 | com_err(whoami, 0, "Shutting down connection due to inactivity"); | |
215 | shutdown(cur_client->con->in.fd, 0); | |
0fa91a0a | 216 | } |
0eb9f52f | 217 | cur_client = NULL; |
218 | if (takedown) break; | |
0fa91a0a | 219 | } |
0fa91a0a | 220 | } |
b4182127 | 221 | com_err(whoami, 0, "%s", takedown); |
f6cc38de | 222 | sms_close_database(); |
49700f8b | 223 | send_zgram("SMS", takedown); |
0ec57336 | 224 | return 0; |
70d54e43 | 225 | } |
226 | ||
0ec57336 | 227 | /* |
228 | * Set up the template connection and queue the first accept. | |
229 | */ | |
230 | ||
231 | int | |
232 | do_listen() | |
70d54e43 | 233 | { |
0ec57336 | 234 | char *service = index(SMS_GDB_SERV, ':') + 1; |
235 | ||
236 | listencon = create_listening_connection(service); | |
237 | ||
238 | if (listencon == NULL) | |
239 | return errno; | |
240 | ||
241 | listenop = create_operation(); | |
242 | client_addrlen = sizeof(client_addr); | |
243 | ||
244 | start_accepting_client(listencon, listenop, &newconn, | |
245 | (char *)&client_addr, | |
246 | &client_addrlen, &client_tuple); | |
247 | return 0; | |
0fa91a0a | 248 | } |
249 | ||
b4182127 | 250 | |
251 | do_reset_listen() | |
252 | { | |
253 | client_addrlen = sizeof(client_addr); | |
254 | start_accepting_client(listencon, listenop, &newconn, | |
255 | (char *)&client_addr, | |
256 | &client_addrlen, &client_tuple); | |
257 | } | |
258 | ||
0ec57336 | 259 | /* |
260 | * This routine is called when a new connection comes in. | |
261 | * | |
262 | * It sets up a new client and adds it to the list of currently active clients. | |
263 | */ | |
264 | int | |
5dbd09a0 | 265 | new_connection() |
0fa91a0a | 266 | { |
7f024a70 | 267 | register client *cp; |
70d54e43 | 268 | static counter = 0; |
0fa91a0a | 269 | |
0fa91a0a | 270 | /* |
271 | * Make sure there's been no error | |
272 | */ | |
c27b3454 | 273 | if(OP_STATUS(listenop) != OP_COMPLETE) { |
0ec57336 | 274 | return errno; |
c27b3454 | 275 | } |
276 | ||
277 | if (newconn == NULL) { | |
278 | return SMS_NOT_CONNECTED; | |
0fa91a0a | 279 | } |
280 | ||
0fa91a0a | 281 | /* |
282 | * Set up the new connection and reply to the client | |
283 | */ | |
7f024a70 | 284 | cp = (client *)malloc(sizeof *cp); |
23e476e8 | 285 | bzero(cp, sizeof(*cp)); |
0fa91a0a | 286 | cp->state = CL_STARTING; |
287 | cp->action = CL_ACCEPT; | |
288 | cp->con = newconn; | |
70d54e43 | 289 | cp->id = counter++; |
0ec57336 | 290 | cp->args = NULL; |
291 | cp->clname = NULL; | |
292 | cp->reply.sms_argv = NULL; | |
4a06eb54 | 293 | cp->first = NULL; |
294 | cp->last = NULL; | |
0eb9f52f | 295 | cp->last_time_used = now; |
0fa91a0a | 296 | newconn = NULL; |
297 | ||
298 | cp->pending_op = create_operation(); | |
299 | reset_operation(cp->pending_op); | |
300 | oplist_append(&op_list, cp->pending_op); | |
70d54e43 | 301 | cur_client = cp; |
302 | ||
0ec57336 | 303 | /* |
304 | * Add a new client to the array.. | |
305 | */ | |
306 | clist_append(cp); | |
307 | ||
308 | /* | |
309 | * Let him know we heard him. | |
310 | */ | |
0fa91a0a | 311 | start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED, |
312 | "", ""); | |
0ec57336 | 313 | |
314 | cp->haddr = client_addr; | |
315 | ||
316 | /* | |
317 | * Log new connection. | |
318 | */ | |
319 | ||
b4182127 | 320 | com_err(whoami, 0, "New connection from %s port %d (now %d client%s)", |
321 | inet_ntoa(cp->haddr.sin_addr), | |
322 | (int)ntohs(cp->haddr.sin_port), | |
323 | nclients, | |
324 | nclients!=1?"s":""); | |
0fa91a0a | 325 | |
326 | /* | |
0ec57336 | 327 | * Get ready to accept the next connection. |
0fa91a0a | 328 | */ |
329 | reset_operation(listenop); | |
0ec57336 | 330 | client_addrlen = sizeof(client_addr); |
0fa91a0a | 331 | |
332 | start_accepting_client(listencon, listenop, &newconn, | |
0ec57336 | 333 | (char *)&client_addr, |
334 | &client_addrlen, &client_tuple); | |
335 | return 0; | |
0fa91a0a | 336 | } |
337 | ||
0fa91a0a | 338 | /* |
339 | * Add a new client to the known clients. | |
340 | */ | |
0ec57336 | 341 | void |
0fa91a0a | 342 | clist_append(cp) |
343 | client *cp; | |
344 | { | |
345 | client **clients_n; | |
346 | ||
347 | nclients++; | |
0ec57336 | 348 | clients_n = (client **)malloc |
349 | ((unsigned)(nclients * sizeof(client *))); | |
0fa91a0a | 350 | bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp)); |
351 | clients_n[nclients-1] = cp; | |
352 | free((char *)clients); | |
353 | clients = clients_n; | |
354 | clients_n = NULL; | |
355 | } | |
356 | ||
70d54e43 | 357 | |
0ec57336 | 358 | void |
70d54e43 | 359 | clist_delete(cp) |
360 | client *cp; | |
361 | { | |
70d54e43 | 362 | client **clients_n, **scpp, **dcpp; /* source and dest client */ |
363 | /* ptr ptr */ | |
364 | ||
70d54e43 | 365 | int found_it = 0; |
366 | ||
0ec57336 | 367 | clients_n = (client **)malloc |
368 | ((unsigned)((nclients - 1)* sizeof(client *))); | |
70d54e43 | 369 | for (scpp = clients, dcpp = clients_n; scpp < clients+nclients; ) { |
370 | if (*scpp != cp) { | |
371 | *dcpp++ = *scpp++; | |
372 | } else { | |
373 | scpp++; | |
374 | if (found_it) abort(); | |
375 | found_it = 1; | |
376 | } | |
377 | } | |
378 | --nclients; | |
379 | free((char *)clients); | |
380 | clients = clients_n; | |
381 | clients_n = NULL; | |
c27b3454 | 382 | oplist_delete(op_list, cp->pending_op); |
70d54e43 | 383 | reset_operation(cp->pending_op); |
384 | delete_operation(cp->pending_op); | |
7f024a70 | 385 | sever_connection(cp->con); |
0ec57336 | 386 | free((char *)cp); |
70d54e43 | 387 | } |
0ec57336 | 388 | |
0fa91a0a | 389 | /* |
0ec57336 | 390 | * Add a new operation to a list of operations. |
391 | * | |
392 | * This should be rewritten to use realloc instead, since in most | |
393 | * cases it won't have to copy the array. | |
0fa91a0a | 394 | */ |
395 | ||
0ec57336 | 396 | void |
0fa91a0a | 397 | oplist_append(oplp, op) |
398 | LIST_OF_OPERATIONS *oplp; | |
399 | OPERATION op; | |
400 | { | |
401 | int count = (*oplp)->count+1; | |
402 | LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS) | |
403 | db_alloc(size_of_list_of_operations(count)); | |
404 | bcopy((char *)(*oplp), (char *)newlist, | |
405 | size_of_list_of_operations((*oplp)->count)); | |
0fa91a0a | 406 | newlist->count++; |
407 | newlist->op[count-1] = op; | |
467f5982 | 408 | db_free((char *)(*oplp), size_of_list_of_operations(count-1)); |
0fa91a0a | 409 | (*oplp) = newlist; |
410 | } | |
411 | ||
c27b3454 | 412 | |
413 | oplist_delete(oplp, op) | |
414 | LIST_OF_OPERATIONS oplp; | |
415 | register OPERATION op; | |
416 | { | |
417 | register OPERATION *s; | |
418 | register int c; | |
419 | ||
420 | for (s = oplp->op, c=oplp->count; c; --c, ++s) { | |
421 | if (*s == op) { | |
422 | while (c > 0) { | |
423 | *s = *(s+1); | |
424 | ++s; | |
425 | --c; | |
426 | } | |
427 | oplp->count--; | |
428 | return; | |
429 | } | |
430 | } | |
431 | abort(); | |
432 | } |