* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*
- * $Id: backend.h,v 1.28 2005-01-15 19:47:09 adam Exp $
+ * $Id: backend.h,v 1.29 2005-02-01 14:46:47 adam Exp $
*/
/**
char pid_fname[128]; /* pid fname */
int background; /* auto daemon */
char cert_fname[128]; /* SSL certificate fname */
+ char xml_config[128]; /* XML config filename */
} statserv_options_block;
YAZ_EXPORT int statserv_main(
* Copyright (C) 1995-2005, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: eventl.c,v 1.6 2005-01-17 12:53:04 adam Exp $
+ * $Id: eventl.c,v 1.7 2005-02-01 14:46:47 adam Exp $
*/
/**
static int log_level=0;
static int log_level_initialized=0;
-IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
+IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int port)
{
IOCHAN new_iochan;
new_iochan->force_event = 0;
new_iochan->last_event = new_iochan->max_idle = 0;
new_iochan->next = NULL;
+ new_iochan->port = port;
return new_iochan;
}
* Copyright (C) 1995-2005, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: eventl.h,v 1.3 2005-01-15 19:47:13 adam Exp $
+ * $Id: eventl.h,v 1.4 2005-02-01 14:46:47 adam Exp $
*/
/**
time_t max_idle;
struct iochan *next;
+ int port; /* listening port (0 if none ) */
} *IOCHAN;
#define iochan_destroy(i) (void)((i)->destroyed = 1)
#define iochan_getnext(i) ((i)->next)
#define iochan_settimeout(i, t) ((i)->max_idle = (t), (i)->last_event = time(0))
-IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags);
+IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int port);
int event_loop(IOCHAN *iochans);
void statserv_remove (IOCHAN pIOChannel);
#endif
* Copyright (C) 1995-2005, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: seshigh.c,v 1.46 2005-01-19 09:18:08 adam Exp $
+ * $Id: seshigh.c,v 1.47 2005-02-01 14:46:47 adam Exp $
*/
/**
* \file seshigh.c
int *fd);
static Z_APDU *process_segmentRequest (association *assoc, request *reqb);
-static FILE *apduf = 0; /* for use in static mode */
-static statserv_options_block *control_block = 0;
-
static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd);
/* dynamic logging levels */
{ /* needs to be called after parsing cmd-line args that can set loglevels!*/
if (!logbits_set)
{
- logbits_set=1;
- log_session=yaz_log_module_level("session");
- log_request=yaz_log_module_level("request");
- log_requestdetail=yaz_log_module_level("requestdetail");
+ logbits_set = 1;
+ log_session = yaz_log_module_level("session");
+ log_request = yaz_log_module_level("request");
+ log_requestdetail = yaz_log_module_level("requestdetail");
}
}
* link : communications channel.
* Returns: 0 or a new association handle.
*/
-association *create_association(IOCHAN channel, COMSTACK link)
+association *create_association(IOCHAN channel, COMSTACK link,
+ const char *apdufile)
{
association *anew;
if (!logbits_set)
get_logbits();
- if (!control_block)
- control_block = statserv_getcontrol();
if (!(anew = (association *)xmalloc(sizeof(*anew))))
return 0;
anew->init = 0;
anew->version = 0;
+ anew->last_control = 0;
anew->client_chan = channel;
anew->client_link = link;
anew->cs_get_mask = 0;
if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
!(anew->encode = odr_createmem(ODR_ENCODE)))
return 0;
- if (*control_block->apdufile)
+ if (apdufile && *apdufile)
{
- char filename[256];
FILE *f;
- strcpy(filename, control_block->apdufile);
if (!(anew->print = odr_createmem(ODR_PRINT)))
return 0;
- if (*control_block->apdufile == '@')
+ if (*apdufile == '@')
{
odr_setprint(anew->print, yaz_log_file());
}
- else if (*control_block->apdufile != '-')
+ else if (*apdufile != '-')
{
- strcpy(filename, control_block->apdufile);
- if (!control_block->dynamic)
- {
- if (!apduf)
- {
- if (!(apduf = fopen(filename, "w")))
- {
- yaz_log(YLOG_WARN|YLOG_ERRNO, "can't open apdu dump %s", filename);
- return 0;
- }
- setvbuf(apduf, 0, _IONBF, 0);
- }
- f = apduf;
- }
- else
- {
- sprintf(filename + strlen(filename), ".%ld", (long)getpid());
- if (!(f = fopen(filename, "w")))
- {
- yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", filename);
- return 0;
- }
- setvbuf(f, 0, _IONBF, 0);
- }
+ char filename[256];
+ sprintf(filename, "%.200s.%ld", apdufile, (long)getpid());
+ if (!(f = fopen(filename, "w")))
+ {
+ yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", filename);
+ return 0;
+ }
+ setvbuf(f, 0, _IONBF, 0);
odr_setprint(anew->print, f);
}
}
request_delq(&h->outgoing);
xfree(h);
xmalloc_trav("session closed");
- if (control_block && control_block->one_shot)
+ if (cb && cb->one_shot)
{
exit (0);
}
static int process_z_request(association *assoc, request *req, char **msg);
+
static void assoc_init_reset(association *assoc)
{
xfree (assoc->init);
assoc->init->decode = assoc->decode;
assoc->init->peer_name =
odr_strdup (assoc->encode, cs_addrstr(assoc->client_link));
+
+ yaz_log(log_requestdetail, "peer %s", assoc->init->peer_name);
+
+
}
static int srw_bend_init(association *assoc, Z_SRW_diagnostic **d, int *num)
{
- const char *encoding = "UTF-8";
- Z_External *ce;
- bend_initresult *binitres;
statserv_options_block *cb = statserv_getcontrol();
-
- assoc_init_reset(assoc);
+ if (!assoc->init)
+ {
+ const char *encoding = "UTF-8";
+ Z_External *ce;
+ bend_initresult *binitres;
- assoc->maximumRecordSize = 3000000;
- assoc->preferredMessageSize = 3000000;
+ yaz_log(YLOG_LOG, "srw_bend_init config=%s", cb->configname);
+ assoc_init_reset(assoc);
+
+ assoc->maximumRecordSize = 3000000;
+ assoc->preferredMessageSize = 3000000;
#if 1
- ce = yaz_set_proposal_charneg(assoc->decode, &encoding, 1, 0, 0, 1);
- assoc->init->charneg_request = ce->u.charNeg3;
+ ce = yaz_set_proposal_charneg(assoc->decode, &encoding, 1, 0, 0, 1);
+ assoc->init->charneg_request = ce->u.charNeg3;
#endif
- assoc->backend = 0;
- if (!(binitres = (*cb->bend_init)(assoc->init)))
- {
- assoc->state = ASSOC_DEAD;
- yaz_add_srw_diagnostic(assoc->encode, d, num, 3, 0);
- return 0;
- }
- assoc->backend = binitres->handle;
- if (binitres->errcode)
- {
- assoc->state = ASSOC_DEAD;
- yaz_add_srw_diagnostic(assoc->encode, d, num, binitres->errcode,
- binitres->errstring);
- return 0;
+ assoc->backend = 0;
+ if (!(binitres = (*cb->bend_init)(assoc->init)))
+ {
+ assoc->state = ASSOC_DEAD;
+ yaz_add_srw_diagnostic(assoc->encode, d, num, 3, 0);
+ return 0;
+ }
+ assoc->backend = binitres->handle;
+ if (binitres->errcode)
+ {
+ assoc->state = ASSOC_DEAD;
+ yaz_add_srw_diagnostic(assoc->encode, d, num, binitres->errcode,
+ binitres->errstring);
+ return 0;
+ }
+ return 1;
}
return 1;
}
*http_code = 200;
yaz_log(log_requestdetail, "Got SRW SearchRetrieveRequest");
- if (!assoc->init)
- srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics);
+ srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics);
if (srw_req->sort_type != Z_SRW_sort_type_none)
yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
&srw_res->num_diagnostics, 80, 0);
{
yaz_log(log_requestdetail, "Got SRW ExplainRequest");
*http_code = 404;
- if (!assoc->init)
- srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics);
+ srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics);
if (assoc->init && assoc->init->bend_explain)
{
bend_explain_rr rr;
yaz_log(log_requestdetail, "Got SRW ScanRequest");
*http_code = 200;
- if (!assoc->init)
- srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics);
+ srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics);
if (srw_res->num_diagnostics == 0 && assoc->init)
{
struct scan_entry *save_entries;
char *stylesheet = 0;
Z_SRW_diagnostic *diagnostic = 0;
int num_diagnostic = 0;
+ const char *host = z_HTTP_header_lookup(hreq->headers, "Host");
- if (!strcmp(hreq->path, "/test"))
+ if (!control_association(assoc, host, 0))
+ {
+ p = z_get_HTTP_Response(o, 404);
+ r = 1;
+ }
+ if (r == 2 && !strcmp(hreq->path, "/test"))
{
p = z_get_HTTP_Response(o, 200);
hres = p->u.HTTP_Response;
switch (req->apdu_request->which)
{
case Z_APDU_initRequest:
- iochan_settimeout(assoc->client_chan,
- statserv_getcontrol()->idle_timeout * 60);
res = process_initRequest(assoc, req); break;
case Z_APDU_searchRequest:
res = process_searchRequest(assoc, req, &fd); break;
yaz_log(YLOG_DEBUG, " establishing handler for result");
req->state = REQUEST_PENDING;
- if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
+ if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT, 0)))
abort();
iochan_setdata(chan, assoc);
retval = 0;
return process_gdu_response(assoc, req, gres);
}
+static char *get_vhost(Z_OtherInformation *otherInfo)
+{
+ return yaz_oi_get_string_oidval(&otherInfo, VAL_PROXY, 1, 0);
+}
/*
* Handle init request.
*/
static Z_APDU *process_initRequest(association *assoc, request *reqb)
{
- statserv_options_block *cb = statserv_getcontrol();
Z_InitRequest *req = reqb->apdu_request->u.initRequest;
Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
Z_InitResponse *resp = apdu->u.initResponse;
bend_initresult *binitres;
char *version;
char options[140];
+ statserv_options_block *cb = 0; /* by default no control for backend */
+ if (control_association(assoc, get_vhost(req->otherInfo), 1))
+ cb = statserv_getcontrol(); /* got control block for backend */
+
yaz_log(log_requestdetail, "Got initRequest");
if (req->implementationId)
- yaz_log(log_requestdetail, "Id: %s", req->implementationId);
+ yaz_log(log_requestdetail, "Id: %s",
+ req->implementationId);
if (req->implementationName)
- yaz_log(log_requestdetail, "Name: %s", req->implementationName);
+ yaz_log(log_requestdetail, "Name: %s",
+ req->implementationName);
if (req->implementationVersion)
- yaz_log(log_requestdetail, "Version: %s", req->implementationVersion);
-
+ yaz_log(log_requestdetail, "Version: %s",
+ req->implementationVersion);
+
assoc_init_reset(assoc);
assoc->init->auth = req->idAuthentication;
}
assoc->backend = 0;
- if (!(binitres = (*cb->bend_init)(assoc->init)))
+ if (cb)
{
- yaz_log(YLOG_WARN, "Bad response from backend.");
- return 0;
+ if (req->implementationVersion)
+ yaz_log(log_requestdetail, "Config: %s",
+ cb->configname);
+
+ iochan_settimeout(assoc->client_chan, cb->idle_timeout * 60);
+
+ /* we have a backend control block, so call that init function */
+ if (!(binitres = (*cb->bend_init)(assoc->init)))
+ {
+ yaz_log(YLOG_WARN, "Bad response from backend.");
+ return 0;
+ }
+ assoc->backend = binitres->handle;
+ }
+ else
+ {
+ /* no backend. return error */
+ binitres = odr_malloc(assoc->encode, sizeof(*binitres));
+ binitres->errstring = 0;
+ binitres->errcode = 1;
+ iochan_settimeout(assoc->client_chan, 10);
}
-
- assoc->backend = binitres->handle;
if ((assoc->init->bend_sort))
- yaz_log (YLOG_DEBUG, "Sort handler installed");
+ yaz_log (YLOG_DEBUG, "Sort handler installed");
if ((assoc->init->bend_search))
- yaz_log (YLOG_DEBUG, "Search handler installed");
+ yaz_log (YLOG_DEBUG, "Search handler installed");
if ((assoc->init->bend_present))
- yaz_log (YLOG_DEBUG, "Present handler installed");
+ yaz_log (YLOG_DEBUG, "Present handler installed");
if ((assoc->init->bend_esrequest))
- yaz_log (YLOG_DEBUG, "ESRequest handler installed");
+ yaz_log (YLOG_DEBUG, "ESRequest handler installed");
if ((assoc->init->bend_delete))
- yaz_log (YLOG_DEBUG, "Delete handler installed");
+ yaz_log (YLOG_DEBUG, "Delete handler installed");
if ((assoc->init->bend_scan))
- yaz_log (YLOG_DEBUG, "Scan handler installed");
+ yaz_log (YLOG_DEBUG, "Scan handler installed");
if ((assoc->init->bend_segment))
- yaz_log (YLOG_DEBUG, "Segment handler installed");
+ yaz_log (YLOG_DEBUG, "Segment handler installed");
resp->referenceId = req->referenceId;
*options = '\0';
yaz_log(log_requestdetail, "Negotiated to v%d: %s", assoc->version, options);
assoc->maximumRecordSize = *req->maximumRecordSize;
- if (assoc->maximumRecordSize > control_block->maxrecordsize)
- assoc->maximumRecordSize = control_block->maxrecordsize;
+
+ if (cb && assoc->maximumRecordSize > cb->maxrecordsize)
+ assoc->maximumRecordSize = cb->maxrecordsize;
assoc->preferredMessageSize = *req->preferredMessageSize;
if (assoc->preferredMessageSize > assoc->maximumRecordSize)
assoc->preferredMessageSize = assoc->maximumRecordSize;
assoc->init->implementation_name,
odr_prepend(assoc->encode, "GFS", resp->implementationName));
- version = odr_strdup(assoc->encode, "$Revision: 1.46 $");
+ version = odr_strdup(assoc->encode, "$Revision: 1.47 $");
if (strlen(version) > 10) /* check for unexpanded CVS strings */
version[strlen(version)-2] = '\0';
resp->implementationVersion = odr_prepend(assoc->encode,
resp->diagnostics = diagRecs->diagRecs;
if (log_request)
{
- WRBUF wr=wrbuf_alloc();
+ WRBUF wr = wrbuf_alloc();
wrbuf_diags(wr, resp->num_diagnostics, resp->diagnostics);
yaz_log(log_request, "EsRequest %s", wrbuf_buf(wr) );
wrbuf_free(wr, 1);
* Copyright (C) 1995-2005, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: session.h,v 1.4 2005-01-16 21:51:50 adam Exp $
+ * $Id: session.h,v 1.5 2005-02-01 14:46:47 adam Exp $
*/
/**
* \file session.h
#include <yaz/odr.h>
#include <yaz/oid.h>
#include <yaz/proto.h>
+#include <yaz/backend.h>
#include "eventl.h"
+struct gfs_server {
+ statserv_options_block cb;
+ char *host;
+ int port;
+ struct gfs_server *next;
+};
+
typedef enum {
- REQUEST_IDLE, /* the request is just sitting in the queue */
- REQUEST_PENDING /* operation pending (b'end processing or network I/O*/
- /* this list will have more elements when acc/res control is added */
+ REQUEST_IDLE, /* the request is just sitting in the queue */
+ REQUEST_PENDING /* operation pending (b'end processing or network I/O*/
+ /* this list will have more elements when acc/res control is added */
} request_state;
typedef struct request
unsigned cs_accept_mask;
struct bend_initrequest *init;
+ statserv_options_block *last_control;
} association;
-association *create_association(IOCHAN channel, COMSTACK link);
+association *create_association(IOCHAN channel, COMSTACK link,
+ const char *apdufile);
void destroy_association(association *h);
void ir_session(IOCHAN h, int event);
int statserv_must_terminate(void);
+int control_association(association *assoc, const char *host, int force);
+
#endif
* NT threaded server code by
* Chas Woodfield, Fretwell Downing Informatics.
*
- * $Id: statserv.c,v 1.19 2005-01-16 21:51:50 adam Exp $
+ * $Id: statserv.c,v 1.20 2005-02-01 14:46:47 adam Exp $
*/
/**
#include <stdio.h>
#include <string.h>
+#include <ctype.h>
#ifdef WIN32
#include <process.h>
#include <winsock.h>
#include <pwd.h>
#endif
+#if HAVE_XML2
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#endif
+
#if YAZ_POSIX_THREADS
#include <pthread.h>
#elif YAZ_GNU_THREADS
static IOCHAN pListener = NULL;
+static struct gfs_server *gfs_server_list = 0;
+static NMEM gfs_nmem = 0;
+
static char *me = "statserver"; /* log prefix */
static char *programname="statserver"; /* full program name */
+#if YAZ_POSIX_THREADS
+static pthread_key_t current_control_tls;
+static int init_control_tls = 0;
+#else
+static statserv_options_block *current_control_block = 0;
+#endif
+
/*
* default behavior.
*/
0, /* SOAP handlers */
"", /* PID fname */
0, /* background daemon */
- "" /* SSL certificate filename */
+ "", /* SSL certificate filename */
+ "", /* XML config filename */
};
static int max_sessions = 0;
}
+static int add_listener(char *where, int listen_id);
+
+#if HAVE_XML2
+static xmlDocPtr xml_config_doc = 0;
+#endif
+
+#if HAVE_XML2
+static xmlNodePtr xml_config_get_root()
+{
+ xmlNodePtr ptr = 0;
+ if (xml_config_doc)
+ {
+ ptr = xmlDocGetRootElement(xml_config_doc);
+ if (!ptr || ptr->type != XML_ELEMENT_NODE ||
+ strcmp((const char *) ptr->name, "yazgfs"))
+ {
+ yaz_log(YLOG_WARN, "Bad/missing root element for config %s",
+ control_block.xml_config);
+ return 0;
+
+ }
+ }
+ return ptr;
+}
+#endif
+
+#if HAVE_XML2
+static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
+{
+ unsigned char *cp;
+ xmlNodePtr p;
+ int len = 1; /* start with 1, because of trailing 0 */
+ char *str;
+ int first = 1; /* whitespace lead flag .. */
+ /* determine length */
+ for (p = ptr; p; p = p->next)
+ {
+ if (p->type == XML_TEXT_NODE)
+ len += strlen(p->content);
+ }
+ /* now allocate for the string */
+ str = nmem_malloc(n, len);
+ *str = '\0'; /* so we can use strcat */
+ for (p = ptr; p; p = p->next)
+ {
+ if (p->type == XML_TEXT_NODE)
+ {
+ cp = p->content;
+ if (first)
+ {
+ while(*cp && isspace(*cp))
+ cp++;
+ if (*cp)
+ first = 0; /* reset if we got non-whitespace out */
+ }
+ strcat(str, cp); /* append */
+ }
+ }
+ /* remove trailing whitespace */
+ cp = strlen(str) + str;
+ while ((char*) cp != str && isspace(cp[-1]))
+ cp--;
+ *cp = '\0';
+ /* return resulting string */
+ return str;
+}
+#endif
+
+static struct gfs_server * gfs_server_new()
+{
+ struct gfs_server *n = nmem_malloc(gfs_nmem, sizeof(*n));
+ memcpy(&n->cb, &control_block, sizeof(control_block));
+ n->next = 0;
+ n->host = 0;
+ n->port = 0;
+ return n;
+}
+
+int control_association(association *assoc, const char *host, int force_open)
+{
+ char vhost[128], *cp;
+ if (host)
+ {
+ strncpy(vhost, host, 127);
+ vhost[127] = '\0';
+ cp = strchr(vhost, ':');
+ if (cp)
+ *cp = '\0';
+ host = vhost;
+ }
+ if (control_block.xml_config[0])
+ {
+ struct gfs_server *gfs;
+ for (gfs = gfs_server_list; gfs; gfs = gfs->next)
+ {
+ int port_match = 0;
+ int host_match = 0;
+ if ( !gfs->host || (host && gfs->host && !strcmp(host, gfs->host)))
+ host_match = 1;
+ if (assoc->client_chan->port == gfs->port)
+ port_match= 1;
+ if (port_match && host_match)
+ {
+ if (force_open ||
+ (assoc->last_control != &gfs->cb && assoc->backend))
+ {
+ statserv_setcontrol(assoc->last_control);
+ if (assoc->backend && assoc->init)
+ (assoc->last_control->bend_close)(assoc->backend);
+ assoc->backend = 0;
+ xfree(assoc->init);
+ assoc->init = 0;
+ }
+ assoc->last_control = &gfs->cb;
+ statserv_setcontrol(&gfs->cb);
+ return 1;
+ }
+ }
+ statserv_setcontrol(0);
+ assoc->last_control = 0;
+ return 0;
+ }
+ else
+ {
+ statserv_setcontrol(&control_block);
+ assoc->last_control = &control_block;
+ return 1;
+ }
+}
+
+static void xml_config_read()
+{
+ struct gfs_server **gfsp = &gfs_server_list;
+#if HAVE_XML2
+ xmlNodePtr ptr = xml_config_get_root();
+
+ if (!ptr)
+ return;
+ for (ptr = ptr->children; ptr; ptr = ptr->next)
+ {
+ if (ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "server"))
+ {
+ xmlNodePtr ptr_children = ptr->children;
+ xmlNodePtr ptr;
+
+ *gfsp = gfs_server_new();
+ for (ptr = ptr_children; ptr; ptr = ptr->next)
+ {
+ if (ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "host"))
+ {
+ (*gfsp)->host = nmem_dup_xml_content(gfs_nmem,
+ ptr->children);
+ }
+ if (ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "port"))
+ {
+ (*gfsp)->port = atoi(nmem_dup_xml_content(gfs_nmem,
+ ptr->children));
+ }
+ if (ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "config"))
+ {
+ strcpy((*gfsp)->cb.configname,
+ nmem_dup_xml_content(gfs_nmem, ptr->children));
+ }
+ }
+ gfsp = &(*gfsp)->next;
+ }
+ }
+#endif
+ *gfsp = 0;
+}
+
+static void xml_config_open()
+{
+ gfs_nmem = nmem_create();
+#if HAVE_XML2
+ if (control_block.xml_config[0] == '\0')
+ return;
+
+ if (!xml_config_doc)
+ {
+ xml_config_doc = xmlParseFile(control_block.xml_config);
+ if (!xml_config_doc)
+ {
+ yaz_log(YLOG_WARN, "Could not parse %s", control_block.xml_config);
+ return ;
+ }
+ }
+ xml_config_read();
+
+#endif
+}
+
+static void xml_config_close()
+{
+#if HAVE_XML2
+ if (xml_config_doc)
+ {
+ xmlFreeDoc(xml_config_doc);
+ xml_config_doc = 0;
+ }
+#endif
+ gfs_server_list = 0;
+ nmem_destroy(gfs_nmem);
+}
+
+static void xml_config_add_listeners()
+{
+#define MAX_PORTS 200
+ struct gfs_server *gfs = gfs_server_list;
+ int i, ports[MAX_PORTS];
+ for (i = 0; i<MAX_PORTS; i++)
+ ports[i] = 0;
+
+ for (; gfs; gfs = gfs->next)
+ {
+ int port = gfs->port;
+ if (port)
+ {
+ for (i = 0; i<MAX_PORTS && ports[i] != port; i++)
+ if (ports[i] == 0)
+ {
+ ports[i] = port;
+ break;
+ }
+ }
+ }
+ for (i = 0; i<MAX_PORTS && ports[i]; i++)
+ {
+ char where[80];
+ sprintf(where, "@:%d", ports[i]);
+ add_listener(where, ports[i]);
+ }
+}
+
+static void xml_config_bend_start()
+{
+ if (control_block.xml_config[0])
+ {
+ struct gfs_server *gfs = gfs_server_list;
+ for (; gfs; gfs = gfs->next)
+ {
+ yaz_log(YLOG_DEBUG, "xml_config_bend_start config=%s",
+ gfs->cb.configname);
+ statserv_setcontrol(&gfs->cb);
+ if (control_block.bend_start)
+ (control_block.bend_start)(&gfs->cb);
+ }
+ }
+ else
+ {
+ yaz_log(YLOG_DEBUG, "xml_config_bend_start default config");
+ statserv_setcontrol(&control_block);
+ if (control_block.bend_start)
+ (*control_block.bend_start)(&control_block);
+ }
+
+}
+
+static void xml_config_bend_stop()
+{
+ if (control_block.xml_config[0])
+ {
+ struct gfs_server *gfs = gfs_server_list;
+ for (; gfs; gfs = gfs->next)
+ {
+ yaz_log(YLOG_DEBUG, "xml_config_bend_stop config=%s",
+ gfs->cb.configname);
+ statserv_setcontrol(&gfs->cb);
+ if (control_block.bend_stop)
+ (control_block.bend_stop)(&gfs->cb);
+ }
+ }
+ else
+ {
+ yaz_log(YLOG_DEBUG, "xml_config_bend_stop default config");
+ statserv_setcontrol(&control_block);
+ if (control_block.bend_stop)
+ (*control_block.bend_stop)(&control_block);
+ }
+}
+
/*
* handle incoming connect requests.
* The dynamic mode is a bit tricky mostly because we want to avoid
ThreadList *pNextThread;
ThreadList *pPrevThread =NULL;
- /* Step through alll the threads */
+ /* Step through all the threads */
for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
{
/* We only need to compare on the IO Channel */
/* Now we can really do something */
if (iHandles > 0)
{
- logf (log_server, "waiting for %d to die", iHandles);
+ yaz_log(log_server, "waiting for %d to die", iHandles);
/* This will now wait, until all the threads close */
WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
free(pThreadHandles);
}
- if (control_block.bend_stop)
- (*control_block.bend_stop)(&control_block);
+ xml_config_bend_stop();
/* No longer require the critical section, since all threads are dead */
DeleteCriticalSection(&Thread_CritSect);
}
+ xml_config_close();
}
void __cdecl event_loop_thread (IOCHAN iochan)
}
yaz_log(YLOG_DEBUG, "Creating association");
- if (!(newas = create_association(new_chan, new_line)))
+ if (!(newas = create_association(new_chan, new_line,
+ control_block.apdu_file)))
{
yaz_log(YLOG_FATAL, "Failed to create new assoc.");
iochan_destroy(h);
{
IOCHAN p;
- if (control_block.bend_stop)
- (*control_block.bend_stop)(&control_block);
+ xml_config_bend_stop();
for (p = pListener; p; p = p->next)
{
iochan_destroy(p);
}
+ xml_config_close();
+#if YAZ_POSIX_THREADS
+ pthread_key_delete(current_control_tls);
+#endif
}
void sigterm(int sig)
iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
return;
}
+
+ yaz_log(log_session, "Connect from %s", cs_addrstr(new_line));
+
no_sessions++;
if (control_block.dynamic)
{
return;
}
}
+
if (control_block.threads)
{
#if YAZ_POSIX_THREADS
association *newas;
IOCHAN new_chan;
COMSTACK new_line = (COMSTACK) vp;
+ IOCHAN parent_chan = new_line->user;
unsigned cs_get_mask, cs_accept_mask, mask =
((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
- if (mask)
+ if (mask)
{
cs_accept_mask = mask; /* accept didn't complete */
cs_get_mask = 0;
cs_get_mask = mask = EVENT_INPUT;
}
- if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask)))
+ if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask,
+ parent_chan->port)))
{
yaz_log(YLOG_FATAL, "Failed to create iochan");
return 0;
}
- if (!(newas = create_association(new_chan, new_line)))
+ if (!(newas = create_association(new_chan, new_line,
+ control_block.apdufile)))
{
yaz_log(YLOG_FATAL, "Failed to create new assoc.");
return 0;
if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
{
- if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
+ if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT,
+ 0)))
{
- if ((assoc = create_association(chan, line)))
+ if ((assoc = create_association(chan, line,
+ control_block.apdufile)))
{
iochan_setdata(chan, assoc);
iochan_settimeout(chan, 60);
/*
* Set up a listening endpoint, and give it to the event-handler.
*/
-static int add_listener(char *where, int what)
+static int add_listener(char *where, int listen_id)
{
COMSTACK l;
void *ap;
else
mode = "static";
- yaz_log(log_server, "Adding %s %s listener on %s", mode,
- what == PROTO_SR ? "SR" : "Z3950", where);
+ yaz_log(log_server, "Adding %s listener on %s id=%d", mode, where,
+ listen_id);
l = cs_create_host(where, 2, &ap);
if (!l)
return -1;
}
if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
- EVENT_EXCEPT)))
+ EVENT_EXCEPT, listen_id)))
{
yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
cs_close (l);
return -1;
}
- iochan_setdata(lst, l);
+ iochan_setdata(lst, l); /* user-defined data for listener is COMSTACK */
+ l->user = lst; /* user-defined data for COMSTACK is listener chan */
- /* Ensure our listener chain is setup properly */
+ /* Add listener to chain */
lst->next = pListener;
pListener = lst;
return 0; /* OK */
statserv_options_block *statserv_getcontrol(void)
{
- static statserv_options_block cb;
-
- memcpy(&cb, &control_block, sizeof(cb));
- return &cb;
+#if YAZ_POSIX_THREADS
+ if (init_control_tls)
+ return pthread_getspecific(current_control_tls);
+ else
+ return &control_block;
+#else
+ return current_control_block;
+#endif
}
void statserv_setcontrol(statserv_options_block *block)
{
- memcpy(&control_block, block, sizeof(*block));
+#if YAZ_POSIX_THREADS
+ if (init_control_tls)
+ pthread_setspecific(current_control_tls, block);
+#else
+ current_control_block = block;
+#endif
}
static void statserv_reset(void)
int statserv_start(int argc, char **argv)
{
- int ret = 0;
char sep;
#ifdef WIN32
/* We need to initialize the thread list */
ThreadList_Initialize();
/* WIN32 */
#endif
-
+
+
#ifdef WIN32
sep = '\\';
#else
programname = argv[0];
if (control_block.options_func(argc, argv))
- return(1);
+ return 1;
+
+#if YAZ_POSIX_THREADS
+ init_control_tls = 1;
+ pthread_key_create(¤t_control_tls, 0);
+#endif
- if (control_block.bend_start)
- (*control_block.bend_start)(&control_block);
+ xml_config_open();
+
+ xml_config_bend_start();
+
#ifdef WIN32
+ xml_config_add_listeners();
+
yaz_log (log_server, "Starting server %s", me);
if (!pListener && *control_block.default_listen)
- add_listener(control_block.default_listen,
- control_block.default_proto);
-
- if (!pListener)
- return 1;
+ add_listener(control_block.default_listen, 0);
#else
/* UNIX */
if (control_block.inetd)
open("/dev/null", O_RDWR);
dup(0); dup(0);
}
+ xml_config_add_listeners();
+
if (!pListener && *control_block.default_listen)
- add_listener(control_block.default_listen,
- control_block.default_proto);
+ add_listener(control_block.default_listen, 0);
if (!pListener)
return 1;
fprintf(f, "%ld", (long) getpid());
fclose(f);
}
-
+
if (control_block.background)
close(hand[1]);
- yaz_log (log_server, "Starting server %s pid=%ld", programname,
- (long) getpid());
-
+
+ yaz_log (log_server, "Starting server %s pid=%ld", programname,
+ (long) getpid());
#if 0
sigset_t sigs_to_block;
}
/* UNIX */
#endif
- if ((pListener == NULL) && *control_block.default_listen)
- add_listener(control_block.default_listen,
- control_block.default_proto);
-
if (pListener == NULL)
- ret = 1;
- else
- {
- yaz_log(YLOG_DEBUG, "Entering event loop.");
- ret = event_loop(&pListener);
- }
- return ret;
+ return 1;
+ yaz_log(YLOG_DEBUG, "Entering event loop.");
+ return event_loop(&pListener);
+}
+
+static void option_copy(char *dst, const char *src)
+{
+ strncpy(dst, src ? src : "", 127);
+ dst[127] = '\0';
}
int check_options(int argc, char **argv)
control_block.loglevel = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
yaz_log_init_level(control_block.loglevel);
- while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:",
+ while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:",
argv, argc, &arg)) != -2)
{
switch (ret)
{
case 0:
- if (add_listener(arg, control_block.default_proto))
+ if (add_listener(arg, 0))
return 1; /* failed to create listener */
break;
case '1':
#endif
break;
case 'l':
- strcpy(control_block.logfile, arg ? arg : "");
+ option_copy(control_block.logfile, arg);
yaz_log_init(control_block.loglevel, me, control_block.logfile);
break;
case 'v':
- control_block.loglevel = yaz_log_mask_str_x(arg,control_block.loglevel);
+ control_block.loglevel =
+ yaz_log_mask_str_x(arg,control_block.loglevel);
yaz_log_init(control_block.loglevel, me, control_block.logfile);
break;
case 'a':
- strcpy(control_block.apdufile, arg ? arg : "");
+ option_copy(control_block.apdufile, arg);
break;
case 'u':
- strcpy(control_block.setuid, arg ? arg : "");
+ option_copy(control_block.setuid, arg);
break;
case 'c':
- strcpy(control_block.configname, arg ? arg : "");
+ option_copy(control_block.configname, arg);
break;
case 'C':
- strcpy(control_block.cert_fname, arg ? arg : "");
+ option_copy(control_block.cert_fname, arg);
break;
case 'd':
- strcpy(control_block.daemon_name, arg ? arg : "");
+ option_copy(control_block.daemon_name, arg);
break;
case 't':
if (!arg || !(r = atoi(arg)))
max_sessions = atoi(arg);
break;
case 'p':
- if (strlen(arg) >= sizeof(control_block.pid_fname))
- {
- yaz_log(YLOG_FATAL, "pid fname too long");
- exit(1);
- }
- strcpy(control_block.pid_fname, arg);
+ option_copy(control_block.pid_fname, arg);
+ break;
+ case 'f':
+#if HAVE_XML2
+ option_copy(control_block.xml_config, arg);
+#else
+ fprintf(stderr, "%s: Option -f unsupported since YAZ is compiled without Libxml2 support\n", me);
+ exit(1);
+#endif
break;
case 'D':
control_block.background = 1;
bend_initresult *(*bend_init)(bend_initrequest *r),
void (*bend_close)(void *handle))
{
- statserv_options_block *cb = statserv_getcontrol();
-
- cb->bend_init = bend_init;
- cb->bend_close = bend_close;
-
- statserv_setcontrol(cb);
+ control_block.bend_init = bend_init;
+ control_block.bend_close = bend_close;
/* Lets setup the Arg structure */
ArgDetails.argc = argc;
void (*bend_close)(void *handle))
{
int ret;
- statserv_options_block *cb = statserv_getcontrol();
-
- cb->bend_init = bend_init;
- cb->bend_close = bend_close;
- statserv_setcontrol(cb);
+ control_block.bend_init = bend_init;
+ control_block.bend_close = bend_close;
+
ret = statserv_start (argc, argv);
statserv_closedown ();
statserv_reset();
* Copyright (C) 1995-2005, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: ztest.c,v 1.71 2005-01-15 19:47:16 adam Exp $
+ * $Id: ztest.c,v 1.72 2005-02-01 14:46:48 adam Exp $
*/
/*
char *dummy_marc_record (int num, ODR odr);
char *dummy_xml_record (int num, ODR odr);
-int ztest_search (void *handle, bend_search_rr *rr);
-int ztest_sort (void *handle, bend_sort_rr *rr);
-int ztest_present (void *handle, bend_present_rr *rr);
-int ztest_esrequest (void *handle, bend_esrequest_rr *rr);
-int ztest_delete (void *handle, bend_delete_rr *rr);
+int ztest_search(void *handle, bend_search_rr *rr);
+int ztest_sort(void *handle, bend_sort_rr *rr);
+int ztest_present(void *handle, bend_present_rr *rr);
+int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
+int ztest_delete(void *handle, bend_delete_rr *rr);
-int ztest_search (void *handle, bend_search_rr *rr)
+int ztest_search(void *handle, bend_search_rr *rr)
{
if (rr->num_bases != 1)
{
q->bend_explain = ztest_explain;
q->bend_srw_scan = ztest_scan;
+ yaz_log(YLOG_LOG, "ztest_init handle=%p control=%p",
+ counter, statserv_getcontrol());
+
return r;
}
void bend_close(void *handle)
{
+ yaz_log(YLOG_LOG, "ztest_close handle=%p control=%p",
+ handle, statserv_getcontrol());
+
xfree (handle); /* release our user-defined handle */
return;
}