Fix null ptr WRT sortkeys check
[idzebra-moved-to-github.git] / index / zebrasrv.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <fcntl.h>
27 #ifdef WIN32
28 #include <io.h>
29 #include <process.h>
30 #include <sys/locking.h>
31 #endif
32 #if HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 #include <errno.h>
37 #include <yaz/log.h>
38 #include <yaz/ill.h>
39 #include <yaz/yaz-util.h>
40 #include <yaz/diagbib1.h>
41 #include <yaz/querytowrbuf.h>
42 #include <yaz/pquery.h>
43 #include <sys/types.h>
44
45 #include <yaz/backend.h>
46 #include <yaz/charneg.h>
47 #include <idzebra/api.h>
48
49 static int bend_sort(void *handle, bend_sort_rr *rr);
50 static int bend_delete(void *handle, bend_delete_rr *rr);
51 static int bend_esrequest(void *handle, bend_esrequest_rr *rr);
52 static int bend_segment(void *handle, bend_segment_rr *rr);
53 static int bend_search(void *handle, bend_search_rr *r);
54 static int bend_fetch(void *handle, bend_fetch_rr *r);
55 static int bend_scan(void *handle, bend_scan_rr *r);
56
57 bend_initresult *bend_init(bend_initrequest *q)
58 {
59     bend_initresult *r = (bend_initresult *)
60         odr_malloc(q->stream, sizeof(*r));
61     ZebraHandle zh;
62     struct statserv_options_block *sob;
63     char *user = NULL;
64     char *passwd = NULL;
65     char version_str[16];
66
67     r->errcode = 0;
68     r->errstring = 0;
69     q->bend_sort = bend_sort;
70     q->bend_delete = bend_delete;
71     q->bend_esrequest = bend_esrequest;
72     q->bend_segment = bend_segment;
73     q->bend_search = bend_search;
74     q->bend_fetch = bend_fetch;
75     q->bend_scan = bend_scan;
76
77     zebra_get_version(version_str, 0);
78
79     q->implementation_name = "Zebra Information Server";
80     q->implementation_version = odr_strdup(q->stream, version_str);
81
82     yaz_log(YLOG_DEBUG, "bend_init");
83
84     sob = statserv_getcontrol();
85     if (!(zh = zebra_open(sob->handle, 0)))
86     {
87         yaz_log(YLOG_WARN, "Failed to read config `%s'", sob->configname);
88         r->errcode = YAZ_BIB1_PERMANENT_SYSTEM_ERROR;
89         return r;
90     }
91     r->handle = zh;
92
93     q->query_charset = odr_strdup(q->stream, zebra_get_encoding(zh));
94     if (q->auth)
95     {
96         if (q->auth->which == Z_IdAuthentication_open)
97         {
98             char *openpass = xstrdup(q->auth->u.open);
99             char *cp = strchr(openpass, '/');
100             if (cp)
101             {
102                 *cp = '\0';
103                 user = nmem_strdup(odr_getmem(q->stream), openpass);
104                 passwd = nmem_strdup(odr_getmem(q->stream), cp+1);
105             }
106             xfree(openpass);
107         }
108         else if (q->auth->which == Z_IdAuthentication_idPass)
109         {
110             Z_IdPass *idPass = q->auth->u.idPass;
111
112             user = idPass->userId;
113             passwd = idPass->password;
114         }
115     }
116     if (zebra_auth(zh, user, passwd) != ZEBRA_OK)
117     {
118         r->errcode = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
119         r->errstring = user;
120         return r;
121     }
122     if (q->charneg_request) /* characater set and language negotiation? */
123     {
124         char **charsets = 0;
125         int num_charsets;
126         char **langs = 0;
127         int num_langs = 0;
128         int selected = 0;
129         int i;
130         NMEM nmem = nmem_create();
131
132         yaz_get_proposal_charneg(nmem, q->charneg_request,
133                                  &charsets, &num_charsets,
134                                  &langs, &num_langs, &selected);
135         
136         for (i = 0; i < num_charsets; i++)
137         {
138             const char *right_name = "";
139             /*
140              * FIXME! It is like rudiment :-))
141              * We have to support this short names of character sets,
142              * because a lot servers in Russia to use own in during
143              * character set and language negotiation still.
144              */
145             
146             if (!yaz_matchstr(charsets[i], "win")) {
147                 right_name = "WINDOWS-1251";
148             } else if (!yaz_matchstr(charsets[i], "koi")) {
149                 right_name = "KOI8-R";
150             } else if (!yaz_matchstr(charsets[i], "iso")) {
151                 right_name = "ISO-8859-5";
152             } else if (!yaz_matchstr(charsets[i], "dos")) {
153                 right_name = "CP866";
154             } else if (!yaz_matchstr(charsets[i], "uni")) {
155                 right_name = "UTF-8";
156             } else {
157                 right_name = charsets[i];
158             }
159             if (odr_set_charset(q->decode, "UTF-8", right_name) == 0)
160             {
161                 yaz_log(YLOG_LOG, "charset %d %s (proper name %s): OK", i,
162                         charsets[i], right_name);
163                 odr_set_charset(q->stream, right_name, "UTF-8");
164                 if (selected)
165                     zebra_record_encoding(zh, right_name);
166                 zebra_octet_term_encoding(zh, right_name);
167                 q->charneg_response =
168                     yaz_set_response_charneg(q->stream, charsets[i],
169                                              0, selected);
170                 break;
171             } else {
172                 yaz_log(YLOG_LOG, "charset %d %s (proper name %s): unsupported", i,
173                         charsets[i], right_name);
174             }
175         }
176         nmem_destroy(nmem);
177     }
178     return r;
179 }
180
181 static void search_terms(ZebraHandle zh, bend_search_rr *r)
182 {
183     int no_terms;
184     int i;
185     int type = Z_Term_general;
186     struct Z_External *ext;
187     Z_SearchInfoReport *sr;
188
189     zebra_result_set_term_no(zh, r->setname, &no_terms);
190     if (!no_terms)
191         return;
192
193     r->search_info = odr_malloc(r->stream, sizeof(*r->search_info));
194
195     r->search_info->num_elements = 1;
196     r->search_info->list =
197         odr_malloc(r->stream, sizeof(*r->search_info->list));
198     r->search_info->list[0] =
199         odr_malloc(r->stream, sizeof(**r->search_info->list));
200     r->search_info->list[0]->category = 0;
201     r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
202     ext = odr_malloc(r->stream, sizeof(*ext));
203     r->search_info->list[0]->information.externallyDefinedInfo = ext;
204     ext->direct_reference = odr_oiddup(r->stream,
205                                        yaz_oid_userinfo_searchresult_1);
206     ext->indirect_reference = 0;
207     ext->descriptor = 0;
208     ext->which = Z_External_searchResult1;
209     sr = odr_malloc(r->stream, sizeof(Z_SearchInfoReport));
210     ext->u.searchResult1 = sr;
211     sr->num = no_terms;
212     sr->elements = odr_malloc(r->stream, sr->num *
213                                sizeof(*sr->elements));
214     for (i = 0; i<no_terms; i++)
215     {
216         Z_SearchInfoReport_s *se;
217         Z_Term *term;
218         zint count;
219         int approx;
220         char outbuf[1024];
221         size_t len = sizeof(outbuf);
222         const char *term_ref_id = 0;
223
224         zebra_result_set_term_info(zh, r->setname, i,
225                                    &count, &approx, outbuf, &len,
226                                    &term_ref_id);
227         se = sr->elements[i] = odr_malloc(r->stream, sizeof(**sr->elements));
228         se->subqueryId = term_ref_id ? 
229             odr_strdup(r->stream, term_ref_id) : 0;
230             
231         se->fullQuery = odr_booldup(r->stream, 0);
232         se->subqueryExpression = 
233             odr_malloc(r->stream, sizeof(Z_QueryExpression));
234         se->subqueryExpression->which = 
235             Z_QueryExpression_term;
236         se->subqueryExpression->u.term =
237             odr_malloc(r->stream, sizeof(Z_QueryExpressionTerm));
238         term = odr_malloc(r->stream, sizeof(Z_Term));
239         se->subqueryExpression->u.term->queryTerm = term;
240         switch (type)
241         {
242         case Z_Term_characterString:
243             term->which = Z_Term_characterString;
244             term->u.characterString = odr_strdup(r->stream, outbuf);
245             break;
246         case Z_Term_general:
247             term->which = Z_Term_general;
248             term->u.general = odr_malloc(r->stream, sizeof(*term->u.general));
249             term->u.general->size = term->u.general->len = len;
250             term->u.general->buf = odr_malloc(r->stream, len);
251             memcpy(term->u.general->buf, outbuf, len);
252             break;
253         default:
254             term->which = Z_Term_general;
255             term->u.null = odr_nullval();
256         }
257         se->subqueryExpression->u.term->termComment = 0;
258         se->subqueryInterpretation = 0;
259         se->subqueryRecommendation = 0;
260         se->subqueryCount = odr_intdup(r->stream, count);
261         se->subqueryWeight = 0;
262         se->resultsByDB = 0;
263     }
264 }
265
266 static int break_handler(void *client_data)
267 {
268     bend_association assoc =(bend_association) client_data;    
269     if (!bend_assoc_is_alive(assoc))
270         return 1;
271     return 0;
272 }
273
274 static Z_RPNQuery *query_add_sortkeys(ODR o, Z_RPNQuery *query,
275                                       const char *sortKeys)
276 {
277     /* sortkey layour: path,schema,ascending,caseSensitive,missingValue */
278     /* see cql_sortby_to_sortkeys of YAZ. */
279     char **sortspec;
280     int num_sortspec = 0;
281     int i;
282
283     if (sortKeys)
284         nmem_strsplit_blank(odr_getmem(o), sortKeys, &sortspec, &num_sortspec);
285     if (num_sortspec > 0)
286     {
287         Z_RPNQuery *nquery;
288         WRBUF w = wrbuf_alloc();
289
290         /* left operand 'd' will be replaced by original query */
291         wrbuf_puts(w, "@or d");
292         for (i = 0; i < num_sortspec; i++)
293         {
294             char **arg;
295             int num_arg;
296             int ascending = 1;
297             nmem_strsplitx(odr_getmem(o), ",", sortspec[i], &arg, &num_arg, 0);
298             
299             if (num_arg != 5)
300             {
301                 yaz_log(YLOG_WARN, "Invalid sort spec '%s' num_arg=%d",
302                         sortspec[i], num_arg);
303                 break;
304             }
305             if (arg[2][0])
306                 ascending = atoi(arg[2]);
307             
308             if (i < num_sortspec-1)
309                 wrbuf_puts(w, " @or");
310             wrbuf_puts(w, " @attr 1=");
311             yaz_encode_pqf_term(w, arg[0], strlen(arg[0]));
312             wrbuf_printf(w, "@attr 7=%d %d", ascending ? 1 : 2, i);
313         }
314         nquery = p_query_rpn(o, wrbuf_cstr(w));
315         if (!nquery)
316         {
317             yaz_log(YLOG_WARN, "query_add_sortkeys: bad RPN: '%s'",
318                     wrbuf_cstr(w));
319         }
320         else
321         {
322             Z_RPNStructure *s = nquery->RPNStructure;
323             
324             if (s->which != Z_RPNStructure_complex)
325             {
326                 yaz_log(YLOG_WARN, "query_add_sortkeys: not complex operand");
327             }
328             else
329             {
330                 /* left operand 'd' is replaced by origial query */
331                 s->u.complex->s1 = query->RPNStructure;
332                 /* and original query points to our new or+sort things */
333                 query->RPNStructure = s;
334             }
335         }
336         wrbuf_destroy(w);
337     }
338     return query;
339 }
340
341 int bend_search(void *handle, bend_search_rr *r)
342 {
343     ZebraHandle zh = (ZebraHandle) handle;
344     zint zhits = 0;
345     ZEBRA_RES res;
346     Z_RPNQuery *q2;
347
348     res = zebra_select_databases(zh, r->num_bases,
349                                   (const char **) r->basenames);
350     if (res != ZEBRA_OK)
351     {
352         zebra_result(zh, &r->errcode, &r->errstring);
353         return 0;
354     }
355     zebra_set_break_handler(zh, break_handler, r->association);
356     yaz_log(YLOG_DEBUG, "ResultSet '%s'", r->setname);
357     switch (r->query->which)
358     {
359     case Z_Query_type_1: case Z_Query_type_101:
360         q2 = query_add_sortkeys(r->stream, r->query->u.type_1, r->srw_sortKeys);
361         res = zebra_search_RPN_x(zh, r->stream, q2,
362                                  r->setname, &zhits,
363                                  &r->estimated_hit_count,
364                                  &r->partial_resultset);
365         if (res != ZEBRA_OK)
366             zebra_result(zh, &r->errcode, &r->errstring);
367         else
368         {
369             r->hits = zhits;
370             search_terms(zh, r);
371         }
372         break;
373     case Z_Query_type_2:
374         r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
375         r->errstring = "type-2";
376         break;
377     default:
378         r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
379     }
380     zebra_set_break_handler(zh, 0, 0);
381     return 0;
382 }
383
384
385 int bend_fetch(void *handle, bend_fetch_rr *r)
386 {
387     ZebraHandle zh = (ZebraHandle) handle;
388     ZebraRetrievalRecord retrievalRecord;
389     ZEBRA_RES res;
390
391     retrievalRecord.position = r->number;
392     
393     r->last_in_set = 0;
394     res = zebra_records_retrieve(zh, r->stream, r->setname, r->comp,
395                                   r->request_format, 1, &retrievalRecord);
396     if (res != ZEBRA_OK)
397     {
398         /* non-surrogate diagnostic */
399         zebra_result(zh, &r->errcode, &r->errstring);
400     }
401     else if (retrievalRecord.errCode)
402     {
403         /* surrogate diagnostic (diagnostic per record) */
404         r->surrogate_flag = 1;
405         r->errcode = retrievalRecord.errCode;
406         r->errstring = retrievalRecord.errString;
407         r->basename = retrievalRecord.base;
408     }
409     else
410     {
411         r->basename = retrievalRecord.base;
412         r->record = retrievalRecord.buf;
413         r->len = retrievalRecord.len;
414         r->output_format = odr_oiddup(r->stream, retrievalRecord.format);
415     }
416     return 0;
417 }
418
419 static int bend_scan(void *handle, bend_scan_rr *r)
420 {
421     ZebraScanEntry *entries;
422     ZebraHandle zh = (ZebraHandle) handle;
423     int is_partial, i;
424     ZEBRA_RES res;
425
426     res = zebra_select_databases(zh, r->num_bases, 
427                                  (const char **) r->basenames);
428     if (res != ZEBRA_OK)
429     {
430         zebra_result(zh, &r->errcode, &r->errstring);
431         return 0;
432     }
433     if (r->step_size != 0 && *r->step_size != 0) {
434         r->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
435         r->errstring = 0;
436         return 0;
437     }
438     res = zebra_scan(zh, r->stream, r->term,
439                      r->attributeset,
440                      &r->term_position,
441                      &r->num_entries, &entries, &is_partial, 
442                      0 /* setname */);
443     if (res == ZEBRA_OK)
444     {
445         if (is_partial)
446             r->status = BEND_SCAN_PARTIAL;
447         else
448             r->status = BEND_SCAN_SUCCESS;
449         for (i = 0; i < r->num_entries; i++)
450         {
451             r->entries[i].term = entries[i].term;
452             r->entries[i].display_term = entries[i].display_term;
453             r->entries[i].occurrences = entries[i].occurrences;
454         }
455     }
456     else
457     {
458         r->status = BEND_SCAN_PARTIAL;
459         zebra_result(zh, &r->errcode, &r->errstring);
460     }
461     return 0;
462 }
463
464 void bend_close(void *handle)
465 {
466     zebra_close((ZebraHandle) handle);
467     xmalloc_trav("bend_close");
468 }
469
470 int bend_sort(void *handle, bend_sort_rr *rr)
471 {
472     ZebraHandle zh = (ZebraHandle) handle;
473
474     if (zebra_sort(zh, rr->stream,
475                     rr->num_input_setnames, (const char **) rr->input_setnames,
476                     rr->output_setname, rr->sort_sequence, &rr->sort_status)
477         != ZEBRA_OK)
478         zebra_result(zh, &rr->errcode, &rr->errstring);
479     return 0;
480 }
481
482 int bend_delete(void *handle, bend_delete_rr *rr)
483 {
484     ZebraHandle zh = (ZebraHandle) handle;
485
486     rr->delete_status = zebra_deleteResultSet(zh, rr->function,
487                                               rr->num_setnames, rr->setnames,
488                                               rr->statuses);
489     return 0;
490 }
491
492 static void es_admin_request(bend_esrequest_rr *rr, ZebraHandle zh, Z_AdminEsRequest *r)
493 {
494     ZEBRA_RES res = ZEBRA_OK;
495     if (r->toKeep->databaseName)
496     {
497         yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
498     }
499     switch (r->toKeep->which)
500     {
501     case Z_ESAdminOriginPartToKeep_reIndex:
502         yaz_log(YLOG_LOG, "adm-reindex");
503         rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
504         rr->errstring = "adm-reindex not implemented yet";
505         break;
506     case Z_ESAdminOriginPartToKeep_truncate:
507         rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
508         rr->errstring = "adm-reindex not implemented yet";
509         yaz_log(YLOG_LOG, "adm-truncate");
510         break;
511     case Z_ESAdminOriginPartToKeep_drop:
512         yaz_log(YLOG_LOG, "adm-drop");
513         res = zebra_drop_database(zh, r->toKeep->databaseName);
514         break;
515     case Z_ESAdminOriginPartToKeep_create:
516         yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
517         res = zebra_create_database(zh, r->toKeep->databaseName);
518         break;
519     case Z_ESAdminOriginPartToKeep_import:
520         yaz_log(YLOG_LOG, "adm-import");
521         res = zebra_admin_import_begin(zh, r->toKeep->databaseName,
522                                         r->toKeep->u.import->recordType);
523         break;
524     case Z_ESAdminOriginPartToKeep_refresh:
525         yaz_log(YLOG_LOG, "adm-refresh");
526         break;
527     case Z_ESAdminOriginPartToKeep_commit:
528         yaz_log(YLOG_LOG, "adm-commit");
529         if (r->toKeep->databaseName)
530         {
531             if (zebra_select_database(zh, r->toKeep->databaseName) !=
532                 ZEBRA_OK)
533                 yaz_log(YLOG_WARN, "zebra_select_database failed in "
534                         "adm-commit");
535         }
536         zebra_commit(zh);
537         break;
538     case Z_ESAdminOriginPartToKeep_shutdown:
539         yaz_log(YLOG_LOG, "shutdown");
540         res = zebra_admin_shutdown(zh);
541         break;
542     case Z_ESAdminOriginPartToKeep_start:
543         yaz_log(YLOG_LOG, "start");
544         zebra_admin_start(zh);
545         break;
546     default:
547         yaz_log(YLOG_LOG, "unknown admin");
548         rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
549         rr->errstring = "adm-reindex not implemented yet";
550     }
551     if (res != ZEBRA_OK)
552         zebra_result(zh, &rr->errcode, &rr->errstring);
553 }
554
555 static void es_admin(bend_esrequest_rr *rr, ZebraHandle zh, Z_Admin *r)
556 {
557     switch (r->which)
558     {
559     case Z_Admin_esRequest:
560         es_admin_request(rr, zh, r->u.esRequest);
561         return;
562     default:
563         break;
564     }
565         yaz_log(YLOG_WARN, "adm taskpackage (unhandled)");
566     rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
567     rr->errstring = "adm-task package (unhandled)";
568 }
569
570 int bend_segment(void *handle, bend_segment_rr *rr)
571 {
572     ZebraHandle zh = (ZebraHandle) handle;
573     Z_Segment *segment = rr->segment;
574
575     if (segment->num_segmentRecords)
576         zebra_admin_import_segment(zh, rr->segment);
577     else
578         zebra_admin_import_end(zh);
579     return 0;
580 }
581
582 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
583 {
584     ZebraHandle zh = (ZebraHandle) handle;
585     
586     yaz_log(YLOG_LOG, "function: " ODR_INT_PRINTF, *rr->esr->function);
587     if (rr->esr->packageName)
588         yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
589     yaz_log(YLOG_LOG, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
590
591     if (!rr->esr->taskSpecificParameters)
592     {
593         yaz_log(YLOG_WARN, "No task specific parameters");
594     }
595     else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
596     {
597         es_admin(rr, zh, rr->esr->taskSpecificParameters->u.adminService);
598     }
599     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
600     {
601         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
602         yaz_log(YLOG_LOG, "Received DB Update");
603         if (up->which == Z_IUUpdate_esRequest)
604         {
605             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
606             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
607             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
608             
609             yaz_log(YLOG_LOG, "action");
610             if (toKeep->action)
611             {
612                 switch (*toKeep->action)
613                 {
614                 case Z_IUOriginPartToKeep_recordInsert:
615                     yaz_log(YLOG_LOG, "recordInsert");
616                     break;
617                 case Z_IUOriginPartToKeep_recordReplace:
618                     yaz_log(YLOG_LOG, "recordUpdate");
619                     break;
620                 case Z_IUOriginPartToKeep_recordDelete:
621                     yaz_log(YLOG_LOG, "recordDelete");
622                     break;
623                 case Z_IUOriginPartToKeep_elementUpdate:
624                     yaz_log(YLOG_LOG, "elementUpdate");
625                     break;
626                 case Z_IUOriginPartToKeep_specialUpdate:
627                     yaz_log(YLOG_LOG, "specialUpdate");
628                     break;
629                 case Z_ESAdminOriginPartToKeep_shutdown:
630                     yaz_log(YLOG_LOG, "shutDown");
631                     break;
632                 case Z_ESAdminOriginPartToKeep_start:
633                     yaz_log(YLOG_LOG, "start");
634                     break;
635                 default:
636                     yaz_log(YLOG_LOG, " unknown (" ODR_INT_PRINTF ")",
637                             *toKeep->action);
638                 }
639             }
640             if (toKeep->databaseName)
641             {
642                 yaz_log(YLOG_LOG, "database: %s", toKeep->databaseName);
643
644                 if (zebra_select_database(zh, toKeep->databaseName))
645                     return 0;
646             }
647             else
648             {
649                 yaz_log(YLOG_WARN, "no database supplied for ES Update");
650                 rr->errcode =
651                     YAZ_BIB1_ES_MISSING_MANDATORY_PARAMETER_FOR_SPECIFIED_FUNCTION_;
652                 rr->errstring = "database";
653                 return 0;
654             }
655             if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
656             {
657                 zebra_result(zh, &rr->errcode, &rr->errstring);
658             }
659             else
660             {
661                 int i;
662                 for (i = 0; notToKeep && i < notToKeep->num; i++)
663                 {
664                     Z_External *rec = notToKeep->elements[i]->record;
665                     Odr_oct *opaque_recid = 0;
666                     zint *sysno = 0;
667                     zint sysno_tmp;
668
669                     if (notToKeep->elements[i]->u.opaque)
670                     {
671                         switch(notToKeep->elements[i]->which)
672                         {
673                         case Z_IUSuppliedRecords_elem_opaque:
674                             opaque_recid = notToKeep->elements[i]->u.opaque;
675                             break; /* OK, recid already set */
676                         case Z_IUSuppliedRecords_elem_number:
677                             sysno_tmp = *notToKeep->elements[i]->u.number;
678                             sysno = &sysno_tmp;
679                             break;
680                         }
681                     }
682                     if (rec->direct_reference)
683                     {
684                         char oid_name_str[OID_STR_MAX];
685                         const char *oid_name =
686                             yaz_oid_to_string_buf(
687                                 rec->direct_reference,
688                                 0, oid_name_str);
689                         if (oid_name)
690                             yaz_log(YLOG_LOG, "record %d type %s", i,
691                                      oid_name);
692                     }
693                     switch (rec->which)
694                     {
695                     case Z_External_sutrs:
696                         if (rec->u.octet_aligned->len > 170)
697                             yaz_log(YLOG_LOG, "%d bytes:\n%.168s ...",
698                                      rec->u.sutrs->len,
699                                      rec->u.sutrs->buf);
700                         else
701                             yaz_log(YLOG_LOG, "%d bytes:\n%s",
702                                      rec->u.sutrs->len,
703                                      rec->u.sutrs->buf);
704                         break;
705                     case Z_External_octet:
706                         if (rec->u.octet_aligned->len > 170)
707                             yaz_log(YLOG_LOG, "%d bytes:\n%.168s ...",
708                                      rec->u.octet_aligned->len,
709                                      rec->u.octet_aligned->buf);
710                         else
711                             yaz_log(YLOG_LOG, "%d bytes\n%s",
712                                      rec->u.octet_aligned->len,
713                                      rec->u.octet_aligned->buf);
714                     }
715                     if (rec->which == Z_External_octet)
716                     {
717                         enum zebra_recctrl_action_t action = action_update;
718                         char recid_str[256];
719                         const char *match_criteria = 0;
720                         ZEBRA_RES res;
721
722                         if (*toKeep->action ==
723                             Z_IUOriginPartToKeep_recordInsert)
724                             action = action_insert;
725                         else if (*toKeep->action ==
726                             Z_IUOriginPartToKeep_recordReplace)
727                             action = action_replace;
728                         else if (*toKeep->action ==
729                             Z_IUOriginPartToKeep_recordDelete)
730                             action = action_delete;
731                         else if (*toKeep->action ==
732                             Z_IUOriginPartToKeep_specialUpdate)
733                             action = action_update;
734                         else
735                         {
736                             rr->errcode =
737                                 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
738                             rr->errstring = "unsupported ES Update action";
739                             break;
740                         }
741                         
742                         if (opaque_recid)
743                         {
744                             size_t l = opaque_recid->len;
745                             if (l >= sizeof(recid_str))
746                             {
747                                 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
748                                 rr->errstring = "opaque record ID too large";
749                                 break;
750                             }
751                             memcpy(recid_str, opaque_recid->buf, l);
752                             recid_str[l] = '\0';
753                             match_criteria = recid_str;
754                         }
755                         res = zebra_update_record(
756                             zh, action,
757                             0, /* recordType */
758                             sysno, match_criteria, 0, /* fname */
759                             (const char *) rec->u.octet_aligned->buf,
760                             rec->u.octet_aligned->len);
761                         if (res == ZEBRA_FAIL)
762                         {
763                             rr->errcode =
764                                 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
765                             rr->errstring = "update_record failed";
766                         }
767                     }
768                 }
769                 if (zebra_end_trans(zh) != ZEBRA_OK)
770                 {
771                     yaz_log(YLOG_WARN, "zebra_end_trans failed for"
772                             " extended service operation");
773                 }
774             }
775         }
776     }
777     else
778     {
779         yaz_log(YLOG_WARN, "Unknown Extended Service(%d)",
780                  rr->esr->taskSpecificParameters->which);
781         rr->errcode = YAZ_BIB1_ES_EXTENDED_SERVICE_TYPE_UNSUPP;
782         
783     }
784     return 0;
785 }
786
787 static void bend_start(struct statserv_options_block *sob)
788 {
789     Res default_res = res_open(0, 0);
790
791     if (sob->handle)
792         zebra_stop((ZebraService) sob->handle);
793     res_set(default_res, "profilePath", DEFAULT_PROFILE_PATH);
794     res_set(default_res, "modulePath", DEFAULT_MODULE_PATH);
795     sob->handle = zebra_start_res(sob->configname, default_res, 0);
796     res_close(default_res);
797     if (!sob->handle)
798     {
799         yaz_log(YLOG_FATAL, "Failed to read config `%s'", sob->configname);
800         exit(1);
801     }
802 #ifdef WIN32
803     
804 #else
805     if (!sob->inetd && !sob->background) 
806     {
807         char pidfname[4096];
808         struct flock area;
809         int fd;
810
811         zebra_pidfname(sob->handle, pidfname);
812
813         fd = open(pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
814         if (fd == -1)
815         {
816             if (errno != EEXIST)
817             {
818                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
819                 exit(1);
820             }
821             fd = open(pidfname, O_RDWR, 0666);
822             if (fd == -1)
823             {
824                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
825                 exit(1);
826             }
827         }
828         area.l_type = F_WRLCK;
829         area.l_whence = SEEK_SET;
830         area.l_len = area.l_start = 0L;
831         if (fcntl(fd, F_SETLK, &area) == -1)
832         {
833             yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
834             exit(1);
835         }
836         else
837         {
838             char pidstr[30];
839         
840             sprintf(pidstr, "%ld", (long) getpid());
841             if (write(fd, pidstr, strlen(pidstr)) != strlen(pidstr))
842             {
843                 yaz_log(YLOG_ERRNO|YLOG_FATAL, "write fail %s", pidfname);
844                 exit(1);
845             }
846         }
847     }
848 #endif
849 }
850
851 static void bend_stop(struct statserv_options_block *sob)
852 {
853 #ifdef WIN32
854
855 #else
856     if (!sob->inetd && !sob->background && sob->handle) 
857     {
858         char pidfname[4096];
859         zebra_pidfname(sob->handle, pidfname);
860         unlink(pidfname);
861     }
862 #endif
863     if (sob->handle)
864     {
865         ZebraService service = sob->handle;
866         zebra_stop(service);
867     }
868 }
869
870 int main(int argc, char **argv)
871 {
872     struct statserv_options_block *sob;
873
874     sob = statserv_getcontrol();
875     strcpy(sob->configname, "zebra.cfg");
876     sob->bend_start = bend_start;
877     sob->bend_stop = bend_stop;
878 #ifdef WIN32
879     strcpy(sob->service_name, "zebrasrv");
880     strcpy(sob->service_display_name, "Zebra Server");
881 #endif
882     statserv_setcontrol(sob);
883
884     return statserv_main(argc, argv, bend_init, bend_close);
885 }
886 /*
887  * Local variables:
888  * c-basic-offset: 4
889  * c-file-style: "Stroustrup"
890  * indent-tabs-mode: nil
891  * End:
892  * vim: shiftwidth=4 tabstop=8 expandtab
893  */
894