1 /* Gateway kernel - Main
5 * Revision 1.22 1995/05/03 07:37:39 adam
6 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
7 * are used when possible.
9 * Revision 1.21 1995/05/01 16:26:56 adam
10 * More work on resource monitor.
12 * Revision 1.20 1995/05/01 12:43:32 adam
13 * First work on resource monitor program.
15 * Revision 1.19 1995/04/19 16:01:58 adam
16 * Some hacks to get the FIFO communication work!! Isn't reliable.
17 * Resource gw.account added - default account info.
19 * Revision 1.18 1995/04/19 13:19:09 adam
20 * New command: account - for authentication.
22 * Revision 1.17 1995/04/19 10:46:18 adam
23 * Persistency works much better now. New command: status - history-like
25 * Revision 1.16 1995/04/19 07:31:07 adam
26 * First work on Z39.50 persistence.
28 * Revision 1.15 1995/04/17 09:34:30 adam
29 * Timeout (idletime) adjustable. Minor changes in kernel.
31 * Revision 1.14 1995/03/28 11:42:34 adam
32 * First use of string-queue utility.
34 * Revision 1.13 1995/03/28 08:01:25 adam
35 * FIFO existence is used to test for a running kernel.
37 * Revision 1.12 1995/03/27 12:51:05 adam
38 * New log level in use: GW_LOG_ERRNO.
40 * Revision 1.11 1995/03/27 08:24:02 adam
41 * First use of gip interface and gw-db.
42 * First work on eti program.
44 * Revision 1.10 1995/03/01 14:32:25 adam
45 * Better diagnostics. Default is, that only one database selected when
48 * Revision 1.9 1995/02/23 08:32:17 adam
51 * Revision 1.7 1995/02/22 15:22:33 adam
52 * Much more checking of run-time state. Show command never retrieves
53 * more records than indicated by the previous search request. Help
54 * command available. The maximum number of records retrieved can be
57 * Revision 1.6 1995/02/22 08:51:34 adam
58 * Output function can be customized in fml, which is used to print
59 * the reply to reply_fd.
61 * Revision 1.5 1995/02/20 21:16:20 adam
62 * FML support. Bug fixes. Profile for drewdb.
64 * Revision 1.4 1995/02/17 17:06:16 adam
67 * Revision 1.3 1995/02/16 18:35:09 adam
68 * First use of Zdist library. Search requests are supported.
69 * Present requests are not supported yet.
71 * Revision 1.2 1995/02/16 13:21:00 adam
72 * Organization of resource files for targets and conversion
73 * language implemented.
75 * Revision 1.1 1995/02/15 17:45:29 adam
76 * First version of email gateway kernel. Email requests are read
77 * from stdin. The output is transferred to an MTA if 'From' is
78 * found in the header - or stdout if absent. No Z39.50 client is used.
87 #include <sys/types.h>
95 FILE *reply_fd = stdout;
97 struct gw_kernel_info info;
99 static void kernel_events (struct str_queue *queue, int userid)
101 char fifo_client_name[1024];
102 char fifo_server_name[1024];
109 int continuation = 0;
113 persist_flag = gw_res_bool (info.kernel_res, "gw.persist", 0);
114 timeout = gw_res_int (info.kernel_res, "gw.timeout", 600);
115 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "event loop");
117 sprintf (fifo_client_name, "fifo.c.%d", userid);
118 sprintf (fifo_server_name, "fifo.s.%d", userid);
120 gip = gips_initialize (fifo_server_name);
121 gips_open (gip, fifo_client_name);
122 gip_fd = gip_infileno (gip);
123 extra_fd = open (fifo_server_name, O_WRONLY);
128 FD_SET (gip_fd, &set_r);
132 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
133 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
136 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
141 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds",
143 if (info.zass && persist_flag)
144 save_p_state (userid);
147 if (FD_ISSET (gip_fd, &set_r))
149 char command[128], *cp;
151 if (!(lgets (command, 127, gip_fd)))
153 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
156 if ((cp = strchr (command, '\n')))
158 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
159 if (!strcmp (command, "mail"))
161 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail");
162 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
163 str_queue_enq (queue, line_buf);
164 urp_start (continuation, queue);
165 if (persist_flag && !continuation)
166 load_p_state (userid);
167 r = urp_command (queue);
168 if (persist_flag && r == 1)
169 del_p_state (userid);
171 while (str_queue_deq (queue, 0, 0))
174 else if (!strcmp (command, "stop"))
176 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
181 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
189 unlink (fifo_client_name);
190 unlink (fifo_server_name);
193 int main (int argc, char **argv)
196 struct str_queue *queue;
198 info.kernel_res = NULL;
199 info.default_res = "default.res";
200 info.override_res = NULL;
206 info.override_portno = NULL;
207 info.override_hostname = NULL;
208 info.databases = NULL;
209 info.database = NULL;
217 info.kernel_res = gw_res_init ();
225 fprintf (stderr, "kernel [option..] [resource]\n");
226 fprintf (stderr, "If no resource file is given");
227 fprintf (stderr, " default.res is used\n");
228 fprintf (stderr, "Options:\n");
229 fprintf (stderr, " -d Enable debugging log\n");
230 fprintf (stderr, " -t target Open target immediately\n");
231 fprintf (stderr, " -g lang Set language\n");
232 fprintf (stderr, " -o resource Override with resource\n");
233 fprintf (stderr, " -h host Override host\n");
234 fprintf (stderr, " -p port Override port\n");
235 fprintf (stderr, " -l log Set Log file\n");
236 fprintf (stderr, " -i id Set IPC userid\n");
239 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
242 gw_log_level (GW_LOG_ALL);
246 strcpy (info.target, argv[0]+2);
250 strcpy (info.target, *++argv);
254 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
260 info.lang = argv[0]+2;
268 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
274 info.override_res = argv[0]+2;
278 info.override_res = *++argv;
282 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
288 info.override_portno = argv[0]+2;
292 info.override_portno = *++argv;
296 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
302 info.override_hostname = argv[0]+2;
306 info.override_hostname = *++argv;
310 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
316 gw_log_file (GW_LOG_ALL, argv[0]+2);
320 gw_log_file (GW_LOG_ALL, *++argv);
324 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
330 userid = atoi (argv[0]+2);
334 userid = atoi (*++argv);
338 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
343 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
348 info.default_res = *argv;
350 if (!(queue = str_queue_mk ()))
352 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
358 kernel_events (queue, userid);
364 while (lgets (line_buf, sizeof(line_buf)-1, 0))
365 str_queue_enq (queue, line_buf);
366 urp_start (0, queue);
373 struct gw_user_set *user_set_add (const char *name, int hits,
374 const char *database,
375 struct ccl_rpn_node *rpn,
377 const char *search_str)
379 struct gw_user_set *s;
381 s = malloc (sizeof (*s));
384 s->name = gw_strdup (name);
386 s->database = gw_strdup (database);
388 s->present_flag = present_flag;
389 s->search_str = gw_strdup (search_str);
395 void user_set_init (void)
397 struct gw_user_set *s, *s1;
399 for (s = info.sets; s; s = s1)
403 ccl_rpn_delete (s->rpn);
410 struct gw_user_set *user_set_search (const char *name)
412 struct gw_user_set *s;
416 for (s = info.sets; s; s = s->prev)
417 if (!strcmp (s->name, name))
423 static void fml_inf_write (int ch)
427 static FILE *fml_inf;
429 static int fml_inf_read (void)
431 return getc (fml_inf);
435 void read_kernel_res (void)
437 char path_prefix[128];
441 char resource_name[256];
446 ccl_qual_rm (&info.bibset);
447 info.bibset = ccl_qual_mk ();
450 gw_res_close (info.kernel_res);
451 info.kernel_res = gw_res_init ();
453 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
456 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
459 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
462 if (gw_res_merge (info.kernel_res, info.default_res))
464 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
468 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
472 sprintf (resource_name, "gw.target.%s", info.target);
473 v = gw_res_get (info.kernel_res, resource_name, NULL);
476 sprintf (fname, "%s/%s", path_prefix, v);
477 gw_res_merge (info.kernel_res, fname);
482 sprintf (resource_name, "gw.lang.%s", info.lang);
483 v = gw_res_get (info.kernel_res, resource_name, NULL);
486 sprintf (fname, "%s/%s", path_prefix, v);
487 gw_res_merge (info.kernel_res, fname);
490 if (info.override_res)
492 sprintf (fname, "%s/%s", path_prefix, info.override_res);
493 gw_res_merge (info.kernel_res, fname);
495 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
500 sprintf (fname, "%s/%s", path_prefix, v);
501 bib_inf = fopen (fname, "r");
503 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
506 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
507 ccl_qual_file (info.bibset, bib_inf);
511 sprintf (resource_name, "gw.target.%s", info.target);
512 if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
514 /* target is there, and there is no sub-resource for it... */
517 if ((split = strchr (info.target, ':')))
519 memcpy (info.hostname, info.target, split-info.target);
520 info.hostname[split-info.target] = '\0';
521 info.port = atoi (split+1);
525 strcpy (info.hostname, info.target);
526 info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
531 strncpy (info.hostname, gw_res_get (info.kernel_res,
532 "gw.hostname", "localhost"),
533 sizeof(info.hostname)-1);
534 info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
535 strcpy (info.account, gw_res_get (info.kernel_res, "gw.account", ""));
538 free (info.databases);
540 free (info.database);
541 v = gw_res_get (info.kernel_res, "gw.databases", "");
542 info.databases = gw_strdup (v);
543 for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
545 v = gw_res_get (info.kernel_res, "gw.database", "");
546 if (*v == '\0' && *info.databases)
549 cp = strchr (info.databases, ',');
551 len = cp ? (cp-info.databases) : strlen (info.databases);
552 info.database = malloc (len+1);
553 assert (info.database);
554 memcpy (info.database, info.databases, len);
555 info.database[len] = '\0';
559 info.database = gw_strdup (v);
560 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
563 if (info.override_portno)
564 info.port = atoi (info.override_portno);
565 if (info.override_hostname)
566 strncpy (info.hostname, info.override_hostname,
567 sizeof(info.hostname)-1);
568 if (gw_res_bool (info.kernel_res, "gw.result.set", 1))
575 v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
576 sprintf (fname, "%s/%s", path_prefix, v);
577 fml_inf = fopen (fname, "r");
579 gw_log (GW_LOG_WARN, KERNEL_LOG,
580 "cannot open fml script %s", fname);
583 info.fml = fml_open ();
584 info.fml->read_func = fml_inf_read;
585 info.fml->write_func = fml_inf_write;
586 fml_preprocess (info.fml);