2 * Copyright 2001-2009 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * shar.cpp -- the shibd "main" code. All the functionality is elsewhere
20 * Created By: Derek Atkins <derek@ihtfp.com>
22 * $Id: shar.cpp 2164 2007-02-11 05:26:18 +0000 (Sun, 11 Feb 2007) cantor $
26 // eventually we might be able to support autoconf via cygwin...
27 #if defined (_MSC_VER) || defined(__BORLANDC__)
28 # include "config_win32.h"
34 # define _CRT_NONSTDC_NO_DEPRECATE 1
35 # define _CRT_SECURE_NO_DEPRECATE 1
38 #include <shibsp/SPConfig.h>
42 #include <sys/select.h>
47 #include <shibsp/ServiceProvider.h>
48 #include <shibsp/remoting/ListenerService.h>
49 #include <xercesc/util/XMLUniDefs.hpp>
50 #include <xmltooling/XMLToolingConfig.h>
51 #include <xmltooling/util/XMLConstants.h>
52 #include <xmltooling/util/XMLHelper.h>
54 using namespace shibsp;
55 using namespace xmltooling;
58 bool shibd_shutdown = false;
59 const char* shar_config = NULL;
60 const char* shar_schemadir = NULL;
61 const char* shar_prefix = NULL;
62 bool shar_checkonly = false;
63 bool shar_version = false;
64 static bool unlink_socket = false;
65 const char* pidfile = NULL;
71 #define nNoMansLandSize 4
72 typedef struct _CrtMemBlockHeader
74 struct _CrtMemBlockHeader * pBlockHeaderNext;
75 struct _CrtMemBlockHeader * pBlockHeaderPrev;
81 unsigned char gap[nNoMansLandSize];
83 * unsigned char data[nDataSize];
84 * unsigned char anotherGap[nNoMansLandSize];
89 int MyAllocHook(int nAllocType, void *pvData,
90 size_t nSize, int nBlockUse, long lRequest,
91 const unsigned char * szFileName, int nLine)
93 if ( nBlockUse == _CRT_BLOCK )
95 if (nAllocType == _HOOK_FREE) {
96 _CrtMemBlockHeader* ptr = (_CrtMemBlockHeader*)(((_CrtMemBlockHeader *)pvData)-1);
97 if (ptr->nDataSize == 8192)
98 fprintf(stderr,"free request %u size %u\n", ptr->lRequest, ptr->nDataSize);
100 else if (nAllocType == _HOOK_ALLOC && nSize == 8192)
101 fprintf(stderr,"%s request %u size %u\n", ((nAllocType == _HOOK_ALLOC) ? "alloc" : "realloc"), lRequest, nSize);
106 int real_main(int preinit)
108 SPConfig& conf=SPConfig::getConfig();
111 // Initialize the SP library.
117 SPConfig::Credentials |
118 SPConfig::AttributeResolution |
120 SPConfig::OutOfProcess |
121 (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)
123 if (!conf.init(shar_schemadir, shar_prefix)) {
124 fprintf(stderr, "configuration is invalid, see console for specific problems\n");
128 if (!conf.instantiate(shar_config)) {
129 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
134 // If just a test run, bail.
135 if (shar_checkonly) {
136 fprintf(stdout, "overall configuration is loadable, check console for non-fatal problems\n");
142 //_CrtSetAllocHook(MyAllocHook);
144 if (!shar_checkonly) {
146 ListenerService* listener = conf.getServiceProvider()->getListenerService();
147 if (!listener->init(unlink_socket)) {
148 fprintf(stderr, "listener failed to initialize\n");
152 else if (!listener->run(&shibd_shutdown)) {
153 fprintf(stderr, "listener failed during service\n");
169 bool shibd_running = false;
170 bool daemonize = true;
172 static void term_handler(int arg)
174 shibd_shutdown = true;
177 static void run_handler(int arg)
179 shibd_running = true;
182 static void child_handler(int arg)
184 // Terminate the parent's wait/sleep if the newly born daemon dies early.
187 static int setup_signals(void)
190 memset(&sa, 0, sizeof (sa));
191 sa.sa_handler = SIG_IGN;
192 sa.sa_flags = SA_RESTART;
194 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
198 memset(&sa, 0, sizeof (sa));
199 sa.sa_handler = term_handler;
200 sa.sa_flags = SA_RESTART;
202 if (sigaction(SIGHUP, &sa, NULL) < 0) {
205 if (sigaction(SIGINT, &sa, NULL) < 0) {
208 if (sigaction(SIGQUIT, &sa, NULL) < 0) {
211 if (sigaction(SIGTERM, &sa, NULL) < 0) {
216 memset(&sa, 0, sizeof (sa));
217 sa.sa_handler = run_handler;
219 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
223 memset(&sa, 0, sizeof (sa));
224 sa.sa_handler = child_handler;
226 if (sigaction(SIGCHLD, &sa, NULL) < 0) {
234 static void usage(char* whoami)
236 fprintf(stderr, "usage: %s [-dcxtfpvh]\n", whoami);
237 fprintf(stderr, " -d\tinstallation prefix to use.\n");
238 fprintf(stderr, " -c\tconfig file to use.\n");
239 fprintf(stderr, " -x\tXML schema catalogs to use.\n");
240 fprintf(stderr, " -t\ttest configuration file for problems.\n");
241 fprintf(stderr, " -f\tforce removal of listener socket.\n");
242 fprintf(stderr, " -F\tstay in the foreground.\n");
243 fprintf(stderr, " -p\tpid file to use.\n");
244 fprintf(stderr, " -w\tseconds to wait for successful daemonization.\n");
245 fprintf(stderr, " -v\tprint software version.\n");
246 fprintf(stderr, " -h\tprint this help message.\n");
250 static int parse_args(int argc, char* argv[])
254 while ((opt = getopt(argc, argv, "d:c:x:p:w:fFtvh")) > 0) {
263 shar_schemadir=optarg;
266 unlink_socket = true;
283 daemon_wait = atoi(optarg);
284 if (daemon_wait <= 0)
294 int main(int argc, char *argv[])
296 if (parse_args(argc, argv) != 0)
298 else if (shar_version) {
299 fprintf(stdout, PACKAGE_STRING"\n");
303 if (setup_signals() != 0)
306 // initialize the shib-target library
307 SPConfig& conf=SPConfig::getConfig();
313 SPConfig::Credentials |
314 SPConfig::AttributeResolution |
316 SPConfig::OutOfProcess |
317 (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)
319 if (!conf.init(shar_schemadir, shar_prefix)) {
320 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
325 // We must fork() early, while we're single threaded.
326 // StorageService cleanup thread is about to start.
335 exit(shibd_running ? EXIT_SUCCESS : EXIT_FAILURE);
339 if (!conf.instantiate(shar_config)) {
340 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
346 fprintf(stderr, "overall configuration is loadable, check console for non-fatal problems\n");
348 // Init the listener.
349 ListenerService* listener = conf.getServiceProvider()->getListenerService();
350 if (!listener->init(unlink_socket)) {
351 fprintf(stderr, "listener failed to initialize\n");
357 if (setsid() == -1) {
361 if (chdir("/") == -1) {
362 perror("chdir to root");
367 FILE* pidf = fopen(pidfile, "w");
369 fprintf(pidf, "%d\n", getpid());
377 freopen("/dev/null", "r", stdin);
378 freopen("/dev/null", "w", stdout);
379 freopen("/dev/null", "w", stderr);
381 // Signal our parent that we are A-OK.
382 kill(getppid(), SIGUSR1);
386 if (!listener->run(&shibd_shutdown)) {
387 fprintf(stderr, "listener failure during service\n");