2 * Copyright 2001-2007 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 bool shar_checkonly = false;
\r
62 bool shar_version = false;
\r
63 static int unlink_socket = 0;
\r
64 const char* pidfile = NULL;
\r
68 //#include <CRTDBG.H>
\r
70 #define nNoMansLandSize 4
\r
71 typedef struct _CrtMemBlockHeader
\r
73 struct _CrtMemBlockHeader * pBlockHeaderNext;
\r
74 struct _CrtMemBlockHeader * pBlockHeaderPrev;
\r
80 unsigned char gap[nNoMansLandSize];
\r
82 * unsigned char data[nDataSize];
\r
83 * unsigned char anotherGap[nNoMansLandSize];
\r
85 } _CrtMemBlockHeader;
\r
88 int MyAllocHook(int nAllocType, void *pvData,
\r
89 size_t nSize, int nBlockUse, long lRequest,
\r
90 const unsigned char * szFileName, int nLine)
\r
92 if ( nBlockUse == _CRT_BLOCK )
\r
94 if (nAllocType == _HOOK_FREE) {
\r
95 _CrtMemBlockHeader* ptr = (_CrtMemBlockHeader*)(((_CrtMemBlockHeader *)pvData)-1);
\r
96 if (ptr->nDataSize == 8192)
\r
97 fprintf(stderr,"free request %u size %u\n", ptr->lRequest, ptr->nDataSize);
\r
99 else if (nAllocType == _HOOK_ALLOC && nSize == 8192)
\r
100 fprintf(stderr,"%s request %u size %u\n", ((nAllocType == _HOOK_ALLOC) ? "alloc" : "realloc"), lRequest, nSize);
\r
105 int real_main(int preinit)
\r
107 SPConfig& conf=SPConfig::getConfig();
\r
110 // Initialize the SP library.
\r
112 SPConfig::Listener |
\r
113 SPConfig::Caching |
\r
114 SPConfig::Metadata |
\r
116 SPConfig::Credentials |
\r
117 SPConfig::AttributeResolution |
\r
118 SPConfig::Handlers |
\r
119 SPConfig::OutOfProcess |
\r
120 (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)
\r
123 shar_config=getenv("SHIBSP_CONFIG");
\r
124 if (!shar_schemadir)
\r
125 shar_schemadir=getenv("SHIBSP_SCHEMAS");
\r
126 if (!shar_schemadir)
\r
127 shar_schemadir=SHIBSP_SCHEMAS;
\r
129 shar_config=SHIBSP_CONFIG;
\r
130 if (!conf.init(shar_schemadir)) {
\r
131 fprintf(stderr, "configuration is invalid, see console for specific problems\n");
\r
136 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
\r
137 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
\r
138 xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();
\r
139 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
\r
140 xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,path);
\r
141 auto_ptr_XMLCh src(shar_config);
\r
142 dummy->setAttributeNS(NULL,path,src.get());
\r
143 dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);
\r
145 conf.setServiceProvider(conf.ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
\r
146 conf.getServiceProvider()->init();
\r
148 catch (exception& ex) {
\r
149 fprintf(stderr, "caught exception while loading configuration: %s\n", ex.what());
\r
154 // If just a test run, bail.
\r
155 if (shar_checkonly) {
\r
156 fprintf(stdout, "overall configuration is loadable, check console for non-fatal problems\n");
\r
162 //_CrtSetAllocHook(MyAllocHook);
\r
164 // Run the listener
\r
165 if (!shar_checkonly) {
\r
167 // Run the listener.
\r
168 if (!conf.getServiceProvider()->getListenerService()->run(&shibd_shutdown)) {
\r
169 fprintf(stderr, "listener failed to enter listen loop\n");
\r
181 static void term_handler(int arg)
\r
183 shibd_shutdown = true;
\r
186 static int setup_signals(void)
\r
188 struct sigaction sa;
\r
189 memset(&sa, 0, sizeof (sa));
\r
190 sa.sa_handler = SIG_IGN;
\r
191 sa.sa_flags = SA_RESTART;
\r
193 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
\r
197 memset(&sa, 0, sizeof (sa));
\r
198 sa.sa_handler = term_handler;
\r
199 sa.sa_flags = SA_RESTART;
\r
201 if (sigaction(SIGHUP, &sa, NULL) < 0) {
\r
204 if (sigaction(SIGINT, &sa, NULL) < 0) {
\r
207 if (sigaction(SIGQUIT, &sa, NULL) < 0) {
\r
210 if (sigaction(SIGTERM, &sa, NULL) < 0) {
\r
216 static void usage(char* whoami)
\r
218 fprintf(stderr, "usage: %s [-fcdt]\n", whoami);
\r
219 fprintf(stderr, " -c\tconfig file to use.\n");
\r
220 fprintf(stderr, " -x\tXML schema catalogs to use.\n");
\r
221 fprintf(stderr, " -t\tcheck configuration file for problems.\n");
\r
222 fprintf(stderr, " -f\tforce removal of listener socket.\n");
\r
223 fprintf(stderr, " -p\tpid file to use.\n");
\r
224 fprintf(stderr, " -v\tprint software version.\n");
\r
225 fprintf(stderr, " -h\tprint this help message.\n");
\r
229 static int parse_args(int argc, char* argv[])
\r
233 while ((opt = getopt(argc, argv, "c:x:p:ftvh")) > 0) {
\r
236 shar_config=optarg;
\r
239 shar_schemadir=optarg;
\r
245 shar_checkonly=true;
\r
260 int main(int argc, char *argv[])
\r
262 if (parse_args(argc, argv) != 0)
\r
264 else if (shar_version) {
\r
265 fprintf(stdout, PACKAGE_STRING);
\r
269 if (setup_signals() != 0)
\r
273 shar_config=getenv("SHIBSP_CONFIG");
\r
274 if (!shar_schemadir)
\r
275 shar_schemadir=getenv("SHIBSP_SCHEMAS");
\r
276 if (!shar_schemadir)
\r
277 shar_schemadir=SHIBSP_SCHEMAS;
\r
279 shar_config=SHIBSP_CONFIG;
\r
281 // initialize the shib-target library
\r
282 SPConfig& conf=SPConfig::getConfig();
\r
284 SPConfig::Listener |
\r
285 SPConfig::Caching |
\r
286 SPConfig::Metadata |
\r
288 SPConfig::Credentials |
\r
289 SPConfig::AttributeResolution |
\r
290 SPConfig::Handlers |
\r
291 SPConfig::OutOfProcess |
\r
292 (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)
\r
294 if (!conf.init(shar_schemadir)) {
\r
295 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
\r
300 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
\r
301 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
\r
302 xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();
\r
303 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
\r
304 xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,path);
\r
305 auto_ptr_XMLCh src(shar_config);
\r
306 dummy->setAttributeNS(NULL,path,src.get());
\r
307 dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);
\r
309 conf.setServiceProvider(conf.ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
\r
310 conf.getServiceProvider()->init();
\r
312 catch (exception& ex) {
\r
313 fprintf(stderr, "caught exception while loading configuration: %s\n", ex.what());
\r
318 if (shar_checkonly)
\r
319 fprintf(stderr, "overall configuration is loadable, check console for non-fatal problems\n");
\r
322 // Write the pid file
\r
324 FILE* pidf = fopen(pidfile, "w");
\r
326 fprintf(pidf, "%d\n", getpid());
\r
329 perror(pidfile); // keep running though
\r
333 // Run the listener
\r
334 if (!conf.getServiceProvider()->getListenerService()->run(&shibd_shutdown)) {
\r
335 fprintf(stderr, "listener failed to enter listen loop\n");
\r