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