Bug fix: missing argument to gw_log-call.
[egate.git] / kernel / main.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /* Gateway kernel - Main
45  * Europagate, 1995
46  *
47  * $Log: main.c,v $
48  * Revision 1.29  1995/07/28 10:51:08  adam
49  * Bug fix: account from previous session had effect when new target command
50  * was executed.
51  *
52  * Revision 1.28  1995/07/03  08:20:15  adam
53  * More help information and better diagnostics.
54  *
55  * Revision 1.27  1995/05/19  13:25:59  adam
56  * Bug fixes. Better command line options.
57  *
58  * Revision 1.26  1995/05/18  12:03:08  adam
59  * Bug fixes and minor improvements.
60  *
61  * Revision 1.25  1995/05/16  09:40:42  adam
62  * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
63  *
64  * Revision 1.24  1995/05/04  10:40:07  adam
65  * More work on Def-settings.
66  *
67  * Revision 1.23  1995/05/03  16:34:18  adam
68  * CCL def command, i.e. user definitions - saved as resource files.
69  *
70  * Revision 1.22  1995/05/03  07:37:39  adam
71  * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
72  * are used when possible.
73  *
74  * Revision 1.21  1995/05/01  16:26:56  adam
75  * More work on resource monitor.
76  *
77  * Revision 1.20  1995/05/01  12:43:32  adam
78  * First work on resource monitor program.
79  *
80  * Revision 1.19  1995/04/19  16:01:58  adam
81  * Some hacks to get the FIFO communication work!! Isn't reliable.
82  * Resource gw.account added - default account info.
83  *
84  * Revision 1.18  1995/04/19  13:19:09  adam
85  * New command: account - for authentication.
86  *
87  * Revision 1.17  1995/04/19  10:46:18  adam
88  * Persistency works much better now. New command: status - history-like
89  *
90  * Revision 1.16  1995/04/19  07:31:07  adam
91  * First work on Z39.50 persistence.
92  *
93  * Revision 1.15  1995/04/17  09:34:30  adam
94  * Timeout (idletime) adjustable. Minor changes in kernel.
95  *
96  * Revision 1.14  1995/03/28  11:42:34  adam
97  * First use of string-queue utility.
98  *
99  * Revision 1.13  1995/03/28  08:01:25  adam
100  * FIFO existence is used to test for a running kernel.
101  *
102  * Revision 1.12  1995/03/27  12:51:05  adam
103  * New log level in use: GW_LOG_ERRNO.
104  *
105  * Revision 1.11  1995/03/27  08:24:02  adam
106  * First use of gip interface and gw-db.
107  * First work on eti program.
108  *
109  * Revision 1.10  1995/03/01  14:32:25  adam
110  * Better diagnostics. Default is, that only one database selected when
111  * several are known.
112  *
113  * Revision 1.9  1995/02/23  08:32:17  adam
114  * Changed header.
115  *
116  * Revision 1.7  1995/02/22  15:22:33  adam
117  * Much more checking of run-time state. Show command never retrieves
118  * more records than indicated by the previous search request. Help
119  * command available. The maximum number of records retrieved can be
120  * controlled now.
121  *
122  * Revision 1.6  1995/02/22  08:51:34  adam
123  * Output function can be customized in fml, which is used to print
124  * the reply to reply_fd.
125  *
126  * Revision 1.5  1995/02/20  21:16:20  adam
127  * FML support. Bug fixes. Profile for drewdb.
128  *
129  * Revision 1.4  1995/02/17  17:06:16  adam
130  * Minor changes.
131  *
132  * Revision 1.3  1995/02/16  18:35:09  adam
133  * First use of Zdist library. Search requests are supported.
134  * Present requests are not supported yet.
135  *
136  * Revision 1.2  1995/02/16  13:21:00  adam
137  * Organization of resource files for targets and conversion
138  * language implemented.
139  *
140  * Revision 1.1  1995/02/15  17:45:29  adam
141  * First version of email gateway kernel. Email requests are read
142  * from stdin. The output is transferred to an MTA if 'From' is
143  * found in the header - or stdout if absent. No Z39.50 client is used.
144  *
145  */
146
147 #include <stdio.h>
148 #include <stdlib.h>
149 #include <string.h>
150 #include <assert.h>
151 #include <unistd.h>
152 #include <sys/types.h>
153 #include <sys/time.h>
154 #include <fcntl.h>
155
156 #include <gip.h>
157 #include <strqueue.h>
158 #include "kernel.h"
159
160 FILE *reply_fd = stdout;
161
162 struct gw_kernel_info info;
163
164 static void kernel_events (struct str_queue *queue)
165 {
166     char fifo_client_name[1024];
167     char fifo_server_name[1024];
168     char line_buf[1024];
169     GIP gip;
170     fd_set set_r;
171     int r, gip_fd;
172     struct timeval tv;
173     int timeout;
174     int continuation = 0;
175     int extra_fd;
176     int persist_flag;
177     int stop_flag = 0;
178
179     persist_flag = gw_res_bool (info.kernel_res, "gw.persist", 0);
180     timeout = gw_res_int (info.kernel_res, "gw.timeout", 600);
181     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "event loop");
182
183     sprintf (fifo_client_name, "fifo.c.%d", info.userid);
184     sprintf (fifo_server_name, "fifo.s.%d", info.userid);
185
186     gip = gips_initialize (fifo_server_name);
187     gips_open (gip, fifo_client_name, 1);
188     gip_fd = gip_infileno (gip);
189     extra_fd = open (fifo_server_name, O_WRONLY);
190
191     while (1)
192     {
193         FD_ZERO (&set_r);
194         FD_SET (gip_fd, &set_r);
195         if (stop_flag)
196             tv.tv_sec = 1;
197         else
198             tv.tv_sec = timeout;
199         tv.tv_usec = 0;
200
201         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
202         r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
203         if (r == -1)
204         {
205             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
206             exit (1);
207         }
208         if (r == 0)
209         {
210             if (stop_flag)
211                 gw_log (GW_LOG_STAT, KERNEL_LOG, "Stopping");
212             else
213                 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds", 
214                         timeout);
215             if (info.zass && persist_flag)
216                 save_p_state (info.userid);
217             break;
218         }
219         if (FD_ISSET (gip_fd, &set_r))
220         {
221             char command[128], *cp;
222
223             if (!(lgets (command, 127, gip_fd)))
224             {
225                 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
226                 break;
227             }
228             if ((cp = strchr (command, '\n')))
229                 *cp = '\0';
230             gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
231             if (!strcmp (command, "mail"))
232             {
233                 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail");
234                 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
235                     str_queue_enq (queue, line_buf);
236                 urp_start (continuation, queue);
237                 if (persist_flag && !continuation)
238                     load_p_state (info.userid); 
239                 r = urp_command (queue);
240                 if (r == 1)                         /* stop? */
241                 {
242                     info.zass = NULL;               /* delete association */
243                     *info.target = 0;               /* indicate no target */
244                     read_kernel_res();              /* reread resources */
245                     if (persist_flag)
246                         del_p_state (info.userid);  /* remove persist file */
247                 }
248                 urp_end ();
249                 while (str_queue_deq (queue, 0, 0))
250                     ;
251             }
252             else if (!strcmp (command, "stop"))
253             {
254                 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
255                 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
256                     ;
257                 break;
258             }
259             else 
260                 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
261             continuation = 1;
262         }
263     }
264     close (extra_fd);
265     gips_close (gip);
266     gips_destroy (gip);
267 }
268
269 int main (int argc, char **argv)
270 {
271     struct str_queue *queue;
272
273     info.kernel_res = NULL;
274     info.default_res = "default.res";
275     info.override_res = NULL;
276     *info.target = 0;
277     *info.account = 0;
278     info.account_in_session = 1;
279     info.lang = NULL;
280     info.bibset = NULL;
281     info.zass = NULL;
282     info.override_portno = NULL;
283     info.override_hostname = NULL;
284     info.databases = NULL;
285     info.database = NULL;
286     info.setno = -1;
287     info.userid = -1;
288 #if USE_FML
289     info.fml = NULL;
290 #endif
291     info.sets = NULL;
292
293     gw_log_init (*argv);
294     info.kernel_res = gw_res_init ();
295     while (--argc > 0)
296     {
297         if (**++argv == '-')
298         {
299             switch (argv[0][1])
300             {
301             case 'H':
302                 fprintf (stderr, "kernel [options] [resourceFile]\n");
303                 fprintf (stderr, "If no resource file is specified");
304                 fprintf (stderr, " default.res is used\n");
305                 fprintf (stderr, "Options:\n");
306                 fprintf (stderr, " -d           Enable debugging log\n");
307                 fprintf (stderr, " -t target    Open target immediately\n");
308                 fprintf (stderr, " -g lang      Set language\n");
309                 fprintf (stderr, " -o resource  Override with resource\n");
310                 fprintf (stderr, " -h host      Override host\n");
311                 fprintf (stderr, " -p port      Override port\n");
312                 fprintf (stderr, " -l log       Set Log file\n");
313                 fprintf (stderr, " -i id        Set IPC userid\n");
314                 exit (1);
315             case 'd':
316                 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
317                 break;
318             case 'D':
319                 gw_log_level (GW_LOG_ALL);
320                 break;
321             case 't':
322                 if (argv[0][2])
323                     strcpy (info.target, argv[0]+2);
324                 else if (argc > 0)
325                 {
326                     --argc;
327                     strcpy (info.target, *++argv);
328                 }
329                 else
330                 {
331                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
332                     exit (1);
333                 }
334                 break;
335             case 'g':
336                 if (argv[0][2])
337                     info.lang = argv[0]+2;
338                 else if (argc > 0)
339                 {
340                     --argc;
341                     info.lang = *++argv;
342                 }
343                 else
344                 {
345                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
346                     exit (1);
347                 }
348                 break;
349             case 'o':
350                 if (argv[0][2])
351                     info.override_res = argv[0]+2;
352                 else if (argc > 0)
353                 {
354                     --argc;
355                     info.override_res = *++argv;
356                 }
357                 else
358                 {
359                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
360                     exit (1);
361                 }
362                 break;
363             case 'p':
364                 if (argv[0][2])
365                     info.override_portno = argv[0]+2;
366                 else if (argc > 0)
367                 {
368                     --argc;
369                     info.override_portno = *++argv;
370                 }
371                 else
372                 {
373                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
374                     exit (1);
375                 }
376                 break;
377             case 'h':
378                 if (argv[0][2])
379                     info.override_hostname = argv[0]+2;
380                 else if (argc > 0)
381                 {
382                     --argc;
383                     info.override_hostname = *++argv;
384                 }
385                 else
386                 {
387                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
388                     exit (1);
389                 }
390                 break;
391             case 'l':
392                 if (argv[0][2])
393                     gw_log_file (GW_LOG_ALL, argv[0]+2);
394                 else if (argc > 0)
395                 {
396                     --argc;
397                     gw_log_file (GW_LOG_ALL, *++argv);
398                 }
399                 else
400                 {
401                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
402                     exit (1);
403                 }
404                 break;
405             case 'i':
406                 if (argv[0][2])
407                     info.userid = atoi (argv[0]+2);
408                 else if (argc > 0)
409                 {
410                     --argc;
411                     info.userid = atoi (*++argv);
412                 }
413                 else
414                 {
415                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
416                     exit (1);
417                 }
418                 gw_log_session (info.userid);
419                 break;
420             default:
421                 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s;"
422                         " use -H for help", *argv);
423                 exit (1);
424             }
425         }
426         else
427             info.default_res = *argv;
428     }
429     if (!(queue = str_queue_mk ()))
430     {
431         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
432         exit (1);
433     }
434     if (info.userid != -1)
435     {
436         read_kernel_res ();
437         kernel_events (queue);
438     }
439     else
440     {
441         char line_buf[512];
442         read_kernel_res ();
443         while (lgets (line_buf, sizeof(line_buf)-1, 0))
444             str_queue_enq (queue, line_buf);
445         urp_start (0, queue);
446         urp_command (queue);
447         urp_end ();
448     }
449     return 0;
450 }
451
452 struct gw_user_set *user_set_add (const char *name, int hits, 
453                                   const char *database, 
454                                   struct ccl_rpn_node *rpn,
455                                   int present_flag,
456                                   const char *search_str)
457 {
458     struct gw_user_set *s;
459
460     s = malloc (sizeof (*s));
461     assert (s);
462
463     s->name = gw_strdup (name);
464     s->hits = hits;
465     s->database = gw_strdup (database);
466     s->rpn = rpn;
467     s->present_flag = present_flag;
468     s->search_str = gw_strdup (search_str);
469     s->prev = info.sets;
470     info.sets = s;
471     return s;
472 }
473
474 void user_set_init (void)
475 {
476     struct gw_user_set *s, *s1;
477
478     for (s = info.sets; s; s = s1)
479     {
480         free (s->name);
481         free (s->database);
482         ccl_rpn_delete (s->rpn);
483         s1 = s->prev;
484         free (s);
485     }
486     info.sets = NULL;
487 }
488
489 struct gw_user_set *user_set_search (const char *name)
490 {
491     struct gw_user_set *s;
492
493     if (!name)
494         return info.sets;
495     for (s = info.sets; s; s = s->prev)
496         if (!strcmp (s->name, name))
497             return s;
498     return NULL;
499 }
500
501 #if USE_FML
502 static void fml_inf_write (int ch)
503 {
504     putc (ch, reply_fd);
505 }
506 static FILE *fml_inf;
507
508 static int fml_inf_read (void)
509 {
510     return getc (fml_inf);
511 }
512 #endif
513
514 void read_kernel_res (void)
515 {
516     const char *v;
517     char *cp;
518     char resource_name[256];
519
520     user_set_init ();
521
522     if (info.bibset)
523         ccl_qual_rm (&info.bibset);
524     info.bibset = ccl_qual_mk ();
525
526     if (info.kernel_res)
527         gw_res_close (info.kernel_res);
528     info.kernel_res = gw_res_init ();
529
530     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
531             info.default_res);
532     if (*info.target)
533         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
534                 info.target);
535     if (info.lang)
536         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
537                 info.lang);
538
539     /* read default resources. These should exist */
540     if (gw_res_merge (info.kernel_res, info.default_res))
541     {
542         gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
543                 info.default_res);
544         return;
545     }
546     /* fetch target definitions (if defined at all) */
547     if (*info.target)
548     {
549         sprintf (resource_name, "gw.target.%s", info.target);
550         v = gw_res_get (info.kernel_res, resource_name, NULL);
551         if (v)
552             gw_res_merge (info.kernel_res, v);
553     }
554     /* fetch user definitions (if user-id is specified) */
555     if (info.userid >= 0)
556     {
557         char fname[250];
558         sprintf (fname, "user.%d.r", info.userid);
559         gw_res_merge (info.kernel_res, fname);
560     }
561     /* fetch language definitions (if specified at all) */
562     v = gw_res_get (info.kernel_res, "gw.language", info.lang);
563     if (v)
564     {
565         sprintf (resource_name, "gw.lang.%s", v);
566         v = gw_res_get (info.kernel_res, resource_name, NULL);
567         if (v)
568         {
569             gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Reading language resources %s",
570                     v);
571             gw_res_merge (info.kernel_res, v);
572         }
573     }
574     /* fetch overriding resources from file (if specified) */
575     if (info.override_res)
576         gw_res_merge (info.kernel_res, info.override_res);        
577
578     /* read bibset definition for ccl */
579     v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
580     if (v)
581     {
582         FILE *bib_inf;
583
584         bib_inf = fopen (v, "r");
585         if (!bib_inf)
586             gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", v);
587         else
588         {
589             gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", v);
590             ccl_qual_file (info.bibset, bib_inf);
591             fclose (bib_inf);
592         }
593     }
594
595     /* determine host name and port no */
596     sprintf (resource_name, "gw.target.%s", info.target);
597     if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
598     {
599         /* target is specified, and there is no sub-resource for it... */
600         const char *split;
601
602         if ((split = strchr (info.target, ':')))
603         {
604             memcpy (info.hostname, info.target, split-info.target);
605             info.hostname[split-info.target] = '\0';
606             info.port = atoi (split+1);
607         }
608         else
609         {
610             strcpy (info.hostname, info.target);
611             info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
612         }
613     }
614     else
615     {   /* resources gw.hostname and gw.portno will be used */
616         strncpy (info.hostname, gw_res_get (info.kernel_res,
617                                             "gw.hostname", "localhost"),
618                  sizeof(info.hostname)-1);
619         info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
620         strcpy (info.account, gw_res_get (info.kernel_res, "gw.account", ""));
621         info.account_in_session = 0;
622     }
623     /* set info.databases (all available databases for target) */
624     /* set info.database (first database for target) */
625     if (info.databases)
626         free (info.databases);
627     if (info.database)
628         free (info.database);
629     v = gw_res_get (info.kernel_res, "gw.databases", "");
630     info.databases = gw_strdup (v);
631     for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
632         *cp = ',';
633     v = gw_res_get (info.kernel_res, "gw.database", "");
634     if (*v == '\0' && *info.databases)
635     {
636         int len;
637         cp = strchr (info.databases, ',');
638         
639         len = cp ? (cp-info.databases) : strlen (info.databases);
640         info.database = malloc (len+1);
641         assert (info.database);
642         memcpy (info.database, info.databases, len);
643         info.database[len] = '\0';
644     }
645     else
646     {
647         info.database = gw_strdup (v);
648         for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
649             *cp = ',';
650     }
651
652     /* the port no can be explicitly overridden by a command line option */
653     if (info.override_portno)
654         info.port = atoi (info.override_portno);
655
656     /* the hostname can be explicitly overridden by a command line option */
657     if (info.override_hostname)
658         strncpy (info.hostname, info.override_hostname,
659                  sizeof(info.hostname)-1);
660
661     ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
662     ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
663     ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
664     ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
665
666     /* determine if more than one result-set names is supported */
667     if (gw_res_bool (info.kernel_res, "gw.result.set", 1))
668         info.setno = 0;
669     else
670         info.setno = -1;
671 #if USE_FML
672     if (!info.fml)
673     {
674         v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");    
675         fml_inf = fopen (v, "r");
676         if (!fml_inf)
677             gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open fml script %s", v);
678         else
679         {
680             info.fml = fml_open ();
681             info.fml->read_func = fml_inf_read;
682             info.fml->write_func = fml_inf_write;
683             fml_preprocess (info.fml);
684             fml_exec (info.fml);
685             fclose (fml_inf);
686         }
687     }
688 #endif
689 }