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 fprintf(stderr, "loading configuration file: %s\n", shar_config);
\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, " -h\tprint this help message.\n");
\r
228 static int parse_args(int argc, char* argv[])
\r
232 while ((opt = getopt(argc, argv, "c:x:p:fth")) > 0) {
\r
235 shar_config=optarg;
\r
238 shar_schemadir=optarg;
\r
244 shar_checkonly=true;
\r
256 int main(int argc, char *argv[])
\r
258 if (setup_signals() != 0)
\r
261 if (parse_args(argc, argv) != 0)
\r
265 shar_config=getenv("SHIBSP_CONFIG");
\r
266 if (!shar_schemadir)
\r
267 shar_schemadir=getenv("SHIBSP_SCHEMAS");
\r
268 if (!shar_schemadir)
\r
269 shar_schemadir=SHIBSP_SCHEMAS;
\r
271 shar_config=SHIBSP_CONFIG;
\r
273 // initialize the shib-target library
\r
274 SPConfig& conf=SPConfig::getConfig();
\r
276 SPConfig::Listener |
\r
277 SPConfig::Caching |
\r
278 SPConfig::Metadata |
\r
280 SPConfig::Credentials |
\r
281 SPConfig::AttributeResolution |
\r
282 SPConfig::Handlers |
\r
283 SPConfig::OutOfProcess |
\r
284 (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)
\r
286 if (!conf.init(shar_schemadir)) {
\r
287 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
\r
292 fprintf(stderr, "loading configuration file: %s\n", shar_config);
\r
293 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
\r
294 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
\r
295 xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();
\r
296 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
\r
297 xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,path);
\r
298 auto_ptr_XMLCh src(shar_config);
\r
299 dummy->setAttributeNS(NULL,path,src.get());
\r
300 dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);
\r
302 conf.setServiceProvider(conf.ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
\r
303 conf.getServiceProvider()->init();
\r
305 catch (exception& ex) {
\r
306 fprintf(stderr, "caught exception while loading configuration: %s\n", ex.what());
\r
311 if (shar_checkonly)
\r
312 fprintf(stderr, "overall configuration is loadable, check console for non-fatal problems\n");
\r
315 // Write the pid file
\r
317 FILE* pidf = fopen(pidfile, "w");
\r
319 fprintf(pidf, "%d\n", getpid());
\r
322 perror(pidfile); // keep running though
\r
326 // Run the listener
\r
327 if (!conf.getServiceProvider()->getListenerService()->run(&shibd_shutdown)) {
\r
328 fprintf(stderr, "listener failed to enter listen loop\n");
\r