1 /* Gateway kernel - Main
5 * Revision 1.24 1995/05/04 10:40:07 adam
6 * More work on Def-settings.
8 * Revision 1.23 1995/05/03 16:34:18 adam
9 * CCL def command, i.e. user definitions - saved as resource files.
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.
15 * Revision 1.21 1995/05/01 16:26:56 adam
16 * More work on resource monitor.
18 * Revision 1.20 1995/05/01 12:43:32 adam
19 * First work on resource monitor program.
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.
25 * Revision 1.18 1995/04/19 13:19:09 adam
26 * New command: account - for authentication.
28 * Revision 1.17 1995/04/19 10:46:18 adam
29 * Persistency works much better now. New command: status - history-like
31 * Revision 1.16 1995/04/19 07:31:07 adam
32 * First work on Z39.50 persistence.
34 * Revision 1.15 1995/04/17 09:34:30 adam
35 * Timeout (idletime) adjustable. Minor changes in kernel.
37 * Revision 1.14 1995/03/28 11:42:34 adam
38 * First use of string-queue utility.
40 * Revision 1.13 1995/03/28 08:01:25 adam
41 * FIFO existence is used to test for a running kernel.
43 * Revision 1.12 1995/03/27 12:51:05 adam
44 * New log level in use: GW_LOG_ERRNO.
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.
50 * Revision 1.10 1995/03/01 14:32:25 adam
51 * Better diagnostics. Default is, that only one database selected when
54 * Revision 1.9 1995/02/23 08:32:17 adam
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
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.
67 * Revision 1.5 1995/02/20 21:16:20 adam
68 * FML support. Bug fixes. Profile for drewdb.
70 * Revision 1.4 1995/02/17 17:06:16 adam
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.
77 * Revision 1.2 1995/02/16 13:21:00 adam
78 * Organization of resource files for targets and conversion
79 * language implemented.
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.
93 #include <sys/types.h>
101 FILE *reply_fd = stdout;
103 struct gw_kernel_info info;
105 static void kernel_events (struct str_queue *queue)
107 char fifo_client_name[1024];
108 char fifo_server_name[1024];
115 int continuation = 0;
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");
123 sprintf (fifo_client_name, "fifo.c.%d", info.userid);
124 sprintf (fifo_server_name, "fifo.s.%d", info.userid);
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);
134 FD_SET (gip_fd, &set_r);
138 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
139 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
142 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
147 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds",
149 if (info.zass && persist_flag)
150 save_p_state (info.userid);
153 if (FD_ISSET (gip_fd, &set_r))
155 char command[128], *cp;
157 if (!(lgets (command, 127, gip_fd)))
159 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
162 if ((cp = strchr (command, '\n')))
164 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
165 if (!strcmp (command, "mail"))
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? */
176 info.zass = NULL; /* delete association */
177 *info.target = 0; /* indicate no target */
178 read_kernel_res(); /* reread resources */
180 del_p_state (info.userid); /* remove persist file */
183 while (str_queue_deq (queue, 0, 0))
186 else if (!strcmp (command, "stop"))
188 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
193 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
202 unlink (fifo_client_name);
203 unlink (fifo_server_name);
206 int main (int argc, char **argv)
208 struct str_queue *queue;
210 info.kernel_res = NULL;
211 info.default_res = "default.res";
212 info.override_res = NULL;
218 info.override_portno = NULL;
219 info.override_hostname = NULL;
220 info.databases = NULL;
221 info.database = NULL;
230 info.kernel_res = gw_res_init ();
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");
252 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
255 gw_log_level (GW_LOG_ALL);
259 strcpy (info.target, argv[0]+2);
263 strcpy (info.target, *++argv);
267 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
273 info.lang = argv[0]+2;
281 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
287 info.override_res = argv[0]+2;
291 info.override_res = *++argv;
295 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
301 info.override_portno = argv[0]+2;
305 info.override_portno = *++argv;
309 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
315 info.override_hostname = argv[0]+2;
319 info.override_hostname = *++argv;
323 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
329 gw_log_file (GW_LOG_ALL, argv[0]+2);
333 gw_log_file (GW_LOG_ALL, *++argv);
337 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
343 info.userid = atoi (argv[0]+2);
347 info.userid = atoi (*++argv);
351 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
356 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
361 info.default_res = *argv;
363 if (!(queue = str_queue_mk ()))
365 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
368 if (info.userid != -1)
371 kernel_events (queue);
377 while (lgets (line_buf, sizeof(line_buf)-1, 0))
378 str_queue_enq (queue, line_buf);
379 urp_start (0, queue);
386 struct gw_user_set *user_set_add (const char *name, int hits,
387 const char *database,
388 struct ccl_rpn_node *rpn,
390 const char *search_str)
392 struct gw_user_set *s;
394 s = malloc (sizeof (*s));
397 s->name = gw_strdup (name);
399 s->database = gw_strdup (database);
401 s->present_flag = present_flag;
402 s->search_str = gw_strdup (search_str);
408 void user_set_init (void)
410 struct gw_user_set *s, *s1;
412 for (s = info.sets; s; s = s1)
416 ccl_rpn_delete (s->rpn);
423 struct gw_user_set *user_set_search (const char *name)
425 struct gw_user_set *s;
429 for (s = info.sets; s; s = s->prev)
430 if (!strcmp (s->name, name))
436 static void fml_inf_write (int ch)
440 static FILE *fml_inf;
442 static int fml_inf_read (void)
444 return getc (fml_inf);
448 void read_kernel_res (void)
450 char path_prefix[128];
454 char resource_name[256];
459 ccl_qual_rm (&info.bibset);
460 info.bibset = ccl_qual_mk ();
463 gw_res_close (info.kernel_res);
464 info.kernel_res = gw_res_init ();
466 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
469 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
472 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
475 /* read default resources. These should exist */
476 if (gw_res_merge (info.kernel_res, info.default_res))
478 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
482 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
484 /* fetch target definitions (if defined at all) */
487 sprintf (resource_name, "gw.target.%s", info.target);
488 v = gw_res_get (info.kernel_res, resource_name, NULL);
491 sprintf (fname, "%s/%s", path_prefix, v);
492 gw_res_merge (info.kernel_res, fname);
495 /* fetch user definitions (if user-id is specified) */
496 if (info.userid >= 0)
498 sprintf (fname, "%s/user.%d.r", path_prefix, info.userid);
499 gw_res_merge (info.kernel_res, fname);
501 /* fetch language definitions (if specified at all) */
502 v = gw_res_get (info.kernel_res, "gw.language", info.lang);
505 sprintf (resource_name, "gw.lang.%s", v);
506 v = gw_res_get (info.kernel_res, resource_name, NULL);
509 sprintf (fname, "%s/%s", path_prefix, v);
510 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Reading language resources %s",
512 gw_res_merge (info.kernel_res, fname);
515 /* fetch overriding resources from file (if specified) */
516 if (info.override_res)
518 sprintf (fname, "%s/%s", path_prefix, info.override_res);
519 gw_res_merge (info.kernel_res, fname);
522 /* read bibset definition for ccl */
523 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
528 sprintf (fname, "%s/%s", path_prefix, v);
529 bib_inf = fopen (fname, "r");
531 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
534 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
535 ccl_qual_file (info.bibset, bib_inf);
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))
544 /* target is specified, and there is no sub-resource for it... */
547 if ((split = strchr (info.target, ':')))
549 memcpy (info.hostname, info.target, split-info.target);
550 info.hostname[split-info.target] = '\0';
551 info.port = atoi (split+1);
555 strcpy (info.hostname, info.target);
556 info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
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", ""));
567 /* set info.databases (all available databases for target) */
568 /* set info.database (first database for target) */
570 free (info.databases);
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++)
577 v = gw_res_get (info.kernel_res, "gw.database", "");
578 if (*v == '\0' && *info.databases)
581 cp = strchr (info.databases, ',');
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';
591 info.database = gw_strdup (v);
592 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
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);
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);
605 /* determine if more than one result-set names is supported */
606 if (gw_res_bool (info.kernel_res, "gw.result.set", 1))
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");
617 gw_log (GW_LOG_WARN, KERNEL_LOG,
618 "cannot open fml script %s", fname);
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);