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.75 2001-10-04 00:37:58 adam
11 * Fixes for GNU threads (not working yet).
13 * Revision 1.74 2001/10/03 23:55:18 adam
14 * GNU threads support.
16 * Revision 1.73 2001/06/28 09:27:06 adam
17 * Number of started sessions logged.
19 * Revision 1.72 2001/03/25 21:55:13 adam
20 * Added odr_intdup. Ztest server returns TaskPackage for ItemUpdate.
22 * Revision 1.71 2001/03/21 12:43:36 adam
23 * Implemented cs_create_host. Better error reporting for SSL comstack.
25 * Revision 1.70 2001/02/01 08:52:26 adam
26 * Fixed bug regarding inetd mode.
28 * Revision 1.69 2000/12/01 17:56:41 adam
29 * on WIN32 function statserv_closedown closes socket(s) to provoke close.
31 * Revision 1.68 2000/11/29 14:22:47 adam
32 * Implemented XML/SGML attributes for data1 so that d1_read reads them
33 * and d1_write generates proper attributes for XML/SGML records. Added
34 * register locking for threaded version.
36 * Revision 1.67 2000/11/23 10:58:32 adam
37 * SSL comstack support. Separate POSIX thread support library.
39 * Revision 1.66 2000/10/06 12:00:28 adam
40 * Fixed Handle leak for WIN32.
42 * Revision 1.65 2000/09/04 08:58:15 adam
43 * Added prefix yaz_ for most logging utility functions.
45 * Revision 1.64 2000/04/05 07:39:55 adam
46 * Added shared library support (libtool).
48 * Revision 1.63 2000/03/20 19:06:25 adam
49 * Added Segment request for fronend server. Work on admin for client.
51 * Revision 1.62 2000/03/17 12:47:02 adam
52 * Minor changes to admin client.
54 * Revision 1.61 2000/03/15 12:59:49 adam
55 * Added handle member to statserv_control.
57 * Revision 1.60 2000/03/14 09:06:11 adam
58 * Added POSIX threads support for frontend server.
60 * Revision 1.59 1999/11/30 13:47:12 adam
61 * Improved installation. Moved header files to include/yaz.
63 * Revision 1.58 1999/08/27 09:40:32 adam
64 * Renamed logf function to yaz_log. Removed VC++ project files.
66 * Revision 1.57 1999/07/06 12:17:15 adam
67 * Added option -1 that runs server once (for profiling purposes).
69 * Revision 1.56 1999/06/10 11:45:30 adam
70 * Added bend_start, bend_stop handlers and removed pre_init.
71 * Handlers bend_start/bend_stop are called when service/daemon is
74 * Revision 1.55 1999/06/10 09:18:54 adam
75 * Modified so that pre_init is called when service/server is started.
77 * Revision 1.54 1999/04/16 14:45:55 adam
78 * Added interface for tcpd wrapper for access control.
80 * Revision 1.53 1999/02/02 13:57:39 adam
81 * Uses preprocessor define WIN32 instead of WINDOWS to build code
82 * for Microsoft WIN32.
84 * Revision 1.52 1998/08/21 14:13:34 adam
85 * Added GNU Configure script to build Makefiles.
87 * Revision 1.51 1998/07/07 15:51:03 adam
88 * Changed server so that it stops if bind fails - "address already in
89 * use" typically causes this.
91 * Revision 1.50 1998/06/22 11:32:39 adam
92 * Added 'conditional cs_listen' feature.
94 * Revision 1.49 1998/02/27 14:04:55 adam
95 * Fixed bug in statserv_remove.
97 * Revision 1.48 1998/02/11 11:53:36 adam
98 * Changed code so that it compiles as C++.
100 * Revision 1.47 1998/02/10 10:28:57 adam
101 * Added app_name, service_dependencies, service_display_name and
102 * options_func. options_func allows us to specify a different function
103 * to interogate the command line arguments. The other members allow us
104 * to pass the full service details accross to the service manager (CW).
107 * Revision 1.46 1998/01/30 15:24:57 adam
108 * Fixed bug in inetd code. The server listened on tcp:@:9999 even
109 * though it was started in inetd mode.
111 * Revision 1.45 1998/01/29 13:30:23 adam
112 * Better event handle system for NT/Unix.
114 * Revision 1.44 1997/11/07 13:31:52 adam
115 * Added NT Service name part of statserv_options_block. Moved NT
116 * service utility to server library.
118 * Revision 1.43 1997/10/31 12:20:09 adam
119 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
120 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
121 * Bug fix: missing fclose in data1_read_espec1.
123 * Revision 1.42 1997/10/27 14:03:02 adam
124 * Added new member to statserver_options_block, pre_init, which
125 * specifies a callback to be invoked after command line parsing and
126 * before the server listens for the first time.
128 * Revision 1.41 1997/09/29 07:19:32 adam
129 * Server library uses nmem_init/nmem_exit. The log prefix no longer
130 * includes leading path on NT.
132 * Revision 1.40 1997/09/17 12:10:41 adam
135 * Revision 1.39 1997/09/09 10:10:19 adam
136 * Another MSV5.0 port. Changed projects to include proper
137 * library/include paths.
138 * Server starts server in test-mode when no options are given.
140 * Revision 1.38 1997/09/04 14:19:14 adam
143 * Revision 1.37 1997/09/01 08:53:01 adam
144 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
145 * moved a separate directory. MSV5.0 project server.dsp created.
146 * As an option, the server can now operate as an NT service.
148 * Revision 1.36 1996/07/06 19:58:36 quinn
149 * System headerfiles gathered in yconfig
151 * Revision 1.35 1996/05/29 10:03:28 quinn
154 * Revision 1.34 1996/02/21 13:12:07 quinn
155 * *** empty log message ***
157 * Revision 1.33 1996/02/10 12:23:49 quinn
158 * Enable inetd operations fro TCP/IP stack
160 * Revision 1.32 1996/01/19 15:41:52 quinn
161 * *** empty log message ***
163 * Revision 1.31 1995/11/17 11:09:39 adam
164 * Added new option '-c' to specify configuration name in control block.
166 * Revision 1.30 1995/11/01 13:54:59 quinn
169 * Revision 1.29 1995/10/30 12:41:29 quinn
170 * Added hostname lookup for server.
172 * Revision 1.28 1995/09/29 17:12:30 quinn
175 * Revision 1.27 1995/09/27 15:03:02 quinn
176 * Modified function heads & prototypes.
178 * Revision 1.26 1995/08/29 14:44:51 quinn
181 * Revision 1.25 1995/08/29 11:18:02 quinn
182 * Added code to receive close
184 * Revision 1.24 1995/06/16 10:31:39 quinn
185 * Added session timeout.
187 * Revision 1.23 1995/06/15 12:30:48 quinn
190 * Revision 1.22 1995/06/15 07:45:17 quinn
193 * Revision 1.21 1995/06/06 08:15:40 quinn
196 * Revision 1.20 1995/05/29 08:12:09 quinn
199 * Revision 1.19 1995/05/16 09:37:27 quinn
202 * Revision 1.18 1995/05/16 08:51:09 quinn
203 * License, documentation, and memory fixes
205 * Revision 1.17 1995/05/15 11:56:42 quinn
206 * Asynchronous facilities. Restructuring of seshigh code.
208 * Revision 1.16 1995/04/10 10:23:40 quinn
209 * Some work to add scan and other things.
211 * Revision 1.15 1995/03/31 10:16:51 quinn
214 * Revision 1.14 1995/03/31 09:18:58 quinn
217 * Revision 1.13 1995/03/30 16:08:39 quinn
220 * Revision 1.12 1995/03/30 13:29:02 quinn
223 * Revision 1.11 1995/03/30 12:18:17 quinn
226 * Revision 1.10 1995/03/29 15:40:16 quinn
227 * Ongoing work. Statserv is now dynamic by default
229 * Revision 1.9 1995/03/27 08:34:30 quinn
230 * Added dynamic server functionality.
231 * Released bindings to session.c (is now redundant)
233 * Revision 1.8 1995/03/20 09:46:26 quinn
236 * Revision 1.7 1995/03/16 13:29:04 quinn
237 * Partitioned server.
239 * Revision 1.6 1995/03/15 15:18:52 quinn
240 * Little changes to better support nonblocking I/O
243 * Revision 1.5 1995/03/15 08:37:45 quinn
244 * Now we're pretty much set for nonblocking I/O.
246 * Revision 1.4 1995/03/14 16:59:48 quinn
249 * Revision 1.3 1995/03/14 11:30:15 quinn
252 * Revision 1.2 1995/03/14 10:28:03 quinn
253 * More work on demo server.
255 * Revision 1.1 1995/03/10 18:22:45 quinn
256 * The rudiments of an asynchronous server.
284 #include <yaz/comstack.h>
285 #include <yaz/tcpip.h>
286 #include <yaz/options.h>
288 #include <yaz/xmosi.h>
293 #include <yaz/statserv.h>
295 static IOCHAN pListener = NULL;
297 static char *me = "statserver";
301 int check_options(int argc, char **argv);
302 statserv_options_block control_block = {
303 1, /* dynamic mode */
304 0, /* threaded mode */
305 0, /* one shot (single session) */
306 LOG_DEFAULT_LEVEL, /* log level */
308 "", /* diagnostic output to stderr */
309 "tcp:@:9999", /* default listener port */
310 PROTO_Z3950, /* default application protocol */
311 60, /* idle timeout (minutes) */
312 1024*1024, /* maximum PDU size (approx.) to allow */
313 "default-config", /* configuration name to pass to backend */
314 "", /* set user id */
315 0, /* bend_start handler */
316 0, /* bend_stop handler */
317 check_options, /* Default routine, for checking the run-time arguments */
320 0, /* default value for inet deamon */
321 0, /* handle (for service, etc) */
322 0, /* bend_init handle */
323 0 /* bend_close handle */
325 ,"Z39.50 Server", /* NT Service Name */
326 "Server", /* NT application Name */
327 "", /* NT Service Dependencies */
328 "Z39.50 Server" /* NT Service Display Name */
333 * handle incoming connect requests.
334 * The dynamic mode is a bit tricky mostly because we want to avoid
335 * doing all of the listening and accepting in the parent - it's
340 typedef struct _ThreadList ThreadList;
349 static ThreadList *pFirstThread;
350 static CRITICAL_SECTION Thread_CritSect;
351 static BOOL bInitialized = FALSE;
353 static void ThreadList_Initialize()
355 /* Initialize the critical Sections */
356 InitializeCriticalSection(&Thread_CritSect);
358 /* Set the first thraed */
361 /* we have been initialized */
365 static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
367 /* Only one thread can go through this section at a time */
368 EnterCriticalSection(&Thread_CritSect);
371 /* Lets create our new object */
372 ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
373 pNewThread->hThread = hThread;
374 pNewThread->pIOChannel = pIOChannel;
375 pNewThread->pNext = pFirstThread;
376 pFirstThread = pNewThread;
378 /* Lets let somebody else create a new object now */
379 LeaveCriticalSection(&Thread_CritSect);
383 void statserv_remove(IOCHAN pIOChannel)
385 /* Only one thread can go through this section at a time */
386 EnterCriticalSection(&Thread_CritSect);
389 ThreadList *pCurrentThread = pFirstThread;
390 ThreadList *pNextThread;
391 ThreadList *pPrevThread =NULL;
393 /* Step through alll the threads */
394 for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
396 /* We only need to compare on the IO Channel */
397 if (pCurrentThread->pIOChannel == pIOChannel)
399 /* We have found the thread we want to delete */
400 /* First of all reset the next pointers */
401 if (pPrevThread == NULL)
402 pFirstThread = pCurrentThread->pNext;
404 pPrevThread->pNext = pCurrentThread->pNext;
406 /* All we need todo now is delete the memory */
407 free(pCurrentThread);
409 /* No need to look at any more threads */
414 /* We need to look at another thread */
415 pNextThread = pCurrentThread->pNext;
416 pPrevThread = pCurrentThread;
420 /* Lets let somebody else remove an object now */
421 LeaveCriticalSection(&Thread_CritSect);
425 /* WIN32 statserv_closedown */
426 void statserv_closedown()
428 /* Shouldn't do anything if we are not initialized */
432 HANDLE *pThreadHandles = NULL;
434 /* We need to stop threads adding and removing while we */
435 /* start the closedown process */
436 EnterCriticalSection(&Thread_CritSect);
439 /* We have exclusive access to the thread stuff now */
440 /* Y didn't i use a semaphore - Oh well never mind */
441 ThreadList *pCurrentThread = pFirstThread;
443 /* Before we do anything else, we need to shutdown the listener */
444 if (pListener != NULL)
445 iochan_destroy(pListener);
447 for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
449 /* Just destroy the IOCHAN, that should do the trick */
450 iochan_destroy(pCurrentThread->pIOChannel);
451 closesocket(pCurrentThread->pIOChannel->fd);
453 /* Keep a running count of our handles */
459 HANDLE *pCurrentHandle ;
461 /* Allocate the thread handle array */
462 pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
463 pCurrentHandle = pThreadHandles;
465 for (pCurrentThread = pFirstThread;
466 pCurrentThread != NULL;
467 pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
469 /* Just the handle */
470 *pCurrentHandle = pCurrentThread->hThread;
474 /* We can now leave the critical section */
475 LeaveCriticalSection(&Thread_CritSect);
478 /* Now we can really do something */
481 logf (LOG_LOG, "waiting for %d to die", iHandles);
482 /* This will now wait, until all the threads close */
483 WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
485 /* Free the memory we allocated for the handle array */
486 free(pThreadHandles);
489 if (control_block.bend_stop)
490 (*control_block.bend_stop)(&control_block);
491 /* No longer require the critical section, since all threads are dead */
492 DeleteCriticalSection(&Thread_CritSect);
496 void event_loop_thread (IOCHAN iochan)
498 event_loop (&iochan);
502 static void listener(IOCHAN h, int event)
504 COMSTACK line = (COMSTACK) iochan_getdata(h);
509 if (event == EVENT_INPUT)
511 if ((res = cs_listen(line, 0, 0)) < 0)
513 yaz_log(LOG_FATAL, "cs_listen failed");
518 yaz_log(LOG_DEBUG, "listen ok");
519 iochan_setevent(h, EVENT_OUTPUT);
520 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
522 else if (event == EVENT_OUTPUT)
524 COMSTACK new_line = cs_accept(line);
530 yaz_log(LOG_FATAL, "Accept failed.");
531 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT);
534 yaz_log(LOG_DEBUG, "Accept ok");
536 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
539 yaz_log(LOG_FATAL, "Failed to create iochan");
544 yaz_log(LOG_DEBUG, "Creating association");
545 if (!(newas = create_association(new_chan, new_line)))
547 yaz_log(LOG_FATAL, "Failed to create new assoc.");
551 newas->cs_get_mask = EVENT_INPUT;
552 newas->cs_put_mask = 0;
553 newas->cs_accept_mask = 0;
555 yaz_log(LOG_DEBUG, "Setting timeout %d", control_block.idle_timeout);
556 iochan_setdata(new_chan, newas);
557 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
559 /* Now what we need todo is create a new thread with this iochan as
561 newHandle = (HANDLE) _beginthread(event_loop_thread, 0, new_chan);
562 if (newHandle == (HANDLE) -1)
565 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create new thread.");
569 /* We successfully created the thread, so add it to the list */
570 statserv_add(newHandle, new_chan);
572 yaz_log(LOG_DEBUG, "Created new thread, id = %ld iochan %p",(long) newHandle, new_chan);
573 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
577 yaz_log(LOG_FATAL, "Bad event on listener.");
585 /* To save having an #ifdef in event_loop we need to define this empty function */
586 void statserv_remove(IOCHAN pIOChannel)
590 void statserv_closedown()
594 if (control_block.bend_stop)
595 (*control_block.bend_stop)(&control_block);
597 for (p = pListener; p; p = p->next)
601 void sigterm(int sig)
603 statserv_closedown();
607 static void *new_session (void *vp);
608 static int no_sessions = 0;
611 static void listener(IOCHAN h, int event)
613 COMSTACK line = (COMSTACK) iochan_getdata(h);
615 static int child = 0;
618 if (event == EVENT_INPUT)
620 if (control_block.dynamic && !child)
627 yaz_log(LOG_FATAL|LOG_ERRNO, "pipe");
631 if ((res = fork()) < 0)
633 yaz_log(LOG_FATAL|LOG_ERRNO, "fork");
637 else if (res == 0) /* child */
644 for (pp = pListener; pp; pp = iochan_getnext(pp))
648 COMSTACK l = (COMSTACK)iochan_getdata(pp);
653 sprintf(nbuf, "%s(%d)", me, getpid());
654 yaz_log_init(control_block.loglevel, nbuf, 0);
659 /* wait for child to take the call */
665 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
667 yaz_log(LOG_FATAL|LOG_ERRNO, "handshake read");
673 yaz_log(LOG_DEBUG, "P: Child has taken the call");
678 if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
679 control_block.daemon_name)) < 0)
681 yaz_log(LOG_WARN, "cs_listen failed");
686 yaz_log(LOG_DEBUG, "listen ok");
687 iochan_setevent(h, EVENT_OUTPUT);
688 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
690 /* in dynamic mode, only the child ever comes down here */
691 else if (event == EVENT_OUTPUT)
693 COMSTACK new_line = cs_accept(line);
697 yaz_log(LOG_FATAL, "Accept failed.");
698 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
701 yaz_log(LOG_DEBUG, "accept ok");
702 if (control_block.dynamic)
705 /* close our half of the listener socket */
706 for (pp = pListener; pp; pp = iochan_getnext(pp))
708 COMSTACK l = (COMSTACK)iochan_getdata(pp);
713 yaz_log(LOG_DEBUG, "Releasing parent");
718 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
723 if (control_block.threads)
725 pthread_t child_thread;
726 pthread_create (&child_thread, 0, new_session, new_line);
727 pthread_detach (child_thread);
730 new_session(new_line);
732 if (control_block.threads)
737 attr = pth_attr_new ();
738 pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
739 pth_attr_set (attr, PTH_ATTR_STACK_SIZE, 32*1024);
740 pth_attr_set (attr, PTH_ATTR_NAME, "session");
741 yaz_log (LOG_LOG, "pth_spawn");
742 child_thread = pth_spawn (attr, new_session, new_line);
744 pth_attr_destroy (attr);
748 new_session(new_line);
752 new_session(new_line);
757 yaz_log(LOG_FATAL, "Bad event on listener.");
763 static void *new_session (void *vp)
768 COMSTACK new_line = (COMSTACK) vp;
770 unsigned cs_get_mask, cs_accept_mask, mask =
771 ((new_line->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
772 ((new_line->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
776 cs_accept_mask = mask; /* accept didn't complete */
781 cs_accept_mask = 0; /* accept completed. */
782 cs_get_mask = mask = EVENT_INPUT;
785 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session, mask)))
787 yaz_log(LOG_FATAL, "Failed to create iochan");
790 if (!(newas = create_association(new_chan, new_line)))
792 yaz_log(LOG_FATAL, "Failed to create new assoc.");
795 newas->cs_accept_mask = cs_accept_mask;
796 newas->cs_get_mask = cs_get_mask;
798 iochan_setdata(new_chan, newas);
799 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
800 a = cs_addrstr(new_line);
801 yaz_log(LOG_LOG, "Starting session %d from %s", no_sessions, a ? a : "[Unknown]");
803 if (control_block.threads)
805 event_loop(&new_chan);
809 new_chan->next = pListener;
810 pListener = new_chan;
817 static void inetd_connection(int what)
824 if ((line = cs_createbysocket(0, tcpip_type, 0, what)))
826 if ((chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
828 if ((assoc = create_association(chan, line)))
830 iochan_setdata(chan, assoc);
831 iochan_settimeout(chan, control_block.idle_timeout * 60);
832 addr = cs_addrstr(line);
833 yaz_log(LOG_LOG, "Inetd association from %s", addr ? addr : "[UNKNOWN]");
834 assoc->cs_get_mask = EVENT_INPUT;
838 yaz_log(LOG_FATAL, "Failed to create association structure");
840 chan->next = pListener;
845 yaz_log(LOG_FATAL, "Failed to create iochan");
850 yaz_log(LOG_ERRNO|LOG_FATAL, "Failed to create comstack on socket 0");
855 * Set up a listening endpoint, and give it to the event-handler.
857 static void add_listener(char *where, int what)
864 if (control_block.dynamic)
866 else if (control_block.threads)
871 yaz_log(LOG_LOG, "Adding %s %s listener on %s", mode,
872 what == PROTO_SR ? "SR" : "Z3950", where);
874 l = cs_create_host(where, 0, &ap);
877 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to listen on %s", where);
880 if (cs_bind(l, ap, CS_SERVER) < 0)
882 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
886 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
889 yaz_log(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
893 iochan_setdata(lst, l);
895 /* Ensure our listener chain is setup properly */
896 lst->next = pListener;
901 /* For windows we don't need to catch the signals */
902 static void catchchld(int num)
904 while (waitpid(-1, 0, WNOHANG) > 0)
906 signal(SIGCHLD, catchchld);
910 statserv_options_block *statserv_getcontrol(void)
912 static statserv_options_block cb;
914 memcpy(&cb, &control_block, sizeof(cb));
918 void statserv_setcontrol(statserv_options_block *block)
920 memcpy(&control_block, block, sizeof(*block));
923 int statserv_start(int argc, char **argv)
929 /* We need to initialize the thread list */
930 ThreadList_Initialize();
934 if ((me = strrchr (argv[0], '\\')))
941 if (control_block.options_func(argc, argv))
944 if (control_block.bend_start)
945 (*control_block.bend_start)(&control_block);
947 logf (LOG_LOG, "Starting server %s", me);
949 if (control_block.inetd)
950 inetd_connection(control_block.default_proto);
953 logf (LOG_LOG, "Starting server %s pid=%d", me, getpid());
955 sigset_t sigs_to_block;
957 sigemptyset(&sigs_to_block);
958 sigaddset (&sigs_to_block, SIGTERM);
959 pthread_sigmask (SIG_BLOCK, &sigs_to_block, 0);
962 if (control_block.dynamic)
963 signal(SIGCHLD, catchchld);
965 signal (SIGPIPE, SIG_IGN);
966 signal (SIGTERM, sigterm);
967 if (*control_block.setuid)
971 if (!(pw = getpwnam(control_block.setuid)))
973 yaz_log(LOG_FATAL, "%s: Unknown user", control_block.setuid);
976 if (setuid(pw->pw_uid) < 0)
978 yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
984 if ((pListener == NULL) && *control_block.default_listen)
985 add_listener(control_block.default_listen,
986 control_block.default_proto);
988 if (pListener == NULL)
992 yaz_log(LOG_LOG, "Entering event loop.");
993 ret = event_loop(&pListener);
999 int check_options(int argc, char **argv)
1004 while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:", argv, argc, &arg)) != -2)
1009 add_listener(arg, control_block.default_proto);
1012 control_block.one_shot = 1;
1013 control_block.dynamic = 0;
1016 control_block.default_proto = PROTO_Z3950;
1019 control_block.default_proto = PROTO_SR;
1022 control_block.dynamic = 0;
1026 control_block.dynamic = 0;
1027 control_block.threads = 1;
1029 fprintf(stderr, "%s: Threaded mode not available.\n", me);
1034 strcpy(control_block.logfile, arg ? arg : "");
1035 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1038 control_block.loglevel = yaz_log_mask_str(arg);
1039 yaz_log_init(control_block.loglevel, me, control_block.logfile);
1042 strcpy(control_block.apdufile, arg ? arg : "");
1045 strcpy(control_block.setuid, arg ? arg : "");
1048 strcpy(control_block.configname, arg ? arg : "");
1051 strcpy(control_block.daemon_name, arg ? arg : "");
1054 if (!arg || !(r = atoi(arg)))
1056 fprintf(stderr, "%s: Specify positive timeout for -t.\n", me);
1059 control_block.idle_timeout = r;
1062 if (!arg || !(r = atoi(arg)))
1064 fprintf(stderr, "%s: Specify positive size for -k.\n", me);
1067 control_block.maxrecordsize = r * 1024;
1070 control_block.inetd = 1;
1080 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
1081 " -l <logfile> -u <user> -c <config> -t <minutes>"
1082 " -k <kilobytes> -d <daemon>"
1083 " -zsiST -w <directory> <listender-addr>... ]\n", me);
1091 typedef struct _Args
1097 static Args ArgDetails;
1099 /* name of the executable */
1100 #define SZAPPNAME "server"
1102 /* list of service dependencies - "dep1\0dep2\0\0" */
1103 #define SZDEPENDENCIES ""
1105 int statserv_main(int argc, char **argv,
1106 bend_initresult *(*bend_init)(bend_initrequest *r),
1107 void (*bend_close)(void *handle))
1109 statserv_options_block *cb = statserv_getcontrol();
1111 cb->bend_init = bend_init;
1112 cb->bend_close = bend_close;
1114 statserv_setcontrol(cb);
1116 /* Lets setup the Arg structure */
1117 ArgDetails.argc = argc;
1118 ArgDetails.argv = argv;
1120 /* Now setup the service with the service controller */
1121 SetupService(argc, argv, &ArgDetails, SZAPPNAME,
1122 cb->service_name, /* internal service name */
1123 cb->service_name, /* displayed name of the service */
1128 int StartAppService(void *pHandle, int argc, char **argv)
1130 /* Initializes the App */
1134 void RunAppService(void *pHandle)
1136 Args *pArgs = (Args *)pHandle;
1138 /* Starts the app running */
1139 statserv_start(pArgs->argc, pArgs->argv);
1142 void StopAppService(void *pHandle)
1145 statserv_closedown();
1148 int statserv_main(int argc, char **argv,
1149 bend_initresult *(*bend_init)(bend_initrequest *r),
1150 void (*bend_close)(void *handle))
1153 statserv_options_block *cb = statserv_getcontrol();
1155 cb->bend_init = bend_init;
1156 cb->bend_close = bend_close;
1158 statserv_setcontrol(cb);
1159 ret = statserv_start (argc, argv);
1160 statserv_closedown ();