Fixing communication problem with security-server
authorZofia Abramowska <z.abramowska@samsung.com>
Wed, 16 Jan 2013 13:44:23 +0000 (14:44 +0100)
committerZofia Abramowska <z.abramowska@samsung.com>
Wed, 16 Jan 2013 14:11:56 +0000 (15:11 +0100)
[Issue#] LINUXSWAP-454
[Bug] Security-server crashes
[Cause] No handling of SIGPIPE signal when writing to closed socket
[Solution] blocking SIGPIPE signal in all threads created by socket
service and handling error returned by write
[Verification] Build, run tests and check  JIRA bug(N_SE-20154)

Change-Id: Id6d15922bb9b94cb9ee565e9e90ed0e46a27e25c

socket_connection/connection/SocketStream.cpp
src/daemon/sockets/security_socket_service.cpp

index 7172559..cc5d1a5 100644 (file)
@@ -150,7 +150,7 @@ void SocketStream::Write(size_t num, const void * bytes){
 
         if(FD_ISSET(m_socketFd, &wset)){
             if(-1 == (write_res = write(m_socketFd, reinterpret_cast<const char *>(bytes) + current_offset, bytes_to_write))){
-                if(errno == ECONNRESET){
+                if(errno == ECONNRESET || errno == EPIPE){
                     LogInfo("Connection closed : " << strerror(errno));
                     ThrowMsg(Exception::SocketStreamException,
                             "Connection closed : " << strerror(errno) << ". Couldn't write whole data");
index 47d3769..689de1c 100644 (file)
@@ -166,6 +166,7 @@ void SecuritySocketService::start(){
     }
 
     pthread_t mainThread;
+
     if((returned_value = pthread_create(&mainThread, NULL, &serverThread, this)) < 0){
         errno = returned_value;
         throwWithErrnoMessage("pthread_create()");
@@ -218,6 +219,13 @@ void SecuritySocketService::mainLoop(){
     timespec timeout;
     maxfd = (m_listenFd > signal_fd) ? (m_listenFd) : (signal_fd);
     ++maxfd;
+    //this will block SIGPIPE for this thread and every thread created in it
+    //reason : from here on we don't won't to receive SIGPIPE on writing to closed socket
+    //instead of signal we want to receive error from write - hence blocking SIGPIPE
+    sigset_t set;
+    sigemptyset(&set);
+    sigaddset(&set, SIGPIPE);
+    pthread_sigmask(SIG_BLOCK, &set, NULL);
 
     while(1){
         timeout.tv_sec = TIMEOUT_SEC;