Fixed bug in return of handle for init when authentication failed.
[idzebra-moved-to-github.git] / index / zserver.c
1 /* $Id: zserver.c,v 1.114 2004-03-29 15:48:14 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 (LOG_DEBUG, "bend_init");
74
75     sob = statserv_getcontrol ();
76     if (!(zh = zebra_open (sob->handle)))
77     {
78         yaz_log (LOG_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 (LOG_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 (LOG_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                 q->charneg_response =
158                     yaz_set_response_charneg (q->stream, charsets[i],
159                                               0, selected);
160                 break;
161             } else {
162                 yaz_log (LOG_LOG, "charset %d %s (proper name %s): unsupported", i,
163                          charsets[i], right_name);
164             }
165         }
166         nmem_destroy(nmem);
167     }
168     return r;
169 }
170
171 static void search_terms (ZebraHandle zh, bend_search_rr *r)
172 {
173     int count;
174     int no_terms;
175     int i;
176     int type;
177     struct Z_External *ext;
178     Z_SearchInfoReport *sr;
179
180     /* get no of terms for result set */
181     no_terms = zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0);
182     if (!no_terms)
183         return;
184
185     r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
186
187     r->search_info->num_elements = 1;
188     r->search_info->list =
189         odr_malloc (r->stream, sizeof(*r->search_info->list));
190     r->search_info->list[0] =
191         odr_malloc (r->stream, sizeof(**r->search_info->list));
192     r->search_info->list[0]->category = 0;
193     r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
194     ext = odr_malloc (r->stream, sizeof(*ext));
195     r->search_info->list[0]->information.externallyDefinedInfo = ext;
196     ext->direct_reference =
197         yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
198     ext->indirect_reference = 0;
199     ext->descriptor = 0;
200     ext->which = Z_External_searchResult1;
201     sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
202     ext->u.searchResult1 = sr;
203     sr->num = no_terms;
204     sr->elements = odr_malloc (r->stream, sr->num *
205                                sizeof(*sr->elements));
206     for (i = 0; i<no_terms; i++)
207     {
208         Z_Term *term;
209         char outbuf[1024];
210         size_t len = sizeof(outbuf);
211         zebra_resultSetTerms (zh, r->setname, i,
212                               &count, &type, outbuf, &len);
213         
214         sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
215         sr->elements[i]->subqueryId = 0;
216         sr->elements[i]->fullQuery = odr_malloc (r->stream, 
217                                                  sizeof(bool_t));
218         *sr->elements[i]->fullQuery = 0;
219         sr->elements[i]->subqueryExpression = 
220             odr_malloc (r->stream, sizeof(Z_QueryExpression));
221         sr->elements[i]->subqueryExpression->which = 
222             Z_QueryExpression_term;
223         sr->elements[i]->subqueryExpression->u.term =
224             odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
225         term = odr_malloc (r->stream, sizeof(Z_Term));
226         sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
227         switch (type)
228         {
229         case Z_Term_characterString:
230             yaz_log (LOG_DEBUG, "term as characterString");
231             term->which = Z_Term_characterString;
232             term->u.characterString = odr_strdup (r->stream, outbuf);
233             break;
234         case Z_Term_general:
235             yaz_log (LOG_DEBUG, "term as general");
236             term->which = Z_Term_general;
237             term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
238             term->u.general->size = term->u.general->len = len;
239             term->u.general->buf = odr_malloc (r->stream, len);
240             memcpy (term->u.general->buf, outbuf, len);
241             break;
242         default:
243             term->which = Z_Term_general;
244             term->u.null = odr_nullval();
245         }
246         sr->elements[i]->subqueryExpression->u.term->termComment = 0;
247         sr->elements[i]->subqueryInterpretation = 0;
248         sr->elements[i]->subqueryRecommendation = 0;
249         sr->elements[i]->subqueryCount = odr_intdup (r->stream, count);
250         sr->elements[i]->subqueryWeight = 0;
251         sr->elements[i]->resultsByDB = 0;
252     }
253 }
254
255 int bend_search (void *handle, bend_search_rr *r)
256 {
257     ZebraHandle zh = (ZebraHandle) handle;
258
259     r->hits = 0;
260     r->errcode = 0;
261     r->errstring = NULL;
262     
263     if (zebra_select_databases (zh, r->num_bases,
264                                 (const char **) r->basenames))
265     {
266         zebra_result (zh, &r->errcode, &r->errstring);
267         return 0;
268     }
269     yaz_log (LOG_LOG, "ResultSet '%s'", r->setname);
270     switch (r->query->which)
271     {
272     case Z_Query_type_1: case Z_Query_type_101:
273         zebra_search_RPN (zh, r->stream, r->query->u.type_1,
274                           r->setname, &r->hits);
275         zebra_result (zh, &r->errcode, &r->errstring);
276         if (!r->errcode)
277             search_terms (zh, r);
278         break;
279     case Z_Query_type_2:
280         r->errcode = 107;
281         r->errstring = "type-2";
282         break;
283     default:
284         r->errcode = 107;
285     }
286     return 0;
287 }
288
289
290 int bend_fetch (void *handle, bend_fetch_rr *r)
291 {
292     ZebraHandle zh = (ZebraHandle) handle;
293     ZebraRetrievalRecord retrievalRecord;
294
295     retrievalRecord.position = r->number;
296     
297     r->last_in_set = 0;
298     zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
299                             r->request_format, 1, &retrievalRecord);
300     zebra_result (zh, &r->errcode, &r->errstring);
301     /*  non Surrogate Diagnostic OR Surrogate Diagnostic */
302     if (r->errcode == 0 && retrievalRecord.errCode)
303     {
304         r->surrogate_flag = 1;
305         r->errcode = retrievalRecord.errCode;
306         r->errstring = retrievalRecord.errString;
307         r->basename = retrievalRecord.base;
308     }
309     else if (r->errcode == 0)        /* Database Record */
310     {
311         r->errcode = 0;
312         r->basename = retrievalRecord.base;
313         r->record = retrievalRecord.buf;
314         r->len = retrievalRecord.len;
315         r->output_format = retrievalRecord.format;
316     }
317     return 0;
318 }
319
320 static int bend_scan (void *handle, bend_scan_rr *r)
321 {
322     ZebraScanEntry *entries;
323     ZebraHandle zh = (ZebraHandle) handle;
324     int is_partial, i;
325
326     if (zebra_select_databases (zh, r->num_bases, 
327                                 (const char **) r->basenames))
328     {
329         zebra_result (zh, &r->errcode, &r->errstring);
330         return 0;
331     }
332     r->entries = (struct scan_entry *)
333         odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
334     zebra_scan (zh, r->stream, r->term,
335                 r->attributeset,
336                 &r->term_position,
337                 &r->num_entries, &entries, &is_partial);
338     if (is_partial)
339         r->status = BEND_SCAN_PARTIAL;
340     else
341         r->status = BEND_SCAN_SUCCESS;
342     for (i = 0; i < r->num_entries; i++)
343     {
344         r->entries[i].term = entries[i].term;
345         r->entries[i].occurrences = entries[i].occurrences;
346     }
347     zebra_result (zh, &r->errcode, &r->errstring);
348     return 0;
349 }
350
351 void bend_close (void *handle)
352 {
353     zebra_close ((ZebraHandle) handle);
354     xmalloc_trav("bend_close");
355     nmem_print_list();
356 }
357
358 int bend_sort (void *handle, bend_sort_rr *rr)
359 {
360     ZebraHandle zh = (ZebraHandle) handle;
361
362     zebra_sort (zh, rr->stream,
363                 rr->num_input_setnames, (const char **) rr->input_setnames,
364                 rr->output_setname, rr->sort_sequence, &rr->sort_status);
365     zebra_result (zh, &rr->errcode, &rr->errstring);
366     return 0;
367 }
368
369 int bend_delete (void *handle, bend_delete_rr *rr)
370 {
371     ZebraHandle zh = (ZebraHandle) handle;
372
373     rr->delete_status = zebra_deleleResultSet(zh, rr->function,
374                                               rr->num_setnames, rr->setnames,
375                                               rr->statuses);
376     return 0;
377 }
378
379 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
380 {
381     switch (r->toKeep->which)
382     {
383     case Z_ESAdminOriginPartToKeep_reIndex:
384         yaz_log(LOG_LOG, "adm-reindex");
385         break;
386     case Z_ESAdminOriginPartToKeep_truncate:
387         yaz_log(LOG_LOG, "adm-truncate");
388         break;
389     case Z_ESAdminOriginPartToKeep_drop:
390         yaz_log(LOG_LOG, "adm-drop");
391         zebra_drop_database (zh, r->toKeep->databaseName);
392         break;
393     case Z_ESAdminOriginPartToKeep_create:
394         yaz_log(LOG_LOG, "adm-create %s", r->toKeep->databaseName);
395         zebra_create_database (zh, r->toKeep->databaseName);
396         break;
397     case Z_ESAdminOriginPartToKeep_import:
398         yaz_log(LOG_LOG, "adm-import");
399         zebra_admin_import_begin (zh, r->toKeep->databaseName,
400                         r->toKeep->u.import->recordType);
401         break;
402     case Z_ESAdminOriginPartToKeep_refresh:
403         yaz_log(LOG_LOG, "adm-refresh");
404         break;
405     case Z_ESAdminOriginPartToKeep_commit:
406         yaz_log(LOG_LOG, "adm-commit");
407         break;
408     case Z_ESAdminOriginPartToKeep_shutdown:
409         yaz_log(LOG_LOG, "shutdown");
410         zebra_admin_shutdown(zh);
411         break;
412     case Z_ESAdminOriginPartToKeep_start:
413         yaz_log(LOG_LOG, "start");
414         zebra_admin_start(zh);
415         break;
416     default:
417         yaz_log(LOG_LOG, "unknown admin");
418     }
419     if (r->toKeep->databaseName)
420     {
421         yaz_log(LOG_LOG, "database %s", r->toKeep->databaseName);
422     }
423     return 0;
424 }
425
426 static int es_admin (ZebraHandle zh, Z_Admin *r)
427 {
428     switch (r->which)
429     {
430     case Z_Admin_esRequest:
431         es_admin_request (zh, r->u.esRequest);
432         break;
433     case Z_Admin_taskPackage:
434         yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
435         break;
436     default:
437         break;
438     }
439
440     return 0;
441 }
442
443 int bend_segment (void *handle, bend_segment_rr *rr)
444 {
445     ZebraHandle zh = (ZebraHandle) handle;
446     Z_Segment *segment = rr->segment;
447
448     if (segment->num_segmentRecords)
449         zebra_admin_import_segment (zh, rr->segment);
450     else
451         zebra_admin_import_end (zh);
452     return 0;
453 }
454
455 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
456 {
457     ZebraHandle zh = (ZebraHandle) handle;
458     
459     yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
460     if (rr->esr->packageName)
461         yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
462     yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
463
464     if (!rr->esr->taskSpecificParameters)
465     {
466         yaz_log (LOG_WARN, "No task specific parameters");
467     }
468     else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
469     {
470         es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
471
472         zebra_result (zh, &rr->errcode, &rr->errstring);
473     }
474     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
475     {
476         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
477         yaz_log (LOG_LOG, "Received DB Update");
478         if (up->which == Z_IUUpdate_esRequest)
479         {
480             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
481             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
482             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
483             
484             yaz_log (LOG_LOG, "action");
485             if (toKeep->action)
486             {
487                 switch (*toKeep->action)
488                 {
489                 case Z_IUOriginPartToKeep_recordInsert:
490                     yaz_log (LOG_LOG, "recordInsert");
491                     break;
492                 case Z_IUOriginPartToKeep_recordReplace:
493                     yaz_log (LOG_LOG, "recordUpdate");
494                     break;
495                 case Z_IUOriginPartToKeep_recordDelete:
496                     yaz_log (LOG_LOG, "recordDelete");
497                     break;
498                 case Z_IUOriginPartToKeep_elementUpdate:
499                     yaz_log (LOG_LOG, "elementUpdate");
500                     break;
501                 case Z_IUOriginPartToKeep_specialUpdate:
502                     yaz_log (LOG_LOG, "specialUpdate");
503                     break;
504                 case Z_ESAdminOriginPartToKeep_shutdown:
505                     yaz_log (LOG_LOG, "shutDown");
506                     break;
507                 case Z_ESAdminOriginPartToKeep_start:
508                     yaz_log (LOG_LOG, "start");
509                     break;
510                 default:
511                     yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
512                 }
513             }
514             if (toKeep->databaseName)
515             {
516                 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
517
518                 if (zebra_select_database(zh, toKeep->databaseName))
519                     return 0;
520             }
521             else
522             {
523                 yaz_log (LOG_WARN, "no database supplied for ES Update");
524                 rr->errcode = 1008;
525                 rr->errstring = "database";
526                 return 0;
527             }
528             if (notToKeep)
529             {
530                 int i;
531                 zebra_begin_trans (zh, 1);
532                 for (i = 0; i < notToKeep->num; i++)
533                 {
534                     Z_External *rec = notToKeep->elements[i]->record;
535                     struct oident *oident = 0;
536                     Odr_oct *opaque_recid = 0;
537                     int sysno = 0;
538
539                     if (notToKeep->elements[i]->u.opaque)
540                     {
541                         switch(notToKeep->elements[i]->which)
542                         {
543                         case Z_IUSuppliedRecords_elem_opaque:
544                             opaque_recid = notToKeep->elements[i]->u.opaque;
545                             break; /* OK, recid already set */
546                         case Z_IUSuppliedRecords_elem_number:
547                             sysno = *notToKeep->elements[i]->u.number;
548                             break;
549                         }
550                     }
551                     if (rec->direct_reference)
552                     {
553                         oident = oid_getentbyoid(rec->direct_reference);
554                         if (oident)
555                             yaz_log (LOG_LOG, "record %d type %s", i,
556                                      oident->desc);
557                     }
558                     switch (rec->which)
559                     {
560                     case Z_External_sutrs:
561                         if (rec->u.octet_aligned->len > 170)
562                             yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
563                                      rec->u.sutrs->len,
564                                      rec->u.sutrs->buf);
565                         else
566                             yaz_log (LOG_LOG, "%d bytes:\n%s",
567                                      rec->u.sutrs->len,
568                                      rec->u.sutrs->buf);
569                         break;
570                     case Z_External_octet:
571                         if (rec->u.octet_aligned->len > 170)
572                             yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
573                                      rec->u.octet_aligned->len,
574                                      rec->u.octet_aligned->buf);
575                         else
576                             yaz_log (LOG_LOG, "%d bytes\n%s",
577                                      rec->u.octet_aligned->len,
578                                      rec->u.octet_aligned->buf);
579                     }
580                     if (oident && oident->value != VAL_TEXT_XML)
581                     {
582                         rr->errcode = 224;
583                         rr->errstring = "only XML update supported";
584                         break;
585                     }
586                     if (rec->which == Z_External_octet)
587                     {
588                         int action = 0;
589
590                         if (*toKeep->action ==
591                             Z_IUOriginPartToKeep_recordInsert)
592                             action = 1;
593                         if (*toKeep->action ==
594                             Z_IUOriginPartToKeep_recordReplace)
595                             action = 2;
596                         if (*toKeep->action ==
597                             Z_IUOriginPartToKeep_recordDelete)
598                             action = 3;
599                         if (*toKeep->action ==
600                             Z_IUOriginPartToKeep_specialUpdate)
601                             action = 4;
602
603                         if (!action)
604                         {
605                             rr->errcode = 224;
606                             rr->errstring = "unsupported ES Update action";
607                             break;
608                         }
609                         else if (opaque_recid)
610                         {
611                             int r = zebra_admin_exchange_record (
612                                 zh,
613                                 rec->u.octet_aligned->buf,
614                                 rec->u.octet_aligned->len,
615                                 opaque_recid->buf, opaque_recid->len,
616                                 action);
617                             if (r)
618                             {
619                                 rr->errcode = 224;
620                                 rr->errstring = "record exchange failed";
621                                 break;
622                             }
623                         }
624                         else
625                         {
626                             int r = -1;
627                             switch(action) {
628                             case 1:
629                                 r = zebra_insert_record(
630                                     zh,
631                                     0, /* recordType */
632                                     &sysno,
633                                     0, /* match */
634                                     0, /* fname */
635                                     rec->u.octet_aligned->buf,
636                                     rec->u.octet_aligned->len);
637                                 if (r)
638                                 {
639                                     rr->errcode = 224;
640                                     rr->errstring = "insert_record failed";
641                                 }
642                                 break;
643                             case 2:
644                             case 4:
645                                 r = zebra_update_record(
646                                     zh,
647                                     0, /* recordType */
648                                     &sysno,
649                                     0, /* match */
650                                     0, /* fname */
651                                     rec->u.octet_aligned->buf,
652                                     rec->u.octet_aligned->len,
653                                     1);
654                                 if (r)
655                                 {
656                                     rr->errcode = 224;
657                                     rr->errstring = "update_record failed";
658                                 }
659                                 break;
660                             case 3:
661                                 r = zebra_delete_record(
662                                     zh,
663                                     0, /* recordType */
664                                     &sysno,
665                                     0, /* match */
666                                     0, /* fname */
667                                     rec->u.octet_aligned->buf,
668                                     rec->u.octet_aligned->len,
669                                     0);
670                                 if (r)
671                                 {
672                                     rr->errcode = 224;
673                                     rr->errstring = "delete_record failed";
674                                 }
675                                 break;
676                             }                           
677                         }
678                     }
679                 }
680                 zebra_end_trans (zh);
681             }
682         }
683     }
684     else
685     {
686         yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
687                  rr->esr->taskSpecificParameters->which);
688         rr->errcode = 221;
689         
690     }
691     return 0;
692 }
693
694 static void bend_start (struct statserv_options_block *sob)
695 {
696     if (sob->handle)
697         zebra_stop((ZebraService) sob->handle);
698     sob->handle = zebra_start(sob->configname, 0, 0);
699     if (!sob->handle)
700     {
701         yaz_log (LOG_FATAL, "Failed to read config `%s'", sob->configname);
702         exit (1);
703     }
704 #ifdef WIN32
705     
706 #else
707     if (!sob->inetd) 
708     {
709         char pidfname[4096];
710         struct flock area;
711         int fd;
712
713         zebra_pidfname(sob->handle, pidfname);
714
715         fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
716         if (fd == -1)
717         {
718             if (errno != EEXIST)
719             {
720                 yaz_log(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfname);
721                 exit(1);
722             }
723             fd = open(pidfname, O_RDWR, 0666);
724             if (fd == -1)
725             {
726                 yaz_log(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfname);
727                 exit(1);
728             }
729         }
730         area.l_type = F_WRLCK;
731         area.l_whence = SEEK_SET;
732         area.l_len = area.l_start = 0L;
733         if (fcntl (fd, F_SETLK, &area) == -1)
734         {
735             yaz_log(LOG_ERRNO|LOG_FATAL, "Zebra server already running");
736             exit(1);
737         }
738         else
739         {
740             char pidstr[30];
741         
742             sprintf (pidstr, "%ld", (long) getpid ());
743             write (fd, pidstr, strlen(pidstr));
744         }
745     }
746 #endif
747 }
748
749 static void bend_stop(struct statserv_options_block *sob)
750 {
751 #ifdef WIN32
752
753 #else
754     if (!sob->inetd && sob->handle) 
755     {
756         char pidfname[4096];
757         zebra_pidfname(sob->handle, pidfname);
758         unlink (pidfname);
759     }
760 #endif
761     if (sob->handle)
762     {
763         ZebraService service = sob->handle;
764         zebra_stop(service);
765     }
766 }
767
768 int main (int argc, char **argv)
769 {
770     struct statserv_options_block *sob;
771
772     sob = statserv_getcontrol ();
773     strcpy (sob->configname, "zebra.cfg");
774     sob->bend_start = bend_start;
775     sob->bend_stop = bend_stop;
776 #ifdef WIN32
777     strcpy (sob->service_display_name, "Zebra Server");
778 #endif
779     statserv_setcontrol (sob);
780
781     return statserv_main (argc, argv, bend_init, bend_close);
782 }