1 /* Gateway Email Transport Interface
5 * Revision 1.11 1995/05/01 16:26:56 adam
6 * More work on resource monitor.
8 * Revision 1.10 1995/05/01 12:43:29 adam
9 * First work on resource monitor program.
11 * Revision 1.9 1995/04/21 16:37:43 adam
12 * Parent (eti) creates BOTH FIFOs. dtbsun is more happy now.
14 * Revision 1.8 1995/04/20 15:12:24 adam
17 * Revision 1.7 1995/04/19 16:01:57 adam
18 * Some hacks to get the FIFO communication work!! Isn't reliable.
19 * Resource gw.account added - default account info.
21 * Revision 1.6 1995/04/17 09:34:27 adam
22 * Timeout (idletime) adjustable. Minor changes in kernel.
24 * Revision 1.5 1995/03/31 09:43:13 adam
25 * Removed unused variable.
27 * Revision 1.4 1995/03/28 11:42:34 adam
28 * First use of string-queue utility.
30 * Revision 1.3 1995/03/28 08:01:23 adam
31 * FIFO existence is used to test for a running kernel.
33 * Revision 1.2 1995/03/27 12:51:05 adam
34 * New log level in use: GW_LOG_ERRNO.
36 * Revision 1.1 1995/03/27 08:24:00 adam
37 * First use of gip interface and gw-db.
38 * First work on eti program.
62 static char *module = "eti";
63 static jmp_buf retry_jmp;
65 static void pipe_handle (int dummy)
67 longjmp (retry_jmp, 1);
70 static int email_header (struct str_queue *sq,
71 char *from_str, char *subject_str)
78 while ((msg = str_queue_get (sq, index++)))
82 if (memcmp (msg, "From ", 5) == 0)
83 sscanf (msg+4, "%s", from_str);
84 if (memcmp (msg, "Subject: ", 9) == 0 &&
85 sscanf (msg+9, "%s", subject_str+1) == 1)
86 strcpy (subject_str, msg+9);
92 static void start_kernel (int argc, char **argv, int id)
97 char userid_option[20];
99 argv_p = malloc (sizeof(*argv_p)*(argc+2));
102 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
105 argv_p[0] = "kernel";
106 for (i = 1; i<argc; i++)
108 sprintf (userid_option, "-i%d", id);
109 argv_p[i++] = userid_option;
112 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
116 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
121 execv ("kernel", argv_p);
122 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
128 static void deliver (struct str_queue *sq, GIP gip, const char *msg)
132 gip_wline (gip, msg);
133 while ((msg = str_queue_get (sq, index++)))
134 gip_wline (gip, msg);
135 gip_wline (gip, "\001");
138 int main (int argc, char **argv)
140 char from_str[LINE_MAX+1];
141 char subject_str[LINE_MAX+1];
142 char line_str[LINE_MAX+1];
151 char fifo_client_name[1024];
152 char fifo_server_name[1024];
153 struct str_queue *queue;
156 gw_log_level (GW_LOG_ALL);
157 gw_log_file (GW_LOG_ALL, "eti.log");
159 if (!(queue = str_queue_mk ()))
161 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
164 while (fgets (line_str, LINE_MAX, stdin))
165 str_queue_enq (queue, line_str);
167 r = email_header (queue, from_str, subject_str);
170 gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
173 gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
174 user_db = gw_db_open ("user.db", 1);
176 r = gw_db_lookup (user_db, from_str, strlen(from_str),
177 &user_info, &info_length);
180 gw_log (GW_LOG_STAT, module, "New user");
181 id = gw_db_seq_no (user_db);
182 r = gw_db_insert (user_db, from_str, strlen(from_str),
184 gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
188 if (sizeof(id) != info_length)
190 gw_log (GW_LOG_FATAL, module, "Bad id-size");
193 memcpy (&id, user_info, sizeof(id));
194 gw_log (GW_LOG_STAT, module, "Found with id %d", id);
198 gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
202 sprintf (fifo_server_name, "fifo.s");
203 sprintf (fifo_client_name, "fifo.c");
205 sprintf (fifo_server_name, "fifo.s.%d", id);
206 sprintf (fifo_client_name, "fifo.c.%d", id);
209 gip = gipc_initialize (fifo_client_name);
211 signal (SIGPIPE, pipe_handle);
214 signal (SIGPIPE, pipe_handle);
215 gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
217 r = gipc_open (gip, fifo_server_name, 0);
221 gw_log (GW_LOG_FATAL, module, "Cannot contact monitor");
227 unlink (fifo_server_name);
228 unlink (fifo_client_name);
229 gip = gipc_initialize (fifo_client_name);
232 mknod (fifo_server_name, S_IFIFO|0666, 0);
233 start_kernel (argc, argv, id);
234 r = gipc_open (gip, fifo_server_name, 1);
238 gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
245 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
246 longjmp (retry_jmp, 1);
250 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
251 longjmp (retry_jmp, 1);
255 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
258 /* deliver message ... */
259 gw_log (GW_LOG_DEBUG, module, "Delivering mail");
261 sprintf (msg, "eti %d\n", id);
263 strcpy (msg, "mail\n");
265 deliver (queue, gip, msg);
266 gw_log (GW_LOG_DEBUG, module, "Closing");
269 gw_db_close (user_db);
270 gw_log (GW_LOG_DEBUG, module, "Exiting");