1 /* Gateway Email Transport Interface
5 * Revision 1.12 1995/05/03 07:37:35 adam
6 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
7 * are used when possible.
9 * Revision 1.11 1995/05/01 16:26:56 adam
10 * More work on resource monitor.
12 * Revision 1.10 1995/05/01 12:43:29 adam
13 * First work on resource monitor program.
15 * Revision 1.9 1995/04/21 16:37:43 adam
16 * Parent (eti) creates BOTH FIFOs. dtbsun is more happy now.
18 * Revision 1.8 1995/04/20 15:12:24 adam
21 * Revision 1.7 1995/04/19 16:01:57 adam
22 * Some hacks to get the FIFO communication work!! Isn't reliable.
23 * Resource gw.account added - default account info.
25 * Revision 1.6 1995/04/17 09:34:27 adam
26 * Timeout (idletime) adjustable. Minor changes in kernel.
28 * Revision 1.5 1995/03/31 09:43:13 adam
29 * Removed unused variable.
31 * Revision 1.4 1995/03/28 11:42:34 adam
32 * First use of string-queue utility.
34 * Revision 1.3 1995/03/28 08:01:23 adam
35 * FIFO existence is used to test for a running kernel.
37 * Revision 1.2 1995/03/27 12:51:05 adam
38 * New log level in use: GW_LOG_ERRNO.
40 * Revision 1.1 1995/03/27 08:24:00 adam
41 * First use of gip interface and gw-db.
42 * First work on eti program.
66 static char *module = "eti";
67 static jmp_buf retry_jmp;
69 static void pipe_handle (int dummy)
71 longjmp (retry_jmp, 1);
74 static int email_header (struct str_queue *sq,
75 char *from_str, char *subject_str)
82 while ((msg = str_queue_get (sq, index++)))
86 if (memcmp (msg, "From ", 5) == 0)
87 sscanf (msg+4, "%s", from_str);
88 if (memcmp (msg, "Subject: ", 9) == 0 &&
89 sscanf (msg+9, "%s", subject_str+1) == 1)
90 strcpy (subject_str, msg+9);
96 static void start_kernel (int argc, char **argv, int id)
101 char userid_option[20];
103 argv_p = malloc (sizeof(*argv_p)*(argc+2));
106 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
109 argv_p[0] = "kernel";
110 for (i = 1; i<argc; i++)
112 sprintf (userid_option, "-i%d", id);
113 argv_p[i++] = userid_option;
116 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
120 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
125 execv ("kernel", argv_p);
126 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
132 static void deliver (struct str_queue *sq, GIP gip, const char *msg)
136 gip_wline (gip, msg);
137 while ((msg = str_queue_get (sq, index++)))
138 gip_wline (gip, msg);
139 gip_wline (gip, "\001");
142 int main (int argc, char **argv)
144 char from_str[LINE_MAX+1];
145 char subject_str[LINE_MAX+1];
146 char line_str[LINE_MAX+1];
155 char fifo_client_name[1024];
156 char fifo_server_name[1024];
157 struct str_queue *queue;
160 gw_log_level (GW_LOG_ALL);
161 gw_log_file (GW_LOG_ALL, "eti.log");
163 if (!(queue = str_queue_mk ()))
165 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
168 while (fgets (line_str, LINE_MAX, stdin))
169 str_queue_enq (queue, line_str);
171 r = email_header (queue, from_str, subject_str);
174 gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
177 gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
178 user_db = gw_db_open ("user.db", 1);
180 r = gw_db_lookup (user_db, from_str, strlen(from_str),
181 &user_info, &info_length);
184 gw_log (GW_LOG_STAT, module, "New user");
185 id = gw_db_seq_no (user_db);
186 r = gw_db_insert (user_db, from_str, strlen(from_str),
188 gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
192 if (sizeof(id) != info_length)
194 gw_log (GW_LOG_FATAL, module, "Bad id-size");
197 memcpy (&id, user_info, sizeof(id));
198 gw_log (GW_LOG_STAT, module, "Found with id %d", id);
202 gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
206 sprintf (fifo_server_name, "fifo.s.m");
207 sprintf (fifo_client_name, "fifo.c.m");
209 sprintf (fifo_server_name, "fifo.s.%d", id);
210 sprintf (fifo_client_name, "fifo.c.%d", id);
213 gip = gipc_initialize (fifo_client_name);
215 signal (SIGPIPE, pipe_handle);
218 signal (SIGPIPE, pipe_handle);
219 gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
221 r = gipc_open (gip, fifo_server_name, 0);
225 gw_log (GW_LOG_FATAL, module, "Cannot contact monitor");
231 unlink (fifo_server_name);
232 unlink (fifo_client_name);
233 gip = gipc_initialize (fifo_client_name);
236 mknod (fifo_server_name, S_IFIFO|0666, 0);
237 start_kernel (argc, argv, id);
238 r = gipc_open (gip, fifo_server_name, 1);
242 gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
249 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
250 longjmp (retry_jmp, 1);
254 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
255 longjmp (retry_jmp, 1);
259 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
262 /* deliver message ... */
263 gw_log (GW_LOG_DEBUG, module, "Delivering mail");
265 sprintf (msg, "eti %d\n", id);
267 strcpy (msg, "mail\n");
269 deliver (queue, gip, msg);
270 gw_log (GW_LOG_DEBUG, module, "Closing");
273 gw_db_close (user_db);
274 gw_log (GW_LOG_DEBUG, module, "Exiting");