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>
25 // eventually we might be able to support autoconf via cygwin...
26 #if defined (_MSC_VER) || defined(__BORLANDC__)
27 # include "config_win32.h"
34 #include <sys/select.h>
41 #include "shar-utils.h"
45 using namespace shibboleth;
46 using namespace shibtarget;
47 using namespace shibd::logging;
50 # define FD_SETSIZE 1024
53 extern "C" void shibrpc_prog_2(struct svc_req* rqstp, register SVCXPRT* transp);
55 // Declare a "MemoryListener" that our server methods will forward their work to.
56 IListener* g_MemoryListener = NULL;
59 const char* shar_config = NULL;
60 const char* shar_schemadir = NULL;
61 bool shar_checkonly = false;
62 static int unlink_socket = 0;
63 const char* pidfile = NULL;
65 static bool new_connection(IListener::ShibSocket& listener, const Iterator<ShibRPCProtocols>& protos)
67 IListener::ShibSocket sock;
69 // Accept the connection.
70 if (!ShibTargetConfig::getConfig().getINI()->getListener()->accept(listener, sock))
73 // We throw away the result because the children manage themselves...
75 new SharChild(sock,protos);
78 saml::NDC ndc("new_connection");
79 Category& log=Category::getInstance("shibd");
80 log.crit("error starting new child thread to service request");
86 static void shar_svc_run(IListener::ShibSocket& listener, const Iterator<ShibRPCProtocols>& protos)
89 saml::NDC ndc("shar_svc_run");
91 Category& log=Category::getInstance("shibd");
96 FD_SET(listener, &readfds);
97 struct timeval tv = { 0, 0 };
100 switch (select(listener + 1, &readfds, 0, 0, &tv)) {
106 if (errno == EINTR) continue;
107 SHARUtils::log_error();
108 log.error("select() on main listener socket failed");
115 if (!new_connection(listener, protos))
116 log.crit("new_connection failed");
119 log.info("shar_svc_run ended");
124 //#include <CRTDBG.H>
126 #define nNoMansLandSize 4
127 typedef struct _CrtMemBlockHeader
129 struct _CrtMemBlockHeader * pBlockHeaderNext;
130 struct _CrtMemBlockHeader * pBlockHeaderPrev;
136 unsigned char gap[nNoMansLandSize];
138 * unsigned char data[nDataSize];
139 * unsigned char anotherGap[nNoMansLandSize];
141 } _CrtMemBlockHeader;
144 int MyAllocHook(int nAllocType, void *pvData,
145 size_t nSize, int nBlockUse, long lRequest,
146 const unsigned char * szFileName, int nLine)
148 if ( nBlockUse == _CRT_BLOCK )
150 if (nAllocType == _HOOK_FREE) {
151 _CrtMemBlockHeader* ptr = (_CrtMemBlockHeader*)(((_CrtMemBlockHeader *)pvData)-1);
152 if (ptr->nDataSize == 8192)
153 fprintf(stderr,"free request %u size %u\n", ptr->lRequest, ptr->nDataSize);
155 else if (nAllocType == _HOOK_ALLOC && nSize == 8192)
156 fprintf(stderr,"%s request %u size %u\n", ((nAllocType == _HOOK_ALLOC) ? "alloc" : "realloc"), lRequest, nSize);
161 int real_main(int preinit)
163 static IListener::ShibSocket sock;
164 ShibRPCProtocols protos[1] = {
165 { SHIBRPC_PROG, SHIBRPC_VERS_2, shibrpc_prog_2 }
168 ShibTargetConfig& conf=ShibTargetConfig::getConfig();
171 // initialize the shib-target library
173 ShibTargetConfig::Listener |
174 ShibTargetConfig::Caching |
175 ShibTargetConfig::Metadata |
176 ShibTargetConfig::Trust |
177 ShibTargetConfig::Credentials |
178 ShibTargetConfig::AAP |
179 ShibTargetConfig::GlobalExtensions |
180 (shar_checkonly ? (ShibTargetConfig::LocalExtensions | ShibTargetConfig::RequestMapper) : ShibTargetConfig::Logging)
183 shar_config=getenv("SHIBCONFIG");
185 shar_schemadir=getenv("SHIBSCHEMAS");
187 shar_schemadir=SHIB_SCHEMAS;
189 shar_config=SHIB_CONFIG;
190 if (!conf.init(shar_schemadir) || !conf.load(shar_config)) {
191 fprintf(stderr, "configuration is invalid, see console for specific problems\n");
195 // If just a test run, bail.
196 if (shar_checkonly) {
197 fprintf(stdout, "overall configuration is loadable, check console for non-fatal problems\n");
201 // Build an internal "listener" to handle the work.
202 IPlugIn* plugin=SAMLConfig::getConfig().getPlugMgr().newPlugin(shibtarget::XML::MemoryListenerType,NULL);
203 g_MemoryListener=dynamic_cast<IListener*>(plugin);
204 if (!g_MemoryListener) {
206 fprintf(stderr, "MemoryListener plugin failed to load");
211 const IListener* listener=conf.getINI()->getListener();
213 // Create the SHAR listener socket
214 if (!listener->create(sock)) {
215 delete g_MemoryListener;
220 // Bind to the proper port
221 if (!listener->bind(sock)) {
222 delete g_MemoryListener;
227 // Initialize the SHAR Utilitites
232 //_CrtSetAllocHook(MyAllocHook);
235 if (!shar_checkonly) {
236 shar_svc_run(sock, ArrayIterator<ShibRPCProtocols>(protos,1));
238 // Finalize the SHAR, close all clients
240 conf.getINI()->getListener()->close(sock);
243 delete g_MemoryListener;
251 static void term_handler(int arg)
256 static int setup_signals(void)
258 NDC ndc("setup_signals");
261 memset(&sa, 0, sizeof (sa));
262 sa.sa_handler = SIG_IGN;
263 sa.sa_flags = SA_RESTART;
265 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
266 SHARUtils::log_error();
270 memset(&sa, 0, sizeof (sa));
271 sa.sa_handler = term_handler;
272 sa.sa_flags = SA_RESTART;
274 if (sigaction(SIGHUP, &sa, NULL) < 0) {
275 SHARUtils::log_error();
278 if (sigaction(SIGINT, &sa, NULL) < 0) {
279 SHARUtils::log_error();
282 if (sigaction(SIGQUIT, &sa, NULL) < 0) {
283 SHARUtils::log_error();
286 if (sigaction(SIGTERM, &sa, NULL) < 0) {
287 SHARUtils::log_error();
293 static void usage(char* whoami)
295 fprintf(stderr, "usage: %s [-fcdt]\n", whoami);
296 fprintf(stderr, " -c\tconfig file to use.\n");
297 fprintf(stderr, " -d\tschema directory to use.\n");
298 fprintf(stderr, " -t\tcheck configuration file for problems.\n");
299 fprintf(stderr, " -f\tforce removal of listener socket.\n");
300 fprintf(stderr, " -p\tpid file to use.\n");
301 fprintf(stderr, " -h\tprint this help message.\n");
305 static int parse_args(int argc, char* argv[])
309 while ((opt = getopt(argc, argv, "c:d:p:fth")) > 0) {
315 shar_schemadir=optarg;
333 int main(int argc, char *argv[])
335 IListener::ShibSocket sock;
336 ShibRPCProtocols protos[] = {
337 { SHIBRPC_PROG, SHIBRPC_VERS_2, shibrpc_prog_2 }
340 if (setup_signals() != 0)
343 if (parse_args(argc, argv) != 0)
347 shar_config=getenv("SHIBCONFIG");
349 shar_schemadir=getenv("SHIBSCHEMAS");
351 shar_schemadir=SHIB_SCHEMAS;
353 shar_config=SHIB_CONFIG;
355 // initialize the shib-target library
356 ShibTargetConfig& conf=ShibTargetConfig::getConfig();
358 ShibTargetConfig::Listener |
359 ShibTargetConfig::Caching |
360 ShibTargetConfig::Metadata |
361 ShibTargetConfig::Trust |
362 ShibTargetConfig::Credentials |
363 ShibTargetConfig::AAP |
364 ShibTargetConfig::GlobalExtensions |
365 (shar_checkonly ? (ShibTargetConfig::LocalExtensions | ShibTargetConfig::RequestMapper) : ShibTargetConfig::Logging)
367 if (!conf.init(shar_schemadir) || !conf.load(shar_config)) {
368 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
373 fprintf(stderr, "overall configuration is loadable, check console for non-fatal problems\n");
376 // Build an internal "listener" to handle the work.
377 IPlugIn* plugin=SAMLConfig::getConfig().getPlugMgr().newPlugin(shibtarget::XML::MemoryListenerType,NULL);
378 g_MemoryListener=dynamic_cast<IListener*>(plugin);
379 if (!g_MemoryListener) {
381 fprintf(stderr, "MemoryListener plugin failed to load");
386 const IListener* listener=conf.getINI()->getListener();
388 // Create the SHAR listener socket
389 if (!listener->create(sock)) {
390 delete g_MemoryListener;
395 // Bind to the proper port
396 if (!listener->bind(sock, unlink_socket==1)) {
397 delete g_MemoryListener;
402 // Write the pid file
404 FILE* pidf = fopen(pidfile, "w");
406 fprintf(pidf, "%d\n", getpid());
409 perror(pidfile); // keep running though
413 // Initialize the SHAR Utilitites
417 shar_svc_run(sock, ArrayIterator<ShibRPCProtocols>(protos,1));
419 /* Finalize the SHAR, close all clients */
422 listener->close(sock);