+ else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
+ {
+ es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
+
+ zebra_result (zh, &rr->errcode, &rr->errstring);
+ }
+ else if (rr->esr->taskSpecificParameters->which == Z_External_update)
+ {
+ Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
+ yaz_log (YLOG_LOG, "Received DB Update");
+ if (up->which == Z_IUUpdate_esRequest)
+ {
+ Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
+ Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
+ Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
+
+ yaz_log (YLOG_LOG, "action");
+ if (toKeep->action)
+ {
+ switch (*toKeep->action)
+ {
+ case Z_IUOriginPartToKeep_recordInsert:
+ yaz_log (YLOG_LOG, "recordInsert");
+ break;
+ case Z_IUOriginPartToKeep_recordReplace:
+ yaz_log (YLOG_LOG, "recordUpdate");
+ break;
+ case Z_IUOriginPartToKeep_recordDelete:
+ yaz_log (YLOG_LOG, "recordDelete");
+ break;
+ case Z_IUOriginPartToKeep_elementUpdate:
+ yaz_log (YLOG_LOG, "elementUpdate");
+ break;
+ case Z_IUOriginPartToKeep_specialUpdate:
+ yaz_log (YLOG_LOG, "specialUpdate");
+ break;
+ case Z_ESAdminOriginPartToKeep_shutdown:
+ yaz_log (YLOG_LOG, "shutDown");
+ break;
+ case Z_ESAdminOriginPartToKeep_start:
+ yaz_log (YLOG_LOG, "start");
+ break;
+ default:
+ yaz_log (YLOG_LOG, " unknown (%d)", *toKeep->action);
+ }
+ }
+ if (toKeep->databaseName)
+ {
+ yaz_log (YLOG_LOG, "database: %s", toKeep->databaseName);
+
+ if (zebra_select_database(zh, toKeep->databaseName))
+ return 0;
+ }
+ else
+ {
+ yaz_log (YLOG_WARN, "no database supplied for ES Update");
+ rr->errcode = 1008;
+ rr->errstring = "database";
+ return 0;
+ }
+ if (notToKeep)
+ {
+ int i;
+ zebra_begin_trans (zh, 1);
+ for (i = 0; i < notToKeep->num; i++)
+ {
+ Z_External *rec = notToKeep->elements[i]->record;
+ struct oident *oident = 0;
+ Odr_oct *opaque_recid = 0;
+ SYSNO sysno = 0;
+
+ if (notToKeep->elements[i]->u.opaque)
+ {
+ switch(notToKeep->elements[i]->which)
+ {
+ case Z_IUSuppliedRecords_elem_opaque:
+ opaque_recid = notToKeep->elements[i]->u.opaque;
+ break; /* OK, recid already set */
+ case Z_IUSuppliedRecords_elem_number:
+ sysno = *notToKeep->elements[i]->u.number;
+ break;
+ }
+ }
+ if (rec->direct_reference)
+ {
+ oident = oid_getentbyoid(rec->direct_reference);
+ if (oident)
+ yaz_log (YLOG_LOG, "record %d type %s", i,
+ oident->desc);
+ }
+ switch (rec->which)
+ {
+ case Z_External_sutrs:
+ if (rec->u.octet_aligned->len > 170)
+ yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
+ rec->u.sutrs->len,
+ rec->u.sutrs->buf);
+ else
+ yaz_log (YLOG_LOG, "%d bytes:\n%s",
+ rec->u.sutrs->len,
+ rec->u.sutrs->buf);
+ break;
+ case Z_External_octet:
+ if (rec->u.octet_aligned->len > 170)
+ yaz_log (YLOG_LOG, "%d bytes:\n%.168s ...",
+ rec->u.octet_aligned->len,
+ rec->u.octet_aligned->buf);
+ else
+ yaz_log (YLOG_LOG, "%d bytes\n%s",
+ rec->u.octet_aligned->len,
+ rec->u.octet_aligned->buf);
+ }
+ if (oident && oident->value != VAL_TEXT_XML)
+ {
+ rr->errcode = 224;
+ rr->errstring = "only XML update supported";
+ break;
+ }
+ if (rec->which == Z_External_octet)
+ {
+ int action = 0;
+
+ if (*toKeep->action ==
+ Z_IUOriginPartToKeep_recordInsert)
+ action = 1;
+ if (*toKeep->action ==
+ Z_IUOriginPartToKeep_recordReplace)
+ action = 2;
+ if (*toKeep->action ==
+ Z_IUOriginPartToKeep_recordDelete)
+ action = 3;
+ if (*toKeep->action ==
+ Z_IUOriginPartToKeep_specialUpdate)
+ action = 4;
+
+ if (!action)
+ {
+ rr->errcode = 224;
+ rr->errstring = "unsupported ES Update action";
+ break;
+ }
+ else if (opaque_recid)
+ {
+ int r = zebra_admin_exchange_record (
+ zh,
+ rec->u.octet_aligned->buf,
+ rec->u.octet_aligned->len,
+ opaque_recid->buf, opaque_recid->len,
+ action);
+ if (r)
+ {
+ rr->errcode = 224;
+ rr->errstring = "record exchange failed";
+ break;
+ }
+ }
+ else
+ {
+ int r = -1;
+ switch(action) {
+ case 1:
+ r = zebra_insert_record(
+ zh,
+ 0, /* recordType */
+ &sysno,
+ 0, /* match */
+ 0, /* fname */
+ rec->u.octet_aligned->buf,
+ rec->u.octet_aligned->len,
+ 0);
+ if (r)
+ {
+ rr->errcode = 224;
+ rr->errstring = "insert_record failed";
+ }
+ break;
+ case 2:
+ case 4:
+ r = zebra_update_record(
+ zh,
+ 0, /* recordType */
+ &sysno,
+ 0, /* match */
+ 0, /* fname */
+ rec->u.octet_aligned->buf,
+ rec->u.octet_aligned->len,
+ 1);
+ if (r)
+ {
+ rr->errcode = 224;
+ rr->errstring = "update_record failed";
+ }
+ break;
+ case 3:
+ r = zebra_delete_record(
+ zh,
+ 0, /* recordType */
+ &sysno,
+ 0, /* match */
+ 0, /* fname */
+ rec->u.octet_aligned->buf,
+ rec->u.octet_aligned->len,
+ 0);
+ if (r)
+ {
+ rr->errcode = 224;
+ rr->errstring = "delete_record failed";
+ }
+ break;
+ }
+ }
+ }
+ }
+ zebra_end_trans (zh);
+ }
+ }
+ }
+ else
+ {
+ yaz_log (YLOG_WARN, "Unknown Extended Service(%d)",
+ rr->esr->taskSpecificParameters->which);
+ rr->errcode = 221;
+
+ }
+ return 0;
+}
+
+static void bend_start (struct statserv_options_block *sob)
+{
+ if (sob->handle)
+ zebra_stop((ZebraService) sob->handle);
+ sob->handle = zebra_start(sob->configname);
+ if (!sob->handle)
+ {
+ yaz_log (YLOG_FATAL, "Failed to read config `%s'", sob->configname);
+ exit (1);
+ }
+#ifdef WIN32
+
+#else
+ if (!sob->inetd)
+ {
+ char pidfname[4096];
+ struct flock area;
+ int fd;
+
+ zebra_pidfname(sob->handle, pidfname);
+
+ fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
+ if (fd == -1)
+ {
+ if (errno != EEXIST)
+ {
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
+ exit(1);
+ }
+ fd = open(pidfname, O_RDWR, 0666);
+ if (fd == -1)
+ {
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
+ exit(1);
+ }
+ }
+ area.l_type = F_WRLCK;
+ area.l_whence = SEEK_SET;
+ area.l_len = area.l_start = 0L;
+ if (fcntl (fd, F_SETLK, &area) == -1)
+ {
+ yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
+ exit(1);
+ }
+ else
+ {
+ char pidstr[30];
+
+ sprintf (pidstr, "%ld", (long) getpid ());
+ write (fd, pidstr, strlen(pidstr));
+ }
+ }
+#endif
+}
+
+static void bend_stop(struct statserv_options_block *sob)
+{
+#ifdef WIN32
+
+#else
+ if (!sob->inetd && sob->handle)
+ {
+ char pidfname[4096];
+ zebra_pidfname(sob->handle, pidfname);
+ unlink (pidfname);
+ }
+#endif
+ if (sob->handle)
+ {
+ ZebraService service = sob->handle;
+ zebra_stop(service);
+ }
+}
+
+int main (int argc, char **argv)
+{
+ struct statserv_options_block *sob;
+
+ sob = statserv_getcontrol ();
+ strcpy (sob->configname, "zebra.cfg");
+ sob->bend_start = bend_start;
+ sob->bend_stop = bend_stop;
+#ifdef WIN32
+ strcpy (sob->service_display_name, "Zebra Server");
+#endif
+ statserv_setcontrol (sob);
+
+ return statserv_main (argc, argv, bend_init, bend_close);