2 * Copyright 2001-2005 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>
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 <shib-target/shib-target.h>
39 #include <shibsp/SPConfig.h>
43 #include <sys/select.h>
48 #include <log4cpp/Category.hh>
50 using namespace shibsp;
51 using namespace shibtarget;
52 using namespace shibboleth;
54 using namespace log4cpp;
57 bool shibd_shutdown = false;
58 const char* shar_config = NULL;
59 const char* shar_schemadir = NULL;
60 bool shar_checkonly = false;
61 static int unlink_socket = 0;
62 const char* pidfile = NULL;
68 #define nNoMansLandSize 4
69 typedef struct _CrtMemBlockHeader
71 struct _CrtMemBlockHeader * pBlockHeaderNext;
72 struct _CrtMemBlockHeader * pBlockHeaderPrev;
78 unsigned char gap[nNoMansLandSize];
80 * unsigned char data[nDataSize];
81 * unsigned char anotherGap[nNoMansLandSize];
86 int MyAllocHook(int nAllocType, void *pvData,
87 size_t nSize, int nBlockUse, long lRequest,
88 const unsigned char * szFileName, int nLine)
90 if ( nBlockUse == _CRT_BLOCK )
92 if (nAllocType == _HOOK_FREE) {
93 _CrtMemBlockHeader* ptr = (_CrtMemBlockHeader*)(((_CrtMemBlockHeader *)pvData)-1);
94 if (ptr->nDataSize == 8192)
95 fprintf(stderr,"free request %u size %u\n", ptr->lRequest, ptr->nDataSize);
97 else if (nAllocType == _HOOK_ALLOC && nSize == 8192)
98 fprintf(stderr,"%s request %u size %u\n", ((nAllocType == _HOOK_ALLOC) ? "alloc" : "realloc"), lRequest, nSize);
103 int real_main(int preinit)
105 ShibTargetConfig& conf=ShibTargetConfig::getConfig();
108 // initialize the shib-target library
109 SPConfig::getConfig().setFeatures(
114 SPConfig::Credentials |
116 SPConfig::OutOfProcess |
117 (shar_checkonly ? (SPConfig::InProcess | SPConfig::RequestMapper) : SPConfig::Logging)
120 shar_config=getenv("SHIBCONFIG");
122 shar_schemadir=getenv("SHIBSCHEMAS");
124 shar_schemadir=SHIB_SCHEMAS;
126 shar_config=SHIB_CONFIG;
127 if (!conf.init(shar_schemadir) || !conf.load(shar_config)) {
128 fprintf(stderr, "configuration is invalid, see console for specific problems\n");
132 // If just a test run, bail.
133 if (shar_checkonly) {
134 fprintf(stdout, "overall configuration is loadable, check console for non-fatal problems\n");
140 //_CrtSetAllocHook(MyAllocHook);
143 if (!shar_checkonly) {
146 if (!conf.getINI()->getListener()->run(&shibd_shutdown)) {
147 fprintf(stderr, "listener failed to enter listen loop\n");
159 static void term_handler(int arg)
161 shibd_shutdown = true;
164 static int setup_signals(void)
167 memset(&sa, 0, sizeof (sa));
168 sa.sa_handler = SIG_IGN;
169 sa.sa_flags = SA_RESTART;
171 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
175 memset(&sa, 0, sizeof (sa));
176 sa.sa_handler = term_handler;
177 sa.sa_flags = SA_RESTART;
179 if (sigaction(SIGHUP, &sa, NULL) < 0) {
182 if (sigaction(SIGINT, &sa, NULL) < 0) {
185 if (sigaction(SIGQUIT, &sa, NULL) < 0) {
188 if (sigaction(SIGTERM, &sa, NULL) < 0) {
194 static void usage(char* whoami)
196 fprintf(stderr, "usage: %s [-fcdt]\n", whoami);
197 fprintf(stderr, " -c\tconfig file to use.\n");
198 fprintf(stderr, " -d\tschema directory to use.\n");
199 fprintf(stderr, " -t\tcheck configuration file for problems.\n");
200 fprintf(stderr, " -f\tforce removal of listener socket.\n");
201 fprintf(stderr, " -p\tpid file to use.\n");
202 fprintf(stderr, " -h\tprint this help message.\n");
206 static int parse_args(int argc, char* argv[])
210 while ((opt = getopt(argc, argv, "c:d:p:fth")) > 0) {
216 shar_schemadir=optarg;
234 int main(int argc, char *argv[])
236 if (setup_signals() != 0)
239 if (parse_args(argc, argv) != 0)
243 shar_config=getenv("SHIBCONFIG");
245 shar_schemadir=getenv("SHIBSCHEMAS");
247 shar_schemadir=SHIB_SCHEMAS;
249 shar_config=SHIB_CONFIG;
251 // initialize the shib-target library
252 ShibTargetConfig& conf=ShibTargetConfig::getConfig();
253 SPConfig::getConfig().setFeatures(
258 SPConfig::Credentials |
260 SPConfig::OutOfProcess |
261 (shar_checkonly ? (SPConfig::InProcess | SPConfig::RequestMapper) : SPConfig::Logging)
263 if (!conf.init(shar_schemadir) || !conf.load(shar_config)) {
264 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
269 fprintf(stderr, "overall configuration is loadable, check console for non-fatal problems\n");
272 // Write the pid file
274 FILE* pidf = fopen(pidfile, "w");
276 fprintf(pidf, "%d\n", getpid());
279 perror(pidfile); // keep running though
284 if (!conf.getINI()->getListener()->run(&shibd_shutdown)) {
285 fprintf(stderr, "listener failed to enter listen loop\n");