1 /* $Id: zserver.c,v 1.111 2003-09-05 12:35:06 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
5 This file is part of the Zebra server.
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
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
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
29 #include <sys/locking.h>
37 #include <yaz/yaz-util.h>
39 #include <sys/types.h>
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);
51 bend_initresult *bend_init (bend_initrequest *q)
53 bend_initresult *r = (bend_initresult *)
54 odr_malloc (q->stream, sizeof(*r));
56 struct statserv_options_block *sob;
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;
70 q->implementation_name = "Zebra Information Server";
71 q->implementation_version = "Zebra " ZEBRAVER;
73 yaz_log (LOG_DEBUG, "bend_init");
75 sob = statserv_getcontrol ();
76 if (!(zh = zebra_open (sob->handle)))
78 yaz_log (LOG_WARN, "Failed to read config `%s'", sob->configname);
84 if (q->auth->which == Z_IdAuthentication_open)
86 char *openpass = xstrdup (q->auth->u.open);
87 char *cp = strchr (openpass, '/');
91 user = nmem_strdup (odr_getmem (q->stream), openpass);
92 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
97 if (zebra_auth (zh, user, passwd))
105 if (q->charneg_request) /* characater set and langauge negotiation? */
113 NMEM nmem = nmem_create();
115 yaz_log (LOG_LOG, "character set and language negotiation");
117 yaz_get_proposal_charneg (nmem, q->charneg_request,
118 &charsets, &num_charsets,
119 &langs, &num_langs, &selected);
121 for (i = 0; i < num_charsets; i++)
123 const char *right_name = "";
125 * FIXME! It is like rudiment :-))
126 * We have to support this short names of character sets,
127 * because a lot servers in Russia to use own in during
128 * character set and language negotiation still.
131 if (!yaz_matchstr(charsets[i], "win")) {
132 right_name = "WINDOWS-1251";
133 } else if (!yaz_matchstr(charsets[i], "koi")) {
134 right_name = "KOI8-R";
135 } else if (!yaz_matchstr(charsets[i], "iso")) {
136 right_name = "ISO-8859-5";
137 } else if (!yaz_matchstr(charsets[i], "dos")) {
138 right_name = "CP866";
139 } else if (!yaz_matchstr(charsets[i], "uni")) {
140 right_name = "UTF-8";
142 right_name = charsets[i];
144 if (odr_set_charset (q->decode, "UTF-8", right_name) == 0)
146 yaz_log (LOG_LOG, "charset %d %s (proper name %s): OK", i,
147 charsets[i], right_name);
148 odr_set_charset (q->stream, right_name, "UTF-8");
150 zebra_record_encoding (zh, right_name);
151 q->charneg_response =
152 yaz_set_response_charneg (q->stream, right_name,
156 yaz_log (LOG_LOG, "charset %d %s (proper name %s): unsupported", i,
157 charsets[i], right_name);
165 static void search_terms (ZebraHandle zh, bend_search_rr *r)
171 struct Z_External *ext;
172 Z_SearchInfoReport *sr;
174 /* get no of terms for result set */
175 no_terms = zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0);
179 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
181 r->search_info->num_elements = 1;
182 r->search_info->list =
183 odr_malloc (r->stream, sizeof(*r->search_info->list));
184 r->search_info->list[0] =
185 odr_malloc (r->stream, sizeof(**r->search_info->list));
186 r->search_info->list[0]->category = 0;
187 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
188 ext = odr_malloc (r->stream, sizeof(*ext));
189 r->search_info->list[0]->information.externallyDefinedInfo = ext;
190 ext->direct_reference =
191 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
192 ext->indirect_reference = 0;
194 ext->which = Z_External_searchResult1;
195 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
196 ext->u.searchResult1 = sr;
198 sr->elements = odr_malloc (r->stream, sr->num *
199 sizeof(*sr->elements));
200 for (i = 0; i<no_terms; i++)
204 size_t len = sizeof(outbuf);
205 zebra_resultSetTerms (zh, r->setname, i,
206 &count, &type, outbuf, &len);
208 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
209 sr->elements[i]->subqueryId = 0;
210 sr->elements[i]->fullQuery = odr_malloc (r->stream,
212 *sr->elements[i]->fullQuery = 0;
213 sr->elements[i]->subqueryExpression =
214 odr_malloc (r->stream, sizeof(Z_QueryExpression));
215 sr->elements[i]->subqueryExpression->which =
216 Z_QueryExpression_term;
217 sr->elements[i]->subqueryExpression->u.term =
218 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
219 term = odr_malloc (r->stream, sizeof(Z_Term));
220 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
223 case Z_Term_characterString:
224 yaz_log (LOG_DEBUG, "term as characterString");
225 term->which = Z_Term_characterString;
226 term->u.characterString = odr_strdup (r->stream, outbuf);
229 yaz_log (LOG_DEBUG, "term as general");
230 term->which = Z_Term_general;
231 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
232 term->u.general->size = term->u.general->len = len;
233 term->u.general->buf = odr_malloc (r->stream, len);
234 memcpy (term->u.general->buf, outbuf, len);
237 term->which = Z_Term_general;
238 term->u.null = odr_nullval();
240 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
241 sr->elements[i]->subqueryInterpretation = 0;
242 sr->elements[i]->subqueryRecommendation = 0;
243 sr->elements[i]->subqueryCount = odr_intdup (r->stream, count);
244 sr->elements[i]->subqueryWeight = 0;
245 sr->elements[i]->resultsByDB = 0;
249 int bend_search (void *handle, bend_search_rr *r)
251 ZebraHandle zh = (ZebraHandle) handle;
257 if (zebra_select_databases (zh, r->num_bases,
258 (const char **) r->basenames))
260 zebra_result (zh, &r->errcode, &r->errstring);
263 yaz_log (LOG_LOG, "ResultSet '%s'", r->setname);
264 switch (r->query->which)
266 case Z_Query_type_1: case Z_Query_type_101:
267 zebra_search_RPN (zh, r->stream, r->query->u.type_1,
268 r->setname, &r->hits);
269 zebra_result (zh, &r->errcode, &r->errstring);
271 search_terms (zh, r);
275 r->errstring = "type-2";
284 int bend_fetch (void *handle, bend_fetch_rr *r)
286 ZebraHandle zh = (ZebraHandle) handle;
287 ZebraRetrievalRecord retrievalRecord;
289 retrievalRecord.position = r->number;
292 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
293 r->request_format, 1, &retrievalRecord);
294 zebra_result (zh, &r->errcode, &r->errstring);
295 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
296 if (r->errcode == 0 && retrievalRecord.errCode)
298 r->surrogate_flag = 1;
299 r->errcode = retrievalRecord.errCode;
300 r->errstring = retrievalRecord.errString;
301 r->basename = retrievalRecord.base;
303 else if (r->errcode == 0) /* Database Record */
306 r->basename = retrievalRecord.base;
307 r->record = retrievalRecord.buf;
308 r->len = retrievalRecord.len;
309 r->output_format = retrievalRecord.format;
314 static int bend_scan (void *handle, bend_scan_rr *r)
316 ZebraScanEntry *entries;
317 ZebraHandle zh = (ZebraHandle) handle;
320 if (zebra_select_databases (zh, r->num_bases,
321 (const char **) r->basenames))
323 zebra_result (zh, &r->errcode, &r->errstring);
326 r->entries = (struct scan_entry *)
327 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
328 zebra_scan (zh, r->stream, r->term,
331 &r->num_entries, &entries, &is_partial);
333 r->status = BEND_SCAN_PARTIAL;
335 r->status = BEND_SCAN_SUCCESS;
336 for (i = 0; i < r->num_entries; i++)
338 r->entries[i].term = entries[i].term;
339 r->entries[i].occurrences = entries[i].occurrences;
341 zebra_result (zh, &r->errcode, &r->errstring);
345 void bend_close (void *handle)
347 zebra_close ((ZebraHandle) handle);
348 xmalloc_trav("bend_close");
352 int bend_sort (void *handle, bend_sort_rr *rr)
354 ZebraHandle zh = (ZebraHandle) handle;
356 zebra_sort (zh, rr->stream,
357 rr->num_input_setnames, (const char **) rr->input_setnames,
358 rr->output_setname, rr->sort_sequence, &rr->sort_status);
359 zebra_result (zh, &rr->errcode, &rr->errstring);
363 int bend_delete (void *handle, bend_delete_rr *rr)
365 ZebraHandle zh = (ZebraHandle) handle;
367 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
368 rr->num_setnames, rr->setnames,
373 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
375 switch (r->toKeep->which)
377 case Z_ESAdminOriginPartToKeep_reIndex:
378 yaz_log(LOG_LOG, "adm-reindex");
380 case Z_ESAdminOriginPartToKeep_truncate:
381 yaz_log(LOG_LOG, "adm-truncate");
383 case Z_ESAdminOriginPartToKeep_drop:
384 yaz_log(LOG_LOG, "adm-drop");
385 zebra_drop_database (zh, r->toKeep->databaseName);
387 case Z_ESAdminOriginPartToKeep_create:
388 yaz_log(LOG_LOG, "adm-create %s", r->toKeep->databaseName);
389 zebra_create_database (zh, r->toKeep->databaseName);
391 case Z_ESAdminOriginPartToKeep_import:
392 yaz_log(LOG_LOG, "adm-import");
393 zebra_admin_import_begin (zh, r->toKeep->databaseName,
394 r->toKeep->u.import->recordType);
396 case Z_ESAdminOriginPartToKeep_refresh:
397 yaz_log(LOG_LOG, "adm-refresh");
399 case Z_ESAdminOriginPartToKeep_commit:
400 yaz_log(LOG_LOG, "adm-commit");
402 case Z_ESAdminOriginPartToKeep_shutdown:
403 yaz_log(LOG_LOG, "shutdown");
404 zebra_admin_shutdown(zh);
406 case Z_ESAdminOriginPartToKeep_start:
407 yaz_log(LOG_LOG, "start");
408 zebra_admin_start(zh);
411 yaz_log(LOG_LOG, "unknown admin");
413 if (r->toKeep->databaseName)
415 yaz_log(LOG_LOG, "database %s", r->toKeep->databaseName);
420 static int es_admin (ZebraHandle zh, Z_Admin *r)
424 case Z_Admin_esRequest:
425 es_admin_request (zh, r->u.esRequest);
427 case Z_Admin_taskPackage:
428 yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
437 int bend_segment (void *handle, bend_segment_rr *rr)
439 ZebraHandle zh = (ZebraHandle) handle;
440 Z_Segment *segment = rr->segment;
442 if (segment->num_segmentRecords)
443 zebra_admin_import_segment (zh, rr->segment);
445 zebra_admin_import_end (zh);
449 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
451 ZebraHandle zh = (ZebraHandle) handle;
453 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
454 if (rr->esr->packageName)
455 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
456 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
458 if (!rr->esr->taskSpecificParameters)
460 yaz_log (LOG_WARN, "No task specific parameters");
462 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
464 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
466 zebra_result (zh, &rr->errcode, &rr->errstring);
468 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
470 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
471 yaz_log (LOG_LOG, "Received DB Update");
472 if (up->which == Z_IUUpdate_esRequest)
474 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
475 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
476 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
478 yaz_log (LOG_LOG, "action");
481 switch (*toKeep->action)
483 case Z_IUOriginPartToKeep_recordInsert:
484 yaz_log (LOG_LOG, "recordInsert");
486 case Z_IUOriginPartToKeep_recordReplace:
487 yaz_log (LOG_LOG, "recordUpdate");
489 case Z_IUOriginPartToKeep_recordDelete:
490 yaz_log (LOG_LOG, "recordDelete");
492 case Z_IUOriginPartToKeep_elementUpdate:
493 yaz_log (LOG_LOG, "elementUpdate");
495 case Z_IUOriginPartToKeep_specialUpdate:
496 yaz_log (LOG_LOG, "specialUpdate");
498 case Z_ESAdminOriginPartToKeep_shutdown:
499 yaz_log (LOG_LOG, "shutDown");
501 case Z_ESAdminOriginPartToKeep_start:
502 yaz_log (LOG_LOG, "start");
505 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
508 if (toKeep->databaseName)
510 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
512 if (zebra_select_database(zh, toKeep->databaseName))
517 yaz_log (LOG_WARN, "no database supplied for ES Update");
519 rr->errstring = "database";
525 zebra_begin_trans (zh, 1);
526 for (i = 0; i < notToKeep->num; i++)
528 Z_External *rec = notToKeep->elements[i]->record;
529 struct oident *oident = 0;
530 Odr_oct *recid = notToKeep->elements[i]->u.opaque;
535 rr->errstring = "record Id not supplied";
538 if (notToKeep->elements[i]->which !=
539 Z_IUSuppliedRecords_elem_opaque)
542 rr->errstring = "only opaque record ID supported";
546 if (rec->direct_reference)
548 oident = oid_getentbyoid(rec->direct_reference);
550 yaz_log (LOG_LOG, "record %d type %s", i,
555 case Z_External_sutrs:
556 if (rec->u.octet_aligned->len > 170)
557 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
561 yaz_log (LOG_LOG, "%d bytes:\n%s",
565 case Z_External_octet:
566 if (rec->u.octet_aligned->len > 170)
567 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
568 rec->u.octet_aligned->len,
569 rec->u.octet_aligned->buf);
571 yaz_log (LOG_LOG, "%d bytes\n%s",
572 rec->u.octet_aligned->len,
573 rec->u.octet_aligned->buf);
575 if (oident && oident->value != VAL_TEXT_XML)
578 rr->errstring = "only XML update supported";
581 if (rec->which == Z_External_octet)
585 if (*toKeep->action ==
586 Z_IUOriginPartToKeep_recordInsert)
588 if (*toKeep->action ==
589 Z_IUOriginPartToKeep_recordReplace)
591 if (*toKeep->action ==
592 Z_IUOriginPartToKeep_recordDelete)
594 if (*toKeep->action ==
595 Z_IUOriginPartToKeep_specialUpdate)
601 rr->errstring = "unsupported ES Update action";
606 int r = zebra_admin_exchange_record (
607 zh, toKeep->databaseName,
608 rec->u.octet_aligned->buf,
609 rec->u.octet_aligned->len,
610 recid->buf, recid->len,
612 if (r && *toKeep->action ==
613 Z_IUOriginPartToKeep_specialUpdate)
615 r = zebra_admin_exchange_record (
616 zh, toKeep->databaseName,
617 rec->u.octet_aligned->buf,
618 rec->u.octet_aligned->len,
619 recid->buf, recid->len,
625 rr->errstring = "record exchange failed";
631 zebra_end_trans (zh);
637 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
638 rr->esr->taskSpecificParameters->which);
645 static void bend_start (struct statserv_options_block *sob)
648 zebra_stop((ZebraService) sob->handle);
649 sob->handle = zebra_start(sob->configname);
652 yaz_log (LOG_FATAL, "Failed to read config `%s'", sob->configname);
664 zebra_pidfname(sob->handle, pidfname);
666 fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
671 yaz_log(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfname);
674 fd = open(pidfname, O_RDWR, 0666);
677 yaz_log(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfname);
681 area.l_type = F_WRLCK;
682 area.l_whence = SEEK_SET;
683 area.l_len = area.l_start = 0L;
684 if (fcntl (fd, F_SETLK, &area) == -1)
686 yaz_log(LOG_ERRNO|LOG_FATAL, "Zebra server already running");
693 sprintf (pidstr, "%ld", (long) getpid ());
694 write (fd, pidstr, strlen(pidstr));
700 static void bend_stop(struct statserv_options_block *sob)
705 if (!sob->inetd && sob->handle)
708 zebra_pidfname(sob->handle, pidfname);
714 ZebraService service = sob->handle;
719 int main (int argc, char **argv)
721 struct statserv_options_block *sob;
723 sob = statserv_getcontrol ();
724 strcpy (sob->configname, "zebra.cfg");
725 sob->bend_start = bend_start;
726 sob->bend_stop = bend_stop;
728 strcpy (sob->service_display_name, "Zebra Server");
730 statserv_setcontrol (sob);
732 return statserv_main (argc, argv, bend_init, bend_close);