1 /* Gateway kernel - Main
5 * Revision 1.16 1995/04/19 07:31:07 adam
6 * First work on Z39.50 persistence.
8 * Revision 1.15 1995/04/17 09:34:30 adam
9 * Timeout (idletime) adjustable. Minor changes in kernel.
11 * Revision 1.14 1995/03/28 11:42:34 adam
12 * First use of string-queue utility.
14 * Revision 1.13 1995/03/28 08:01:25 adam
15 * FIFO existence is used to test for a running kernel.
17 * Revision 1.12 1995/03/27 12:51:05 adam
18 * New log level in use: GW_LOG_ERRNO.
20 * Revision 1.11 1995/03/27 08:24:02 adam
21 * First use of gip interface and gw-db.
22 * First work on eti program.
24 * Revision 1.10 1995/03/01 14:32:25 adam
25 * Better diagnostics. Default is, that only one database selected when
28 * Revision 1.9 1995/02/23 08:32:17 adam
31 * Revision 1.7 1995/02/22 15:22:33 adam
32 * Much more checking of run-time state. Show command never retrieves
33 * more records than indicated by the previous search request. Help
34 * command available. The maximum number of records retrieved can be
37 * Revision 1.6 1995/02/22 08:51:34 adam
38 * Output function can be customized in fml, which is used to print
39 * the reply to reply_fd.
41 * Revision 1.5 1995/02/20 21:16:20 adam
42 * FML support. Bug fixes. Profile for drewdb.
44 * Revision 1.4 1995/02/17 17:06:16 adam
47 * Revision 1.3 1995/02/16 18:35:09 adam
48 * First use of Zdist library. Search requests are supported.
49 * Present requests are not supported yet.
51 * Revision 1.2 1995/02/16 13:21:00 adam
52 * Organization of resource files for targets and conversion
53 * language implemented.
55 * Revision 1.1 1995/02/15 17:45:29 adam
56 * First version of email gateway kernel. Email requests are read
57 * from stdin. The output is transferred to an MTA if 'From' is
58 * found in the header - or stdout if absent. No Z39.50 client is used.
67 #include <sys/types.h>
75 FILE *reply_fd = stdout;
77 struct gw_kernel_info info;
79 static void kernel_events (struct str_queue *queue, int userid)
81 char fifo_client_name[1024];
82 char fifo_server_name[1024];
91 timeout = atoi(gw_res_get (info.kernel_res, "gw.timeout", "600"));
92 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "kernel event loop");
94 sprintf (fifo_client_name, "fifo.c.%d", userid);
95 sprintf (fifo_server_name, "fifo.s.%d", userid);
97 gip = gips_initialize (fifo_server_name);
98 gips_open (gip, fifo_client_name);
99 gip_fd = gip_infileno (gip);
104 FD_SET (gip_fd, &set_r);
108 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
109 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
112 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
117 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds",
120 save_p_state (userid);
123 if (FD_ISSET (gip_fd, &set_r))
125 char command[128], *cp;
127 if (!(lgets (command, 127, gip_fd)))
129 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
132 if ((cp = strchr (command, '\n')))
134 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
135 if (!strcmp (command, "mail"))
137 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail...\n");
138 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
139 str_queue_enq (queue, line_buf);
140 urp_start (continuation, queue);
142 load_p_state (userid);
145 while (str_queue_deq (queue, 0, 0))
148 else if (!strcmp (command, "stop"))
150 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
155 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
162 unlink (fifo_client_name);
163 unlink (fifo_server_name);
166 int main (int argc, char **argv)
169 struct str_queue *queue;
171 info.kernel_res = NULL;
172 info.default_res = "default.res";
173 info.override_res = NULL;
178 info.override_portno = NULL;
179 info.override_hostname = NULL;
180 info.databases = NULL;
181 info.database = NULL;
189 info.kernel_res = gw_res_init ();
197 fprintf (stderr, "kernel [option..] [resource]\n");
198 fprintf (stderr, "If no resource file is given");
199 fprintf (stderr, " default.res is used\n");
200 fprintf (stderr, "Options:\n");
201 fprintf (stderr, " -d Enable debugging log\n");
202 fprintf (stderr, " -t target Open target immediately\n");
203 fprintf (stderr, " -g lang Set language\n");
204 fprintf (stderr, " -o resource Override with resource\n");
205 fprintf (stderr, " -h host Override host\n");
206 fprintf (stderr, " -p port Override port\n");
207 fprintf (stderr, " -l log Set Log file\n");
208 fprintf (stderr, " -i id Set IPC userid\n");
211 gw_log_level (GW_LOG_ALL);
215 strcpy (info.target, argv[0]+2);
219 strcpy (info.target, *++argv);
223 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
229 info.lang = argv[0]+2;
237 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
243 info.override_res = argv[0]+2;
247 info.override_res = *++argv;
251 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
257 info.override_portno = argv[0]+2;
261 info.override_portno = *++argv;
265 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
271 info.override_hostname = argv[0]+2;
275 info.override_hostname = *++argv;
279 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
285 gw_log_file (GW_LOG_ALL, argv[0]+2);
289 gw_log_file (GW_LOG_ALL, *++argv);
293 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
299 userid = atoi (argv[0]+2);
303 userid = atoi (*++argv);
307 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
312 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
317 info.default_res = *argv;
319 if (!(queue = str_queue_mk ()))
321 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
327 kernel_events (queue, userid);
333 while (lgets (line_buf, sizeof(line_buf)-1, 0))
334 str_queue_enq (queue, line_buf);
335 urp_start (0, queue);
342 struct gw_user_set *user_set_add (const char *name, int hits,
343 const char *database,
344 struct ccl_rpn_node *rpn,
347 struct gw_user_set *s;
349 s = malloc (sizeof (*s));
352 s->name = gw_strdup (name);
354 s->database = gw_strdup (database);
356 s->present_flag = present_flag;
362 void user_set_init (void)
364 struct gw_user_set *s, *s1;
366 for (s = info.sets; s; s = s1)
370 ccl_rpn_delete (s->rpn);
377 struct gw_user_set *user_set_search (const char *name)
379 struct gw_user_set *s;
383 for (s = info.sets; s; s = s->prev)
384 if (!strcmp (s->name, name))
390 static void fml_inf_write (int ch)
394 static FILE *fml_inf;
396 static int fml_inf_read (void)
398 return getc (fml_inf);
402 void read_kernel_res (void)
404 char path_prefix[128];
408 char resource_name[256];
413 ccl_qual_rm (&info.bibset);
414 info.bibset = ccl_qual_mk ();
417 gw_res_close (info.kernel_res);
418 info.kernel_res = gw_res_init ();
420 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
423 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
426 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
429 if (gw_res_merge (info.kernel_res, info.default_res))
431 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
435 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
439 sprintf (resource_name, "gw.target.%s", info.target);
440 v = gw_res_get (info.kernel_res, resource_name, NULL);
443 sprintf (fname, "%s/%s", path_prefix, v);
444 gw_res_merge (info.kernel_res, fname);
449 sprintf (resource_name, "gw.lang.%s", info.lang);
450 v = gw_res_get (info.kernel_res, resource_name, NULL);
453 sprintf (fname, "%s/%s", path_prefix, v);
454 gw_res_merge (info.kernel_res, fname);
457 if (info.override_res)
459 sprintf (fname, "%s/%s", path_prefix, info.override_res);
460 gw_res_merge (info.kernel_res, fname);
462 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
467 sprintf (fname, "%s/%s", path_prefix, v);
468 bib_inf = fopen (fname, "r");
470 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
473 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
474 ccl_qual_file (info.bibset, bib_inf);
478 sprintf (resource_name, "gw.target.%s", info.target);
479 if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
481 /* target is there, and there is no sub-resource for it... */
484 if ((split = strchr (info.target, ':')))
486 strncpy (info.hostname, info.target, sizeof(info.hostname)-1);
488 info.port = atoi (split);
490 info.port = atoi (gw_res_get
491 (info.kernel_res, "gw.portno", "210"));
495 strncpy (info.hostname, gw_res_get (info.kernel_res,
496 "gw.hostname", "localhost"),
497 sizeof(info.hostname)-1);
498 info.port = atoi (gw_res_get (info.kernel_res,
499 "gw.portno", "210"));
502 free (info.databases);
504 free (info.database);
505 v = gw_res_get (info.kernel_res, "gw.databases", "");
506 info.databases = gw_strdup (v);
507 for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
509 v = gw_res_get (info.kernel_res, "gw.database", "");
510 if (*v == '\0' && *info.databases)
513 cp = strchr (info.databases, ',');
515 len = cp ? (cp-info.databases) : strlen (info.databases);
516 info.database = malloc (len+1);
517 assert (info.database);
518 memcpy (info.database, info.databases, len);
519 info.database[len] = '\0';
523 info.database = gw_strdup (v);
524 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
527 if (info.override_portno)
528 info.port = atoi (info.override_portno);
529 if (info.override_hostname)
530 strncpy (info.hostname, info.override_hostname,
531 sizeof(info.hostname)-1);
532 v = gw_res_get (info.kernel_res, "gw.result.set", NULL);
533 info.setno = v ? -1 : 0;
537 v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
538 sprintf (fname, "%s/%s", path_prefix, v);
539 fml_inf = fopen (fname, "r");
541 gw_log (GW_LOG_WARN, KERNEL_LOG,
542 "cannot open fml script %s", fname);
545 info.fml = fml_open ();
546 info.fml->read_func = fml_inf_read;
547 info.fml->write_func = fml_inf_write;
548 fml_preprocess (info.fml);