1 /* Gateway kernel - Main
5 * Revision 1.21 1995/05/01 16:26:56 adam
6 * More work on resource monitor.
8 * Revision 1.20 1995/05/01 12:43:32 adam
9 * First work on resource monitor program.
11 * Revision 1.19 1995/04/19 16:01:58 adam
12 * Some hacks to get the FIFO communication work!! Isn't reliable.
13 * Resource gw.account added - default account info.
15 * Revision 1.18 1995/04/19 13:19:09 adam
16 * New command: account - for authentication.
18 * Revision 1.17 1995/04/19 10:46:18 adam
19 * Persistency works much better now. New command: status - history-like
21 * Revision 1.16 1995/04/19 07:31:07 adam
22 * First work on Z39.50 persistence.
24 * Revision 1.15 1995/04/17 09:34:30 adam
25 * Timeout (idletime) adjustable. Minor changes in kernel.
27 * Revision 1.14 1995/03/28 11:42:34 adam
28 * First use of string-queue utility.
30 * Revision 1.13 1995/03/28 08:01:25 adam
31 * FIFO existence is used to test for a running kernel.
33 * Revision 1.12 1995/03/27 12:51:05 adam
34 * New log level in use: GW_LOG_ERRNO.
36 * Revision 1.11 1995/03/27 08:24:02 adam
37 * First use of gip interface and gw-db.
38 * First work on eti program.
40 * Revision 1.10 1995/03/01 14:32:25 adam
41 * Better diagnostics. Default is, that only one database selected when
44 * Revision 1.9 1995/02/23 08:32:17 adam
47 * Revision 1.7 1995/02/22 15:22:33 adam
48 * Much more checking of run-time state. Show command never retrieves
49 * more records than indicated by the previous search request. Help
50 * command available. The maximum number of records retrieved can be
53 * Revision 1.6 1995/02/22 08:51:34 adam
54 * Output function can be customized in fml, which is used to print
55 * the reply to reply_fd.
57 * Revision 1.5 1995/02/20 21:16:20 adam
58 * FML support. Bug fixes. Profile for drewdb.
60 * Revision 1.4 1995/02/17 17:06:16 adam
63 * Revision 1.3 1995/02/16 18:35:09 adam
64 * First use of Zdist library. Search requests are supported.
65 * Present requests are not supported yet.
67 * Revision 1.2 1995/02/16 13:21:00 adam
68 * Organization of resource files for targets and conversion
69 * language implemented.
71 * Revision 1.1 1995/02/15 17:45:29 adam
72 * First version of email gateway kernel. Email requests are read
73 * from stdin. The output is transferred to an MTA if 'From' is
74 * found in the header - or stdout if absent. No Z39.50 client is used.
83 #include <sys/types.h>
91 FILE *reply_fd = stdout;
93 struct gw_kernel_info info;
95 static void kernel_events (struct str_queue *queue, int userid)
97 char fifo_client_name[1024];
98 char fifo_server_name[1024];
105 int continuation = 0;
108 timeout = atoi(gw_res_get (info.kernel_res, "gw.timeout", "600"));
109 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "event loop");
111 sprintf (fifo_client_name, "fifo.c.%d", userid);
112 sprintf (fifo_server_name, "fifo.s.%d", userid);
114 gip = gips_initialize (fifo_server_name);
115 gips_open (gip, fifo_client_name);
116 gip_fd = gip_infileno (gip);
117 extra_fd = open (fifo_server_name, O_WRONLY);
122 FD_SET (gip_fd, &set_r);
126 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
127 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
130 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
135 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds",
138 save_p_state (userid);
141 if (FD_ISSET (gip_fd, &set_r))
143 char command[128], *cp;
145 if (!(lgets (command, 127, gip_fd)))
147 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
150 if ((cp = strchr (command, '\n')))
152 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
153 if (!strcmp (command, "mail"))
155 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail");
156 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
157 str_queue_enq (queue, line_buf);
158 urp_start (continuation, queue);
160 load_p_state (userid);
163 while (str_queue_deq (queue, 0, 0))
166 else if (!strcmp (command, "stop"))
168 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
173 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
181 unlink (fifo_client_name);
182 unlink (fifo_server_name);
185 int main (int argc, char **argv)
188 struct str_queue *queue;
190 info.kernel_res = NULL;
191 info.default_res = "default.res";
192 info.override_res = NULL;
198 info.override_portno = NULL;
199 info.override_hostname = NULL;
200 info.databases = NULL;
201 info.database = NULL;
209 info.kernel_res = gw_res_init ();
217 fprintf (stderr, "kernel [option..] [resource]\n");
218 fprintf (stderr, "If no resource file is given");
219 fprintf (stderr, " default.res is used\n");
220 fprintf (stderr, "Options:\n");
221 fprintf (stderr, " -d Enable debugging log\n");
222 fprintf (stderr, " -t target Open target immediately\n");
223 fprintf (stderr, " -g lang Set language\n");
224 fprintf (stderr, " -o resource Override with resource\n");
225 fprintf (stderr, " -h host Override host\n");
226 fprintf (stderr, " -p port Override port\n");
227 fprintf (stderr, " -l log Set Log file\n");
228 fprintf (stderr, " -i id Set IPC userid\n");
231 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
234 gw_log_level (GW_LOG_ALL);
238 strcpy (info.target, argv[0]+2);
242 strcpy (info.target, *++argv);
246 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
252 info.lang = argv[0]+2;
260 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
266 info.override_res = argv[0]+2;
270 info.override_res = *++argv;
274 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
280 info.override_portno = argv[0]+2;
284 info.override_portno = *++argv;
288 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
294 info.override_hostname = argv[0]+2;
298 info.override_hostname = *++argv;
302 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
308 gw_log_file (GW_LOG_ALL, argv[0]+2);
312 gw_log_file (GW_LOG_ALL, *++argv);
316 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
322 userid = atoi (argv[0]+2);
326 userid = atoi (*++argv);
330 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
335 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
340 info.default_res = *argv;
342 if (!(queue = str_queue_mk ()))
344 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
350 kernel_events (queue, userid);
356 while (lgets (line_buf, sizeof(line_buf)-1, 0))
357 str_queue_enq (queue, line_buf);
358 urp_start (0, queue);
365 struct gw_user_set *user_set_add (const char *name, int hits,
366 const char *database,
367 struct ccl_rpn_node *rpn,
369 const char *search_str)
371 struct gw_user_set *s;
373 s = malloc (sizeof (*s));
376 s->name = gw_strdup (name);
378 s->database = gw_strdup (database);
380 s->present_flag = present_flag;
381 s->search_str = gw_strdup (search_str);
387 void user_set_init (void)
389 struct gw_user_set *s, *s1;
391 for (s = info.sets; s; s = s1)
395 ccl_rpn_delete (s->rpn);
402 struct gw_user_set *user_set_search (const char *name)
404 struct gw_user_set *s;
408 for (s = info.sets; s; s = s->prev)
409 if (!strcmp (s->name, name))
415 static void fml_inf_write (int ch)
419 static FILE *fml_inf;
421 static int fml_inf_read (void)
423 return getc (fml_inf);
427 void read_kernel_res (void)
429 char path_prefix[128];
433 char resource_name[256];
438 ccl_qual_rm (&info.bibset);
439 info.bibset = ccl_qual_mk ();
442 gw_res_close (info.kernel_res);
443 info.kernel_res = gw_res_init ();
445 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
448 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
451 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
454 if (gw_res_merge (info.kernel_res, info.default_res))
456 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
460 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
464 sprintf (resource_name, "gw.target.%s", info.target);
465 v = gw_res_get (info.kernel_res, resource_name, NULL);
468 sprintf (fname, "%s/%s", path_prefix, v);
469 gw_res_merge (info.kernel_res, fname);
474 sprintf (resource_name, "gw.lang.%s", info.lang);
475 v = gw_res_get (info.kernel_res, resource_name, NULL);
478 sprintf (fname, "%s/%s", path_prefix, v);
479 gw_res_merge (info.kernel_res, fname);
482 if (info.override_res)
484 sprintf (fname, "%s/%s", path_prefix, info.override_res);
485 gw_res_merge (info.kernel_res, fname);
487 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
492 sprintf (fname, "%s/%s", path_prefix, v);
493 bib_inf = fopen (fname, "r");
495 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
498 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
499 ccl_qual_file (info.bibset, bib_inf);
503 sprintf (resource_name, "gw.target.%s", info.target);
504 if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
506 /* target is there, and there is no sub-resource for it... */
509 if ((split = strchr (info.target, ':')))
511 memcpy (info.hostname, info.target, split-info.target);
512 info.hostname[split-info.target] = '\0';
513 info.port = atoi (split+1);
517 strcpy (info.hostname, info.target);
518 info.port = atoi (gw_res_get
519 (info.kernel_res, "gw.portno", "210"));
524 strncpy (info.hostname, gw_res_get (info.kernel_res,
525 "gw.hostname", "localhost"),
526 sizeof(info.hostname)-1);
527 info.port = atoi (gw_res_get (info.kernel_res,
528 "gw.portno", "210"));
529 strcpy (info.account, gw_res_get (info.kernel_res, "gw.account", ""));
532 free (info.databases);
534 free (info.database);
535 v = gw_res_get (info.kernel_res, "gw.databases", "");
536 info.databases = gw_strdup (v);
537 for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
539 v = gw_res_get (info.kernel_res, "gw.database", "");
540 if (*v == '\0' && *info.databases)
543 cp = strchr (info.databases, ',');
545 len = cp ? (cp-info.databases) : strlen (info.databases);
546 info.database = malloc (len+1);
547 assert (info.database);
548 memcpy (info.database, info.databases, len);
549 info.database[len] = '\0';
553 info.database = gw_strdup (v);
554 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
557 if (info.override_portno)
558 info.port = atoi (info.override_portno);
559 if (info.override_hostname)
560 strncpy (info.hostname, info.override_hostname,
561 sizeof(info.hostname)-1);
562 v = gw_res_get (info.kernel_res, "gw.result.set", NULL);
563 info.setno = v ? -1 : 0;
567 v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
568 sprintf (fname, "%s/%s", path_prefix, v);
569 fml_inf = fopen (fname, "r");
571 gw_log (GW_LOG_WARN, KERNEL_LOG,
572 "cannot open fml script %s", fname);
575 info.fml = fml_open ();
576 info.fml->read_func = fml_inf_read;
577 info.fml->write_func = fml_inf_write;
578 fml_preprocess (info.fml);