1 /* Gateway kernel - Main
5 * Revision 1.23 1995/05/03 16:34:18 adam
6 * CCL def command, i.e. user definitions - saved as resource files.
8 * Revision 1.22 1995/05/03 07:37:39 adam
9 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
10 * are used when possible.
12 * Revision 1.21 1995/05/01 16:26:56 adam
13 * More work on resource monitor.
15 * Revision 1.20 1995/05/01 12:43:32 adam
16 * First work on resource monitor program.
18 * Revision 1.19 1995/04/19 16:01:58 adam
19 * Some hacks to get the FIFO communication work!! Isn't reliable.
20 * Resource gw.account added - default account info.
22 * Revision 1.18 1995/04/19 13:19:09 adam
23 * New command: account - for authentication.
25 * Revision 1.17 1995/04/19 10:46:18 adam
26 * Persistency works much better now. New command: status - history-like
28 * Revision 1.16 1995/04/19 07:31:07 adam
29 * First work on Z39.50 persistence.
31 * Revision 1.15 1995/04/17 09:34:30 adam
32 * Timeout (idletime) adjustable. Minor changes in kernel.
34 * Revision 1.14 1995/03/28 11:42:34 adam
35 * First use of string-queue utility.
37 * Revision 1.13 1995/03/28 08:01:25 adam
38 * FIFO existence is used to test for a running kernel.
40 * Revision 1.12 1995/03/27 12:51:05 adam
41 * New log level in use: GW_LOG_ERRNO.
43 * Revision 1.11 1995/03/27 08:24:02 adam
44 * First use of gip interface and gw-db.
45 * First work on eti program.
47 * Revision 1.10 1995/03/01 14:32:25 adam
48 * Better diagnostics. Default is, that only one database selected when
51 * Revision 1.9 1995/02/23 08:32:17 adam
54 * Revision 1.7 1995/02/22 15:22:33 adam
55 * Much more checking of run-time state. Show command never retrieves
56 * more records than indicated by the previous search request. Help
57 * command available. The maximum number of records retrieved can be
60 * Revision 1.6 1995/02/22 08:51:34 adam
61 * Output function can be customized in fml, which is used to print
62 * the reply to reply_fd.
64 * Revision 1.5 1995/02/20 21:16:20 adam
65 * FML support. Bug fixes. Profile for drewdb.
67 * Revision 1.4 1995/02/17 17:06:16 adam
70 * Revision 1.3 1995/02/16 18:35:09 adam
71 * First use of Zdist library. Search requests are supported.
72 * Present requests are not supported yet.
74 * Revision 1.2 1995/02/16 13:21:00 adam
75 * Organization of resource files for targets and conversion
76 * language implemented.
78 * Revision 1.1 1995/02/15 17:45:29 adam
79 * First version of email gateway kernel. Email requests are read
80 * from stdin. The output is transferred to an MTA if 'From' is
81 * found in the header - or stdout if absent. No Z39.50 client is used.
90 #include <sys/types.h>
98 FILE *reply_fd = stdout;
100 struct gw_kernel_info info;
102 static void kernel_events (struct str_queue *queue)
104 char fifo_client_name[1024];
105 char fifo_server_name[1024];
112 int continuation = 0;
116 persist_flag = gw_res_bool (info.kernel_res, "gw.persist", 0);
117 timeout = gw_res_int (info.kernel_res, "gw.timeout", 600);
118 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "event loop");
120 sprintf (fifo_client_name, "fifo.c.%d", info.userid);
121 sprintf (fifo_server_name, "fifo.s.%d", info.userid);
123 gip = gips_initialize (fifo_server_name);
124 gips_open (gip, fifo_client_name);
125 gip_fd = gip_infileno (gip);
126 extra_fd = open (fifo_server_name, O_WRONLY);
131 FD_SET (gip_fd, &set_r);
135 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
136 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
139 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
144 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds",
146 if (info.zass && persist_flag)
147 save_p_state (info.userid);
150 if (FD_ISSET (gip_fd, &set_r))
152 char command[128], *cp;
154 if (!(lgets (command, 127, gip_fd)))
156 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
159 if ((cp = strchr (command, '\n')))
161 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
162 if (!strcmp (command, "mail"))
164 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail");
165 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
166 str_queue_enq (queue, line_buf);
167 urp_start (continuation, queue);
168 if (persist_flag && !continuation)
169 load_p_state (info.userid);
170 r = urp_command (queue);
171 if (persist_flag && r == 1)
172 del_p_state (info.userid);
174 while (str_queue_deq (queue, 0, 0))
177 else if (!strcmp (command, "stop"))
179 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
184 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
193 unlink (fifo_client_name);
194 unlink (fifo_server_name);
197 int main (int argc, char **argv)
199 struct str_queue *queue;
201 info.kernel_res = NULL;
202 info.default_res = "default.res";
203 info.override_res = NULL;
209 info.override_portno = NULL;
210 info.override_hostname = NULL;
211 info.databases = NULL;
212 info.database = NULL;
221 info.kernel_res = gw_res_init ();
229 fprintf (stderr, "kernel [option..] [resource]\n");
230 fprintf (stderr, "If no resource file is given");
231 fprintf (stderr, " default.res is used\n");
232 fprintf (stderr, "Options:\n");
233 fprintf (stderr, " -d Enable debugging log\n");
234 fprintf (stderr, " -t target Open target immediately\n");
235 fprintf (stderr, " -g lang Set language\n");
236 fprintf (stderr, " -o resource Override with resource\n");
237 fprintf (stderr, " -h host Override host\n");
238 fprintf (stderr, " -p port Override port\n");
239 fprintf (stderr, " -l log Set Log file\n");
240 fprintf (stderr, " -i id Set IPC userid\n");
243 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
246 gw_log_level (GW_LOG_ALL);
250 strcpy (info.target, argv[0]+2);
254 strcpy (info.target, *++argv);
258 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
264 info.lang = argv[0]+2;
272 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
278 info.override_res = argv[0]+2;
282 info.override_res = *++argv;
286 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
292 info.override_portno = argv[0]+2;
296 info.override_portno = *++argv;
300 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
306 info.override_hostname = argv[0]+2;
310 info.override_hostname = *++argv;
314 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
320 gw_log_file (GW_LOG_ALL, argv[0]+2);
324 gw_log_file (GW_LOG_ALL, *++argv);
328 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
334 info.userid = atoi (argv[0]+2);
338 info.userid = atoi (*++argv);
342 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
347 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
352 info.default_res = *argv;
354 if (!(queue = str_queue_mk ()))
356 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
359 if (info.userid != -1)
362 kernel_events (queue);
368 while (lgets (line_buf, sizeof(line_buf)-1, 0))
369 str_queue_enq (queue, line_buf);
370 urp_start (0, queue);
377 struct gw_user_set *user_set_add (const char *name, int hits,
378 const char *database,
379 struct ccl_rpn_node *rpn,
381 const char *search_str)
383 struct gw_user_set *s;
385 s = malloc (sizeof (*s));
388 s->name = gw_strdup (name);
390 s->database = gw_strdup (database);
392 s->present_flag = present_flag;
393 s->search_str = gw_strdup (search_str);
399 void user_set_init (void)
401 struct gw_user_set *s, *s1;
403 for (s = info.sets; s; s = s1)
407 ccl_rpn_delete (s->rpn);
414 struct gw_user_set *user_set_search (const char *name)
416 struct gw_user_set *s;
420 for (s = info.sets; s; s = s->prev)
421 if (!strcmp (s->name, name))
427 static void fml_inf_write (int ch)
431 static FILE *fml_inf;
433 static int fml_inf_read (void)
435 return getc (fml_inf);
439 void read_kernel_res (void)
441 char path_prefix[128];
445 char resource_name[256];
450 ccl_qual_rm (&info.bibset);
451 info.bibset = ccl_qual_mk ();
454 gw_res_close (info.kernel_res);
455 info.kernel_res = gw_res_init ();
457 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
460 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
463 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
466 /* read default resources. These should exist */
467 if (gw_res_merge (info.kernel_res, info.default_res))
469 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
473 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
475 /* fetch target definitions (if defined at all) */
478 sprintf (resource_name, "gw.target.%s", info.target);
479 v = gw_res_get (info.kernel_res, resource_name, NULL);
482 sprintf (fname, "%s/%s", path_prefix, v);
483 gw_res_merge (info.kernel_res, fname);
486 /* fetch user definitions (if user-id is specified) */
487 if (info.userid >= 0)
489 sprintf (fname, "%s/user.%d.r", path_prefix, info.userid);
490 gw_res_merge (info.kernel_res, fname);
492 /* fetch language definitions (if specified at all) */
493 v = gw_res_get (info.kernel_res, "gw.language", info.lang);
496 sprintf (resource_name, "gw.lang.%s", v);
497 v = gw_res_get (info.kernel_res, resource_name, NULL);
500 sprintf (fname, "%s/%s", path_prefix, v);
501 gw_res_merge (info.kernel_res, fname);
504 /* fetch overriding resources from file (if specified) */
505 if (info.override_res)
507 sprintf (fname, "%s/%s", path_prefix, info.override_res);
508 gw_res_merge (info.kernel_res, fname);
511 /* read bibset definition for ccl */
512 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
517 sprintf (fname, "%s/%s", path_prefix, v);
518 bib_inf = fopen (fname, "r");
520 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
523 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
524 ccl_qual_file (info.bibset, bib_inf);
529 /* determine host name and port no */
530 sprintf (resource_name, "gw.target.%s", info.target);
531 if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
533 /* target is specified, and there is no sub-resource for it... */
536 if ((split = strchr (info.target, ':')))
538 memcpy (info.hostname, info.target, split-info.target);
539 info.hostname[split-info.target] = '\0';
540 info.port = atoi (split+1);
544 strcpy (info.hostname, info.target);
545 info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
549 { /* resources gw.hostname and gw.portno will be used */
550 strncpy (info.hostname, gw_res_get (info.kernel_res,
551 "gw.hostname", "localhost"),
552 sizeof(info.hostname)-1);
553 info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
554 strcpy (info.account, gw_res_get (info.kernel_res, "gw.account", ""));
556 /* set info.databases (all available databases for target) */
557 /* set info.database (first database for target) */
559 free (info.databases);
561 free (info.database);
562 v = gw_res_get (info.kernel_res, "gw.databases", "");
563 info.databases = gw_strdup (v);
564 for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
566 v = gw_res_get (info.kernel_res, "gw.database", "");
567 if (*v == '\0' && *info.databases)
570 cp = strchr (info.databases, ',');
572 len = cp ? (cp-info.databases) : strlen (info.databases);
573 info.database = malloc (len+1);
574 assert (info.database);
575 memcpy (info.database, info.databases, len);
576 info.database[len] = '\0';
580 info.database = gw_strdup (v);
581 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
585 /* the port no can be explicitly overridden by a command line option */
586 if (info.override_portno)
587 info.port = atoi (info.override_portno);
589 /* the hostname can be explicitly overridden by a command line option */
590 if (info.override_hostname)
591 strncpy (info.hostname, info.override_hostname,
592 sizeof(info.hostname)-1);
594 /* determine if more than one result-set names is supported */
595 if (gw_res_bool (info.kernel_res, "gw.result.set", 1))
602 v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
603 sprintf (fname, "%s/%s", path_prefix, v);
604 fml_inf = fopen (fname, "r");
606 gw_log (GW_LOG_WARN, KERNEL_LOG,
607 "cannot open fml script %s", fname);
610 info.fml = fml_open ();
611 info.fml->read_func = fml_inf_read;
612 info.fml->write_func = fml_inf_write;
613 fml_preprocess (info.fml);