CCL def command, i.e. user definitions - saved as resource files.
[egate.git] / kernel / eti.c
index 183f0e3..7b028d0 100644 (file)
@@ -1,8 +1,40 @@
-/* Gateway kernel
+/* Gateway Email Transport Interface
  * Europagate, 1995
  *
  * $Log: eti.c,v $
- * Revision 1.2  1995/03/27 12:51:05  adam
+ * Revision 1.12  1995/05/03 07:37:35  adam
+ * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
+ * are used when possible.
+ *
+ * Revision 1.11  1995/05/01  16:26:56  adam
+ * More work on resource monitor.
+ *
+ * Revision 1.10  1995/05/01  12:43:29  adam
+ * First work on resource monitor program.
+ *
+ * Revision 1.9  1995/04/21  16:37:43  adam
+ * Parent (eti) creates BOTH FIFOs. dtbsun is more happy now.
+ *
+ * Revision 1.8  1995/04/20  15:12:24  adam
+ * Minor hacks really.
+ *
+ * Revision 1.7  1995/04/19  16:01:57  adam
+ * Some hacks to get the FIFO communication work!! Isn't reliable.
+ * Resource gw.account added - default account info.
+ *
+ * Revision 1.6  1995/04/17  09:34:27  adam
+ * Timeout (idletime) adjustable. Minor changes in kernel.
+ *
+ * Revision 1.5  1995/03/31  09:43:13  adam
+ * Removed unused variable.
+ *
+ * Revision 1.4  1995/03/28  11:42:34  adam
+ * First use of string-queue utility.
+ *
+ * Revision 1.3  1995/03/28  08:01:23  adam
+ * FIFO existence is used to test for a running kernel.
+ *
+ * Revision 1.2  1995/03/27  12:51:05  adam
  * New log level in use: GW_LOG_ERRNO.
  *
  * Revision 1.1  1995/03/27  08:24:00  adam
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <setjmp.h>
+#include <signal.h>
+
+#include <sys/file.h>
+#include <sys/stat.h>
 
 #include <gw-log.h>
 #include <gw-db.h>
 #include <gip.h>
+#include <strqueue.h>
 
 #define LINE_MAX 1024
-static char line_buf[LINE_MAX+1];
 
 static char *module = "eti";
+static jmp_buf retry_jmp;
+
+static void pipe_handle (int dummy)
+{
+    longjmp (retry_jmp, 1);
+}
 
-static int email_header (FILE *inf, char *from_str, char *subject_str)
+static int email_header (struct str_queue *sq,
+                         char *from_str, char *subject_str)
 {
+    char *msg;
+    int index = 0;
+
     *from_str = '\0';
-    *subject_str = '\0';
-    while (fgets (line_buf, LINE_MAX, inf))
+    *subject_str = '\0';    
+    while ((msg = str_queue_get (sq, index++)))
     {
-        if (line_buf[0] == '\n')
+        if (msg[0] == '\n')
             return 0;
-        if (strncmp (line_buf, "From ", 5) == 0)
-            sscanf (line_buf+4, "%s", from_str);
-        if (strncmp (line_buf, "Subject: ", 9) == 0 &&
-            sscanf (line_buf+9, "%s", subject_str+1) == 1)
-            strcpy (subject_str, line_buf+9);
+        if (memcmp (msg, "From ", 5) == 0)
+            sscanf (msg+4, "%s", from_str);
+        if (memcmp (msg, "Subject: ", 9) == 0 &&
+            sscanf (msg+9, "%s", subject_str+1) == 1)
+            strcpy (subject_str, msg+9);
     }
     return 1;
 }
 
-static int kernel_active (int id)
+#if !USE_MONITOR
+static void start_kernel (int argc, char **argv, int id)
 {
-    char active_name[1024];
-    int fd;
-
-    sprintf (active_name, "kernel.pid.%d", id);
-    fd = open (active_name, O_RDONLY);
-    if (fd == -1)
-        return 0;
-    close (fd);
-    return 1;
+    pid_t pid;
+    int i;
+    char **argv_p;
+    char userid_option[20];
+
+    argv_p = malloc (sizeof(*argv_p)*(argc+2));
+    if (!argv_p)
+    {
+       gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
+       exit (1);
+    }
+    argv_p[0] = "kernel";
+    for (i = 1; i<argc; i++)
+        argv_p[i] = argv[i];
+    sprintf (userid_option, "-i%d", id);
+    argv_p[i++] = userid_option;
+    argv_p[i++] = NULL;
+
+    gw_log (GW_LOG_DEBUG, module, "Starting kernel");
+    pid = fork ();
+    if (pid == -1)
+    {
+       gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
+       exit (1);
+    }
+    if (!pid)
+    {
+        execv ("kernel", argv_p);
+       gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
+       exit (1);
+    }
+}
+#endif
+
+static void deliver (struct str_queue *sq, GIP gip, const char *msg)
+{
+    int index = 0;
+
+    gip_wline (gip, msg);
+    while ((msg = str_queue_get (sq, index++)))
+        gip_wline (gip, msg);
+    gip_wline (gip, "\001");
 }
 
 int main (int argc, char **argv)
@@ -63,19 +144,31 @@ int main (int argc, char **argv)
     char from_str[LINE_MAX+1];
     char subject_str[LINE_MAX+1];
     char line_str[LINE_MAX+1];
+    char msg[20];
     GW_DB user_db;
     GIP  gip;
     void *user_info;
     size_t info_length;
     int  id;
     int  r;
+    static int pass = 0;
     char fifo_client_name[1024];
     char fifo_server_name[1024];
+    struct str_queue *queue;
 
     gw_log_init (*argv);
     gw_log_level (GW_LOG_ALL);
     gw_log_file (GW_LOG_ALL, "eti.log");
-    r = email_header (stdin, from_str, subject_str);
+
+    if (!(queue = str_queue_mk ()))
+    {
+        gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
+        exit (1);
+    }
+    while (fgets (line_str, LINE_MAX, stdin))
+        str_queue_enq (queue, line_str);
+
+    r = email_header (queue, from_str, subject_str);
     if (! *from_str)
     {
         gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
@@ -107,72 +200,78 @@ int main (int argc, char **argv)
     else
     {
         gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
+       exit (1);
     }
+#if USE_MONITOR
+    sprintf (fifo_server_name, "fifo.s.m");
+    sprintf (fifo_client_name, "fifo.c.m");
+#else
     sprintf (fifo_server_name, "fifo.s.%d", id);
     sprintf (fifo_client_name, "fifo.c.%d", id);
+#endif
 
     gip = gipc_initialize (fifo_client_name);
-    if (kernel_active (id))
+
+    signal (SIGPIPE, pipe_handle);
+    setjmp (retry_jmp);
+    ++pass;
+    signal (SIGPIPE, pipe_handle);
+    gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
+    if (pass == 1)
+        r = gipc_open (gip, fifo_server_name, 0);
+    else if (pass == 2)
     {
-        gw_log (GW_LOG_DEBUG, module, "Kernel already active");
-        gipc_open (gip, fifo_server_name, 0);
+#if USE_MONITOR
+        gw_log (GW_LOG_FATAL, module, "Cannot contact monitor");
+       exit (1);
+#else
+#if 0
+        gipc_close (gip);
+        gipc_destroy (gip);
+        unlink (fifo_server_name);
+        unlink (fifo_client_name);
+        gip = gipc_initialize (fifo_client_name);
+#endif
+
+       mknod (fifo_server_name, S_IFIFO|0666, 0);
+        start_kernel (argc, argv, id);
+        r = gipc_open (gip, fifo_server_name, 1);
     }
-    else
+    else if (pass == 3)
     {
-        pid_t pid;
-       int i;
-       char **argv_p;
-       char userid_option[20];
-
-        argv_p = malloc (sizeof(*argv_p)*(argc+2));
-       if (!argv_p)
+        gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
+       exit (1);
+#endif
+    }
+    if (r < 0)
+        if (r == -2)
        {
-           gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "Malloc fail");
-           exit (1);
+           gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
+           longjmp (retry_jmp, 1);
        }
-       argv_p[0] = "kernel";
-        for (i = 1; i<argc; i++)
-            argv_p[i] = argv[i];
-       sprintf (userid_option, "-i%d", id);
-       argv_p[i++] = userid_option;
-       argv_p[i++] = NULL;
-
-        gw_log (GW_LOG_DEBUG, module, "Kernel not active");
-       pid = fork ();
-       if (pid == -1)
+       else if (r == -1)
        {
-           gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
-           exit (1);
+           gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
+           longjmp (retry_jmp, 1);
        }
-       if (!pid)
+       else
        {
-            execvp ("kernel", argv_p);
-           gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
+           gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
            exit (1);
        }
-        gipc_open (gip, fifo_server_name, 1);
-    }
     /* deliver message ... */
-    gw_log (GW_LOG_DEBUG, module, "Deliver mail header");
-    gip_wline (gip, "mail\n");
-    gip_wline (gip, "From ");
-    gip_wline (gip, from_str);
-    gip_wline (gip, "\n");
-    if (*subject_str)
-    {
-        gip_wline (gip, "Subject: ");
-       gip_wline (gip, subject_str);
-       gip_wline (gip, "\n");
-    }
-    gip_wline (gip, "\n");
-    gw_log (GW_LOG_DEBUG, module, "Deliver mail body");
-    while (fgets (line_str, LINE_MAX, stdin))
-        gip_wline (gip, line_str);
-    gip_wline (gip, "\001");
+    gw_log (GW_LOG_DEBUG, module, "Delivering mail");
+#if USE_MONITOR
+    sprintf (msg, "eti %d\n", id);
+#else
+    strcpy (msg, "mail\n");
+#endif
+    deliver (queue, gip, msg);
     gw_log (GW_LOG_DEBUG, module, "Closing");
     gipc_close (gip);
     gipc_destroy (gip);
     gw_db_close (user_db);
+    gw_log (GW_LOG_DEBUG, module, "Exiting");
     exit (0);
 }