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