1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements GFS logic
25 #include <sys/types.h>
38 #include <libxml/parser.h>
39 #include <libxml/tree.h>
40 #include <libxml/xinclude.h>
53 #include <yaz/comstack.h>
54 #include <yaz/tcpip.h>
55 #include <yaz/options.h>
56 #include <yaz/errno.h>
58 #include <yaz/xmosi.h>
63 #include <yaz/statserv.h>
64 #include <yaz/daemon.h>
66 static IOCHAN pListener = NULL;
68 static char gfs_root_dir[FILENAME_MAX+1];
69 static struct gfs_server *gfs_server_list = 0;
70 static struct gfs_listen *gfs_listen_list = 0;
71 static NMEM gfs_nmem = 0;
73 static char *me = "statserver"; /* log prefix */
74 static char *programname="statserver"; /* full program name */
76 DWORD current_control_tls;
77 static int init_control_tls = 0;
78 #elif YAZ_POSIX_THREADS
79 static pthread_key_t current_control_tls;
80 static int init_control_tls = 0;
82 static statserv_options_block *current_control_block = 0;
88 #define STAT_DEFAULT_LOG_LEVEL "server,session,request"
90 int check_options(int argc, char **argv);
91 statserv_options_block control_block = {
93 0, /* threaded mode */
94 0, /* one shot (single session) */
96 "", /* diagnostic output to stderr */
97 "tcp:@:9999", /* default listener port */
98 PROTO_Z3950, /* default application protocol */
99 900, /* idle timeout (seconds) */
100 1024*1024, /* maximum PDU size (approx.) to allow */
101 "default-config", /* configuration name to pass to backend */
102 "", /* set user id */
103 0, /* bend_start handler */
104 0, /* bend_stop handler */
105 check_options, /* Default routine, for checking the run-time arguments */
108 0, /* default value for inet deamon */
109 0, /* handle (for service, etc) */
110 0, /* bend_init handle */
111 0, /* bend_close handle */
113 "Z39.50 Server", /* NT Service Name */
114 "Server", /* NT application Name */
115 "", /* NT Service Dependencies */
116 "Z39.50 Server", /* NT Service Display Name */
118 0, /* SOAP handlers */
120 0, /* background daemon */
121 "", /* SSL certificate filename */
122 "" /* XML config filename */
125 static int max_sessions = 0;
127 static int logbits_set = 0;
128 static int log_session = 0; /* one-line logs for session */
129 static int log_sessiondetail = 0; /* more detailed stuff */
130 static int log_server = 0;
132 /** get_logbits sets global loglevel bits */
133 static void get_logbits(int force)
134 { /* needs to be called after parsing cmd-line args that can set loglevels!*/
135 if (force || !logbits_set)
138 log_session = yaz_log_module_level("session");
139 log_sessiondetail = yaz_log_module_level("sessiondetail");
140 log_server = yaz_log_module_level("server");
145 static int add_listener(char *where, int listen_id);
148 static xmlDocPtr xml_config_doc = 0;
152 static xmlNodePtr xml_config_get_root(void)
157 ptr = xmlDocGetRootElement(xml_config_doc);
158 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
159 strcmp((const char *) ptr->name, "yazgfs"))
161 yaz_log(YLOG_WARN, "Bad/missing root element for config %s",
162 control_block.xml_config);
172 static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
176 int len = 1; /* start with 1, because of trailing 0 */
178 int first = 1; /* whitespace lead flag .. */
179 /* determine length */
180 for (p = ptr; p; p = p->next)
182 if (p->type == XML_TEXT_NODE)
183 len += xmlStrlen(p->content);
185 /* now allocate for the string */
186 str = (unsigned char *) nmem_malloc(n, len);
187 *str = '\0'; /* so we can use strcat */
188 for (p = ptr; p; p = p->next)
190 if (p->type == XML_TEXT_NODE)
195 while(*cp && isspace(*cp))
198 first = 0; /* reset if we got non-whitespace out */
200 strcat((char *)str, (const char *)cp); /* append */
203 /* remove trailing whitespace */
204 cp = strlen((const char *)str) + str;
205 while (cp != str && isspace(cp[-1]))
208 /* return resulting string */
214 static struct gfs_server * gfs_server_new(void)
216 struct gfs_server *n = (struct gfs_server *)
217 nmem_malloc(gfs_nmem, sizeof(*n));
218 memcpy(&n->cb, &control_block, sizeof(control_block));
222 n->cql_transform = 0;
223 n->ccl_transform = 0;
224 n->server_node_ptr = 0;
228 n->retrieval = yaz_retrieval_create();
234 static struct gfs_listen * gfs_listen_new(const char *id,
237 struct gfs_listen *n = (struct gfs_listen *)
238 nmem_malloc(gfs_nmem, sizeof(*n));
240 n->id = nmem_strdup(gfs_nmem, id);
244 n->address = nmem_strdup(gfs_nmem, address);
249 static void gfs_server_chdir(struct gfs_server *gfs)
253 if (chdir(gfs_root_dir))
254 yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s", gfs_root_dir);
258 if (chdir(gfs->directory))
259 yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s",
264 int control_association(association *assoc, const char *host, int force_open)
266 char vhost[128], *cp;
269 strncpy(vhost, host, 127);
271 cp = strchr(vhost, ':');
277 if (control_block.xml_config[0])
279 struct gfs_server *gfs;
280 for (gfs = gfs_server_list; gfs; gfs = gfs->next)
282 int listen_match = 0;
284 if ( !gfs->host || (host && gfs->host && !strcmp(host, gfs->host)))
286 if (!gfs->listen_ref ||
287 gfs->listen_ref == assoc->client_chan->chan_id)
289 if (listen_match && host_match)
292 (assoc->last_control != &gfs->cb && assoc->backend))
294 statserv_setcontrol(assoc->last_control);
295 if (assoc->backend && assoc->init)
297 gfs_server_chdir(gfs);
298 (assoc->last_control->bend_close)(assoc->backend);
305 assoc->last_control = &gfs->cb;
306 statserv_setcontrol(&gfs->cb);
308 gfs_server_chdir(gfs);
314 statserv_setcontrol(0);
315 assoc->last_control = 0;
321 statserv_setcontrol(&control_block);
322 assoc->last_control = &control_block;
324 yaz_log(YLOG_DEBUG, "server select: config=%s",
325 assoc->last_control->configname);
327 assoc->maximumRecordSize = assoc->last_control->maxrecordsize;
328 assoc->preferredMessageSize = assoc->last_control->maxrecordsize;
329 cs_set_max_recv_bytes(assoc->client_link, assoc->maximumRecordSize);
334 static void xml_config_read(void)
336 struct gfs_server **gfsp = &gfs_server_list;
337 struct gfs_listen **gfslp = &gfs_listen_list;
338 xmlNodePtr ptr = xml_config_get_root();
342 for (ptr = ptr->children; ptr; ptr = ptr->next)
344 struct _xmlAttr *attr;
345 if (ptr->type != XML_ELEMENT_NODE)
347 attr = ptr->properties;
348 if (!strcmp((const char *) ptr->name, "listen"))
351 <listen id="listenerid">tcp:@:9999</listen>
354 const char *address =
355 nmem_dup_xml_content(gfs_nmem, ptr->children);
356 for ( ; attr; attr = attr->next)
357 if (!xmlStrcmp(attr->name, BAD_CAST "id")
358 && attr->children && attr->children->type == XML_TEXT_NODE)
359 id = nmem_dup_xml_content(gfs_nmem, attr->children);
362 *gfslp = gfs_listen_new(id, address);
363 gfslp = &(*gfslp)->next;
364 *gfslp = 0; /* make listener list consistent for search */
367 else if (!strcmp((const char *) ptr->name, "server"))
369 xmlNodePtr ptr_server = ptr;
371 const char *listenref = 0;
373 struct gfs_server *gfs;
375 for ( ; attr; attr = attr->next)
376 if (!xmlStrcmp(attr->name, BAD_CAST "listenref")
377 && attr->children && attr->children->type == XML_TEXT_NODE)
378 listenref = nmem_dup_xml_content(gfs_nmem, attr->children);
379 else if (!xmlStrcmp(attr->name, BAD_CAST "id")
381 && attr->children->type == XML_TEXT_NODE)
382 id = nmem_dup_xml_content(gfs_nmem, attr->children);
384 yaz_log(YLOG_WARN, "Unknown attribute '%s' for server",
386 gfs = *gfsp = gfs_server_new();
387 gfs->server_node_ptr = ptr_server;
391 struct gfs_listen *gl = gfs_listen_list;
392 for (id_no = 1; gl; gl = gl->next, id_no++)
393 if (gl->id && !strcmp(gl->id, listenref))
395 gfs->listen_ref = id_no;
399 yaz_log(YLOG_WARN, "Non-existent listenref '%s' in server "
400 "config element", listenref);
402 for (ptr = ptr_server->children; ptr; ptr = ptr->next)
404 if (ptr->type != XML_ELEMENT_NODE)
406 if (!strcmp((const char *) ptr->name, "host"))
408 gfs->host = nmem_dup_xml_content(gfs_nmem,
411 else if (!strcmp((const char *) ptr->name, "config"))
413 strcpy(gfs->cb.configname,
414 nmem_dup_xml_content(gfs_nmem, ptr->children));
416 else if (!strcmp((const char *) ptr->name, "cql2rpn"))
418 gfs->cql_transform = cql_transform_open_fname(
419 nmem_dup_xml_content(gfs_nmem, ptr->children)
422 else if (!strcmp((const char *) ptr->name, "ccl2rpn"))
427 name = nmem_dup_xml_content(gfs_nmem, ptr->children);
428 if ((f = fopen(name, "r")) == 0) {
429 yaz_log(YLOG_FATAL, "can't open CCL file '%s'", name);
432 gfs->ccl_transform = ccl_qual_mk();
433 ccl_qual_file (gfs->ccl_transform, f);
436 else if (!strcmp((const char *) ptr->name, "directory"))
439 nmem_dup_xml_content(gfs_nmem, ptr->children);
441 else if (!strcmp((const char *) ptr->name, "docpath"))
444 nmem_dup_xml_content(gfs_nmem, ptr->children);
446 else if (!strcmp((const char *) ptr->name, "maximumrecordsize"))
448 gfs->cb.maxrecordsize = atoi(
449 nmem_dup_xml_content(gfs_nmem, ptr->children));
451 else if (!strcmp((const char *) ptr->name, "stylesheet"))
453 char *s = nmem_dup_xml_content(gfs_nmem, ptr->children);
454 gfs->stylesheet = (char *)
455 nmem_malloc(gfs_nmem, strlen(s) + 2);
456 sprintf(gfs->stylesheet, "/%s", s);
458 else if (!strcmp((const char *) ptr->name, "explain"))
460 ; /* being processed separately */
462 else if (!strcmp((const char *) ptr->name, "retrievalinfo"))
464 if (yaz_retrieval_configure(gfs->retrieval, ptr))
466 yaz_log(YLOG_FATAL, "%s in config %s",
467 yaz_retrieval_get_error(gfs->retrieval),
468 control_block.xml_config);
474 yaz_log(YLOG_FATAL, "Unknown element '%s' in config %s",
475 ptr->name, control_block.xml_config);
479 gfsp = &(*gfsp)->next;
486 static void xml_config_open(void)
488 if (!getcwd(gfs_root_dir, FILENAME_MAX))
490 yaz_log(YLOG_WARN|YLOG_ERRNO, "getcwd failed");
491 gfs_root_dir[0] = '\0';
494 init_control_tls = 1;
495 current_control_tls = TlsAlloc();
496 #elif YAZ_POSIX_THREADS
497 init_control_tls = 1;
498 pthread_key_create(¤t_control_tls, 0);
501 gfs_nmem = nmem_create();
503 if (control_block.xml_config[0] == '\0')
508 xml_config_doc = xmlParseFile(control_block.xml_config);
511 yaz_log(YLOG_FATAL, "Could not parse %s", control_block.xml_config);
516 int noSubstitutions = xmlXIncludeProcess(xml_config_doc);
517 if (noSubstitutions == -1)
519 yaz_log(YLOG_WARN, "XInclude processing failed for config %s",
520 control_block.xml_config);
529 static void xml_config_close(void)
534 xmlFreeDoc(xml_config_doc);
539 nmem_destroy(gfs_nmem);
541 if (init_control_tls)
542 TlsFree(current_control_tls);
543 #elif YAZ_POSIX_THREADS
544 if (init_control_tls)
545 pthread_key_delete(current_control_tls);
549 static void xml_config_add_listeners(void)
551 struct gfs_listen *gfs = gfs_listen_list;
554 for (id_no = 1; gfs; gfs = gfs->next, id_no++)
557 add_listener(gfs->address, id_no);
561 static void xml_config_bend_start(void)
563 if (control_block.xml_config[0])
565 struct gfs_server *gfs = gfs_server_list;
566 for (; gfs; gfs = gfs->next)
568 yaz_log(YLOG_DEBUG, "xml_config_bend_start config=%s",
570 statserv_setcontrol(&gfs->cb);
571 if (control_block.bend_start)
573 gfs_server_chdir(gfs);
574 (control_block.bend_start)(&gfs->cb);
580 yaz_log(YLOG_DEBUG, "xml_config_bend_start default config");
581 statserv_setcontrol(&control_block);
582 if (control_block.bend_start)
583 (*control_block.bend_start)(&control_block);
587 static void xml_config_bend_stop(void)
589 if (control_block.xml_config[0])
591 struct gfs_server *gfs = gfs_server_list;
592 for (; gfs; gfs = gfs->next)
594 yaz_log(YLOG_DEBUG, "xml_config_bend_stop config=%s",
596 statserv_setcontrol(&gfs->cb);
597 if (control_block.bend_stop)
598 (control_block.bend_stop)(&gfs->cb);
603 yaz_log(YLOG_DEBUG, "xml_config_bend_stop default config");
604 statserv_setcontrol(&control_block);
605 if (control_block.bend_stop)
606 (*control_block.bend_stop)(&control_block);
611 * handle incoming connect requests.
612 * The dynamic mode is a bit tricky mostly because we want to avoid
613 * doing all of the listening and accepting in the parent - it's
618 typedef struct _ThreadList ThreadList;
627 static ThreadList *pFirstThread;
628 static CRITICAL_SECTION Thread_CritSect;
629 static BOOL bInitialized = FALSE;
631 static void ThreadList_Initialize()
633 /* Initialize the critical Sections */
634 InitializeCriticalSection(&Thread_CritSect);
636 /* Set the first thraed */
639 /* we have been initialized */
643 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
645 /* Only one thread can go through this section at a time */
646 EnterCriticalSection(&Thread_CritSect);
649 /* Lets create our new object */
650 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
651 pNewThread->hThread = hThread;
652 pNewThread->pIOChannel = pIOChannel;
653 pNewThread->pNext = pFirstThread;
654 pFirstThread = pNewThread;
656 /* Lets let somebody else create a new object now */
657 LeaveCriticalSection(&Thread_CritSect);
661 void statserv_remove(IOCHAN pIOChannel)
663 /* Only one thread can go through this section at a time */
664 EnterCriticalSection(&Thread_CritSect);
667 ThreadList *pCurrentThread = pFirstThread;
668 ThreadList *pNextThread;
669 ThreadList *pPrevThread =NULL;
671 /* Step through all the threads */
672 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
674 /* We only need to compare on the IO Channel */
675 if (pCurrentThread->pIOChannel == pIOChannel)
677 /* We have found the thread we want to delete */
678 /* First of all reset the next pointers */
679 if (pPrevThread == NULL)
680 pFirstThread = pCurrentThread->pNext;
682 pPrevThread->pNext = pCurrentThread->pNext;
684 /* All we need todo now is delete the memory */
685 free(pCurrentThread);
687 /* No need to look at any more threads */
692 /* We need to look at another thread */
693 pNextThread = pCurrentThread->pNext;
694 pPrevThread = pCurrentThread;
698 /* Lets let somebody else remove an object now */
699 LeaveCriticalSection(&Thread_CritSect);
703 /* WIN32 statserv_closedown */
704 static void statserv_closedown()
706 /* Shouldn't do anything if we are not initialized */
710 HANDLE *pThreadHandles = NULL;
712 /* We need to stop threads adding and removing while we */
713 /* start the closedown process */
714 EnterCriticalSection(&Thread_CritSect);
717 /* We have exclusive access to the thread stuff now */
718 /* Y didn't i use a semaphore - Oh well never mind */
719 ThreadList *pCurrentThread = pFirstThread;
721 /* Before we do anything else, we need to shutdown the listener */
722 if (pListener != NULL)
723 iochan_destroy(pListener);
725 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
727 /* Just destroy the IOCHAN, that should do the trick */
728 iochan_destroy(pCurrentThread->pIOChannel);
729 closesocket(pCurrentThread->pIOChannel->fd);
731 /* Keep a running count of our handles */
737 HANDLE *pCurrentHandle ;
739 /* Allocate the thread handle array */
740 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
741 pCurrentHandle = pThreadHandles;
743 for (pCurrentThread = pFirstThread;
744 pCurrentThread != NULL;
745 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
747 /* Just the handle */
748 *pCurrentHandle = pCurrentThread->hThread;
752 /* We can now leave the critical section */
753 LeaveCriticalSection(&Thread_CritSect);
756 /* Now we can really do something */
759 yaz_log(log_server, "waiting for %d to die", iHandles);
760 /* This will now wait, until all the threads close */
761 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
763 /* Free the memory we allocated for the handle array */
764 free(pThreadHandles);
767 xml_config_bend_stop();
768 /* No longer require the critical section, since all threads are dead */
769 DeleteCriticalSection(&Thread_CritSect);
774 void __cdecl event_loop_thread(IOCHAN iochan)
776 iochan_event_loop(&iochan);
780 static void listener(IOCHAN h, int event)
782 COMSTACK line = (COMSTACK) iochan_getdata(h);
783 IOCHAN parent_chan = line->user;
788 if (event == EVENT_INPUT)
793 if ((res = cs_listen(line, 0, 0)) < 0)
795 yaz_log(YLOG_FATAL|YLOG_ERRNO, "cs_listen failed");
799 return; /* incomplete */
800 yaz_log(YLOG_DEBUG, "listen ok");
801 new_line = cs_accept(line);
804 yaz_log(YLOG_FATAL, "Accept failed.");
807 yaz_log(YLOG_DEBUG, "Accept ok");
809 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
810 EVENT_INPUT, parent_chan->chan_id)))
812 yaz_log(YLOG_FATAL, "Failed to create iochan");
817 yaz_log(YLOG_DEBUG, "Creating association");
818 if (!(newas = create_association(new_chan, new_line,
819 control_block.apdufile)))
821 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
825 newas->cs_get_mask = EVENT_INPUT;
826 newas->cs_put_mask = 0;
827 newas->cs_accept_mask = 0;
829 yaz_log(YLOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
830 iochan_setdata(new_chan, newas);
831 iochan_settimeout(new_chan, 60);
833 /* Now what we need todo is create a new thread with this iochan as
835 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
836 if (newHandle == (HANDLE) -1)
839 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create new thread.");
843 /* We successfully created the thread, so add it to the list */
844 statserv_add(newHandle, new_chan);
846 yaz_log(YLOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
847 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
851 yaz_log(YLOG_FATAL, "Bad event on listener.");
859 /* To save having an #ifdef in event_loop we need to
860 define this empty function
862 void statserv_remove(IOCHAN pIOChannel)
866 static void statserv_closedown(void)
870 xml_config_bend_stop();
871 for (p = pListener; p; p = p->next)
878 static void *new_session(void *vp);
879 static int no_sessions = 0;
882 static void listener(IOCHAN h, int event)
884 COMSTACK line = (COMSTACK) iochan_getdata(h);
887 if (event == EVENT_INPUT)
890 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
891 control_block.daemon_name)) < 0)
893 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
898 yaz_log(YLOG_WARN, "cs_listen incomplete");
901 new_line = cs_accept(line);
904 yaz_log(YLOG_FATAL, "Accept failed.");
905 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
909 yaz_log(log_sessiondetail, "Connect from %s", cs_addrstr(new_line));
912 if (control_block.dynamic)
914 if ((res = fork()) < 0)
916 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
920 else if (res == 0) /* child */
925 for (pp = pListener; pp; pp = iochan_getnext(pp))
927 COMSTACK l = (COMSTACK)iochan_getdata(pp);
931 sprintf(nbuf, "%s(%d)", me, no_sessions);
932 yaz_log_init_prefix(nbuf);
933 /* ensure that bend_stop is not called when each child exits -
934 only for the main process .. */
935 control_block.bend_stop = 0;
944 if (control_block.threads)
946 #if YAZ_POSIX_THREADS
947 pthread_t child_thread;
948 pthread_create(&child_thread, 0, new_session, new_line);
949 pthread_detach(child_thread);
950 #elif YAZ_GNU_THREADS
954 attr = pth_attr_new();
955 pth_attr_set(attr, PTH_ATTR_JOINABLE, FALSE);
956 pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 32*1024);
957 pth_attr_set(attr, PTH_ATTR_NAME, "session");
958 yaz_log(YLOG_DEBUG, "pth_spawn begin");
959 child_thread = pth_spawn(attr, new_session, new_line);
960 yaz_log(YLOG_DEBUG, "pth_spawn finish");
961 pth_attr_destroy(attr);
963 new_session(new_line);
967 new_session(new_line);
969 else if (event == EVENT_TIMEOUT)
971 yaz_log(log_server, "Shutting down listener.");
976 yaz_log(YLOG_FATAL, "Bad event on listener.");
981 static void *new_session(void *vp)
986 COMSTACK new_line = (COMSTACK) vp;
987 IOCHAN parent_chan = (IOCHAN) new_line->user;
989 unsigned cs_get_mask, cs_accept_mask, mask =
990 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
991 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
995 cs_accept_mask = mask; /* accept didn't complete */
1000 cs_accept_mask = 0; /* accept completed. */
1001 cs_get_mask = mask = EVENT_INPUT;
1004 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask,
1005 parent_chan->chan_id)))
1007 yaz_log(YLOG_FATAL, "Failed to create iochan");
1010 if (!(newas = create_association(new_chan, new_line,
1011 control_block.apdufile)))
1013 yaz_log(YLOG_FATAL, "Failed to create new assoc.");
1016 newas->cs_accept_mask = cs_accept_mask;
1017 newas->cs_get_mask = cs_get_mask;
1019 iochan_setdata(new_chan, newas);
1020 iochan_settimeout(new_chan, 60);
1022 a = cs_addrstr(new_line);
1026 yaz_log_xml_errors(0, YLOG_WARN);
1027 yaz_log(log_session, "Session - OK %d %s %ld",
1028 no_sessions, a ? a : "[Unknown]", (long) getpid());
1029 if (max_sessions && no_sessions >= max_sessions)
1030 control_block.one_shot = 1;
1031 if (control_block.threads)
1033 iochan_event_loop(&new_chan);
1037 new_chan->next = pListener;
1038 pListener = new_chan;
1046 static void inetd_connection(int what)
1053 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
1055 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT,
1058 if ((assoc = create_association(chan, line,
1059 control_block.apdufile)))
1061 iochan_setdata(chan, assoc);
1062 iochan_settimeout(chan, 60);
1063 addr = cs_addrstr(line);
1064 yaz_log(log_sessiondetail, "Inetd association from %s",
1065 addr ? addr : "[UNKNOWN]");
1066 assoc->cs_get_mask = EVENT_INPUT;
1070 yaz_log(YLOG_FATAL, "Failed to create association structure");
1072 chan->next = pListener;
1077 yaz_log(YLOG_FATAL, "Failed to create iochan");
1082 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Failed to create comstack on socket 0");
1087 * Set up a listening endpoint, and give it to the event-handler.
1089 static int add_listener(char *where, int listen_id)
1096 if (control_block.dynamic)
1098 else if (control_block.threads)
1103 yaz_log(log_server, "Adding %s listener on %s id=%d", mode, where,
1106 l = cs_create_host(where, 2, &ap);
1109 yaz_log(YLOG_FATAL, "Failed to listen on %s", where);
1112 if (*control_block.cert_fname)
1113 cs_set_ssl_certificate_file(l, control_block.cert_fname);
1115 if (cs_bind(l, ap, CS_SERVER) < 0)
1117 if (cs_errno(l) == CSYSERR)
1118 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
1120 yaz_log(YLOG_FATAL, "Failed to bind to %s: %s", where,
1125 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
1126 EVENT_EXCEPT, listen_id)))
1128 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create IOCHAN-type");
1132 iochan_setdata(lst, l); /* user-defined data for listener is COMSTACK */
1133 l->user = lst; /* user-defined data for COMSTACK is listener chan */
1135 /* Add listener to chain */
1136 lst->next = pListener;
1142 /* UNIX only (for windows we don't need to catch the signals) */
1143 static void catchchld(int num)
1145 while (waitpid(-1, 0, WNOHANG) > 0)
1147 signal(SIGCHLD, catchchld);
1151 statserv_options_block *statserv_getcontrol(void)
1154 if (init_control_tls)
1155 return (statserv_options_block *) TlsGetValue(current_control_tls);
1157 return &control_block;
1158 #elif YAZ_POSIX_THREADS
1159 if (init_control_tls)
1160 return (statserv_options_block *)
1161 pthread_getspecific(current_control_tls);
1163 return &control_block;
1165 if (current_control_block)
1166 return current_control_block;
1167 return &control_block;
1171 void statserv_setcontrol(statserv_options_block *block)
1173 if (gfs_root_dir[0])
1175 if (chdir(gfs_root_dir))
1176 yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s", gfs_root_dir);
1179 if (init_control_tls)
1180 TlsSetValue(current_control_tls, block);
1181 #elif YAZ_POSIX_THREADS
1182 if (init_control_tls)
1183 pthread_setspecific(current_control_tls, block);
1185 current_control_block = block;
1189 static void statserv_reset(void)
1193 static void daemon_handler(void *data)
1195 IOCHAN *pListener = data;
1196 iochan_event_loop(pListener);
1199 static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
1203 /* We need to initialize the thread list */
1204 ThreadList_Initialize();
1214 if ((me = strrchr(argv[0], sep)))
1215 me++; /* get the basename */
1218 programname = argv[0];
1220 if (control_block.options_func(argc, argv))
1225 xml_config_bend_start();
1227 if (control_block.inetd)
1230 ; /* no inetd on Windows */
1232 inetd_connection(control_block.default_proto);
1237 xml_config_add_listeners();
1239 if (!pListener && *control_block.default_listen)
1240 add_listener(control_block.default_listen, 0);
1243 if (control_block.dynamic)
1244 signal(SIGCHLD, catchchld);
1247 if (pListener == NULL)
1251 yaz_log(YLOG_DEBUG, "Entering event loop.");
1253 yaz_daemon(programname,
1254 (control_block.background ? YAZ_DAEMON_FORK : 0),
1255 daemon_handler, &pListener,
1256 *control_block.pid_fname ? control_block.pid_fname : 0,
1257 *control_block.setuid ? control_block.setuid : 0);
1261 static void option_copy(char *dst, const char *src)
1263 strncpy(dst, src ? src : "", 127);
1267 int check_options(int argc, char **argv)
1272 yaz_log_init_level(yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL));
1276 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:m:r:",
1277 argv, argc, &arg)) != -2)
1282 if (add_listener(arg, 0))
1283 return 1; /* failed to create listener */
1286 control_block.one_shot = 1;
1287 control_block.dynamic = 0;
1290 control_block.default_proto = PROTO_Z3950;
1293 fprintf(stderr, "%s: SR protocol no longer supported\n", me);
1297 control_block.dynamic = 0;
1300 #if YAZ_POSIX_THREADS
1301 control_block.dynamic = 0;
1302 control_block.threads = 1;
1303 #elif YAZ_GNU_THREADS
1304 control_block.dynamic = 0;
1305 control_block.threads = 1;
1307 fprintf(stderr, "%s: Threaded mode not available.\n", me);
1312 option_copy(control_block.logfile, arg);
1313 yaz_log_init_file(control_block.logfile);
1317 fprintf(stderr, "%s: Specify time format for log file.\n", me);
1320 yaz_log_time_format(arg);
1323 yaz_log_init_level(yaz_log_mask_str(arg));
1327 option_copy(control_block.apdufile, arg);
1330 option_copy(control_block.setuid, arg);
1333 option_copy(control_block.configname, arg);
1336 option_copy(control_block.cert_fname, arg);
1339 option_copy(control_block.daemon_name, arg);
1342 if (!arg || !(r = atoi(arg)))
1344 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1347 control_block.idle_timeout = strchr(arg, 's') ? r : 60 * r;
1350 if (!arg || !(r = atoi(arg)))
1352 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1355 control_block.maxrecordsize = r * 1024;
1358 control_block.inetd = 1;
1368 max_sessions = atoi(arg);
1371 option_copy(control_block.pid_fname, arg);
1375 option_copy(control_block.xml_config, arg);
1377 fprintf(stderr, "%s: Option -f unsupported since YAZ is compiled without Libxml2 support\n", me);
1382 control_block.background = 1;
1385 if (!arg || !(r = atoi(arg)))
1387 fprintf(stderr, "%s: Specify positive size for -r.\n", me);
1390 yaz_log_init_max_size(r * 1024);
1393 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1394 " -l <logfile> -u <user> -c <config> -t <minutes>"
1395 " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
1396 " -ziDST1 -m <time-format> -w <directory> <listener-addr>... ]\n", me);
1403 void statserv_sc_stop(yaz_sc_t s)
1405 statserv_closedown();
1409 int statserv_main(int argc, char **argv,
1410 bend_initresult *(*bend_init)(bend_initrequest *r),
1411 void (*bend_close)(void *handle))
1414 struct statserv_options_block *cb = &control_block;
1416 /* control block does not have service_name member on Unix */
1417 yaz_sc_t s = yaz_sc_create(
1419 cb->service_name, cb->service_display_name
1425 cb->bend_init = bend_init;
1426 cb->bend_close = bend_close;
1428 ret = yaz_sc_program(s, argc, argv, statserv_sc_main, statserv_sc_stop);
1436 * c-file-style: "Stroustrup"
1437 * indent-tabs-mode: nil
1439 * vim: shiftwidth=4 tabstop=8 expandtab