2 * Copyright 2001-2009 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * shar.cpp -- the shibd "main" code. All the functionality is elsewhere
\r
20 * Created By: Derek Atkins <derek@ihtfp.com>
\r
22 * $Id: shar.cpp 2164 2007-02-11 05:26:18 +0000 (Sun, 11 Feb 2007) cantor $
\r
26 // eventually we might be able to support autoconf via cygwin...
\r
27 #if defined (_MSC_VER) || defined(__BORLANDC__)
\r
28 # include "config_win32.h"
\r
30 # include "config.h"
\r
34 # define _CRT_NONSTDC_NO_DEPRECATE 1
\r
35 # define _CRT_SECURE_NO_DEPRECATE 1
\r
38 #include <shibsp/SPConfig.h>
\r
40 #ifdef HAVE_UNISTD_H
\r
42 #include <sys/select.h>
\r
47 #include <shibsp/ServiceProvider.h>
\r
48 #include <shibsp/remoting/ListenerService.h>
\r
49 #include <xercesc/util/XMLUniDefs.hpp>
\r
50 #include <xmltooling/XMLToolingConfig.h>
\r
51 #include <xmltooling/util/XMLConstants.h>
\r
52 #include <xmltooling/util/XMLHelper.h>
\r
54 using namespace shibsp;
\r
55 using namespace xmltooling;
\r
56 using namespace std;
\r
58 bool shibd_shutdown = false;
\r
59 const char* shar_config = NULL;
\r
60 const char* shar_schemadir = NULL;
\r
61 const char* shar_prefix = NULL;
\r
62 bool shar_checkonly = false;
\r
63 bool shar_version = false;
\r
64 static bool unlink_socket = false;
\r
65 const char* pidfile = NULL;
\r
69 //#include <CRTDBG.H>
\r
71 #define nNoMansLandSize 4
\r
72 typedef struct _CrtMemBlockHeader
\r
74 struct _CrtMemBlockHeader * pBlockHeaderNext;
\r
75 struct _CrtMemBlockHeader * pBlockHeaderPrev;
\r
81 unsigned char gap[nNoMansLandSize];
\r
83 * unsigned char data[nDataSize];
\r
84 * unsigned char anotherGap[nNoMansLandSize];
\r
86 } _CrtMemBlockHeader;
\r
89 int MyAllocHook(int nAllocType, void *pvData,
\r
90 size_t nSize, int nBlockUse, long lRequest,
\r
91 const unsigned char * szFileName, int nLine)
\r
93 if ( nBlockUse == _CRT_BLOCK )
\r
95 if (nAllocType == _HOOK_FREE) {
\r
96 _CrtMemBlockHeader* ptr = (_CrtMemBlockHeader*)(((_CrtMemBlockHeader *)pvData)-1);
\r
97 if (ptr->nDataSize == 8192)
\r
98 fprintf(stderr,"free request %u size %u\n", ptr->lRequest, ptr->nDataSize);
\r
100 else if (nAllocType == _HOOK_ALLOC && nSize == 8192)
\r
101 fprintf(stderr,"%s request %u size %u\n", ((nAllocType == _HOOK_ALLOC) ? "alloc" : "realloc"), lRequest, nSize);
\r
106 int real_main(int preinit)
\r
108 SPConfig& conf=SPConfig::getConfig();
\r
111 // Initialize the SP library.
\r
113 SPConfig::Listener |
\r
114 SPConfig::Caching |
\r
115 SPConfig::Metadata |
\r
117 SPConfig::Credentials |
\r
118 SPConfig::AttributeResolution |
\r
119 SPConfig::Handlers |
\r
120 SPConfig::OutOfProcess |
\r
121 (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)
\r
123 if (!conf.init(shar_schemadir, shar_prefix)) {
\r
124 fprintf(stderr, "configuration is invalid, see console for specific problems\n");
\r
128 if (!conf.instantiate(shar_config)) {
\r
129 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
\r
134 // If just a test run, bail.
\r
135 if (shar_checkonly) {
\r
136 fprintf(stdout, "overall configuration is loadable, check console for non-fatal problems\n");
\r
142 //_CrtSetAllocHook(MyAllocHook);
\r
144 if (!shar_checkonly) {
\r
145 // Run the listener.
\r
146 ListenerService* listener = conf.getServiceProvider()->getListenerService();
\r
147 if (!listener->init(unlink_socket)) {
\r
148 fprintf(stderr, "listener failed to initialize\n");
\r
151 else if (!listener->run(&shibd_shutdown)) {
\r
153 fprintf(stderr, "listener failed to begin service\n");
\r
166 int daemon_wait = 3;
\r
167 bool shibd_running = false;
\r
168 bool daemonize = true;
\r
170 static void term_handler(int arg)
\r
172 shibd_shutdown = true;
\r
175 static void run_handler(int arg)
\r
177 shibd_running = true;
\r
180 static void child_handler(int arg)
\r
182 // Terminate the parent's wait/sleep if the newly born daemon dies early.
\r
185 static int setup_signals(void)
\r
187 struct sigaction sa;
\r
188 memset(&sa, 0, sizeof (sa));
\r
189 sa.sa_handler = SIG_IGN;
\r
190 sa.sa_flags = SA_RESTART;
\r
192 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
\r
196 memset(&sa, 0, sizeof (sa));
\r
197 sa.sa_handler = term_handler;
\r
198 sa.sa_flags = SA_RESTART;
\r
200 if (sigaction(SIGHUP, &sa, NULL) < 0) {
\r
203 if (sigaction(SIGINT, &sa, NULL) < 0) {
\r
206 if (sigaction(SIGQUIT, &sa, NULL) < 0) {
\r
209 if (sigaction(SIGTERM, &sa, NULL) < 0) {
\r
214 memset(&sa, 0, sizeof (sa));
\r
215 sa.sa_handler = run_handler;
\r
217 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
\r
221 memset(&sa, 0, sizeof (sa));
\r
222 sa.sa_handler = child_handler;
\r
224 if (sigaction(SIGCHLD, &sa, NULL) < 0) {
\r
232 static void usage(char* whoami)
\r
234 fprintf(stderr, "usage: %s [-dcxtfpvh]\n", whoami);
\r
235 fprintf(stderr, " -d\tinstallation prefix to use.\n");
\r
236 fprintf(stderr, " -c\tconfig file to use.\n");
\r
237 fprintf(stderr, " -x\tXML schema catalogs to use.\n");
\r
238 fprintf(stderr, " -t\ttest configuration file for problems.\n");
\r
239 fprintf(stderr, " -f\tforce removal of listener socket.\n");
\r
240 fprintf(stderr, " -F\tstay in the foreground.\n");
\r
241 fprintf(stderr, " -p\tpid file to use.\n");
\r
242 fprintf(stderr, " -w\tseconds to wait for successful daemonization.\n");
\r
243 fprintf(stderr, " -v\tprint software version.\n");
\r
244 fprintf(stderr, " -h\tprint this help message.\n");
\r
248 static int parse_args(int argc, char* argv[])
\r
252 while ((opt = getopt(argc, argv, "d:c:x:p:w:fFtvh")) > 0) {
\r
255 shar_prefix=optarg;
\r
258 shar_config=optarg;
\r
261 shar_schemadir=optarg;
\r
264 unlink_socket = true;
\r
270 shar_checkonly=true;
\r
281 daemon_wait = atoi(optarg);
\r
282 if (daemon_wait <= 0)
\r
292 int main(int argc, char *argv[])
\r
294 if (parse_args(argc, argv) != 0)
\r
296 else if (shar_version) {
\r
297 fprintf(stdout, PACKAGE_STRING"\n");
\r
301 if (setup_signals() != 0)
\r
304 // initialize the shib-target library
\r
305 SPConfig& conf=SPConfig::getConfig();
\r
307 SPConfig::Listener |
\r
308 SPConfig::Caching |
\r
309 SPConfig::Metadata |
\r
311 SPConfig::Credentials |
\r
312 SPConfig::AttributeResolution |
\r
313 SPConfig::Handlers |
\r
314 SPConfig::OutOfProcess |
\r
315 (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)
\r
317 if (!conf.init(shar_schemadir, shar_prefix)) {
\r
318 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
\r
323 // We must fork() early, while we're single threaded.
\r
324 // StorageService cleanup thread is about to start.
\r
325 pid_t pid = fork();
\r
331 exit(EXIT_FAILURE);
\r
333 sleep(daemon_wait);
\r
334 if (shibd_running) {
\r
336 FILE* pidf = fopen(pidfile, "w");
\r
338 fprintf(pidf, "%d\n", pid);
\r
345 exit(EXIT_SUCCESS);
\r
348 exit(EXIT_FAILURE);
\r
353 if (!conf.instantiate(shar_config)) {
\r
354 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
\r
359 if (shar_checkonly)
\r
360 fprintf(stderr, "overall configuration is loadable, check console for non-fatal problems\n");
\r
363 if (setsid() == -1) {
\r
365 exit(EXIT_FAILURE);
\r
367 if (chdir("/") == -1) {
\r
368 perror("chdir to root");
\r
369 exit(EXIT_FAILURE);
\r
374 freopen("/dev/null", "r", stdin);
\r
375 freopen("/dev/null", "w", stdout);
\r
376 freopen("/dev/null", "w", stderr);
\r
379 // Init the listener.
\r
380 ListenerService* listener = conf.getServiceProvider()->getListenerService();
\r
381 if (!listener->init(unlink_socket)) {
\r
382 fprintf(stderr, "listener failed to initialize\n");
\r
386 // Signal our parent.
\r
388 pid_t ppid = getppid();
\r
389 kill(ppid, SIGUSR1);
\r
392 // Run the listener.
\r
393 if (!listener->run(&shibd_shutdown)) {
\r
395 fprintf(stderr, "listener failed to begin service\n");
\r