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