More work on resource monitor.
[egate.git] / kernel / main.c
1 /* Gateway kernel - Main
2  * Europagate, 1995
3  *
4  * $Log: main.c,v $
5  * Revision 1.21  1995/05/01 16:26:56  adam
6  * More work on resource monitor.
7  *
8  * Revision 1.20  1995/05/01  12:43:32  adam
9  * First work on resource monitor program.
10  *
11  * Revision 1.19  1995/04/19  16:01:58  adam
12  * Some hacks to get the FIFO communication work!! Isn't reliable.
13  * Resource gw.account added - default account info.
14  *
15  * Revision 1.18  1995/04/19  13:19:09  adam
16  * New command: account - for authentication.
17  *
18  * Revision 1.17  1995/04/19  10:46:18  adam
19  * Persistency works much better now. New command: status - history-like
20  *
21  * Revision 1.16  1995/04/19  07:31:07  adam
22  * First work on Z39.50 persistence.
23  *
24  * Revision 1.15  1995/04/17  09:34:30  adam
25  * Timeout (idletime) adjustable. Minor changes in kernel.
26  *
27  * Revision 1.14  1995/03/28  11:42:34  adam
28  * First use of string-queue utility.
29  *
30  * Revision 1.13  1995/03/28  08:01:25  adam
31  * FIFO existence is used to test for a running kernel.
32  *
33  * Revision 1.12  1995/03/27  12:51:05  adam
34  * New log level in use: GW_LOG_ERRNO.
35  *
36  * Revision 1.11  1995/03/27  08:24:02  adam
37  * First use of gip interface and gw-db.
38  * First work on eti program.
39  *
40  * Revision 1.10  1995/03/01  14:32:25  adam
41  * Better diagnostics. Default is, that only one database selected when
42  * several are known.
43  *
44  * Revision 1.9  1995/02/23  08:32:17  adam
45  * Changed header.
46  *
47  * Revision 1.7  1995/02/22  15:22:33  adam
48  * Much more checking of run-time state. Show command never retrieves
49  * more records than indicated by the previous search request. Help
50  * command available. The maximum number of records retrieved can be
51  * controlled now.
52  *
53  * Revision 1.6  1995/02/22  08:51:34  adam
54  * Output function can be customized in fml, which is used to print
55  * the reply to reply_fd.
56  *
57  * Revision 1.5  1995/02/20  21:16:20  adam
58  * FML support. Bug fixes. Profile for drewdb.
59  *
60  * Revision 1.4  1995/02/17  17:06:16  adam
61  * Minor changes.
62  *
63  * Revision 1.3  1995/02/16  18:35:09  adam
64  * First use of Zdist library. Search requests are supported.
65  * Present requests are not supported yet.
66  *
67  * Revision 1.2  1995/02/16  13:21:00  adam
68  * Organization of resource files for targets and conversion
69  * language implemented.
70  *
71  * Revision 1.1  1995/02/15  17:45:29  adam
72  * First version of email gateway kernel. Email requests are read
73  * from stdin. The output is transferred to an MTA if 'From' is
74  * found in the header - or stdout if absent. No Z39.50 client is used.
75  *
76  */
77
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <assert.h>
82 #include <unistd.h>
83 #include <sys/types.h>
84 #include <sys/time.h>
85 #include <fcntl.h>
86
87 #include <gip.h>
88 #include <strqueue.h>
89 #include "kernel.h"
90
91 FILE *reply_fd = stdout;
92
93 struct gw_kernel_info info;
94
95 static void kernel_events (struct str_queue *queue, int userid)
96 {
97     char fifo_client_name[1024];
98     char fifo_server_name[1024];
99     char line_buf[1024];
100     GIP gip;
101     fd_set set_r;
102     int r, gip_fd;
103     struct timeval tv;
104     int timeout;
105     int continuation = 0;
106     int extra_fd;
107
108     timeout = atoi(gw_res_get (info.kernel_res, "gw.timeout", "600"));
109     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "event loop");
110
111     sprintf (fifo_client_name, "fifo.c.%d", userid);
112     sprintf (fifo_server_name, "fifo.s.%d", userid);
113
114     gip = gips_initialize (fifo_server_name);
115     gips_open (gip, fifo_client_name);
116     gip_fd = gip_infileno (gip);
117     extra_fd = open (fifo_server_name, O_WRONLY);
118
119     while (1)
120     {
121         FD_ZERO (&set_r);
122         FD_SET (gip_fd, &set_r);
123         tv.tv_sec = timeout;
124         tv.tv_usec = 0;
125
126         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
127         r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
128         if (r == -1)
129         {
130             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
131             exit (1);
132         }
133         if (r == 0)
134         {
135             gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds", 
136                     timeout);
137             if (info.zass)
138                 save_p_state (userid);
139             break;
140         }
141         if (FD_ISSET (gip_fd, &set_r))
142         {
143             char command[128], *cp;
144
145             if (!(lgets (command, 127, gip_fd)))
146             {
147                 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
148                 break;
149             }
150             if ((cp = strchr (command, '\n')))
151                 *cp = '\0';
152             gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
153             if (!strcmp (command, "mail"))
154             {
155                 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail");
156                 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
157                     str_queue_enq (queue, line_buf);
158                 urp_start (continuation, queue);
159                 if (!continuation)
160                     load_p_state (userid);      
161                 urp_command (queue);
162                 urp_end ();
163                 while (str_queue_deq (queue, 0, 0))
164                     ;
165             }
166             else if (!strcmp (command, "stop"))
167             {
168                 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
169                 break;
170             }
171             else 
172             {
173                 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
174             }
175             continuation = 1;
176         }
177     }
178     close (extra_fd);
179     gips_close (gip);
180     gips_destroy (gip);
181     unlink (fifo_client_name);
182     unlink (fifo_server_name);
183 }
184
185 int main (int argc, char **argv)
186 {
187     int userid = -1;
188     struct str_queue *queue;
189
190     info.kernel_res = NULL;
191     info.default_res = "default.res";
192     info.override_res = NULL;
193     *info.target = 0;
194     *info.account = 0;
195     info.lang = NULL;
196     info.bibset = NULL;
197     info.zass = NULL;
198     info.override_portno = NULL;
199     info.override_hostname = NULL;
200     info.databases = NULL;
201     info.database = NULL;
202     info.setno = -1;
203 #if USE_FML
204     info.fml = NULL;
205 #endif
206     info.sets = NULL;
207
208     gw_log_init (*argv);
209     info.kernel_res = gw_res_init ();
210     while (--argc > 0)
211     {
212         if (**++argv == '-')
213         {
214             switch (argv[0][1])
215             {
216             case 'H':
217                 fprintf (stderr, "kernel [option..] [resource]\n");
218                 fprintf (stderr, "If no resource file is given");
219                 fprintf (stderr, " default.res is used\n");
220                 fprintf (stderr, "Options:\n");
221                 fprintf (stderr, " -d           Enable debugging log\n");
222                 fprintf (stderr, " -t target    Open target immediately\n");
223                 fprintf (stderr, " -g lang      Set language\n");
224                 fprintf (stderr, " -o resource  Override with resource\n");
225                 fprintf (stderr, " -h host      Override host\n");
226                 fprintf (stderr, " -p port      Override port\n");
227                 fprintf (stderr, " -l log       Set Log file\n");
228                 fprintf (stderr, " -i id        Set IPC userid\n");
229                 exit (1);
230             case 'd':
231                 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
232                 break;
233             case 'D':
234                 gw_log_level (GW_LOG_ALL);
235                 break;
236             case 't':
237                 if (argv[0][2])
238                     strcpy (info.target, argv[0]+2);
239                 else if (argc > 0)
240                 {
241                     --argc;
242                     strcpy (info.target, *++argv);
243                 }
244                 else
245                 {
246                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
247                     exit (1);
248                 }
249                 break;
250             case 'g':
251                 if (argv[0][2])
252                     info.lang = argv[0]+2;
253                 else if (argc > 0)
254                 {
255                     --argc;
256                     info.lang = *++argv;
257                 }
258                 else
259                 {
260                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
261                     exit (1);
262                 }
263                 break;
264             case 'o':
265                 if (argv[0][2])
266                     info.override_res = argv[0]+2;
267                 else if (argc > 0)
268                 {
269                     --argc;
270                     info.override_res = *++argv;
271                 }
272                 else
273                 {
274                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
275                     exit (1);
276                 }
277                 break;
278             case 'p':
279                 if (argv[0][2])
280                     info.override_portno = argv[0]+2;
281                 else if (argc > 0)
282                 {
283                     --argc;
284                     info.override_portno = *++argv;
285                 }
286                 else
287                 {
288                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
289                     exit (1);
290                 }
291                 break;
292             case 'h':
293                 if (argv[0][2])
294                     info.override_hostname = argv[0]+2;
295                 else if (argc > 0)
296                 {
297                     --argc;
298                     info.override_hostname = *++argv;
299                 }
300                 else
301                 {
302                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
303                     exit (1);
304                 }
305                 break;
306             case 'l':
307                 if (argv[0][2])
308                     gw_log_file (GW_LOG_ALL, argv[0]+2);
309                 else if (argc > 0)
310                 {
311                     --argc;
312                     gw_log_file (GW_LOG_ALL, *++argv);
313                 }
314                 else
315                 {
316                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
317                     exit (1);
318                 }
319                 break;
320             case 'i':
321                 if (argv[0][2])
322                     userid = atoi (argv[0]+2);
323                 else if (argc > 0)
324                 {
325                     --argc;
326                     userid = atoi (*++argv);
327                 }
328                 else
329                 {
330                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
331                     exit (1);
332                 }
333                 break;
334             default:
335                 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
336                 exit (1);
337             }
338         }
339         else
340             info.default_res = *argv;
341     }
342     if (!(queue = str_queue_mk ()))
343     {
344         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
345         exit (1);
346     }
347     if (userid != -1)
348     {
349         read_kernel_res ();
350         kernel_events (queue, userid);
351     }
352     else
353     {
354         char line_buf[512];
355         read_kernel_res ();
356         while (lgets (line_buf, sizeof(line_buf)-1, 0))
357             str_queue_enq (queue, line_buf);
358         urp_start (0, queue);
359         urp_command (queue);
360         urp_end ();
361     }
362     return 0;
363 }
364
365 struct gw_user_set *user_set_add (const char *name, int hits, 
366                                   const char *database, 
367                                   struct ccl_rpn_node *rpn,
368                                   int present_flag,
369                                   const char *search_str)
370 {
371     struct gw_user_set *s;
372
373     s = malloc (sizeof (*s));
374     assert (s);
375
376     s->name = gw_strdup (name);
377     s->hits = hits;
378     s->database = gw_strdup (database);
379     s->rpn = rpn;
380     s->present_flag = present_flag;
381     s->search_str = gw_strdup (search_str);
382     s->prev = info.sets;
383     info.sets = s;
384     return s;
385 }
386
387 void user_set_init (void)
388 {
389     struct gw_user_set *s, *s1;
390
391     for (s = info.sets; s; s = s1)
392     {
393         free (s->name);
394         free (s->database);
395         ccl_rpn_delete (s->rpn);
396         s1 = s->prev;
397         free (s);
398     }
399     info.sets = NULL;
400 }
401
402 struct gw_user_set *user_set_search (const char *name)
403 {
404     struct gw_user_set *s;
405
406     if (!name)
407         return info.sets;
408     for (s = info.sets; s; s = s->prev)
409         if (!strcmp (s->name, name))
410             return s;
411     return NULL;
412 }
413
414 #if USE_FML
415 static void fml_inf_write (int ch)
416 {
417     putc (ch, reply_fd);
418 }
419 static FILE *fml_inf;
420
421 static int fml_inf_read (void)
422 {
423     return getc (fml_inf);
424 }
425 #endif
426
427 void read_kernel_res (void)
428 {
429     char path_prefix[128];
430     char fname[160];
431     const char *v;
432     char *cp;
433     char resource_name[256];
434
435     user_set_init ();
436
437     if (info.bibset)
438         ccl_qual_rm (&info.bibset);
439     info.bibset = ccl_qual_mk ();
440
441     if (info.kernel_res)
442         gw_res_close (info.kernel_res);
443     info.kernel_res = gw_res_init ();
444
445     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
446             info.default_res);
447     if (*info.target)
448         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
449                 info.target);
450     if (info.lang)
451         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
452                 info.lang);
453
454     if (gw_res_merge (info.kernel_res, info.default_res))
455     {
456         gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
457                 info.default_res);
458         return;
459     }
460     strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
461     
462     if (*info.target)
463     {
464         sprintf (resource_name, "gw.target.%s", info.target);
465         v = gw_res_get (info.kernel_res, resource_name, NULL);
466         if (v)
467         {
468             sprintf (fname, "%s/%s", path_prefix, v);
469             gw_res_merge (info.kernel_res, fname);
470         }
471     }
472     if (info.lang)
473     {
474         sprintf (resource_name, "gw.lang.%s", info.lang);
475         v = gw_res_get (info.kernel_res, resource_name, NULL);
476         if (v)
477         {
478             sprintf (fname, "%s/%s", path_prefix, v);
479             gw_res_merge (info.kernel_res, fname);
480         }
481     }
482     if (info.override_res)
483     {
484         sprintf (fname, "%s/%s", path_prefix, info.override_res);
485         gw_res_merge (info.kernel_res, fname);        
486     }
487     v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
488     if (v)
489     {
490         FILE *bib_inf;
491
492         sprintf (fname, "%s/%s", path_prefix, v);
493         bib_inf = fopen (fname, "r");
494         if (!bib_inf)
495             gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
496         else
497         {
498             gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
499             ccl_qual_file (info.bibset, bib_inf);
500             fclose (bib_inf);
501         }
502     }
503     sprintf (resource_name, "gw.target.%s", info.target);
504     if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
505     {
506         /* target is there, and there is no sub-resource for it... */
507         const char *split;
508
509         if ((split = strchr (info.target, ':')))
510         {
511             memcpy (info.hostname, info.target, split-info.target);
512             info.hostname[split-info.target] = '\0';
513             info.port = atoi (split+1);
514         }
515         else
516         {
517             strcpy (info.hostname, info.target);
518             info.port = atoi (gw_res_get
519                               (info.kernel_res, "gw.portno", "210"));
520         }
521     }
522     else
523     {
524         strncpy (info.hostname, gw_res_get (info.kernel_res,
525                                             "gw.hostname", "localhost"),
526                  sizeof(info.hostname)-1);
527         info.port = atoi (gw_res_get (info.kernel_res,
528                                       "gw.portno", "210"));
529         strcpy (info.account, gw_res_get (info.kernel_res, "gw.account", ""));
530     }
531     if (info.databases)
532         free (info.databases);
533     if (info.database)
534         free (info.database);
535     v = gw_res_get (info.kernel_res, "gw.databases", "");
536     info.databases = gw_strdup (v);
537     for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
538         *cp = ',';
539     v = gw_res_get (info.kernel_res, "gw.database", "");
540     if (*v == '\0' && *info.databases)
541     {
542         int len;
543         cp = strchr (info.databases, ',');
544         
545         len = cp ? (cp-info.databases) : strlen (info.databases);
546         info.database = malloc (len+1);
547         assert (info.database);
548         memcpy (info.database, info.databases, len);
549         info.database[len] = '\0';
550     }
551     else
552     {
553         info.database = gw_strdup (v);
554         for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
555             *cp = ',';
556     }
557     if (info.override_portno)
558         info.port = atoi (info.override_portno);
559     if (info.override_hostname)
560         strncpy (info.hostname, info.override_hostname,
561                  sizeof(info.hostname)-1);
562     v = gw_res_get (info.kernel_res, "gw.result.set", NULL);
563     info.setno = v ? -1 : 0;
564 #if USE_FML
565     if (!info.fml)
566     {
567         v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");    
568         sprintf (fname, "%s/%s", path_prefix, v);
569         fml_inf = fopen (fname, "r");
570         if (!fml_inf)
571             gw_log (GW_LOG_WARN, KERNEL_LOG,
572                     "cannot open fml script %s", fname);
573         else
574         {
575             info.fml = fml_open ();
576             info.fml->read_func = fml_inf_read;
577             info.fml->write_func = fml_inf_write;
578             fml_preprocess (info.fml);
579             fml_exec (info.fml);
580             fclose (fml_inf);
581         }
582     }
583 #endif
584 }