3077ed1e72e18e6db6b024c3026ab599ee41b360
[idzebra-moved-to-github.git] / index / zserver.c
1 /* $Id: zserver.c,v 1.126 2004-12-21 22:00:57 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <fcntl.h>
26 #ifdef WIN32
27 #include <io.h>
28 #include <process.h>
29 #include <sys/locking.h>
30 #else
31 #include <unistd.h>
32 #endif
33
34 #include <errno.h>
35 #include <yaz/log.h>
36 #include <yaz/ill.h>
37 #include <yaz/yaz-util.h>
38
39 #include <sys/types.h>
40
41 #include "zserver.h"
42
43 static int bend_sort (void *handle, bend_sort_rr *rr);
44 static int bend_delete (void *handle, bend_delete_rr *rr);
45 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
46 static int bend_segment (void *handle, bend_segment_rr *rr);
47 static int bend_search (void *handle, bend_search_rr *r);
48 static int bend_fetch (void *handle, bend_fetch_rr *r);
49 static int bend_scan (void *handle, bend_scan_rr *r);
50
51 bend_initresult *bend_init (bend_initrequest *q)
52 {
53     bend_initresult *r = (bend_initresult *)
54         odr_malloc (q->stream, sizeof(*r));
55     ZebraHandle zh;
56     struct statserv_options_block *sob;
57     char *user = NULL;
58     char *passwd = NULL;
59
60     r->errcode = 0;
61     r->errstring = 0;
62     q->bend_sort = bend_sort;
63     q->bend_delete = bend_delete;
64     q->bend_esrequest = bend_esrequest;
65     q->bend_segment = bend_segment;
66     q->bend_search = bend_search;
67     q->bend_fetch = bend_fetch;
68     q->bend_scan = bend_scan;
69
70     q->implementation_name = "Zebra Information Server";
71     q->implementation_version = "Zebra " ZEBRAVER;
72
73     yaz_log (YLOG_DEBUG, "bend_init");
74
75     sob = statserv_getcontrol ();
76     if (!(zh = zebra_open (sob->handle)))
77     {
78         yaz_log (YLOG_WARN, "Failed to read config `%s'", sob->configname);
79         r->errcode = 1;
80         return r;
81     }
82     r->handle = zh;
83     if (q->auth)
84     {
85         if (q->auth->which == Z_IdAuthentication_open)
86         {
87             char *openpass = xstrdup (q->auth->u.open);
88             char *cp = strchr (openpass, '/');
89             if (cp)
90             {
91                 *cp = '\0';
92                 user = nmem_strdup (odr_getmem (q->stream), openpass);
93                 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
94             }
95             xfree (openpass);
96         }
97         else if (q->auth->which == Z_IdAuthentication_idPass)
98         {
99             Z_IdPass *idPass = q->auth->u.idPass;
100
101             user = idPass->userId;
102             passwd = idPass->password;
103         }
104     }
105     if (zebra_auth (zh, user, passwd))
106     {
107         r->errcode = 222;
108         r->errstring = user;
109         return r;
110     }
111     if (q->charneg_request) /* characater set and langauge negotiation? */
112     {
113         char **charsets = 0;
114         int num_charsets;
115         char **langs = 0;
116         int num_langs = 0;
117         int selected = 0;
118         int i;
119         NMEM nmem = nmem_create();
120
121         yaz_log (YLOG_LOG, "character set and language negotiation");
122
123         yaz_get_proposal_charneg (nmem, q->charneg_request,
124                                   &charsets, &num_charsets,
125                                   &langs, &num_langs, &selected);
126         
127         for (i = 0; i < num_charsets; i++)
128         {
129             const char *right_name = "";
130             /*
131              * FIXME! It is like rudiment :-))
132              * We have to support this short names of character sets,
133              * because a lot servers in Russia to use own in during
134              * character set and language negotiation still.
135              */
136             
137             if (!yaz_matchstr(charsets[i], "win")) {
138                 right_name = "WINDOWS-1251";
139             } else if (!yaz_matchstr(charsets[i], "koi")) {
140                 right_name = "KOI8-R";
141             } else if (!yaz_matchstr(charsets[i], "iso")) {
142                 right_name = "ISO-8859-5";
143             } else if (!yaz_matchstr(charsets[i], "dos")) {
144                 right_name = "CP866";
145             } else if (!yaz_matchstr(charsets[i], "uni")) {
146                 right_name = "UTF-8";
147             } else {
148                 right_name = charsets[i];
149             }
150             if (odr_set_charset (q->decode, "UTF-8", right_name) == 0)
151             {
152                 yaz_log (YLOG_LOG, "charset %d %s (proper name %s): OK", i,
153                          charsets[i], right_name);
154                 odr_set_charset (q->stream, right_name, "UTF-8");
155                 if (selected)
156                     zebra_record_encoding(zh, right_name);
157                 zebra_octet_term_encoding(zh, right_name);
158                 q->charneg_response =
159                     yaz_set_response_charneg (q->stream, charsets[i],
160                                               0, selected);
161                 break;
162             } else {
163                 yaz_log (YLOG_LOG, "charset %d %s (proper name %s): unsupported", i,
164                          charsets[i], right_name);
165             }
166         }
167         nmem_destroy(nmem);
168     }
169     return r;
170 }
171
172 static void search_terms (ZebraHandle zh, bend_search_rr *r)
173 {
174     zint count = 0;
175     int no_terms;
176     int i;
177     int type = Z_Term_general;
178     struct Z_External *ext;
179     Z_SearchInfoReport *sr;
180
181     /* get no of terms for result set */
182     no_terms = 0; /* zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0); */
183       /* FIXME - Rsets don't know number of terms no more ??? */
184     if (!no_terms)
185         return;
186
187     r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
188
189     r->search_info->num_elements = 1;
190     r->search_info->list =
191         odr_malloc (r->stream, sizeof(*r->search_info->list));
192     r->search_info->list[0] =
193         odr_malloc (r->stream, sizeof(**r->search_info->list));
194     r->search_info->list[0]->category = 0;
195     r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
196     ext = odr_malloc (r->stream, sizeof(*ext));
197     r->search_info->list[0]->information.externallyDefinedInfo = ext;
198     ext->direct_reference =
199         yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
200     ext->indirect_reference = 0;
201     ext->descriptor = 0;
202     ext->which = Z_External_searchResult1;
203     sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
204     ext->u.searchResult1 = sr;
205     sr->num = no_terms;
206     sr->elements = odr_malloc (r->stream, sr->num *
207                                sizeof(*sr->elements));
208     for (i = 0; i<no_terms; i++)
209     {
210         Z_Term *term;
211         char outbuf[1024];
212         size_t len = sizeof(outbuf);
213         /* FIXME - Can we just skip this ??? */
214         /*
215         zebra_resultSetTerms (zh, r->setname, i,
216                               &count, &type, outbuf, &len);
217         */
218         sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
219         sr->elements[i]->subqueryId = 0;
220         sr->elements[i]->fullQuery = odr_malloc (r->stream, 
221                                                  sizeof(bool_t));
222         *sr->elements[i]->fullQuery = 0;
223         sr->elements[i]->subqueryExpression = 
224             odr_malloc (r->stream, sizeof(Z_QueryExpression));
225         sr->elements[i]->subqueryExpression->which = 
226             Z_QueryExpression_term;
227         sr->elements[i]->subqueryExpression->u.term =
228             odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
229         term = odr_malloc (r->stream, sizeof(Z_Term));
230         sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
231         switch (type)
232         {
233         case Z_Term_characterString:
234             yaz_log (YLOG_DEBUG, "term as characterString");
235             term->which = Z_Term_characterString;
236             term->u.characterString = odr_strdup (r->stream, outbuf);
237             break;
238         case Z_Term_general:
239             yaz_log (YLOG_DEBUG, "term as general");
240             term->which = Z_Term_general;
241             term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
242             term->u.general->size = term->u.general->len = len;
243             term->u.general->buf = odr_malloc (r->stream, len);
244             memcpy (term->u.general->buf, outbuf, len);
245             break;
246         default:
247             term->which = Z_Term_general;
248             term->u.null = odr_nullval();
249         }
250         sr->elements[i]->subqueryExpression->u.term->termComment = 0;
251         sr->elements[i]->subqueryInterpretation = 0;
252         sr->elements[i]->subqueryRecommendation = 0;
253         if (count > 2000000000)
254             count = 2000000000;
255         sr->elements[i]->subqueryCount = odr_intdup (r->stream, (int) count);
256         sr->elements[i]->subqueryWeight = 0;
257         sr->elements[i]->resultsByDB = 0;
258     }
259 }
260
261 int bend_search (void *handle, bend_search_rr *r)
262 {
263     ZebraHandle zh = (ZebraHandle) handle;
264
265     r->hits = 0;
266     r->errcode = 0;
267     r->errstring = NULL;
268     
269     if (zebra_select_databases (zh, r->num_bases,
270                                 (const char **) r->basenames))
271     {
272         zebra_result (zh, &r->errcode, &r->errstring);
273         return 0;
274     }
275     yaz_log (YLOG_DEBUG, "ResultSet '%s'", r->setname);
276     switch (r->query->which)
277     {
278     case Z_Query_type_1: case Z_Query_type_101:
279         zebra_search_RPN (zh, r->stream, r->query->u.type_1,
280                           r->setname, &r->hits);
281         zebra_result (zh, &r->errcode, &r->errstring);
282         if (!r->errcode)
283             search_terms (zh, r);
284         break;
285     case Z_Query_type_2:
286         r->errcode = 107;
287         r->errstring = "type-2";
288         break;
289     default:
290         r->errcode = 107;
291     }
292     return 0;
293 }
294
295
296 int bend_fetch (void *handle, bend_fetch_rr *r)
297 {
298     ZebraHandle zh = (ZebraHandle) handle;
299     ZebraRetrievalRecord retrievalRecord;
300
301     retrievalRecord.position = r->number;
302     
303     r->last_in_set = 0;
304     zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
305                             r->request_format, 1, &retrievalRecord);
306     zebra_result (zh, &r->errcode, &r->errstring);
307     /*  non Surrogate Diagnostic OR Surrogate Diagnostic */
308     if (r->errcode == 0 && retrievalRecord.errCode)
309     {
310         r->surrogate_flag = 1;
311         r->errcode = retrievalRecord.errCode;
312         r->errstring = retrievalRecord.errString;
313         r->basename = retrievalRecord.base;
314     }
315     else if (r->errcode == 0)        /* Database Record */
316     {
317         r->errcode = 0;
318         r->basename = retrievalRecord.base;
319         r->record = retrievalRecord.buf;
320         r->len = retrievalRecord.len;
321         r->output_format = retrievalRecord.format;
322     }
323     return 0;
324 }
325
326 static int bend_scan (void *handle, bend_scan_rr *r)
327 {
328     ZebraScanEntry *entries;
329     ZebraHandle zh = (ZebraHandle) handle;
330     int is_partial, i;
331
332     if (zebra_select_databases (zh, r->num_bases, 
333                                 (const char **) r->basenames))
334     {
335         zebra_result (zh, &r->errcode, &r->errstring);
336         return 0;
337     }
338     if (r->step_size != 0 && *r->step_size != 0) {
339         r->errcode = 205; /* "Only zero step size supported for Scan" */
340         r->errstring = 0;
341         return 0;
342     }
343     r->entries = (struct scan_entry *)
344         odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
345     zebra_scan (zh, r->stream, r->term,
346                 r->attributeset,
347                 &r->term_position,
348                 &r->num_entries, &entries, &is_partial);
349     if (is_partial)
350         r->status = BEND_SCAN_PARTIAL;
351     else
352         r->status = BEND_SCAN_SUCCESS;
353     for (i = 0; i < r->num_entries; i++)
354     {
355         r->entries[i].term = entries[i].term;
356         r->entries[i].occurrences = entries[i].occurrences;
357     }
358     zebra_result (zh, &r->errcode, &r->errstring);
359     return 0;
360 }
361
362 void bend_close (void *handle)
363 {
364     zebra_close ((ZebraHandle) handle);
365     xmalloc_trav("bend_close");
366     nmem_print_list();
367 }
368
369 int bend_sort (void *handle, bend_sort_rr *rr)
370 {
371     ZebraHandle zh = (ZebraHandle) handle;
372
373     zebra_sort (zh, rr->stream,
374                 rr->num_input_setnames, (const char **) rr->input_setnames,
375                 rr->output_setname, rr->sort_sequence, &rr->sort_status);
376     zebra_result (zh, &rr->errcode, &rr->errstring);
377     return 0;
378 }
379
380 int bend_delete (void *handle, bend_delete_rr *rr)
381 {
382     ZebraHandle zh = (ZebraHandle) handle;
383
384     rr->delete_status = zebra_deleleResultSet(zh, rr->function,
385                                               rr->num_setnames, rr->setnames,
386                                               rr->statuses);
387     return 0;
388 }
389
390 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
391 {
392     if (r->toKeep->databaseName)
393     {
394         yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
395     }
396     switch (r->toKeep->which)
397     {
398     case Z_ESAdminOriginPartToKeep_reIndex:
399         yaz_log(YLOG_LOG, "adm-reindex");
400         break;
401     case Z_ESAdminOriginPartToKeep_truncate:
402         yaz_log(YLOG_LOG, "adm-truncate");
403         break;
404     case Z_ESAdminOriginPartToKeep_drop:
405         yaz_log(YLOG_LOG, "adm-drop");
406         zebra_drop_database (zh, r->toKeep->databaseName);
407         break;
408     case Z_ESAdminOriginPartToKeep_create:
409         yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
410         zebra_create_database (zh, r->toKeep->databaseName);
411         break;
412     case Z_ESAdminOriginPartToKeep_import:
413         yaz_log(YLOG_LOG, "adm-import");
414         zebra_admin_import_begin (zh, r->toKeep->databaseName,
415                         r->toKeep->u.import->recordType);
416         break;
417     case Z_ESAdminOriginPartToKeep_refresh:
418         yaz_log(YLOG_LOG, "adm-refresh");
419         break;
420     case Z_ESAdminOriginPartToKeep_commit:
421         yaz_log(YLOG_LOG, "adm-commit");
422         if (r->toKeep->databaseName)
423             zebra_select_database(zh, r->toKeep->databaseName);
424         zebra_commit(zh);
425         break;
426     case Z_ESAdminOriginPartToKeep_shutdown:
427         yaz_log(YLOG_LOG, "shutdown");
428         zebra_admin_shutdown(zh);
429         break;
430     case Z_ESAdminOriginPartToKeep_start:
431         yaz_log(YLOG_LOG, "start");
432         zebra_admin_start(zh);
433         break;
434     default:
435         yaz_log(YLOG_LOG, "unknown admin");
436     }
437     return 0;
438 }
439
440 static int es_admin (ZebraHandle zh, Z_Admin *r)
441 {
442     switch (r->which)
443     {
444     case Z_Admin_esRequest:
445         es_admin_request (zh, r->u.esRequest);
446         break;
447     case Z_Admin_taskPackage:
448         yaz_log (YLOG_LOG, "adm taskpackage (unhandled)");
449         break;
450     default:
451         break;
452     }
453
454     return 0;
455 }
456
457 int bend_segment (void *handle, bend_segment_rr *rr)
458 {
459     ZebraHandle zh = (ZebraHandle) handle;
460     Z_Segment *segment = rr->segment;
461
462     if (segment->num_segmentRecords)
463         zebra_admin_import_segment (zh, rr->segment);
464     else
465         zebra_admin_import_end (zh);
466     return 0;
467 }
468
469 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
470 {
471     ZebraHandle zh = (ZebraHandle) handle;
472     
473     yaz_log(YLOG_LOG, "function: %d", *rr->esr->function);
474     if (rr->esr->packageName)
475         yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
476     yaz_log(YLOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
477
478     if (!rr->esr->taskSpecificParameters)
479     {
480         yaz_log (YLOG_WARN, "No task specific parameters");
481     }
482     else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
483     {
484         es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
485
486         zebra_result (zh, &rr->errcode, &rr->errstring);
487     }
488     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
489     {
490         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
491         yaz_log (YLOG_LOG, "Received DB Update");
492         if (up->which == Z_IUUpdate_esRequest)
493         {
494             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
495             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
496             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
497             
498             yaz_log (YLOG_LOG, "action");
499             if (toKeep->action)
500             {
501                 switch (*toKeep->action)
502                 {
503                 case Z_IUOriginPartToKeep_recordInsert:
504                     yaz_log (YLOG_LOG, "recordInsert");
505                     break;
506                 case Z_IUOriginPartToKeep_recordReplace:
507                     yaz_log (YLOG_LOG, "recordUpdate");
508                     break;
509                 case Z_IUOriginPartToKeep_recordDelete:
510                     yaz_log (YLOG_LOG, "recordDelete");
511                     break;
512                 case Z_IUOriginPartToKeep_elementUpdate:
513                     yaz_log (YLOG_LOG, "elementUpdate");
514                     break;
515                 case Z_IUOriginPartToKeep_specialUpdate:
516                     yaz_log (YLOG_LOG, "specialUpdate");
517                     break;
518                 case Z_ESAdminOriginPartToKeep_shutdown:
519                     yaz_log (YLOG_LOG, "shutDown");
520                     break;
521                 case Z_ESAdminOriginPartToKeep_start:
522                     yaz_log (YLOG_LOG, "start");
523                     break;
524                 default:
525                     yaz_log (YLOG_LOG, " unknown (%d)", *toKeep->action);
526                 }
527             }
528             if (toKeep->databaseName)
529             {
530                 yaz_log (YLOG_LOG, "database: %s", toKeep->databaseName);
531
532                 if (zebra_select_database(zh, toKeep->databaseName))
533                     return 0;
534             }
535             else
536             {
537                 yaz_log (YLOG_WARN, "no database supplied for ES Update");
538                 rr->errcode = 1008;
539                 rr->errstring = "database";
540                 return 0;
541             }
542             if (notToKeep)
543             {
544                 int i;
545                 zebra_begin_trans (zh, 1);
546                 for (i = 0; i < notToKeep->num; i++)
547                 {
548                     Z_External *rec = notToKeep->elements[i]->record;
549                     struct oident *oident = 0;
550                     Odr_oct *opaque_recid = 0;
551                     SYSNO sysno = 0;
552
553                     if (notToKeep->elements[i]->u.opaque)
554                     {
555                         switch(notToKeep->elements[i]->which)
556                         {
557                         case Z_IUSuppliedRecords_elem_opaque:
558                             opaque_recid = notToKeep->elements[i]->u.opaque;
559                             break; /* OK, recid already set */
560                         case Z_IUSuppliedRecords_elem_number:
561                             sysno = *notToKeep->elements[i]->u.number;
562                             break;
563                         }
564                     }
565                     if (rec->direct_reference)
566                     {
567                         oident = oid_getentbyoid(rec->direct_reference);
568                         if (oident)
569                             yaz_log (YLOG_LOG, "record %d type %s", i,
570                                      oident->desc);
571                     }
572                     switch (rec->which)
573                     {
574                     case Z_External_sutrs:
575                         if (rec->u.octet_aligned->len > 170)
576                             yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
577                                      rec->u.sutrs->len,
578                                      rec->u.sutrs->buf);
579                         else
580                             yaz_log (YLOG_LOG, "%d bytes:\n%s",
581                                      rec->u.sutrs->len,
582                                      rec->u.sutrs->buf);
583                         break;
584                     case Z_External_octet:
585                         if (rec->u.octet_aligned->len > 170)
586                             yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
587                                      rec->u.octet_aligned->len,
588                                      rec->u.octet_aligned->buf);
589                         else
590                             yaz_log (YLOG_LOG, "%d bytes\n%s",
591                                      rec->u.octet_aligned->len,
592                                      rec->u.octet_aligned->buf);
593                     }
594                     if (oident && oident->value != VAL_TEXT_XML)
595                     {
596                         rr->errcode = 224;
597                         rr->errstring = "only XML update supported";
598                         break;
599                     }
600                     if (rec->which == Z_External_octet)
601                     {
602                         int action = 0;
603
604                         if (*toKeep->action ==
605                             Z_IUOriginPartToKeep_recordInsert)
606                             action = 1;
607                         if (*toKeep->action ==
608                             Z_IUOriginPartToKeep_recordReplace)
609                             action = 2;
610                         if (*toKeep->action ==
611                             Z_IUOriginPartToKeep_recordDelete)
612                             action = 3;
613                         if (*toKeep->action ==
614                             Z_IUOriginPartToKeep_specialUpdate)
615                             action = 4;
616
617                         if (!action)
618                         {
619                             rr->errcode = 224;
620                             rr->errstring = "unsupported ES Update action";
621                             break;
622                         }
623                         else if (opaque_recid)
624                         {
625                             int r = zebra_admin_exchange_record (
626                                 zh,
627                                 rec->u.octet_aligned->buf,
628                                 rec->u.octet_aligned->len,
629                                 opaque_recid->buf, opaque_recid->len,
630                                 action);
631                             if (r)
632                             {
633                                 rr->errcode = 224;
634                                 rr->errstring = "record exchange failed";
635                                 break;
636                             }
637                         }
638                         else
639                         {
640                             int r = -1;
641                             switch(action) {
642                             case 1:
643                                 r = zebra_insert_record(
644                                     zh,
645                                     0, /* recordType */
646                                     &sysno,
647                                     0, /* match */
648                                     0, /* fname */
649                                     rec->u.octet_aligned->buf,
650                                     rec->u.octet_aligned->len,
651                                     0);
652                                 if (r)
653                                 {
654                                     rr->errcode = 224;
655                                     rr->errstring = "insert_record failed";
656                                 }
657                                 break;
658                             case 2:
659                             case 4:
660                                 r = zebra_update_record(
661                                     zh,
662                                     0, /* recordType */
663                                     &sysno,
664                                     0, /* match */
665                                     0, /* fname */
666                                     rec->u.octet_aligned->buf,
667                                     rec->u.octet_aligned->len,
668                                     1);
669                                 if (r)
670                                 {
671                                     rr->errcode = 224;
672                                     rr->errstring = "update_record failed";
673                                 }
674                                 break;
675                             case 3:
676                                 r = zebra_delete_record(
677                                     zh,
678                                     0, /* recordType */
679                                     &sysno,
680                                     0, /* match */
681                                     0, /* fname */
682                                     rec->u.octet_aligned->buf,
683                                     rec->u.octet_aligned->len,
684                                     0);
685                                 if (r)
686                                 {
687                                     rr->errcode = 224;
688                                     rr->errstring = "delete_record failed";
689                                 }
690                                 break;
691                             }                           
692                         }
693                     }
694                 }
695                 zebra_end_trans (zh);
696             }
697         }
698     }
699     else
700     {
701         yaz_log (YLOG_WARN, "Unknown Extended Service(%d)",
702                  rr->esr->taskSpecificParameters->which);
703         rr->errcode = 221;
704         
705     }
706     return 0;
707 }
708
709 static void bend_start (struct statserv_options_block *sob)
710 {
711     if (sob->handle)
712         zebra_stop((ZebraService) sob->handle);
713     sob->handle = zebra_start(sob->configname);
714     if (!sob->handle)
715     {
716         yaz_log (YLOG_FATAL, "Failed to read config `%s'", sob->configname);
717         exit (1);
718     }
719 #ifdef WIN32
720     
721 #else
722     if (!sob->inetd) 
723     {
724         char pidfname[4096];
725         struct flock area;
726         int fd;
727
728         zebra_pidfname(sob->handle, pidfname);
729
730         fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
731         if (fd == -1)
732         {
733             if (errno != EEXIST)
734             {
735                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
736                 exit(1);
737             }
738             fd = open(pidfname, O_RDWR, 0666);
739             if (fd == -1)
740             {
741                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
742                 exit(1);
743             }
744         }
745         area.l_type = F_WRLCK;
746         area.l_whence = SEEK_SET;
747         area.l_len = area.l_start = 0L;
748         if (fcntl (fd, F_SETLK, &area) == -1)
749         {
750             yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
751             exit(1);
752         }
753         else
754         {
755             char pidstr[30];
756         
757             sprintf (pidstr, "%ld", (long) getpid ());
758             write (fd, pidstr, strlen(pidstr));
759         }
760     }
761 #endif
762 }
763
764 static void bend_stop(struct statserv_options_block *sob)
765 {
766 #ifdef WIN32
767
768 #else
769     if (!sob->inetd && sob->handle) 
770     {
771         char pidfname[4096];
772         zebra_pidfname(sob->handle, pidfname);
773         unlink (pidfname);
774     }
775 #endif
776     if (sob->handle)
777     {
778         ZebraService service = sob->handle;
779         zebra_stop(service);
780     }
781 }
782
783 int main (int argc, char **argv)
784 {
785     struct statserv_options_block *sob;
786
787     sob = statserv_getcontrol ();
788     strcpy (sob->configname, "zebra.cfg");
789     sob->bend_start = bend_start;
790     sob->bend_stop = bend_stop;
791 #ifdef WIN32
792     strcpy (sob->service_display_name, "Zebra Server");
793 #endif
794     statserv_setcontrol (sob);
795
796     return statserv_main (argc, argv, bend_init, bend_close);
797 }