-/*
- * Copyright (C) 1995-2005, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2008 Index Data
* See the file LICENSE for details.
- *
- * NT threaded server code by
- * Chas Woodfield, Fretwell Downing Informatics.
- *
- * $Id: statserv.c,v 1.36 2006-03-22 17:58:32 mike Exp $
*/
/**
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
+
#ifdef WIN32
#include <process.h>
#include <winsock.h>
#include <direct.h>
-#include "service.h"
#endif
+
+#include <yaz/sc.h>
+
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <pwd.h>
#endif
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xinclude.h>
/*
* default behavior.
*/
-#define STAT_DEFAULT_LOG_LEVEL "none,fatal,warn,log,server,session,request"
-/* the 'none' clears yaz' own default settings, including [log] */
+#define STAT_DEFAULT_LOG_LEVEL "server,session,request"
int check_options(int argc, char **argv);
statserv_options_block control_block = {
1, /* dynamic mode */
0, /* threaded mode */
0, /* one shot (single session) */
- 0, /* __UNUSED_loglevel */
"", /* no PDUs */
"", /* diagnostic output to stderr */
"tcp:@:9999", /* default listener port */
static int max_sessions = 0;
static int logbits_set = 0;
-static int log_session = 0;
+static int log_session = 0; /* one-line logs for session */
+static int log_sessiondetail = 0; /* more detailed stuff */
static int log_server = 0;
/** get_logbits sets global loglevel bits */
{
logbits_set = 1;
log_session = yaz_log_module_level("session");
+ log_sessiondetail = yaz_log_module_level("sessiondetail");
log_server = yaz_log_module_level("server");
}
}
static int add_listener(char *where, int listen_id);
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
static xmlDocPtr xml_config_doc = 0;
#endif
-#if HAVE_XML2
-static xmlNodePtr xml_config_get_root()
+#if YAZ_HAVE_XML2
+static xmlNodePtr xml_config_get_root(void)
{
xmlNodePtr ptr = 0;
if (xml_config_doc)
}
#endif
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
{
unsigned char *cp;
len += xmlStrlen(p->content);
}
/* now allocate for the string */
- str = nmem_malloc(n, len);
+ str = (unsigned char *) nmem_malloc(n, len);
*str = '\0'; /* so we can use strcat */
for (p = ptr; p; p = p->next)
{
}
#endif
-static struct gfs_server * gfs_server_new()
+static struct gfs_server * gfs_server_new(void)
{
- struct gfs_server *n = nmem_malloc(gfs_nmem, sizeof(*n));
+ struct gfs_server *n = (struct gfs_server *)
+ nmem_malloc(gfs_nmem, sizeof(*n));
memcpy(&n->cb, &control_block, sizeof(control_block));
n->next = 0;
n->host = 0;
n->listen_ref = 0;
n->cql_transform = 0;
+ n->ccl_transform = 0;
n->server_node_ptr = 0;
n->directory = 0;
n->docpath = 0;
n->stylesheet = 0;
+#if YAZ_HAVE_XML2
+ n->retrieval = yaz_retrieval_create();
+#endif
return n;
}
static struct gfs_listen * gfs_listen_new(const char *id,
const char *address)
{
- struct gfs_listen *n = nmem_malloc(gfs_nmem, sizeof(*n));
+ struct gfs_listen *n = (struct gfs_listen *)
+ nmem_malloc(gfs_nmem, sizeof(*n));
if (id)
n->id = nmem_strdup(gfs_nmem, id);
else
*cp = '\0';
host = vhost;
}
- assoc->cql_transform = 0;
- assoc->server_node_ptr = 0;
- assoc->docpath = 0;
- assoc->stylesheet = 0;
+ assoc->server = 0;
if (control_block.xml_config[0])
{
struct gfs_server *gfs;
xfree(assoc->init);
assoc->init = 0;
}
- assoc->docpath = gfs->docpath;
- assoc->stylesheet = gfs->stylesheet;
- assoc->cql_transform = gfs->cql_transform;
- assoc->server_node_ptr = gfs->server_node_ptr;
+ assoc->server = gfs;
assoc->last_control = &gfs->cb;
statserv_setcontrol(&gfs->cb);
+
gfs_server_chdir(gfs);
- yaz_log(YLOG_DEBUG, "server select: %s", gfs->cb.configname);
- return 1;
+ break;
}
}
- statserv_setcontrol(0);
- assoc->last_control = 0;
- yaz_log(YLOG_DEBUG, "server select: no match");
- return 0;
+ if (!gfs)
+ {
+ statserv_setcontrol(0);
+ assoc->last_control = 0;
+ return 0;
+ }
}
else
{
statserv_setcontrol(&control_block);
assoc->last_control = &control_block;
- yaz_log(YLOG_DEBUG, "server select: config=%s", control_block.configname);
- return 1;
}
+ yaz_log(YLOG_DEBUG, "server select: config=%s",
+ assoc->last_control->configname);
+
+ assoc->maximumRecordSize = assoc->last_control->maxrecordsize;
+ assoc->preferredMessageSize = assoc->last_control->maxrecordsize;
+ cs_set_max_recv_bytes(assoc->client_link, assoc->maximumRecordSize);
+ return 1;
}
-static void xml_config_read()
+static void xml_config_read(void)
{
struct gfs_server **gfsp = &gfs_server_list;
struct gfs_listen **gfslp = &gfs_listen_list;
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
xmlNodePtr ptr = xml_config_get_root();
if (!ptr)
xmlNodePtr ptr;
const char *listenref = 0;
const char *id = 0;
+ struct gfs_server *gfs;
for ( ; attr; attr = attr->next)
if (!xmlStrcmp(attr->name, BAD_CAST "listenref")
else
yaz_log(YLOG_WARN, "Unknown attribute '%s' for server",
attr->name);
- *gfsp = gfs_server_new();
- (*gfsp)->server_node_ptr = ptr_server;
+ gfs = *gfsp = gfs_server_new();
+ gfs->server_node_ptr = ptr_server;
if (listenref)
{
int id_no;
for (id_no = 1; gl; gl = gl->next, id_no++)
if (gl->id && !strcmp(gl->id, listenref))
{
- (*gfsp)->listen_ref = id_no;
+ gfs->listen_ref = id_no;
break;
}
if (!gl)
continue;
if (!strcmp((const char *) ptr->name, "host"))
{
- (*gfsp)->host = nmem_dup_xml_content(gfs_nmem,
- ptr->children);
+ gfs->host = nmem_dup_xml_content(gfs_nmem,
+ ptr->children);
}
else if (!strcmp((const char *) ptr->name, "config"))
{
- strcpy((*gfsp)->cb.configname,
+ strcpy(gfs->cb.configname,
nmem_dup_xml_content(gfs_nmem, ptr->children));
}
else if (!strcmp((const char *) ptr->name, "cql2rpn"))
{
- (*gfsp)->cql_transform = cql_transform_open_fname(
+ gfs->cql_transform = cql_transform_open_fname(
nmem_dup_xml_content(gfs_nmem, ptr->children)
);
}
+ else if (!strcmp((const char *) ptr->name, "ccl2rpn"))
+ {
+ char *name;
+ FILE *f;
+
+ name = nmem_dup_xml_content(gfs_nmem, ptr->children);
+ if ((f = fopen(name, "r")) == 0) {
+ yaz_log(YLOG_FATAL, "can't open CCL file '%s'", name);
+ exit(1);
+ }
+ gfs->ccl_transform = ccl_qual_mk();
+ ccl_qual_file (gfs->ccl_transform, f);
+ fclose(f);
+ }
else if (!strcmp((const char *) ptr->name, "directory"))
{
- (*gfsp)->directory =
+ gfs->directory =
nmem_dup_xml_content(gfs_nmem, ptr->children);
}
else if (!strcmp((const char *) ptr->name, "docpath"))
{
- (*gfsp)->docpath =
+ gfs->docpath =
nmem_dup_xml_content(gfs_nmem, ptr->children);
}
+ else if (!strcmp((const char *) ptr->name, "maximumrecordsize"))
+ {
+ gfs->cb.maxrecordsize = atoi(
+ nmem_dup_xml_content(gfs_nmem, ptr->children));
+ }
else if (!strcmp((const char *) ptr->name, "stylesheet"))
{
char *s = nmem_dup_xml_content(gfs_nmem, ptr->children);
- (*gfsp)->stylesheet =
+ gfs->stylesheet = (char *)
nmem_malloc(gfs_nmem, strlen(s) + 2);
- sprintf((*gfsp)->stylesheet, "/%s", s);
+ sprintf(gfs->stylesheet, "/%s", s);
}
else if (!strcmp((const char *) ptr->name, "explain"))
{
; /* being processed separately */
}
+ else if (!strcmp((const char *) ptr->name, "retrievalinfo"))
+ {
+ if (yaz_retrieval_configure(gfs->retrieval, ptr))
+ {
+ yaz_log(YLOG_FATAL, "%s in config %s",
+ yaz_retrieval_get_error(gfs->retrieval),
+ control_block.xml_config);
+ exit(1);
+ }
+ }
else
{
yaz_log(YLOG_FATAL, "Unknown element '%s' in config %s",
*gfsp = 0;
}
-static void xml_config_open()
+static void xml_config_open(void)
{
if (!getcwd(gfs_root_dir, FILENAME_MAX))
{
#endif
gfs_nmem = nmem_create();
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
if (control_block.xml_config[0] == '\0')
return;
#endif
}
-static void xml_config_close()
+static void xml_config_close(void)
{
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
if (xml_config_doc)
{
xmlFreeDoc(xml_config_doc);
#endif
}
-static void xml_config_add_listeners()
+static void xml_config_add_listeners(void)
{
struct gfs_listen *gfs = gfs_listen_list;
int id_no;
}
}
-static void xml_config_bend_start()
+static void xml_config_bend_start(void)
{
if (control_block.xml_config[0])
{
}
}
-static void xml_config_bend_stop()
+static void xml_config_bend_stop(void)
{
if (control_block.xml_config[0])
{
/* Initialize the critical Sections */
InitializeCriticalSection(&Thread_CritSect);
- /* Set the first thraed */
+ /* Set the first thraed */
pFirstThread = NULL;
/* we have been initialized */
}
/* WIN32 statserv_closedown */
-void statserv_closedown()
+static void statserv_closedown()
{
/* Shouldn't do anything if we are not initialized */
if (bInitialized)
xml_config_close();
}
-void __cdecl event_loop_thread (IOCHAN iochan)
+void __cdecl event_loop_thread(IOCHAN iochan)
{
- event_loop (&iochan);
+ iochan_event_loop(&iochan);
}
/* WIN32 listener */
if (event == EVENT_INPUT)
{
+ COMSTACK new_line;
+ IOCHAN new_chan;
+
if ((res = cs_listen(line, 0, 0)) < 0)
{
- yaz_log(YLOG_FATAL, "cs_listen failed");
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "cs_listen failed");
return;
}
else if (res == 1)
- return;
+ return; /* incomplete */
yaz_log(YLOG_DEBUG, "listen ok");
- iochan_setevent(h, EVENT_OUTPUT);
- iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
- }
- else if (event == EVENT_OUTPUT)
- {
- COMSTACK new_line = cs_accept(line);
- IOCHAN new_chan;
- char *a = NULL;
-
- if (!new_line)
+ new_line = cs_accept(line);
+ if (!new_line)
{
yaz_log(YLOG_FATAL, "Accept failed.");
- iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
return;
}
yaz_log(YLOG_DEBUG, "Accept ok");
{
}
-void statserv_closedown()
+static void statserv_closedown(void)
{
IOCHAN p;
term_flag = 1;
}
-static void *new_session (void *vp);
+static void *new_session(void *vp);
static int no_sessions = 0;
/* UNIX listener */
return;
}
- yaz_log(log_session, "Connect from %s", cs_addrstr(new_line));
+ yaz_log(log_sessiondetail, "Connect from %s", cs_addrstr(new_line));
no_sessions++;
if (control_block.dynamic)
{
#if YAZ_POSIX_THREADS
pthread_t child_thread;
- pthread_create (&child_thread, 0, new_session, new_line);
- pthread_detach (child_thread);
+ pthread_create(&child_thread, 0, new_session, new_line);
+ pthread_detach(child_thread);
#elif YAZ_GNU_THREADS
pth_attr_t attr;
pth_t child_thread;
- attr = pth_attr_new ();
- pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
- pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 32*1024);
- pth_attr_set (attr, PTH_ATTR_NAME, "session");
- yaz_log (YLOG_DEBUG, "pth_spawn begin");
- child_thread = pth_spawn (attr, new_session, new_line);
- yaz_log (YLOG_DEBUG, "pth_spawn finish");
- pth_attr_destroy (attr);
+ attr = pth_attr_new();
+ pth_attr_set(attr, PTH_ATTR_JOINABLE, FALSE);
+ pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 32*1024);
+ pth_attr_set(attr, PTH_ATTR_NAME, "session");
+ yaz_log(YLOG_DEBUG, "pth_spawn begin");
+ child_thread = pth_spawn(attr, new_session, new_line);
+ yaz_log(YLOG_DEBUG, "pth_spawn finish");
+ pth_attr_destroy(attr);
#else
new_session(new_line);
#endif
}
}
-static void *new_session (void *vp)
+static void *new_session(void *vp)
{
char *a;
association *newas;
IOCHAN new_chan;
COMSTACK new_line = (COMSTACK) vp;
- IOCHAN parent_chan = new_line->user;
+ IOCHAN parent_chan = (IOCHAN) new_line->user;
+ yaz_log_xml_errors(0, YLOG_WARN);
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);
#else
a = 0;
#endif
- yaz_log(log_session, "Starting session %d from %s (pid=%ld)",
+ yaz_log(log_session, "Session - OK %d %s %ld",
no_sessions, a ? a : "[Unknown]", (long) getpid());
if (max_sessions && no_sessions >= max_sessions)
control_block.one_shot = 1;
if (control_block.threads)
{
- event_loop(&new_chan);
+ iochan_event_loop(&new_chan);
}
else
{
iochan_setdata(chan, assoc);
iochan_settimeout(chan, 60);
addr = cs_addrstr(line);
- yaz_log(log_session, "Inetd association from %s",
+ yaz_log(log_sessiondetail, "Inetd association from %s",
addr ? addr : "[UNKNOWN]");
assoc->cs_get_mask = EVENT_INPUT;
}
if (cs_bind(l, ap, CS_SERVER) < 0)
{
- yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
- cs_close (l);
+ if (cs_errno(l) == CSYSERR)
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
+ else
+ yaz_log(YLOG_FATAL, "Failed to bind to %s: %s", where,
+ cs_strerror(l));
+ cs_close(l);
return -1;
}
if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
- EVENT_EXCEPT, listen_id)))
+ EVENT_EXCEPT, listen_id)))
{
yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
- cs_close (l);
+ cs_close(l);
return -1;
}
iochan_setdata(lst, l); /* user-defined data for listener is COMSTACK */
return &control_block;
#elif YAZ_POSIX_THREADS
if (init_control_tls)
- return pthread_getspecific(current_control_tls);
+ return (statserv_options_block *)
+ pthread_getspecific(current_control_tls);
else
return &control_block;
#else
{
}
-int statserv_start(int argc, char **argv)
+static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
{
char sep;
#ifdef WIN32
#else
sep = '/';
#endif
- if ((me = strrchr (argv[0], sep)))
+ if ((me = strrchr(argv[0], sep)))
me++; /* get the basename */
else
me = argv[0];
#ifdef WIN32
xml_config_add_listeners();
- yaz_log (log_server, "Starting server %s", me);
+ yaz_log(log_server, "Starting server %s", me);
if (!pListener && *control_block.default_listen)
add_listener(control_block.default_listen, 0);
#else
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;
sigemptyset(&sigs_to_block);
- sigaddset (&sigs_to_block, SIGTERM);
- pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
+ sigaddset(&sigs_to_block, SIGTERM);
+ pthread_sigmask(SIG_BLOCK, &sigs_to_block, 0);
/* missing... */
#endif
if (control_block.dynamic)
signal(SIGCHLD, catchchld);
}
- signal (SIGPIPE, SIG_IGN);
- signal (SIGTERM, sigterm);
+ signal(SIGPIPE, SIG_IGN);
+ signal(SIGTERM, sigterm);
if (*control_block.setuid)
{
struct passwd *pw;
#endif
if (pListener == NULL)
return 1;
+ if (s)
+ yaz_sc_running(s);
yaz_log(YLOG_DEBUG, "Entering event loop.");
- return event_loop(&pListener);
+ return iochan_event_loop(&pListener);
}
static void option_copy(char *dst, const char *src)
int ret = 0, r;
char *arg;
- if (getenv("YAZ_LOG") == 0) {
- /*
- * Set default log level. We want to avoid doing this if the
- * user has already explicitly specified a preferred default
- * log-level, hence the inelegant peek at the YAZ_LOG
- * environment variable that will subsequently be interpreted
- * by the YAZ logging module itself.
- */
- yaz_log_init_level(yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL));
- }
+ yaz_log_init_level(yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL));
get_logbits(1);
- while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:m:",
+
+ while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:m:r:",
argv, argc, &arg)) != -2)
{
switch (ret)
control_block.default_proto = PROTO_Z3950;
break;
case 's':
- fprintf (stderr, "%s: SR protocol no longer supported\n", me);
- exit (1);
+ fprintf(stderr, "%s: SR protocol no longer supported\n", me);
+ exit(1);
break;
case 'S':
control_block.dynamic = 0;
}
yaz_log_time_format(arg);
break;
- case 'v': {
- int default_level = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
- yaz_log_init_level(yaz_log_mask_str_x(arg, default_level));
+ case 'v':
+ yaz_log_init_level(yaz_log_mask_str(arg));
get_logbits(1);
break;
- }
case 'a':
option_copy(control_block.apdufile, arg);
break;
option_copy(control_block.pid_fname, arg);
break;
case 'f':
-#if HAVE_XML2
+#if YAZ_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);
case 'D':
control_block.background = 1;
break;
+ case 'r':
+ if (!arg || !(r = atoi(arg)))
+ {
+ fprintf(stderr, "%s: Specify positive size for -r.\n", me);
+ return(1);
+ }
+ yaz_log_init_max_size(r * 1024);
+ break;
default:
fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
" -l <logfile> -u <user> -c <config> -t <minutes>"
" -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
- " -ziDST1 -m <time-format> -w <directory> <listener-addr>... ]\n", me);
+ " -ziDST1 -m <time-format> -w <directory> <listener-addr>... ]\n", me);
return 1;
}
}
return 0;
}
-#ifdef WIN32
-typedef struct _Args
+void statserv_sc_stop(yaz_sc_t s)
{
- char **argv;
- int argc;
-} Args;
-
-static Args ArgDetails;
-
-/* name of the executable */
-#define SZAPPNAME "server"
-
-/* list of service dependencies - "dep1\0dep2\0\0" */
-#define SZDEPENDENCIES ""
-
-int statserv_main(int argc, char **argv,
- bend_initresult *(*bend_init)(bend_initrequest *r),
- void (*bend_close)(void *handle))
-{
- struct statserv_options_block *cb = &control_block;
- cb->bend_init = bend_init;
- cb->bend_close = bend_close;
-
- /* Lets setup the Arg structure */
- ArgDetails.argc = argc;
- ArgDetails.argv = argv;
-
- /* Now setup the service with the service controller */
- SetupService(argc, argv, &ArgDetails, SZAPPNAME,
- cb->service_name, /* internal service name */
- cb->service_display_name, /* displayed name */
- SZDEPENDENCIES);
- return 0;
-}
-
-int StartAppService(void *pHandle, int argc, char **argv)
-{
- /* Initializes the App */
- return 1;
-}
-
-void RunAppService(void *pHandle)
-{
- Args *pArgs = (Args *)pHandle;
-
- /* Starts the app running */
- statserv_start(pArgs->argc, pArgs->argv);
-}
-
-void StopAppService(void *pHandle)
-{
- /* Stops the app */
statserv_closedown();
statserv_reset();
}
-/* WIN32 */
-#else
-/* UNIX */
+
int statserv_main(int argc, char **argv,
bend_initresult *(*bend_init)(bend_initrequest *r),
void (*bend_close)(void *handle))
{
int ret;
+ struct statserv_options_block *cb = &control_block;
- control_block.bend_init = bend_init;
- control_block.bend_close = bend_close;
+ /* control block does not have service_name member on Unix */
+ yaz_sc_t s = yaz_sc_create(
+#ifdef WIN32
+ cb->service_name, cb->service_display_name
+#else
+ 0, 0
+#endif
+ );
- ret = statserv_start (argc, argv);
- statserv_closedown ();
- statserv_reset();
+ cb->bend_init = bend_init;
+ cb->bend_close = bend_close;
+
+ ret = yaz_sc_program(s, argc, argv, statserv_sc_main, statserv_sc_stop);
+ yaz_sc_destroy(&s);
return ret;
}
-#endif
+
/*
* Local variables:
* c-basic-offset: 4