1 /* $Id: zserver.c,v 1.127 2005-01-15 19:38:29 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
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 zebra_octet_term_encoding(zh, right_name);
158 q->charneg_response =
159 yaz_set_response_charneg (q->stream, charsets[i],
163 yaz_log (YLOG_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)
177 int type = Z_Term_general;
178 struct Z_External *ext;
179 Z_SearchInfoReport *sr;
181 /* get no of terms for result set */
182 no_terms = 0; /* zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0); */
183 /* FIXME - Rsets don't know number of terms no more ??? */
187 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
189 r->search_info->num_elements = 1;
190 r->search_info->list =
191 odr_malloc (r->stream, sizeof(*r->search_info->list));
192 r->search_info->list[0] =
193 odr_malloc (r->stream, sizeof(**r->search_info->list));
194 r->search_info->list[0]->category = 0;
195 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
196 ext = odr_malloc (r->stream, sizeof(*ext));
197 r->search_info->list[0]->information.externallyDefinedInfo = ext;
198 ext->direct_reference =
199 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
200 ext->indirect_reference = 0;
202 ext->which = Z_External_searchResult1;
203 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
204 ext->u.searchResult1 = sr;
206 sr->elements = odr_malloc (r->stream, sr->num *
207 sizeof(*sr->elements));
208 for (i = 0; i<no_terms; i++)
212 size_t len = sizeof(outbuf);
213 /* FIXME - Can we just skip this ??? */
215 zebra_resultSetTerms (zh, r->setname, i,
216 &count, &type, outbuf, &len);
218 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
219 sr->elements[i]->subqueryId = 0;
220 sr->elements[i]->fullQuery = odr_malloc (r->stream,
222 *sr->elements[i]->fullQuery = 0;
223 sr->elements[i]->subqueryExpression =
224 odr_malloc (r->stream, sizeof(Z_QueryExpression));
225 sr->elements[i]->subqueryExpression->which =
226 Z_QueryExpression_term;
227 sr->elements[i]->subqueryExpression->u.term =
228 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
229 term = odr_malloc (r->stream, sizeof(Z_Term));
230 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
233 case Z_Term_characterString:
234 yaz_log (YLOG_DEBUG, "term as characterString");
235 term->which = Z_Term_characterString;
236 term->u.characterString = odr_strdup (r->stream, outbuf);
239 yaz_log (YLOG_DEBUG, "term as general");
240 term->which = Z_Term_general;
241 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
242 term->u.general->size = term->u.general->len = len;
243 term->u.general->buf = odr_malloc (r->stream, len);
244 memcpy (term->u.general->buf, outbuf, len);
247 term->which = Z_Term_general;
248 term->u.null = odr_nullval();
250 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
251 sr->elements[i]->subqueryInterpretation = 0;
252 sr->elements[i]->subqueryRecommendation = 0;
253 if (count > 2000000000)
255 sr->elements[i]->subqueryCount = odr_intdup (r->stream, (int) count);
256 sr->elements[i]->subqueryWeight = 0;
257 sr->elements[i]->resultsByDB = 0;
261 int bend_search (void *handle, bend_search_rr *r)
263 ZebraHandle zh = (ZebraHandle) handle;
269 if (zebra_select_databases (zh, r->num_bases,
270 (const char **) r->basenames))
272 zebra_result (zh, &r->errcode, &r->errstring);
275 yaz_log (YLOG_DEBUG, "ResultSet '%s'", r->setname);
276 switch (r->query->which)
278 case Z_Query_type_1: case Z_Query_type_101:
279 zebra_search_RPN (zh, r->stream, r->query->u.type_1,
280 r->setname, &r->hits);
281 zebra_result (zh, &r->errcode, &r->errstring);
283 search_terms (zh, r);
287 r->errstring = "type-2";
296 int bend_fetch (void *handle, bend_fetch_rr *r)
298 ZebraHandle zh = (ZebraHandle) handle;
299 ZebraRetrievalRecord retrievalRecord;
301 retrievalRecord.position = r->number;
304 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
305 r->request_format, 1, &retrievalRecord);
306 zebra_result (zh, &r->errcode, &r->errstring);
307 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
308 if (r->errcode == 0 && retrievalRecord.errCode)
310 r->surrogate_flag = 1;
311 r->errcode = retrievalRecord.errCode;
312 r->errstring = retrievalRecord.errString;
313 r->basename = retrievalRecord.base;
315 else if (r->errcode == 0) /* Database Record */
318 r->basename = retrievalRecord.base;
319 r->record = retrievalRecord.buf;
320 r->len = retrievalRecord.len;
321 r->output_format = retrievalRecord.format;
326 static int bend_scan (void *handle, bend_scan_rr *r)
328 ZebraScanEntry *entries;
329 ZebraHandle zh = (ZebraHandle) handle;
332 if (zebra_select_databases (zh, r->num_bases,
333 (const char **) r->basenames))
335 zebra_result (zh, &r->errcode, &r->errstring);
338 if (r->step_size != 0 && *r->step_size != 0) {
339 r->errcode = 205; /* "Only zero step size supported for Scan" */
343 r->entries = (struct scan_entry *)
344 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
345 zebra_scan (zh, r->stream, r->term,
348 &r->num_entries, &entries, &is_partial);
350 r->status = BEND_SCAN_PARTIAL;
352 r->status = BEND_SCAN_SUCCESS;
353 for (i = 0; i < r->num_entries; i++)
355 r->entries[i].term = entries[i].term;
356 r->entries[i].occurrences = entries[i].occurrences;
358 zebra_result (zh, &r->errcode, &r->errstring);
362 void bend_close (void *handle)
364 zebra_close ((ZebraHandle) handle);
365 xmalloc_trav("bend_close");
369 int bend_sort (void *handle, bend_sort_rr *rr)
371 ZebraHandle zh = (ZebraHandle) handle;
373 zebra_sort (zh, rr->stream,
374 rr->num_input_setnames, (const char **) rr->input_setnames,
375 rr->output_setname, rr->sort_sequence, &rr->sort_status);
376 zebra_result (zh, &rr->errcode, &rr->errstring);
380 int bend_delete (void *handle, bend_delete_rr *rr)
382 ZebraHandle zh = (ZebraHandle) handle;
384 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
385 rr->num_setnames, rr->setnames,
390 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
392 if (r->toKeep->databaseName)
394 yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
396 switch (r->toKeep->which)
398 case Z_ESAdminOriginPartToKeep_reIndex:
399 yaz_log(YLOG_LOG, "adm-reindex");
401 case Z_ESAdminOriginPartToKeep_truncate:
402 yaz_log(YLOG_LOG, "adm-truncate");
404 case Z_ESAdminOriginPartToKeep_drop:
405 yaz_log(YLOG_LOG, "adm-drop");
406 zebra_drop_database (zh, r->toKeep->databaseName);
408 case Z_ESAdminOriginPartToKeep_create:
409 yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
410 zebra_create_database (zh, r->toKeep->databaseName);
412 case Z_ESAdminOriginPartToKeep_import:
413 yaz_log(YLOG_LOG, "adm-import");
414 zebra_admin_import_begin (zh, r->toKeep->databaseName,
415 r->toKeep->u.import->recordType);
417 case Z_ESAdminOriginPartToKeep_refresh:
418 yaz_log(YLOG_LOG, "adm-refresh");
420 case Z_ESAdminOriginPartToKeep_commit:
421 yaz_log(YLOG_LOG, "adm-commit");
422 if (r->toKeep->databaseName)
423 zebra_select_database(zh, r->toKeep->databaseName);
426 case Z_ESAdminOriginPartToKeep_shutdown:
427 yaz_log(YLOG_LOG, "shutdown");
428 zebra_admin_shutdown(zh);
430 case Z_ESAdminOriginPartToKeep_start:
431 yaz_log(YLOG_LOG, "start");
432 zebra_admin_start(zh);
435 yaz_log(YLOG_LOG, "unknown admin");
440 static int es_admin (ZebraHandle zh, Z_Admin *r)
444 case Z_Admin_esRequest:
445 es_admin_request (zh, r->u.esRequest);
447 case Z_Admin_taskPackage:
448 yaz_log (YLOG_LOG, "adm taskpackage (unhandled)");
457 int bend_segment (void *handle, bend_segment_rr *rr)
459 ZebraHandle zh = (ZebraHandle) handle;
460 Z_Segment *segment = rr->segment;
462 if (segment->num_segmentRecords)
463 zebra_admin_import_segment (zh, rr->segment);
465 zebra_admin_import_end (zh);
469 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
471 ZebraHandle zh = (ZebraHandle) handle;
473 yaz_log(YLOG_LOG, "function: %d", *rr->esr->function);
474 if (rr->esr->packageName)
475 yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
476 yaz_log(YLOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
478 if (!rr->esr->taskSpecificParameters)
480 yaz_log (YLOG_WARN, "No task specific parameters");
482 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
484 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
486 zebra_result (zh, &rr->errcode, &rr->errstring);
488 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
490 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
491 yaz_log (YLOG_LOG, "Received DB Update");
492 if (up->which == Z_IUUpdate_esRequest)
494 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
495 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
496 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
498 yaz_log (YLOG_LOG, "action");
501 switch (*toKeep->action)
503 case Z_IUOriginPartToKeep_recordInsert:
504 yaz_log (YLOG_LOG, "recordInsert");
506 case Z_IUOriginPartToKeep_recordReplace:
507 yaz_log (YLOG_LOG, "recordUpdate");
509 case Z_IUOriginPartToKeep_recordDelete:
510 yaz_log (YLOG_LOG, "recordDelete");
512 case Z_IUOriginPartToKeep_elementUpdate:
513 yaz_log (YLOG_LOG, "elementUpdate");
515 case Z_IUOriginPartToKeep_specialUpdate:
516 yaz_log (YLOG_LOG, "specialUpdate");
518 case Z_ESAdminOriginPartToKeep_shutdown:
519 yaz_log (YLOG_LOG, "shutDown");
521 case Z_ESAdminOriginPartToKeep_start:
522 yaz_log (YLOG_LOG, "start");
525 yaz_log (YLOG_LOG, " unknown (%d)", *toKeep->action);
528 if (toKeep->databaseName)
530 yaz_log (YLOG_LOG, "database: %s", toKeep->databaseName);
532 if (zebra_select_database(zh, toKeep->databaseName))
537 yaz_log (YLOG_WARN, "no database supplied for ES Update");
539 rr->errstring = "database";
545 zebra_begin_trans (zh, 1);
546 for (i = 0; i < notToKeep->num; i++)
548 Z_External *rec = notToKeep->elements[i]->record;
549 struct oident *oident = 0;
550 Odr_oct *opaque_recid = 0;
553 if (notToKeep->elements[i]->u.opaque)
555 switch(notToKeep->elements[i]->which)
557 case Z_IUSuppliedRecords_elem_opaque:
558 opaque_recid = notToKeep->elements[i]->u.opaque;
559 break; /* OK, recid already set */
560 case Z_IUSuppliedRecords_elem_number:
561 sysno = *notToKeep->elements[i]->u.number;
565 if (rec->direct_reference)
567 oident = oid_getentbyoid(rec->direct_reference);
569 yaz_log (YLOG_LOG, "record %d type %s", i,
574 case Z_External_sutrs:
575 if (rec->u.octet_aligned->len > 170)
576 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
580 yaz_log (YLOG_LOG, "%d bytes:\n%s",
584 case Z_External_octet:
585 if (rec->u.octet_aligned->len > 170)
586 yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
587 rec->u.octet_aligned->len,
588 rec->u.octet_aligned->buf);
590 yaz_log (YLOG_LOG, "%d bytes\n%s",
591 rec->u.octet_aligned->len,
592 rec->u.octet_aligned->buf);
594 if (oident && oident->value != VAL_TEXT_XML)
597 rr->errstring = "only XML update supported";
600 if (rec->which == Z_External_octet)
604 if (*toKeep->action ==
605 Z_IUOriginPartToKeep_recordInsert)
607 if (*toKeep->action ==
608 Z_IUOriginPartToKeep_recordReplace)
610 if (*toKeep->action ==
611 Z_IUOriginPartToKeep_recordDelete)
613 if (*toKeep->action ==
614 Z_IUOriginPartToKeep_specialUpdate)
620 rr->errstring = "unsupported ES Update action";
623 else if (opaque_recid)
625 int r = zebra_admin_exchange_record (
627 rec->u.octet_aligned->buf,
628 rec->u.octet_aligned->len,
629 opaque_recid->buf, opaque_recid->len,
634 rr->errstring = "record exchange failed";
643 r = zebra_insert_record(
649 rec->u.octet_aligned->buf,
650 rec->u.octet_aligned->len,
655 rr->errstring = "insert_record failed";
660 r = zebra_update_record(
666 rec->u.octet_aligned->buf,
667 rec->u.octet_aligned->len,
672 rr->errstring = "update_record failed";
676 r = zebra_delete_record(
682 rec->u.octet_aligned->buf,
683 rec->u.octet_aligned->len,
688 rr->errstring = "delete_record failed";
695 zebra_end_trans (zh);
701 yaz_log (YLOG_WARN, "Unknown Extended Service(%d)",
702 rr->esr->taskSpecificParameters->which);
709 static void bend_start (struct statserv_options_block *sob)
712 zebra_stop((ZebraService) sob->handle);
713 sob->handle = zebra_start(sob->configname);
716 yaz_log (YLOG_FATAL, "Failed to read config `%s'", sob->configname);
728 zebra_pidfname(sob->handle, pidfname);
730 fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
735 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
738 fd = open(pidfname, O_RDWR, 0666);
741 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
745 area.l_type = F_WRLCK;
746 area.l_whence = SEEK_SET;
747 area.l_len = area.l_start = 0L;
748 if (fcntl (fd, F_SETLK, &area) == -1)
750 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
757 sprintf (pidstr, "%ld", (long) getpid ());
758 write (fd, pidstr, strlen(pidstr));
764 static void bend_stop(struct statserv_options_block *sob)
769 if (!sob->inetd && sob->handle)
772 zebra_pidfname(sob->handle, pidfname);
778 ZebraService service = sob->handle;
783 int main (int argc, char **argv)
785 struct statserv_options_block *sob;
787 sob = statserv_getcontrol ();
788 strcpy (sob->configname, "zebra.cfg");
789 sob->bend_start = bend_start;
790 sob->bend_stop = bend_stop;
792 strcpy (sob->service_display_name, "Zebra Server");
794 statserv_setcontrol (sob);
796 return statserv_main (argc, argv, bend_init, bend_close);