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