5 * Revision 1.3 1995/03/28 08:01:23 adam
6 * FIFO existence is used to test for a running kernel.
8 * Revision 1.2 1995/03/27 12:51:05 adam
9 * New log level in use: GW_LOG_ERRNO.
11 * Revision 1.1 1995/03/27 08:24:00 adam
12 * First use of gip interface and gw-db.
13 * First work on eti program.
32 static char line_buf[LINE_MAX+1];
34 static char *module = "eti";
35 static jmp_buf retry_jmp;
37 static void pipe_handle (int dummy)
39 longjmp (retry_jmp, 1);
42 static int email_header (FILE *inf, char *from_str, char *subject_str)
46 while (fgets (line_buf, LINE_MAX, inf))
48 if (line_buf[0] == '\n')
50 if (strncmp (line_buf, "From ", 5) == 0)
51 sscanf (line_buf+4, "%s", from_str);
52 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
53 sscanf (line_buf+9, "%s", subject_str+1) == 1)
54 strcpy (subject_str, line_buf+9);
59 static void start_kernel (int argc, char **argv, int id)
64 char userid_option[20];
66 argv_p = malloc (sizeof(*argv_p)*(argc+2));
69 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
73 for (i = 1; i<argc; i++)
75 sprintf (userid_option, "-i%d", id);
76 argv_p[i++] = userid_option;
79 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
83 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
88 execvp ("kernel", argv_p);
89 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
94 int main (int argc, char **argv)
96 char from_str[LINE_MAX+1];
97 char subject_str[LINE_MAX+1];
98 char line_str[LINE_MAX+1];
106 char fifo_client_name[1024];
107 char fifo_server_name[1024];
110 gw_log_level (GW_LOG_ALL);
111 gw_log_file (GW_LOG_ALL, "eti.log");
112 r = email_header (stdin, from_str, subject_str);
115 gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
118 gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
119 user_db = gw_db_open ("user.db", 1);
121 r = gw_db_lookup (user_db, from_str, strlen(from_str),
122 &user_info, &info_length);
125 gw_log (GW_LOG_STAT, module, "New user");
126 id = gw_db_seq_no (user_db);
127 r = gw_db_insert (user_db, from_str, strlen(from_str),
129 gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
133 if (sizeof(id) != info_length)
135 gw_log (GW_LOG_FATAL, module, "Bad id-size");
138 memcpy (&id, user_info, sizeof(id));
139 gw_log (GW_LOG_STAT, module, "Found with id %d", id);
143 gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
145 sprintf (fifo_server_name, "fifo.s.%d", id);
146 sprintf (fifo_client_name, "fifo.c.%d", id);
148 gip = gipc_initialize (fifo_client_name);
150 signal (SIGPIPE, pipe_handle);
153 gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
155 r = gipc_open (gip, fifo_server_name, 0);
159 start_kernel (argc, argv, id);
160 r = gipc_open (gip, fifo_server_name, 1);
164 gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
170 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "r==-2");
171 longjmp (retry_jmp, 1);
175 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "r==-1");
176 longjmp (retry_jmp, 1);
180 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
183 /* deliver message ... */
184 gw_log (GW_LOG_DEBUG, module, "Delivering mail header");
185 gip_wline (gip, "mail\n");
186 gip_wline (gip, "From ");
187 gip_wline (gip, from_str);
188 gip_wline (gip, "\n");
191 gip_wline (gip, "Subject: ");
192 gip_wline (gip, subject_str);
193 gip_wline (gip, "\n");
195 gip_wline (gip, "\n");
196 gw_log (GW_LOG_DEBUG, module, "Delivering mail body");
197 while (fgets (line_str, LINE_MAX, stdin))
198 gip_wline (gip, line_str);
199 gip_wline (gip, "\001");
200 gw_log (GW_LOG_DEBUG, module, "Closing");
203 gw_db_close (user_db);