+static void mk_active (int userid)
+{
+ char active_name[1024];
+ char pid_buf[30];
+ int fd;
+
+ sprintf (active_name, "kernel.pid.%d", userid);
+ fd = open (active_name, O_WRONLY|O_CREAT, 0666);
+ if (fd == -1)
+ {
+ gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, "Cannot create %s", active_name);
+ exit (1);
+ }
+ sprintf (pid_buf, "%ld", (long) getpid());
+ write (fd, pid_buf, strlen(pid_buf));
+ close (fd);
+}
+
+static void rm_active (int userid)
+{
+ char active_name[1024];
+
+ sprintf (active_name, "kernel.pid.%d", userid);
+ unlink (active_name);
+}
+
+static void kernel_events (int userid)
+{
+ char fifo_client_name[1024];
+ char fifo_server_name[1024];
+ GIP gip;
+ fd_set set_r;
+ int r, gip_fd;
+ struct timeval tv;
+
+ gw_log (GW_LOG_DEBUG, KERNEL_LOG, "kernel event loop");
+
+ sprintf (fifo_client_name, "fifo.c.%d", userid);
+ sprintf (fifo_server_name, "fifo.s.%d", userid);
+
+ gip = gips_initialize (fifo_server_name);
+ gips_open (gip, fifo_client_name);
+ gip_fd = gip_infileno (gip);
+
+ while (1)
+ {
+ FD_ZERO (&set_r);
+ FD_SET (gip_fd, &set_r);
+ tv.tv_sec = 60;
+ tv.tv_usec = 0;
+
+ gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
+ r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
+ if (r == -1)
+ {
+ gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
+ exit (1);
+ }
+ if (r == 0)
+ {
+ gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout");
+ break;
+ }
+ if (FD_ISSET (gip_fd, &set_r))
+ {
+ char command[128], *cp;
+
+ if (!(lgets (command, 127, gip_fd)))
+ break;
+ if ((cp = strchr (command, '\n')))
+ *cp = '\0';
+ gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
+ if (!strcmp (command, "mail"))
+ {
+ gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail...\n");
+ urp (gip_fd);
+ }
+ else if (!strcmp (command, "stop"))
+ {
+ gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
+ break;
+ }
+ else
+ {
+ gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
+ }
+ }
+ }
+ gips_close (gip);
+ gips_destroy (gip);
+}
+