1 /* $Id: zserver.c,v 1.107 2003-05-24 22:35:11 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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);
84 if (q->auth->which == Z_IdAuthentication_open)
86 char *openpass = xstrdup (q->auth->u.open);
87 char *cp = strchr (openpass, '/');
91 user = nmem_strdup (odr_getmem (q->stream), openpass);
92 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
97 if (zebra_auth (zh, user, passwd))
105 if (q->charneg_request) /* characater set and langauge negotiation? */
113 NMEM nmem = nmem_create();
115 yaz_log (LOG_LOG, "character set and language negotiation");
117 yaz_get_proposal_charneg (nmem, q->charneg_request,
118 &charsets, &num_charsets,
119 &langs, &num_langs, &selected);
121 for (i = 0; i < num_charsets; i++)
123 const char *right_name = "";
125 * FIXME! It is like rudiment :-))
126 * We have to support this short names of character sets,
127 * because a lot servers in Russia to use own in during
128 * character set and language negotiation still.
131 if (!yaz_matchstr(charsets[i], "win")) {
132 right_name = "WINDOWS-1251";
133 } else if (!yaz_matchstr(charsets[i], "koi")) {
134 right_name = "KOI8-R";
135 } else if (!yaz_matchstr(charsets[i], "iso")) {
136 right_name = "ISO-8859-5";
137 } else if (!yaz_matchstr(charsets[i], "dos")) {
138 right_name = "CP866";
139 } else if (!yaz_matchstr(charsets[i], "uni")) {
140 right_name = "UTF-8";
142 right_name = charsets[i];
144 if (odr_set_charset (q->decode, "UTF-8", right_name) == 0)
146 yaz_log (LOG_LOG, "charset %d %s (proper name %s): OK", i,
147 charsets[i], right_name);
148 odr_set_charset (q->stream, right_name, "UTF-8");
150 zebra_record_encoding (zh, right_name);
151 q->charneg_response =
152 yaz_set_response_charneg (q->stream, right_name,
156 yaz_log (LOG_LOG, "charset %d %s (proper name %s): unsupported", i,
157 charsets[i], right_name);
165 static void search_terms (ZebraHandle zh, bend_search_rr *r)
171 struct Z_External *ext;
172 Z_SearchInfoReport *sr;
174 /* get no of terms for result set */
175 no_terms = zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0);
179 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
181 r->search_info->num_elements = 1;
182 r->search_info->list =
183 odr_malloc (r->stream, sizeof(*r->search_info->list));
184 r->search_info->list[0] =
185 odr_malloc (r->stream, sizeof(**r->search_info->list));
186 r->search_info->list[0]->category = 0;
187 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
188 ext = odr_malloc (r->stream, sizeof(*ext));
189 r->search_info->list[0]->information.externallyDefinedInfo = ext;
190 ext->direct_reference =
191 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
192 ext->indirect_reference = 0;
194 ext->which = Z_External_searchResult1;
195 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
196 ext->u.searchResult1 = sr;
198 sr->elements = odr_malloc (r->stream, sr->num *
199 sizeof(*sr->elements));
200 for (i = 0; i<no_terms; i++)
204 size_t len = sizeof(outbuf);
205 zebra_resultSetTerms (zh, r->setname, i,
206 &count, &type, outbuf, &len);
208 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
209 sr->elements[i]->subqueryId = 0;
210 sr->elements[i]->fullQuery = odr_malloc (r->stream,
212 *sr->elements[i]->fullQuery = 0;
213 sr->elements[i]->subqueryExpression =
214 odr_malloc (r->stream, sizeof(Z_QueryExpression));
215 sr->elements[i]->subqueryExpression->which =
216 Z_QueryExpression_term;
217 sr->elements[i]->subqueryExpression->u.term =
218 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
219 term = odr_malloc (r->stream, sizeof(Z_Term));
220 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
223 case Z_Term_characterString:
224 yaz_log (LOG_DEBUG, "term as characterString");
225 term->which = Z_Term_characterString;
226 term->u.characterString = odr_strdup (r->stream, outbuf);
229 yaz_log (LOG_DEBUG, "term as general");
230 term->which = Z_Term_general;
231 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
232 term->u.general->size = term->u.general->len = len;
233 term->u.general->buf = odr_malloc (r->stream, len);
234 memcpy (term->u.general->buf, outbuf, len);
237 term->which = Z_Term_general;
238 term->u.null = odr_nullval();
240 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
241 sr->elements[i]->subqueryInterpretation = 0;
242 sr->elements[i]->subqueryRecommendation = 0;
243 sr->elements[i]->subqueryCount = odr_intdup (r->stream, count);
244 sr->elements[i]->subqueryWeight = 0;
245 sr->elements[i]->resultsByDB = 0;
249 int bend_search (void *handle, bend_search_rr *r)
251 ZebraHandle zh = (ZebraHandle) handle;
257 if (zebra_select_databases (zh, r->num_bases,
258 (const char **) r->basenames))
260 zebra_result (zh, &r->errcode, &r->errstring);
263 yaz_log (LOG_LOG, "ResultSet '%s'", r->setname);
264 switch (r->query->which)
266 case Z_Query_type_1: case Z_Query_type_101:
267 zebra_search_RPN (zh, r->query->u.type_1,
268 r->setname, &r->hits);
269 zebra_result (zh, &r->errcode, &r->errstring);
271 search_terms (zh, r);
275 r->errstring = "type-2";
284 int bend_fetch (void *handle, bend_fetch_rr *r)
286 ZebraHandle zh = (ZebraHandle) handle;
287 ZebraRetrievalRecord retrievalRecord;
289 retrievalRecord.position = r->number;
292 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
293 r->request_format, 1, &retrievalRecord);
294 zebra_result (zh, &r->errcode, &r->errstring);
295 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
296 if (r->errcode == 0 && retrievalRecord.errCode)
298 r->surrogate_flag = 1;
299 r->errcode = retrievalRecord.errCode;
300 r->errstring = retrievalRecord.errString;
301 r->basename = retrievalRecord.base;
303 else if (r->errcode == 0) /* Database Record */
306 r->basename = retrievalRecord.base;
307 r->record = retrievalRecord.buf;
308 r->len = retrievalRecord.len;
309 r->output_format = retrievalRecord.format;
314 static int bend_scan (void *handle, bend_scan_rr *r)
316 ZebraScanEntry *entries;
317 ZebraHandle zh = (ZebraHandle) handle;
320 if (zebra_select_databases (zh, r->num_bases,
321 (const char **) r->basenames))
323 zebra_result (zh, &r->errcode, &r->errstring);
326 r->entries = (struct scan_entry *)
327 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
328 zebra_scan (zh, r->stream, r->term,
331 &r->num_entries, &entries, &is_partial);
333 r->status = BEND_SCAN_PARTIAL;
335 r->status = BEND_SCAN_SUCCESS;
336 for (i = 0; i < r->num_entries; i++)
338 r->entries[i].term = entries[i].term;
339 r->entries[i].occurrences = entries[i].occurrences;
341 zebra_result (zh, &r->errcode, &r->errstring);
345 void bend_close (void *handle)
347 zebra_close ((ZebraHandle) handle);
348 xmalloc_trav("bend_close");
352 int bend_sort (void *handle, bend_sort_rr *rr)
354 ZebraHandle zh = (ZebraHandle) handle;
356 zebra_sort (zh, rr->stream,
357 rr->num_input_setnames, (const char **) rr->input_setnames,
358 rr->output_setname, rr->sort_sequence, &rr->sort_status);
359 zebra_result (zh, &rr->errcode, &rr->errstring);
363 int bend_delete (void *handle, bend_delete_rr *rr)
365 ZebraHandle zh = (ZebraHandle) handle;
367 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
368 rr->num_setnames, rr->setnames,
373 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
375 switch (r->toKeep->which)
377 case Z_ESAdminOriginPartToKeep_reIndex:
378 yaz_log(LOG_LOG, "adm-reindex");
380 case Z_ESAdminOriginPartToKeep_truncate:
381 yaz_log(LOG_LOG, "adm-truncate");
383 case Z_ESAdminOriginPartToKeep_drop:
384 yaz_log(LOG_LOG, "adm-drop");
386 case Z_ESAdminOriginPartToKeep_create:
387 yaz_log(LOG_LOG, "adm-create %s", r->toKeep->databaseName);
388 zebra_create_database (zh, r->toKeep->databaseName);
390 case Z_ESAdminOriginPartToKeep_import:
391 yaz_log(LOG_LOG, "adm-import");
392 zebra_admin_import_begin (zh, r->toKeep->databaseName,
393 r->toKeep->u.import->recordType);
395 case Z_ESAdminOriginPartToKeep_refresh:
396 yaz_log(LOG_LOG, "adm-refresh");
398 case Z_ESAdminOriginPartToKeep_commit:
399 yaz_log(LOG_LOG, "adm-commit");
401 case Z_ESAdminOriginPartToKeep_shutdown:
402 yaz_log(LOG_LOG, "shutdown");
403 zebra_admin_shutdown(zh);
405 case Z_ESAdminOriginPartToKeep_start:
406 yaz_log(LOG_LOG, "start");
407 zebra_admin_start(zh);
410 yaz_log(LOG_LOG, "unknown admin");
412 if (r->toKeep->databaseName)
414 yaz_log(LOG_LOG, "database %s", r->toKeep->databaseName);
419 static int es_admin (ZebraHandle zh, Z_Admin *r)
423 case Z_Admin_esRequest:
424 es_admin_request (zh, r->u.esRequest);
426 case Z_Admin_taskPackage:
427 yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
436 int bend_segment (void *handle, bend_segment_rr *rr)
438 ZebraHandle zh = (ZebraHandle) handle;
439 Z_Segment *segment = rr->segment;
441 if (segment->num_segmentRecords)
442 zebra_admin_import_segment (zh, rr->segment);
444 zebra_admin_import_end (zh);
448 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
450 ZebraHandle zh = (ZebraHandle) handle;
452 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
453 if (rr->esr->packageName)
454 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
455 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
457 if (!rr->esr->taskSpecificParameters)
459 yaz_log (LOG_WARN, "No task specific parameters");
461 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
463 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
465 zebra_result (zh, &rr->errcode, &rr->errstring);
467 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
469 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
470 yaz_log (LOG_LOG, "Received DB Update");
471 if (up->which == Z_IUUpdate_esRequest)
473 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
474 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
475 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
477 yaz_log (LOG_LOG, "action");
480 switch (*toKeep->action)
482 case Z_IUOriginPartToKeep_recordInsert:
483 yaz_log (LOG_LOG, "recordInsert");
485 case Z_IUOriginPartToKeep_recordReplace:
486 yaz_log (LOG_LOG, "recordUpdate");
488 case Z_IUOriginPartToKeep_recordDelete:
489 yaz_log (LOG_LOG, "recordDelete");
491 case Z_IUOriginPartToKeep_elementUpdate:
492 yaz_log (LOG_LOG, "elementUpdate");
494 case Z_IUOriginPartToKeep_specialUpdate:
495 yaz_log (LOG_LOG, "specialUpdate");
497 case Z_ESAdminOriginPartToKeep_shutdown:
498 yaz_log (LOG_LOG, "shutDown");
500 case Z_ESAdminOriginPartToKeep_start:
501 yaz_log (LOG_LOG, "start");
504 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
507 if (toKeep->databaseName)
509 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
511 if (zebra_select_database(zh, toKeep->databaseName))
516 yaz_log (LOG_WARN, "no database supplied for ES Update");
518 rr->errstring = "database";
524 zebra_begin_trans (zh, 1);
525 for (i = 0; i < notToKeep->num; i++)
527 Z_External *rec = notToKeep->elements[i]->record;
528 struct oident *oident = 0;
529 Odr_oct *recid = notToKeep->elements[i]->u.opaque;
534 rr->errstring = "record Id not supplied";
537 if (notToKeep->elements[i]->which !=
538 Z_IUSuppliedRecords_elem_opaque)
541 rr->errstring = "only opaque record ID supported";
545 if (rec->direct_reference)
547 oident = oid_getentbyoid(rec->direct_reference);
549 yaz_log (LOG_LOG, "record %d type %s", i,
554 case Z_External_sutrs:
555 if (rec->u.octet_aligned->len > 170)
556 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
560 yaz_log (LOG_LOG, "%d bytes:\n%s",
564 case Z_External_octet:
565 if (rec->u.octet_aligned->len > 170)
566 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
567 rec->u.octet_aligned->len,
568 rec->u.octet_aligned->buf);
570 yaz_log (LOG_LOG, "%d bytes\n%s",
571 rec->u.octet_aligned->len,
572 rec->u.octet_aligned->buf);
574 if (oident && oident->value != VAL_TEXT_XML)
577 rr->errstring = "only XML update supported";
580 if (rec->which == Z_External_octet)
584 if (*toKeep->action ==
585 Z_IUOriginPartToKeep_recordInsert)
587 if (*toKeep->action ==
588 Z_IUOriginPartToKeep_recordReplace)
590 if (*toKeep->action ==
591 Z_IUOriginPartToKeep_recordDelete)
593 if (*toKeep->action ==
594 Z_IUOriginPartToKeep_specialUpdate)
600 rr->errstring = "unsupported ES Update action";
605 int r = zebra_admin_exchange_record (
606 zh, toKeep->databaseName,
607 rec->u.octet_aligned->buf,
608 rec->u.octet_aligned->len,
609 recid->buf, recid->len,
611 if (r && *toKeep->action ==
612 Z_IUOriginPartToKeep_specialUpdate)
614 r = zebra_admin_exchange_record (
615 zh, toKeep->databaseName,
616 rec->u.octet_aligned->buf,
617 rec->u.octet_aligned->len,
618 recid->buf, recid->len,
624 rr->errstring = "record exchange failed";
630 zebra_end_trans (zh);
636 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
637 rr->esr->taskSpecificParameters->which);
644 static void bend_start (struct statserv_options_block *sob)
647 zebra_stop((ZebraService) sob->handle);
648 sob->handle = zebra_start(sob->configname);
651 yaz_log (LOG_FATAL, "Failed to read config `%s'", sob->configname);
660 char *pidfile = "zebrasrv.pid";
661 int fd = open (pidfile, O_EXCL|O_WRONLY|O_CREAT, 0666);
666 yaz_log(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfile);
669 fd = open(pidfile, O_RDWR, 0666);
672 yaz_log(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfile);
676 area.l_type = F_WRLCK;
677 area.l_whence = SEEK_SET;
678 area.l_len = area.l_start = 0L;
679 if (fcntl (fd, F_SETLK, &area) == -1)
681 yaz_log(LOG_ERRNO|LOG_FATAL, "Zebra server already running");
688 sprintf (pidstr, "%ld", (long) getpid ());
689 write (fd, pidstr, strlen(pidstr));
695 static void bend_stop(struct statserv_options_block *sob)
701 unlink ("zebrasrv.pid");
705 ZebraService service = sob->handle;
710 int main (int argc, char **argv)
712 struct statserv_options_block *sob;
714 sob = statserv_getcontrol ();
715 strcpy (sob->configname, "zebra.cfg");
716 sob->bend_start = bend_start;
717 sob->bend_stop = bend_stop;
719 strcpy (sob->service_display_name, "Zebra Server");
721 statserv_setcontrol (sob);
723 return statserv_main (argc, argv, bend_init, bend_close);