1 /* $Id: zserver.c,v 1.130 2005-03-08 14:02:12 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>
40 #include <sys/types.h>
44 static int bend_sort (void *handle, bend_sort_rr *rr);
45 static int bend_delete (void *handle, bend_delete_rr *rr);
46 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
47 static int bend_segment (void *handle, bend_segment_rr *rr);
48 static int bend_search (void *handle, bend_search_rr *r);
49 static int bend_fetch (void *handle, bend_fetch_rr *r);
50 static int bend_scan (void *handle, bend_scan_rr *r);
52 bend_initresult *bend_init (bend_initrequest *q)
54 bend_initresult *r = (bend_initresult *)
55 odr_malloc (q->stream, sizeof(*r));
57 struct statserv_options_block *sob;
63 q->bend_sort = bend_sort;
64 q->bend_delete = bend_delete;
65 q->bend_esrequest = bend_esrequest;
66 q->bend_segment = bend_segment;
67 q->bend_search = bend_search;
68 q->bend_fetch = bend_fetch;
69 q->bend_scan = bend_scan;
71 q->implementation_name = "Zebra Information Server";
72 q->implementation_version = "Zebra " ZEBRAVER;
74 yaz_log (YLOG_DEBUG, "bend_init");
76 sob = statserv_getcontrol ();
77 if (!(zh = zebra_open (sob->handle)))
79 yaz_log (YLOG_WARN, "Failed to read config `%s'", sob->configname);
86 if (q->auth->which == Z_IdAuthentication_open)
88 char *openpass = xstrdup (q->auth->u.open);
89 char *cp = strchr (openpass, '/');
93 user = nmem_strdup (odr_getmem (q->stream), openpass);
94 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
98 else if (q->auth->which == Z_IdAuthentication_idPass)
100 Z_IdPass *idPass = q->auth->u.idPass;
102 user = idPass->userId;
103 passwd = idPass->password;
106 if (zebra_auth (zh, user, passwd))
112 if (q->charneg_request) /* characater set and langauge negotiation? */
120 NMEM nmem = nmem_create();
122 yaz_log (YLOG_LOG, "character set and language negotiation");
124 yaz_get_proposal_charneg (nmem, q->charneg_request,
125 &charsets, &num_charsets,
126 &langs, &num_langs, &selected);
128 for (i = 0; i < num_charsets; i++)
130 const char *right_name = "";
132 * FIXME! It is like rudiment :-))
133 * We have to support this short names of character sets,
134 * because a lot servers in Russia to use own in during
135 * character set and language negotiation still.
138 if (!yaz_matchstr(charsets[i], "win")) {
139 right_name = "WINDOWS-1251";
140 } else if (!yaz_matchstr(charsets[i], "koi")) {
141 right_name = "KOI8-R";
142 } else if (!yaz_matchstr(charsets[i], "iso")) {
143 right_name = "ISO-8859-5";
144 } else if (!yaz_matchstr(charsets[i], "dos")) {
145 right_name = "CP866";
146 } else if (!yaz_matchstr(charsets[i], "uni")) {
147 right_name = "UTF-8";
149 right_name = charsets[i];
151 if (odr_set_charset (q->decode, "UTF-8", right_name) == 0)
153 yaz_log (YLOG_LOG, "charset %d %s (proper name %s): OK", i,
154 charsets[i], right_name);
155 odr_set_charset (q->stream, right_name, "UTF-8");
157 zebra_record_encoding(zh, right_name);
158 zebra_octet_term_encoding(zh, right_name);
159 q->charneg_response =
160 yaz_set_response_charneg (q->stream, charsets[i],
164 yaz_log (YLOG_LOG, "charset %d %s (proper name %s): unsupported", i,
165 charsets[i], right_name);
173 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 /* get no of terms for result set */
183 no_terms = 0; /* zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0); */
184 /* FIXME - Rsets don't know number of terms no more ??? */
188 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
190 r->search_info->num_elements = 1;
191 r->search_info->list =
192 odr_malloc (r->stream, sizeof(*r->search_info->list));
193 r->search_info->list[0] =
194 odr_malloc (r->stream, sizeof(**r->search_info->list));
195 r->search_info->list[0]->category = 0;
196 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
197 ext = odr_malloc (r->stream, sizeof(*ext));
198 r->search_info->list[0]->information.externallyDefinedInfo = ext;
199 ext->direct_reference =
200 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
201 ext->indirect_reference = 0;
203 ext->which = Z_External_searchResult1;
204 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
205 ext->u.searchResult1 = sr;
207 sr->elements = odr_malloc (r->stream, sr->num *
208 sizeof(*sr->elements));
209 for (i = 0; i<no_terms; i++)
213 size_t len = sizeof(outbuf);
214 /* FIXME - Can we just skip this ??? */
216 zebra_resultSetTerms (zh, r->setname, i,
217 &count, &type, outbuf, &len);
219 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
220 sr->elements[i]->subqueryId = 0;
221 sr->elements[i]->fullQuery = odr_malloc (r->stream,
223 *sr->elements[i]->fullQuery = 0;
224 sr->elements[i]->subqueryExpression =
225 odr_malloc (r->stream, sizeof(Z_QueryExpression));
226 sr->elements[i]->subqueryExpression->which =
227 Z_QueryExpression_term;
228 sr->elements[i]->subqueryExpression->u.term =
229 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
230 term = odr_malloc (r->stream, sizeof(Z_Term));
231 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
234 case Z_Term_characterString:
235 yaz_log (YLOG_DEBUG, "term as characterString");
236 term->which = Z_Term_characterString;
237 term->u.characterString = odr_strdup (r->stream, outbuf);
240 yaz_log (YLOG_DEBUG, "term as general");
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 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
252 sr->elements[i]->subqueryInterpretation = 0;
253 sr->elements[i]->subqueryRecommendation = 0;
254 if (count > 2000000000)
256 sr->elements[i]->subqueryCount = odr_intdup (r->stream, (int) count);
257 sr->elements[i]->subqueryWeight = 0;
258 sr->elements[i]->resultsByDB = 0;
262 int bend_search (void *handle, bend_search_rr *r)
264 ZebraHandle zh = (ZebraHandle) handle;
271 if (zebra_select_databases (zh, r->num_bases,
272 (const char **) r->basenames))
274 zebra_result (zh, &r->errcode, &r->errstring);
277 yaz_log (YLOG_DEBUG, "ResultSet '%s'", r->setname);
278 switch (r->query->which)
280 case Z_Query_type_1: case Z_Query_type_101:
281 zebra_search_RPN (zh, r->stream, r->query->u.type_1,
283 if (zhits > 2147483646)
284 r->hits = 2147483647;
286 r->hits = (int) zhits;
287 zebra_result (zh, &r->errcode, &r->errstring);
289 search_terms (zh, r);
293 r->errstring = "type-2";
302 int bend_fetch (void *handle, bend_fetch_rr *r)
304 ZebraHandle zh = (ZebraHandle) handle;
305 ZebraRetrievalRecord retrievalRecord;
307 retrievalRecord.position = r->number;
310 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
311 r->request_format, 1, &retrievalRecord);
312 zebra_result (zh, &r->errcode, &r->errstring);
313 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
314 if (r->errcode == 0 && retrievalRecord.errCode)
316 r->surrogate_flag = 1;
317 r->errcode = retrievalRecord.errCode;
318 r->errstring = retrievalRecord.errString;
319 r->basename = retrievalRecord.base;
321 else if (r->errcode == 0) /* Database Record */
324 r->basename = retrievalRecord.base;
325 r->record = retrievalRecord.buf;
326 r->len = retrievalRecord.len;
327 r->output_format = retrievalRecord.format;
332 static int bend_scan (void *handle, bend_scan_rr *r)
334 ZebraScanEntry *entries;
335 ZebraHandle zh = (ZebraHandle) handle;
338 if (zebra_select_databases (zh, r->num_bases,
339 (const char **) r->basenames))
341 zebra_result (zh, &r->errcode, &r->errstring);
344 if (r->step_size != 0 && *r->step_size != 0) {
345 r->errcode = 205; /* "Only zero step size supported for Scan" */
349 r->entries = (struct scan_entry *)
350 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
351 zebra_scan (zh, r->stream, r->term,
354 &r->num_entries, &entries, &is_partial);
356 r->status = BEND_SCAN_PARTIAL;
358 r->status = BEND_SCAN_SUCCESS;
359 for (i = 0; i < r->num_entries; i++)
361 r->entries[i].term = entries[i].term;
362 r->entries[i].occurrences = entries[i].occurrences;
364 zebra_result (zh, &r->errcode, &r->errstring);
368 void bend_close (void *handle)
370 zebra_close ((ZebraHandle) handle);
371 xmalloc_trav("bend_close");
375 int bend_sort (void *handle, bend_sort_rr *rr)
377 ZebraHandle zh = (ZebraHandle) handle;
379 zebra_sort (zh, rr->stream,
380 rr->num_input_setnames, (const char **) rr->input_setnames,
381 rr->output_setname, rr->sort_sequence, &rr->sort_status);
382 zebra_result (zh, &rr->errcode, &rr->errstring);
386 int bend_delete (void *handle, bend_delete_rr *rr)
388 ZebraHandle zh = (ZebraHandle) handle;
390 rr->delete_status = zebra_deleteResultSet(zh, rr->function,
391 rr->num_setnames, rr->setnames,
396 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
398 if (r->toKeep->databaseName)
400 yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
402 switch (r->toKeep->which)
404 case Z_ESAdminOriginPartToKeep_reIndex:
405 yaz_log(YLOG_LOG, "adm-reindex");
407 case Z_ESAdminOriginPartToKeep_truncate:
408 yaz_log(YLOG_LOG, "adm-truncate");
410 case Z_ESAdminOriginPartToKeep_drop:
411 yaz_log(YLOG_LOG, "adm-drop");
412 zebra_drop_database (zh, r->toKeep->databaseName);
414 case Z_ESAdminOriginPartToKeep_create:
415 yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
416 zebra_create_database (zh, r->toKeep->databaseName);
418 case Z_ESAdminOriginPartToKeep_import:
419 yaz_log(YLOG_LOG, "adm-import");
420 zebra_admin_import_begin (zh, r->toKeep->databaseName,
421 r->toKeep->u.import->recordType);
423 case Z_ESAdminOriginPartToKeep_refresh:
424 yaz_log(YLOG_LOG, "adm-refresh");
426 case Z_ESAdminOriginPartToKeep_commit:
427 yaz_log(YLOG_LOG, "adm-commit");
428 if (r->toKeep->databaseName)
429 zebra_select_database(zh, r->toKeep->databaseName);
432 case Z_ESAdminOriginPartToKeep_shutdown:
433 yaz_log(YLOG_LOG, "shutdown");
434 zebra_admin_shutdown(zh);
436 case Z_ESAdminOriginPartToKeep_start:
437 yaz_log(YLOG_LOG, "start");
438 zebra_admin_start(zh);
441 yaz_log(YLOG_LOG, "unknown admin");
446 static int es_admin (ZebraHandle zh, Z_Admin *r)
450 case Z_Admin_esRequest:
451 es_admin_request (zh, r->u.esRequest);
453 case Z_Admin_taskPackage:
454 yaz_log (YLOG_LOG, "adm taskpackage (unhandled)");
463 int bend_segment (void *handle, bend_segment_rr *rr)
465 ZebraHandle zh = (ZebraHandle) handle;
466 Z_Segment *segment = rr->segment;
468 if (segment->num_segmentRecords)
469 zebra_admin_import_segment (zh, rr->segment);
471 zebra_admin_import_end (zh);
475 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
477 ZebraHandle zh = (ZebraHandle) handle;
479 yaz_log(YLOG_LOG, "function: %d", *rr->esr->function);
480 if (rr->esr->packageName)
481 yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
482 yaz_log(YLOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
484 if (!rr->esr->taskSpecificParameters)
486 yaz_log (YLOG_WARN, "No task specific parameters");
488 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
490 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
492 zebra_result (zh, &rr->errcode, &rr->errstring);
494 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
496 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
497 yaz_log (YLOG_LOG, "Received DB Update");
498 if (up->which == Z_IUUpdate_esRequest)
500 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
501 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
502 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
504 yaz_log (YLOG_LOG, "action");
507 switch (*toKeep->action)
509 case Z_IUOriginPartToKeep_recordInsert:
510 yaz_log (YLOG_LOG, "recordInsert");
512 case Z_IUOriginPartToKeep_recordReplace:
513 yaz_log (YLOG_LOG, "recordUpdate");
515 case Z_IUOriginPartToKeep_recordDelete:
516 yaz_log (YLOG_LOG, "recordDelete");
518 case Z_IUOriginPartToKeep_elementUpdate:
519 yaz_log (YLOG_LOG, "elementUpdate");
521 case Z_IUOriginPartToKeep_specialUpdate:
522 yaz_log (YLOG_LOG, "specialUpdate");
524 case Z_ESAdminOriginPartToKeep_shutdown:
525 yaz_log (YLOG_LOG, "shutDown");
527 case Z_ESAdminOriginPartToKeep_start:
528 yaz_log (YLOG_LOG, "start");
531 yaz_log (YLOG_LOG, " unknown (%d)", *toKeep->action);
534 if (toKeep->databaseName)
536 yaz_log (YLOG_LOG, "database: %s", toKeep->databaseName);
538 if (zebra_select_database(zh, toKeep->databaseName))
543 yaz_log (YLOG_WARN, "no database supplied for ES Update");
545 rr->errstring = "database";
551 zebra_begin_trans (zh, 1);
552 for (i = 0; i < notToKeep->num; i++)
554 Z_External *rec = notToKeep->elements[i]->record;
555 struct oident *oident = 0;
556 Odr_oct *opaque_recid = 0;
559 if (notToKeep->elements[i]->u.opaque)
561 switch(notToKeep->elements[i]->which)
563 case Z_IUSuppliedRecords_elem_opaque:
564 opaque_recid = notToKeep->elements[i]->u.opaque;
565 break; /* OK, recid already set */
566 case Z_IUSuppliedRecords_elem_number:
567 sysno = *notToKeep->elements[i]->u.number;
571 if (rec->direct_reference)
573 oident = oid_getentbyoid(rec->direct_reference);
575 yaz_log (YLOG_LOG, "record %d type %s", i,
580 case Z_External_sutrs:
581 if (rec->u.octet_aligned->len > 170)
582 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
586 yaz_log (YLOG_LOG, "%d bytes:\n%s",
590 case Z_External_octet:
591 if (rec->u.octet_aligned->len > 170)
592 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
593 rec->u.octet_aligned->len,
594 rec->u.octet_aligned->buf);
596 yaz_log (YLOG_LOG, "%d bytes\n%s",
597 rec->u.octet_aligned->len,
598 rec->u.octet_aligned->buf);
600 if (oident && oident->value != VAL_TEXT_XML)
603 rr->errstring = "only XML update supported";
606 if (rec->which == Z_External_octet)
610 if (*toKeep->action ==
611 Z_IUOriginPartToKeep_recordInsert)
613 if (*toKeep->action ==
614 Z_IUOriginPartToKeep_recordReplace)
616 if (*toKeep->action ==
617 Z_IUOriginPartToKeep_recordDelete)
619 if (*toKeep->action ==
620 Z_IUOriginPartToKeep_specialUpdate)
626 rr->errstring = "unsupported ES Update action";
629 else if (opaque_recid)
631 int r = zebra_admin_exchange_record (
633 rec->u.octet_aligned->buf,
634 rec->u.octet_aligned->len,
635 opaque_recid->buf, opaque_recid->len,
640 rr->errstring = "record exchange failed";
649 r = zebra_insert_record(
655 rec->u.octet_aligned->buf,
656 rec->u.octet_aligned->len,
661 rr->errstring = "insert_record failed";
666 r = zebra_update_record(
672 rec->u.octet_aligned->buf,
673 rec->u.octet_aligned->len,
678 rr->errstring = "update_record failed";
682 r = zebra_delete_record(
688 rec->u.octet_aligned->buf,
689 rec->u.octet_aligned->len,
694 rr->errstring = "delete_record failed";
701 zebra_end_trans (zh);
707 yaz_log (YLOG_WARN, "Unknown Extended Service(%d)",
708 rr->esr->taskSpecificParameters->which);
715 static void bend_start (struct statserv_options_block *sob)
718 zebra_stop((ZebraService) sob->handle);
719 sob->handle = zebra_start(sob->configname);
722 yaz_log (YLOG_FATAL, "Failed to read config `%s'", sob->configname);
734 zebra_pidfname(sob->handle, pidfname);
736 fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
741 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
744 fd = open(pidfname, O_RDWR, 0666);
747 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
751 area.l_type = F_WRLCK;
752 area.l_whence = SEEK_SET;
753 area.l_len = area.l_start = 0L;
754 if (fcntl (fd, F_SETLK, &area) == -1)
756 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
763 sprintf (pidstr, "%ld", (long) getpid ());
764 write (fd, pidstr, strlen(pidstr));
770 static void bend_stop(struct statserv_options_block *sob)
775 if (!sob->inetd && sob->handle)
778 zebra_pidfname(sob->handle, pidfname);
784 ZebraService service = sob->handle;
789 int main (int argc, char **argv)
791 struct statserv_options_block *sob;
793 sob = statserv_getcontrol ();
794 strcpy (sob->configname, "zebra.cfg");
795 sob->bend_start = bend_start;
796 sob->bend_stop = bend_stop;
798 strcpy (sob->service_display_name, "Zebra Server");
800 statserv_setcontrol (sob);
802 return statserv_main (argc, argv, bend_init, bend_close);