1 /* $Id: zebrasrv.c,v 1.19 2007-08-27 17:22:22 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/locking.h>
39 #include <yaz/yaz-util.h>
40 #include <yaz/diagbib1.h>
42 #include <sys/types.h>
44 #include <yaz/backend.h>
45 #include <yaz/charneg.h>
46 #include <idzebra/api.h>
48 static int bend_sort (void *handle, bend_sort_rr *rr);
49 static int bend_delete (void *handle, bend_delete_rr *rr);
50 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
51 static int bend_segment (void *handle, bend_segment_rr *rr);
52 static int bend_search (void *handle, bend_search_rr *r);
53 static int bend_fetch (void *handle, bend_fetch_rr *r);
54 static int bend_scan (void *handle, bend_scan_rr *r);
56 bend_initresult *bend_init (bend_initrequest *q)
58 bend_initresult *r = (bend_initresult *)
59 odr_malloc (q->stream, sizeof(*r));
61 struct statserv_options_block *sob;
68 q->bend_sort = bend_sort;
69 q->bend_delete = bend_delete;
70 q->bend_esrequest = bend_esrequest;
71 q->bend_segment = bend_segment;
72 q->bend_search = bend_search;
73 q->bend_fetch = bend_fetch;
74 q->bend_scan = bend_scan;
76 zebra_get_version(version_str, 0);
78 q->implementation_name = "Zebra Information Server";
79 q->implementation_version = odr_strdup(q->stream, version_str);
81 yaz_log (YLOG_DEBUG, "bend_init");
83 sob = statserv_getcontrol ();
84 if (!(zh = zebra_open (sob->handle, 0)))
86 yaz_log (YLOG_WARN, "Failed to read config `%s'", sob->configname);
87 r->errcode = YAZ_BIB1_PERMANENT_SYSTEM_ERROR;
92 q->query_charset = odr_strdup(q->stream, zebra_get_encoding(zh));
95 if (q->auth->which == Z_IdAuthentication_open)
97 char *openpass = xstrdup (q->auth->u.open);
98 char *cp = strchr (openpass, '/');
102 user = nmem_strdup (odr_getmem (q->stream), openpass);
103 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
107 else if (q->auth->which == Z_IdAuthentication_idPass)
109 Z_IdPass *idPass = q->auth->u.idPass;
111 user = idPass->userId;
112 passwd = idPass->password;
115 if (zebra_auth(zh, user, passwd) != ZEBRA_OK)
117 r->errcode = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
121 if (q->charneg_request) /* characater set and language negotiation? */
129 NMEM nmem = nmem_create();
131 yaz_get_proposal_charneg(nmem, q->charneg_request,
132 &charsets, &num_charsets,
133 &langs, &num_langs, &selected);
135 for (i = 0; i < num_charsets; i++)
137 const char *right_name = "";
139 * FIXME! It is like rudiment :-))
140 * We have to support this short names of character sets,
141 * because a lot servers in Russia to use own in during
142 * character set and language negotiation still.
145 if (!yaz_matchstr(charsets[i], "win")) {
146 right_name = "WINDOWS-1251";
147 } else if (!yaz_matchstr(charsets[i], "koi")) {
148 right_name = "KOI8-R";
149 } else if (!yaz_matchstr(charsets[i], "iso")) {
150 right_name = "ISO-8859-5";
151 } else if (!yaz_matchstr(charsets[i], "dos")) {
152 right_name = "CP866";
153 } else if (!yaz_matchstr(charsets[i], "uni")) {
154 right_name = "UTF-8";
156 right_name = charsets[i];
158 if (odr_set_charset(q->decode, "UTF-8", right_name) == 0)
160 yaz_log(YLOG_LOG, "charset %d %s (proper name %s): OK", i,
161 charsets[i], right_name);
162 odr_set_charset(q->stream, right_name, "UTF-8");
164 zebra_record_encoding(zh, right_name);
165 zebra_octet_term_encoding(zh, right_name);
166 q->charneg_response =
167 yaz_set_response_charneg(q->stream, charsets[i],
171 yaz_log(YLOG_LOG, "charset %d %s (proper name %s): unsupported", i,
172 charsets[i], right_name);
180 static void search_terms(ZebraHandle zh, bend_search_rr *r)
184 int type = Z_Term_general;
185 struct Z_External *ext;
186 Z_SearchInfoReport *sr;
188 zebra_result_set_term_no(zh, r->setname, &no_terms);
192 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
194 r->search_info->num_elements = 1;
195 r->search_info->list =
196 odr_malloc (r->stream, sizeof(*r->search_info->list));
197 r->search_info->list[0] =
198 odr_malloc (r->stream, sizeof(**r->search_info->list));
199 r->search_info->list[0]->category = 0;
200 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
201 ext = odr_malloc (r->stream, sizeof(*ext));
202 r->search_info->list[0]->information.externallyDefinedInfo = ext;
203 ext->direct_reference = odr_oiddup(r->stream,
204 yaz_oid_userinfo_searchresult_1);
205 ext->indirect_reference = 0;
207 ext->which = Z_External_searchResult1;
208 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
209 ext->u.searchResult1 = sr;
211 sr->elements = odr_malloc (r->stream, sr->num *
212 sizeof(*sr->elements));
213 for (i = 0; i<no_terms; i++)
215 Z_SearchInfoReport_s *se;
220 size_t len = sizeof(outbuf);
221 const char *term_ref_id = 0;
223 zebra_result_set_term_info(zh, r->setname, i,
224 &count, &approx, outbuf, &len,
226 se = sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
227 se->subqueryId = term_ref_id ?
228 odr_strdup(r->stream, term_ref_id) : 0;
230 se->fullQuery = odr_intdup(r->stream, 0);
231 se->subqueryExpression =
232 odr_malloc (r->stream, sizeof(Z_QueryExpression));
233 se->subqueryExpression->which =
234 Z_QueryExpression_term;
235 se->subqueryExpression->u.term =
236 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
237 term = odr_malloc (r->stream, sizeof(Z_Term));
238 se->subqueryExpression->u.term->queryTerm = term;
241 case Z_Term_characterString:
242 term->which = Z_Term_characterString;
243 term->u.characterString = odr_strdup (r->stream, outbuf);
246 term->which = Z_Term_general;
247 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
248 term->u.general->size = term->u.general->len = len;
249 term->u.general->buf = odr_malloc (r->stream, len);
250 memcpy (term->u.general->buf, outbuf, len);
253 term->which = Z_Term_general;
254 term->u.null = odr_nullval();
256 se->subqueryExpression->u.term->termComment = 0;
257 se->subqueryInterpretation = 0;
258 se->subqueryRecommendation = 0;
259 if (count > 2147483646)
261 se->subqueryCount = odr_intdup(r->stream, CAST_ZINT_TO_INT(count));
262 se->subqueryWeight = 0;
268 static int break_handler(void *client_data)
270 bend_association assoc =(bend_association) client_data;
271 if (!bend_assoc_is_alive(assoc))
276 int bend_search(void *handle, bend_search_rr *r)
278 ZebraHandle zh = (ZebraHandle) handle;
282 res = zebra_select_databases (zh, r->num_bases,
283 (const char **) r->basenames);
286 zebra_result (zh, &r->errcode, &r->errstring);
289 zebra_set_break_handler(zh, break_handler, r->association);
290 yaz_log (YLOG_DEBUG, "ResultSet '%s'", r->setname);
291 switch (r->query->which)
293 case Z_Query_type_1: case Z_Query_type_101:
294 res = zebra_search_RPN_x(zh, r->stream, r->query->u.type_1,
296 &r->estimated_hit_count,
297 &r->partial_resultset);
299 zebra_result(zh, &r->errcode, &r->errstring);
302 if (zhits > 2147483646)
304 r->hits = CAST_ZINT_TO_INT(zhits);
305 search_terms (zh, r);
309 r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
310 r->errstring = "type-2";
313 r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
315 zebra_set_break_handler(zh, 0, 0);
320 int bend_fetch(void *handle, bend_fetch_rr *r)
322 ZebraHandle zh = (ZebraHandle) handle;
323 ZebraRetrievalRecord retrievalRecord;
326 retrievalRecord.position = r->number;
329 res = zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
330 r->request_format, 1, &retrievalRecord);
333 /* non-surrogate diagnostic */
334 zebra_result (zh, &r->errcode, &r->errstring);
336 else if (retrievalRecord.errCode)
338 /* surrogate diagnostic (diagnostic per record) */
339 r->surrogate_flag = 1;
340 r->errcode = retrievalRecord.errCode;
341 r->errstring = retrievalRecord.errString;
342 r->basename = retrievalRecord.base;
346 r->basename = retrievalRecord.base;
347 r->record = retrievalRecord.buf;
348 r->len = retrievalRecord.len;
349 r->output_format = odr_oiddup(r->stream, retrievalRecord.format);
354 static int bend_scan (void *handle, bend_scan_rr *r)
356 ZebraScanEntry *entries;
357 ZebraHandle zh = (ZebraHandle) handle;
361 res = zebra_select_databases(zh, r->num_bases,
362 (const char **) r->basenames);
365 zebra_result (zh, &r->errcode, &r->errstring);
368 if (r->step_size != 0 && *r->step_size != 0) {
369 r->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
373 res = zebra_scan(zh, r->stream, r->term,
376 &r->num_entries, &entries, &is_partial,
381 r->status = BEND_SCAN_PARTIAL;
383 r->status = BEND_SCAN_SUCCESS;
384 for (i = 0; i < r->num_entries; i++)
386 r->entries[i].term = entries[i].term;
387 r->entries[i].display_term = entries[i].display_term;
388 r->entries[i].occurrences =
389 CAST_ZINT_TO_INT(entries[i].occurrences);
394 r->status = BEND_SCAN_PARTIAL;
395 zebra_result(zh, &r->errcode, &r->errstring);
400 void bend_close (void *handle)
402 zebra_close ((ZebraHandle) handle);
403 xmalloc_trav("bend_close");
406 int bend_sort (void *handle, bend_sort_rr *rr)
408 ZebraHandle zh = (ZebraHandle) handle;
410 if (zebra_sort (zh, rr->stream,
411 rr->num_input_setnames, (const char **) rr->input_setnames,
412 rr->output_setname, rr->sort_sequence, &rr->sort_status)
414 zebra_result (zh, &rr->errcode, &rr->errstring);
418 int bend_delete (void *handle, bend_delete_rr *rr)
420 ZebraHandle zh = (ZebraHandle) handle;
422 rr->delete_status = zebra_deleteResultSet(zh, rr->function,
423 rr->num_setnames, rr->setnames,
428 static void es_admin_request (bend_esrequest_rr *rr, ZebraHandle zh, Z_AdminEsRequest *r)
430 ZEBRA_RES res = ZEBRA_OK;
431 if (r->toKeep->databaseName)
433 yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
435 switch (r->toKeep->which)
437 case Z_ESAdminOriginPartToKeep_reIndex:
438 yaz_log(YLOG_LOG, "adm-reindex");
439 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
440 rr->errstring = "adm-reindex not implemented yet";
442 case Z_ESAdminOriginPartToKeep_truncate:
443 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
444 rr->errstring = "adm-reindex not implemented yet";
445 yaz_log(YLOG_LOG, "adm-truncate");
447 case Z_ESAdminOriginPartToKeep_drop:
448 yaz_log(YLOG_LOG, "adm-drop");
449 res = zebra_drop_database (zh, r->toKeep->databaseName);
451 case Z_ESAdminOriginPartToKeep_create:
452 yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
453 res = zebra_create_database (zh, r->toKeep->databaseName);
455 case Z_ESAdminOriginPartToKeep_import:
456 yaz_log(YLOG_LOG, "adm-import");
457 res = zebra_admin_import_begin (zh, r->toKeep->databaseName,
458 r->toKeep->u.import->recordType);
460 case Z_ESAdminOriginPartToKeep_refresh:
461 yaz_log(YLOG_LOG, "adm-refresh");
463 case Z_ESAdminOriginPartToKeep_commit:
464 yaz_log(YLOG_LOG, "adm-commit");
465 if (r->toKeep->databaseName)
467 if (zebra_select_database(zh, r->toKeep->databaseName) !=
469 yaz_log(YLOG_WARN, "zebra_select_database failed in "
474 case Z_ESAdminOriginPartToKeep_shutdown:
475 yaz_log(YLOG_LOG, "shutdown");
476 res = zebra_admin_shutdown(zh);
478 case Z_ESAdminOriginPartToKeep_start:
479 yaz_log(YLOG_LOG, "start");
480 zebra_admin_start(zh);
483 yaz_log(YLOG_LOG, "unknown admin");
484 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
485 rr->errstring = "adm-reindex not implemented yet";
488 zebra_result(zh, &rr->errcode, &rr->errstring);
491 static void es_admin (bend_esrequest_rr *rr, ZebraHandle zh, Z_Admin *r)
495 case Z_Admin_esRequest:
496 es_admin_request (rr, zh, r->u.esRequest);
501 yaz_log (YLOG_WARN, "adm taskpackage (unhandled)");
502 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
503 rr->errstring = "adm-task package (unhandled)";
506 int bend_segment (void *handle, bend_segment_rr *rr)
508 ZebraHandle zh = (ZebraHandle) handle;
509 Z_Segment *segment = rr->segment;
511 if (segment->num_segmentRecords)
512 zebra_admin_import_segment (zh, rr->segment);
514 zebra_admin_import_end (zh);
518 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
520 ZebraHandle zh = (ZebraHandle) handle;
522 yaz_log(YLOG_LOG, "function: %d", *rr->esr->function);
523 if (rr->esr->packageName)
524 yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
525 yaz_log(YLOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
527 if (!rr->esr->taskSpecificParameters)
529 yaz_log (YLOG_WARN, "No task specific parameters");
531 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
533 es_admin(rr, zh, rr->esr->taskSpecificParameters->u.adminService);
535 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
537 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
538 yaz_log (YLOG_LOG, "Received DB Update");
539 if (up->which == Z_IUUpdate_esRequest)
541 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
542 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
543 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
545 yaz_log (YLOG_LOG, "action");
548 switch (*toKeep->action)
550 case Z_IUOriginPartToKeep_recordInsert:
551 yaz_log (YLOG_LOG, "recordInsert");
553 case Z_IUOriginPartToKeep_recordReplace:
554 yaz_log (YLOG_LOG, "recordUpdate");
556 case Z_IUOriginPartToKeep_recordDelete:
557 yaz_log (YLOG_LOG, "recordDelete");
559 case Z_IUOriginPartToKeep_elementUpdate:
560 yaz_log (YLOG_LOG, "elementUpdate");
562 case Z_IUOriginPartToKeep_specialUpdate:
563 yaz_log (YLOG_LOG, "specialUpdate");
565 case Z_ESAdminOriginPartToKeep_shutdown:
566 yaz_log (YLOG_LOG, "shutDown");
568 case Z_ESAdminOriginPartToKeep_start:
569 yaz_log (YLOG_LOG, "start");
572 yaz_log (YLOG_LOG, " unknown (%d)", *toKeep->action);
575 if (toKeep->databaseName)
577 yaz_log (YLOG_LOG, "database: %s", toKeep->databaseName);
579 if (zebra_select_database(zh, toKeep->databaseName))
584 yaz_log (YLOG_WARN, "no database supplied for ES Update");
586 YAZ_BIB1_ES_MISSING_MANDATORY_PARAMETER_FOR_SPECIFIED_FUNCTION_;
587 rr->errstring = "database";
590 if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
592 zebra_result(zh, &rr->errcode, &rr->errstring);
597 for (i = 0; notToKeep && i < notToKeep->num; i++)
599 Z_External *rec = notToKeep->elements[i]->record;
600 Odr_oct *opaque_recid = 0;
604 if (notToKeep->elements[i]->u.opaque)
606 switch(notToKeep->elements[i]->which)
608 case Z_IUSuppliedRecords_elem_opaque:
609 opaque_recid = notToKeep->elements[i]->u.opaque;
610 break; /* OK, recid already set */
611 case Z_IUSuppliedRecords_elem_number:
612 sysno_tmp = *notToKeep->elements[i]->u.number;
617 if (rec->direct_reference)
619 char oid_name_str[OID_STR_MAX];
620 const char *oid_name =
621 yaz_oid_to_string_buf(
622 rec->direct_reference,
625 yaz_log (YLOG_LOG, "record %d type %s", i,
630 case Z_External_sutrs:
631 if (rec->u.octet_aligned->len > 170)
632 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
636 yaz_log (YLOG_LOG, "%d bytes:\n%s",
640 case Z_External_octet:
641 if (rec->u.octet_aligned->len > 170)
642 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
643 rec->u.octet_aligned->len,
644 rec->u.octet_aligned->buf);
646 yaz_log (YLOG_LOG, "%d bytes\n%s",
647 rec->u.octet_aligned->len,
648 rec->u.octet_aligned->buf);
650 if (rec->which == Z_External_octet)
652 enum zebra_recctrl_action_t action = action_update;
654 const char *match_criteria = 0;
657 if (*toKeep->action ==
658 Z_IUOriginPartToKeep_recordInsert)
659 action = action_insert;
660 else if (*toKeep->action ==
661 Z_IUOriginPartToKeep_recordReplace)
662 action = action_replace;
663 else if (*toKeep->action ==
664 Z_IUOriginPartToKeep_recordDelete)
665 action = action_delete;
666 else if (*toKeep->action ==
667 Z_IUOriginPartToKeep_specialUpdate)
668 action = action_update;
672 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
673 rr->errstring = "unsupported ES Update action";
679 size_t l = opaque_recid->len;
680 if (l >= sizeof(recid_str))
682 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
683 rr->errstring = "opaque record ID too large";
686 memcpy(recid_str, opaque_recid->buf, l);
688 match_criteria = recid_str;
690 res = zebra_update_record(
693 sysno, match_criteria, 0, /* fname */
694 (const char *) rec->u.octet_aligned->buf,
695 rec->u.octet_aligned->len);
696 if (res == ZEBRA_FAIL)
699 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
700 rr->errstring = "update_record failed";
704 if (zebra_end_trans (zh) != ZEBRA_OK)
706 yaz_log(YLOG_WARN, "zebra_end_trans failed for"
707 " extended service operation");
714 yaz_log (YLOG_WARN, "Unknown Extended Service(%d)",
715 rr->esr->taskSpecificParameters->which);
716 rr->errcode = YAZ_BIB1_ES_EXTENDED_SERVICE_TYPE_UNSUPP;
722 static void bend_start (struct statserv_options_block *sob)
724 Res default_res = res_open(0, 0);
727 zebra_stop((ZebraService) sob->handle);
728 res_set(default_res, "profilePath", DEFAULT_PROFILE_PATH);
729 res_set(default_res, "modulePath", DEFAULT_MODULE_PATH);
730 sob->handle = zebra_start_res(sob->configname, default_res, 0);
731 res_close(default_res);
734 yaz_log (YLOG_FATAL, "Failed to read config `%s'", sob->configname);
746 zebra_pidfname(sob->handle, pidfname);
748 fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
753 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
756 fd = open(pidfname, O_RDWR, 0666);
759 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
763 area.l_type = F_WRLCK;
764 area.l_whence = SEEK_SET;
765 area.l_len = area.l_start = 0L;
766 if (fcntl (fd, F_SETLK, &area) == -1)
768 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
775 sprintf (pidstr, "%ld", (long) getpid ());
776 write (fd, pidstr, strlen(pidstr));
782 static void bend_stop(struct statserv_options_block *sob)
787 if (!sob->inetd && sob->handle)
790 zebra_pidfname(sob->handle, pidfname);
796 ZebraService service = sob->handle;
801 int main (int argc, char **argv)
803 struct statserv_options_block *sob;
805 sob = statserv_getcontrol ();
806 strcpy (sob->configname, "zebra.cfg");
807 sob->bend_start = bend_start;
808 sob->bend_stop = bend_stop;
810 strcpy (sob->service_display_name, "Zebra Server");
812 statserv_setcontrol (sob);
814 return statserv_main (argc, argv, bend_init, bend_close);
819 * indent-tabs-mode: nil
821 * vim: shiftwidth=4 tabstop=8 expandtab