1 /* $Id: zserver.c,v 1.135 2005-06-13 10:29:20 adam Exp $
2 Copyright (C) 1995-2005
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
30 #include <sys/locking.h>
38 #include <yaz/yaz-util.h>
39 #include <yaz/diagbib1.h>
41 #include <sys/types.h>
45 static int bend_sort (void *handle, bend_sort_rr *rr);
46 static int bend_delete (void *handle, bend_delete_rr *rr);
47 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
48 static int bend_segment (void *handle, bend_segment_rr *rr);
49 static int bend_search (void *handle, bend_search_rr *r);
50 static int bend_fetch (void *handle, bend_fetch_rr *r);
51 static int bend_scan (void *handle, bend_scan_rr *r);
53 bend_initresult *bend_init (bend_initrequest *q)
55 bend_initresult *r = (bend_initresult *)
56 odr_malloc (q->stream, sizeof(*r));
58 struct statserv_options_block *sob;
64 q->bend_sort = bend_sort;
65 q->bend_delete = bend_delete;
66 q->bend_esrequest = bend_esrequest;
67 q->bend_segment = bend_segment;
68 q->bend_search = bend_search;
69 q->bend_fetch = bend_fetch;
70 q->bend_scan = bend_scan;
72 q->implementation_name = "Zebra Information Server";
73 q->implementation_version = "Zebra " ZEBRAVER;
75 yaz_log (YLOG_DEBUG, "bend_init");
77 sob = statserv_getcontrol ();
78 if (!(zh = zebra_open (sob->handle)))
80 yaz_log (YLOG_WARN, "Failed to read config `%s'", sob->configname);
81 r->errcode = YAZ_BIB1_PERMANENT_SYSTEM_ERROR;
87 if (q->auth->which == Z_IdAuthentication_open)
89 char *openpass = xstrdup (q->auth->u.open);
90 char *cp = strchr (openpass, '/');
94 user = nmem_strdup (odr_getmem (q->stream), openpass);
95 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
99 else if (q->auth->which == Z_IdAuthentication_idPass)
101 Z_IdPass *idPass = q->auth->u.idPass;
103 user = idPass->userId;
104 passwd = idPass->password;
107 if (zebra_auth(zh, user, passwd) != ZEBRA_OK)
109 r->errcode = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
113 if (q->charneg_request) /* characater set and langauge negotiation? */
121 NMEM nmem = nmem_create();
123 yaz_log (YLOG_LOG, "character set and language negotiation");
125 yaz_get_proposal_charneg (nmem, q->charneg_request,
126 &charsets, &num_charsets,
127 &langs, &num_langs, &selected);
129 for (i = 0; i < num_charsets; i++)
131 const char *right_name = "";
133 * FIXME! It is like rudiment :-))
134 * We have to support this short names of character sets,
135 * because a lot servers in Russia to use own in during
136 * character set and language negotiation still.
139 if (!yaz_matchstr(charsets[i], "win")) {
140 right_name = "WINDOWS-1251";
141 } else if (!yaz_matchstr(charsets[i], "koi")) {
142 right_name = "KOI8-R";
143 } else if (!yaz_matchstr(charsets[i], "iso")) {
144 right_name = "ISO-8859-5";
145 } else if (!yaz_matchstr(charsets[i], "dos")) {
146 right_name = "CP866";
147 } else if (!yaz_matchstr(charsets[i], "uni")) {
148 right_name = "UTF-8";
150 right_name = charsets[i];
152 if (odr_set_charset (q->decode, "UTF-8", right_name) == 0)
154 yaz_log (YLOG_LOG, "charset %d %s (proper name %s): OK", i,
155 charsets[i], right_name);
156 odr_set_charset (q->stream, right_name, "UTF-8");
158 zebra_record_encoding(zh, right_name);
159 zebra_octet_term_encoding(zh, right_name);
160 q->charneg_response =
161 yaz_set_response_charneg (q->stream, charsets[i],
165 yaz_log (YLOG_LOG, "charset %d %s (proper name %s): unsupported", i,
166 charsets[i], right_name);
174 static void search_terms(ZebraHandle zh, bend_search_rr *r)
178 int type = Z_Term_general;
179 struct Z_External *ext;
180 Z_SearchInfoReport *sr;
182 zebra_result_set_term_no(zh, r->setname, &no_terms);
186 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
188 r->search_info->num_elements = 1;
189 r->search_info->list =
190 odr_malloc (r->stream, sizeof(*r->search_info->list));
191 r->search_info->list[0] =
192 odr_malloc (r->stream, sizeof(**r->search_info->list));
193 r->search_info->list[0]->category = 0;
194 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
195 ext = odr_malloc (r->stream, sizeof(*ext));
196 r->search_info->list[0]->information.externallyDefinedInfo = ext;
197 ext->direct_reference =
198 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
199 ext->indirect_reference = 0;
201 ext->which = Z_External_searchResult1;
202 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
203 ext->u.searchResult1 = sr;
205 sr->elements = odr_malloc (r->stream, sr->num *
206 sizeof(*sr->elements));
207 for (i = 0; i<no_terms; i++)
213 size_t len = sizeof(outbuf);
215 zebra_result_set_term_info(zh, r->setname, i,
216 &count, &approx, outbuf, &len);
218 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
219 sr->elements[i]->subqueryId = 0;
220 sr->elements[i]->fullQuery = odr_malloc (r->stream,
222 *sr->elements[i]->fullQuery = 0;
223 sr->elements[i]->subqueryExpression =
224 odr_malloc (r->stream, sizeof(Z_QueryExpression));
225 sr->elements[i]->subqueryExpression->which =
226 Z_QueryExpression_term;
227 sr->elements[i]->subqueryExpression->u.term =
228 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
229 term = odr_malloc (r->stream, sizeof(Z_Term));
230 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
233 case Z_Term_characterString:
234 yaz_log (YLOG_DEBUG, "term as characterString");
235 term->which = Z_Term_characterString;
236 term->u.characterString = odr_strdup (r->stream, outbuf);
239 yaz_log (YLOG_DEBUG, "term as general");
240 term->which = Z_Term_general;
241 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
242 term->u.general->size = term->u.general->len = len;
243 term->u.general->buf = odr_malloc (r->stream, len);
244 memcpy (term->u.general->buf, outbuf, len);
247 term->which = Z_Term_general;
248 term->u.null = odr_nullval();
250 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
251 sr->elements[i]->subqueryInterpretation = 0;
252 sr->elements[i]->subqueryRecommendation = 0;
253 if (count > 2000000000)
255 sr->elements[i]->subqueryCount = odr_intdup (r->stream, (int) count);
256 sr->elements[i]->subqueryWeight = 0;
257 sr->elements[i]->resultsByDB = 0;
261 int bend_search (void *handle, bend_search_rr *r)
263 ZebraHandle zh = (ZebraHandle) handle;
267 res = zebra_select_databases (zh, r->num_bases,
268 (const char **) r->basenames);
271 zebra_result (zh, &r->errcode, &r->errstring);
274 yaz_log (YLOG_DEBUG, "ResultSet '%s'", r->setname);
275 switch (r->query->which)
277 case Z_Query_type_1: case Z_Query_type_101:
278 res = zebra_search_RPN(zh, r->stream, r->query->u.type_1,
281 zebra_result(zh, &r->errcode, &r->errstring);
284 if (zhits > 2147483646)
285 r->hits = 2147483647;
287 r->hits = (int) zhits;
288 search_terms (zh, r);
292 r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
293 r->errstring = "type-2";
296 r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
302 int bend_fetch (void *handle, bend_fetch_rr *r)
304 ZebraHandle zh = (ZebraHandle) handle;
305 ZebraRetrievalRecord retrievalRecord;
308 retrievalRecord.position = r->number;
311 res = zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
312 r->request_format, 1, &retrievalRecord);
315 /* non-surrogate diagnostic */
316 zebra_result (zh, &r->errcode, &r->errstring);
318 else if (retrievalRecord.errCode)
320 /* surrogate diagnostic (diagnostic per record) */
321 r->surrogate_flag = 1;
322 r->errcode = retrievalRecord.errCode;
323 r->errstring = retrievalRecord.errString;
324 r->basename = retrievalRecord.base;
328 r->basename = retrievalRecord.base;
329 r->record = retrievalRecord.buf;
330 r->len = retrievalRecord.len;
331 r->output_format = retrievalRecord.format;
336 static int bend_scan (void *handle, bend_scan_rr *r)
338 ZebraScanEntry *entries;
339 ZebraHandle zh = (ZebraHandle) handle;
343 res = zebra_select_databases(zh, r->num_bases,
344 (const char **) r->basenames);
347 zebra_result (zh, &r->errcode, &r->errstring);
350 if (r->step_size != 0 && *r->step_size != 0) {
351 r->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
355 r->entries = (struct scan_entry *)
356 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
357 res = zebra_scan(zh, r->stream, r->term,
360 &r->num_entries, &entries, &is_partial);
364 r->status = BEND_SCAN_PARTIAL;
366 r->status = BEND_SCAN_SUCCESS;
367 for (i = 0; i < r->num_entries; i++)
369 r->entries[i].term = entries[i].term;
370 r->entries[i].occurrences = entries[i].occurrences;
375 r->status = BEND_SCAN_PARTIAL;
376 zebra_result(zh, &r->errcode, &r->errstring);
381 void bend_close (void *handle)
383 zebra_close ((ZebraHandle) handle);
384 xmalloc_trav("bend_close");
388 int bend_sort (void *handle, bend_sort_rr *rr)
390 ZebraHandle zh = (ZebraHandle) handle;
392 zebra_sort (zh, rr->stream,
393 rr->num_input_setnames, (const char **) rr->input_setnames,
394 rr->output_setname, rr->sort_sequence, &rr->sort_status);
395 zebra_result (zh, &rr->errcode, &rr->errstring);
399 int bend_delete (void *handle, bend_delete_rr *rr)
401 ZebraHandle zh = (ZebraHandle) handle;
403 rr->delete_status = zebra_deleteResultSet(zh, rr->function,
404 rr->num_setnames, rr->setnames,
409 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
411 if (r->toKeep->databaseName)
413 yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
415 switch (r->toKeep->which)
417 case Z_ESAdminOriginPartToKeep_reIndex:
418 yaz_log(YLOG_LOG, "adm-reindex");
420 case Z_ESAdminOriginPartToKeep_truncate:
421 yaz_log(YLOG_LOG, "adm-truncate");
423 case Z_ESAdminOriginPartToKeep_drop:
424 yaz_log(YLOG_LOG, "adm-drop");
425 zebra_drop_database (zh, r->toKeep->databaseName);
427 case Z_ESAdminOriginPartToKeep_create:
428 yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
429 zebra_create_database (zh, r->toKeep->databaseName);
431 case Z_ESAdminOriginPartToKeep_import:
432 yaz_log(YLOG_LOG, "adm-import");
433 zebra_admin_import_begin (zh, r->toKeep->databaseName,
434 r->toKeep->u.import->recordType);
436 case Z_ESAdminOriginPartToKeep_refresh:
437 yaz_log(YLOG_LOG, "adm-refresh");
439 case Z_ESAdminOriginPartToKeep_commit:
440 yaz_log(YLOG_LOG, "adm-commit");
441 if (r->toKeep->databaseName)
442 zebra_select_database(zh, r->toKeep->databaseName);
445 case Z_ESAdminOriginPartToKeep_shutdown:
446 yaz_log(YLOG_LOG, "shutdown");
447 zebra_admin_shutdown(zh);
449 case Z_ESAdminOriginPartToKeep_start:
450 yaz_log(YLOG_LOG, "start");
451 zebra_admin_start(zh);
454 yaz_log(YLOG_LOG, "unknown admin");
459 static int es_admin (ZebraHandle zh, Z_Admin *r)
463 case Z_Admin_esRequest:
464 es_admin_request (zh, r->u.esRequest);
466 case Z_Admin_taskPackage:
467 yaz_log (YLOG_LOG, "adm taskpackage (unhandled)");
475 int bend_segment (void *handle, bend_segment_rr *rr)
477 ZebraHandle zh = (ZebraHandle) handle;
478 Z_Segment *segment = rr->segment;
480 if (segment->num_segmentRecords)
481 zebra_admin_import_segment (zh, rr->segment);
483 zebra_admin_import_end (zh);
487 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
489 ZebraHandle zh = (ZebraHandle) handle;
491 yaz_log(YLOG_LOG, "function: %d", *rr->esr->function);
492 if (rr->esr->packageName)
493 yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
494 yaz_log(YLOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
496 if (!rr->esr->taskSpecificParameters)
498 yaz_log (YLOG_WARN, "No task specific parameters");
500 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
502 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
504 zebra_result (zh, &rr->errcode, &rr->errstring);
506 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
508 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
509 yaz_log (YLOG_LOG, "Received DB Update");
510 if (up->which == Z_IUUpdate_esRequest)
512 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
513 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
514 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
516 yaz_log (YLOG_LOG, "action");
519 switch (*toKeep->action)
521 case Z_IUOriginPartToKeep_recordInsert:
522 yaz_log (YLOG_LOG, "recordInsert");
524 case Z_IUOriginPartToKeep_recordReplace:
525 yaz_log (YLOG_LOG, "recordUpdate");
527 case Z_IUOriginPartToKeep_recordDelete:
528 yaz_log (YLOG_LOG, "recordDelete");
530 case Z_IUOriginPartToKeep_elementUpdate:
531 yaz_log (YLOG_LOG, "elementUpdate");
533 case Z_IUOriginPartToKeep_specialUpdate:
534 yaz_log (YLOG_LOG, "specialUpdate");
536 case Z_ESAdminOriginPartToKeep_shutdown:
537 yaz_log (YLOG_LOG, "shutDown");
539 case Z_ESAdminOriginPartToKeep_start:
540 yaz_log (YLOG_LOG, "start");
543 yaz_log (YLOG_LOG, " unknown (%d)", *toKeep->action);
546 if (toKeep->databaseName)
548 yaz_log (YLOG_LOG, "database: %s", toKeep->databaseName);
550 if (zebra_select_database(zh, toKeep->databaseName))
555 yaz_log (YLOG_WARN, "no database supplied for ES Update");
557 YAZ_BIB1_ES_MISSING_MANDATORY_PARAMETER_FOR_SPECIFIED_FUNCTION_;
558 rr->errstring = "database";
561 if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
563 zebra_result(zh, &rr->errcode, &rr->errstring);
568 for (i = 0; notToKeep && i < notToKeep->num; i++)
570 Z_External *rec = notToKeep->elements[i]->record;
571 struct oident *oident = 0;
572 Odr_oct *opaque_recid = 0;
575 if (notToKeep->elements[i]->u.opaque)
577 switch(notToKeep->elements[i]->which)
579 case Z_IUSuppliedRecords_elem_opaque:
580 opaque_recid = notToKeep->elements[i]->u.opaque;
581 break; /* OK, recid already set */
582 case Z_IUSuppliedRecords_elem_number:
583 sysno = *notToKeep->elements[i]->u.number;
587 if (rec->direct_reference)
589 oident = oid_getentbyoid(rec->direct_reference);
591 yaz_log (YLOG_LOG, "record %d type %s", i,
596 case Z_External_sutrs:
597 if (rec->u.octet_aligned->len > 170)
598 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
602 yaz_log (YLOG_LOG, "%d bytes:\n%s",
606 case Z_External_octet:
607 if (rec->u.octet_aligned->len > 170)
608 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
609 rec->u.octet_aligned->len,
610 rec->u.octet_aligned->buf);
612 yaz_log (YLOG_LOG, "%d bytes\n%s",
613 rec->u.octet_aligned->len,
614 rec->u.octet_aligned->buf);
616 if (oident && oident->value != VAL_TEXT_XML)
618 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
619 rr->errstring = "only XML update supported";
622 if (rec->which == Z_External_octet)
626 if (*toKeep->action ==
627 Z_IUOriginPartToKeep_recordInsert)
629 if (*toKeep->action ==
630 Z_IUOriginPartToKeep_recordReplace)
632 if (*toKeep->action ==
633 Z_IUOriginPartToKeep_recordDelete)
635 if (*toKeep->action ==
636 Z_IUOriginPartToKeep_specialUpdate)
642 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
643 rr->errstring = "unsupported ES Update action";
646 else if (opaque_recid)
648 int r = zebra_admin_exchange_record (
650 rec->u.octet_aligned->buf,
651 rec->u.octet_aligned->len,
652 opaque_recid->buf, opaque_recid->len,
657 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
658 rr->errstring = "record exchange failed";
664 ZEBRA_RES r = ZEBRA_FAIL;
667 r = zebra_insert_record(
673 rec->u.octet_aligned->buf,
674 rec->u.octet_aligned->len,
679 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
680 rr->errstring = "insert_record failed";
685 r = zebra_update_record(
691 rec->u.octet_aligned->buf,
692 rec->u.octet_aligned->len,
697 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
698 rr->errstring = "update_record failed";
702 r = zebra_delete_record(
708 rec->u.octet_aligned->buf,
709 rec->u.octet_aligned->len,
714 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
715 rr->errstring = "delete_record failed";
722 zebra_end_trans (zh);
728 yaz_log (YLOG_WARN, "Unknown Extended Service(%d)",
729 rr->esr->taskSpecificParameters->which);
730 rr->errcode = YAZ_BIB1_ES_EXTENDED_SERVICE_TYPE_UNSUPP;
736 static void bend_start (struct statserv_options_block *sob)
739 zebra_stop((ZebraService) sob->handle);
740 sob->handle = zebra_start(sob->configname);
743 yaz_log (YLOG_FATAL, "Failed to read config `%s'", sob->configname);
755 zebra_pidfname(sob->handle, pidfname);
757 fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
762 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
765 fd = open(pidfname, O_RDWR, 0666);
768 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
772 area.l_type = F_WRLCK;
773 area.l_whence = SEEK_SET;
774 area.l_len = area.l_start = 0L;
775 if (fcntl (fd, F_SETLK, &area) == -1)
777 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
784 sprintf (pidstr, "%ld", (long) getpid ());
785 write (fd, pidstr, strlen(pidstr));
791 static void bend_stop(struct statserv_options_block *sob)
796 if (!sob->inetd && sob->handle)
799 zebra_pidfname(sob->handle, pidfname);
805 ZebraService service = sob->handle;
810 int main (int argc, char **argv)
812 struct statserv_options_block *sob;
814 sob = statserv_getcontrol ();
815 strcpy (sob->configname, "zebra.cfg");
816 sob->bend_start = bend_start;
817 sob->bend_stop = bend_stop;
819 strcpy (sob->service_display_name, "Zebra Server");
821 statserv_setcontrol (sob);
823 return statserv_main (argc, argv, bend_init, bend_close);