Minor changes.
[egate.git] / kernel / main.c
index 10dc733..e6d0081 100644 (file)
@@ -2,7 +2,13 @@
  * Europagate, 1995
  *
  * $Log: main.c,v $
- * Revision 1.12  1995/03/27 12:51:05  adam
+ * Revision 1.14  1995/03/28 11:42:34  adam
+ * First use of string-queue utility.
+ *
+ * Revision 1.13  1995/03/28  08:01:25  adam
+ * FIFO existence is used to test for a running kernel.
+ *
+ * Revision 1.12  1995/03/27  12:51:05  adam
  * New log level in use: GW_LOG_ERRNO.
  *
  * Revision 1.11  1995/03/27  08:24:02  adam
 #include <fcntl.h>
 
 #include <gip.h>
+#include <strqueue.h>
 #include "kernel.h"
 
 FILE *reply_fd = stdout;
 
 struct gw_kernel_info info;
 
-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)
+static void kernel_events (struct str_queue *queue, int userid)
 {
     char fifo_client_name[1024];
     char fifo_server_name[1024];
+    char line_buf[1024];
     GIP gip;
     fd_set set_r;
     int r, gip_fd;
@@ -131,14 +113,21 @@ static void kernel_events (int userid)
             char command[128], *cp;
 
            if (!(lgets (command, 127, gip_fd)))
+           {
+               gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
                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);
+                while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
+                    str_queue_enq (queue, line_buf);
+                urp (queue);
+                while (str_queue_deq (queue, 0, 0))
+                    ;
            }
            else if (!strcmp (command, "stop"))
            {
@@ -153,11 +142,14 @@ static void kernel_events (int userid)
     }
     gips_close (gip);
     gips_destroy (gip);
+    unlink (fifo_client_name);
+    unlink (fifo_server_name);
 }
 
 int main (int argc, char **argv)
 {
     int userid = -1;
+    struct str_queue *queue;
 
     info.kernel_res = NULL;
     info.default_res = "default.res";
@@ -307,14 +299,20 @@ int main (int argc, char **argv)
             info.default_res = *argv;
     }
     read_kernel_res ();
-    if (userid != -1)
+    if (!(queue = str_queue_mk ()))
     {
-        mk_active (userid);
-        kernel_events (userid);
-       rm_active (userid);
+        gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
+        exit (1);
     }
+    if (userid != -1)
+        kernel_events (queue, userid);
     else
-        urp (0);
+    {
+        char line_buf[512];
+        while (lgets (line_buf, sizeof(line_buf)-1, 0))
+            str_queue_enq (queue, line_buf);
+        urp (queue);
+    }
     return 0;
 }