1 /* $Id: zserver.c,v 1.146 2006-05-10 08:13:23 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>
39 #include <yaz/yaz-util.h>
40 #include <yaz/diagbib1.h>
42 #include <sys/types.h>
46 static int bend_sort (void *handle, bend_sort_rr *rr);
47 static int bend_delete (void *handle, bend_delete_rr *rr);
48 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
49 static int bend_segment (void *handle, bend_segment_rr *rr);
50 static int bend_search (void *handle, bend_search_rr *r);
51 static int bend_fetch (void *handle, bend_fetch_rr *r);
52 static int bend_scan (void *handle, bend_scan_rr *r);
54 bend_initresult *bend_init (bend_initrequest *q)
56 bend_initresult *r = (bend_initresult *)
57 odr_malloc (q->stream, sizeof(*r));
59 struct statserv_options_block *sob;
65 q->bend_sort = bend_sort;
66 q->bend_delete = bend_delete;
67 q->bend_esrequest = bend_esrequest;
68 q->bend_segment = bend_segment;
69 q->bend_search = bend_search;
70 q->bend_fetch = bend_fetch;
71 q->bend_scan = bend_scan;
73 q->implementation_name = "Zebra Information Server";
74 q->implementation_version = "Zebra " ZEBRAVER;
76 yaz_log (YLOG_DEBUG, "bend_init");
78 sob = statserv_getcontrol ();
79 if (!(zh = zebra_open (sob->handle, 0)))
81 yaz_log (YLOG_WARN, "Failed to read config `%s'", sob->configname);
82 r->errcode = YAZ_BIB1_PERMANENT_SYSTEM_ERROR;
88 if (q->auth->which == Z_IdAuthentication_open)
90 char *openpass = xstrdup (q->auth->u.open);
91 char *cp = strchr (openpass, '/');
95 user = nmem_strdup (odr_getmem (q->stream), openpass);
96 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
100 else if (q->auth->which == Z_IdAuthentication_idPass)
102 Z_IdPass *idPass = q->auth->u.idPass;
104 user = idPass->userId;
105 passwd = idPass->password;
108 if (zebra_auth(zh, user, passwd) != ZEBRA_OK)
110 r->errcode = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
114 if (q->charneg_request) /* characater set and langauge negotiation? */
122 NMEM nmem = nmem_create();
124 yaz_log (YLOG_LOG, "character set and language negotiation");
126 yaz_get_proposal_charneg (nmem, q->charneg_request,
127 &charsets, &num_charsets,
128 &langs, &num_langs, &selected);
130 for (i = 0; i < num_charsets; i++)
132 const char *right_name = "";
134 * FIXME! It is like rudiment :-))
135 * We have to support this short names of character sets,
136 * because a lot servers in Russia to use own in during
137 * character set and language negotiation still.
140 if (!yaz_matchstr(charsets[i], "win")) {
141 right_name = "WINDOWS-1251";
142 } else if (!yaz_matchstr(charsets[i], "koi")) {
143 right_name = "KOI8-R";
144 } else if (!yaz_matchstr(charsets[i], "iso")) {
145 right_name = "ISO-8859-5";
146 } else if (!yaz_matchstr(charsets[i], "dos")) {
147 right_name = "CP866";
148 } else if (!yaz_matchstr(charsets[i], "uni")) {
149 right_name = "UTF-8";
151 right_name = charsets[i];
153 if (odr_set_charset (q->decode, "UTF-8", right_name) == 0)
155 yaz_log (YLOG_LOG, "charset %d %s (proper name %s): OK", i,
156 charsets[i], right_name);
157 odr_set_charset (q->stream, right_name, "UTF-8");
159 zebra_record_encoding(zh, right_name);
160 zebra_octet_term_encoding(zh, right_name);
161 q->charneg_response =
162 yaz_set_response_charneg (q->stream, charsets[i],
166 yaz_log (YLOG_LOG, "charset %d %s (proper name %s): unsupported", i,
167 charsets[i], right_name);
175 static void search_terms(ZebraHandle zh, bend_search_rr *r)
179 int type = Z_Term_general;
180 struct Z_External *ext;
181 Z_SearchInfoReport *sr;
183 zebra_result_set_term_no(zh, r->setname, &no_terms);
187 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
189 r->search_info->num_elements = 1;
190 r->search_info->list =
191 odr_malloc (r->stream, sizeof(*r->search_info->list));
192 r->search_info->list[0] =
193 odr_malloc (r->stream, sizeof(**r->search_info->list));
194 r->search_info->list[0]->category = 0;
195 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
196 ext = odr_malloc (r->stream, sizeof(*ext));
197 r->search_info->list[0]->information.externallyDefinedInfo = ext;
198 ext->direct_reference =
199 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
200 ext->indirect_reference = 0;
202 ext->which = Z_External_searchResult1;
203 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
204 ext->u.searchResult1 = sr;
206 sr->elements = odr_malloc (r->stream, sr->num *
207 sizeof(*sr->elements));
208 for (i = 0; i<no_terms; i++)
210 Z_SearchInfoReport_s *se;
215 size_t len = sizeof(outbuf);
216 const char *term_ref_id = 0;
218 zebra_result_set_term_info(zh, r->setname, i,
219 &count, &approx, outbuf, &len,
221 se = sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
222 se->subqueryId = term_ref_id ?
223 odr_strdup(r->stream, term_ref_id) : 0;
225 se->fullQuery = odr_intdup(r->stream, 0);
226 se->subqueryExpression =
227 odr_malloc (r->stream, sizeof(Z_QueryExpression));
228 se->subqueryExpression->which =
229 Z_QueryExpression_term;
230 se->subqueryExpression->u.term =
231 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
232 term = odr_malloc (r->stream, sizeof(Z_Term));
233 se->subqueryExpression->u.term->queryTerm = term;
236 case Z_Term_characterString:
237 term->which = Z_Term_characterString;
238 term->u.characterString = odr_strdup (r->stream, outbuf);
241 term->which = Z_Term_general;
242 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
243 term->u.general->size = term->u.general->len = len;
244 term->u.general->buf = odr_malloc (r->stream, len);
245 memcpy (term->u.general->buf, outbuf, len);
248 term->which = Z_Term_general;
249 term->u.null = odr_nullval();
251 se->subqueryExpression->u.term->termComment = 0;
252 se->subqueryInterpretation = 0;
253 se->subqueryRecommendation = 0;
254 if (count > 2000000000)
256 se->subqueryCount = odr_intdup(r->stream, (int) count);
257 se->subqueryWeight = 0;
262 int bend_search (void *handle, bend_search_rr *r)
264 ZebraHandle zh = (ZebraHandle) handle;
268 res = zebra_select_databases (zh, r->num_bases,
269 (const char **) r->basenames);
272 zebra_result (zh, &r->errcode, &r->errstring);
275 yaz_log (YLOG_DEBUG, "ResultSet '%s'", r->setname);
276 switch (r->query->which)
278 case Z_Query_type_1: case Z_Query_type_101:
279 res = zebra_search_RPN(zh, r->stream, r->query->u.type_1,
282 zebra_result(zh, &r->errcode, &r->errstring);
285 if (zhits > 2147483646)
286 r->hits = 2147483647;
288 r->hits = (int) zhits;
289 search_terms (zh, r);
293 r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
294 r->errstring = "type-2";
297 r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
303 int bend_fetch (void *handle, bend_fetch_rr *r)
305 ZebraHandle zh = (ZebraHandle) handle;
306 ZebraRetrievalRecord retrievalRecord;
309 retrievalRecord.position = r->number;
312 res = zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
313 r->request_format, 1, &retrievalRecord);
316 /* non-surrogate diagnostic */
317 zebra_result (zh, &r->errcode, &r->errstring);
319 else if (retrievalRecord.errCode)
321 /* surrogate diagnostic (diagnostic per record) */
322 r->surrogate_flag = 1;
323 r->errcode = retrievalRecord.errCode;
324 r->errstring = retrievalRecord.errString;
325 r->basename = retrievalRecord.base;
329 r->basename = retrievalRecord.base;
330 r->record = retrievalRecord.buf;
331 r->len = retrievalRecord.len;
332 r->output_format = retrievalRecord.format;
337 static int bend_scan (void *handle, bend_scan_rr *r)
339 ZebraScanEntry *entries;
340 ZebraHandle zh = (ZebraHandle) handle;
344 res = zebra_select_databases(zh, r->num_bases,
345 (const char **) r->basenames);
348 zebra_result (zh, &r->errcode, &r->errstring);
351 if (r->step_size != 0 && *r->step_size != 0) {
352 r->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
356 r->entries = (struct scan_entry *)
357 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
358 res = zebra_scan(zh, r->stream, r->term,
361 &r->num_entries, &entries, &is_partial,
366 r->status = BEND_SCAN_PARTIAL;
368 r->status = BEND_SCAN_SUCCESS;
369 for (i = 0; i < r->num_entries; i++)
371 r->entries[i].term = entries[i].term;
372 r->entries[i].occurrences = entries[i].occurrences;
377 r->status = BEND_SCAN_PARTIAL;
378 zebra_result(zh, &r->errcode, &r->errstring);
383 void bend_close (void *handle)
385 zebra_close ((ZebraHandle) handle);
386 xmalloc_trav("bend_close");
390 int bend_sort (void *handle, bend_sort_rr *rr)
392 ZebraHandle zh = (ZebraHandle) handle;
394 if (zebra_sort (zh, rr->stream,
395 rr->num_input_setnames, (const char **) rr->input_setnames,
396 rr->output_setname, rr->sort_sequence, &rr->sort_status)
398 zebra_result (zh, &rr->errcode, &rr->errstring);
402 int bend_delete (void *handle, bend_delete_rr *rr)
404 ZebraHandle zh = (ZebraHandle) handle;
406 rr->delete_status = zebra_deleteResultSet(zh, rr->function,
407 rr->num_setnames, rr->setnames,
412 static void es_admin_request (bend_esrequest_rr *rr, ZebraHandle zh, Z_AdminEsRequest *r)
414 ZEBRA_RES res = ZEBRA_OK;
415 if (r->toKeep->databaseName)
417 yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
419 switch (r->toKeep->which)
421 case Z_ESAdminOriginPartToKeep_reIndex:
422 yaz_log(YLOG_LOG, "adm-reindex");
423 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
424 rr->errstring = "adm-reindex not implemented yet";
426 case Z_ESAdminOriginPartToKeep_truncate:
427 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
428 rr->errstring = "adm-reindex not implemented yet";
429 yaz_log(YLOG_LOG, "adm-truncate");
431 case Z_ESAdminOriginPartToKeep_drop:
432 yaz_log(YLOG_LOG, "adm-drop");
433 res = zebra_drop_database (zh, r->toKeep->databaseName);
435 case Z_ESAdminOriginPartToKeep_create:
436 yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
437 res = zebra_create_database (zh, r->toKeep->databaseName);
439 case Z_ESAdminOriginPartToKeep_import:
440 yaz_log(YLOG_LOG, "adm-import");
441 res = zebra_admin_import_begin (zh, r->toKeep->databaseName,
442 r->toKeep->u.import->recordType);
444 case Z_ESAdminOriginPartToKeep_refresh:
445 yaz_log(YLOG_LOG, "adm-refresh");
447 case Z_ESAdminOriginPartToKeep_commit:
448 yaz_log(YLOG_LOG, "adm-commit");
449 if (r->toKeep->databaseName)
451 if (zebra_select_database(zh, r->toKeep->databaseName) !=
453 yaz_log(YLOG_WARN, "zebra_select_database failed in "
458 case Z_ESAdminOriginPartToKeep_shutdown:
459 yaz_log(YLOG_LOG, "shutdown");
460 res = zebra_admin_shutdown(zh);
462 case Z_ESAdminOriginPartToKeep_start:
463 yaz_log(YLOG_LOG, "start");
464 zebra_admin_start(zh);
467 yaz_log(YLOG_LOG, "unknown admin");
468 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
469 rr->errstring = "adm-reindex not implemented yet";
472 zebra_result(zh, &rr->errcode, &rr->errstring);
475 static void es_admin (bend_esrequest_rr *rr, ZebraHandle zh, Z_Admin *r)
479 case Z_Admin_esRequest:
480 es_admin_request (rr, zh, r->u.esRequest);
485 yaz_log (YLOG_WARN, "adm taskpackage (unhandled)");
486 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
487 rr->errstring = "adm-task package (unhandled)";
490 int bend_segment (void *handle, bend_segment_rr *rr)
492 ZebraHandle zh = (ZebraHandle) handle;
493 Z_Segment *segment = rr->segment;
495 if (segment->num_segmentRecords)
496 zebra_admin_import_segment (zh, rr->segment);
498 zebra_admin_import_end (zh);
502 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
504 ZebraHandle zh = (ZebraHandle) handle;
506 yaz_log(YLOG_LOG, "function: %d", *rr->esr->function);
507 if (rr->esr->packageName)
508 yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
509 yaz_log(YLOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
511 if (!rr->esr->taskSpecificParameters)
513 yaz_log (YLOG_WARN, "No task specific parameters");
515 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
517 es_admin(rr, zh, rr->esr->taskSpecificParameters->u.adminService);
519 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
521 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
522 yaz_log (YLOG_LOG, "Received DB Update");
523 if (up->which == Z_IUUpdate_esRequest)
525 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
526 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
527 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
529 yaz_log (YLOG_LOG, "action");
532 switch (*toKeep->action)
534 case Z_IUOriginPartToKeep_recordInsert:
535 yaz_log (YLOG_LOG, "recordInsert");
537 case Z_IUOriginPartToKeep_recordReplace:
538 yaz_log (YLOG_LOG, "recordUpdate");
540 case Z_IUOriginPartToKeep_recordDelete:
541 yaz_log (YLOG_LOG, "recordDelete");
543 case Z_IUOriginPartToKeep_elementUpdate:
544 yaz_log (YLOG_LOG, "elementUpdate");
546 case Z_IUOriginPartToKeep_specialUpdate:
547 yaz_log (YLOG_LOG, "specialUpdate");
549 case Z_ESAdminOriginPartToKeep_shutdown:
550 yaz_log (YLOG_LOG, "shutDown");
552 case Z_ESAdminOriginPartToKeep_start:
553 yaz_log (YLOG_LOG, "start");
556 yaz_log (YLOG_LOG, " unknown (%d)", *toKeep->action);
559 if (toKeep->databaseName)
561 yaz_log (YLOG_LOG, "database: %s", toKeep->databaseName);
563 if (zebra_select_database(zh, toKeep->databaseName))
568 yaz_log (YLOG_WARN, "no database supplied for ES Update");
570 YAZ_BIB1_ES_MISSING_MANDATORY_PARAMETER_FOR_SPECIFIED_FUNCTION_;
571 rr->errstring = "database";
574 if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
576 zebra_result(zh, &rr->errcode, &rr->errstring);
581 for (i = 0; notToKeep && i < notToKeep->num; i++)
583 Z_External *rec = notToKeep->elements[i]->record;
584 struct oident *oident = 0;
585 Odr_oct *opaque_recid = 0;
589 if (notToKeep->elements[i]->u.opaque)
591 switch(notToKeep->elements[i]->which)
593 case Z_IUSuppliedRecords_elem_opaque:
594 opaque_recid = notToKeep->elements[i]->u.opaque;
595 break; /* OK, recid already set */
596 case Z_IUSuppliedRecords_elem_number:
597 sysno_tmp = *notToKeep->elements[i]->u.number;
602 if (rec->direct_reference)
604 oident = oid_getentbyoid(rec->direct_reference);
606 yaz_log (YLOG_LOG, "record %d type %s", i,
611 case Z_External_sutrs:
612 if (rec->u.octet_aligned->len > 170)
613 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
617 yaz_log (YLOG_LOG, "%d bytes:\n%s",
621 case Z_External_octet:
622 if (rec->u.octet_aligned->len > 170)
623 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
624 rec->u.octet_aligned->len,
625 rec->u.octet_aligned->buf);
627 yaz_log (YLOG_LOG, "%d bytes\n%s",
628 rec->u.octet_aligned->len,
629 rec->u.octet_aligned->buf);
631 if (oident && oident->value != VAL_TEXT_XML)
633 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
634 rr->errstring = "only XML update supported";
637 if (rec->which == Z_External_octet)
641 if (*toKeep->action ==
642 Z_IUOriginPartToKeep_recordInsert)
644 if (*toKeep->action ==
645 Z_IUOriginPartToKeep_recordReplace)
647 if (*toKeep->action ==
648 Z_IUOriginPartToKeep_recordDelete)
650 if (*toKeep->action ==
651 Z_IUOriginPartToKeep_specialUpdate)
657 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
658 rr->errstring = "unsupported ES Update action";
661 else if (opaque_recid)
663 int r = zebra_admin_exchange_record (
665 (const char *) rec->u.octet_aligned->buf,
666 rec->u.octet_aligned->len,
667 (const char *) opaque_recid->buf,
672 zebra_result(zh, &rr->errcode,
679 ZEBRA_RES r = ZEBRA_FAIL;
682 r = zebra_insert_record(
688 (const char *) rec->u.octet_aligned->buf,
689 rec->u.octet_aligned->len,
694 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
695 rr->errstring = "insert_record failed";
700 r = zebra_update_record(
706 (const char *) rec->u.octet_aligned->buf,
707 rec->u.octet_aligned->len,
712 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
713 rr->errstring = "update_record failed";
717 r = zebra_delete_record(
723 (const char *) rec->u.octet_aligned->buf,
724 rec->u.octet_aligned->len,
729 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
730 rr->errstring = "delete_record failed";
737 if (zebra_end_trans (zh) != ZEBRA_OK)
739 yaz_log(YLOG_WARN, "zebra_end_trans failed for"
740 " extended service operation");
747 yaz_log (YLOG_WARN, "Unknown Extended Service(%d)",
748 rr->esr->taskSpecificParameters->which);
749 rr->errcode = YAZ_BIB1_ES_EXTENDED_SERVICE_TYPE_UNSUPP;
755 static void bend_start (struct statserv_options_block *sob)
758 zebra_stop((ZebraService) sob->handle);
759 sob->handle = zebra_start(sob->configname);
762 yaz_log (YLOG_FATAL, "Failed to read config `%s'", sob->configname);
774 zebra_pidfname(sob->handle, pidfname);
776 fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
781 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
784 fd = open(pidfname, O_RDWR, 0666);
787 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
791 area.l_type = F_WRLCK;
792 area.l_whence = SEEK_SET;
793 area.l_len = area.l_start = 0L;
794 if (fcntl (fd, F_SETLK, &area) == -1)
796 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
803 sprintf (pidstr, "%ld", (long) getpid ());
804 write (fd, pidstr, strlen(pidstr));
810 static void bend_stop(struct statserv_options_block *sob)
815 if (!sob->inetd && sob->handle)
818 zebra_pidfname(sob->handle, pidfname);
824 ZebraService service = sob->handle;
829 int main (int argc, char **argv)
831 struct statserv_options_block *sob;
833 sob = statserv_getcontrol ();
834 strcpy (sob->configname, "zebra.cfg");
835 sob->bend_start = bend_start;
836 sob->bend_stop = bend_stop;
838 strcpy (sob->service_display_name, "Zebra Server");
840 statserv_setcontrol (sob);
842 return statserv_main (argc, argv, bend_init, bend_close);
847 * indent-tabs-mode: nil
849 * vim: shiftwidth=4 tabstop=8 expandtab