1 /* Gateway Resource Monitor
5 * Revision 1.2 1995/05/01 16:26:57 adam
6 * More work on resource monitor.
8 * Revision 1.1 1995/05/01 12:43:36 adam
9 * First work on resource monitor program.
25 #include <sys/types.h>
37 static char *module = "monitor";
38 static jmp_buf retry_jmp;
44 struct str_queue *queue;
48 struct ke_info *ke_info_list = NULL;
50 struct ke_info *ke_info_add (int id)
54 for (kip = &ke_info_list; *kip; kip= &(*kip)->next)
57 *kip = malloc (sizeof(**kip));
62 if (!((*kip)->queue = str_queue_mk ()))
64 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
70 void ke_info_del (void)
74 assert (ke_info_list);
76 str_queue_rm (&ki->queue);
77 ke_info_list = ki->next;
81 static void catchchild (int num)
83 while (waitpid (-1, 0, WNOHANG) > 0)
85 signal (SIGCHLD, catchchild);
88 static void pipe_handle (int dummy)
90 longjmp (retry_jmp, 1);
93 static pid_t start_kernel (int argc, char **argv, int id)
98 char userid_option[20];
100 argv_p = malloc (sizeof(*argv_p)*(argc+2));
103 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
106 argv_p[0] = "kernel";
107 for (i = 1; i<argc; i++)
109 sprintf (userid_option, "-i%d", id);
110 argv_p[i++] = userid_option;
113 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
117 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
122 execv ("kernel", argv_p);
123 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
129 static void deliver (int argc, char **argv, int id, struct str_queue *queue,
135 char fifo_server_name[128];
136 char fifo_client_name[128];
140 sprintf (fifo_server_name, "fifo.s.%d", id);
141 sprintf (fifo_client_name, "fifo.c.%d", id);
145 *gip = gipc_initialize (fifo_client_name);
147 oldsig = signal (SIGPIPE, pipe_handle);
151 r = gipc_open (*gip, fifo_server_name, 0);
154 mknod (fifo_server_name, S_IFIFO|0666, 0);
155 start_kernel (argc, argv, id);
156 r = gipc_open (*gip, fifo_server_name, 1);
160 gw_log (GW_LOG_WARN, module, "Cannot start kernel");
167 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
168 longjmp (retry_jmp, 1);
172 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
173 longjmp (retry_jmp, 1);
177 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "gipc_open");
182 while ((msg = str_queue_get (queue, index++)))
183 gip_wline (*gip, msg);
184 signal (SIGPIPE, oldsig);
187 static void monitor_events (int argc, char **argv)
193 char command[128], *cp;
197 gip_m = gips_initialize ("fifo.s");
198 r = gips_open (gip_m, "fifo.c");
199 gip_m_fd = gip_infileno (gip_m);
200 open ("fifo.s", O_WRONLY);
208 FD_SET (gip_m_fd, &set_r);
209 gw_log (GW_LOG_DEBUG, module, "set r %d", gip_m_fd);
212 for (ki = ke_info_list; ki; ki = ki->next)
216 gw_log (GW_LOG_DEBUG, module, "Transfer mail to %d", ki->id);
217 deliver (argc, argv, ki->id, ki->queue, &ki->gip);
218 str_queue_rm (&ki->queue);
220 for (ki = ke_info_list; ki; ki = ki->next)
223 if (ki->gip && (fd = gip_infileno (ki->gip)) != -1)
225 gw_log (GW_LOG_DEBUG, module, "set r %d", fd);
233 gw_log (GW_LOG_DEBUG, module, "IPC select");
234 r = select (fd_max+1, &set_r, NULL, NULL, NULL);
239 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "select");
242 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "select");
244 gw_log (GW_LOG_DEBUG, module, "Testing ke_info_list");
245 for (ki = ke_info_list; ki; ki = ki->next)
248 if (ki->gip && (fd = gip_infileno (ki->gip)) != -1)
250 gw_log (GW_LOG_DEBUG, module, "Test of %d", fd);
251 if (FD_ISSET (fd, &set_r))
253 if (lgets (line_buf, sizeof(line_buf)-1, fd))
255 gw_log (GW_LOG_DEBUG, module, "IPC: %s", line_buf);
259 gw_log (GW_LOG_DEBUG, module, "Close of %d", ki->id);
260 gipc_close (ki->gip);
261 gipc_destroy (ki->gip);
268 gw_log (GW_LOG_DEBUG, module, "Testing gip_m_fd %d", gip_m_fd);
269 if (FD_ISSET (gip_m_fd, &set_r))
271 gw_log (GW_LOG_DEBUG, module, "Reading from %d", gip_m_fd);
272 if (!(lgets (command, sizeof(command)-1, gip_m_fd)))
274 gw_log (GW_LOG_FATAL, module, "Unexpected close");
277 gw_log (GW_LOG_DEBUG, module, "Done");
278 if ((cp = strchr (command, '\n')))
280 gw_log (GW_LOG_DEBUG, module, "IPC: %s", command);
281 if (!memcmp (command, "eti ", 4))
283 int id = atoi (command+4);
284 struct ke_info *new_k;
286 new_k = ke_info_add (id);
287 gw_log (GW_LOG_DEBUG, module, "Incoming mail %d", id);
288 str_queue_enq (new_k->queue, "mail\n");
289 while (lgets (line_buf, sizeof(line_buf)-1, gip_m_fd))
290 str_queue_enq (new_k->queue, line_buf);
291 str_queue_enq (new_k->queue, "\001");
297 int main (int argc, char **argv)
300 gw_log_level (GW_LOG_ALL);
302 signal (SIGCHLD, catchchild);
303 gw_log_file (GW_LOG_ALL, "monitor.log");
305 monitor_events (argc, argv);