5 * Revision 1.5 1995/03/31 09:43:13 adam
6 * Removed unused variable.
8 * Revision 1.4 1995/03/28 11:42:34 adam
9 * First use of string-queue utility.
11 * Revision 1.3 1995/03/28 08:01:23 adam
12 * FIFO existence is used to test for a running kernel.
14 * Revision 1.2 1995/03/27 12:51:05 adam
15 * New log level in use: GW_LOG_ERRNO.
17 * Revision 1.1 1995/03/27 08:24:00 adam
18 * First use of gip interface and gw-db.
19 * First work on eti program.
40 static char *module = "eti";
41 static jmp_buf retry_jmp;
43 static void pipe_handle (int dummy)
45 longjmp (retry_jmp, 1);
48 static int email_header (struct str_queue *sq,
49 char *from_str, char *subject_str)
56 while ((msg = str_queue_get (sq, index++)))
60 if (memcmp (msg, "From ", 5) == 0)
61 sscanf (msg+4, "%s", from_str);
62 if (memcmp (msg, "Subject: ", 9) == 0 &&
63 sscanf (msg+9, "%s", subject_str+1) == 1)
64 strcpy (subject_str, msg+9);
69 static void start_kernel (int argc, char **argv, int id)
74 char userid_option[20];
76 argv_p = malloc (sizeof(*argv_p)*(argc+2));
79 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
83 for (i = 1; i<argc; i++)
85 sprintf (userid_option, "-i%d", id);
86 argv_p[i++] = userid_option;
89 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
93 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
98 execvp ("kernel", argv_p);
99 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
104 static void deliver (struct str_queue *sq, GIP gip)
109 gip_wline (gip, "mail\n");
110 while ((msg = str_queue_get (sq, index++)))
111 gip_wline (gip, msg);
112 gip_wline (gip, "\001");
115 int main (int argc, char **argv)
117 char from_str[LINE_MAX+1];
118 char subject_str[LINE_MAX+1];
119 char line_str[LINE_MAX+1];
127 char fifo_client_name[1024];
128 char fifo_server_name[1024];
129 struct str_queue *queue;
132 gw_log_level (GW_LOG_ALL);
133 gw_log_file (GW_LOG_ALL, "eti.log");
135 if (!(queue = str_queue_mk ()))
137 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
140 while (fgets (line_str, LINE_MAX, stdin))
141 str_queue_enq (queue, line_str);
143 r = email_header (queue, from_str, subject_str);
146 gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
149 gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
150 user_db = gw_db_open ("user.db", 1);
152 r = gw_db_lookup (user_db, from_str, strlen(from_str),
153 &user_info, &info_length);
156 gw_log (GW_LOG_STAT, module, "New user");
157 id = gw_db_seq_no (user_db);
158 r = gw_db_insert (user_db, from_str, strlen(from_str),
160 gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
164 if (sizeof(id) != info_length)
166 gw_log (GW_LOG_FATAL, module, "Bad id-size");
169 memcpy (&id, user_info, sizeof(id));
170 gw_log (GW_LOG_STAT, module, "Found with id %d", id);
174 gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
176 sprintf (fifo_server_name, "fifo.s.%d", id);
177 sprintf (fifo_client_name, "fifo.c.%d", id);
179 gip = gipc_initialize (fifo_client_name);
181 signal (SIGPIPE, pipe_handle);
184 gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
186 r = gipc_open (gip, fifo_server_name, 0);
190 start_kernel (argc, argv, id);
191 r = gipc_open (gip, fifo_server_name, 1);
195 gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
201 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
202 longjmp (retry_jmp, 1);
206 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
207 longjmp (retry_jmp, 1);
211 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
214 /* deliver message ... */
215 gw_log (GW_LOG_DEBUG, module, "Delivering mail");
216 deliver (queue, gip);
217 gw_log (GW_LOG_DEBUG, module, "Closing");
220 gw_db_close (user_db);
221 gw_log (GW_LOG_DEBUG, module, "Exiting");