Fix unused var.
[idzebra-moved-to-github.git] / index / zserver.c
1 /*
2  * Copyright (C) 1995-2000, Index Data 
3  * All rights reserved.
4  *
5  * $Log: zserver.c,v $
6  * Revision 1.82  2001-07-09 23:40:09  adam
7  * WIN32 fix.
8  *
9  * Revision 1.81  2001/02/14 21:42:07  adam
10  * Fixed versions as returned in INIT response.
11  *
12  * Revision 1.80  2000/09/05 14:04:05  adam
13  * Updates for prefix 'yaz_' for YAZ log functions.
14  *
15  * Revision 1.79  2000/05/09 10:56:50  adam
16  * Added call to xmalloc/nmem debugging functions.
17  *
18  * Revision 1.78  2000/04/05 09:49:35  adam
19  * On Unix, zebra/z'mbol uses automake.
20  *
21  * Revision 1.77  2000/03/20 19:08:36  adam
22  * Added remote record import using Z39.50 extended services and Segment
23  * Requests.
24  *
25  * Revision 1.76  2000/03/15 15:00:31  adam
26  * First work on threaded version.
27  *
28  * Revision 1.75  1999/11/30 13:48:04  adam
29  * Improved installation. Updated for inclusion of YAZ header files.
30  *
31  * Revision 1.74  1999/11/29 15:13:26  adam
32  * Server sets implementationName - and Version.
33  *
34  * Revision 1.73  1999/11/04 15:00:45  adam
35  * Implemented delete result set(s).
36  *
37  * Revision 1.71  1999/07/14 10:59:26  adam
38  * Changed functions isc_getmethod, isams_getmethod.
39  * Improved fatal error handling (such as missing EXPLAIN schema).
40  *
41  * Revision 1.70  1999/06/10 12:14:56  adam
42  * Fixed to use bend_start instead of pre_init.
43  *
44  * Revision 1.69  1999/06/10 09:20:03  adam
45  * Minor change to pre_init handler.
46  *
47  * Revision 1.68  1999/05/26 07:49:13  adam
48  * C++ compilation.
49  *
50  * Revision 1.67  1999/02/02 14:51:14  adam
51  * Updated WIN32 code specific sections. Changed header.
52  *
53  * Revision 1.66  1998/10/28 10:54:41  adam
54  * SDRKit integration.
55  *
56  * Revision 1.65  1998/10/18 07:54:54  adam
57  * Additional info added for diagnostics 114 (Unsupported use attribute) and
58  * 121 (Unsupported attribute set).
59  *
60  * Revision 1.64  1998/09/22 10:48:21  adam
61  * Minor changes in search API.
62  *
63  * Revision 1.63  1998/09/02 13:53:21  adam
64  * Extra parameter decode added to search routines to implement
65  * persistent queries.
66  *
67  * Revision 1.62  1998/08/06 14:35:28  adam
68  * Routine bend_deleterequest removed.
69  *
70  * Revision 1.61  1998/06/24 12:16:15  adam
71  * Support for relations on text operands. Open range support in
72  * DFA module (i.e. [-j], [g-]).
73  *
74  * Revision 1.60  1998/06/22 11:36:49  adam
75  * Added authentication check facility to zebra.
76  *
77  * Revision 1.59  1998/06/12 12:22:13  adam
78  * Work on Zebra API.
79  *
80  * Revision 1.58  1998/05/27 16:57:46  adam
81  * Zebra returns surrogate diagnostic for single records when
82  * appropriate.
83  *
84  * Revision 1.57  1998/04/03 14:45:18  adam
85  * Fixed setting of last_in_set in bend_fetch.
86  *
87  * Revision 1.56  1998/03/05 08:45:13  adam
88  * New result set model and modular ranking system. Moved towards
89  * descent server API. System information stored as "SGML" records.
90  *
91  * Revision 1.55  1998/02/10 12:03:06  adam
92  * Implemented Sort.
93  *
94  * Revision 1.54  1998/01/29 13:39:13  adam
95  * Compress ISAM is default.
96  *
97  * Revision 1.53  1998/01/12 15:04:09  adam
98  * The test option (-s) only uses read-lock (and not write lock).
99  *
100  * Revision 1.52  1997/11/18 10:05:08  adam
101  * Changed character map facility so that admin can specify character
102  * mapping files for each register type, w, p, etc.
103  *
104  * Revision 1.51  1997/10/27 14:33:06  adam
105  * Moved towards generic character mapping depending on "structure"
106  * field in abstract syntax file. Fixed a few memory leaks. Fixed
107  * bug with negative integers when doing searches with relational
108  * operators.
109  *
110  * Revision 1.50  1997/09/29 09:08:36  adam
111  * Revised locking system to be thread safe for the server.
112  *
113  * Revision 1.49  1997/09/25 14:57:23  adam
114  * Windows NT port.
115  *
116  * Revision 1.48  1997/09/17 12:19:19  adam
117  * Zebra version corresponds to YAZ version 1.4.
118  * Changed Zebra server so that it doesn't depend on global common_resource.
119  *
120  * Revision 1.47  1997/09/04 13:58:36  adam
121  * New retrieve/extract method tellf (added).
122  * Added O_BINARY for open calls.
123  *
124  * Revision 1.46  1997/07/28 08:30:47  adam
125  * Server returns diagnostic 14 when record doesn't exist.
126  *
127  * Revision 1.45  1996/12/23 15:30:45  adam
128  * Work on truncation.
129  * Bug fix: result sets weren't deleted after server shut down.
130  *
131  * Revision 1.44  1996/12/11 12:08:01  adam
132  * Added better compression.
133  *
134  * Revision 1.43  1996/11/15 15:03:58  adam
135  * Logging of execution speed by using the times(2) call.
136  *
137  * Revision 1.42  1996/11/08  11:10:36  adam
138  * Buffers used during file match got bigger.
139  * Compressed ISAM support everywhere.
140  * Bug fixes regarding masking characters in queries.
141  * Redesigned Regexp-2 queries.
142  *
143  * Revision 1.41  1996/10/29 14:09:56  adam
144  * Use of cisam system - enabled if setting isamc is 1.
145  *
146  * Revision 1.40  1996/06/04 10:19:02  adam
147  * Minor changes - removed include of ctype.h.
148  *
149  * Revision 1.39  1996/05/31  09:07:05  quinn
150  * Work on character-set handling
151  *
152  * Revision 1.38  1996/05/14  11:34:01  adam
153  * Scan support in multiple registers/databases.
154  *
155  * Revision 1.37  1996/05/14  06:16:48  adam
156  * Compact use/set bytes used in search service.
157  *
158  * Revision 1.36  1996/05/01 13:46:37  adam
159  * First work on multiple records in one file.
160  * New option, -offset, to the "unread" command in the filter module.
161  *
162  * Revision 1.35  1996/03/26  16:01:14  adam
163  * New setting lockPath: directory of various lock files.
164  *
165  * Revision 1.34  1996/03/20  09:36:46  adam
166  * Function dict_lookup_grep got extra parameter, init_pos, which marks
167  * from which position in pattern approximate pattern matching should occur.
168  * Approximate pattern matching is used in relevance=re-2.
169  *
170  * Revision 1.33  1996/01/17  14:57:56  adam
171  * Prototype changed for reader functions in extract/retrieve. File
172  *  is identified by 'void *' instead of 'int.
173  *
174  * Revision 1.32  1995/12/11  09:12:58  adam
175  * The rec_get function returns NULL if record doesn't exist - will
176  * happen in the server if the result set records have been deleted since
177  * the creation of the set (i.e. the search).
178  * The server saves a result temporarily if it is 'volatile', i.e. the
179  * set is register dependent.
180  *
181  * Revision 1.31  1995/12/08  16:22:56  adam
182  * Work on update while servers are running. Three lock files introduced.
183  * The servers reload their registers when necessary, but they don't
184  * reestablish result sets yet.
185  *
186  * Revision 1.30  1995/12/07  17:38:48  adam
187  * Work locking mechanisms for concurrent updates/commit.
188  *
189  * Revision 1.29  1995/12/04  14:22:32  adam
190  * Extra arg to recType_byName.
191  * Started work on new regular expression parsed input to
192  * structured records.
193  *
194  * Revision 1.28  1995/11/28  09:09:48  adam
195  * Zebra config renamed.
196  * Use setting 'recordId' to identify record now.
197  * Bug fix in recindex.c: rec_release_blocks was invokeded even
198  * though the blocks were already released.
199  * File traversal properly deletes records when needed.
200  *
201  * Revision 1.27  1995/11/27  13:58:54  adam
202  * New option -t. storeStore data implemented in server.
203  *
204  * Revision 1.26  1995/11/25  10:24:07  adam
205  * More record fields - they are enumerated now.
206  * New options: flagStoreData flagStoreKey.
207  *
208  * Revision 1.25  1995/11/21  15:29:13  adam
209  * Config file 'base' read by default by both indexer and server.
210  *
211  * Revision 1.24  1995/11/20  16:59:47  adam
212  * New update method: the 'old' keys are saved for each records.
213  *
214  * Revision 1.23  1995/11/16  17:00:56  adam
215  * Better logging of rpn query.
216  *
217  * Revision 1.22  1995/11/16  15:34:55  adam
218  * Uses new record management system in both indexer and server.
219  *
220  * Revision 1.21  1995/11/01  16:25:52  quinn
221  * *** empty log message ***
222  *
223  * Revision 1.20  1995/10/27  14:00:12  adam
224  * Implemented detection of database availability.
225  *
226  * Revision 1.19  1995/10/17  18:02:11  adam
227  * New feature: databases. Implemented as prefix to words in dictionary.
228  *
229  * Revision 1.18  1995/10/16  14:03:09  quinn
230  * Changes to support element set names and espec1
231  *
232  * Revision 1.17  1995/10/16  09:32:40  adam
233  * More work on relational op.
234  *
235  * Revision 1.16  1995/10/13  12:26:44  adam
236  * Optimization of truncation.
237  *
238  * Revision 1.15  1995/10/12  12:40:55  adam
239  * Bug fixes in rpn_prox.
240  *
241  * Revision 1.14  1995/10/09  16:18:37  adam
242  * Function dict_lookup_grep got extra client data parameter.
243  *
244  * Revision 1.13  1995/10/06  14:38:00  adam
245  * New result set method: r_score.
246  * Local no (sysno) and score is transferred to retrieveCtrl.
247  *
248  * Revision 1.12  1995/10/06  13:52:06  adam
249  * Bug fixes. Handler may abort further scanning.
250  *
251  * Revision 1.11  1995/10/06  10:43:57  adam
252  * Scan added. 'occurrences' in scan entries not set yet.
253  *
254  * Revision 1.10  1995/10/02  16:43:32  quinn
255  * Set default resulting record type in fetch.
256  *
257  * Revision 1.9  1995/10/02  15:18:52  adam
258  * New member in recRetrieveCtrl: diagnostic.
259  *
260  * Revision 1.8  1995/09/28  09:19:47  adam
261  * xfree/xmalloc used everywhere.
262  * Extract/retrieve method seems to work for text records.
263  *
264  * Revision 1.7  1995/09/27  16:17:32  adam
265  * More work on retrieve.
266  *
267  * Revision 1.6  1995/09/08  08:53:22  adam
268  * Record buffer maintained in server_info.
269  *
270  * Revision 1.5  1995/09/06  16:11:18  adam
271  * Option: only one word key per file.
272  *
273  * Revision 1.4  1995/09/06  10:33:04  adam
274  * More work on present. Some log messages removed.
275  *
276  * Revision 1.3  1995/09/05  15:28:40  adam
277  * More work on search engine.
278  *
279  * Revision 1.2  1995/09/04  12:33:43  adam
280  * Various cleanup. YAZ util used instead.
281  *
282  * Revision 1.1  1995/09/04  09:10:41  adam
283  * More work on index add/del/update.
284  * Merge sort implemented.
285  * Initial work on z39 server.
286  *
287  */
288
289 #include <stdio.h>
290 #include <assert.h>
291 #include <fcntl.h>
292 #ifdef WIN32
293 #include <io.h>
294 #include <process.h>
295 #else
296 #include <unistd.h>
297 #endif
298
299 #include <yaz/data1.h>
300 #ifdef ASN_COMPILED
301 #include <yaz/ill.h>
302 #endif
303
304 #include "zserver.h"
305
306 #ifndef ZEBRASDR
307 #define ZEBRASDR 0
308 #endif
309 #if ZEBRASDR
310 #include "zebrasdr.h"
311 #endif
312
313 static int bend_sort (void *handle, bend_sort_rr *rr);
314 static int bend_delete (void *handle, bend_delete_rr *rr);
315 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
316 static int bend_segment (void *handle, bend_segment_rr *rr);
317 static int bend_search (void *handle, bend_search_rr *r);
318 static int bend_fetch (void *handle, bend_fetch_rr *r);
319 static int bend_scan (void *handle, bend_scan_rr *r);
320
321 bend_initresult *bend_init (bend_initrequest *q)
322 {
323     bend_initresult *r = (bend_initresult *)
324         odr_malloc (q->stream, sizeof(*r));
325     ZebraHandle zh;
326     struct statserv_options_block *sob;
327     char *user = NULL;
328     char *passwd = NULL;
329
330     r->errcode = 0;
331     r->errstring = 0;
332     q->bend_sort = bend_sort;
333     q->bend_delete = bend_delete;
334     q->bend_esrequest = bend_esrequest;
335     q->bend_segment = bend_segment;
336     q->bend_search = bend_search;
337     q->bend_fetch = bend_fetch;
338     q->bend_scan = bend_scan;
339
340 #if ZMBOL
341     q->implementation_name = "Z'mbol Information Server";
342     q->implementation_version = "Z'mbol " ZEBRAVER;
343 #else
344     q->implementation_name = "Zebra Information Server";
345     q->implementation_version = "Zebra " ZEBRAVER;
346 #endif
347
348     logf (LOG_DEBUG, "bend_init");
349
350     sob = statserv_getcontrol ();
351     if (!(zh = zebra_open (sob->handle)))
352     {
353         logf (LOG_FATAL, "Failed to open Zebra `%s'", sob->configname);
354         r->errcode = 1;
355         return r;
356     }
357     if (q->auth)
358     {
359         if (q->auth->which == Z_IdAuthentication_open)
360         {
361             char *openpass = xstrdup (q->auth->u.open);
362             char *cp = strchr (openpass, '/');
363             if (cp)
364             {
365                 *cp = '\0';
366                 user = nmem_strdup (odr_getmem (q->stream), openpass);
367                 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
368             }
369             xfree (openpass);
370         }
371     }
372     if (zebra_auth (zh->service, user, passwd))
373     {
374         r->errcode = 222;
375         r->errstring = user;
376         zebra_close (zh);
377         return r;
378     }
379     r->handle = zh;
380     return r;
381 }
382
383 int bend_search (void *handle, bend_search_rr *r)
384 {
385     ZebraHandle zh = (ZebraHandle) handle;
386
387     r->hits = 0;
388     r->errcode = 0;
389     r->errstring = NULL;
390     
391     logf (LOG_LOG, "ResultSet '%s'", r->setname);
392     switch (r->query->which)
393     {
394     case Z_Query_type_1: case Z_Query_type_101:
395         zebra_search_rpn (zh, r->decode, r->stream, r->query->u.type_1,
396                           r->num_bases, r->basenames, r->setname);
397         r->errcode = zh->errCode;
398         r->errstring = zh->errString;
399         r->hits = zh->hits;
400         break;
401     case Z_Query_type_2:
402         r->errcode = 107;
403         r->errstring = "type-2";
404         break;
405     default:
406         r->errcode = 107;
407     }
408     return 0;
409 }
410
411 int bend_fetch (void *handle, bend_fetch_rr *r)
412 {
413     ZebraHandle zh = (ZebraHandle) handle;
414     ZebraRetrievalRecord retrievalRecord;
415
416     retrievalRecord.position = r->number;
417     
418     r->last_in_set = 0;
419     zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
420                             r->request_format, 1, &retrievalRecord);
421     if (zh->errCode)                  /* non Surrogate Diagnostic */
422     {
423         r->errcode = zh->errCode;
424         r->errstring = zh->errString;
425     }
426     else if (retrievalRecord.errCode) /* Surrogate Diagnostic */
427     {
428         r->surrogate_flag = 1;
429         r->errcode = retrievalRecord.errCode;
430         r->errstring = retrievalRecord.errString;
431         r->basename = retrievalRecord.base;
432     }
433     else                              /* Database Record */
434     {
435         r->errcode = 0;
436         r->basename = retrievalRecord.base;
437         r->record = retrievalRecord.buf;
438         r->len = retrievalRecord.len;
439         r->output_format = retrievalRecord.format;
440     }
441     return 0;
442 }
443
444 static int bend_scan (void *handle, bend_scan_rr *r)
445 {
446     ZebraScanEntry *entries;
447     ZebraHandle zh = (ZebraHandle) handle;
448     int is_partial, i;
449     
450     r->entries = (struct scan_entry *)
451         odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
452     zebra_scan (zh, r->stream, r->term,
453                 r->attributeset,
454                 r->num_bases, r->basenames,
455                 &r->term_position,
456                 &r->num_entries, &entries, &is_partial);
457     if (is_partial)
458         r->status = BEND_SCAN_PARTIAL;
459     else
460         r->status = BEND_SCAN_SUCCESS;
461     for (i = 0; i < r->num_entries; i++)
462     {
463         r->entries[i].term = entries[i].term;
464         r->entries[i].occurrences = entries[i].occurrences;
465     }
466     r->errcode = zh->errCode;
467     r->errstring = zh->errString;
468     return 0;
469 }
470
471 void bend_close (void *handle)
472 {
473     zebra_close ((ZebraHandle) handle);
474     xmalloc_trav("bend_close");
475     nmem_print_list();
476 }
477
478 int bend_sort (void *handle, bend_sort_rr *rr)
479 {
480     ZebraHandle zh = (ZebraHandle) handle;
481
482     zebra_sort (zh, rr->stream,
483                 rr->num_input_setnames, (const char **) rr->input_setnames,
484                 rr->output_setname, rr->sort_sequence, &rr->sort_status);
485     rr->errcode = zh->errCode;
486     rr->errstring = zh->errString;
487     return 0;
488 }
489
490 int bend_delete (void *handle, bend_delete_rr *rr)
491 {
492     ZebraHandle zh = (ZebraHandle) handle;
493
494     rr->delete_status = zebra_deleleResultSet(zh, rr->function,
495                                               rr->num_setnames, rr->setnames,
496                                               rr->statuses);
497     return 0;
498 }
499
500 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
501 {
502     switch (r->toKeep->which)
503     {
504     case Z_ESAdminOriginPartToKeep_reIndex:
505         yaz_log(LOG_LOG, "adm-reindex");
506         break;
507     case Z_ESAdminOriginPartToKeep_truncate:
508         yaz_log(LOG_LOG, "adm-truncate");
509         break;
510     case Z_ESAdminOriginPartToKeep_drop:
511         yaz_log(LOG_LOG, "adm-drop");
512         break;
513     case Z_ESAdminOriginPartToKeep_create:
514         yaz_log(LOG_LOG, "adm-create");
515         zebra_admin_create (zh, r->toKeep->databaseName);
516         break;
517     case Z_ESAdminOriginPartToKeep_import:
518         yaz_log(LOG_LOG, "adm-import");
519         zebra_admin_import_begin (zh, r->toKeep->databaseName);
520         break;
521     case Z_ESAdminOriginPartToKeep_refresh:
522         yaz_log(LOG_LOG, "adm-refresh");
523         break;
524     case Z_ESAdminOriginPartToKeep_commit:
525         yaz_log(LOG_LOG, "adm-commit");
526         break;
527     case Z_ESAdminOriginPartToKeep_shutdown:
528         yaz_log(LOG_LOG, "shutdown");
529         zebra_admin_shutdown(zh);
530         break;
531     case Z_ESAdminOriginPartToKeep_start:
532         yaz_log(LOG_LOG, "start");
533         zebra_admin_start(zh);
534         break;
535     default:
536         yaz_log(LOG_LOG, "unknown admin");
537         zh->errCode = 1001;
538     }
539     if (r->toKeep->databaseName)
540     {
541         yaz_log(LOG_LOG, "database %s", r->toKeep->databaseName);
542     }
543     return 0;
544 }
545
546 static int es_admin (ZebraHandle zh, Z_Admin *r)
547 {
548     switch (r->which)
549     {
550     case Z_Admin_esRequest:
551         es_admin_request (zh, r->u.esRequest);
552         break;
553     case Z_Admin_taskPackage:
554         yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
555         break;
556     default:
557         zh->errCode = 1001;
558         break;
559     }
560
561     return 0;
562 }
563
564 int bend_segment (void *handle, bend_segment_rr *rr)
565 {
566     ZebraHandle zh = (ZebraHandle) handle;
567     Z_Segment *segment = rr->segment;
568
569     if (segment->num_segmentRecords)
570         zebra_admin_import_segment (zh, rr->segment);
571     else
572         zebra_admin_import_end (zh);
573     return 0;
574 }
575
576 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
577 {
578     ZebraHandle zh = (ZebraHandle) handle;
579     
580     yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
581     if (rr->esr->packageName)
582         yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
583     yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
584
585     if (!rr->esr->taskSpecificParameters)
586     {
587         yaz_log (LOG_WARN, "No task specific parameters");
588     }
589     else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
590     {
591         es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
592         rr->errcode = zh->errCode;
593         rr->errstring = zh->errString;
594     }
595     else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
596     {
597         Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
598         yaz_log (LOG_LOG, "Received ItemOrder");
599         switch (it->which)
600         {
601 #ifdef ASN_COMPILED
602         case Z_IOItemOrder_esRequest:
603 #else
604         case Z_ItemOrder_esRequest:
605 #endif
606         {
607             Z_IORequest *ir = it->u.esRequest;
608             Z_IOOriginPartToKeep *k = ir->toKeep;
609             Z_IOOriginPartNotToKeep *n = ir->notToKeep;
610             
611             if (k && k->contact)
612             {
613                 if (k->contact->name)
614                     yaz_log(LOG_LOG, "contact name %s", k->contact->name);
615                 if (k->contact->phone)
616                     yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
617                 if (k->contact->email)
618                     yaz_log(LOG_LOG, "contact email %s", k->contact->email);
619             }
620             if (k->addlBilling)
621             {
622                 yaz_log(LOG_LOG, "Billing info (not shown)");
623             }
624             
625             if (n->resultSetItem)
626             {
627                 yaz_log(LOG_LOG, "resultsetItem");
628                 yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
629                 yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
630             }
631 #ifdef ASN_COMPILED
632             if (n->itemRequest)
633             {
634                 Z_External *r = (Z_External*) n->itemRequest;
635                 ILL_ItemRequest *item_req = 0;
636                 ILL_Request *ill_req = 0;
637                 if (r->direct_reference)
638                 {
639                     oident *ent = oid_getentbyoid(r->direct_reference);
640                     if (ent)
641                         yaz_log(LOG_LOG, "OID %s", ent->desc);
642                     if (ent && ent->value == VAL_ISO_ILL_1)
643                     {
644                         yaz_log (LOG_LOG, "ItemRequest");
645                         if (r->which == ODR_EXTERNAL_single)
646                         {
647                             odr_setbuf(rr->decode,
648                                        r->u.single_ASN1_type->buf,
649                                        r->u.single_ASN1_type->len, 0);
650                             
651                             if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
652                             {
653                                 yaz_log (LOG_LOG,
654                                     "Couldn't decode ItemRequest %s near %d",
655                                        odr_errmsg(odr_geterror(rr->decode)),
656                                        odr_offset(rr->decode));
657                                 yaz_log(LOG_LOG, "PDU dump:");
658                                 odr_dumpBER(yaz_log_file(),
659                                      r->u.single_ASN1_type->buf,
660                                      r->u.single_ASN1_type->len);
661                             }
662                             if (rr->print)
663                             {
664                                 ill_ItemRequest (rr->print, &item_req, 0,
665                                     "ItemRequest");
666                                 odr_reset (rr->print);
667                             }
668                         }
669                         if (!item_req && r->which == ODR_EXTERNAL_single)
670                         {
671                             yaz_log (LOG_LOG, "ILLRequest");
672                             odr_setbuf(rr->decode,
673                                        r->u.single_ASN1_type->buf,
674                                        r->u.single_ASN1_type->len, 0);
675                             
676                             if (!ill_Request (rr->decode, &ill_req, 0, 0))
677                             {
678                                 yaz_log (LOG_LOG,
679                                     "Couldn't decode ILLRequest %s near %d",
680                                        odr_errmsg(odr_geterror(rr->decode)),
681                                        odr_offset(rr->decode));
682                                 yaz_log(LOG_LOG, "PDU dump:");
683                                 odr_dumpBER(yaz_log_file(),
684                                      r->u.single_ASN1_type->buf,
685                                      r->u.single_ASN1_type->len);
686                             }
687                             if (rr->print)
688                             {
689                                 ill_Request (rr->print, &ill_req, 0,
690                                     "ILLRequest");
691                                 odr_reset (rr->print);
692                             }
693                         }
694                     }
695                 }
696                 if (item_req)
697                 {
698                     yaz_log (LOG_LOG, "ILL protocol version = %d",
699                              *item_req->protocol_version_num);
700                 }
701             }
702 #endif
703         }
704         break;
705         }
706     }
707     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
708     {
709         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
710         yaz_log (LOG_LOG, "Received DB Update");
711         if (up->which == Z_IUUpdate_esRequest)
712         {
713             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
714             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
715             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
716             
717             yaz_log (LOG_LOG, "action");
718             if (toKeep->action)
719             {
720                 switch (*toKeep->action)
721                 {
722                 case Z_IUOriginPartToKeep_recordInsert:
723                     yaz_log (LOG_LOG, "recordInsert");
724                     break;
725                 case Z_IUOriginPartToKeep_recordReplace:
726                     yaz_log (LOG_LOG, "recordUpdate");
727                     break;
728                 case Z_IUOriginPartToKeep_recordDelete:
729                     yaz_log (LOG_LOG, "recordDelete");
730                     break;
731                 case Z_IUOriginPartToKeep_elementUpdate:
732                     yaz_log (LOG_LOG, "elementUpdate");
733                     break;
734                 case Z_IUOriginPartToKeep_specialUpdate:
735                     yaz_log (LOG_LOG, "specialUpdate");
736                     break;
737                 case Z_ESAdminOriginPartToKeep_shutdown:
738                     yaz_log (LOG_LOG, "shutDown");
739                     break;
740                 case Z_ESAdminOriginPartToKeep_start:
741                     yaz_log (LOG_LOG, "start");
742                     break;
743                 default:
744                     yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
745                 }
746             }
747             if (toKeep->databaseName)
748             {
749                 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
750                 if (!strcmp(toKeep->databaseName, "fault"))
751                 {
752                     rr->errcode = 109;
753                     rr->errstring = toKeep->databaseName;
754                 }
755                 if (!strcmp(toKeep->databaseName, "accept"))
756                     rr->errcode = -1;
757             }
758             if (notToKeep)
759             {
760                 int i;
761                 for (i = 0; i < notToKeep->num; i++)
762                 {
763                     Z_External *rec = notToKeep->elements[i]->record;
764
765                     if (rec->direct_reference)
766                     {
767                         struct oident *oident;
768                         oident = oid_getentbyoid(rec->direct_reference);
769                         if (oident)
770                             yaz_log (LOG_LOG, "record %d type %s", i,
771                                      oident->desc);
772                     }
773                     switch (rec->which)
774                     {
775                     case Z_External_sutrs:
776                         if (rec->u.octet_aligned->len > 170)
777                             yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
778                                      rec->u.sutrs->len,
779                                      rec->u.sutrs->buf);
780                         else
781                             yaz_log (LOG_LOG, "%d bytes:\n%s",
782                                      rec->u.sutrs->len,
783                                      rec->u.sutrs->buf);
784                         break;
785                     case Z_External_octet:
786                         if (rec->u.octet_aligned->len > 170)
787                             yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
788                                      rec->u.octet_aligned->len,
789                                      rec->u.octet_aligned->buf);
790                         else
791                             yaz_log (LOG_LOG, "%d bytes\n%s",
792                                      rec->u.octet_aligned->len,
793                                      rec->u.octet_aligned->buf);
794                     }
795                 }
796             }
797         }
798     }
799     else
800     {
801         yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
802                  rr->esr->taskSpecificParameters->which);
803         
804     }
805     return 0;
806 }
807
808 static void bend_start (struct statserv_options_block *sob)
809 {
810 #ifdef WIN32
811     
812 #else
813     if (!sob->inetd) 
814     {
815         char *pidfile = "zebrasrv.pid";
816         int fd = creat (pidfile, 0666);
817         if (fd == -1)
818             logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
819         else
820         {
821             char pidstr[30];
822         
823             sprintf (pidstr, "%ld", (long) getpid ());
824             write (fd, pidstr, strlen(pidstr));
825             close (fd);
826         }
827     }
828 #endif
829     if (sob->handle)
830         zebra_stop((ZebraService) sob->handle);
831     sob->handle = zebra_start(sob->configname);
832 }
833
834 static void bend_stop(struct statserv_options_block *sob)
835 {
836     if (sob->handle)
837     {
838         ZebraService service = sob->handle;
839         zebra_stop(service);
840     }
841 }
842
843 int main (int argc, char **argv)
844 {
845     struct statserv_options_block *sob;
846
847     sob = statserv_getcontrol ();
848     strcpy (sob->configname, FNAME_CONFIG);
849     sob->bend_start = bend_start;
850     sob->bend_stop = bend_stop;
851
852     statserv_setcontrol (sob);
853
854     return statserv_main (argc, argv, bend_init, bend_close);
855 }