2 * Copyright (c) 1995-2000, 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.69 2000-12-01 17:56:41 adam
11 * on WIN32 function statserv_closedown closes socket(s) to provoke close.
13 * Revision 1.68 2000/11/29 14:22:47 adam
14 * Implemented XML/SGML attributes for data1 so that d1_read reads them
15 * and d1_write generates proper attributes for XML/SGML records. Added
16 * register locking for threaded version.
18 * Revision 1.67 2000/11/23 10:58:32 adam
19 * SSL comstack support. Separate POSIX thread support library.
21 * Revision 1.66 2000/10/06 12:00:28 adam
22 * Fixed Handle leak for WIN32.
24 * Revision 1.65 2000/09/04 08:58:15 adam
25 * Added prefix yaz_ for most logging utility functions.
27 * Revision 1.64 2000/04/05 07:39:55 adam
28 * Added shared library support (libtool).
30 * Revision 1.63 2000/03/20 19:06:25 adam
31 * Added Segment request for fronend server. Work on admin for client.
33 * Revision 1.62 2000/03/17 12:47:02 adam
34 * Minor changes to admin client.
36 * Revision 1.61 2000/03/15 12:59:49 adam
37 * Added handle member to statserv_control.
39 * Revision 1.60 2000/03/14 09:06:11 adam
40 * Added POSIX threads support for frontend server.
42 * Revision 1.59 1999/11/30 13:47:12 adam
43 * Improved installation. Moved header files to include/yaz.
45 * Revision 1.58 1999/08/27 09:40:32 adam
46 * Renamed logf function to yaz_log. Removed VC++ project files.
48 * Revision 1.57 1999/07/06 12:17:15 adam
49 * Added option -1 that runs server once (for profiling purposes).
51 * Revision 1.56 1999/06/10 11:45:30 adam
52 * Added bend_start, bend_stop handlers and removed pre_init.
53 * Handlers bend_start/bend_stop are called when service/daemon is
56 * Revision 1.55 1999/06/10 09:18:54 adam
57 * Modified so that pre_init is called when service/server is started.
59 * Revision 1.54 1999/04/16 14:45:55 adam
60 * Added interface for tcpd wrapper for access control.
62 * Revision 1.53 1999/02/02 13:57:39 adam
63 * Uses preprocessor define WIN32 instead of WINDOWS to build code
64 * for Microsoft WIN32.
66 * Revision 1.52 1998/08/21 14:13:34 adam
67 * Added GNU Configure script to build Makefiles.
69 * Revision 1.51 1998/07/07 15:51:03 adam
70 * Changed server so that it stops if bind fails - "address already in
71 * use" typically causes this.
73 * Revision 1.50 1998/06/22 11:32:39 adam
74 * Added 'conditional cs_listen' feature.
76 * Revision 1.49 1998/02/27 14:04:55 adam
77 * Fixed bug in statserv_remove.
79 * Revision 1.48 1998/02/11 11:53:36 adam
80 * Changed code so that it compiles as C++.
82 * Revision 1.47 1998/02/10 10:28:57 adam
83 * Added app_name, service_dependencies, service_display_name and
84 * options_func. options_func allows us to specify a different function
85 * to interogate the command line arguments. The other members allow us
86 * to pass the full service details accross to the service manager (CW).
89 * Revision 1.46 1998/01/30 15:24:57 adam
90 * Fixed bug in inetd code. The server listened on tcp:@:9999 even
91 * though it was started in inetd mode.
93 * Revision 1.45 1998/01/29 13:30:23 adam
94 * Better event handle system for NT/Unix.
96 * Revision 1.44 1997/11/07 13:31:52 adam
97 * Added NT Service name part of statserv_options_block. Moved NT
98 * service utility to server library.
100 * Revision 1.43 1997/10/31 12:20:09 adam
101 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
102 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
103 * Bug fix: missing fclose in data1_read_espec1.
105 * Revision 1.42 1997/10/27 14:03:02 adam
106 * Added new member to statserver_options_block, pre_init, which
107 * specifies a callback to be invoked after command line parsing and
108 * before the server listens for the first time.
110 * Revision 1.41 1997/09/29 07:19:32 adam
111 * Server library uses nmem_init/nmem_exit. The log prefix no longer
112 * includes leading path on NT.
114 * Revision 1.40 1997/09/17 12:10:41 adam
117 * Revision 1.39 1997/09/09 10:10:19 adam
118 * Another MSV5.0 port. Changed projects to include proper
119 * library/include paths.
120 * Server starts server in test-mode when no options are given.
122 * Revision 1.38 1997/09/04 14:19:14 adam
125 * Revision 1.37 1997/09/01 08:53:01 adam
126 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
127 * moved a separate directory. MSV5.0 project server.dsp created.
128 * As an option, the server can now operate as an NT service.
130 * Revision 1.36 1996/07/06 19:58:36 quinn
131 * System headerfiles gathered in yconfig
133 * Revision 1.35 1996/05/29 10:03:28 quinn
136 * Revision 1.34 1996/02/21 13:12:07 quinn
137 * *** empty log message ***
139 * Revision 1.33 1996/02/10 12:23:49 quinn
140 * Enable inetd operations fro TCP/IP stack
142 * Revision 1.32 1996/01/19 15:41:52 quinn
143 * *** empty log message ***
145 * Revision 1.31 1995/11/17 11:09:39 adam
146 * Added new option '-c' to specify configuration name in control block.
148 * Revision 1.30 1995/11/01 13:54:59 quinn
151 * Revision 1.29 1995/10/30 12:41:29 quinn
152 * Added hostname lookup for server.
154 * Revision 1.28 1995/09/29 17:12:30 quinn
157 * Revision 1.27 1995/09/27 15:03:02 quinn
158 * Modified function heads & prototypes.
160 * Revision 1.26 1995/08/29 14:44:51 quinn
163 * Revision 1.25 1995/08/29 11:18:02 quinn
164 * Added code to receive close
166 * Revision 1.24 1995/06/16 10:31:39 quinn
167 * Added session timeout.
169 * Revision 1.23 1995/06/15 12:30:48 quinn
172 * Revision 1.22 1995/06/15 07:45:17 quinn
175 * Revision 1.21 1995/06/06 08:15:40 quinn
178 * Revision 1.20 1995/05/29 08:12:09 quinn
181 * Revision 1.19 1995/05/16 09:37:27 quinn
184 * Revision 1.18 1995/05/16 08:51:09 quinn
185 * License, documentation, and memory fixes
187 * Revision 1.17 1995/05/15 11:56:42 quinn
188 * Asynchronous facilities. Restructuring of seshigh code.
190 * Revision 1.16 1995/04/10 10:23:40 quinn
191 * Some work to add scan and other things.
193 * Revision 1.15 1995/03/31 10:16:51 quinn
196 * Revision 1.14 1995/03/31 09:18:58 quinn
199 * Revision 1.13 1995/03/30 16:08:39 quinn
202 * Revision 1.12 1995/03/30 13:29:02 quinn
205 * Revision 1.11 1995/03/30 12:18:17 quinn
208 * Revision 1.10 1995/03/29 15:40:16 quinn
209 * Ongoing work. Statserv is now dynamic by default
211 * Revision 1.9 1995/03/27 08:34:30 quinn
212 * Added dynamic server functionality.
213 * Released bindings to session.c (is now redundant)
215 * Revision 1.8 1995/03/20 09:46:26 quinn
218 * Revision 1.7 1995/03/16 13:29:04 quinn
219 * Partitioned server.
221 * Revision 1.6 1995/03/15 15:18:52 quinn
222 * Little changes to better support nonblocking I/O
225 * Revision 1.5 1995/03/15 08:37:45 quinn
226 * Now we're pretty much set for nonblocking I/O.
228 * Revision 1.4 1995/03/14 16:59:48 quinn
231 * Revision 1.3 1995/03/14 11:30:15 quinn
234 * Revision 1.2 1995/03/14 10:28:03 quinn
235 * More work on demo server.
237 * Revision 1.1 1995/03/10 18:22:45 quinn
238 * The rudiments of an asynchronous server.
260 #include <yaz/comstack.h>
261 #include <yaz/tcpip.h>
262 #include <yaz/options.h>
264 #include <yaz/xmosi.h>
269 #include <yaz/statserv.h>
271 static IOCHAN pListener = NULL;
273 static char *me = "statserver";
277 int check_options(int argc, char **argv);
278 statserv_options_block control_block = {
279 1, /* dynamic mode */
280 0, /* threaded mode */
281 0, /* one shot (single session) */
282 LOG_DEFAULT_LEVEL, /* log level */
284 "", /* diagnostic output to stderr */
285 "tcp:@:9999", /* default listener port */
286 PROTO_Z3950, /* default application protocol */
287 60, /* idle timeout (minutes) */
288 1024*1024, /* maximum PDU size (approx.) to allow */
289 "default-config", /* configuration name to pass to backend */
290 "", /* set user id */
291 0, /* bend_start handler */
292 0, /* bend_stop handler */
293 check_options, /* Default routine, for checking the run-time arguments */
296 0, /* default value for inet deamon */
297 0, /* handle (for service, etc) */
298 0, /* bend_init handle */
299 0 /* bend_close handle */
301 ,"Z39.50 Server", /* NT Service Name */
302 "Server", /* NT application Name */
303 "", /* NT Service Dependencies */
304 "Z39.50 Server" /* NT Service Display Name */
309 * handle incoming connect requests.
310 * The dynamic mode is a bit tricky mostly because we want to avoid
311 * doing all of the listening and accepting in the parent - it's
316 typedef struct _ThreadList ThreadList;
325 static ThreadList *pFirstThread;
326 static CRITICAL_SECTION Thread_CritSect;
327 static BOOL bInitialized = FALSE;
329 static void ThreadList_Initialize()
331 /* Initialize the critical Sections */
332 InitializeCriticalSection(&Thread_CritSect);
334 /* Set the first thraed */
337 /* we have been initialized */
341 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
343 /* Only one thread can go through this section at a time */
344 EnterCriticalSection(&Thread_CritSect);
347 /* Lets create our new object */
348 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
349 pNewThread->hThread = hThread;
350 pNewThread->pIOChannel = pIOChannel;
351 pNewThread->pNext = pFirstThread;
352 pFirstThread = pNewThread;
354 /* Lets let somebody else create a new object now */
355 LeaveCriticalSection(&Thread_CritSect);
359 void statserv_remove(IOCHAN pIOChannel)
361 /* Only one thread can go through this section at a time */
362 EnterCriticalSection(&Thread_CritSect);
365 ThreadList *pCurrentThread = pFirstThread;
366 ThreadList *pNextThread;
367 ThreadList *pPrevThread =NULL;
369 /* Step through alll the threads */
370 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
372 /* We only need to compare on the IO Channel */
373 if (pCurrentThread->pIOChannel == pIOChannel)
375 /* We have found the thread we want to delete */
376 /* First of all reset the next pointers */
377 if (pPrevThread == NULL)
378 pFirstThread = pCurrentThread->pNext;
380 pPrevThread->pNext = pCurrentThread->pNext;
382 /* All we need todo now is delete the memory */
383 free(pCurrentThread);
385 /* No need to look at any more threads */
390 /* We need to look at another thread */
391 pNextThread = pCurrentThread->pNext;
392 pPrevThread = pCurrentThread;
396 /* Lets let somebody else remove an object now */
397 LeaveCriticalSection(&Thread_CritSect);
401 /* WIN32 statserv_closedown */
402 void statserv_closedown()
404 /* Shouldn't do anything if we are not initialized */
408 HANDLE *pThreadHandles = NULL;
410 /* We need to stop threads adding and removing while we */
411 /* start the closedown process */
412 EnterCriticalSection(&Thread_CritSect);
415 /* We have exclusive access to the thread stuff now */
416 /* Y didn't i use a semaphore - Oh well never mind */
417 ThreadList *pCurrentThread = pFirstThread;
419 /* Before we do anything else, we need to shutdown the listener */
420 if (pListener != NULL)
421 iochan_destroy(pListener);
423 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
425 /* Just destroy the IOCHAN, that should do the trick */
426 iochan_destroy(pCurrentThread->pIOChannel);
427 closesocket(pCurrentThread->pIOChannel->fd);
429 /* Keep a running count of our handles */
435 HANDLE *pCurrentHandle ;
437 /* Allocate the thread handle array */
438 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
439 pCurrentHandle = pThreadHandles;
441 for (pCurrentThread = pFirstThread;
442 pCurrentThread != NULL;
443 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
445 /* Just the handle */
446 *pCurrentHandle = pCurrentThread->hThread;
450 /* We can now leave the critical section */
451 LeaveCriticalSection(&Thread_CritSect);
454 /* Now we can really do something */
457 logf (LOG_LOG, "waiting for %d to die", iHandles);
458 /* This will now wait, until all the threads close */
459 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
461 /* Free the memory we allocated for the handle array */
462 free(pThreadHandles);
465 if (control_block.bend_stop)
466 (*control_block.bend_stop)(&control_block);
467 /* No longer require the critical section, since all threads are dead */
468 DeleteCriticalSection(&Thread_CritSect);
472 void event_loop_thread (IOCHAN iochan)
474 event_loop (&iochan);
478 static void listener(IOCHAN h, int event)
480 COMSTACK line = (COMSTACK) iochan_getdata(h);
485 if (event == EVENT_INPUT)
487 if ((res = cs_listen(line, 0, 0)) < 0)
489 yaz_log(LOG_FATAL, "cs_listen failed");
494 yaz_log(LOG_DEBUG, "listen ok");
495 iochan_setevent(h, EVENT_OUTPUT);
496 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
498 else if (event == EVENT_OUTPUT)
500 COMSTACK new_line = cs_accept(line);
506 yaz_log(LOG_FATAL, "Accept failed.");
507 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
510 yaz_log(LOG_DEBUG, "Accept ok");
512 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
515 yaz_log(LOG_FATAL, "Failed to create iochan");
520 yaz_log(LOG_DEBUG, "Creating association");
521 if (!(newas = create_association(new_chan, new_line)))
523 yaz_log(LOG_FATAL, "Failed to create new assoc.");
527 newas->cs_get_mask = EVENT_INPUT;
528 newas->cs_put_mask = 0;
529 newas->cs_accept_mask = 0;
531 yaz_log(LOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
532 iochan_setdata(new_chan, newas);
533 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
535 /* Now what we need todo is create a new thread with this iochan as
537 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
538 if (newHandle == (HANDLE) -1)
541 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create new thread.");
545 /* We successfully created the thread, so add it to the list */
546 statserv_add(newHandle, new_chan);
548 yaz_log(LOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
549 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
553 yaz_log(LOG_FATAL, "Bad event on listener.");
561 /* To save having an #ifdef in event_loop we need to define this empty function */
562 void statserv_remove(IOCHAN pIOChannel)
566 void statserv_closedown()
570 if (control_block.bend_stop)
571 (*control_block.bend_stop)(&control_block);
573 for (p = pListener; p; p = p->next)
577 void sigterm(int sig)
579 statserv_closedown();
583 static void *new_session (void *vp);
586 static void listener(IOCHAN h, int event)
588 COMSTACK line = (COMSTACK) iochan_getdata(h);
590 static int child = 0;
593 if (event == EVENT_INPUT)
595 if (control_block.dynamic && !child)
601 yaz_log(LOG_FATAL|LOG_ERRNO, "pipe");
605 if ((res = fork()) < 0)
607 yaz_log(LOG_FATAL|LOG_ERRNO, "fork");
611 else if (res == 0) /* child */
618 for (pp = pListener; pp; pp = iochan_getnext(pp))
622 COMSTACK l = (COMSTACK)iochan_getdata(pp);
627 sprintf(nbuf, "%s(%d)", me, getpid());
628 yaz_log_init(control_block.loglevel, nbuf, 0);
633 /* wait for child to take the call */
639 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
641 yaz_log(LOG_FATAL|LOG_ERRNO, "handshake read");
647 yaz_log(LOG_DEBUG, "P: Child has taken the call");
652 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
653 control_block.daemon_name)) < 0)
655 yaz_log(LOG_WARN, "cs_listen failed");
660 yaz_log(LOG_DEBUG, "listen ok");
661 iochan_setevent(h, EVENT_OUTPUT);
662 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
664 /* in dynamic mode, only the child ever comes down here */
665 else if (event == EVENT_OUTPUT)
667 COMSTACK new_line = cs_accept(line);
671 yaz_log(LOG_FATAL, "Accept failed.");
672 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
675 yaz_log(LOG_DEBUG, "accept ok");
676 if (control_block.dynamic)
679 /* close our half of the listener socket */
680 for (pp = pListener; pp; pp = iochan_getnext(pp))
682 COMSTACK l = (COMSTACK)iochan_getdata(pp);
687 yaz_log(LOG_DEBUG, "Releasing parent");
691 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
694 if (control_block.threads)
696 pthread_t child_thread;
697 pthread_create (&child_thread, 0, new_session, new_line);
698 pthread_detach (child_thread);
701 new_session(new_line);
703 new_session(new_line);
708 yaz_log(LOG_FATAL, "Bad event on listener.");
714 static void *new_session (void *vp)
719 COMSTACK new_line = (COMSTACK) vp;
721 unsigned cs_get_mask, cs_accept_mask, mask =
722 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
723 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
727 cs_accept_mask = mask; /* accept didn't complete */
732 cs_accept_mask = 0; /* accept completed. */
733 cs_get_mask = mask = EVENT_INPUT;
736 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask)))
738 yaz_log(LOG_FATAL, "Failed to create iochan");
741 if (!(newas = create_association(new_chan, new_line)))
743 yaz_log(LOG_FATAL, "Failed to create new assoc.");
746 newas->cs_accept_mask = cs_accept_mask;
747 newas->cs_get_mask = cs_get_mask;
749 iochan_setdata(new_chan, newas);
750 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
751 a = cs_addrstr(new_line);
752 yaz_log(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
754 if (control_block.threads)
756 event_loop(&new_chan);
760 new_chan->next = pListener;
761 pListener = new_chan;
768 static void inetd_connection(int what)
775 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
777 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
779 if ((assoc = create_association(chan, line)))
781 iochan_setdata(chan, assoc);
782 iochan_settimeout(chan, control_block.idle_timeout * 60);
783 addr = cs_addrstr(line);
784 yaz_log(LOG_LOG, "Inetd association from %s", addr ? addr : "[UNKNOWN]");
788 yaz_log(LOG_FATAL, "Failed to create association structure");
790 chan->next = pListener;
795 yaz_log(LOG_FATAL, "Failed to create iochan");
800 yaz_log(LOG_ERRNO|LOG_FATAL, "Failed to create comstack on socket 0");
805 * Set up a listening endpoint, and give it to the event-handler.
807 static void add_listener(char *where, int what)
811 char mode[100], addr[100];
815 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
817 yaz_log (LOG_WARN, "%s: Address format: ('tcp'|'ssl')':'<address>",
821 if (!strcmp(mode, "tcp"))
823 else if (!strcmp(mode, "ssl"))
825 #if HAVE_OPENSSL_SSL_H
828 yaz_log (LOG_WARN, "SSL Transport not allowed by configuration.");
834 yaz_log (LOG_WARN, "You must specify either 'ssl:' or 'tcp:'");
837 yaz_log(LOG_LOG, "Adding %s %s listener on %s",
838 control_block.dynamic ? "dynamic" :
839 (control_block.threads ? "threaded" : "static"),
840 what == PROTO_SR ? "SR" : "Z3950", where);
841 if (!(l = cs_create(type, 0, what)))
843 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
846 ap = cs_straddr (l, addr);
849 fprintf(stderr, "Address resolution failed.\n");
853 if (cs_bind(l, ap, CS_SERVER) < 0)
855 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
859 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
862 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
866 iochan_setdata(lst, l);
868 /* Ensure our listener chain is setup properly */
869 lst->next = pListener;
874 /* For windows we don't need to catch the signals */
875 static void catchchld(int num)
877 while (waitpid(-1, 0, WNOHANG) > 0)
879 signal(SIGCHLD, catchchld);
883 statserv_options_block *statserv_getcontrol(void)
885 static statserv_options_block cb;
887 memcpy(&cb, &control_block, sizeof(cb));
891 void statserv_setcontrol(statserv_options_block *block)
893 memcpy(&control_block, block, sizeof(*block));
896 int statserv_start(int argc, char **argv)
902 /* We need to initialize the thread list */
903 ThreadList_Initialize();
907 if ((me = strrchr (argv[0], '\\')))
914 if (control_block.options_func(argc, argv))
917 if (control_block.bend_start)
918 (*control_block.bend_start)(&control_block);
920 logf (LOG_LOG, "Starting server %s", me);
922 if (control_block.inetd)
923 inetd_connection(control_block.default_proto);
926 logf (LOG_LOG, "Starting server %s pid=%d", me, getpid());
928 sigset_t sigs_to_block;
930 sigemptyset(&sigs_to_block);
931 sigaddset (&sigs_to_block, SIGTERM);
932 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
935 if (control_block.dynamic)
936 signal(SIGCHLD, catchchld);
938 signal (SIGPIPE, SIG_IGN);
939 signal (SIGTERM, sigterm);
940 if (*control_block.setuid)
944 if (!(pw = getpwnam(control_block.setuid)))
946 yaz_log(LOG_FATAL, "%s: Unknown user", control_block.setuid);
949 if (setuid(pw->pw_uid) < 0)
951 yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
957 if ((pListener == NULL) && *control_block.default_listen)
958 add_listener(control_block.default_listen,
959 control_block.default_proto);
961 if (pListener == NULL)
965 yaz_log(LOG_LOG, "Entering event loop.");
966 ret = event_loop(&pListener);
972 int check_options(int argc, char **argv)
977 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:", argv, argc, &arg)) != -2)
982 add_listener(arg, control_block.default_proto);
985 control_block.one_shot = 1;
986 control_block.dynamic = 0;
989 control_block.default_proto = PROTO_Z3950;
992 control_block.default_proto = PROTO_SR;
995 control_block.dynamic = 0;
999 control_block.dynamic = 0;
1000 control_block.threads = 1;
1002 fprintf(stderr, "%s: Threaded mode not available.\n", me);
1007 strcpy(control_block.logfile, arg ? arg : "");
1008 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1011 control_block.loglevel = yaz_log_mask_str(arg);
1012 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1015 strcpy(control_block.apdufile, arg ? arg : "");
1018 strcpy(control_block.setuid, arg ? arg : "");
1021 strcpy(control_block.configname, arg ? arg : "");
1024 strcpy(control_block.daemon_name, arg ? arg : "");
1027 if (!arg || !(r = atoi(arg)))
1029 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1032 control_block.idle_timeout = r;
1035 if (!arg || !(r = atoi(arg)))
1037 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1040 control_block.maxrecordsize = r * 1024;
1043 control_block.inetd = 1;
1053 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1054 " -l <logfile> -u <user> -c <config> -t <minutes>"
1055 " -k <kilobytes> -d <daemon>"
1056 " -zsiST -w <directory> <listender-addr>... ]\n", me);
1064 typedef struct _Args
1070 static Args ArgDetails;
1072 /* name of the executable */
1073 #define SZAPPNAME "server"
1075 /* list of service dependencies - "dep1\0dep2\0\0" */
1076 #define SZDEPENDENCIES ""
1078 int statserv_main(int argc, char **argv,
1079 bend_initresult *(*bend_init)(bend_initrequest *r),
1080 void (*bend_close)(void *handle))
1082 statserv_options_block *cb = statserv_getcontrol();
1084 cb->bend_init = bend_init;
1085 cb->bend_close = bend_close;
1087 statserv_setcontrol(cb);
1089 /* Lets setup the Arg structure */
1090 ArgDetails.argc = argc;
1091 ArgDetails.argv = argv;
1093 /* Now setup the service with the service controller */
1094 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
1095 cb->service_name, /* internal service name */
1096 cb->service_name, /* displayed name of the service */
1101 int StartAppService(void *pHandle, int argc, char **argv)
1103 /* Initializes the App */
1107 void RunAppService(void *pHandle)
1109 Args *pArgs = (Args *)pHandle;
1111 /* Starts the app running */
1112 statserv_start(pArgs->argc, pArgs->argv);
1115 void StopAppService(void *pHandle)
1118 statserv_closedown();
1121 int statserv_main(int argc, char **argv,
1122 bend_initresult *(*bend_init)(bend_initrequest *r),
1123 void (*bend_close)(void *handle))
1126 statserv_options_block *cb = statserv_getcontrol();
1128 cb->bend_init = bend_init;
1129 cb->bend_close = bend_close;
1131 statserv_setcontrol(cb);
1132 ret = statserv_start (argc, argv);
1133 statserv_closedown ();