FIFO existence is used to test for a running kernel.
[egate.git] / kernel / main.c
index 428ffac..fd6ef7e 100644 (file)
@@ -2,7 +2,24 @@
  * Europagate, 1995
  *
  * $Log: main.c,v $
- * Revision 1.7  1995/02/22 15:22:33  adam
+ * 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
+ * First use of gip interface and gw-db.
+ * First work on eti program.
+ *
+ * Revision 1.10  1995/03/01  14:32:25  adam
+ * Better diagnostics. Default is, that only one database selected when
+ * several are known.
+ *
+ * Revision 1.9  1995/02/23  08:32:17  adam
+ * Changed header.
+ *
+ * Revision 1.7  1995/02/22  15:22:33  adam
  * Much more checking of run-time state. Show command never retrieves
  * more records than indicated by the previous search request. Help
  * command available. The maximum number of records retrieved can be
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <fcntl.h>
 
+#include <gip.h>
 #include "kernel.h"
 
 FILE *reply_fd = stdout;
 
 struct gw_kernel_info info;
 
+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)))
+           {
+               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);
+           }
+           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);
+    unlink (fifo_client_name);
+    unlink (fifo_server_name);
+}
+
 int main (int argc, char **argv)
 {
+    int userid = -1;
+
     info.kernel_res = NULL;
     info.default_res = "default.res";
     info.override_res = NULL;
@@ -56,6 +151,7 @@ int main (int argc, char **argv)
     info.override_portno = NULL;
     info.override_hostname = NULL;
     info.databases = NULL;
+    info.database = NULL;
 #if USE_FML
     info.fml = NULL;
 #endif
@@ -69,6 +165,20 @@ int main (int argc, char **argv)
         {
             switch (argv[0][1])
             {
+            case 'H':
+                fprintf (stderr, "kernel [option..] [resource]\n");
+                fprintf (stderr, "If no resource file is given");
+                fprintf (stderr, " default.res is used\n");
+                fprintf (stderr, "Options:\n");
+                fprintf (stderr, " -d           Enable debugging log\n");
+                fprintf (stderr, " -t target    Open target immediately\n");
+                fprintf (stderr, " -l lang      Set language\n");
+                fprintf (stderr, " -o resource  Override with resource\n");
+                fprintf (stderr, " -h host      Override host\n");
+                fprintf (stderr, " -p port      Override port\n");
+                fprintf (stderr, " -g log       Set Log file\n");
+                fprintf (stderr, " -i id        Set IPC userid\n");
+                exit (1);
             case 'd':
                 gw_log_level (GW_LOG_ALL);
                 break;
@@ -82,7 +192,7 @@ int main (int argc, char **argv)
                 }
                 else
                 {
-                    gw_log (GW_LOG_FATAL, "main", "missing target name");
+                    gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
                     exit (1);
                 }
                 break;
@@ -96,7 +206,7 @@ int main (int argc, char **argv)
                 }
                 else
                 {
-                    gw_log (GW_LOG_FATAL, "main", "missing language name");
+                    gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
                     exit (1);
                 }
                 break;
@@ -110,7 +220,7 @@ int main (int argc, char **argv)
                 }
                 else
                 {
-                    gw_log (GW_LOG_FATAL, "main", "missing language name");
+                    gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
                     exit (1);
                 }
                 break;
@@ -124,7 +234,7 @@ int main (int argc, char **argv)
                 }
                 else
                 {
-                    gw_log (GW_LOG_FATAL, "main", "missing portno");
+                    gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
                     exit (1);
                 }
                 break;
@@ -138,7 +248,7 @@ int main (int argc, char **argv)
                 }
                 else
                 {
-                    gw_log (GW_LOG_FATAL, "main", "missing hostname");
+                    gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
                     exit (1);
                 }
                 break;
@@ -152,12 +262,26 @@ int main (int argc, char **argv)
                 }
                 else
                 {
-                    gw_log (GW_LOG_FATAL, "main", "missing log filename");
+                    gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
                     exit (1);
                 }
                 break;
+           case 'i':
+                if (argv[0][2])
+                   userid = atoi (argv[0]+2);
+                else if (argc > 0)
+                {
+                    --argc;
+                   userid = atoi (*++argv);
+                }
+                else
+                {
+                    gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
+                   exit (1);
+               }
+               break;
             default:
-                gw_log (GW_LOG_FATAL, "main", "unknown option %s", *argv);
+                gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
                 exit (1);
             }
         }
@@ -165,7 +289,10 @@ int main (int argc, char **argv)
             info.default_res = *argv;
     }
     read_kernel_res ();
-    urp (stdin);
+    if (userid != -1)
+        kernel_events (userid);
+    else
+        urp (0);
     return 0;
 }
 
@@ -239,18 +366,18 @@ void read_kernel_res (void)
         gw_res_close (info.kernel_res);
     info.kernel_res = gw_res_init ();
 
-    gw_log (GW_LOG_DEBUG, "main", "reading kernel resource, default %s",
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
             info.default_res);
     if (*info.target)
-        gw_log (GW_LOG_DEBUG, "main", "reading kernel resource, target %s",
+        gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
                 info.target);
     if (info.lang)
-        gw_log (GW_LOG_DEBUG, "main", "reading kernel resource, lang %s",
+        gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
                 info.lang);
 
     if (gw_res_merge (info.kernel_res, info.default_res))
     {
-        gw_log (GW_LOG_WARN, "main", "Couldn't read resource file %s",
+        gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
                 info.default_res);
         return;
     }
@@ -289,10 +416,10 @@ void read_kernel_res (void)
         sprintf (fname, "%s/%s", path_prefix, v);
         bib_inf = fopen (fname, "r");
         if (!bib_inf)
-            gw_log (GW_LOG_WARN, "main", "cannot open %s", fname);
+            gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
         else
         {
-            gw_log (GW_LOG_DEBUG, "main", "reading bib file %s", fname);
+            gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
             ccl_qual_file (info.bibset, bib_inf);
             fclose (bib_inf);
         }
@@ -322,10 +449,30 @@ void read_kernel_res (void)
     }
     if (info.databases)
         free (info.databases);
+    if (info.database)
+        free (info.database);
     v = gw_res_get (info.kernel_res, "gw.databases", "");
     info.databases = gw_strdup (v);
     for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
         *cp = ',';
+    v = gw_res_get (info.kernel_res, "gw.database", "");
+    if (*v == '\0' && *info.databases)
+    {
+        int len;
+        cp = strchr (info.databases, ',');
+        
+        len = cp ? (cp-info.databases) : strlen (info.databases);
+        info.database = malloc (len+1);
+        assert (info.database);
+        memcpy (info.database, info.databases, len);
+        info.database[len] = '\0';
+    }
+    else
+    {
+        info.database = gw_strdup (v);
+        for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
+            *cp = ',';
+    }
     if (info.override_portno)
         info.port = atoi (info.override_portno);
     if (info.override_hostname)
@@ -338,7 +485,8 @@ void read_kernel_res (void)
         sprintf (fname, "%s/%s", path_prefix, v);
         fml_inf = fopen (fname, "r");
         if (!fml_inf)
-            gw_log (GW_LOG_WARN, "main", "cannot open fml script %s", fname);
+            gw_log (GW_LOG_WARN, KERNEL_LOG,
+                    "cannot open fml script %s", fname);
         else
         {
             info.fml = fml_open ();