1 /* $Id: zserver.c,v 1.114 2004-03-29 15:48:14 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 (LOG_DEBUG, "bend_init");
75 sob = statserv_getcontrol ();
76 if (!(zh = zebra_open (sob->handle)))
78 yaz_log (LOG_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 (LOG_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 (LOG_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 (LOG_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)
177 struct Z_External *ext;
178 Z_SearchInfoReport *sr;
180 /* get no of terms for result set */
181 no_terms = zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0);
185 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
187 r->search_info->num_elements = 1;
188 r->search_info->list =
189 odr_malloc (r->stream, sizeof(*r->search_info->list));
190 r->search_info->list[0] =
191 odr_malloc (r->stream, sizeof(**r->search_info->list));
192 r->search_info->list[0]->category = 0;
193 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
194 ext = odr_malloc (r->stream, sizeof(*ext));
195 r->search_info->list[0]->information.externallyDefinedInfo = ext;
196 ext->direct_reference =
197 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
198 ext->indirect_reference = 0;
200 ext->which = Z_External_searchResult1;
201 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
202 ext->u.searchResult1 = sr;
204 sr->elements = odr_malloc (r->stream, sr->num *
205 sizeof(*sr->elements));
206 for (i = 0; i<no_terms; i++)
210 size_t len = sizeof(outbuf);
211 zebra_resultSetTerms (zh, r->setname, i,
212 &count, &type, outbuf, &len);
214 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
215 sr->elements[i]->subqueryId = 0;
216 sr->elements[i]->fullQuery = odr_malloc (r->stream,
218 *sr->elements[i]->fullQuery = 0;
219 sr->elements[i]->subqueryExpression =
220 odr_malloc (r->stream, sizeof(Z_QueryExpression));
221 sr->elements[i]->subqueryExpression->which =
222 Z_QueryExpression_term;
223 sr->elements[i]->subqueryExpression->u.term =
224 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
225 term = odr_malloc (r->stream, sizeof(Z_Term));
226 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
229 case Z_Term_characterString:
230 yaz_log (LOG_DEBUG, "term as characterString");
231 term->which = Z_Term_characterString;
232 term->u.characterString = odr_strdup (r->stream, outbuf);
235 yaz_log (LOG_DEBUG, "term as general");
236 term->which = Z_Term_general;
237 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
238 term->u.general->size = term->u.general->len = len;
239 term->u.general->buf = odr_malloc (r->stream, len);
240 memcpy (term->u.general->buf, outbuf, len);
243 term->which = Z_Term_general;
244 term->u.null = odr_nullval();
246 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
247 sr->elements[i]->subqueryInterpretation = 0;
248 sr->elements[i]->subqueryRecommendation = 0;
249 sr->elements[i]->subqueryCount = odr_intdup (r->stream, count);
250 sr->elements[i]->subqueryWeight = 0;
251 sr->elements[i]->resultsByDB = 0;
255 int bend_search (void *handle, bend_search_rr *r)
257 ZebraHandle zh = (ZebraHandle) handle;
263 if (zebra_select_databases (zh, r->num_bases,
264 (const char **) r->basenames))
266 zebra_result (zh, &r->errcode, &r->errstring);
269 yaz_log (LOG_LOG, "ResultSet '%s'", r->setname);
270 switch (r->query->which)
272 case Z_Query_type_1: case Z_Query_type_101:
273 zebra_search_RPN (zh, r->stream, r->query->u.type_1,
274 r->setname, &r->hits);
275 zebra_result (zh, &r->errcode, &r->errstring);
277 search_terms (zh, r);
281 r->errstring = "type-2";
290 int bend_fetch (void *handle, bend_fetch_rr *r)
292 ZebraHandle zh = (ZebraHandle) handle;
293 ZebraRetrievalRecord retrievalRecord;
295 retrievalRecord.position = r->number;
298 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
299 r->request_format, 1, &retrievalRecord);
300 zebra_result (zh, &r->errcode, &r->errstring);
301 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
302 if (r->errcode == 0 && retrievalRecord.errCode)
304 r->surrogate_flag = 1;
305 r->errcode = retrievalRecord.errCode;
306 r->errstring = retrievalRecord.errString;
307 r->basename = retrievalRecord.base;
309 else if (r->errcode == 0) /* Database Record */
312 r->basename = retrievalRecord.base;
313 r->record = retrievalRecord.buf;
314 r->len = retrievalRecord.len;
315 r->output_format = retrievalRecord.format;
320 static int bend_scan (void *handle, bend_scan_rr *r)
322 ZebraScanEntry *entries;
323 ZebraHandle zh = (ZebraHandle) handle;
326 if (zebra_select_databases (zh, r->num_bases,
327 (const char **) r->basenames))
329 zebra_result (zh, &r->errcode, &r->errstring);
332 r->entries = (struct scan_entry *)
333 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
334 zebra_scan (zh, r->stream, r->term,
337 &r->num_entries, &entries, &is_partial);
339 r->status = BEND_SCAN_PARTIAL;
341 r->status = BEND_SCAN_SUCCESS;
342 for (i = 0; i < r->num_entries; i++)
344 r->entries[i].term = entries[i].term;
345 r->entries[i].occurrences = entries[i].occurrences;
347 zebra_result (zh, &r->errcode, &r->errstring);
351 void bend_close (void *handle)
353 zebra_close ((ZebraHandle) handle);
354 xmalloc_trav("bend_close");
358 int bend_sort (void *handle, bend_sort_rr *rr)
360 ZebraHandle zh = (ZebraHandle) handle;
362 zebra_sort (zh, rr->stream,
363 rr->num_input_setnames, (const char **) rr->input_setnames,
364 rr->output_setname, rr->sort_sequence, &rr->sort_status);
365 zebra_result (zh, &rr->errcode, &rr->errstring);
369 int bend_delete (void *handle, bend_delete_rr *rr)
371 ZebraHandle zh = (ZebraHandle) handle;
373 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
374 rr->num_setnames, rr->setnames,
379 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
381 switch (r->toKeep->which)
383 case Z_ESAdminOriginPartToKeep_reIndex:
384 yaz_log(LOG_LOG, "adm-reindex");
386 case Z_ESAdminOriginPartToKeep_truncate:
387 yaz_log(LOG_LOG, "adm-truncate");
389 case Z_ESAdminOriginPartToKeep_drop:
390 yaz_log(LOG_LOG, "adm-drop");
391 zebra_drop_database (zh, r->toKeep->databaseName);
393 case Z_ESAdminOriginPartToKeep_create:
394 yaz_log(LOG_LOG, "adm-create %s", r->toKeep->databaseName);
395 zebra_create_database (zh, r->toKeep->databaseName);
397 case Z_ESAdminOriginPartToKeep_import:
398 yaz_log(LOG_LOG, "adm-import");
399 zebra_admin_import_begin (zh, r->toKeep->databaseName,
400 r->toKeep->u.import->recordType);
402 case Z_ESAdminOriginPartToKeep_refresh:
403 yaz_log(LOG_LOG, "adm-refresh");
405 case Z_ESAdminOriginPartToKeep_commit:
406 yaz_log(LOG_LOG, "adm-commit");
408 case Z_ESAdminOriginPartToKeep_shutdown:
409 yaz_log(LOG_LOG, "shutdown");
410 zebra_admin_shutdown(zh);
412 case Z_ESAdminOriginPartToKeep_start:
413 yaz_log(LOG_LOG, "start");
414 zebra_admin_start(zh);
417 yaz_log(LOG_LOG, "unknown admin");
419 if (r->toKeep->databaseName)
421 yaz_log(LOG_LOG, "database %s", r->toKeep->databaseName);
426 static int es_admin (ZebraHandle zh, Z_Admin *r)
430 case Z_Admin_esRequest:
431 es_admin_request (zh, r->u.esRequest);
433 case Z_Admin_taskPackage:
434 yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
443 int bend_segment (void *handle, bend_segment_rr *rr)
445 ZebraHandle zh = (ZebraHandle) handle;
446 Z_Segment *segment = rr->segment;
448 if (segment->num_segmentRecords)
449 zebra_admin_import_segment (zh, rr->segment);
451 zebra_admin_import_end (zh);
455 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
457 ZebraHandle zh = (ZebraHandle) handle;
459 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
460 if (rr->esr->packageName)
461 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
462 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
464 if (!rr->esr->taskSpecificParameters)
466 yaz_log (LOG_WARN, "No task specific parameters");
468 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
470 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
472 zebra_result (zh, &rr->errcode, &rr->errstring);
474 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
476 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
477 yaz_log (LOG_LOG, "Received DB Update");
478 if (up->which == Z_IUUpdate_esRequest)
480 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
481 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
482 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
484 yaz_log (LOG_LOG, "action");
487 switch (*toKeep->action)
489 case Z_IUOriginPartToKeep_recordInsert:
490 yaz_log (LOG_LOG, "recordInsert");
492 case Z_IUOriginPartToKeep_recordReplace:
493 yaz_log (LOG_LOG, "recordUpdate");
495 case Z_IUOriginPartToKeep_recordDelete:
496 yaz_log (LOG_LOG, "recordDelete");
498 case Z_IUOriginPartToKeep_elementUpdate:
499 yaz_log (LOG_LOG, "elementUpdate");
501 case Z_IUOriginPartToKeep_specialUpdate:
502 yaz_log (LOG_LOG, "specialUpdate");
504 case Z_ESAdminOriginPartToKeep_shutdown:
505 yaz_log (LOG_LOG, "shutDown");
507 case Z_ESAdminOriginPartToKeep_start:
508 yaz_log (LOG_LOG, "start");
511 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
514 if (toKeep->databaseName)
516 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
518 if (zebra_select_database(zh, toKeep->databaseName))
523 yaz_log (LOG_WARN, "no database supplied for ES Update");
525 rr->errstring = "database";
531 zebra_begin_trans (zh, 1);
532 for (i = 0; i < notToKeep->num; i++)
534 Z_External *rec = notToKeep->elements[i]->record;
535 struct oident *oident = 0;
536 Odr_oct *opaque_recid = 0;
539 if (notToKeep->elements[i]->u.opaque)
541 switch(notToKeep->elements[i]->which)
543 case Z_IUSuppliedRecords_elem_opaque:
544 opaque_recid = notToKeep->elements[i]->u.opaque;
545 break; /* OK, recid already set */
546 case Z_IUSuppliedRecords_elem_number:
547 sysno = *notToKeep->elements[i]->u.number;
551 if (rec->direct_reference)
553 oident = oid_getentbyoid(rec->direct_reference);
555 yaz_log (LOG_LOG, "record %d type %s", i,
560 case Z_External_sutrs:
561 if (rec->u.octet_aligned->len > 170)
562 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
566 yaz_log (LOG_LOG, "%d bytes:\n%s",
570 case Z_External_octet:
571 if (rec->u.octet_aligned->len > 170)
572 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
573 rec->u.octet_aligned->len,
574 rec->u.octet_aligned->buf);
576 yaz_log (LOG_LOG, "%d bytes\n%s",
577 rec->u.octet_aligned->len,
578 rec->u.octet_aligned->buf);
580 if (oident && oident->value != VAL_TEXT_XML)
583 rr->errstring = "only XML update supported";
586 if (rec->which == Z_External_octet)
590 if (*toKeep->action ==
591 Z_IUOriginPartToKeep_recordInsert)
593 if (*toKeep->action ==
594 Z_IUOriginPartToKeep_recordReplace)
596 if (*toKeep->action ==
597 Z_IUOriginPartToKeep_recordDelete)
599 if (*toKeep->action ==
600 Z_IUOriginPartToKeep_specialUpdate)
606 rr->errstring = "unsupported ES Update action";
609 else if (opaque_recid)
611 int r = zebra_admin_exchange_record (
613 rec->u.octet_aligned->buf,
614 rec->u.octet_aligned->len,
615 opaque_recid->buf, opaque_recid->len,
620 rr->errstring = "record exchange failed";
629 r = zebra_insert_record(
635 rec->u.octet_aligned->buf,
636 rec->u.octet_aligned->len);
640 rr->errstring = "insert_record failed";
645 r = zebra_update_record(
651 rec->u.octet_aligned->buf,
652 rec->u.octet_aligned->len,
657 rr->errstring = "update_record failed";
661 r = zebra_delete_record(
667 rec->u.octet_aligned->buf,
668 rec->u.octet_aligned->len,
673 rr->errstring = "delete_record failed";
680 zebra_end_trans (zh);
686 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
687 rr->esr->taskSpecificParameters->which);
694 static void bend_start (struct statserv_options_block *sob)
697 zebra_stop((ZebraService) sob->handle);
698 sob->handle = zebra_start(sob->configname, 0, 0);
701 yaz_log (LOG_FATAL, "Failed to read config `%s'", sob->configname);
713 zebra_pidfname(sob->handle, pidfname);
715 fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
720 yaz_log(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfname);
723 fd = open(pidfname, O_RDWR, 0666);
726 yaz_log(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfname);
730 area.l_type = F_WRLCK;
731 area.l_whence = SEEK_SET;
732 area.l_len = area.l_start = 0L;
733 if (fcntl (fd, F_SETLK, &area) == -1)
735 yaz_log(LOG_ERRNO|LOG_FATAL, "Zebra server already running");
742 sprintf (pidstr, "%ld", (long) getpid ());
743 write (fd, pidstr, strlen(pidstr));
749 static void bend_stop(struct statserv_options_block *sob)
754 if (!sob->inetd && sob->handle)
757 zebra_pidfname(sob->handle, pidfname);
763 ZebraService service = sob->handle;
768 int main (int argc, char **argv)
770 struct statserv_options_block *sob;
772 sob = statserv_getcontrol ();
773 strcpy (sob->configname, "zebra.cfg");
774 sob->bend_start = bend_start;
775 sob->bend_stop = bend_stop;
777 strcpy (sob->service_display_name, "Zebra Server");
779 statserv_setcontrol (sob);
781 return statserv_main (argc, argv, bend_init, bend_close);