2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * NT server based on threads by
7 * Chas Woodfield, Fretwell Downing Informatics.
10 * Revision 1.74 2001-10-03 23:55:18 adam
11 * GNU threads support.
13 * Revision 1.73 2001/06/28 09:27:06 adam
14 * Number of started sessions logged.
16 * Revision 1.72 2001/03/25 21:55:13 adam
17 * Added odr_intdup. Ztest server returns TaskPackage for ItemUpdate.
19 * Revision 1.71 2001/03/21 12:43:36 adam
20 * Implemented cs_create_host. Better error reporting for SSL comstack.
22 * Revision 1.70 2001/02/01 08:52:26 adam
23 * Fixed bug regarding inetd mode.
25 * Revision 1.69 2000/12/01 17:56:41 adam
26 * on WIN32 function statserv_closedown closes socket(s) to provoke close.
28 * Revision 1.68 2000/11/29 14:22:47 adam
29 * Implemented XML/SGML attributes for data1 so that d1_read reads them
30 * and d1_write generates proper attributes for XML/SGML records. Added
31 * register locking for threaded version.
33 * Revision 1.67 2000/11/23 10:58:32 adam
34 * SSL comstack support. Separate POSIX thread support library.
36 * Revision 1.66 2000/10/06 12:00:28 adam
37 * Fixed Handle leak for WIN32.
39 * Revision 1.65 2000/09/04 08:58:15 adam
40 * Added prefix yaz_ for most logging utility functions.
42 * Revision 1.64 2000/04/05 07:39:55 adam
43 * Added shared library support (libtool).
45 * Revision 1.63 2000/03/20 19:06:25 adam
46 * Added Segment request for fronend server. Work on admin for client.
48 * Revision 1.62 2000/03/17 12:47:02 adam
49 * Minor changes to admin client.
51 * Revision 1.61 2000/03/15 12:59:49 adam
52 * Added handle member to statserv_control.
54 * Revision 1.60 2000/03/14 09:06:11 adam
55 * Added POSIX threads support for frontend server.
57 * Revision 1.59 1999/11/30 13:47:12 adam
58 * Improved installation. Moved header files to include/yaz.
60 * Revision 1.58 1999/08/27 09:40:32 adam
61 * Renamed logf function to yaz_log. Removed VC++ project files.
63 * Revision 1.57 1999/07/06 12:17:15 adam
64 * Added option -1 that runs server once (for profiling purposes).
66 * Revision 1.56 1999/06/10 11:45:30 adam
67 * Added bend_start, bend_stop handlers and removed pre_init.
68 * Handlers bend_start/bend_stop are called when service/daemon is
71 * Revision 1.55 1999/06/10 09:18:54 adam
72 * Modified so that pre_init is called when service/server is started.
74 * Revision 1.54 1999/04/16 14:45:55 adam
75 * Added interface for tcpd wrapper for access control.
77 * Revision 1.53 1999/02/02 13:57:39 adam
78 * Uses preprocessor define WIN32 instead of WINDOWS to build code
79 * for Microsoft WIN32.
81 * Revision 1.52 1998/08/21 14:13:34 adam
82 * Added GNU Configure script to build Makefiles.
84 * Revision 1.51 1998/07/07 15:51:03 adam
85 * Changed server so that it stops if bind fails - "address already in
86 * use" typically causes this.
88 * Revision 1.50 1998/06/22 11:32:39 adam
89 * Added 'conditional cs_listen' feature.
91 * Revision 1.49 1998/02/27 14:04:55 adam
92 * Fixed bug in statserv_remove.
94 * Revision 1.48 1998/02/11 11:53:36 adam
95 * Changed code so that it compiles as C++.
97 * Revision 1.47 1998/02/10 10:28:57 adam
98 * Added app_name, service_dependencies, service_display_name and
99 * options_func. options_func allows us to specify a different function
100 * to interogate the command line arguments. The other members allow us
101 * to pass the full service details accross to the service manager (CW).
104 * Revision 1.46 1998/01/30 15:24:57 adam
105 * Fixed bug in inetd code. The server listened on tcp:@:9999 even
106 * though it was started in inetd mode.
108 * Revision 1.45 1998/01/29 13:30:23 adam
109 * Better event handle system for NT/Unix.
111 * Revision 1.44 1997/11/07 13:31:52 adam
112 * Added NT Service name part of statserv_options_block. Moved NT
113 * service utility to server library.
115 * Revision 1.43 1997/10/31 12:20:09 adam
116 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
117 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
118 * Bug fix: missing fclose in data1_read_espec1.
120 * Revision 1.42 1997/10/27 14:03:02 adam
121 * Added new member to statserver_options_block, pre_init, which
122 * specifies a callback to be invoked after command line parsing and
123 * before the server listens for the first time.
125 * Revision 1.41 1997/09/29 07:19:32 adam
126 * Server library uses nmem_init/nmem_exit. The log prefix no longer
127 * includes leading path on NT.
129 * Revision 1.40 1997/09/17 12:10:41 adam
132 * Revision 1.39 1997/09/09 10:10:19 adam
133 * Another MSV5.0 port. Changed projects to include proper
134 * library/include paths.
135 * Server starts server in test-mode when no options are given.
137 * Revision 1.38 1997/09/04 14:19:14 adam
140 * Revision 1.37 1997/09/01 08:53:01 adam
141 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
142 * moved a separate directory. MSV5.0 project server.dsp created.
143 * As an option, the server can now operate as an NT service.
145 * Revision 1.36 1996/07/06 19:58:36 quinn
146 * System headerfiles gathered in yconfig
148 * Revision 1.35 1996/05/29 10:03:28 quinn
151 * Revision 1.34 1996/02/21 13:12:07 quinn
152 * *** empty log message ***
154 * Revision 1.33 1996/02/10 12:23:49 quinn
155 * Enable inetd operations fro TCP/IP stack
157 * Revision 1.32 1996/01/19 15:41:52 quinn
158 * *** empty log message ***
160 * Revision 1.31 1995/11/17 11:09:39 adam
161 * Added new option '-c' to specify configuration name in control block.
163 * Revision 1.30 1995/11/01 13:54:59 quinn
166 * Revision 1.29 1995/10/30 12:41:29 quinn
167 * Added hostname lookup for server.
169 * Revision 1.28 1995/09/29 17:12:30 quinn
172 * Revision 1.27 1995/09/27 15:03:02 quinn
173 * Modified function heads & prototypes.
175 * Revision 1.26 1995/08/29 14:44:51 quinn
178 * Revision 1.25 1995/08/29 11:18:02 quinn
179 * Added code to receive close
181 * Revision 1.24 1995/06/16 10:31:39 quinn
182 * Added session timeout.
184 * Revision 1.23 1995/06/15 12:30:48 quinn
187 * Revision 1.22 1995/06/15 07:45:17 quinn
190 * Revision 1.21 1995/06/06 08:15:40 quinn
193 * Revision 1.20 1995/05/29 08:12:09 quinn
196 * Revision 1.19 1995/05/16 09:37:27 quinn
199 * Revision 1.18 1995/05/16 08:51:09 quinn
200 * License, documentation, and memory fixes
202 * Revision 1.17 1995/05/15 11:56:42 quinn
203 * Asynchronous facilities. Restructuring of seshigh code.
205 * Revision 1.16 1995/04/10 10:23:40 quinn
206 * Some work to add scan and other things.
208 * Revision 1.15 1995/03/31 10:16:51 quinn
211 * Revision 1.14 1995/03/31 09:18:58 quinn
214 * Revision 1.13 1995/03/30 16:08:39 quinn
217 * Revision 1.12 1995/03/30 13:29:02 quinn
220 * Revision 1.11 1995/03/30 12:18:17 quinn
223 * Revision 1.10 1995/03/29 15:40:16 quinn
224 * Ongoing work. Statserv is now dynamic by default
226 * Revision 1.9 1995/03/27 08:34:30 quinn
227 * Added dynamic server functionality.
228 * Released bindings to session.c (is now redundant)
230 * Revision 1.8 1995/03/20 09:46:26 quinn
233 * Revision 1.7 1995/03/16 13:29:04 quinn
234 * Partitioned server.
236 * Revision 1.6 1995/03/15 15:18:52 quinn
237 * Little changes to better support nonblocking I/O
240 * Revision 1.5 1995/03/15 08:37:45 quinn
241 * Now we're pretty much set for nonblocking I/O.
243 * Revision 1.4 1995/03/14 16:59:48 quinn
246 * Revision 1.3 1995/03/14 11:30:15 quinn
249 * Revision 1.2 1995/03/14 10:28:03 quinn
250 * More work on demo server.
252 * Revision 1.1 1995/03/10 18:22:45 quinn
253 * The rudiments of an asynchronous server.
281 #include <yaz/comstack.h>
282 #include <yaz/tcpip.h>
283 #include <yaz/options.h>
285 #include <yaz/xmosi.h>
290 #include <yaz/statserv.h>
292 static IOCHAN pListener = NULL;
294 static char *me = "statserver";
298 int check_options(int argc, char **argv);
299 statserv_options_block control_block = {
300 1, /* dynamic mode */
301 0, /* threaded mode */
302 0, /* one shot (single session) */
303 LOG_DEFAULT_LEVEL, /* log level */
305 "", /* diagnostic output to stderr */
306 "tcp:@:9999", /* default listener port */
307 PROTO_Z3950, /* default application protocol */
308 60, /* idle timeout (minutes) */
309 1024*1024, /* maximum PDU size (approx.) to allow */
310 "default-config", /* configuration name to pass to backend */
311 "", /* set user id */
312 0, /* bend_start handler */
313 0, /* bend_stop handler */
314 check_options, /* Default routine, for checking the run-time arguments */
317 0, /* default value for inet deamon */
318 0, /* handle (for service, etc) */
319 0, /* bend_init handle */
320 0 /* bend_close handle */
322 ,"Z39.50 Server", /* NT Service Name */
323 "Server", /* NT application Name */
324 "", /* NT Service Dependencies */
325 "Z39.50 Server" /* NT Service Display Name */
330 * handle incoming connect requests.
331 * The dynamic mode is a bit tricky mostly because we want to avoid
332 * doing all of the listening and accepting in the parent - it's
337 typedef struct _ThreadList ThreadList;
346 static ThreadList *pFirstThread;
347 static CRITICAL_SECTION Thread_CritSect;
348 static BOOL bInitialized = FALSE;
350 static void ThreadList_Initialize()
352 /* Initialize the critical Sections */
353 InitializeCriticalSection(&Thread_CritSect);
355 /* Set the first thraed */
358 /* we have been initialized */
362 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
364 /* Only one thread can go through this section at a time */
365 EnterCriticalSection(&Thread_CritSect);
368 /* Lets create our new object */
369 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
370 pNewThread->hThread = hThread;
371 pNewThread->pIOChannel = pIOChannel;
372 pNewThread->pNext = pFirstThread;
373 pFirstThread = pNewThread;
375 /* Lets let somebody else create a new object now */
376 LeaveCriticalSection(&Thread_CritSect);
380 void statserv_remove(IOCHAN pIOChannel)
382 /* Only one thread can go through this section at a time */
383 EnterCriticalSection(&Thread_CritSect);
386 ThreadList *pCurrentThread = pFirstThread;
387 ThreadList *pNextThread;
388 ThreadList *pPrevThread =NULL;
390 /* Step through alll the threads */
391 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
393 /* We only need to compare on the IO Channel */
394 if (pCurrentThread->pIOChannel == pIOChannel)
396 /* We have found the thread we want to delete */
397 /* First of all reset the next pointers */
398 if (pPrevThread == NULL)
399 pFirstThread = pCurrentThread->pNext;
401 pPrevThread->pNext = pCurrentThread->pNext;
403 /* All we need todo now is delete the memory */
404 free(pCurrentThread);
406 /* No need to look at any more threads */
411 /* We need to look at another thread */
412 pNextThread = pCurrentThread->pNext;
413 pPrevThread = pCurrentThread;
417 /* Lets let somebody else remove an object now */
418 LeaveCriticalSection(&Thread_CritSect);
422 /* WIN32 statserv_closedown */
423 void statserv_closedown()
425 /* Shouldn't do anything if we are not initialized */
429 HANDLE *pThreadHandles = NULL;
431 /* We need to stop threads adding and removing while we */
432 /* start the closedown process */
433 EnterCriticalSection(&Thread_CritSect);
436 /* We have exclusive access to the thread stuff now */
437 /* Y didn't i use a semaphore - Oh well never mind */
438 ThreadList *pCurrentThread = pFirstThread;
440 /* Before we do anything else, we need to shutdown the listener */
441 if (pListener != NULL)
442 iochan_destroy(pListener);
444 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
446 /* Just destroy the IOCHAN, that should do the trick */
447 iochan_destroy(pCurrentThread->pIOChannel);
448 closesocket(pCurrentThread->pIOChannel->fd);
450 /* Keep a running count of our handles */
456 HANDLE *pCurrentHandle ;
458 /* Allocate the thread handle array */
459 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
460 pCurrentHandle = pThreadHandles;
462 for (pCurrentThread = pFirstThread;
463 pCurrentThread != NULL;
464 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
466 /* Just the handle */
467 *pCurrentHandle = pCurrentThread->hThread;
471 /* We can now leave the critical section */
472 LeaveCriticalSection(&Thread_CritSect);
475 /* Now we can really do something */
478 logf (LOG_LOG, "waiting for %d to die", iHandles);
479 /* This will now wait, until all the threads close */
480 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
482 /* Free the memory we allocated for the handle array */
483 free(pThreadHandles);
486 if (control_block.bend_stop)
487 (*control_block.bend_stop)(&control_block);
488 /* No longer require the critical section, since all threads are dead */
489 DeleteCriticalSection(&Thread_CritSect);
493 void event_loop_thread (IOCHAN iochan)
495 event_loop (&iochan);
499 static void listener(IOCHAN h, int event)
501 COMSTACK line = (COMSTACK) iochan_getdata(h);
506 if (event == EVENT_INPUT)
508 if ((res = cs_listen(line, 0, 0)) < 0)
510 yaz_log(LOG_FATAL, "cs_listen failed");
515 yaz_log(LOG_DEBUG, "listen ok");
516 iochan_setevent(h, EVENT_OUTPUT);
517 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
519 else if (event == EVENT_OUTPUT)
521 COMSTACK new_line = cs_accept(line);
527 yaz_log(LOG_FATAL, "Accept failed.");
528 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
531 yaz_log(LOG_DEBUG, "Accept ok");
533 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
536 yaz_log(LOG_FATAL, "Failed to create iochan");
541 yaz_log(LOG_DEBUG, "Creating association");
542 if (!(newas = create_association(new_chan, new_line)))
544 yaz_log(LOG_FATAL, "Failed to create new assoc.");
548 newas->cs_get_mask = EVENT_INPUT;
549 newas->cs_put_mask = 0;
550 newas->cs_accept_mask = 0;
552 yaz_log(LOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
553 iochan_setdata(new_chan, newas);
554 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
556 /* Now what we need todo is create a new thread with this iochan as
558 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
559 if (newHandle == (HANDLE) -1)
562 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create new thread.");
566 /* We successfully created the thread, so add it to the list */
567 statserv_add(newHandle, new_chan);
569 yaz_log(LOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
570 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
574 yaz_log(LOG_FATAL, "Bad event on listener.");
582 /* To save having an #ifdef in event_loop we need to define this empty function */
583 void statserv_remove(IOCHAN pIOChannel)
587 void statserv_closedown()
591 if (control_block.bend_stop)
592 (*control_block.bend_stop)(&control_block);
594 for (p = pListener; p; p = p->next)
598 void sigterm(int sig)
600 statserv_closedown();
604 static void *new_session (void *vp);
605 static int no_sessions = 0;
608 static void listener(IOCHAN h, int event)
610 COMSTACK line = (COMSTACK) iochan_getdata(h);
612 static int child = 0;
615 if (event == EVENT_INPUT)
617 if (control_block.dynamic && !child)
624 yaz_log(LOG_FATAL|LOG_ERRNO, "pipe");
628 if ((res = fork()) < 0)
630 yaz_log(LOG_FATAL|LOG_ERRNO, "fork");
634 else if (res == 0) /* child */
641 for (pp = pListener; pp; pp = iochan_getnext(pp))
645 COMSTACK l = (COMSTACK)iochan_getdata(pp);
650 sprintf(nbuf, "%s(%d)", me, getpid());
651 yaz_log_init(control_block.loglevel, nbuf, 0);
656 /* wait for child to take the call */
662 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
664 yaz_log(LOG_FATAL|LOG_ERRNO, "handshake read");
670 yaz_log(LOG_DEBUG, "P: Child has taken the call");
675 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
676 control_block.daemon_name)) < 0)
678 yaz_log(LOG_WARN, "cs_listen failed");
683 yaz_log(LOG_DEBUG, "listen ok");
684 iochan_setevent(h, EVENT_OUTPUT);
685 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
687 /* in dynamic mode, only the child ever comes down here */
688 else if (event == EVENT_OUTPUT)
690 COMSTACK new_line = cs_accept(line);
694 yaz_log(LOG_FATAL, "Accept failed.");
695 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
698 yaz_log(LOG_DEBUG, "accept ok");
699 if (control_block.dynamic)
702 /* close our half of the listener socket */
703 for (pp = pListener; pp; pp = iochan_getnext(pp))
705 COMSTACK l = (COMSTACK)iochan_getdata(pp);
710 yaz_log(LOG_DEBUG, "Releasing parent");
715 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
720 if (control_block.threads)
722 pthread_t child_thread;
723 pthread_create (&child_thread, 0, new_session, new_line);
724 pthread_detach (child_thread);
727 new_session(new_line);
729 if (control_block.threads)
734 pth_attr_init (attr);
735 pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
736 child_thread = pth_spawn (attr, new_session, new_line);
737 pth_attr_destroy (attr);
740 new_session(new_line);
744 new_session(new_line);
749 yaz_log(LOG_FATAL, "Bad event on listener.");
755 static void *new_session (void *vp)
760 COMSTACK new_line = (COMSTACK) vp;
762 unsigned cs_get_mask, cs_accept_mask, mask =
763 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
764 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
768 cs_accept_mask = mask; /* accept didn't complete */
773 cs_accept_mask = 0; /* accept completed. */
774 cs_get_mask = mask = EVENT_INPUT;
777 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask)))
779 yaz_log(LOG_FATAL, "Failed to create iochan");
782 if (!(newas = create_association(new_chan, new_line)))
784 yaz_log(LOG_FATAL, "Failed to create new assoc.");
787 newas->cs_accept_mask = cs_accept_mask;
788 newas->cs_get_mask = cs_get_mask;
790 iochan_setdata(new_chan, newas);
791 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
792 a = cs_addrstr(new_line);
793 yaz_log(LOG_LOG, "Starting session %d from %s", no_sessions, a ? a : "[Unknown]");
795 if (control_block.threads)
797 event_loop(&new_chan);
801 new_chan->next = pListener;
802 pListener = new_chan;
809 static void inetd_connection(int what)
816 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
818 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
820 if ((assoc = create_association(chan, line)))
822 iochan_setdata(chan, assoc);
823 iochan_settimeout(chan, control_block.idle_timeout * 60);
824 addr = cs_addrstr(line);
825 yaz_log(LOG_LOG, "Inetd association from %s", addr ? addr : "[UNKNOWN]");
826 assoc->cs_get_mask = EVENT_INPUT;
830 yaz_log(LOG_FATAL, "Failed to create association structure");
832 chan->next = pListener;
837 yaz_log(LOG_FATAL, "Failed to create iochan");
842 yaz_log(LOG_ERRNO|LOG_FATAL, "Failed to create comstack on socket 0");
847 * Set up a listening endpoint, and give it to the event-handler.
849 static void add_listener(char *where, int what)
856 if (control_block.dynamic)
858 else if (control_block.threads)
863 yaz_log(LOG_LOG, "Adding %s %s listener on %s", mode,
864 what == PROTO_SR ? "SR" : "Z3950", where);
866 l = cs_create_host(where, 0, &ap);
869 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to listen on %s", where);
872 if (cs_bind(l, ap, CS_SERVER) < 0)
874 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
878 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
881 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
885 iochan_setdata(lst, l);
887 /* Ensure our listener chain is setup properly */
888 lst->next = pListener;
893 /* For windows we don't need to catch the signals */
894 static void catchchld(int num)
896 while (waitpid(-1, 0, WNOHANG) > 0)
898 signal(SIGCHLD, catchchld);
902 statserv_options_block *statserv_getcontrol(void)
904 static statserv_options_block cb;
906 memcpy(&cb, &control_block, sizeof(cb));
910 void statserv_setcontrol(statserv_options_block *block)
912 memcpy(&control_block, block, sizeof(*block));
915 int statserv_start(int argc, char **argv)
921 /* We need to initialize the thread list */
922 ThreadList_Initialize();
926 if ((me = strrchr (argv[0], '\\')))
933 if (control_block.options_func(argc, argv))
936 if (control_block.bend_start)
937 (*control_block.bend_start)(&control_block);
939 logf (LOG_LOG, "Starting server %s", me);
941 if (control_block.inetd)
942 inetd_connection(control_block.default_proto);
945 logf (LOG_LOG, "Starting server %s pid=%d", me, getpid());
947 sigset_t sigs_to_block;
949 sigemptyset(&sigs_to_block);
950 sigaddset (&sigs_to_block, SIGTERM);
951 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
954 if (control_block.dynamic)
955 signal(SIGCHLD, catchchld);
957 signal (SIGPIPE, SIG_IGN);
958 signal (SIGTERM, sigterm);
959 if (*control_block.setuid)
963 if (!(pw = getpwnam(control_block.setuid)))
965 yaz_log(LOG_FATAL, "%s: Unknown user", control_block.setuid);
968 if (setuid(pw->pw_uid) < 0)
970 yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
976 if ((pListener == NULL) && *control_block.default_listen)
977 add_listener(control_block.default_listen,
978 control_block.default_proto);
980 if (pListener == NULL)
984 yaz_log(LOG_LOG, "Entering event loop.");
985 ret = event_loop(&pListener);
991 int check_options(int argc, char **argv)
996 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:", argv, argc, &arg)) != -2)
1001 add_listener(arg, control_block.default_proto);
1004 control_block.one_shot = 1;
1005 control_block.dynamic = 0;
1008 control_block.default_proto = PROTO_Z3950;
1011 control_block.default_proto = PROTO_SR;
1014 control_block.dynamic = 0;
1018 control_block.dynamic = 0;
1019 control_block.threads = 1;
1021 fprintf(stderr, "%s: Threaded mode not available.\n", me);
1026 strcpy(control_block.logfile, arg ? arg : "");
1027 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1030 control_block.loglevel = yaz_log_mask_str(arg);
1031 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1034 strcpy(control_block.apdufile, arg ? arg : "");
1037 strcpy(control_block.setuid, arg ? arg : "");
1040 strcpy(control_block.configname, arg ? arg : "");
1043 strcpy(control_block.daemon_name, arg ? arg : "");
1046 if (!arg || !(r = atoi(arg)))
1048 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1051 control_block.idle_timeout = r;
1054 if (!arg || !(r = atoi(arg)))
1056 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1059 control_block.maxrecordsize = r * 1024;
1062 control_block.inetd = 1;
1072 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1073 " -l <logfile> -u <user> -c <config> -t <minutes>"
1074 " -k <kilobytes> -d <daemon>"
1075 " -zsiST -w <directory> <listender-addr>... ]\n", me);
1083 typedef struct _Args
1089 static Args ArgDetails;
1091 /* name of the executable */
1092 #define SZAPPNAME "server"
1094 /* list of service dependencies - "dep1\0dep2\0\0" */
1095 #define SZDEPENDENCIES ""
1097 int statserv_main(int argc, char **argv,
1098 bend_initresult *(*bend_init)(bend_initrequest *r),
1099 void (*bend_close)(void *handle))
1101 statserv_options_block *cb = statserv_getcontrol();
1103 cb->bend_init = bend_init;
1104 cb->bend_close = bend_close;
1106 statserv_setcontrol(cb);
1108 /* Lets setup the Arg structure */
1109 ArgDetails.argc = argc;
1110 ArgDetails.argv = argv;
1112 /* Now setup the service with the service controller */
1113 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
1114 cb->service_name, /* internal service name */
1115 cb->service_name, /* displayed name of the service */
1120 int StartAppService(void *pHandle, int argc, char **argv)
1122 /* Initializes the App */
1126 void RunAppService(void *pHandle)
1128 Args *pArgs = (Args *)pHandle;
1130 /* Starts the app running */
1131 statserv_start(pArgs->argc, pArgs->argv);
1134 void StopAppService(void *pHandle)
1137 statserv_closedown();
1140 int statserv_main(int argc, char **argv,
1141 bend_initresult *(*bend_init)(bend_initrequest *r),
1142 void (*bend_close)(void *handle))
1145 statserv_options_block *cb = statserv_getcontrol();
1147 cb->bend_init = bend_init;
1148 cb->bend_close = bend_close;
1150 statserv_setcontrol(cb);
1151 ret = statserv_start (argc, argv);
1152 statserv_closedown ();