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