1 /* $Id: zserver.c,v 1.117.2.2 2005-01-16 23:13:29 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
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 (LOG_DEBUG, "bend_init");
76 sob = statserv_getcontrol ();
77 if (!(zh = zebra_open (sob->handle)))
79 yaz_log (LOG_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 (LOG_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 (LOG_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 q->charneg_response =
159 yaz_set_response_charneg (q->stream, charsets[i],
163 yaz_log (LOG_LOG, "charset %d %s (proper name %s): unsupported", i,
164 charsets[i], right_name);
172 static void search_terms (ZebraHandle zh, bend_search_rr *r)
178 struct Z_External *ext;
179 Z_SearchInfoReport *sr;
181 /* get no of terms for result set */
182 no_terms = zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0);
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 zebra_resultSetTerms (zh, r->setname, i,
213 &count, &type, outbuf, &len);
215 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
216 sr->elements[i]->subqueryId = 0;
217 sr->elements[i]->fullQuery = odr_malloc (r->stream,
219 *sr->elements[i]->fullQuery = 0;
220 sr->elements[i]->subqueryExpression =
221 odr_malloc (r->stream, sizeof(Z_QueryExpression));
222 sr->elements[i]->subqueryExpression->which =
223 Z_QueryExpression_term;
224 sr->elements[i]->subqueryExpression->u.term =
225 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
226 term = odr_malloc (r->stream, sizeof(Z_Term));
227 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
230 case Z_Term_characterString:
231 yaz_log (LOG_DEBUG, "term as characterString");
232 term->which = Z_Term_characterString;
233 term->u.characterString = odr_strdup (r->stream, outbuf);
236 yaz_log (LOG_DEBUG, "term as general");
237 term->which = Z_Term_general;
238 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
239 term->u.general->size = term->u.general->len = len;
240 term->u.general->buf = odr_malloc (r->stream, len);
241 memcpy (term->u.general->buf, outbuf, len);
244 term->which = Z_Term_general;
245 term->u.null = odr_nullval();
247 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
248 sr->elements[i]->subqueryInterpretation = 0;
249 sr->elements[i]->subqueryRecommendation = 0;
250 sr->elements[i]->subqueryCount = odr_intdup (r->stream, count);
251 sr->elements[i]->subqueryWeight = 0;
252 sr->elements[i]->resultsByDB = 0;
256 int bend_search (void *handle, bend_search_rr *r)
258 ZebraHandle zh = (ZebraHandle) handle;
264 if (zebra_select_databases (zh, r->num_bases,
265 (const char **) r->basenames))
267 zebra_result (zh, &r->errcode, &r->errstring);
270 yaz_log (LOG_LOG, "ResultSet '%s'", r->setname);
271 switch (r->query->which)
273 case Z_Query_type_1: case Z_Query_type_101:
274 zebra_search_RPN (zh, r->stream, r->query->u.type_1,
275 r->setname, &r->hits);
276 zebra_result (zh, &r->errcode, &r->errstring);
278 search_terms (zh, r);
282 r->errstring = "type-2";
291 int bend_fetch (void *handle, bend_fetch_rr *r)
293 ZebraHandle zh = (ZebraHandle) handle;
294 ZebraRetrievalRecord retrievalRecord;
296 retrievalRecord.position = r->number;
299 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
300 r->request_format, 1, &retrievalRecord);
301 zebra_result (zh, &r->errcode, &r->errstring);
302 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
303 if (r->errcode == 0 && retrievalRecord.errCode)
305 r->surrogate_flag = 1;
306 r->errcode = retrievalRecord.errCode;
307 r->errstring = retrievalRecord.errString;
308 r->basename = retrievalRecord.base;
310 else if (r->errcode == 0) /* Database Record */
313 r->basename = retrievalRecord.base;
314 r->record = retrievalRecord.buf;
315 r->len = retrievalRecord.len;
316 r->output_format = retrievalRecord.format;
321 static int bend_scan (void *handle, bend_scan_rr *r)
323 ZebraScanEntry *entries;
324 ZebraHandle zh = (ZebraHandle) handle;
327 if (zebra_select_databases (zh, r->num_bases,
328 (const char **) r->basenames))
330 zebra_result (zh, &r->errcode, &r->errstring);
333 if (r->step_size != 0 && *r->step_size != 0) {
334 r->errcode = 205; /* "Only zero step size supported for Scan" */
338 r->entries = (struct scan_entry *)
339 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
340 zebra_scan (zh, r->stream, r->term,
343 &r->num_entries, &entries, &is_partial);
345 r->status = BEND_SCAN_PARTIAL;
347 r->status = BEND_SCAN_SUCCESS;
348 for (i = 0; i < r->num_entries; i++)
350 r->entries[i].term = entries[i].term;
351 r->entries[i].occurrences = entries[i].occurrences;
353 zebra_result (zh, &r->errcode, &r->errstring);
357 void bend_close (void *handle)
359 zebra_close ((ZebraHandle) handle);
360 xmalloc_trav("bend_close");
364 int bend_sort (void *handle, bend_sort_rr *rr)
366 ZebraHandle zh = (ZebraHandle) handle;
368 zebra_sort (zh, rr->stream,
369 rr->num_input_setnames, (const char **) rr->input_setnames,
370 rr->output_setname, rr->sort_sequence, &rr->sort_status);
371 zebra_result (zh, &rr->errcode, &rr->errstring);
375 int bend_delete (void *handle, bend_delete_rr *rr)
377 ZebraHandle zh = (ZebraHandle) handle;
379 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
380 rr->num_setnames, rr->setnames,
385 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
387 if (r->toKeep->databaseName)
389 yaz_log(LOG_LOG, "adm request database %s", r->toKeep->databaseName);
391 switch (r->toKeep->which)
393 case Z_ESAdminOriginPartToKeep_reIndex:
394 yaz_log(LOG_LOG, "adm-reindex");
396 case Z_ESAdminOriginPartToKeep_truncate:
397 yaz_log(LOG_LOG, "adm-truncate");
399 case Z_ESAdminOriginPartToKeep_drop:
400 yaz_log(LOG_LOG, "adm-drop");
401 zebra_drop_database (zh, r->toKeep->databaseName);
403 case Z_ESAdminOriginPartToKeep_create:
404 yaz_log(LOG_LOG, "adm-create %s", r->toKeep->databaseName);
405 zebra_create_database (zh, r->toKeep->databaseName);
407 case Z_ESAdminOriginPartToKeep_import:
408 yaz_log(LOG_LOG, "adm-import");
409 zebra_admin_import_begin (zh, r->toKeep->databaseName,
410 r->toKeep->u.import->recordType);
412 case Z_ESAdminOriginPartToKeep_refresh:
413 yaz_log(LOG_LOG, "adm-refresh");
415 case Z_ESAdminOriginPartToKeep_commit:
416 yaz_log(LOG_LOG, "adm-commit");
417 if (r->toKeep->databaseName)
418 zebra_select_database(zh, r->toKeep->databaseName);
421 case Z_ESAdminOriginPartToKeep_shutdown:
422 yaz_log(LOG_LOG, "shutdown");
423 zebra_admin_shutdown(zh);
425 case Z_ESAdminOriginPartToKeep_start:
426 yaz_log(LOG_LOG, "start");
427 zebra_admin_start(zh);
430 yaz_log(LOG_LOG, "unknown admin");
435 static int es_admin (ZebraHandle zh, Z_Admin *r)
439 case Z_Admin_esRequest:
440 es_admin_request (zh, r->u.esRequest);
442 case Z_Admin_taskPackage:
443 yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
452 int bend_segment (void *handle, bend_segment_rr *rr)
454 ZebraHandle zh = (ZebraHandle) handle;
455 Z_Segment *segment = rr->segment;
457 if (segment->num_segmentRecords)
458 zebra_admin_import_segment (zh, rr->segment);
460 zebra_admin_import_end (zh);
464 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
466 ZebraHandle zh = (ZebraHandle) handle;
468 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
469 if (rr->esr->packageName)
470 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
471 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
473 if (!rr->esr->taskSpecificParameters)
475 yaz_log (LOG_WARN, "No task specific parameters");
477 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
479 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
481 zebra_result (zh, &rr->errcode, &rr->errstring);
483 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
485 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
486 yaz_log (LOG_LOG, "Received DB Update");
487 if (up->which == Z_IUUpdate_esRequest)
489 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
490 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
491 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
493 yaz_log (LOG_LOG, "action");
496 switch (*toKeep->action)
498 case Z_IUOriginPartToKeep_recordInsert:
499 yaz_log (LOG_LOG, "recordInsert");
501 case Z_IUOriginPartToKeep_recordReplace:
502 yaz_log (LOG_LOG, "recordUpdate");
504 case Z_IUOriginPartToKeep_recordDelete:
505 yaz_log (LOG_LOG, "recordDelete");
507 case Z_IUOriginPartToKeep_elementUpdate:
508 yaz_log (LOG_LOG, "elementUpdate");
510 case Z_IUOriginPartToKeep_specialUpdate:
511 yaz_log (LOG_LOG, "specialUpdate");
513 case Z_ESAdminOriginPartToKeep_shutdown:
514 yaz_log (LOG_LOG, "shutDown");
516 case Z_ESAdminOriginPartToKeep_start:
517 yaz_log (LOG_LOG, "start");
520 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
523 if (toKeep->databaseName)
525 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
527 if (zebra_select_database(zh, toKeep->databaseName))
532 yaz_log (LOG_WARN, "no database supplied for ES Update");
534 rr->errstring = "database";
540 zebra_begin_trans (zh, 1);
541 for (i = 0; i < notToKeep->num; i++)
543 Z_External *rec = notToKeep->elements[i]->record;
544 struct oident *oident = 0;
545 Odr_oct *opaque_recid = 0;
548 if (notToKeep->elements[i]->u.opaque)
550 switch(notToKeep->elements[i]->which)
552 case Z_IUSuppliedRecords_elem_opaque:
553 opaque_recid = notToKeep->elements[i]->u.opaque;
554 break; /* OK, recid already set */
555 case Z_IUSuppliedRecords_elem_number:
556 sysno = *notToKeep->elements[i]->u.number;
560 if (rec->direct_reference)
562 oident = oid_getentbyoid(rec->direct_reference);
564 yaz_log (LOG_LOG, "record %d type %s", i,
569 case Z_External_sutrs:
570 if (rec->u.octet_aligned->len > 170)
571 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
575 yaz_log (LOG_LOG, "%d bytes:\n%s",
579 case Z_External_octet:
580 if (rec->u.octet_aligned->len > 170)
581 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
582 rec->u.octet_aligned->len,
583 rec->u.octet_aligned->buf);
585 yaz_log (LOG_LOG, "%d bytes\n%s",
586 rec->u.octet_aligned->len,
587 rec->u.octet_aligned->buf);
589 if (oident && oident->value != VAL_TEXT_XML)
592 rr->errstring = "only XML update supported";
595 if (rec->which == Z_External_octet)
599 if (*toKeep->action ==
600 Z_IUOriginPartToKeep_recordInsert)
602 if (*toKeep->action ==
603 Z_IUOriginPartToKeep_recordReplace)
605 if (*toKeep->action ==
606 Z_IUOriginPartToKeep_recordDelete)
608 if (*toKeep->action ==
609 Z_IUOriginPartToKeep_specialUpdate)
615 rr->errstring = "unsupported ES Update action";
618 else if (opaque_recid)
620 int r = zebra_admin_exchange_record (
622 rec->u.octet_aligned->buf,
623 rec->u.octet_aligned->len,
624 opaque_recid->buf, opaque_recid->len,
629 rr->errstring = "record exchange failed";
638 r = zebra_insert_record(
644 rec->u.octet_aligned->buf,
645 rec->u.octet_aligned->len,
649 yaz_log(LOG_WARN, "zebra_insert_record failed r=%d", r);
651 rr->errstring = "insert_record failed";
656 r = zebra_update_record(
662 rec->u.octet_aligned->buf,
663 rec->u.octet_aligned->len,
667 yaz_log(LOG_WARN, "zebra_update_record failed r=%d", r);
670 "update_record failed";
674 r = zebra_delete_record(
680 rec->u.octet_aligned->buf,
681 rec->u.octet_aligned->len,
685 yaz_log(LOG_WARN, "zebra_delete_record failed r=%d", r);
687 rr->errstring = "delete_record failed";
694 zebra_end_trans (zh);
700 yaz_log (LOG_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 (LOG_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(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfname);
737 fd = open(pidfname, O_RDWR, 0666);
740 yaz_log(LOG_FATAL|LOG_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(LOG_ERRNO|LOG_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);