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.68 2000-11-29 14:22:47 adam
11 * Implemented XML/SGML attributes for data1 so that d1_read reads them
12 * and d1_write generates proper attributes for XML/SGML records. Added
13 * register locking for threaded version.
15 * Revision 1.67 2000/11/23 10:58:32 adam
16 * SSL comstack support. Separate POSIX thread support library.
18 * Revision 1.66 2000/10/06 12:00:28 adam
19 * Fixed Handle leak for WIN32.
21 * Revision 1.65 2000/09/04 08:58:15 adam
22 * Added prefix yaz_ for most logging utility functions.
24 * Revision 1.64 2000/04/05 07:39:55 adam
25 * Added shared library support (libtool).
27 * Revision 1.63 2000/03/20 19:06:25 adam
28 * Added Segment request for fronend server. Work on admin for client.
30 * Revision 1.62 2000/03/17 12:47:02 adam
31 * Minor changes to admin client.
33 * Revision 1.61 2000/03/15 12:59:49 adam
34 * Added handle member to statserv_control.
36 * Revision 1.60 2000/03/14 09:06:11 adam
37 * Added POSIX threads support for frontend server.
39 * Revision 1.59 1999/11/30 13:47:12 adam
40 * Improved installation. Moved header files to include/yaz.
42 * Revision 1.58 1999/08/27 09:40:32 adam
43 * Renamed logf function to yaz_log. Removed VC++ project files.
45 * Revision 1.57 1999/07/06 12:17:15 adam
46 * Added option -1 that runs server once (for profiling purposes).
48 * Revision 1.56 1999/06/10 11:45:30 adam
49 * Added bend_start, bend_stop handlers and removed pre_init.
50 * Handlers bend_start/bend_stop are called when service/daemon is
53 * Revision 1.55 1999/06/10 09:18:54 adam
54 * Modified so that pre_init is called when service/server is started.
56 * Revision 1.54 1999/04/16 14:45:55 adam
57 * Added interface for tcpd wrapper for access control.
59 * Revision 1.53 1999/02/02 13:57:39 adam
60 * Uses preprocessor define WIN32 instead of WINDOWS to build code
61 * for Microsoft WIN32.
63 * Revision 1.52 1998/08/21 14:13:34 adam
64 * Added GNU Configure script to build Makefiles.
66 * Revision 1.51 1998/07/07 15:51:03 adam
67 * Changed server so that it stops if bind fails - "address already in
68 * use" typically causes this.
70 * Revision 1.50 1998/06/22 11:32:39 adam
71 * Added 'conditional cs_listen' feature.
73 * Revision 1.49 1998/02/27 14:04:55 adam
74 * Fixed bug in statserv_remove.
76 * Revision 1.48 1998/02/11 11:53:36 adam
77 * Changed code so that it compiles as C++.
79 * Revision 1.47 1998/02/10 10:28:57 adam
80 * Added app_name, service_dependencies, service_display_name and
81 * options_func. options_func allows us to specify a different function
82 * to interogate the command line arguments. The other members allow us
83 * to pass the full service details accross to the service manager (CW).
86 * Revision 1.46 1998/01/30 15:24:57 adam
87 * Fixed bug in inetd code. The server listened on tcp:@:9999 even
88 * though it was started in inetd mode.
90 * Revision 1.45 1998/01/29 13:30:23 adam
91 * Better event handle system for NT/Unix.
93 * Revision 1.44 1997/11/07 13:31:52 adam
94 * Added NT Service name part of statserv_options_block. Moved NT
95 * service utility to server library.
97 * Revision 1.43 1997/10/31 12:20:09 adam
98 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
99 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
100 * Bug fix: missing fclose in data1_read_espec1.
102 * Revision 1.42 1997/10/27 14:03:02 adam
103 * Added new member to statserver_options_block, pre_init, which
104 * specifies a callback to be invoked after command line parsing and
105 * before the server listens for the first time.
107 * Revision 1.41 1997/09/29 07:19:32 adam
108 * Server library uses nmem_init/nmem_exit. The log prefix no longer
109 * includes leading path on NT.
111 * Revision 1.40 1997/09/17 12:10:41 adam
114 * Revision 1.39 1997/09/09 10:10:19 adam
115 * Another MSV5.0 port. Changed projects to include proper
116 * library/include paths.
117 * Server starts server in test-mode when no options are given.
119 * Revision 1.38 1997/09/04 14:19:14 adam
122 * Revision 1.37 1997/09/01 08:53:01 adam
123 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
124 * moved a separate directory. MSV5.0 project server.dsp created.
125 * As an option, the server can now operate as an NT service.
127 * Revision 1.36 1996/07/06 19:58:36 quinn
128 * System headerfiles gathered in yconfig
130 * Revision 1.35 1996/05/29 10:03:28 quinn
133 * Revision 1.34 1996/02/21 13:12:07 quinn
134 * *** empty log message ***
136 * Revision 1.33 1996/02/10 12:23:49 quinn
137 * Enable inetd operations fro TCP/IP stack
139 * Revision 1.32 1996/01/19 15:41:52 quinn
140 * *** empty log message ***
142 * Revision 1.31 1995/11/17 11:09:39 adam
143 * Added new option '-c' to specify configuration name in control block.
145 * Revision 1.30 1995/11/01 13:54:59 quinn
148 * Revision 1.29 1995/10/30 12:41:29 quinn
149 * Added hostname lookup for server.
151 * Revision 1.28 1995/09/29 17:12:30 quinn
154 * Revision 1.27 1995/09/27 15:03:02 quinn
155 * Modified function heads & prototypes.
157 * Revision 1.26 1995/08/29 14:44:51 quinn
160 * Revision 1.25 1995/08/29 11:18:02 quinn
161 * Added code to receive close
163 * Revision 1.24 1995/06/16 10:31:39 quinn
164 * Added session timeout.
166 * Revision 1.23 1995/06/15 12:30:48 quinn
169 * Revision 1.22 1995/06/15 07:45:17 quinn
172 * Revision 1.21 1995/06/06 08:15:40 quinn
175 * Revision 1.20 1995/05/29 08:12:09 quinn
178 * Revision 1.19 1995/05/16 09:37:27 quinn
181 * Revision 1.18 1995/05/16 08:51:09 quinn
182 * License, documentation, and memory fixes
184 * Revision 1.17 1995/05/15 11:56:42 quinn
185 * Asynchronous facilities. Restructuring of seshigh code.
187 * Revision 1.16 1995/04/10 10:23:40 quinn
188 * Some work to add scan and other things.
190 * Revision 1.15 1995/03/31 10:16:51 quinn
193 * Revision 1.14 1995/03/31 09:18:58 quinn
196 * Revision 1.13 1995/03/30 16:08:39 quinn
199 * Revision 1.12 1995/03/30 13:29:02 quinn
202 * Revision 1.11 1995/03/30 12:18:17 quinn
205 * Revision 1.10 1995/03/29 15:40:16 quinn
206 * Ongoing work. Statserv is now dynamic by default
208 * Revision 1.9 1995/03/27 08:34:30 quinn
209 * Added dynamic server functionality.
210 * Released bindings to session.c (is now redundant)
212 * Revision 1.8 1995/03/20 09:46:26 quinn
215 * Revision 1.7 1995/03/16 13:29:04 quinn
216 * Partitioned server.
218 * Revision 1.6 1995/03/15 15:18:52 quinn
219 * Little changes to better support nonblocking I/O
222 * Revision 1.5 1995/03/15 08:37:45 quinn
223 * Now we're pretty much set for nonblocking I/O.
225 * Revision 1.4 1995/03/14 16:59:48 quinn
228 * Revision 1.3 1995/03/14 11:30:15 quinn
231 * Revision 1.2 1995/03/14 10:28:03 quinn
232 * More work on demo server.
234 * Revision 1.1 1995/03/10 18:22:45 quinn
235 * The rudiments of an asynchronous server.
257 #include <yaz/comstack.h>
258 #include <yaz/tcpip.h>
259 #include <yaz/options.h>
261 #include <yaz/xmosi.h>
266 #include <yaz/statserv.h>
268 static IOCHAN pListener = NULL;
270 static char *me = "statserver";
274 int check_options(int argc, char **argv);
275 statserv_options_block control_block = {
276 1, /* dynamic mode */
277 0, /* threaded mode */
278 0, /* one shot (single session) */
279 LOG_DEFAULT_LEVEL, /* log level */
281 "", /* diagnostic output to stderr */
282 "tcp:@:9999", /* default listener port */
283 PROTO_Z3950, /* default application protocol */
284 60, /* idle timeout (minutes) */
285 1024*1024, /* maximum PDU size (approx.) to allow */
286 "default-config", /* configuration name to pass to backend */
287 "", /* set user id */
288 0, /* bend_start handler */
289 0, /* bend_stop handler */
290 check_options, /* Default routine, for checking the run-time arguments */
293 0, /* default value for inet deamon */
294 0, /* handle (for service, etc) */
295 0, /* bend_init handle */
296 0 /* bend_close handle */
298 ,"Z39.50 Server", /* NT Service Name */
299 "Server", /* NT application Name */
300 "", /* NT Service Dependencies */
301 "Z39.50 Server" /* NT Service Display Name */
306 * handle incoming connect requests.
307 * The dynamic mode is a bit tricky mostly because we want to avoid
308 * doing all of the listening and accepting in the parent - it's
313 typedef struct _ThreadList ThreadList;
322 static ThreadList *pFirstThread;
323 static CRITICAL_SECTION Thread_CritSect;
324 static BOOL bInitialized = FALSE;
326 static void ThreadList_Initialize()
328 /* Initialize the critical Sections */
329 InitializeCriticalSection(&Thread_CritSect);
331 /* Set the first thraed */
334 /* we have been initialized */
338 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
340 /* Only one thread can go through this section at a time */
341 EnterCriticalSection(&Thread_CritSect);
344 /* Lets create our new object */
345 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
346 pNewThread->hThread = hThread;
347 pNewThread->pIOChannel = pIOChannel;
348 pNewThread->pNext = pFirstThread;
349 pFirstThread = pNewThread;
351 /* Lets let somebody else create a new object now */
352 LeaveCriticalSection(&Thread_CritSect);
356 void statserv_remove(IOCHAN pIOChannel)
358 /* Only one thread can go through this section at a time */
359 EnterCriticalSection(&Thread_CritSect);
362 ThreadList *pCurrentThread = pFirstThread;
363 ThreadList *pNextThread;
364 ThreadList *pPrevThread =NULL;
366 /* Step through alll the threads */
367 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
369 /* We only need to compare on the IO Channel */
370 if (pCurrentThread->pIOChannel == pIOChannel)
372 /* We have found the thread we want to delete */
373 /* First of all reset the next pointers */
374 if (pPrevThread == NULL)
375 pFirstThread = pCurrentThread->pNext;
377 pPrevThread->pNext = pCurrentThread->pNext;
379 /* All we need todo now is delete the memory */
380 free(pCurrentThread);
382 /* No need to look at any more threads */
387 /* We need to look at another thread */
388 pNextThread = pCurrentThread->pNext;
389 pPrevThread = pCurrentThread;
393 /* Lets let somebody else remove an object now */
394 LeaveCriticalSection(&Thread_CritSect);
398 /* WIN32 statserv_closedown */
399 void statserv_closedown()
401 /* Shouldn't do anything if we are not initialized */
405 HANDLE *pThreadHandles = NULL;
407 /* We need to stop threads adding and removing while we */
408 /* start the closedown process */
409 EnterCriticalSection(&Thread_CritSect);
412 /* We have exclusive access to the thread stuff now */
413 /* Y didn't i use a semaphore - Oh well never mind */
414 ThreadList *pCurrentThread = pFirstThread;
416 /* Before we do anything else, we need to shutdown the listener */
417 if (pListener != NULL)
418 iochan_destroy(pListener);
420 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
422 /* Just destroy the IOCHAN, that should do the trick */
423 iochan_destroy(pCurrentThread->pIOChannel);
425 /* Keep a running count of our handles */
431 HANDLE *pCurrentHandle ;
433 /* Allocate the thread handle array */
434 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
435 pCurrentHandle = pThreadHandles;
437 for (pCurrentThread = pFirstThread;
438 pCurrentThread != NULL;
439 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
441 /* Just the handle */
442 *pCurrentHandle = pCurrentThread->hThread;
446 /* We can now leave the critical section */
447 LeaveCriticalSection(&Thread_CritSect);
450 /* Now we can really do something */
453 /* This will now wait, until all the threads close */
454 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
456 /* Free the memory we allocated for the handle array */
457 free(pThreadHandles);
460 if (control_block.bend_stop)
461 (*control_block.bend_stop)(&control_block);
462 /* No longer require the critical section, since all threads are dead */
463 DeleteCriticalSection(&Thread_CritSect);
467 void event_loop_thread (IOCHAN iochan)
469 event_loop (&iochan);
473 static void listener(IOCHAN h, int event)
475 COMSTACK line = (COMSTACK) iochan_getdata(h);
480 if (event == EVENT_INPUT)
482 if ((res = cs_listen(line, 0, 0)) < 0)
484 yaz_log(LOG_FATAL, "cs_listen failed");
489 yaz_log(LOG_DEBUG, "listen ok");
490 iochan_setevent(h, EVENT_OUTPUT);
491 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
493 else if (event == EVENT_OUTPUT)
495 COMSTACK new_line = cs_accept(line);
501 yaz_log(LOG_FATAL, "Accept failed.");
502 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
505 yaz_log(LOG_DEBUG, "Accept ok");
507 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
510 yaz_log(LOG_FATAL, "Failed to create iochan");
515 yaz_log(LOG_DEBUG, "Creating association");
516 if (!(newas = create_association(new_chan, new_line)))
518 yaz_log(LOG_FATAL, "Failed to create new assoc.");
522 newas->cs_get_mask = EVENT_INPUT;
523 newas->cs_put_mask = 0;
524 newas->cs_accept_mask = 0;
526 yaz_log(LOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
527 iochan_setdata(new_chan, newas);
528 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
530 /* Now what we need todo is create a new thread with this iochan as
532 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
533 if (newHandle == (HANDLE) -1)
536 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create new thread.");
540 /* We successfully created the thread, so add it to the list */
541 statserv_add(newHandle, new_chan);
543 yaz_log(LOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
544 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
548 yaz_log(LOG_FATAL, "Bad event on listener.");
556 /* To save having an #ifdef in event_loop we need to define this empty function */
557 void statserv_remove(IOCHAN pIOChannel)
561 void statserv_closedown()
565 if (control_block.bend_stop)
566 (*control_block.bend_stop)(&control_block);
568 for (p = pListener; p; p = p->next)
572 void sigterm(int sig)
574 statserv_closedown();
578 static void *new_session (void *vp);
581 static void listener(IOCHAN h, int event)
583 COMSTACK line = (COMSTACK) iochan_getdata(h);
585 static int child = 0;
588 if (event == EVENT_INPUT)
590 if (control_block.dynamic && !child)
596 yaz_log(LOG_FATAL|LOG_ERRNO, "pipe");
600 if ((res = fork()) < 0)
602 yaz_log(LOG_FATAL|LOG_ERRNO, "fork");
606 else if (res == 0) /* child */
613 for (pp = pListener; pp; pp = iochan_getnext(pp))
617 COMSTACK l = (COMSTACK)iochan_getdata(pp);
622 sprintf(nbuf, "%s(%d)", me, getpid());
623 yaz_log_init(control_block.loglevel, nbuf, 0);
628 /* wait for child to take the call */
634 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
636 yaz_log(LOG_FATAL|LOG_ERRNO, "handshake read");
642 yaz_log(LOG_DEBUG, "P: Child has taken the call");
647 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
648 control_block.daemon_name)) < 0)
650 yaz_log(LOG_WARN, "cs_listen failed");
655 yaz_log(LOG_DEBUG, "listen ok");
656 iochan_setevent(h, EVENT_OUTPUT);
657 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
659 /* in dynamic mode, only the child ever comes down here */
660 else if (event == EVENT_OUTPUT)
662 COMSTACK new_line = cs_accept(line);
666 yaz_log(LOG_FATAL, "Accept failed.");
667 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
670 yaz_log(LOG_DEBUG, "accept ok");
671 if (control_block.dynamic)
674 /* close our half of the listener socket */
675 for (pp = pListener; pp; pp = iochan_getnext(pp))
677 COMSTACK l = (COMSTACK)iochan_getdata(pp);
682 yaz_log(LOG_DEBUG, "Releasing parent");
686 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
689 if (control_block.threads)
691 pthread_t child_thread;
692 pthread_create (&child_thread, 0, new_session, new_line);
693 pthread_detach (child_thread);
696 new_session(new_line);
698 new_session(new_line);
703 yaz_log(LOG_FATAL, "Bad event on listener.");
709 static void *new_session (void *vp)
714 COMSTACK new_line = (COMSTACK) vp;
716 unsigned cs_get_mask, cs_accept_mask, mask =
717 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
718 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
722 cs_accept_mask = mask; /* accept didn't complete */
727 cs_accept_mask = 0; /* accept completed. */
728 cs_get_mask = mask = EVENT_INPUT;
731 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask)))
733 yaz_log(LOG_FATAL, "Failed to create iochan");
736 if (!(newas = create_association(new_chan, new_line)))
738 yaz_log(LOG_FATAL, "Failed to create new assoc.");
741 newas->cs_accept_mask = cs_accept_mask;
742 newas->cs_get_mask = cs_get_mask;
744 iochan_setdata(new_chan, newas);
745 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
746 a = cs_addrstr(new_line);
747 yaz_log(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
749 if (control_block.threads)
751 event_loop(&new_chan);
755 new_chan->next = pListener;
756 pListener = new_chan;
763 static void inetd_connection(int what)
770 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
772 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
774 if ((assoc = create_association(chan, line)))
776 iochan_setdata(chan, assoc);
777 iochan_settimeout(chan, control_block.idle_timeout * 60);
778 addr = cs_addrstr(line);
779 yaz_log(LOG_LOG, "Inetd association from %s", addr ? addr : "[UNKNOWN]");
783 yaz_log(LOG_FATAL, "Failed to create association structure");
785 chan->next = pListener;
790 yaz_log(LOG_FATAL, "Failed to create iochan");
795 yaz_log(LOG_ERRNO|LOG_FATAL, "Failed to create comstack on socket 0");
800 * Set up a listening endpoint, and give it to the event-handler.
802 static void add_listener(char *where, int what)
806 char mode[100], addr[100];
810 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
812 yaz_log (LOG_WARN, "%s: Address format: ('tcp'|'ssl')':'<address>",
816 if (!strcmp(mode, "tcp"))
818 else if (!strcmp(mode, "ssl"))
820 #if HAVE_OPENSSL_SSL_H
823 yaz_log (LOG_WARN, "SSL Transport not allowed by configuration.");
829 yaz_log (LOG_WARN, "You must specify either 'ssl:' or 'tcp:'");
832 yaz_log(LOG_LOG, "Adding %s %s listener on %s",
833 control_block.dynamic ? "dynamic" :
834 (control_block.threads ? "threaded" : "static"),
835 what == PROTO_SR ? "SR" : "Z3950", where);
836 if (!(l = cs_create(type, 0, what)))
838 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
841 ap = cs_straddr (l, addr);
844 fprintf(stderr, "Address resolution failed.\n");
848 if (cs_bind(l, ap, CS_SERVER) < 0)
850 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
854 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
857 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
861 iochan_setdata(lst, l);
863 /* Ensure our listener chain is setup properly */
864 lst->next = pListener;
869 /* For windows we don't need to catch the signals */
870 static void catchchld(int num)
872 while (waitpid(-1, 0, WNOHANG) > 0)
874 signal(SIGCHLD, catchchld);
878 statserv_options_block *statserv_getcontrol(void)
880 static statserv_options_block cb;
882 memcpy(&cb, &control_block, sizeof(cb));
886 void statserv_setcontrol(statserv_options_block *block)
888 memcpy(&control_block, block, sizeof(*block));
891 int statserv_start(int argc, char **argv)
897 /* We need to initialize the thread list */
898 ThreadList_Initialize();
902 if ((me = strrchr (argv[0], '\\')))
909 if (control_block.options_func(argc, argv))
912 if (control_block.bend_start)
913 (*control_block.bend_start)(&control_block);
915 logf (LOG_LOG, "Starting server %s", me);
917 if (control_block.inetd)
918 inetd_connection(control_block.default_proto);
921 logf (LOG_LOG, "Starting server %s pid=%d", me, getpid());
923 sigset_t sigs_to_block;
925 sigemptyset(&sigs_to_block);
926 sigaddset (&sigs_to_block, SIGTERM);
927 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
930 if (control_block.dynamic)
931 signal(SIGCHLD, catchchld);
933 signal (SIGPIPE, SIG_IGN);
934 signal (SIGTERM, sigterm);
935 if (*control_block.setuid)
939 if (!(pw = getpwnam(control_block.setuid)))
941 yaz_log(LOG_FATAL, "%s: Unknown user", control_block.setuid);
944 if (setuid(pw->pw_uid) < 0)
946 yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
952 if ((pListener == NULL) && *control_block.default_listen)
953 add_listener(control_block.default_listen,
954 control_block.default_proto);
956 if (pListener == NULL)
960 yaz_log(LOG_LOG, "Entering event loop.");
961 ret = event_loop(&pListener);
967 int check_options(int argc, char **argv)
972 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:", argv, argc, &arg)) != -2)
977 add_listener(arg, control_block.default_proto);
980 control_block.one_shot = 1;
981 control_block.dynamic = 0;
984 control_block.default_proto = PROTO_Z3950;
987 control_block.default_proto = PROTO_SR;
990 control_block.dynamic = 0;
994 control_block.dynamic = 0;
995 control_block.threads = 1;
997 fprintf(stderr, "%s: Threaded mode not available.\n", me);
1002 strcpy(control_block.logfile, arg ? arg : "");
1003 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1006 control_block.loglevel = yaz_log_mask_str(arg);
1007 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1010 strcpy(control_block.apdufile, arg ? arg : "");
1013 strcpy(control_block.setuid, arg ? arg : "");
1016 strcpy(control_block.configname, arg ? arg : "");
1019 strcpy(control_block.daemon_name, arg ? arg : "");
1022 if (!arg || !(r = atoi(arg)))
1024 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1027 control_block.idle_timeout = r;
1030 if (!arg || !(r = atoi(arg)))
1032 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1035 control_block.maxrecordsize = r * 1024;
1038 control_block.inetd = 1;
1048 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1049 " -l <logfile> -u <user> -c <config> -t <minutes>"
1050 " -k <kilobytes> -d <daemon>"
1051 " -zsiST -w <directory> <listender-addr>... ]\n", me);
1059 typedef struct _Args
1065 static Args ArgDetails;
1067 /* name of the executable */
1068 #define SZAPPNAME "server"
1070 /* list of service dependencies - "dep1\0dep2\0\0" */
1071 #define SZDEPENDENCIES ""
1073 int statserv_main(int argc, char **argv,
1074 bend_initresult *(*bend_init)(bend_initrequest *r),
1075 void (*bend_close)(void *handle))
1077 statserv_options_block *cb = statserv_getcontrol();
1079 cb->bend_init = bend_init;
1080 cb->bend_close = bend_close;
1082 statserv_setcontrol(cb);
1084 /* Lets setup the Arg structure */
1085 ArgDetails.argc = argc;
1086 ArgDetails.argv = argv;
1088 /* Now setup the service with the service controller */
1089 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
1090 cb->service_name, /* internal service name */
1091 cb->service_name, /* displayed name of the service */
1096 int StartAppService(void *pHandle, int argc, char **argv)
1098 /* Initializes the App */
1102 void RunAppService(void *pHandle)
1104 Args *pArgs = (Args *)pHandle;
1106 /* Starts the app running */
1107 statserv_start(pArgs->argc, pArgs->argv);
1110 void StopAppService(void *pHandle)
1113 statserv_closedown();
1116 int statserv_main(int argc, char **argv,
1117 bend_initresult *(*bend_init)(bend_initrequest *r),
1118 void (*bend_close)(void *handle))
1121 statserv_options_block *cb = statserv_getcontrol();
1123 cb->bend_init = bend_init;
1124 cb->bend_close = bend_close;
1126 statserv_setcontrol(cb);
1127 ret = statserv_start (argc, argv);
1128 statserv_closedown ();