Monitor observes death of child (email kernel). The number
[egate.git] / kernel / persist.c
1 /* Gateway kernel - Z39.50 Persistence
2  * Europagate, 1995
3  *
4  * $Log: persist.c,v $
5  * Revision 1.5  1995/05/02 15:26:00  adam
6  * Monitor observes death of child (email kernel). The number
7  * of simultanous processes is controlled now. Email requests are
8  * queued if necessary. This scheme should only be forced if no kernels
9  * are idle.
10  *
11  * Revision 1.4  1995/04/20  16:10:46  adam
12  * Modified to work with non-blocking zass-api. Not using non-blocking
13  * facility yet.
14  *
15  * Revision 1.3  1995/04/19  13:19:09  adam
16  * New command: account - for authentication.
17  *
18  * Revision 1.2  1995/04/19  10:46:19  adam
19  * Persistency works much better now. New command: status - history-like
20  *
21  * Revision 1.1  1995/04/19  07:31:10  adam
22  * First work on Z39.50 persistence.
23  *
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33
34 #include "kernel.h"
35
36 static int fgetsx (char *buf, int size, FILE *inf)
37 {
38     char *cp;
39
40     if (!fgets (buf, size, inf))
41         return 0;
42     if ((cp = strchr (buf, '\n')))
43         *cp = '\0';
44     return 1;
45 }
46
47 static int set_change;
48
49 static int obtain_set (ZASS zass, struct gw_user_set *set)
50 {
51     const struct zass_searchent *p;
52
53     p = zass_search (zass, set->rpn, set->name, set->database, NULL);
54     if (!p)
55         return 2;
56     if (p->errcode != -1)
57         return 3;
58     if (p->num != set->hits)
59         set_change = 1;
60     set->present_flag = 1;
61     set->hits = p->num;
62     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s researched", set->name);
63     return 0;
64 }
65
66 static int obtain_sets (ZASS zass, struct ccl_rpn_node *rpn, 
67                         struct gw_user_set *sets)
68 {
69     struct gw_user_set *set;
70     int r;
71
72     switch (rpn->kind)
73     {
74     case CCL_RPN_AND:
75     case CCL_RPN_OR:
76     case CCL_RPN_NOT:
77     case CCL_RPN_PROX:
78         if ((r=obtain_sets (zass, rpn->u.p[0], sets)))
79             return r;
80         return obtain_sets (zass, rpn->u.p[1], sets);
81     case CCL_RPN_TERM:
82         return 0;
83     case CCL_RPN_SET:
84         set = user_set_search (rpn->u.setname);
85         if (!set)
86         {
87             gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s not there at all",
88                     rpn->u.setname);
89             return 1;
90         }
91         if (set->present_flag)
92         {
93             gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s already there",
94                     rpn->u.setname);
95             return 0;
96         }
97     }
98     return obtain_set (zass, set);
99 }
100
101 const struct zass_searchent *zass_p_search (ZASS zass, 
102                         struct ccl_rpn_node *rpn, 
103                         const char *result_set,
104                         const char *database,
105                         struct gw_user_set *sets)
106 {
107     int r;
108
109     set_change = 0;
110     r = obtain_sets (zass, rpn, sets);
111     if (r)
112         return NULL;
113     return zass_search (zass, rpn, (char*) result_set, (char*) database, NULL);
114 }
115
116 const struct zass_presentent *zass_p_present (ZASS zass,
117                         const char *result_set, int offset, int number)
118 {
119     struct gw_user_set *set;
120
121     set = user_set_search (result_set);
122     if (!set)
123         return NULL;
124     if (!set->present_flag)
125     {
126         const struct zass_searchent *p;
127
128         p = zass_p_search (zass, set->rpn, result_set, set->database,
129                            info.sets);
130         if (!p)
131             return NULL;
132     }
133     return zass_present (zass, (char*) result_set, offset, number, NULL);
134 }
135
136 struct ccl_rpn_node *load_rpn (char *buf, FILE *inf)
137 {
138     struct ccl_rpn_node *rpn;
139     struct ccl_rpn_attr **attrp;
140     int type, value, no_read;
141     char *cp;
142
143     if (!fgetsx (buf, 1024, inf))
144         return NULL;
145     rpn = malloc (sizeof (*rpn));
146     if (!rpn)
147         return NULL;
148     switch (*buf)
149     {
150     case 'A':
151         rpn->kind = CCL_RPN_AND;
152         rpn->u.p[0] = load_rpn (buf, inf);
153         rpn->u.p[1] = load_rpn (buf, inf);
154         break;
155     case 'O':
156         rpn->kind = CCL_RPN_OR;
157         rpn->u.p[0] = load_rpn (buf, inf);
158         rpn->u.p[1] = load_rpn (buf, inf);
159         break;
160     case 'N':
161         rpn->kind = CCL_RPN_NOT;
162         rpn->u.p[0] = load_rpn (buf, inf);
163         rpn->u.p[1] = load_rpn (buf, inf);
164         break;
165     case 'P':
166         rpn->kind = CCL_RPN_PROX;
167         rpn->u.p[0] = load_rpn (buf, inf);
168         rpn->u.p[1] = load_rpn (buf, inf);
169         break;
170     case 'T':
171         rpn->kind = CCL_RPN_TERM;
172
173         rpn->u.t.term = gw_strdup (buf+2);
174         attrp = &rpn->u.t.attr_list;
175         if (!fgetsx (buf, 1024, inf))
176             return NULL;
177         cp = buf;
178         while (sscanf (cp, "%d %d%n", &type, &value, &no_read) > 1)
179         {
180             *attrp = malloc (sizeof(**attrp));
181             (*attrp)->type = type;
182             (*attrp)->value = value;
183             attrp = &(*attrp)->next;
184             cp += no_read; 
185         }
186         *attrp = NULL;
187         break;
188     case 'S':
189         rpn->kind = CCL_RPN_SET;
190         rpn->u.setname = gw_strdup (buf+2);
191         break;
192     default:
193         free (rpn);
194         return NULL;
195     }
196     return rpn;
197 }
198
199 int load_p_state (int userid)
200 {
201     FILE *inf;
202     char fname[128];
203     char fline[1025];
204     char database[1024];
205     char resultname[32];
206     int hits;
207     struct gw_user_set *set;
208
209     sprintf (fname, "persist.%d", userid);
210
211     inf = fopen (fname, "r");
212     if (!inf)
213     {
214         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG, 
215                 "Couldn't open %s", fname);
216         return -1;
217     }
218     gw_log (GW_LOG_DEBUG, KERNEL_LOG, 
219        "Reading persistence file %s", fname);
220
221     if (!fgetsx (fline, 1024, inf))
222         return -1;
223     if (sscanf (fline, "%s", info.target) != 1)
224         *info.target = '\0';
225     read_kernel_res ();
226
227     if (!fgetsx (fline, 1024, inf))
228         return -1;
229     if (sscanf (fline, "%s", info.account) != 1)
230         *info.account = '\0';
231    
232     if (!fgetsx (fline, 1024, inf))
233         return -1;
234     free (info.database);
235     info.database = gw_strdup (fline);
236
237     if (!fgetsx (fline, 1024, inf))
238         return -1;
239     if (sscanf (fline, "%d", &info.setno) != 1)
240         return -1;
241     gw_log (GW_LOG_DEBUG, KERNEL_LOG, 
242            "Reading persistence file %s (2)", fname);
243 #if 0
244     reopen_target ();
245 #endif
246     while (fgetsx (fline, 1024, inf))
247     {
248         gw_log (GW_LOG_DEBUG, KERNEL_LOG, 
249               "Reading persistence file %s (3)", fname);
250         if (sscanf (fline, "%s %d %s", resultname, &hits, database) != 3)
251             return -1;
252         if (!fgetsx (fline, 1024, inf))        /* search string */
253             return -1;
254         gw_log (GW_LOG_DEBUG, KERNEL_LOG,
255                 "Adding %s, %d hits, database %s",
256                 resultname, hits, database);
257         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Search string %s", fline);
258         set = user_set_add (resultname, hits, database, NULL, 0, fline);
259         set->rpn = load_rpn (fline, inf);
260         ccl_pr_tree (set->rpn, stderr);
261         fgetsx (fline, 1024, inf);
262     }
263     fclose (inf);
264     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Finished reading %s", fname);
265     return 0;
266 }
267
268 static void save_rpn (struct ccl_rpn_node *rpn, FILE *of)
269 {
270     struct ccl_rpn_attr *attr;
271
272     switch (rpn->kind)
273     {
274     case CCL_RPN_AND:
275         fprintf (of, "A\n");
276         save_rpn (rpn->u.p[0], of);
277         save_rpn (rpn->u.p[1], of);
278         break;
279     case CCL_RPN_OR:
280         fprintf (of, "O\n");
281         save_rpn (rpn->u.p[0], of);
282         save_rpn (rpn->u.p[1], of);
283         break;
284     case CCL_RPN_NOT:
285         fprintf (of, "N\n");
286         save_rpn (rpn->u.p[0], of);
287         save_rpn (rpn->u.p[1], of);
288         break;
289     case CCL_RPN_PROX:
290         fprintf (of, "P\n");
291         save_rpn (rpn->u.p[0], of);
292         save_rpn (rpn->u.p[1], of);
293         break;
294     case CCL_RPN_TERM:
295         fprintf (of, "T %s\n", rpn->u.t.term);
296         for (attr = rpn->u.t.attr_list; attr; attr = attr->next)
297             fprintf (of, "%d %d ", attr->type, attr->value);
298         fprintf (of, "\n");
299         break;
300     case CCL_RPN_SET:
301         fprintf (of, "S %s\n", rpn->u.setname); 
302     }
303 }
304
305 static void save_sets (FILE *of, struct gw_user_set *sp)
306 {
307     if (!sp)
308         return;
309     save_sets (of, sp->prev);
310     fprintf (of, "%s %d %s\n%s\n", sp->name, sp->hits, sp->database,
311              sp->search_str);
312     save_rpn (sp->rpn, of);
313     fprintf (of, "X\n");
314 }
315
316 int save_p_state (int userid)
317 {
318     FILE *of;
319     char fname[128];
320
321     sprintf (fname, "persist.%d", userid);
322
323     of = fopen (fname, "w");
324     if (!of)
325     {
326         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG, 
327                 "Couldn't open %s", fname);
328         return -1;
329     }
330     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Writing persistence file %s", fname);
331     fprintf (of, "%s\n%s\n%s\n%d\n", info.target, info.account,
332              info.database, info.setno);
333     save_sets (of, info.sets);
334     fclose (of);
335     return 0;
336 }