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