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.71 2001-03-21 12:43:36 adam
11 * Implemented cs_create_host. Better error reporting for SSL comstack.
13 * Revision 1.70 2001/02/01 08:52:26 adam
14 * Fixed bug regarding inetd mode.
16 * Revision 1.69 2000/12/01 17:56:41 adam
17 * on WIN32 function statserv_closedown closes socket(s) to provoke close.
19 * Revision 1.68 2000/11/29 14:22:47 adam
20 * Implemented XML/SGML attributes for data1 so that d1_read reads them
21 * and d1_write generates proper attributes for XML/SGML records. Added
22 * register locking for threaded version.
24 * Revision 1.67 2000/11/23 10:58:32 adam
25 * SSL comstack support. Separate POSIX thread support library.
27 * Revision 1.66 2000/10/06 12:00:28 adam
28 * Fixed Handle leak for WIN32.
30 * Revision 1.65 2000/09/04 08:58:15 adam
31 * Added prefix yaz_ for most logging utility functions.
33 * Revision 1.64 2000/04/05 07:39:55 adam
34 * Added shared library support (libtool).
36 * Revision 1.63 2000/03/20 19:06:25 adam
37 * Added Segment request for fronend server. Work on admin for client.
39 * Revision 1.62 2000/03/17 12:47:02 adam
40 * Minor changes to admin client.
42 * Revision 1.61 2000/03/15 12:59:49 adam
43 * Added handle member to statserv_control.
45 * Revision 1.60 2000/03/14 09:06:11 adam
46 * Added POSIX threads support for frontend server.
48 * Revision 1.59 1999/11/30 13:47:12 adam
49 * Improved installation. Moved header files to include/yaz.
51 * Revision 1.58 1999/08/27 09:40:32 adam
52 * Renamed logf function to yaz_log. Removed VC++ project files.
54 * Revision 1.57 1999/07/06 12:17:15 adam
55 * Added option -1 that runs server once (for profiling purposes).
57 * Revision 1.56 1999/06/10 11:45:30 adam
58 * Added bend_start, bend_stop handlers and removed pre_init.
59 * Handlers bend_start/bend_stop are called when service/daemon is
62 * Revision 1.55 1999/06/10 09:18:54 adam
63 * Modified so that pre_init is called when service/server is started.
65 * Revision 1.54 1999/04/16 14:45:55 adam
66 * Added interface for tcpd wrapper for access control.
68 * Revision 1.53 1999/02/02 13:57:39 adam
69 * Uses preprocessor define WIN32 instead of WINDOWS to build code
70 * for Microsoft WIN32.
72 * Revision 1.52 1998/08/21 14:13:34 adam
73 * Added GNU Configure script to build Makefiles.
75 * Revision 1.51 1998/07/07 15:51:03 adam
76 * Changed server so that it stops if bind fails - "address already in
77 * use" typically causes this.
79 * Revision 1.50 1998/06/22 11:32:39 adam
80 * Added 'conditional cs_listen' feature.
82 * Revision 1.49 1998/02/27 14:04:55 adam
83 * Fixed bug in statserv_remove.
85 * Revision 1.48 1998/02/11 11:53:36 adam
86 * Changed code so that it compiles as C++.
88 * Revision 1.47 1998/02/10 10:28:57 adam
89 * Added app_name, service_dependencies, service_display_name and
90 * options_func. options_func allows us to specify a different function
91 * to interogate the command line arguments. The other members allow us
92 * to pass the full service details accross to the service manager (CW).
95 * Revision 1.46 1998/01/30 15:24:57 adam
96 * Fixed bug in inetd code. The server listened on tcp:@:9999 even
97 * though it was started in inetd mode.
99 * Revision 1.45 1998/01/29 13:30:23 adam
100 * Better event handle system for NT/Unix.
102 * Revision 1.44 1997/11/07 13:31:52 adam
103 * Added NT Service name part of statserv_options_block. Moved NT
104 * service utility to server library.
106 * Revision 1.43 1997/10/31 12:20:09 adam
107 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
108 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
109 * Bug fix: missing fclose in data1_read_espec1.
111 * Revision 1.42 1997/10/27 14:03:02 adam
112 * Added new member to statserver_options_block, pre_init, which
113 * specifies a callback to be invoked after command line parsing and
114 * before the server listens for the first time.
116 * Revision 1.41 1997/09/29 07:19:32 adam
117 * Server library uses nmem_init/nmem_exit. The log prefix no longer
118 * includes leading path on NT.
120 * Revision 1.40 1997/09/17 12:10:41 adam
123 * Revision 1.39 1997/09/09 10:10:19 adam
124 * Another MSV5.0 port. Changed projects to include proper
125 * library/include paths.
126 * Server starts server in test-mode when no options are given.
128 * Revision 1.38 1997/09/04 14:19:14 adam
131 * Revision 1.37 1997/09/01 08:53:01 adam
132 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
133 * moved a separate directory. MSV5.0 project server.dsp created.
134 * As an option, the server can now operate as an NT service.
136 * Revision 1.36 1996/07/06 19:58:36 quinn
137 * System headerfiles gathered in yconfig
139 * Revision 1.35 1996/05/29 10:03:28 quinn
142 * Revision 1.34 1996/02/21 13:12:07 quinn
143 * *** empty log message ***
145 * Revision 1.33 1996/02/10 12:23:49 quinn
146 * Enable inetd operations fro TCP/IP stack
148 * Revision 1.32 1996/01/19 15:41:52 quinn
149 * *** empty log message ***
151 * Revision 1.31 1995/11/17 11:09:39 adam
152 * Added new option '-c' to specify configuration name in control block.
154 * Revision 1.30 1995/11/01 13:54:59 quinn
157 * Revision 1.29 1995/10/30 12:41:29 quinn
158 * Added hostname lookup for server.
160 * Revision 1.28 1995/09/29 17:12:30 quinn
163 * Revision 1.27 1995/09/27 15:03:02 quinn
164 * Modified function heads & prototypes.
166 * Revision 1.26 1995/08/29 14:44:51 quinn
169 * Revision 1.25 1995/08/29 11:18:02 quinn
170 * Added code to receive close
172 * Revision 1.24 1995/06/16 10:31:39 quinn
173 * Added session timeout.
175 * Revision 1.23 1995/06/15 12:30:48 quinn
178 * Revision 1.22 1995/06/15 07:45:17 quinn
181 * Revision 1.21 1995/06/06 08:15:40 quinn
184 * Revision 1.20 1995/05/29 08:12:09 quinn
187 * Revision 1.19 1995/05/16 09:37:27 quinn
190 * Revision 1.18 1995/05/16 08:51:09 quinn
191 * License, documentation, and memory fixes
193 * Revision 1.17 1995/05/15 11:56:42 quinn
194 * Asynchronous facilities. Restructuring of seshigh code.
196 * Revision 1.16 1995/04/10 10:23:40 quinn
197 * Some work to add scan and other things.
199 * Revision 1.15 1995/03/31 10:16:51 quinn
202 * Revision 1.14 1995/03/31 09:18:58 quinn
205 * Revision 1.13 1995/03/30 16:08:39 quinn
208 * Revision 1.12 1995/03/30 13:29:02 quinn
211 * Revision 1.11 1995/03/30 12:18:17 quinn
214 * Revision 1.10 1995/03/29 15:40:16 quinn
215 * Ongoing work. Statserv is now dynamic by default
217 * Revision 1.9 1995/03/27 08:34:30 quinn
218 * Added dynamic server functionality.
219 * Released bindings to session.c (is now redundant)
221 * Revision 1.8 1995/03/20 09:46:26 quinn
224 * Revision 1.7 1995/03/16 13:29:04 quinn
225 * Partitioned server.
227 * Revision 1.6 1995/03/15 15:18:52 quinn
228 * Little changes to better support nonblocking I/O
231 * Revision 1.5 1995/03/15 08:37:45 quinn
232 * Now we're pretty much set for nonblocking I/O.
234 * Revision 1.4 1995/03/14 16:59:48 quinn
237 * Revision 1.3 1995/03/14 11:30:15 quinn
240 * Revision 1.2 1995/03/14 10:28:03 quinn
241 * More work on demo server.
243 * Revision 1.1 1995/03/10 18:22:45 quinn
244 * The rudiments of an asynchronous server.
266 #include <yaz/comstack.h>
267 #include <yaz/tcpip.h>
268 #include <yaz/options.h>
270 #include <yaz/xmosi.h>
275 #include <yaz/statserv.h>
277 static IOCHAN pListener = NULL;
279 static char *me = "statserver";
283 int check_options(int argc, char **argv);
284 statserv_options_block control_block = {
285 1, /* dynamic mode */
286 0, /* threaded mode */
287 0, /* one shot (single session) */
288 LOG_DEFAULT_LEVEL, /* log level */
290 "", /* diagnostic output to stderr */
291 "tcp:@:9999", /* default listener port */
292 PROTO_Z3950, /* default application protocol */
293 60, /* idle timeout (minutes) */
294 1024*1024, /* maximum PDU size (approx.) to allow */
295 "default-config", /* configuration name to pass to backend */
296 "", /* set user id */
297 0, /* bend_start handler */
298 0, /* bend_stop handler */
299 check_options, /* Default routine, for checking the run-time arguments */
302 0, /* default value for inet deamon */
303 0, /* handle (for service, etc) */
304 0, /* bend_init handle */
305 0 /* bend_close handle */
307 ,"Z39.50 Server", /* NT Service Name */
308 "Server", /* NT application Name */
309 "", /* NT Service Dependencies */
310 "Z39.50 Server" /* NT Service Display Name */
315 * handle incoming connect requests.
316 * The dynamic mode is a bit tricky mostly because we want to avoid
317 * doing all of the listening and accepting in the parent - it's
322 typedef struct _ThreadList ThreadList;
331 static ThreadList *pFirstThread;
332 static CRITICAL_SECTION Thread_CritSect;
333 static BOOL bInitialized = FALSE;
335 static void ThreadList_Initialize()
337 /* Initialize the critical Sections */
338 InitializeCriticalSection(&Thread_CritSect);
340 /* Set the first thraed */
343 /* we have been initialized */
347 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
349 /* Only one thread can go through this section at a time */
350 EnterCriticalSection(&Thread_CritSect);
353 /* Lets create our new object */
354 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
355 pNewThread->hThread = hThread;
356 pNewThread->pIOChannel = pIOChannel;
357 pNewThread->pNext = pFirstThread;
358 pFirstThread = pNewThread;
360 /* Lets let somebody else create a new object now */
361 LeaveCriticalSection(&Thread_CritSect);
365 void statserv_remove(IOCHAN pIOChannel)
367 /* Only one thread can go through this section at a time */
368 EnterCriticalSection(&Thread_CritSect);
371 ThreadList *pCurrentThread = pFirstThread;
372 ThreadList *pNextThread;
373 ThreadList *pPrevThread =NULL;
375 /* Step through alll the threads */
376 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
378 /* We only need to compare on the IO Channel */
379 if (pCurrentThread->pIOChannel == pIOChannel)
381 /* We have found the thread we want to delete */
382 /* First of all reset the next pointers */
383 if (pPrevThread == NULL)
384 pFirstThread = pCurrentThread->pNext;
386 pPrevThread->pNext = pCurrentThread->pNext;
388 /* All we need todo now is delete the memory */
389 free(pCurrentThread);
391 /* No need to look at any more threads */
396 /* We need to look at another thread */
397 pNextThread = pCurrentThread->pNext;
398 pPrevThread = pCurrentThread;
402 /* Lets let somebody else remove an object now */
403 LeaveCriticalSection(&Thread_CritSect);
407 /* WIN32 statserv_closedown */
408 void statserv_closedown()
410 /* Shouldn't do anything if we are not initialized */
414 HANDLE *pThreadHandles = NULL;
416 /* We need to stop threads adding and removing while we */
417 /* start the closedown process */
418 EnterCriticalSection(&Thread_CritSect);
421 /* We have exclusive access to the thread stuff now */
422 /* Y didn't i use a semaphore - Oh well never mind */
423 ThreadList *pCurrentThread = pFirstThread;
425 /* Before we do anything else, we need to shutdown the listener */
426 if (pListener != NULL)
427 iochan_destroy(pListener);
429 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
431 /* Just destroy the IOCHAN, that should do the trick */
432 iochan_destroy(pCurrentThread->pIOChannel);
433 closesocket(pCurrentThread->pIOChannel->fd);
435 /* Keep a running count of our handles */
441 HANDLE *pCurrentHandle ;
443 /* Allocate the thread handle array */
444 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
445 pCurrentHandle = pThreadHandles;
447 for (pCurrentThread = pFirstThread;
448 pCurrentThread != NULL;
449 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
451 /* Just the handle */
452 *pCurrentHandle = pCurrentThread->hThread;
456 /* We can now leave the critical section */
457 LeaveCriticalSection(&Thread_CritSect);
460 /* Now we can really do something */
463 logf (LOG_LOG, "waiting for %d to die", iHandles);
464 /* This will now wait, until all the threads close */
465 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
467 /* Free the memory we allocated for the handle array */
468 free(pThreadHandles);
471 if (control_block.bend_stop)
472 (*control_block.bend_stop)(&control_block);
473 /* No longer require the critical section, since all threads are dead */
474 DeleteCriticalSection(&Thread_CritSect);
478 void event_loop_thread (IOCHAN iochan)
480 event_loop (&iochan);
484 static void listener(IOCHAN h, int event)
486 COMSTACK line = (COMSTACK) iochan_getdata(h);
491 if (event == EVENT_INPUT)
493 if ((res = cs_listen(line, 0, 0)) < 0)
495 yaz_log(LOG_FATAL, "cs_listen failed");
500 yaz_log(LOG_DEBUG, "listen ok");
501 iochan_setevent(h, EVENT_OUTPUT);
502 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
504 else if (event == EVENT_OUTPUT)
506 COMSTACK new_line = cs_accept(line);
512 yaz_log(LOG_FATAL, "Accept failed.");
513 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
516 yaz_log(LOG_DEBUG, "Accept ok");
518 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
521 yaz_log(LOG_FATAL, "Failed to create iochan");
526 yaz_log(LOG_DEBUG, "Creating association");
527 if (!(newas = create_association(new_chan, new_line)))
529 yaz_log(LOG_FATAL, "Failed to create new assoc.");
533 newas->cs_get_mask = EVENT_INPUT;
534 newas->cs_put_mask = 0;
535 newas->cs_accept_mask = 0;
537 yaz_log(LOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
538 iochan_setdata(new_chan, newas);
539 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
541 /* Now what we need todo is create a new thread with this iochan as
543 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
544 if (newHandle == (HANDLE) -1)
547 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create new thread.");
551 /* We successfully created the thread, so add it to the list */
552 statserv_add(newHandle, new_chan);
554 yaz_log(LOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
555 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
559 yaz_log(LOG_FATAL, "Bad event on listener.");
567 /* To save having an #ifdef in event_loop we need to define this empty function */
568 void statserv_remove(IOCHAN pIOChannel)
572 void statserv_closedown()
576 if (control_block.bend_stop)
577 (*control_block.bend_stop)(&control_block);
579 for (p = pListener; p; p = p->next)
583 void sigterm(int sig)
585 statserv_closedown();
589 static void *new_session (void *vp);
592 static void listener(IOCHAN h, int event)
594 COMSTACK line = (COMSTACK) iochan_getdata(h);
596 static int child = 0;
599 if (event == EVENT_INPUT)
601 if (control_block.dynamic && !child)
607 yaz_log(LOG_FATAL|LOG_ERRNO, "pipe");
611 if ((res = fork()) < 0)
613 yaz_log(LOG_FATAL|LOG_ERRNO, "fork");
617 else if (res == 0) /* child */
624 for (pp = pListener; pp; pp = iochan_getnext(pp))
628 COMSTACK l = (COMSTACK)iochan_getdata(pp);
633 sprintf(nbuf, "%s(%d)", me, getpid());
634 yaz_log_init(control_block.loglevel, nbuf, 0);
639 /* wait for child to take the call */
645 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
647 yaz_log(LOG_FATAL|LOG_ERRNO, "handshake read");
653 yaz_log(LOG_DEBUG, "P: Child has taken the call");
658 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
659 control_block.daemon_name)) < 0)
661 yaz_log(LOG_WARN, "cs_listen failed");
666 yaz_log(LOG_DEBUG, "listen ok");
667 iochan_setevent(h, EVENT_OUTPUT);
668 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
670 /* in dynamic mode, only the child ever comes down here */
671 else if (event == EVENT_OUTPUT)
673 COMSTACK new_line = cs_accept(line);
677 yaz_log(LOG_FATAL, "Accept failed.");
678 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
681 yaz_log(LOG_DEBUG, "accept ok");
682 if (control_block.dynamic)
685 /* close our half of the listener socket */
686 for (pp = pListener; pp; pp = iochan_getnext(pp))
688 COMSTACK l = (COMSTACK)iochan_getdata(pp);
693 yaz_log(LOG_DEBUG, "Releasing parent");
697 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
700 if (control_block.threads)
702 pthread_t child_thread;
703 pthread_create (&child_thread, 0, new_session, new_line);
704 pthread_detach (child_thread);
707 new_session(new_line);
709 new_session(new_line);
714 yaz_log(LOG_FATAL, "Bad event on listener.");
720 static void *new_session (void *vp)
725 COMSTACK new_line = (COMSTACK) vp;
727 unsigned cs_get_mask, cs_accept_mask, mask =
728 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
729 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
733 cs_accept_mask = mask; /* accept didn't complete */
738 cs_accept_mask = 0; /* accept completed. */
739 cs_get_mask = mask = EVENT_INPUT;
742 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask)))
744 yaz_log(LOG_FATAL, "Failed to create iochan");
747 if (!(newas = create_association(new_chan, new_line)))
749 yaz_log(LOG_FATAL, "Failed to create new assoc.");
752 newas->cs_accept_mask = cs_accept_mask;
753 newas->cs_get_mask = cs_get_mask;
755 iochan_setdata(new_chan, newas);
756 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
757 a = cs_addrstr(new_line);
758 yaz_log(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
760 if (control_block.threads)
762 event_loop(&new_chan);
766 new_chan->next = pListener;
767 pListener = new_chan;
774 static void inetd_connection(int what)
781 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
783 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
785 if ((assoc = create_association(chan, line)))
787 iochan_setdata(chan, assoc);
788 iochan_settimeout(chan, control_block.idle_timeout * 60);
789 addr = cs_addrstr(line);
790 yaz_log(LOG_LOG, "Inetd association from %s", addr ? addr : "[UNKNOWN]");
791 assoc->cs_get_mask = EVENT_INPUT;
795 yaz_log(LOG_FATAL, "Failed to create association structure");
797 chan->next = pListener;
802 yaz_log(LOG_FATAL, "Failed to create iochan");
807 yaz_log(LOG_ERRNO|LOG_FATAL, "Failed to create comstack on socket 0");
812 * Set up a listening endpoint, and give it to the event-handler.
814 static void add_listener(char *where, int what)
820 l = cs_create_host(where, 0, &ap);
823 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to listen on %s", where);
826 if (cs_bind(l, ap, CS_SERVER) < 0)
828 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
832 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
835 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
839 iochan_setdata(lst, l);
841 /* Ensure our listener chain is setup properly */
842 lst->next = pListener;
847 /* For windows we don't need to catch the signals */
848 static void catchchld(int num)
850 while (waitpid(-1, 0, WNOHANG) > 0)
852 signal(SIGCHLD, catchchld);
856 statserv_options_block *statserv_getcontrol(void)
858 static statserv_options_block cb;
860 memcpy(&cb, &control_block, sizeof(cb));
864 void statserv_setcontrol(statserv_options_block *block)
866 memcpy(&control_block, block, sizeof(*block));
869 int statserv_start(int argc, char **argv)
875 /* We need to initialize the thread list */
876 ThreadList_Initialize();
880 if ((me = strrchr (argv[0], '\\')))
887 if (control_block.options_func(argc, argv))
890 if (control_block.bend_start)
891 (*control_block.bend_start)(&control_block);
893 logf (LOG_LOG, "Starting server %s", me);
895 if (control_block.inetd)
896 inetd_connection(control_block.default_proto);
899 logf (LOG_LOG, "Starting server %s pid=%d", me, getpid());
901 sigset_t sigs_to_block;
903 sigemptyset(&sigs_to_block);
904 sigaddset (&sigs_to_block, SIGTERM);
905 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
908 if (control_block.dynamic)
909 signal(SIGCHLD, catchchld);
911 signal (SIGPIPE, SIG_IGN);
912 signal (SIGTERM, sigterm);
913 if (*control_block.setuid)
917 if (!(pw = getpwnam(control_block.setuid)))
919 yaz_log(LOG_FATAL, "%s: Unknown user", control_block.setuid);
922 if (setuid(pw->pw_uid) < 0)
924 yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
930 if ((pListener == NULL) && *control_block.default_listen)
931 add_listener(control_block.default_listen,
932 control_block.default_proto);
934 if (pListener == NULL)
938 yaz_log(LOG_LOG, "Entering event loop.");
939 ret = event_loop(&pListener);
945 int check_options(int argc, char **argv)
950 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:", argv, argc, &arg)) != -2)
955 add_listener(arg, control_block.default_proto);
958 control_block.one_shot = 1;
959 control_block.dynamic = 0;
962 control_block.default_proto = PROTO_Z3950;
965 control_block.default_proto = PROTO_SR;
968 control_block.dynamic = 0;
972 control_block.dynamic = 0;
973 control_block.threads = 1;
975 fprintf(stderr, "%s: Threaded mode not available.\n", me);
980 strcpy(control_block.logfile, arg ? arg : "");
981 yaz_log_init(control_block.loglevel, me, control_block.logfile);
984 control_block.loglevel = yaz_log_mask_str(arg);
985 yaz_log_init(control_block.loglevel, me, control_block.logfile);
988 strcpy(control_block.apdufile, arg ? arg : "");
991 strcpy(control_block.setuid, arg ? arg : "");
994 strcpy(control_block.configname, arg ? arg : "");
997 strcpy(control_block.daemon_name, arg ? arg : "");
1000 if (!arg || !(r = atoi(arg)))
1002 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1005 control_block.idle_timeout = r;
1008 if (!arg || !(r = atoi(arg)))
1010 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1013 control_block.maxrecordsize = r * 1024;
1016 control_block.inetd = 1;
1026 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1027 " -l <logfile> -u <user> -c <config> -t <minutes>"
1028 " -k <kilobytes> -d <daemon>"
1029 " -zsiST -w <directory> <listender-addr>... ]\n", me);
1037 typedef struct _Args
1043 static Args ArgDetails;
1045 /* name of the executable */
1046 #define SZAPPNAME "server"
1048 /* list of service dependencies - "dep1\0dep2\0\0" */
1049 #define SZDEPENDENCIES ""
1051 int statserv_main(int argc, char **argv,
1052 bend_initresult *(*bend_init)(bend_initrequest *r),
1053 void (*bend_close)(void *handle))
1055 statserv_options_block *cb = statserv_getcontrol();
1057 cb->bend_init = bend_init;
1058 cb->bend_close = bend_close;
1060 statserv_setcontrol(cb);
1062 /* Lets setup the Arg structure */
1063 ArgDetails.argc = argc;
1064 ArgDetails.argv = argv;
1066 /* Now setup the service with the service controller */
1067 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
1068 cb->service_name, /* internal service name */
1069 cb->service_name, /* displayed name of the service */
1074 int StartAppService(void *pHandle, int argc, char **argv)
1076 /* Initializes the App */
1080 void RunAppService(void *pHandle)
1082 Args *pArgs = (Args *)pHandle;
1084 /* Starts the app running */
1085 statserv_start(pArgs->argc, pArgs->argv);
1088 void StopAppService(void *pHandle)
1091 statserv_closedown();
1094 int statserv_main(int argc, char **argv,
1095 bend_initresult *(*bend_init)(bend_initrequest *r),
1096 void (*bend_close)(void *handle))
1099 statserv_options_block *cb = statserv_getcontrol();
1101 cb->bend_init = bend_init;
1102 cb->bend_close = bend_close;
1104 statserv_setcontrol(cb);
1105 ret = statserv_start (argc, argv);
1106 statserv_closedown ();