* Europagate, 1995
*
* $Log: eti.c,v $
- * Revision 1.2 1995/03/27 12:51:05 adam
+ * 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 <gw-log.h>
#include <gw-db.h>
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)
{
return 1;
}
-static int kernel_active (int id)
+static void start_kernel (int argc, char **argv, int id)
{
- char active_name[1024];
- int fd;
+ pid_t pid;
+ int i;
+ char **argv_p;
+ char userid_option[20];
- sprintf (active_name, "kernel.pid.%d", id);
- fd = open (active_name, O_RDONLY);
- if (fd == -1)
- return 0;
- close (fd);
- return 1;
+ 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)
+ {
+ execvp ("kernel", argv_p);
+ gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
+ exit (1);
+ }
}
int main (int argc, char **argv)
size_t info_length;
int id;
int r;
+ static int pass = 0;
char fifo_client_name[1024];
char fifo_server_name[1024];
sprintf (fifo_client_name, "fifo.c.%d", id);
gip = gipc_initialize (fifo_client_name);
- if (kernel_active (id))
+
+ signal (SIGPIPE, pipe_handle);
+ setjmp (retry_jmp);
+ ++pass;
+ 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);
+ gipc_close (gip);
+ 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);
+ }
+ if (r < 0)
+ if (r == -2)
{
- gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "Malloc fail");
- exit (1);
+ gw_log (GW_LOG_WARN|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_WARN|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");
+ gw_log (GW_LOG_DEBUG, module, "Delivering mail header");
gip_wline (gip, "mail\n");
gip_wline (gip, "From ");
gip_wline (gip, from_str);
gip_wline (gip, "\n");
}
gip_wline (gip, "\n");
- gw_log (GW_LOG_DEBUG, module, "Deliver mail body");
+ gw_log (GW_LOG_DEBUG, module, "Delivering mail body");
while (fgets (line_str, LINE_MAX, stdin))
gip_wline (gip, line_str);
gip_wline (gip, "\001");
* Europagate, 1995
*
* $Log: main.c,v $
- * Revision 1.12 1995/03/27 12:51:05 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
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)
{
char fifo_client_name[1024];
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);
}
gips_close (gip);
gips_destroy (gip);
+ unlink (fifo_client_name);
+ unlink (fifo_server_name);
}
int main (int argc, char **argv)
}
read_kernel_res ();
if (userid != -1)
- {
- mk_active (userid);
kernel_events (userid);
- rm_active (userid);
- }
else
urp (0);
return 0;