1 /* Gateway kernel - Main
5 * Revision 1.20 1995/05/01 12:43:32 adam
6 * First work on resource monitor program.
8 * Revision 1.19 1995/04/19 16:01:58 adam
9 * Some hacks to get the FIFO communication work!! Isn't reliable.
10 * Resource gw.account added - default account info.
12 * Revision 1.18 1995/04/19 13:19:09 adam
13 * New command: account - for authentication.
15 * Revision 1.17 1995/04/19 10:46:18 adam
16 * Persistency works much better now. New command: status - history-like
18 * Revision 1.16 1995/04/19 07:31:07 adam
19 * First work on Z39.50 persistence.
21 * Revision 1.15 1995/04/17 09:34:30 adam
22 * Timeout (idletime) adjustable. Minor changes in kernel.
24 * Revision 1.14 1995/03/28 11:42:34 adam
25 * First use of string-queue utility.
27 * Revision 1.13 1995/03/28 08:01:25 adam
28 * FIFO existence is used to test for a running kernel.
30 * Revision 1.12 1995/03/27 12:51:05 adam
31 * New log level in use: GW_LOG_ERRNO.
33 * Revision 1.11 1995/03/27 08:24:02 adam
34 * First use of gip interface and gw-db.
35 * First work on eti program.
37 * Revision 1.10 1995/03/01 14:32:25 adam
38 * Better diagnostics. Default is, that only one database selected when
41 * Revision 1.9 1995/02/23 08:32:17 adam
44 * Revision 1.7 1995/02/22 15:22:33 adam
45 * Much more checking of run-time state. Show command never retrieves
46 * more records than indicated by the previous search request. Help
47 * command available. The maximum number of records retrieved can be
50 * Revision 1.6 1995/02/22 08:51:34 adam
51 * Output function can be customized in fml, which is used to print
52 * the reply to reply_fd.
54 * Revision 1.5 1995/02/20 21:16:20 adam
55 * FML support. Bug fixes. Profile for drewdb.
57 * Revision 1.4 1995/02/17 17:06:16 adam
60 * Revision 1.3 1995/02/16 18:35:09 adam
61 * First use of Zdist library. Search requests are supported.
62 * Present requests are not supported yet.
64 * Revision 1.2 1995/02/16 13:21:00 adam
65 * Organization of resource files for targets and conversion
66 * language implemented.
68 * Revision 1.1 1995/02/15 17:45:29 adam
69 * First version of email gateway kernel. Email requests are read
70 * from stdin. The output is transferred to an MTA if 'From' is
71 * found in the header - or stdout if absent. No Z39.50 client is used.
80 #include <sys/types.h>
88 FILE *reply_fd = stdout;
90 struct gw_kernel_info info;
92 static void kernel_events (struct str_queue *queue, int userid)
94 char fifo_client_name[1024];
95 char fifo_server_name[1024];
102 int continuation = 0;
104 timeout = atoi(gw_res_get (info.kernel_res, "gw.timeout", "600"));
105 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "kernel event loop");
107 sprintf (fifo_client_name, "fifo.c.%d", userid);
108 sprintf (fifo_server_name, "fifo.s.%d", userid);
110 gip = gips_initialize (fifo_server_name);
111 gips_open (gip, fifo_client_name);
112 gip_fd = gip_infileno (gip);
113 open (fifo_server_name, O_WRONLY);
118 FD_SET (gip_fd, &set_r);
122 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
123 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
126 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
131 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds",
134 save_p_state (userid);
137 if (FD_ISSET (gip_fd, &set_r))
139 char command[128], *cp;
141 if (!(lgets (command, 127, gip_fd)))
143 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
146 if ((cp = strchr (command, '\n')))
148 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
149 if (!strcmp (command, "mail"))
151 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail");
152 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
153 str_queue_enq (queue, line_buf);
154 urp_start (continuation, queue);
156 load_p_state (userid);
159 while (str_queue_deq (queue, 0, 0))
162 else if (!strcmp (command, "stop"))
164 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
169 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
176 unlink (fifo_client_name);
177 unlink (fifo_server_name);
180 int main (int argc, char **argv)
183 struct str_queue *queue;
185 info.kernel_res = NULL;
186 info.default_res = "default.res";
187 info.override_res = NULL;
193 info.override_portno = NULL;
194 info.override_hostname = NULL;
195 info.databases = NULL;
196 info.database = NULL;
204 info.kernel_res = gw_res_init ();
212 fprintf (stderr, "kernel [option..] [resource]\n");
213 fprintf (stderr, "If no resource file is given");
214 fprintf (stderr, " default.res is used\n");
215 fprintf (stderr, "Options:\n");
216 fprintf (stderr, " -d Enable debugging log\n");
217 fprintf (stderr, " -t target Open target immediately\n");
218 fprintf (stderr, " -g lang Set language\n");
219 fprintf (stderr, " -o resource Override with resource\n");
220 fprintf (stderr, " -h host Override host\n");
221 fprintf (stderr, " -p port Override port\n");
222 fprintf (stderr, " -l log Set Log file\n");
223 fprintf (stderr, " -i id Set IPC userid\n");
226 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
229 gw_log_level (GW_LOG_ALL);
233 strcpy (info.target, argv[0]+2);
237 strcpy (info.target, *++argv);
241 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
247 info.lang = argv[0]+2;
255 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
261 info.override_res = argv[0]+2;
265 info.override_res = *++argv;
269 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
275 info.override_portno = argv[0]+2;
279 info.override_portno = *++argv;
283 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
289 info.override_hostname = argv[0]+2;
293 info.override_hostname = *++argv;
297 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
303 gw_log_file (GW_LOG_ALL, argv[0]+2);
307 gw_log_file (GW_LOG_ALL, *++argv);
311 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
317 userid = atoi (argv[0]+2);
321 userid = atoi (*++argv);
325 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
330 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
335 info.default_res = *argv;
337 if (!(queue = str_queue_mk ()))
339 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
345 kernel_events (queue, userid);
351 while (lgets (line_buf, sizeof(line_buf)-1, 0))
352 str_queue_enq (queue, line_buf);
353 urp_start (0, queue);
360 struct gw_user_set *user_set_add (const char *name, int hits,
361 const char *database,
362 struct ccl_rpn_node *rpn,
364 const char *search_str)
366 struct gw_user_set *s;
368 s = malloc (sizeof (*s));
371 s->name = gw_strdup (name);
373 s->database = gw_strdup (database);
375 s->present_flag = present_flag;
376 s->search_str = gw_strdup (search_str);
382 void user_set_init (void)
384 struct gw_user_set *s, *s1;
386 for (s = info.sets; s; s = s1)
390 ccl_rpn_delete (s->rpn);
397 struct gw_user_set *user_set_search (const char *name)
399 struct gw_user_set *s;
403 for (s = info.sets; s; s = s->prev)
404 if (!strcmp (s->name, name))
410 static void fml_inf_write (int ch)
414 static FILE *fml_inf;
416 static int fml_inf_read (void)
418 return getc (fml_inf);
422 void read_kernel_res (void)
424 char path_prefix[128];
428 char resource_name[256];
433 ccl_qual_rm (&info.bibset);
434 info.bibset = ccl_qual_mk ();
437 gw_res_close (info.kernel_res);
438 info.kernel_res = gw_res_init ();
440 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
443 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
446 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
449 if (gw_res_merge (info.kernel_res, info.default_res))
451 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
455 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
459 sprintf (resource_name, "gw.target.%s", info.target);
460 v = gw_res_get (info.kernel_res, resource_name, NULL);
463 sprintf (fname, "%s/%s", path_prefix, v);
464 gw_res_merge (info.kernel_res, fname);
469 sprintf (resource_name, "gw.lang.%s", info.lang);
470 v = gw_res_get (info.kernel_res, resource_name, NULL);
473 sprintf (fname, "%s/%s", path_prefix, v);
474 gw_res_merge (info.kernel_res, fname);
477 if (info.override_res)
479 sprintf (fname, "%s/%s", path_prefix, info.override_res);
480 gw_res_merge (info.kernel_res, fname);
482 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
487 sprintf (fname, "%s/%s", path_prefix, v);
488 bib_inf = fopen (fname, "r");
490 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
493 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
494 ccl_qual_file (info.bibset, bib_inf);
498 sprintf (resource_name, "gw.target.%s", info.target);
499 if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
501 /* target is there, and there is no sub-resource for it... */
504 if ((split = strchr (info.target, ':')))
506 memcpy (info.hostname, info.target, split-info.target);
507 info.hostname[split-info.target] = '\0';
508 info.port = atoi (split+1);
512 strcpy (info.hostname, info.target);
513 info.port = atoi (gw_res_get
514 (info.kernel_res, "gw.portno", "210"));
519 strncpy (info.hostname, gw_res_get (info.kernel_res,
520 "gw.hostname", "localhost"),
521 sizeof(info.hostname)-1);
522 info.port = atoi (gw_res_get (info.kernel_res,
523 "gw.portno", "210"));
524 strcpy (info.account, gw_res_get (info.kernel_res, "gw.account", ""));
527 free (info.databases);
529 free (info.database);
530 v = gw_res_get (info.kernel_res, "gw.databases", "");
531 info.databases = gw_strdup (v);
532 for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
534 v = gw_res_get (info.kernel_res, "gw.database", "");
535 if (*v == '\0' && *info.databases)
538 cp = strchr (info.databases, ',');
540 len = cp ? (cp-info.databases) : strlen (info.databases);
541 info.database = malloc (len+1);
542 assert (info.database);
543 memcpy (info.database, info.databases, len);
544 info.database[len] = '\0';
548 info.database = gw_strdup (v);
549 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
552 if (info.override_portno)
553 info.port = atoi (info.override_portno);
554 if (info.override_hostname)
555 strncpy (info.hostname, info.override_hostname,
556 sizeof(info.hostname)-1);
557 v = gw_res_get (info.kernel_res, "gw.result.set", NULL);
558 info.setno = v ? -1 : 0;
562 v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
563 sprintf (fname, "%s/%s", path_prefix, v);
564 fml_inf = fopen (fname, "r");
566 gw_log (GW_LOG_WARN, KERNEL_LOG,
567 "cannot open fml script %s", fname);
570 info.fml = fml_open ();
571 info.fml->read_func = fml_inf_read;
572 info.fml->write_func = fml_inf_write;
573 fml_preprocess (info.fml);