2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation, in whole or in part, for any purpose, is hereby granted,
15 * 1. This copyright and permission notice appear in all copies of the
16 * software and its documentation. Notices of copyright or attribution
17 * which appear at the beginning of any file must remain unchanged.
19 * 2. The names of EUROPAGATE or the project partners may not be used to
20 * endorse or promote products derived from this software without specific
21 * prior written permission.
23 * 3. Users of this software (implementors and gateway operators) agree to
24 * inform the EUROPAGATE consortium of their use of the software. This
25 * information will be used to evaluate the EUROPAGATE project and the
26 * software, and to plan further developments. The consortium may use
27 * the information in later publications.
29 * 4. Users of this software agree to make their best efforts, when
30 * documenting their use of the software, to acknowledge the EUROPAGATE
31 * consortium, and the role played by the software in their work.
33 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34 * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36 * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37 * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38 * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39 * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40 * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41 * USE OR PERFORMANCE OF THIS SOFTWARE.
44 /* Gateway Resource Monitor
48 * Revision 1.12 1995/05/19 14:51:06 adam
49 * Bug fix: stopped kernels sometimes got IPC messages from the monitor.
51 * Revision 1.11 1995/05/19 13:26:00 adam
52 * Bug fixes. Better command line options.
54 * Revision 1.10 1995/05/18 12:03:09 adam
55 * Bug fixes and minor improvements.
57 * Revision 1.9 1995/05/17 10:51:32 adam
58 * Added a few more error checks to the show command.
60 * Revision 1.8 1995/05/16 09:40:42 adam
61 * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
63 * Revision 1.7 1995/05/03 12:18:46 adam
64 * This code ran on dtbsun. Minor changes.
66 * Revision 1.6 1995/05/03 09:16:17 adam
69 * Revision 1.5 1995/05/03 07:37:42 adam
70 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
71 * are used when possible.
73 * Revision 1.4 1995/05/02 15:26:00 adam
74 * Monitor observes death of child (email kernel). The number
75 * of simultanous processes is controlled now. Email requests are
76 * queued if necessary. This scheme should only be forced if no kernels
79 * Revision 1.3 1995/05/02 07:20:10 adam
80 * Use pid of exited child to close fifos.
82 * Revision 1.2 1995/05/01 16:26:57 adam
83 * More work on resource monitor.
85 * Revision 1.1 1995/05/01 12:43:36 adam
86 * First work on resource monitor program.
101 #include <sys/file.h>
102 #include <sys/stat.h>
103 #include <sys/types.h>
104 #include <sys/time.h>
105 #include <sys/wait.h>
110 #include <strqueue.h>
113 #define LINE_MAX 1024
115 #define MONITOR_FIFO_S "fifo.s.m"
116 #define MONITOR_FIFO_C "fifo.c.m"
118 static char *module = "monitor";
119 static jmp_buf retry_jmp;
121 static GwRes monitor_res = NULL;
122 static int no_process = 0;
123 static int max_process = 1;
124 static int got_sighup = 0;
125 static int got_term = 0;
126 static int got_int = 0;
127 const char *default_res = "default.res";
130 * reread_resources: reread monitor resources. The static variable,
131 * max_process, is updated.
133 static void reread_resources (void)
136 gw_res_close (monitor_res);
137 monitor_res = gw_res_init ();
138 if (gw_res_merge (monitor_res, default_res))
140 gw_log (GW_LOG_WARN, module, "Couldn't read resource file %s",
144 max_process = gw_res_int (monitor_res, "gw.max.process", 10);
148 int id; /* email user-id */
149 int stopped; /* stop flag */
150 pid_t pid; /* pid of email kernel child */
151 GIP gip; /* fifo information */
152 struct str_queue *queue; /* message queue */
153 struct ke_info *next; /* link to next */
156 /* list of email kernel infos */
157 static struct ke_info *ke_info_list = NULL;
160 * ke_info_add: add/lookup of email kernel info.
161 * id: email user-id to search for.
162 * return: pointer to info structure.
164 struct ke_info *ke_info_add (int id)
166 struct ke_info **kip;
168 for (kip = &ke_info_list; *kip; kip= &(*kip)->next)
169 if ((*kip)->id == id)
171 *kip = malloc (sizeof(**kip));
176 (*kip)->queue = NULL;
181 static void ke_info_del (void)
185 assert (ke_info_list);
187 str_queue_rm (&ki->queue);
188 ke_info_list = ki->next;
193 * catch_child: catch SIGCHLD. Set email kernel pid to -1
194 * to indicate that child has exited
196 static void catch_child (int num)
201 while ((pid=waitpid (-1, 0, WNOHANG)) > 0)
203 for (ki = ke_info_list; ki; ki = ki->next)
208 signal (SIGCHLD, catch_child);
212 * catch_int: catch SIGHUP.
214 static void catch_hup (int num)
217 signal (SIGHUP, catch_hup);
221 * catch_int: catch SIGTERM.
223 static void catch_term (int num)
226 signal (SIGTERM, catch_term);
230 * catch_int: catch SIGINT.
232 static void catch_int (int num)
235 signal (SIGINT, catch_int);
239 * pipe_handle: handle SIGPIPE when transferring message to kernel
241 static void pipe_handle (int dummy)
243 longjmp (retry_jmp, 1);
247 * start_kernel: start email kernel.
248 * argc: argc of email kernel
249 * argv: argv of email kernel
251 * return: pid of email kernel child
253 static pid_t start_kernel (int argc, char **argv, int id)
258 char userid_option[20];
260 argv_p = malloc (sizeof(*argv_p)*(argc+2));
263 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
266 argv_p[0] = "kernel";
267 for (i = 1; i<argc; i++)
269 sprintf (userid_option, "-i%d", id);
270 argv_p[i++] = userid_option;
273 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
277 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
282 execv ("kernel", argv_p);
283 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
290 * deliver: deliver message to child (email kernel).
291 * argc: exec argc to child (if it need to be started)
292 * argv: exec argv to child (if it need to be started)
294 * queue: message queue to be transferred
295 * gip: pointer to FIFO info. if *gip is NULL prior invocation
296 * it will be created (initialized) and the pointer will be
298 * pidp: pointer to pid. Will hold process-id of child (if it need to
300 * dont_exec: if non-zero a child will never be started; otherwise child
301 * will be started if not already running.
302 * return: 0 if message couldn't be transferred, i.e. dont_exec is non-zero
303 * and the child is not already running.
304 * 1 if message was transferred and the child was already running.
305 * 2 if message was transferred and the child was started and
306 * dont_exec was zero.
307 * 3 serious error. Permissions denied or kernel couldn't be
310 static int deliver (int argc, char **argv, int id, struct str_queue *queue,
311 GIP *gip, pid_t *pidp, int dont_exec)
316 char fifo_server_name[128];
317 char fifo_client_name[128];
321 sprintf (fifo_server_name, "fifo.s.%d", id);
322 sprintf (fifo_client_name, "fifo.c.%d", id);
326 *gip = gipc_initialize (fifo_client_name);
328 oldsig = signal (SIGPIPE, pipe_handle);
332 { /* assume child is running */
333 gipc_close (*gip); /* shut down existing FIFOs */
334 r = gipc_open (*gip, fifo_server_name, 0); /* try re-open ... */
337 { /* assume child is NOT running */
341 { /* we aren't allowed to start */
342 signal (SIGPIPE, oldsig);
345 mknod (fifo_server_name, S_IFIFO|0666, 0);
346 pid = start_kernel (argc, argv, id);
347 if (pidp) /* set pid of child */
349 r = gipc_open (*gip, fifo_server_name, 1);
352 { /* message couldn't be transferred */
353 signal (SIGPIPE, oldsig);
354 gw_log (GW_LOG_WARN, module, "Cannot start kernel");
357 if (r < 0) /* gipc_open fail? */
360 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
362 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
364 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "gipc_open");
365 longjmp (retry_jmp, 1); /* yet another pass */
367 index = 0; /* transfer. may be interrupted */
368 while ((msg = str_queue_get (queue, index++)))
370 gw_log (GW_LOG_DEBUG, module, "deliver: %s", msg);
371 gip_wline (*gip, msg);
373 signal (SIGPIPE, oldsig);
374 return pass; /* successful transfer */
378 * monitor_events: Event loop of monitor
379 * argc: argc of monitor (used in exec of Email kernel children)
380 * argv: argv of monitor (used in exec of Email kernel children)
382 static void monitor_events (int argc, char **argv)
385 int r, gip_m_fd, too_many;
388 char command[128], *cp;
390 mknod (MONITOR_FIFO_C, S_IFIFO|0666, 0);
391 open (MONITOR_FIFO_C, O_RDONLY|O_NONBLOCK);
392 gip_m = gips_initialize (MONITOR_FIFO_S);
393 r = gips_open (gip_m, MONITOR_FIFO_C, 0);
394 gip_m_fd = gip_infileno (gip_m);
405 gw_log (GW_LOG_STAT, module, "Got SIGHUP. Reading resources");
411 gw_log (GW_LOG_STAT, module, "Got SIGTERM. Exiting...");
412 unlink (MONITOR_FIFO_S);
413 unlink (MONITOR_FIFO_C);
418 gw_log (GW_LOG_STAT, module, "Got SIGINT. Exiting...");
419 unlink (MONITOR_FIFO_S);
420 unlink (MONITOR_FIFO_C);
423 /* deliver any unsent messages to Email kernels */
425 for (ki = ke_info_list; ki; ki = ki->next)
427 if (!ki->queue || ki->stopped)
429 gw_log (GW_LOG_DEBUG, module, "Transfer mail to %d", ki->id);
430 r = deliver (argc, argv, ki->id, ki->queue, &ki->gip, &ki->pid,
431 no_process >= max_process);
432 if (r == 2) /* new child was spawned? */
435 gw_log (GW_LOG_DEBUG, module, "Start of %d", ki->id);
437 if (r == 1 || r == 2) /* transfer at all? */
438 str_queue_rm (&ki->queue);
439 if (r == 0) /* too many pending? */
444 gw_log (GW_LOG_DEBUG, module, "%d too many pending",
446 for (ki = ke_info_list; ki; ki = ki->next)
448 if (!ki->queue && ki->pid != -1 && !ki->stopped)
450 if (!(ki->queue = str_queue_mk ()))
452 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module,
456 str_queue_enq (ki->queue, "stop\n");
457 str_queue_enq (ki->queue, "\001");
458 r = deliver (argc, argv, ki->id, ki->queue, &ki->gip,
461 gw_log (GW_LOG_DEBUG, module,
462 "Stop not sent: %d", r);
463 str_queue_rm (&ki->queue);
470 FD_SET (gip_m_fd, &set_r);
471 gw_log (GW_LOG_DEBUG, module, "set gip_m_fd %d", gip_m_fd);
474 for (ki = ke_info_list; ki; ki = ki->next)
480 { /* child has exited */
481 gw_log (GW_LOG_DEBUG, module, "Close of %d", ki->id);
482 gipc_close (ki->gip);
483 gipc_destroy (ki->gip);
487 else if ((fd = gip_infileno (ki->gip)) != -1)
488 { /* read select on child FIFO */
489 gw_log (GW_LOG_DEBUG, module, "set fd %d", fd);
496 gw_log (GW_LOG_DEBUG, module, "Cur/Max processes %d/%d",
497 no_process, max_process);
498 gw_log (GW_LOG_DEBUG, module, "IPC select");
499 r = select (fd_max+1, &set_r, NULL, NULL, NULL);
503 { /* select aborted. And it was not due to interrupt */
504 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "select");
507 /* select was interrupted. Probably child has died */
508 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "select");
510 /* go through list of Email kernels. See if any message has arrived */
511 gw_log (GW_LOG_DEBUG, module, "Testing ke_info_list");
512 for (ki = ke_info_list; ki; ki = ki->next)
515 if (ki->gip && (fd = gip_infileno (ki->gip)) != -1)
517 gw_log (GW_LOG_DEBUG, module, "Test of %d", fd);
518 if (FD_ISSET (fd, &set_r))
520 if (lgets (line_buf, sizeof(line_buf)-1, fd))
522 gw_log (GW_LOG_DEBUG, module, "IPC: %s", line_buf);
526 gw_log (GW_LOG_DEBUG, module, "Close of %d", ki->id);
527 gipc_close (ki->gip);
528 gipc_destroy (ki->gip);
535 /* see if any message from eti has arrived */
536 gw_log (GW_LOG_DEBUG, module, "Testing gip_m_fd %d", gip_m_fd);
537 if (FD_ISSET (gip_m_fd, &set_r))
539 gw_log (GW_LOG_DEBUG, module, "Reading from %d", gip_m_fd);
540 if (!(lgets (command, sizeof(command)-1, gip_m_fd)))
542 gw_log (GW_LOG_FATAL, module, "Unexpected close");
545 gw_log (GW_LOG_DEBUG, module, "Done");
546 if ((cp = strchr (command, '\n')))
548 gw_log (GW_LOG_DEBUG, module, "IPC: %s", command);
549 if (!memcmp (command, "eti ", 4))
551 int id = atoi (command+4);
552 struct ke_info *new_k;
554 new_k = ke_info_add (id);
555 gw_log (GW_LOG_DEBUG, module, "Incoming mail %d", id);
559 if (!(new_k->queue = str_queue_mk ()))
561 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module,
566 str_queue_enq (new_k->queue, "mail\n");
567 while (lgets (line_buf, sizeof(line_buf)-1, gip_m_fd))
568 str_queue_enq (new_k->queue, line_buf);
569 str_queue_enq (new_k->queue, "\001");
576 * main: main of monitor
578 int main (int argc, char **argv)
583 while (++argno < argc)
585 if (argv[argno][0] == '-')
587 if (argv[argno][1] == '-')
589 switch (argv[argno][1])
593 fprintf (stderr, "monitor [options] [resourceFile]"
594 " -- [kernelOptions]\n");
595 fprintf (stderr, "If no resource file is specified");
596 fprintf (stderr, " default.res is used\n");
597 fprintf (stderr, "Options:\n");
598 fprintf (stderr, " -l log Set Log file\n");
599 fprintf (stderr, " -d Enable debugging log\n");
600 fprintf (stderr, " -D Enable more debugging log\n");
601 fprintf (stderr, " -- Precedes kernel options\n");
602 fprintf (stderr, "Kernel options are transferred to kernel\n");
606 gw_log_file (GW_LOG_ALL, argv[argno]+2);
607 else if (++argno < argc)
608 gw_log_file (GW_LOG_ALL, argv[argno]);
611 fprintf (stderr, "%s: missing log filename\n", *argv);
616 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
619 gw_log_level (GW_LOG_ALL);
622 fprintf (stderr, "%s: unknown option `%s'; use -H for help\n",
628 default_res = argv[argno];
631 signal (SIGCHLD, catch_child);
632 signal (SIGHUP, catch_hup);
633 signal (SIGTERM, catch_term);
634 signal (SIGINT, catch_int);
635 monitor_events (argc-argno, argv+argno);