1 /* $Id: zserver.c,v 1.124 2004-11-29 21:45:11 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
29 #include <sys/locking.h>
37 #include <yaz/yaz-util.h>
39 #include <sys/types.h>
43 static int bend_sort (void *handle, bend_sort_rr *rr);
44 static int bend_delete (void *handle, bend_delete_rr *rr);
45 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
46 static int bend_segment (void *handle, bend_segment_rr *rr);
47 static int bend_search (void *handle, bend_search_rr *r);
48 static int bend_fetch (void *handle, bend_fetch_rr *r);
49 static int bend_scan (void *handle, bend_scan_rr *r);
51 bend_initresult *bend_init (bend_initrequest *q)
53 bend_initresult *r = (bend_initresult *)
54 odr_malloc (q->stream, sizeof(*r));
56 struct statserv_options_block *sob;
62 q->bend_sort = bend_sort;
63 q->bend_delete = bend_delete;
64 q->bend_esrequest = bend_esrequest;
65 q->bend_segment = bend_segment;
66 q->bend_search = bend_search;
67 q->bend_fetch = bend_fetch;
68 q->bend_scan = bend_scan;
70 q->implementation_name = "Zebra Information Server";
71 q->implementation_version = "Zebra " ZEBRAVER;
73 yaz_log (YLOG_DEBUG, "bend_init");
75 sob = statserv_getcontrol ();
76 if (!(zh = zebra_open (sob->handle)))
78 yaz_log (YLOG_WARN, "Failed to read config `%s'", sob->configname);
85 if (q->auth->which == Z_IdAuthentication_open)
87 char *openpass = xstrdup (q->auth->u.open);
88 char *cp = strchr (openpass, '/');
92 user = nmem_strdup (odr_getmem (q->stream), openpass);
93 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
97 else if (q->auth->which == Z_IdAuthentication_idPass)
99 Z_IdPass *idPass = q->auth->u.idPass;
101 user = idPass->userId;
102 passwd = idPass->password;
105 if (zebra_auth (zh, user, passwd))
111 if (q->charneg_request) /* characater set and langauge negotiation? */
119 NMEM nmem = nmem_create();
121 yaz_log (YLOG_LOG, "character set and language negotiation");
123 yaz_get_proposal_charneg (nmem, q->charneg_request,
124 &charsets, &num_charsets,
125 &langs, &num_langs, &selected);
127 for (i = 0; i < num_charsets; i++)
129 const char *right_name = "";
131 * FIXME! It is like rudiment :-))
132 * We have to support this short names of character sets,
133 * because a lot servers in Russia to use own in during
134 * character set and language negotiation still.
137 if (!yaz_matchstr(charsets[i], "win")) {
138 right_name = "WINDOWS-1251";
139 } else if (!yaz_matchstr(charsets[i], "koi")) {
140 right_name = "KOI8-R";
141 } else if (!yaz_matchstr(charsets[i], "iso")) {
142 right_name = "ISO-8859-5";
143 } else if (!yaz_matchstr(charsets[i], "dos")) {
144 right_name = "CP866";
145 } else if (!yaz_matchstr(charsets[i], "uni")) {
146 right_name = "UTF-8";
148 right_name = charsets[i];
150 if (odr_set_charset (q->decode, "UTF-8", right_name) == 0)
152 yaz_log (YLOG_LOG, "charset %d %s (proper name %s): OK", i,
153 charsets[i], right_name);
154 odr_set_charset (q->stream, right_name, "UTF-8");
156 zebra_record_encoding (zh, right_name);
157 q->charneg_response =
158 yaz_set_response_charneg (q->stream, charsets[i],
162 yaz_log (YLOG_LOG, "charset %d %s (proper name %s): unsupported", i,
163 charsets[i], right_name);
171 static void search_terms (ZebraHandle zh, bend_search_rr *r)
176 int type = Z_Term_general;
177 struct Z_External *ext;
178 Z_SearchInfoReport *sr;
180 /* get no of terms for result set */
181 no_terms = 0; /* zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0); */
182 /* FIXME - Rsets don't know number of terms no more ??? */
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++)
211 size_t len = sizeof(outbuf);
212 /* FIXME - Can we just skip this ??? */
214 zebra_resultSetTerms (zh, r->setname, i,
215 &count, &type, outbuf, &len);
217 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
218 sr->elements[i]->subqueryId = 0;
219 sr->elements[i]->fullQuery = odr_malloc (r->stream,
221 *sr->elements[i]->fullQuery = 0;
222 sr->elements[i]->subqueryExpression =
223 odr_malloc (r->stream, sizeof(Z_QueryExpression));
224 sr->elements[i]->subqueryExpression->which =
225 Z_QueryExpression_term;
226 sr->elements[i]->subqueryExpression->u.term =
227 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
228 term = odr_malloc (r->stream, sizeof(Z_Term));
229 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
232 case Z_Term_characterString:
233 yaz_log (YLOG_DEBUG, "term as characterString");
234 term->which = Z_Term_characterString;
235 term->u.characterString = odr_strdup (r->stream, outbuf);
238 yaz_log (YLOG_DEBUG, "term as general");
239 term->which = Z_Term_general;
240 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
241 term->u.general->size = term->u.general->len = len;
242 term->u.general->buf = odr_malloc (r->stream, len);
243 memcpy (term->u.general->buf, outbuf, len);
246 term->which = Z_Term_general;
247 term->u.null = odr_nullval();
249 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
250 sr->elements[i]->subqueryInterpretation = 0;
251 sr->elements[i]->subqueryRecommendation = 0;
252 if (count > 2000000000)
254 sr->elements[i]->subqueryCount = odr_intdup (r->stream, (int) count);
255 sr->elements[i]->subqueryWeight = 0;
256 sr->elements[i]->resultsByDB = 0;
260 int bend_search (void *handle, bend_search_rr *r)
262 ZebraHandle zh = (ZebraHandle) handle;
268 if (zebra_select_databases (zh, r->num_bases,
269 (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 zebra_search_RPN (zh, r->stream, r->query->u.type_1,
279 r->setname, &r->hits);
280 zebra_result (zh, &r->errcode, &r->errstring);
282 search_terms (zh, r);
286 r->errstring = "type-2";
295 int bend_fetch (void *handle, bend_fetch_rr *r)
297 ZebraHandle zh = (ZebraHandle) handle;
298 ZebraRetrievalRecord retrievalRecord;
300 retrievalRecord.position = r->number;
303 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
304 r->request_format, 1, &retrievalRecord);
305 zebra_result (zh, &r->errcode, &r->errstring);
306 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
307 if (r->errcode == 0 && retrievalRecord.errCode)
309 r->surrogate_flag = 1;
310 r->errcode = retrievalRecord.errCode;
311 r->errstring = retrievalRecord.errString;
312 r->basename = retrievalRecord.base;
314 else if (r->errcode == 0) /* Database Record */
317 r->basename = retrievalRecord.base;
318 r->record = retrievalRecord.buf;
319 r->len = retrievalRecord.len;
320 r->output_format = retrievalRecord.format;
325 static int bend_scan (void *handle, bend_scan_rr *r)
327 ZebraScanEntry *entries;
328 ZebraHandle zh = (ZebraHandle) handle;
331 if (zebra_select_databases (zh, r->num_bases,
332 (const char **) r->basenames))
334 zebra_result (zh, &r->errcode, &r->errstring);
337 if (r->step_size != 0 && *r->step_size != 0) {
338 r->errcode = 205; /* "Only zero step size supported for Scan" */
342 r->entries = (struct scan_entry *)
343 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
344 zebra_scan (zh, r->stream, r->term,
347 &r->num_entries, &entries, &is_partial);
349 r->status = BEND_SCAN_PARTIAL;
351 r->status = BEND_SCAN_SUCCESS;
352 for (i = 0; i < r->num_entries; i++)
354 r->entries[i].term = entries[i].term;
355 r->entries[i].occurrences = entries[i].occurrences;
357 zebra_result (zh, &r->errcode, &r->errstring);
361 void bend_close (void *handle)
363 zebra_close ((ZebraHandle) handle);
364 xmalloc_trav("bend_close");
368 int bend_sort (void *handle, bend_sort_rr *rr)
370 ZebraHandle zh = (ZebraHandle) handle;
372 zebra_sort (zh, rr->stream,
373 rr->num_input_setnames, (const char **) rr->input_setnames,
374 rr->output_setname, rr->sort_sequence, &rr->sort_status);
375 zebra_result (zh, &rr->errcode, &rr->errstring);
379 int bend_delete (void *handle, bend_delete_rr *rr)
381 ZebraHandle zh = (ZebraHandle) handle;
383 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
384 rr->num_setnames, rr->setnames,
389 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
391 if (r->toKeep->databaseName)
393 yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
395 switch (r->toKeep->which)
397 case Z_ESAdminOriginPartToKeep_reIndex:
398 yaz_log(YLOG_LOG, "adm-reindex");
400 case Z_ESAdminOriginPartToKeep_truncate:
401 yaz_log(YLOG_LOG, "adm-truncate");
403 case Z_ESAdminOriginPartToKeep_drop:
404 yaz_log(YLOG_LOG, "adm-drop");
405 zebra_drop_database (zh, r->toKeep->databaseName);
407 case Z_ESAdminOriginPartToKeep_create:
408 yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
409 zebra_create_database (zh, r->toKeep->databaseName);
411 case Z_ESAdminOriginPartToKeep_import:
412 yaz_log(YLOG_LOG, "adm-import");
413 zebra_admin_import_begin (zh, r->toKeep->databaseName,
414 r->toKeep->u.import->recordType);
416 case Z_ESAdminOriginPartToKeep_refresh:
417 yaz_log(YLOG_LOG, "adm-refresh");
419 case Z_ESAdminOriginPartToKeep_commit:
420 yaz_log(YLOG_LOG, "adm-commit");
421 if (r->toKeep->databaseName)
422 zebra_select_database(zh, r->toKeep->databaseName);
425 case Z_ESAdminOriginPartToKeep_shutdown:
426 yaz_log(YLOG_LOG, "shutdown");
427 zebra_admin_shutdown(zh);
429 case Z_ESAdminOriginPartToKeep_start:
430 yaz_log(YLOG_LOG, "start");
431 zebra_admin_start(zh);
434 yaz_log(YLOG_LOG, "unknown admin");
439 static int es_admin (ZebraHandle zh, Z_Admin *r)
443 case Z_Admin_esRequest:
444 es_admin_request (zh, r->u.esRequest);
446 case Z_Admin_taskPackage:
447 yaz_log (YLOG_LOG, "adm taskpackage (unhandled)");
456 int bend_segment (void *handle, bend_segment_rr *rr)
458 ZebraHandle zh = (ZebraHandle) handle;
459 Z_Segment *segment = rr->segment;
461 if (segment->num_segmentRecords)
462 zebra_admin_import_segment (zh, rr->segment);
464 zebra_admin_import_end (zh);
468 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
470 ZebraHandle zh = (ZebraHandle) handle;
472 yaz_log(YLOG_LOG, "function: %d", *rr->esr->function);
473 if (rr->esr->packageName)
474 yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
475 yaz_log(YLOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
477 if (!rr->esr->taskSpecificParameters)
479 yaz_log (YLOG_WARN, "No task specific parameters");
481 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
483 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
485 zebra_result (zh, &rr->errcode, &rr->errstring);
487 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
489 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
490 yaz_log (YLOG_LOG, "Received DB Update");
491 if (up->which == Z_IUUpdate_esRequest)
493 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
494 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
495 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
497 yaz_log (YLOG_LOG, "action");
500 switch (*toKeep->action)
502 case Z_IUOriginPartToKeep_recordInsert:
503 yaz_log (YLOG_LOG, "recordInsert");
505 case Z_IUOriginPartToKeep_recordReplace:
506 yaz_log (YLOG_LOG, "recordUpdate");
508 case Z_IUOriginPartToKeep_recordDelete:
509 yaz_log (YLOG_LOG, "recordDelete");
511 case Z_IUOriginPartToKeep_elementUpdate:
512 yaz_log (YLOG_LOG, "elementUpdate");
514 case Z_IUOriginPartToKeep_specialUpdate:
515 yaz_log (YLOG_LOG, "specialUpdate");
517 case Z_ESAdminOriginPartToKeep_shutdown:
518 yaz_log (YLOG_LOG, "shutDown");
520 case Z_ESAdminOriginPartToKeep_start:
521 yaz_log (YLOG_LOG, "start");
524 yaz_log (YLOG_LOG, " unknown (%d)", *toKeep->action);
527 if (toKeep->databaseName)
529 yaz_log (YLOG_LOG, "database: %s", toKeep->databaseName);
531 if (zebra_select_database(zh, toKeep->databaseName))
536 yaz_log (YLOG_WARN, "no database supplied for ES Update");
538 rr->errstring = "database";
544 zebra_begin_trans (zh, 1);
545 for (i = 0; i < notToKeep->num; i++)
547 Z_External *rec = notToKeep->elements[i]->record;
548 struct oident *oident = 0;
549 Odr_oct *opaque_recid = 0;
552 if (notToKeep->elements[i]->u.opaque)
554 switch(notToKeep->elements[i]->which)
556 case Z_IUSuppliedRecords_elem_opaque:
557 opaque_recid = notToKeep->elements[i]->u.opaque;
558 break; /* OK, recid already set */
559 case Z_IUSuppliedRecords_elem_number:
560 sysno = *notToKeep->elements[i]->u.number;
564 if (rec->direct_reference)
566 oident = oid_getentbyoid(rec->direct_reference);
568 yaz_log (YLOG_LOG, "record %d type %s", i,
573 case Z_External_sutrs:
574 if (rec->u.octet_aligned->len > 170)
575 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
579 yaz_log (YLOG_LOG, "%d bytes:\n%s",
583 case Z_External_octet:
584 if (rec->u.octet_aligned->len > 170)
585 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
586 rec->u.octet_aligned->len,
587 rec->u.octet_aligned->buf);
589 yaz_log (YLOG_LOG, "%d bytes\n%s",
590 rec->u.octet_aligned->len,
591 rec->u.octet_aligned->buf);
593 if (oident && oident->value != VAL_TEXT_XML)
596 rr->errstring = "only XML update supported";
599 if (rec->which == Z_External_octet)
603 if (*toKeep->action ==
604 Z_IUOriginPartToKeep_recordInsert)
606 if (*toKeep->action ==
607 Z_IUOriginPartToKeep_recordReplace)
609 if (*toKeep->action ==
610 Z_IUOriginPartToKeep_recordDelete)
612 if (*toKeep->action ==
613 Z_IUOriginPartToKeep_specialUpdate)
619 rr->errstring = "unsupported ES Update action";
622 else if (opaque_recid)
624 int r = zebra_admin_exchange_record (
626 rec->u.octet_aligned->buf,
627 rec->u.octet_aligned->len,
628 opaque_recid->buf, opaque_recid->len,
633 rr->errstring = "record exchange failed";
642 r = zebra_insert_record(
648 rec->u.octet_aligned->buf,
649 rec->u.octet_aligned->len,
654 rr->errstring = "insert_record failed";
659 r = zebra_update_record(
665 rec->u.octet_aligned->buf,
666 rec->u.octet_aligned->len,
671 rr->errstring = "update_record failed";
675 r = zebra_delete_record(
681 rec->u.octet_aligned->buf,
682 rec->u.octet_aligned->len,
687 rr->errstring = "delete_record failed";
694 zebra_end_trans (zh);
700 yaz_log (YLOG_WARN, "Unknown Extended Service(%d)",
701 rr->esr->taskSpecificParameters->which);
708 static void bend_start (struct statserv_options_block *sob)
711 zebra_stop((ZebraService) sob->handle);
712 sob->handle = zebra_start(sob->configname);
715 yaz_log (YLOG_FATAL, "Failed to read config `%s'", sob->configname);
727 zebra_pidfname(sob->handle, pidfname);
729 fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
734 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
737 fd = open(pidfname, O_RDWR, 0666);
740 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
744 area.l_type = F_WRLCK;
745 area.l_whence = SEEK_SET;
746 area.l_len = area.l_start = 0L;
747 if (fcntl (fd, F_SETLK, &area) == -1)
749 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
756 sprintf (pidstr, "%ld", (long) getpid ());
757 write (fd, pidstr, strlen(pidstr));
763 static void bend_stop(struct statserv_options_block *sob)
768 if (!sob->inetd && sob->handle)
771 zebra_pidfname(sob->handle, pidfname);
777 ZebraService service = sob->handle;
782 int main (int argc, char **argv)
784 struct statserv_options_block *sob;
786 sob = statserv_getcontrol ();
787 strcpy (sob->configname, "zebra.cfg");
788 sob->bend_start = bend_start;
789 sob->bend_stop = bend_stop;
791 strcpy (sob->service_display_name, "Zebra Server");
793 statserv_setcontrol (sob);
795 return statserv_main (argc, argv, bend_init, bend_close);