1 /* Gateway Email Transport Interface
5 * Revision 1.10 1995/05/01 12:43:29 adam
6 * First work on resource monitor program.
8 * Revision 1.9 1995/04/21 16:37:43 adam
9 * Parent (eti) creates BOTH FIFOs. dtbsun is more happy now.
11 * Revision 1.8 1995/04/20 15:12:24 adam
14 * Revision 1.7 1995/04/19 16:01:57 adam
15 * Some hacks to get the FIFO communication work!! Isn't reliable.
16 * Resource gw.account added - default account info.
18 * Revision 1.6 1995/04/17 09:34:27 adam
19 * Timeout (idletime) adjustable. Minor changes in kernel.
21 * Revision 1.5 1995/03/31 09:43:13 adam
22 * Removed unused variable.
24 * Revision 1.4 1995/03/28 11:42:34 adam
25 * First use of string-queue utility.
27 * Revision 1.3 1995/03/28 08:01:23 adam
28 * FIFO existence is used to test for a running kernel.
30 * Revision 1.2 1995/03/27 12:51:05 adam
31 * New log level in use: GW_LOG_ERRNO.
33 * Revision 1.1 1995/03/27 08:24:00 adam
34 * First use of gip interface and gw-db.
35 * First work on eti program.
59 static char *module = "eti";
60 static jmp_buf retry_jmp;
62 static void pipe_handle (int dummy)
64 longjmp (retry_jmp, 1);
67 static int email_header (struct str_queue *sq,
68 char *from_str, char *subject_str)
75 while ((msg = str_queue_get (sq, index++)))
79 if (memcmp (msg, "From ", 5) == 0)
80 sscanf (msg+4, "%s", from_str);
81 if (memcmp (msg, "Subject: ", 9) == 0 &&
82 sscanf (msg+9, "%s", subject_str+1) == 1)
83 strcpy (subject_str, msg+9);
89 static void start_kernel (int argc, char **argv, int id)
94 char userid_option[20];
96 argv_p = malloc (sizeof(*argv_p)*(argc+2));
99 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
102 argv_p[0] = "kernel";
103 for (i = 1; i<argc; i++)
105 sprintf (userid_option, "-i%d", id);
106 argv_p[i++] = userid_option;
109 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
113 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
118 execv ("kernel", argv_p);
119 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
125 static void deliver (struct str_queue *sq, GIP gip, const char *msg)
129 gip_wline (gip, msg);
130 while ((msg = str_queue_get (sq, index++)))
131 gip_wline (gip, msg);
132 gip_wline (gip, "\001");
135 int main (int argc, char **argv)
137 char from_str[LINE_MAX+1];
138 char subject_str[LINE_MAX+1];
139 char line_str[LINE_MAX+1];
148 char fifo_client_name[1024];
149 char fifo_server_name[1024];
150 struct str_queue *queue;
153 gw_log_level (GW_LOG_ALL);
154 gw_log_file (GW_LOG_ALL, "eti.log");
156 if (!(queue = str_queue_mk ()))
158 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
161 while (fgets (line_str, LINE_MAX, stdin))
162 str_queue_enq (queue, line_str);
164 r = email_header (queue, from_str, subject_str);
167 gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
170 gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
171 user_db = gw_db_open ("user.db", 1);
173 r = gw_db_lookup (user_db, from_str, strlen(from_str),
174 &user_info, &info_length);
177 gw_log (GW_LOG_STAT, module, "New user");
178 id = gw_db_seq_no (user_db);
179 r = gw_db_insert (user_db, from_str, strlen(from_str),
181 gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
185 if (sizeof(id) != info_length)
187 gw_log (GW_LOG_FATAL, module, "Bad id-size");
190 memcpy (&id, user_info, sizeof(id));
191 gw_log (GW_LOG_STAT, module, "Found with id %d", id);
195 gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
199 sprintf (fifo_server_name, "fifo.s");
200 sprintf (fifo_client_name, "fifo.c");
202 sprintf (fifo_server_name, "fifo.s.%d", id);
203 sprintf (fifo_client_name, "fifo.c.%d", id);
206 gip = gipc_initialize (fifo_client_name);
208 signal (SIGPIPE, pipe_handle);
211 signal (SIGPIPE, pipe_handle);
212 gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
214 r = gipc_open (gip, fifo_server_name, 0);
218 gw_log (GW_LOG_FATAL, module, "Cannot contact monitor");
224 unlink (fifo_server_name);
225 unlink (fifo_client_name);
226 gip = gipc_initialize (fifo_client_name);
228 mknod (fifo_server_name, S_IFIFO|0666, 0);
229 start_kernel (argc, argv, id);
230 r = gipc_open (gip, fifo_server_name, 1);
234 gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
241 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
242 longjmp (retry_jmp, 1);
246 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
247 longjmp (retry_jmp, 1);
251 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
254 /* deliver message ... */
255 gw_log (GW_LOG_DEBUG, module, "Delivering mail");
257 sprintf (msg, "eti %d\n", id);
259 strcpy (msg, "mail\n");
261 deliver (queue, gip, msg);
262 gw_log (GW_LOG_DEBUG, module, "Closing");
265 gw_db_close (user_db);
266 gw_log (GW_LOG_DEBUG, module, "Exiting");