SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-sp.git] / shibsp / remoting / impl / SocketListener.cpp
index 19056c0..8951175 100644 (file)
@@ -1,35 +1,42 @@
-/*
- *  Copyright 2001-2009 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
  * SocketListener.cpp
  *
- * Berkeley Socket-based ListenerService implementation
+ * Berkeley Socket-based ListenerService implementation.
  */
 
 #include "internal.h"
 #include "exceptions.h"
 #include "ServiceProvider.h"
+#include "SPConfig.h"
 #include "remoting/impl/SocketListener.h"
 
 #include <errno.h>
 #include <stack>
 #include <sstream>
-#include <shibsp/SPConfig.h>
+#include <xercesc/util/XMLUniDefs.hpp>
+
 #include <xmltooling/util/NDC.h>
+#include <xmltooling/util/XMLHelper.h>
 
 #ifndef WIN32
 # include <netinet/in.h>
@@ -61,7 +68,7 @@ namespace shibsp {
 
         Category& m_log;
         const SocketListener* m_listener;
-        auto_ptr<Mutex> m_lock;
+        boost::scoped_ptr<Mutex> m_lock;
         stack<SocketListener::ShibSocket> m_pool;
     };
 
@@ -153,40 +160,36 @@ SocketListener::ShibSocket SocketPool::get()
 
 void SocketPool::put(SocketListener::ShibSocket s)
 {
-    m_lock->lock();
+    Lock lock(m_lock);
     m_pool.push(s);
-    m_lock->unlock();
 }
 
 SocketListener::SocketListener(const DOMElement* e)
-    : m_catchAll(false), log(&Category::getInstance(SHIBSP_LOGCAT".Listener")), m_socketpool(NULL),
-#ifndef WIN32
-        m_signal(0),
-#endif
-        m_shutdown(NULL), m_child_lock(NULL), m_child_wait(NULL), m_socket((ShibSocket)0)
+    : m_catchAll(false), log(&Category::getInstance(SHIBSP_LOGCAT ".Listener")),
+        m_shutdown(nullptr), m_stackSize(0), m_socket((ShibSocket)0)
 {
     // Are we a client?
     if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
-        m_socketpool=new SocketPool(*log,this);
+        m_socketpool.reset(new SocketPool(*log,this));
     }
     // Are we a server?
     if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess)) {
-        m_child_lock = Mutex::create();
-        m_child_wait = CondWait::create();
+        m_child_lock.reset(Mutex::create());
+        m_child_wait.reset(CondWait::create());
+
+        static const XMLCh stackSize[] = UNICODE_LITERAL_9(s,t,a,c,k,S,i,z,e);
+        m_stackSize = XMLHelper::getAttrInt(e, 0, stackSize) * 1024;
     }
 }
 
 SocketListener::~SocketListener()
 {
-    delete m_socketpool;
-    delete m_child_wait;
-    delete m_child_lock;
 }
 
-bool SocketListener::run(bool force, bool* shutdown)
+bool SocketListener::init(bool force)
 {
 #ifdef _DEBUG
-    NDC ndc("run");
+    NDC ndc("init");
 #endif
     log->info("listener service starting");
 
@@ -199,10 +202,6 @@ bool SocketListener::run(bool force, bool* shutdown)
     }
     sp->unlock();
 
-    // Save flag to monitor for shutdown request.
-    m_shutdown=shutdown;
-    unsigned long count = 0;
-
     if (!create(m_socket)) {
         log->crit("failed to create socket");
         return false;
@@ -213,14 +212,17 @@ bool SocketListener::run(bool force, bool* shutdown)
         return false;
     }
 
-#ifndef WIN32
-    if (m_signal) {
-        // Notify our parent that we're entering the select loop.
-        pid_t ppid = getppid();
-        kill(ppid, SIGUSR1);
-        log->info("notified parent (%d) upon entering select loop", ppid);
-    }
+    return true;
+}
+
+bool SocketListener::run(bool* shutdown)
+{
+#ifdef _DEBUG
+    NDC ndc("run");
 #endif
+    // Save flag to monitor for shutdown request.
+    m_shutdown = shutdown;
+    unsigned long count = 0;
 
     while (!*m_shutdown) {
         fd_set readfds;
@@ -238,7 +240,8 @@ bool SocketListener::run(bool force, bool* shutdown)
                 if (errno == EINTR) continue;
                 log_error();
                 log->error("select() on main listener socket failed");
-                return false;
+                *m_shutdown = true;
+                break;
 
             case 0:
                 continue;
@@ -254,7 +257,7 @@ bool SocketListener::run(bool force, bool* shutdown)
 
                 // We throw away the result because the children manage themselves...
                 try {
-                    new ServerThread(newsock,this,++count);
+                    new ServerThread(newsock, this, ++count);
                 }
                 catch (exception& ex) {
                     log->crit("exception starting new server thread to service incoming request: %s", ex.what());
@@ -272,12 +275,16 @@ bool SocketListener::run(bool force, bool* shutdown)
     // Wait for all children to exit.
     m_child_lock->lock();
     while (!m_children.empty())
-        m_child_wait->wait(m_child_lock);
+        m_child_wait->wait(m_child_lock.get());
     m_child_lock->unlock();
 
+    return true;
+}
+
+void SocketListener::term()
+{
     this->close(m_socket);
     m_socket=(ShibSocket)0;
-    return true;
 }
 
 DDF SocketListener::send(const DDF& in)
@@ -361,7 +368,7 @@ DDF SocketListener::send(const DDF& in)
     if (out.isstring() && out.name() && !strcmp(out.name(),"exception")) {
         // Reconstitute exception object.
         DDFJanitor jout(out);
-        XMLToolingException* except=NULL;
+        XMLToolingException* except=nullptr;
         try {
             except=XMLToolingException::fromString(out.string());
             log->error("remoted message returned an error: %s", except->what());
@@ -372,15 +379,17 @@ DDF SocketListener::send(const DDF& in)
             throw ListenerException("Remote call failed with an unparsable exception.");
         }
 
-        auto_ptr<XMLToolingException> wrapper(except);
+        boost::scoped_ptr<XMLToolingException> wrapper(except);
         wrapper->raise();
     }
 
     return out;
 }
 
-bool SocketListener::log_error() const
+bool SocketListener::log_error(const char* fn) const
 {
+    if (!fn)
+        fn = "unknown";
 #ifdef WIN32
     int rc=WSAGetLastError();
 #else
@@ -390,10 +399,10 @@ bool SocketListener::log_error() const
     char buf[256];
     memset(buf,0,sizeof(buf));
     strerror_r(rc,buf,sizeof(buf));
-    log->error("socket call resulted in error (%d): %s",rc,isprint(*buf) ? buf : "no message");
+    log->error("socket call (%s) resulted in error (%d): %s",fn, rc, isprint(*buf) ? buf : "no message");
 #else
     const char* buf=strerror(rc);
-    log->error("socket call resulted in error (%d): %s",rc,isprint(*buf) ? buf : "no message");
+    log->error("socket call (%s) resulted in error (%d): %s", fn, rc, isprint(*buf) ? buf : "no message");
 #endif
     return false;
 }
@@ -413,11 +422,11 @@ void* server_thread_fn(void* arg)
 
     // Now we can clean up and exit the thread.
     delete child;
-    return NULL;
+    return nullptr;
 }
 
 ServerThread::ServerThread(SocketListener::ShibSocket& s, SocketListener* listener, unsigned long id)
-    : m_sock(s), m_child(NULL), m_listener(listener)
+    : m_sock(s), m_child(nullptr), m_listener(listener)
 {
 
     ostringstream buf;
@@ -425,7 +434,7 @@ ServerThread::ServerThread(SocketListener::ShibSocket& s, SocketListener* listen
     m_id = buf.str();
 
     // Create the child thread
-    m_child = Thread::create(server_thread_fn, (void*)this);
+    m_child = Thread::create(server_thread_fn, (void*)this, m_listener->m_stackSize);
     m_child->detach();
 }
 
@@ -446,8 +455,8 @@ void ServerThread::run()
 
     // Before starting up, make sure we fully "own" this socket.
     m_listener->m_child_lock->lock();
-    while (m_listener->m_children.find(m_sock)!=m_listener->m_children.end())
-        m_listener->m_child_wait->wait(m_listener->m_child_lock);
+    while (m_listener->m_children.find(m_sock) != m_listener->m_children.end())
+        m_listener->m_child_wait->wait(m_listener->m_child_lock.get());
     m_listener->m_children[m_sock] = m_child;
     m_listener->m_child_lock->unlock();
 
@@ -490,7 +499,7 @@ void ServerThread::run()
 
 int ServerThread::job()
 {
-    Category& log = Category::getInstance(SHIBSP_LOGCAT".Listener");
+    Category& log = Category::getInstance(SHIBSP_LOGCAT ".Listener");
 
     bool incomingError = true;  // set false once incoming message is received
     ostringstream sink;