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 static int unlink_socket = 0;
\r
63 const char* pidfile = NULL;
\r
67 //#include <CRTDBG.H>
\r
69 #define nNoMansLandSize 4
\r
70 typedef struct _CrtMemBlockHeader
\r
72 struct _CrtMemBlockHeader * pBlockHeaderNext;
\r
73 struct _CrtMemBlockHeader * pBlockHeaderPrev;
\r
79 unsigned char gap[nNoMansLandSize];
\r
81 * unsigned char data[nDataSize];
\r
82 * unsigned char anotherGap[nNoMansLandSize];
\r
84 } _CrtMemBlockHeader;
\r
87 int MyAllocHook(int nAllocType, void *pvData,
\r
88 size_t nSize, int nBlockUse, long lRequest,
\r
89 const unsigned char * szFileName, int nLine)
\r
91 if ( nBlockUse == _CRT_BLOCK )
\r
93 if (nAllocType == _HOOK_FREE) {
\r
94 _CrtMemBlockHeader* ptr = (_CrtMemBlockHeader*)(((_CrtMemBlockHeader *)pvData)-1);
\r
95 if (ptr->nDataSize == 8192)
\r
96 fprintf(stderr,"free request %u size %u\n", ptr->lRequest, ptr->nDataSize);
\r
98 else if (nAllocType == _HOOK_ALLOC && nSize == 8192)
\r
99 fprintf(stderr,"%s request %u size %u\n", ((nAllocType == _HOOK_ALLOC) ? "alloc" : "realloc"), lRequest, nSize);
\r
104 int real_main(int preinit)
\r
106 SPConfig& conf=SPConfig::getConfig();
\r
109 // Initialize the SP library.
\r
111 SPConfig::Listener |
\r
112 SPConfig::Caching |
\r
113 SPConfig::Metadata |
\r
115 SPConfig::Credentials |
\r
116 SPConfig::AttributeResolution |
\r
117 SPConfig::Handlers |
\r
118 SPConfig::OutOfProcess |
\r
119 (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)
\r
122 shar_config=getenv("SHIBSP_CONFIG");
\r
123 if (!shar_schemadir)
\r
124 shar_schemadir=getenv("SHIBSP_SCHEMAS");
\r
125 if (!shar_schemadir)
\r
126 shar_schemadir=SHIBSP_SCHEMAS;
\r
128 shar_config=SHIBSP_CONFIG;
\r
129 if (!conf.init(shar_schemadir)) {
\r
130 fprintf(stderr, "configuration is invalid, see console for specific problems\n");
\r
135 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
\r
136 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
\r
137 xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();
\r
138 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
\r
139 xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,path);
\r
140 auto_ptr_XMLCh src(shar_config);
\r
141 dummy->setAttributeNS(NULL,path,src.get());
\r
142 dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);
\r
144 conf.setServiceProvider(conf.ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
\r
145 conf.getServiceProvider()->init();
\r
147 catch (exception& ex) {
\r
148 fprintf(stderr, "caught exception while loading configuration: %s\n", ex.what());
\r
153 // If just a test run, bail.
\r
154 if (shar_checkonly) {
\r
155 fprintf(stdout, "overall configuration is loadable, check console for non-fatal problems\n");
\r
161 //_CrtSetAllocHook(MyAllocHook);
\r
163 // Run the listener
\r
164 if (!shar_checkonly) {
\r
166 // Run the listener.
\r
167 if (!conf.getServiceProvider()->getListenerService()->run(&shibd_shutdown)) {
\r
168 fprintf(stderr, "listener failed to enter listen loop\n");
\r
180 static void term_handler(int arg)
\r
182 shibd_shutdown = true;
\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
215 static void usage(char* whoami)
\r
217 fprintf(stderr, "usage: %s [-fcdt]\n", whoami);
\r
218 fprintf(stderr, " -c\tconfig file to use.\n");
\r
219 fprintf(stderr, " -x\tXML schema catalogs to use.\n");
\r
220 fprintf(stderr, " -t\tcheck configuration file for problems.\n");
\r
221 fprintf(stderr, " -f\tforce removal of listener socket.\n");
\r
222 fprintf(stderr, " -p\tpid file to use.\n");
\r
223 fprintf(stderr, " -h\tprint this help message.\n");
\r
227 static int parse_args(int argc, char* argv[])
\r
231 while ((opt = getopt(argc, argv, "c:x:p:fth")) > 0) {
\r
234 shar_config=optarg;
\r
237 shar_schemadir=optarg;
\r
243 shar_checkonly=true;
\r
255 int main(int argc, char *argv[])
\r
257 if (setup_signals() != 0)
\r
260 if (parse_args(argc, argv) != 0)
\r
264 shar_config=getenv("SHIBSP_CONFIG");
\r
265 if (!shar_schemadir)
\r
266 shar_schemadir=getenv("SHIBSP_SCHEMAS");
\r
267 if (!shar_schemadir)
\r
268 shar_schemadir=SHIBSP_SCHEMAS;
\r
270 shar_config=SHIBSP_CONFIG;
\r
272 // initialize the shib-target library
\r
273 SPConfig& conf=SPConfig::getConfig();
\r
275 SPConfig::Listener |
\r
276 SPConfig::Caching |
\r
277 SPConfig::Metadata |
\r
279 SPConfig::Credentials |
\r
280 SPConfig::AttributeResolution |
\r
281 SPConfig::Handlers |
\r
282 SPConfig::OutOfProcess |
\r
283 (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)
\r
285 if (!conf.init(shar_schemadir)) {
\r
286 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
\r
291 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
\r
292 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
\r
293 xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();
\r
294 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
\r
295 xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,path);
\r
296 auto_ptr_XMLCh src(shar_config);
\r
297 dummy->setAttributeNS(NULL,path,src.get());
\r
298 dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);
\r
300 conf.setServiceProvider(conf.ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
\r
301 conf.getServiceProvider()->init();
\r
303 catch (exception& ex) {
\r
304 fprintf(stderr, "caught exception while loading configuration: %s\n", ex.what());
\r
309 if (shar_checkonly)
\r
310 fprintf(stderr, "overall configuration is loadable, check console for non-fatal problems\n");
\r
313 // Write the pid file
\r
315 FILE* pidf = fopen(pidfile, "w");
\r
317 fprintf(pidf, "%d\n", getpid());
\r
320 perror(pidfile); // keep running though
\r
324 // Run the listener
\r
325 if (!conf.getServiceProvider()->getListenerService()->run(&shibd_shutdown)) {
\r
326 fprintf(stderr, "listener failed to enter listen loop\n");
\r