]> andersk Git - moira.git/blob - server/mr_main.c
Use unsigned char rather than char to prevent sign extension
[moira.git] / server / mr_main.c
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$
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.
20  *
21  * Revision 1.9  87/06/21  16:39:54  wesommer
22  * Performance work, rearrangement of include files.
23  * 
24  * Revision 1.8  87/06/09  18:44:45  wesommer
25  * modified error handling.
26  * 
27  * Revision 1.7  87/06/08  02:44:44  wesommer
28  * Minor lint fix.
29  * 
30  * Revision 1.6  87/06/03  17:41:00  wesommer
31  * Added startup support.
32  * 
33  * Revision 1.5  87/06/03  16:07:17  wesommer
34  * Fixes for lint.
35  * 
36  * Revision 1.4  87/06/02  20:05:11  wesommer
37  * Bug fixes on memory allocation.
38  * 
39  * Revision 1.3  87/06/01  04:34:27  wesommer
40  * Changed returned error code.
41  * 
42  * Revision 1.2  87/06/01  03:34:53  wesommer
43  * Added shutdown, logging.
44  * 
45  * Revision 1.1  87/05/31  22:06:56  wesommer
46  * Initial revision
47  * 
48  */
49
50 static char *rcsid_sms_main_c = "$Header$";
51
52 #include <strings.h>
53 #include <sys/errno.h>
54 #include <sys/signal.h>
55 #include "sms_server.h"
56
57 extern CONNECTION newconn, listencon;
58
59 extern int nclients;
60 extern client **clients, *cur_client;
61
62 extern OPERATION listenop;
63 extern LIST_OF_OPERATIONS op_list;
64
65 extern struct sockaddr_in client_addr;
66 extern int client_addrlen;
67 extern TUPLE client_tuple;
68
69 extern char *whoami;
70 extern char buf1[BUFSIZ];
71 extern char *takedown;
72 extern int errno;
73
74
75 extern char *malloc();
76 extern char *inet_ntoa();
77 extern void sms_com_err();
78 extern void do_client();
79
80 extern int sigshut();
81 void clist_append();
82 void oplist_append();
83 extern u_short ntohs();
84
85 /*
86  * Main SMS server loop.
87  *
88  * Initialize the world, then start accepting connections and
89  * making progress on current connections.
90  */
91
92 /*ARGSUSED*/
93 int
94 main(argc, argv)
95         int argc;
96         char **argv;
97 {
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          */
116         if(gdb_init() != 0) {
117                 com_err(whoami, 0, "GDB initialization failed.");
118                 exit(1);
119         }
120         gdb_debug(0); /* this can be patched, if necessary, to enable */
121                       /* GDB level debugging .. */
122         krb_realm = malloc(REALM_SZ);
123         get_krbrlm(krb_realm, 1);
124         
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         
134         /*
135          * Set up client array handler.
136          */
137         nclients = 0;
138         clients = (client **) malloc(0);
139         
140         /*
141          * Signal handlers
142          *      There should probably be a few more of these.
143          */
144         
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         }
159         
160         op_list = create_list_of_operations(1, listenop);
161         
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          */
169         while(!takedown) {              
170                 register int i;
171                 /*
172                  * Block until something happens.
173                  */
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                 }
189                 if (takedown) break;
190 #ifdef notdef
191                 fprintf(stderr, "    tick\n");
192 #endif notdef
193                 /*
194                  * Handle any existing connections.
195                  */
196                 for (i=0; i<nclients; i++) {
197                         if (OP_DONE(clients[i]->pending_op)) {
198                                 cur_client = clients[i];
199                                 do_client(cur_client);
200                                 cur_client = NULL;
201                                 if (takedown) break;
202                         }
203                 }
204                 /*
205                  * Handle any new connections.
206                  */
207                 if (OP_DONE(listenop)) {
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) {
213                                 com_err(whoami, errno,
214                                         "Error on listening operation.");
215                                 /*
216                                  * Sleep here to prevent hosing?
217                                  */
218                         }
219                 }
220         }
221         com_err(whoami, 0, takedown);
222         sms_close_database();
223         return 0;
224 }
225
226 /*
227  * Set up the template connection and queue the first accept.
228  */
229
230 int
231 do_listen()
232 {
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;
247 }
248
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
255 new_connection()
256 {
257         register client *cp;
258         static counter = 0;
259         
260         /*
261          * Make sure there's been no error
262          */
263         if(OP_STATUS(listenop) != OP_COMPLETE) {
264                 return errno;
265         }
266         
267         if (newconn == NULL) {
268                 return SMS_NOT_CONNECTED;
269         }
270
271         /*
272          * Set up the new connection and reply to the client
273          */
274         cp = (client *)malloc(sizeof *cp);
275         cp->state = CL_STARTING;
276         cp->action = CL_ACCEPT;
277         cp->con = newconn;
278         cp->id = counter++;
279         cp->args = NULL;
280         cp->clname = NULL;
281         cp->reply.sms_argv = NULL;
282         cp->first = NULL;
283         cp->last = NULL;
284         
285         newconn = NULL;
286         
287         cp->pending_op = create_operation();
288         reset_operation(cp->pending_op);
289         oplist_append(&op_list, cp->pending_op);
290         cur_client = cp;
291         
292         /*
293          * Add a new client to the array..
294          */
295         clist_append(cp);
296         
297         /*
298          * Let him know we heard him.
299          */
300         start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED,
301                                  "", "");
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);
316         
317         /*
318          * Get ready to accept the next connection.
319          */
320         reset_operation(listenop);
321         client_addrlen = sizeof(client_addr);
322
323         start_accepting_client(listencon, listenop, &newconn,
324                                (char *)&client_addr,
325                                &client_addrlen, &client_tuple);
326         return 0;
327 }
328
329 /*
330  * Add a new client to the known clients.
331  */
332 void
333 clist_append(cp)
334         client *cp;
335 {               
336         client **clients_n;
337         
338         nclients++;
339         clients_n = (client **)malloc
340                 ((unsigned)(nclients * sizeof(client *)));
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
348                 
349 void
350 clist_delete(cp)
351         client *cp;
352 {
353         client **clients_n, **scpp, **dcpp; /* source and dest client */
354                                             /* ptr ptr */
355         
356         int found_it = 0;
357         
358         clients_n = (client **)malloc
359                 ((unsigned)((nclients - 1)* sizeof(client *)));
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;
373         oplist_delete(op_list, cp->pending_op);
374         reset_operation(cp->pending_op);
375         delete_operation(cp->pending_op);
376         sever_connection(cp->con);
377         free((char *)cp);
378 }
379
380 /*
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.
385  */
386
387 void
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));
397         newlist->count++;
398         newlist->op[count-1] = op;
399         db_free((char *)(*oplp), size_of_list_of_operations(count-1));
400         (*oplp) = newlist;
401 }
402
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 }
This page took 0.148461 seconds and 5 git commands to generate.