1 /* Gateway kernel - Main
5 * Revision 1.19 1995/04/19 16:01:58 adam
6 * Some hacks to get the FIFO communication work!! Isn't reliable.
7 * Resource gw.account added - default account info.
9 * Revision 1.18 1995/04/19 13:19:09 adam
10 * New command: account - for authentication.
12 * Revision 1.17 1995/04/19 10:46:18 adam
13 * Persistency works much better now. New command: status - history-like
15 * Revision 1.16 1995/04/19 07:31:07 adam
16 * First work on Z39.50 persistence.
18 * Revision 1.15 1995/04/17 09:34:30 adam
19 * Timeout (idletime) adjustable. Minor changes in kernel.
21 * Revision 1.14 1995/03/28 11:42:34 adam
22 * First use of string-queue utility.
24 * Revision 1.13 1995/03/28 08:01:25 adam
25 * FIFO existence is used to test for a running kernel.
27 * Revision 1.12 1995/03/27 12:51:05 adam
28 * New log level in use: GW_LOG_ERRNO.
30 * Revision 1.11 1995/03/27 08:24:02 adam
31 * First use of gip interface and gw-db.
32 * First work on eti program.
34 * Revision 1.10 1995/03/01 14:32:25 adam
35 * Better diagnostics. Default is, that only one database selected when
38 * Revision 1.9 1995/02/23 08:32:17 adam
41 * Revision 1.7 1995/02/22 15:22:33 adam
42 * Much more checking of run-time state. Show command never retrieves
43 * more records than indicated by the previous search request. Help
44 * command available. The maximum number of records retrieved can be
47 * Revision 1.6 1995/02/22 08:51:34 adam
48 * Output function can be customized in fml, which is used to print
49 * the reply to reply_fd.
51 * Revision 1.5 1995/02/20 21:16:20 adam
52 * FML support. Bug fixes. Profile for drewdb.
54 * Revision 1.4 1995/02/17 17:06:16 adam
57 * Revision 1.3 1995/02/16 18:35:09 adam
58 * First use of Zdist library. Search requests are supported.
59 * Present requests are not supported yet.
61 * Revision 1.2 1995/02/16 13:21:00 adam
62 * Organization of resource files for targets and conversion
63 * language implemented.
65 * Revision 1.1 1995/02/15 17:45:29 adam
66 * First version of email gateway kernel. Email requests are read
67 * from stdin. The output is transferred to an MTA if 'From' is
68 * found in the header - or stdout if absent. No Z39.50 client is used.
77 #include <sys/types.h>
85 FILE *reply_fd = stdout;
87 struct gw_kernel_info info;
89 static void kernel_events (struct str_queue *queue, int userid)
91 char fifo_client_name[1024];
92 char fifo_server_name[1024];
101 timeout = atoi(gw_res_get (info.kernel_res, "gw.timeout", "600"));
102 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "kernel event loop");
104 sprintf (fifo_client_name, "fifo.c.%d", userid);
105 sprintf (fifo_server_name, "fifo.s.%d", userid);
107 gip = gips_initialize (fifo_server_name);
108 gips_open (gip, fifo_client_name);
109 gip_fd = gip_infileno (gip);
110 open (fifo_server_name, O_WRONLY);
115 FD_SET (gip_fd, &set_r);
119 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
120 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
123 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
128 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds",
131 save_p_state (userid);
134 if (FD_ISSET (gip_fd, &set_r))
136 char command[128], *cp;
138 if (!(lgets (command, 127, gip_fd)))
140 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
143 if ((cp = strchr (command, '\n')))
145 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
146 if (!strcmp (command, "mail"))
148 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail...\n");
149 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
150 str_queue_enq (queue, line_buf);
151 urp_start (continuation, queue);
153 load_p_state (userid);
156 while (str_queue_deq (queue, 0, 0))
159 else if (!strcmp (command, "stop"))
161 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
166 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
173 unlink (fifo_client_name);
174 unlink (fifo_server_name);
177 int main (int argc, char **argv)
180 struct str_queue *queue;
182 info.kernel_res = NULL;
183 info.default_res = "default.res";
184 info.override_res = NULL;
190 info.override_portno = NULL;
191 info.override_hostname = NULL;
192 info.databases = NULL;
193 info.database = NULL;
201 info.kernel_res = gw_res_init ();
209 fprintf (stderr, "kernel [option..] [resource]\n");
210 fprintf (stderr, "If no resource file is given");
211 fprintf (stderr, " default.res is used\n");
212 fprintf (stderr, "Options:\n");
213 fprintf (stderr, " -d Enable debugging log\n");
214 fprintf (stderr, " -t target Open target immediately\n");
215 fprintf (stderr, " -g lang Set language\n");
216 fprintf (stderr, " -o resource Override with resource\n");
217 fprintf (stderr, " -h host Override host\n");
218 fprintf (stderr, " -p port Override port\n");
219 fprintf (stderr, " -l log Set Log file\n");
220 fprintf (stderr, " -i id Set IPC userid\n");
223 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
226 gw_log_level (GW_LOG_ALL);
230 strcpy (info.target, argv[0]+2);
234 strcpy (info.target, *++argv);
238 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
244 info.lang = argv[0]+2;
252 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
258 info.override_res = argv[0]+2;
262 info.override_res = *++argv;
266 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
272 info.override_portno = argv[0]+2;
276 info.override_portno = *++argv;
280 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
286 info.override_hostname = argv[0]+2;
290 info.override_hostname = *++argv;
294 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
300 gw_log_file (GW_LOG_ALL, argv[0]+2);
304 gw_log_file (GW_LOG_ALL, *++argv);
308 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
314 userid = atoi (argv[0]+2);
318 userid = atoi (*++argv);
322 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
327 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
332 info.default_res = *argv;
334 if (!(queue = str_queue_mk ()))
336 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
342 kernel_events (queue, userid);
348 while (lgets (line_buf, sizeof(line_buf)-1, 0))
349 str_queue_enq (queue, line_buf);
350 urp_start (0, queue);
357 struct gw_user_set *user_set_add (const char *name, int hits,
358 const char *database,
359 struct ccl_rpn_node *rpn,
361 const char *search_str)
363 struct gw_user_set *s;
365 s = malloc (sizeof (*s));
368 s->name = gw_strdup (name);
370 s->database = gw_strdup (database);
372 s->present_flag = present_flag;
373 s->search_str = gw_strdup (search_str);
379 void user_set_init (void)
381 struct gw_user_set *s, *s1;
383 for (s = info.sets; s; s = s1)
387 ccl_rpn_delete (s->rpn);
394 struct gw_user_set *user_set_search (const char *name)
396 struct gw_user_set *s;
400 for (s = info.sets; s; s = s->prev)
401 if (!strcmp (s->name, name))
407 static void fml_inf_write (int ch)
411 static FILE *fml_inf;
413 static int fml_inf_read (void)
415 return getc (fml_inf);
419 void read_kernel_res (void)
421 char path_prefix[128];
425 char resource_name[256];
430 ccl_qual_rm (&info.bibset);
431 info.bibset = ccl_qual_mk ();
434 gw_res_close (info.kernel_res);
435 info.kernel_res = gw_res_init ();
437 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
440 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
443 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
446 if (gw_res_merge (info.kernel_res, info.default_res))
448 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
452 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
456 sprintf (resource_name, "gw.target.%s", info.target);
457 v = gw_res_get (info.kernel_res, resource_name, NULL);
460 sprintf (fname, "%s/%s", path_prefix, v);
461 gw_res_merge (info.kernel_res, fname);
466 sprintf (resource_name, "gw.lang.%s", info.lang);
467 v = gw_res_get (info.kernel_res, resource_name, NULL);
470 sprintf (fname, "%s/%s", path_prefix, v);
471 gw_res_merge (info.kernel_res, fname);
474 if (info.override_res)
476 sprintf (fname, "%s/%s", path_prefix, info.override_res);
477 gw_res_merge (info.kernel_res, fname);
479 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
484 sprintf (fname, "%s/%s", path_prefix, v);
485 bib_inf = fopen (fname, "r");
487 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
490 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
491 ccl_qual_file (info.bibset, bib_inf);
495 sprintf (resource_name, "gw.target.%s", info.target);
496 if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
498 /* target is there, and there is no sub-resource for it... */
501 if ((split = strchr (info.target, ':')))
503 memcpy (info.hostname, info.target, split-info.target);
504 info.hostname[split-info.target] = '\0';
505 info.port = atoi (split+1);
509 strcpy (info.hostname, info.target);
510 info.port = atoi (gw_res_get
511 (info.kernel_res, "gw.portno", "210"));
516 strncpy (info.hostname, gw_res_get (info.kernel_res,
517 "gw.hostname", "localhost"),
518 sizeof(info.hostname)-1);
519 info.port = atoi (gw_res_get (info.kernel_res,
520 "gw.portno", "210"));
521 strcpy (info.account, gw_res_get (info.kernel_res, "gw.account", ""));
524 free (info.databases);
526 free (info.database);
527 v = gw_res_get (info.kernel_res, "gw.databases", "");
528 info.databases = gw_strdup (v);
529 for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
531 v = gw_res_get (info.kernel_res, "gw.database", "");
532 if (*v == '\0' && *info.databases)
535 cp = strchr (info.databases, ',');
537 len = cp ? (cp-info.databases) : strlen (info.databases);
538 info.database = malloc (len+1);
539 assert (info.database);
540 memcpy (info.database, info.databases, len);
541 info.database[len] = '\0';
545 info.database = gw_strdup (v);
546 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
549 if (info.override_portno)
550 info.port = atoi (info.override_portno);
551 if (info.override_hostname)
552 strncpy (info.hostname, info.override_hostname,
553 sizeof(info.hostname)-1);
554 v = gw_res_get (info.kernel_res, "gw.result.set", NULL);
555 info.setno = v ? -1 : 0;
559 v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
560 sprintf (fname, "%s/%s", path_prefix, v);
561 fml_inf = fopen (fname, "r");
563 gw_log (GW_LOG_WARN, KERNEL_LOG,
564 "cannot open fml script %s", fname);
567 info.fml = fml_open ();
568 info.fml->read_func = fml_inf_read;
569 info.fml->write_func = fml_inf_write;
570 fml_preprocess (info.fml);