1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 Index Data
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
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
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
27 #include <sys/locking.h>
36 #include <yaz/yaz-util.h>
37 #include <yaz/diagbib1.h>
39 #include <sys/types.h>
41 #include <yaz/backend.h>
42 #include <yaz/charneg.h>
43 #include <idzebra/api.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;
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 zebra_get_version(version_str, 0);
75 q->implementation_name = "Zebra Information Server";
76 q->implementation_version = odr_strdup(q->stream, version_str);
78 yaz_log(YLOG_DEBUG, "bend_init");
80 sob = statserv_getcontrol();
81 if (!(zh = zebra_open(sob->handle, 0)))
83 yaz_log(YLOG_WARN, "Failed to read config `%s'", sob->configname);
84 r->errcode = YAZ_BIB1_PERMANENT_SYSTEM_ERROR;
89 q->query_charset = odr_strdup(q->stream, zebra_get_encoding(zh));
92 if (q->auth->which == Z_IdAuthentication_open)
94 char *openpass = xstrdup(q->auth->u.open);
95 char *cp = strchr(openpass, '/');
99 user = nmem_strdup(odr_getmem(q->stream), openpass);
100 passwd = nmem_strdup(odr_getmem(q->stream), cp+1);
104 else if (q->auth->which == Z_IdAuthentication_idPass)
106 Z_IdPass *idPass = q->auth->u.idPass;
108 user = idPass->userId;
109 passwd = idPass->password;
112 if (zebra_auth(zh, user, passwd) != ZEBRA_OK)
114 r->errcode = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
118 if (q->charneg_request) /* characater set and language negotiation? */
126 NMEM nmem = nmem_create();
128 yaz_get_proposal_charneg(nmem, q->charneg_request,
129 &charsets, &num_charsets,
130 &langs, &num_langs, &selected);
132 for (i = 0; i < num_charsets; i++)
134 const char *right_name = "";
136 * FIXME! It is like rudiment :-))
137 * We have to support this short names of character sets,
138 * because a lot servers in Russia to use own in during
139 * character set and language negotiation still.
142 if (!yaz_matchstr(charsets[i], "win")) {
143 right_name = "WINDOWS-1251";
144 } else if (!yaz_matchstr(charsets[i], "koi")) {
145 right_name = "KOI8-R";
146 } else if (!yaz_matchstr(charsets[i], "iso")) {
147 right_name = "ISO-8859-5";
148 } else if (!yaz_matchstr(charsets[i], "dos")) {
149 right_name = "CP866";
150 } else if (!yaz_matchstr(charsets[i], "uni")) {
151 right_name = "UTF-8";
153 right_name = charsets[i];
155 if (odr_set_charset(q->decode, "UTF-8", right_name) == 0)
157 yaz_log(YLOG_LOG, "charset %d %s (proper name %s): OK", i,
158 charsets[i], right_name);
159 odr_set_charset(q->stream, right_name, "UTF-8");
161 zebra_record_encoding(zh, right_name);
162 zebra_octet_term_encoding(zh, right_name);
163 q->charneg_response =
164 yaz_set_response_charneg(q->stream, charsets[i],
168 yaz_log(YLOG_LOG, "charset %d %s (proper name %s): unsupported", i,
169 charsets[i], right_name);
177 static void search_terms(ZebraHandle zh, bend_search_rr *r)
181 int type = Z_Term_general;
182 struct Z_External *ext;
183 Z_SearchInfoReport *sr;
185 zebra_result_set_term_no(zh, r->setname, &no_terms);
189 r->search_info = odr_malloc(r->stream, sizeof(*r->search_info));
191 r->search_info->num_elements = 1;
192 r->search_info->list =
193 odr_malloc(r->stream, sizeof(*r->search_info->list));
194 r->search_info->list[0] =
195 odr_malloc(r->stream, sizeof(**r->search_info->list));
196 r->search_info->list[0]->category = 0;
197 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
198 ext = odr_malloc(r->stream, sizeof(*ext));
199 r->search_info->list[0]->information.externallyDefinedInfo = ext;
200 ext->direct_reference = odr_oiddup(r->stream,
201 yaz_oid_userinfo_searchresult_1);
202 ext->indirect_reference = 0;
204 ext->which = Z_External_searchResult1;
205 sr = odr_malloc(r->stream, sizeof(Z_SearchInfoReport));
206 ext->u.searchResult1 = sr;
208 sr->elements = odr_malloc(r->stream, sr->num *
209 sizeof(*sr->elements));
210 for (i = 0; i<no_terms; i++)
212 Z_SearchInfoReport_s *se;
217 size_t len = sizeof(outbuf);
218 const char *term_ref_id = 0;
220 zebra_result_set_term_info(zh, r->setname, i,
221 &count, &approx, outbuf, &len,
223 se = sr->elements[i] = odr_malloc(r->stream, sizeof(**sr->elements));
224 se->subqueryId = term_ref_id ?
225 odr_strdup(r->stream, term_ref_id) : 0;
227 se->fullQuery = odr_booldup(r->stream, 0);
228 se->subqueryExpression =
229 odr_malloc(r->stream, sizeof(Z_QueryExpression));
230 se->subqueryExpression->which =
231 Z_QueryExpression_term;
232 se->subqueryExpression->u.term =
233 odr_malloc(r->stream, sizeof(Z_QueryExpressionTerm));
234 term = odr_malloc(r->stream, sizeof(Z_Term));
235 se->subqueryExpression->u.term->queryTerm = term;
238 case Z_Term_characterString:
239 term->which = Z_Term_characterString;
240 term->u.characterString = odr_strdup(r->stream, outbuf);
243 term->which = Z_Term_general;
244 term->u.general = odr_malloc(r->stream, sizeof(*term->u.general));
245 term->u.general->size = term->u.general->len = len;
246 term->u.general->buf = odr_malloc(r->stream, len);
247 memcpy(term->u.general->buf, outbuf, len);
250 term->which = Z_Term_general;
251 term->u.null = odr_nullval();
253 se->subqueryExpression->u.term->termComment = 0;
254 se->subqueryInterpretation = 0;
255 se->subqueryRecommendation = 0;
256 if (count > 2147483646)
258 se->subqueryCount = odr_intdup(r->stream, CAST_ZINT_TO_INT(count));
259 se->subqueryWeight = 0;
265 static int break_handler(void *client_data)
267 bend_association assoc =(bend_association) client_data;
268 if (!bend_assoc_is_alive(assoc))
273 int bend_search(void *handle, bend_search_rr *r)
275 ZebraHandle zh = (ZebraHandle) handle;
279 res = zebra_select_databases(zh, r->num_bases,
280 (const char **) r->basenames);
283 zebra_result(zh, &r->errcode, &r->errstring);
286 zebra_set_break_handler(zh, break_handler, r->association);
287 yaz_log(YLOG_DEBUG, "ResultSet '%s'", r->setname);
288 switch (r->query->which)
290 case Z_Query_type_1: case Z_Query_type_101:
291 res = zebra_search_RPN_x(zh, r->stream, r->query->u.type_1,
293 &r->estimated_hit_count,
294 &r->partial_resultset);
296 zebra_result(zh, &r->errcode, &r->errstring);
299 if (zhits > 2147483646)
301 r->hits = CAST_ZINT_TO_INT(zhits);
306 r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
307 r->errstring = "type-2";
310 r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
312 zebra_set_break_handler(zh, 0, 0);
317 int bend_fetch(void *handle, bend_fetch_rr *r)
319 ZebraHandle zh = (ZebraHandle) handle;
320 ZebraRetrievalRecord retrievalRecord;
323 retrievalRecord.position = r->number;
326 res = zebra_records_retrieve(zh, r->stream, r->setname, r->comp,
327 r->request_format, 1, &retrievalRecord);
330 /* non-surrogate diagnostic */
331 zebra_result(zh, &r->errcode, &r->errstring);
333 else if (retrievalRecord.errCode)
335 /* surrogate diagnostic (diagnostic per record) */
336 r->surrogate_flag = 1;
337 r->errcode = retrievalRecord.errCode;
338 r->errstring = retrievalRecord.errString;
339 r->basename = retrievalRecord.base;
343 r->basename = retrievalRecord.base;
344 r->record = retrievalRecord.buf;
345 r->len = retrievalRecord.len;
346 r->output_format = odr_oiddup(r->stream, retrievalRecord.format);
351 static int bend_scan(void *handle, bend_scan_rr *r)
353 ZebraScanEntry *entries;
354 ZebraHandle zh = (ZebraHandle) handle;
358 res = zebra_select_databases(zh, r->num_bases,
359 (const char **) r->basenames);
362 zebra_result(zh, &r->errcode, &r->errstring);
365 if (r->step_size != 0 && *r->step_size != 0) {
366 r->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
370 res = zebra_scan(zh, r->stream, r->term,
373 &r->num_entries, &entries, &is_partial,
378 r->status = BEND_SCAN_PARTIAL;
380 r->status = BEND_SCAN_SUCCESS;
381 for (i = 0; i < r->num_entries; i++)
383 r->entries[i].term = entries[i].term;
384 r->entries[i].display_term = entries[i].display_term;
385 r->entries[i].occurrences =
386 CAST_ZINT_TO_INT(entries[i].occurrences);
391 r->status = BEND_SCAN_PARTIAL;
392 zebra_result(zh, &r->errcode, &r->errstring);
397 void bend_close(void *handle)
399 zebra_close((ZebraHandle) handle);
400 xmalloc_trav("bend_close");
403 int bend_sort(void *handle, bend_sort_rr *rr)
405 ZebraHandle zh = (ZebraHandle) handle;
407 if (zebra_sort(zh, rr->stream,
408 rr->num_input_setnames, (const char **) rr->input_setnames,
409 rr->output_setname, rr->sort_sequence, &rr->sort_status)
411 zebra_result(zh, &rr->errcode, &rr->errstring);
415 int bend_delete(void *handle, bend_delete_rr *rr)
417 ZebraHandle zh = (ZebraHandle) handle;
419 rr->delete_status = zebra_deleteResultSet(zh, rr->function,
420 rr->num_setnames, rr->setnames,
425 static void es_admin_request(bend_esrequest_rr *rr, ZebraHandle zh, Z_AdminEsRequest *r)
427 ZEBRA_RES res = ZEBRA_OK;
428 if (r->toKeep->databaseName)
430 yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
432 switch (r->toKeep->which)
434 case Z_ESAdminOriginPartToKeep_reIndex:
435 yaz_log(YLOG_LOG, "adm-reindex");
436 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
437 rr->errstring = "adm-reindex not implemented yet";
439 case Z_ESAdminOriginPartToKeep_truncate:
440 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
441 rr->errstring = "adm-reindex not implemented yet";
442 yaz_log(YLOG_LOG, "adm-truncate");
444 case Z_ESAdminOriginPartToKeep_drop:
445 yaz_log(YLOG_LOG, "adm-drop");
446 res = zebra_drop_database(zh, r->toKeep->databaseName);
448 case Z_ESAdminOriginPartToKeep_create:
449 yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
450 res = zebra_create_database(zh, r->toKeep->databaseName);
452 case Z_ESAdminOriginPartToKeep_import:
453 yaz_log(YLOG_LOG, "adm-import");
454 res = zebra_admin_import_begin(zh, r->toKeep->databaseName,
455 r->toKeep->u.import->recordType);
457 case Z_ESAdminOriginPartToKeep_refresh:
458 yaz_log(YLOG_LOG, "adm-refresh");
460 case Z_ESAdminOriginPartToKeep_commit:
461 yaz_log(YLOG_LOG, "adm-commit");
462 if (r->toKeep->databaseName)
464 if (zebra_select_database(zh, r->toKeep->databaseName) !=
466 yaz_log(YLOG_WARN, "zebra_select_database failed in "
471 case Z_ESAdminOriginPartToKeep_shutdown:
472 yaz_log(YLOG_LOG, "shutdown");
473 res = zebra_admin_shutdown(zh);
475 case Z_ESAdminOriginPartToKeep_start:
476 yaz_log(YLOG_LOG, "start");
477 zebra_admin_start(zh);
480 yaz_log(YLOG_LOG, "unknown admin");
481 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
482 rr->errstring = "adm-reindex not implemented yet";
485 zebra_result(zh, &rr->errcode, &rr->errstring);
488 static void es_admin(bend_esrequest_rr *rr, ZebraHandle zh, Z_Admin *r)
492 case Z_Admin_esRequest:
493 es_admin_request(rr, zh, r->u.esRequest);
498 yaz_log(YLOG_WARN, "adm taskpackage (unhandled)");
499 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
500 rr->errstring = "adm-task package (unhandled)";
503 int bend_segment(void *handle, bend_segment_rr *rr)
505 ZebraHandle zh = (ZebraHandle) handle;
506 Z_Segment *segment = rr->segment;
508 if (segment->num_segmentRecords)
509 zebra_admin_import_segment(zh, rr->segment);
511 zebra_admin_import_end(zh);
515 int bend_esrequest(void *handle, bend_esrequest_rr *rr)
517 ZebraHandle zh = (ZebraHandle) handle;
519 yaz_log(YLOG_LOG, "function: " ODR_INT_PRINTF, *rr->esr->function);
520 if (rr->esr->packageName)
521 yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
522 yaz_log(YLOG_LOG, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
524 if (!rr->esr->taskSpecificParameters)
526 yaz_log(YLOG_WARN, "No task specific parameters");
528 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
530 es_admin(rr, zh, rr->esr->taskSpecificParameters->u.adminService);
532 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
534 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
535 yaz_log(YLOG_LOG, "Received DB Update");
536 if (up->which == Z_IUUpdate_esRequest)
538 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
539 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
540 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
542 yaz_log(YLOG_LOG, "action");
545 switch (*toKeep->action)
547 case Z_IUOriginPartToKeep_recordInsert:
548 yaz_log(YLOG_LOG, "recordInsert");
550 case Z_IUOriginPartToKeep_recordReplace:
551 yaz_log(YLOG_LOG, "recordUpdate");
553 case Z_IUOriginPartToKeep_recordDelete:
554 yaz_log(YLOG_LOG, "recordDelete");
556 case Z_IUOriginPartToKeep_elementUpdate:
557 yaz_log(YLOG_LOG, "elementUpdate");
559 case Z_IUOriginPartToKeep_specialUpdate:
560 yaz_log(YLOG_LOG, "specialUpdate");
562 case Z_ESAdminOriginPartToKeep_shutdown:
563 yaz_log(YLOG_LOG, "shutDown");
565 case Z_ESAdminOriginPartToKeep_start:
566 yaz_log(YLOG_LOG, "start");
569 yaz_log(YLOG_LOG, " unknown (" ODR_INT_PRINTF ")",
573 if (toKeep->databaseName)
575 yaz_log(YLOG_LOG, "database: %s", toKeep->databaseName);
577 if (zebra_select_database(zh, toKeep->databaseName))
582 yaz_log(YLOG_WARN, "no database supplied for ES Update");
584 YAZ_BIB1_ES_MISSING_MANDATORY_PARAMETER_FOR_SPECIFIED_FUNCTION_;
585 rr->errstring = "database";
588 if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
590 zebra_result(zh, &rr->errcode, &rr->errstring);
595 for (i = 0; notToKeep && i < notToKeep->num; i++)
597 Z_External *rec = notToKeep->elements[i]->record;
598 Odr_oct *opaque_recid = 0;
602 if (notToKeep->elements[i]->u.opaque)
604 switch(notToKeep->elements[i]->which)
606 case Z_IUSuppliedRecords_elem_opaque:
607 opaque_recid = notToKeep->elements[i]->u.opaque;
608 break; /* OK, recid already set */
609 case Z_IUSuppliedRecords_elem_number:
610 sysno_tmp = *notToKeep->elements[i]->u.number;
615 if (rec->direct_reference)
617 char oid_name_str[OID_STR_MAX];
618 const char *oid_name =
619 yaz_oid_to_string_buf(
620 rec->direct_reference,
623 yaz_log(YLOG_LOG, "record %d type %s", i,
628 case Z_External_sutrs:
629 if (rec->u.octet_aligned->len > 170)
630 yaz_log(YLOG_LOG, "%d bytes:\n%.168s ...",
634 yaz_log(YLOG_LOG, "%d bytes:\n%s",
638 case Z_External_octet:
639 if (rec->u.octet_aligned->len > 170)
640 yaz_log(YLOG_LOG, "%d bytes:\n%.168s ...",
641 rec->u.octet_aligned->len,
642 rec->u.octet_aligned->buf);
644 yaz_log(YLOG_LOG, "%d bytes\n%s",
645 rec->u.octet_aligned->len,
646 rec->u.octet_aligned->buf);
648 if (rec->which == Z_External_octet)
650 enum zebra_recctrl_action_t action = action_update;
652 const char *match_criteria = 0;
655 if (*toKeep->action ==
656 Z_IUOriginPartToKeep_recordInsert)
657 action = action_insert;
658 else if (*toKeep->action ==
659 Z_IUOriginPartToKeep_recordReplace)
660 action = action_replace;
661 else if (*toKeep->action ==
662 Z_IUOriginPartToKeep_recordDelete)
663 action = action_delete;
664 else if (*toKeep->action ==
665 Z_IUOriginPartToKeep_specialUpdate)
666 action = action_update;
670 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
671 rr->errstring = "unsupported ES Update action";
677 size_t l = opaque_recid->len;
678 if (l >= sizeof(recid_str))
680 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
681 rr->errstring = "opaque record ID too large";
684 memcpy(recid_str, opaque_recid->buf, l);
686 match_criteria = recid_str;
688 res = zebra_update_record(
691 sysno, match_criteria, 0, /* fname */
692 (const char *) rec->u.octet_aligned->buf,
693 rec->u.octet_aligned->len);
694 if (res == ZEBRA_FAIL)
697 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
698 rr->errstring = "update_record failed";
702 if (zebra_end_trans(zh) != ZEBRA_OK)
704 yaz_log(YLOG_WARN, "zebra_end_trans failed for"
705 " extended service operation");
712 yaz_log(YLOG_WARN, "Unknown Extended Service(%d)",
713 rr->esr->taskSpecificParameters->which);
714 rr->errcode = YAZ_BIB1_ES_EXTENDED_SERVICE_TYPE_UNSUPP;
720 static void bend_start(struct statserv_options_block *sob)
722 Res default_res = res_open(0, 0);
725 zebra_stop((ZebraService) sob->handle);
726 res_set(default_res, "profilePath", DEFAULT_PROFILE_PATH);
727 res_set(default_res, "modulePath", DEFAULT_MODULE_PATH);
728 sob->handle = zebra_start_res(sob->configname, default_res, 0);
729 res_close(default_res);
732 yaz_log(YLOG_FATAL, "Failed to read config `%s'", sob->configname);
738 if (!sob->inetd && !sob->background)
744 zebra_pidfname(sob->handle, pidfname);
746 fd = open(pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
751 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
754 fd = open(pidfname, O_RDWR, 0666);
757 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
761 area.l_type = F_WRLCK;
762 area.l_whence = SEEK_SET;
763 area.l_len = area.l_start = 0L;
764 if (fcntl(fd, F_SETLK, &area) == -1)
766 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
773 sprintf(pidstr, "%ld", (long) getpid());
774 if (write(fd, pidstr, strlen(pidstr)) != strlen(pidstr))
776 yaz_log(YLOG_ERRNO|YLOG_FATAL, "write fail %s", pidfname);
784 static void bend_stop(struct statserv_options_block *sob)
789 if (!sob->inetd && !sob->background && sob->handle)
792 zebra_pidfname(sob->handle, pidfname);
798 ZebraService service = sob->handle;
803 int main(int argc, char **argv)
805 struct statserv_options_block *sob;
807 sob = statserv_getcontrol();
808 strcpy(sob->configname, "zebra.cfg");
809 sob->bend_start = bend_start;
810 sob->bend_stop = bend_stop;
812 strcpy(sob->service_name, "zebrasrv");
813 strcpy(sob->service_display_name, "Zebra Server");
815 statserv_setcontrol(sob);
817 return statserv_main(argc, argv, bend_init, bend_close);
822 * c-file-style: "Stroustrup"
823 * indent-tabs-mode: nil
825 * vim: shiftwidth=4 tabstop=8 expandtab