1 /* Gateway Resource Monitor
5 * Revision 1.1 1995/05/01 12:43:36 adam
6 * First work on resource monitor program.
22 #include <sys/types.h>
33 static char *module = "monitor";
34 static jmp_buf retry_jmp;
40 struct str_queue *queue;
44 struct ke_info *ke_info_list = NULL;
46 struct ke_info *ke_info_add (int id)
50 for (kip = &ke_info_list; *kip; kip= &(*kip)->next)
53 *kip = malloc (sizeof(**kip));
58 if (!((*kip)->queue = str_queue_mk ()))
60 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
66 void ke_info_del (void)
70 assert (ke_info_list);
72 str_queue_rm (&ki->queue);
73 ke_info_list = ki->next;
77 static void pipe_handle (int dummy)
79 longjmp (retry_jmp, 1);
82 static pid_t start_kernel (int argc, char **argv, int id)
87 char userid_option[20];
89 argv_p = malloc (sizeof(*argv_p)*(argc+2));
92 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
96 for (i = 1; i<argc; i++)
98 sprintf (userid_option, "-i%d", id);
99 argv_p[i++] = userid_option;
102 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
106 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
111 execv ("kernel", argv_p);
112 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
118 static void deliver (int argc, char **argv, int id, struct str_queue *queue,
124 char fifo_server_name[128];
125 char fifo_client_name[128];
129 sprintf (fifo_server_name, "fifo.s.%d", id);
130 sprintf (fifo_client_name, "fifo.c.%d", id);
134 *gip = gipc_initialize (fifo_client_name);
136 oldsig = signal (SIGPIPE, pipe_handle);
140 r = gipc_open (*gip, fifo_server_name, 0);
143 mknod (fifo_server_name, S_IFIFO|0666, 0);
144 start_kernel (argc, argv, id);
145 r = gipc_open (*gip, fifo_server_name, 1);
149 gw_log (GW_LOG_WARN, module, "Cannot start kernel");
156 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
157 longjmp (retry_jmp, 1);
161 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
162 longjmp (retry_jmp, 1);
166 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "gipc_open");
171 while ((msg = str_queue_get (queue, index++)))
172 gip_wline (*gip, msg);
173 signal (SIGPIPE, oldsig);
176 static void monitor_events (int argc, char **argv)
182 char command[128], *cp;
186 gip_m = gips_initialize ("fifo.s");
187 r = gips_open (gip_m, "fifo.c");
188 gip_m_fd = gip_infileno (gip_m);
189 open ("fifo.s", O_WRONLY);
197 FD_SET (gip_m_fd, &set_r);
200 for (ki = ke_info_list; ki; ki = ki->next)
204 gw_log (GW_LOG_DEBUG, module, "Transfer mail to %d", ki->id);
205 deliver (argc, argv, ki->id, ki->queue, &ki->gip);
206 str_queue_rm (&ki->queue);
208 for (ki = ke_info_list; ki; ki = ki->next)
211 if (ki->gip && (fd = gip_infileno (ki->gip)) != -1)
213 gw_log (GW_LOG_DEBUG, module, "set r %d", fd);
219 gw_log (GW_LOG_DEBUG, module, "Monitor select");
220 r = select (fd_max+1, &set_r, NULL, NULL, NULL);
223 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "select");
226 if (FD_ISSET (gip_m_fd, &set_r))
228 if (!(lgets (command, sizeof(command)-1, gip_m_fd)))
230 gw_log (GW_LOG_FATAL, module, "Unexpected close");
233 if ((cp = strchr (command, '\n')))
235 gw_log (GW_LOG_DEBUG, module, "IPC: %s", command);
236 if (!memcmp (command, "eti ", 4))
238 int id = atoi (command+4);
239 struct ke_info *new_k;
241 new_k = ke_info_add (id);
242 gw_log (GW_LOG_DEBUG, "module", "Incoming mail %d", id);
243 str_queue_enq (new_k->queue, "mail\n");
244 while (lgets (line_buf, sizeof(line_buf)-1, gip_m_fd))
245 str_queue_enq (new_k->queue, line_buf);
246 str_queue_enq (new_k->queue, "\001");
249 for (ki = ke_info_list; ki; ki = ki->next)
252 if (ki->gip && (fd = gip_infileno (ki->gip)) != -1 &&
253 FD_ISSET (fd, &set_r))
255 if (lgets (line_buf, sizeof(line_buf)-1, fd))
257 gw_log (GW_LOG_DEBUG, module, "IPC: %s", line_buf);
261 gw_log (GW_LOG_DEBUG, module, "Close of %d", ki->id);
262 gipc_close (ki->gip);
263 gipc_destroy (ki->gip);
271 int main (int argc, char **argv)
274 gw_log_level (GW_LOG_ALL);
275 gw_log_file (GW_LOG_ALL, "monitor.log");
277 monitor_events (argc, argv);