Minor change to pre_init handler.
[idzebra-moved-to-github.git] / index / zserver.c
1 /*
2  * Copyright (C) 1995-1999, Index Data 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zserver.c,v $
7  * Revision 1.69  1999-06-10 09:20:03  adam
8  * Minor change to pre_init handler.
9  *
10  * Revision 1.68  1999/05/26 07:49:13  adam
11  * C++ compilation.
12  *
13  * Revision 1.67  1999/02/02 14:51:14  adam
14  * Updated WIN32 code specific sections. Changed header.
15  *
16  * Revision 1.66  1998/10/28 10:54:41  adam
17  * SDRKit integration.
18  *
19  * Revision 1.65  1998/10/18 07:54:54  adam
20  * Additional info added for diagnostics 114 (Unsupported use attribute) and
21  * 121 (Unsupported attribute set).
22  *
23  * Revision 1.64  1998/09/22 10:48:21  adam
24  * Minor changes in search API.
25  *
26  * Revision 1.63  1998/09/02 13:53:21  adam
27  * Extra parameter decode added to search routines to implement
28  * persistent queries.
29  *
30  * Revision 1.62  1998/08/06 14:35:28  adam
31  * Routine bend_deleterequest removed.
32  *
33  * Revision 1.61  1998/06/24 12:16:15  adam
34  * Support for relations on text operands. Open range support in
35  * DFA module (i.e. [-j], [g-]).
36  *
37  * Revision 1.60  1998/06/22 11:36:49  adam
38  * Added authentication check facility to zebra.
39  *
40  * Revision 1.59  1998/06/12 12:22:13  adam
41  * Work on Zebra API.
42  *
43  * Revision 1.58  1998/05/27 16:57:46  adam
44  * Zebra returns surrogate diagnostic for single records when
45  * appropriate.
46  *
47  * Revision 1.57  1998/04/03 14:45:18  adam
48  * Fixed setting of last_in_set in bend_fetch.
49  *
50  * Revision 1.56  1998/03/05 08:45:13  adam
51  * New result set model and modular ranking system. Moved towards
52  * descent server API. System information stored as "SGML" records.
53  *
54  * Revision 1.55  1998/02/10 12:03:06  adam
55  * Implemented Sort.
56  *
57  * Revision 1.54  1998/01/29 13:39:13  adam
58  * Compress ISAM is default.
59  *
60  * Revision 1.53  1998/01/12 15:04:09  adam
61  * The test option (-s) only uses read-lock (and not write lock).
62  *
63  * Revision 1.52  1997/11/18 10:05:08  adam
64  * Changed character map facility so that admin can specify character
65  * mapping files for each register type, w, p, etc.
66  *
67  * Revision 1.51  1997/10/27 14:33:06  adam
68  * Moved towards generic character mapping depending on "structure"
69  * field in abstract syntax file. Fixed a few memory leaks. Fixed
70  * bug with negative integers when doing searches with relational
71  * operators.
72  *
73  * Revision 1.50  1997/09/29 09:08:36  adam
74  * Revised locking system to be thread safe for the server.
75  *
76  * Revision 1.49  1997/09/25 14:57:23  adam
77  * Windows NT port.
78  *
79  * Revision 1.48  1997/09/17 12:19:19  adam
80  * Zebra version corresponds to YAZ version 1.4.
81  * Changed Zebra server so that it doesn't depend on global common_resource.
82  *
83  * Revision 1.47  1997/09/04 13:58:36  adam
84  * New retrieve/extract method tellf (added).
85  * Added O_BINARY for open calls.
86  *
87  * Revision 1.46  1997/07/28 08:30:47  adam
88  * Server returns diagnostic 14 when record doesn't exist.
89  *
90  * Revision 1.45  1996/12/23 15:30:45  adam
91  * Work on truncation.
92  * Bug fix: result sets weren't deleted after server shut down.
93  *
94  * Revision 1.44  1996/12/11 12:08:01  adam
95  * Added better compression.
96  *
97  * Revision 1.43  1996/11/15 15:03:58  adam
98  * Logging of execution speed by using the times(2) call.
99  *
100  * Revision 1.42  1996/11/08  11:10:36  adam
101  * Buffers used during file match got bigger.
102  * Compressed ISAM support everywhere.
103  * Bug fixes regarding masking characters in queries.
104  * Redesigned Regexp-2 queries.
105  *
106  * Revision 1.41  1996/10/29 14:09:56  adam
107  * Use of cisam system - enabled if setting isamc is 1.
108  *
109  * Revision 1.40  1996/06/04 10:19:02  adam
110  * Minor changes - removed include of ctype.h.
111  *
112  * Revision 1.39  1996/05/31  09:07:05  quinn
113  * Work on character-set handling
114  *
115  * Revision 1.38  1996/05/14  11:34:01  adam
116  * Scan support in multiple registers/databases.
117  *
118  * Revision 1.37  1996/05/14  06:16:48  adam
119  * Compact use/set bytes used in search service.
120  *
121  * Revision 1.36  1996/05/01 13:46:37  adam
122  * First work on multiple records in one file.
123  * New option, -offset, to the "unread" command in the filter module.
124  *
125  * Revision 1.35  1996/03/26  16:01:14  adam
126  * New setting lockPath: directory of various lock files.
127  *
128  * Revision 1.34  1996/03/20  09:36:46  adam
129  * Function dict_lookup_grep got extra parameter, init_pos, which marks
130  * from which position in pattern approximate pattern matching should occur.
131  * Approximate pattern matching is used in relevance=re-2.
132  *
133  * Revision 1.33  1996/01/17  14:57:56  adam
134  * Prototype changed for reader functions in extract/retrieve. File
135  *  is identified by 'void *' instead of 'int.
136  *
137  * Revision 1.32  1995/12/11  09:12:58  adam
138  * The rec_get function returns NULL if record doesn't exist - will
139  * happen in the server if the result set records have been deleted since
140  * the creation of the set (i.e. the search).
141  * The server saves a result temporarily if it is 'volatile', i.e. the
142  * set is register dependent.
143  *
144  * Revision 1.31  1995/12/08  16:22:56  adam
145  * Work on update while servers are running. Three lock files introduced.
146  * The servers reload their registers when necessary, but they don't
147  * reestablish result sets yet.
148  *
149  * Revision 1.30  1995/12/07  17:38:48  adam
150  * Work locking mechanisms for concurrent updates/commit.
151  *
152  * Revision 1.29  1995/12/04  14:22:32  adam
153  * Extra arg to recType_byName.
154  * Started work on new regular expression parsed input to
155  * structured records.
156  *
157  * Revision 1.28  1995/11/28  09:09:48  adam
158  * Zebra config renamed.
159  * Use setting 'recordId' to identify record now.
160  * Bug fix in recindex.c: rec_release_blocks was invokeded even
161  * though the blocks were already released.
162  * File traversal properly deletes records when needed.
163  *
164  * Revision 1.27  1995/11/27  13:58:54  adam
165  * New option -t. storeStore data implemented in server.
166  *
167  * Revision 1.26  1995/11/25  10:24:07  adam
168  * More record fields - they are enumerated now.
169  * New options: flagStoreData flagStoreKey.
170  *
171  * Revision 1.25  1995/11/21  15:29:13  adam
172  * Config file 'base' read by default by both indexer and server.
173  *
174  * Revision 1.24  1995/11/20  16:59:47  adam
175  * New update method: the 'old' keys are saved for each records.
176  *
177  * Revision 1.23  1995/11/16  17:00:56  adam
178  * Better logging of rpn query.
179  *
180  * Revision 1.22  1995/11/16  15:34:55  adam
181  * Uses new record management system in both indexer and server.
182  *
183  * Revision 1.21  1995/11/01  16:25:52  quinn
184  * *** empty log message ***
185  *
186  * Revision 1.20  1995/10/27  14:00:12  adam
187  * Implemented detection of database availability.
188  *
189  * Revision 1.19  1995/10/17  18:02:11  adam
190  * New feature: databases. Implemented as prefix to words in dictionary.
191  *
192  * Revision 1.18  1995/10/16  14:03:09  quinn
193  * Changes to support element set names and espec1
194  *
195  * Revision 1.17  1995/10/16  09:32:40  adam
196  * More work on relational op.
197  *
198  * Revision 1.16  1995/10/13  12:26:44  adam
199  * Optimization of truncation.
200  *
201  * Revision 1.15  1995/10/12  12:40:55  adam
202  * Bug fixes in rpn_prox.
203  *
204  * Revision 1.14  1995/10/09  16:18:37  adam
205  * Function dict_lookup_grep got extra client data parameter.
206  *
207  * Revision 1.13  1995/10/06  14:38:00  adam
208  * New result set method: r_score.
209  * Local no (sysno) and score is transferred to retrieveCtrl.
210  *
211  * Revision 1.12  1995/10/06  13:52:06  adam
212  * Bug fixes. Handler may abort further scanning.
213  *
214  * Revision 1.11  1995/10/06  10:43:57  adam
215  * Scan added. 'occurrences' in scan entries not set yet.
216  *
217  * Revision 1.10  1995/10/02  16:43:32  quinn
218  * Set default resulting record type in fetch.
219  *
220  * Revision 1.9  1995/10/02  15:18:52  adam
221  * New member in recRetrieveCtrl: diagnostic.
222  *
223  * Revision 1.8  1995/09/28  09:19:47  adam
224  * xfree/xmalloc used everywhere.
225  * Extract/retrieve method seems to work for text records.
226  *
227  * Revision 1.7  1995/09/27  16:17:32  adam
228  * More work on retrieve.
229  *
230  * Revision 1.6  1995/09/08  08:53:22  adam
231  * Record buffer maintained in server_info.
232  *
233  * Revision 1.5  1995/09/06  16:11:18  adam
234  * Option: only one word key per file.
235  *
236  * Revision 1.4  1995/09/06  10:33:04  adam
237  * More work on present. Some log messages removed.
238  *
239  * Revision 1.3  1995/09/05  15:28:40  adam
240  * More work on search engine.
241  *
242  * Revision 1.2  1995/09/04  12:33:43  adam
243  * Various cleanup. YAZ util used instead.
244  *
245  * Revision 1.1  1995/09/04  09:10:41  adam
246  * More work on index add/del/update.
247  * Merge sort implemented.
248  * Initial work on z39 server.
249  *
250  */
251
252 #include <stdio.h>
253 #include <assert.h>
254 #include <fcntl.h>
255 #ifdef WIN32
256 #include <io.h>
257 #include <process.h>
258 #else
259 #include <unistd.h>
260 #endif
261
262 #include <data1.h>
263 #include <dmalloc.h>
264
265 #include "zserver.h"
266
267 #ifndef ZEBRASDR
268 #define ZEBRASDR 0
269 #endif
270 #if ZEBRASDR
271 #include "zebrasdr.h"
272 #endif
273
274 static int bend_sort (void *handle, bend_sort_rr *rr);
275
276 bend_initresult *bend_init (bend_initrequest *q)
277 {
278     bend_initresult *r = (bend_initresult *)
279         odr_malloc (q->stream, sizeof(*r));
280     ZebraHandle zh;
281     struct statserv_options_block *sob;
282     char *user = NULL;
283     char *passwd = NULL;
284
285     r->errcode = 0;
286     r->errstring = 0;
287     q->bend_sort = bend_sort;
288
289     logf (LOG_DEBUG, "bend_init");
290
291     sob = statserv_getcontrol ();
292     if (!(zh = zebra_open (sob->configname)))
293     {
294         logf (LOG_FATAL, "Failed to open Zebra `%s'", sob->configname);
295         r->errcode = 1;
296         return r;
297     }
298     if (q->auth)
299     {
300         if (q->auth->which == Z_IdAuthentication_open)
301         {
302             char *openpass = xstrdup (q->auth->u.open);
303             char *cp = strchr (openpass, '/');
304             if (cp)
305             {
306                 *cp = '\0';
307                 user = nmem_strdup (odr_getmem (q->stream), openpass);
308                 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
309             }
310             xfree (openpass);
311         }
312     }
313     if (zebra_auth (zh, user, passwd))
314     {
315         r->errcode = 222;
316         r->errstring = user;
317         zebra_close (zh);
318         return r;
319     }
320     r->handle = zh;
321     return r;
322 }
323
324 bend_searchresult *bend_search (void *handle, bend_searchrequest *q, int *fd)
325 {
326     ZebraHandle zh = (ZebraHandle) handle;
327     bend_searchresult *r = (bend_searchresult *)
328         odr_malloc (q->stream, sizeof(*r));
329
330     r->hits = 0;
331     r->errcode = 0;
332     r->errstring = NULL;
333     
334     logf (LOG_LOG, "ResultSet '%s'", q->setname);
335     switch (q->query->which)
336     {
337     case Z_Query_type_1: case Z_Query_type_101:
338         zebra_search_rpn (zh, q->decode, q->stream, q->query->u.type_1,
339                           q->num_bases, q->basenames, q->setname);
340         r->errcode = zh->errCode;
341         r->errstring = zh->errString;
342         r->hits = zh->hits;
343         break;
344     case Z_Query_type_2:
345         r->errcode = 107;
346         r->errstring = "type-2";
347         break;
348     default:
349         r->errcode = 107;
350     }
351     return r;
352 }
353
354 bend_fetchresult *bend_fetch (void *handle, bend_fetchrequest *q, int *num)
355 {
356     ZebraHandle zh = (ZebraHandle) handle;
357     bend_fetchresult *r = (bend_fetchresult *)
358         odr_malloc (q->stream, sizeof(*r));
359     ZebraRetrievalRecord retrievalRecord;
360
361     retrievalRecord.position = q->number;
362     
363     r->last_in_set = 0;
364     zebra_records_retrieve (zh, q->stream, q->setname, q->comp,
365                             q->format, 1, &retrievalRecord);
366     if (zh->errCode)                  /* non Surrogate Diagnostic */
367     {
368         r->errcode = zh->errCode;
369         r->errstring = zh->errString;
370     }
371     else if (retrievalRecord.errCode) /* Surrogate Diagnostic */
372     {
373         q->surrogate_flag = 1;
374         r->errcode = retrievalRecord.errCode;
375         r->errstring = retrievalRecord.errString;
376         r->basename = retrievalRecord.base;
377     }
378     else                              /* Database Record */
379     {
380         r->errcode = 0;
381         r->basename = retrievalRecord.base;
382         r->record = retrievalRecord.buf;
383         r->len = retrievalRecord.len;
384         r->format = retrievalRecord.format;
385     }
386     return r;
387 }
388
389 bend_scanresult *bend_scan (void *handle, bend_scanrequest *q, int *num)
390 {
391     ZebraScanEntry *entries;
392     ZebraHandle zh = (ZebraHandle) handle;
393     bend_scanresult *r = (bend_scanresult *)
394         odr_malloc (q->stream, sizeof(*r));
395     int is_partial, i;
396     
397     r->term_position = q->term_position;
398     r->num_entries = q->num_entries;
399
400     r->entries = (struct scan_entry *)
401         odr_malloc (q->stream, sizeof(*r->entries) * q->num_entries);
402     zebra_scan (zh, q->stream, q->term,
403                 q->attributeset,
404                 q->num_bases, q->basenames,
405                 &r->term_position,
406                 &r->num_entries, &entries, &is_partial);
407     if (is_partial)
408         r->status = BEND_SCAN_PARTIAL;
409     else
410         r->status = BEND_SCAN_SUCCESS;
411     for (i = 0; i < r->num_entries; i++)
412     {
413         r->entries[i].term = entries[i].term;
414         r->entries[i].occurrences = entries[i].occurrences;
415     }
416     r->errcode = zh->errCode;
417     r->errstring = zh->errString;
418     return r;
419 }
420
421 void bend_close (void *handle)
422 {
423     zebra_close ((ZebraHandle) handle);
424 }
425
426 int bend_sort (void *handle, bend_sort_rr *rr)
427 {
428     ZebraHandle zh = (ZebraHandle) handle;
429
430     zebra_sort (zh, rr->stream,
431                 rr->num_input_setnames, (const char **) rr->input_setnames,
432                 rr->output_setname, rr->sort_sequence, &rr->sort_status);
433     rr->errcode = zh->errCode;
434     rr->errstring = zh->errString;
435     return 0;
436 }
437
438 #ifndef WIN32
439 static void pre_init (struct statserv_options_block *sob)
440 {
441     if (!sob->inetd) 
442     {
443         char *pidfile = "zebrasrv.pid";
444         int fd = creat (pidfile, 0666);
445         if (fd == -1)
446             logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
447         else
448         {
449             char pidstr[30];
450         
451             sprintf (pidstr, "%ld", (long) getpid ());
452             write (fd, pidstr, strlen(pidstr));
453             close (fd);
454         }
455     }
456 }
457 #endif
458
459 int main (int argc, char **argv)
460 {
461     struct statserv_options_block *sob;
462
463     sob = statserv_getcontrol ();
464     strcpy (sob->configname, FNAME_CONFIG);
465 #ifndef WIN32
466     sob->pre_init = pre_init;
467 #endif
468     statserv_setcontrol (sob);
469
470 #if ZEBRASDR
471     zebraSdr_std ();
472 #endif
473     return statserv_main (argc, argv);
474 }