javamanager/javacaptain/src.linux/signalhandler.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  SignalHandler
       
    15 *
       
    16 */
       
    17 
       
    18 #include <sys/types.h>
       
    19 #include <sys/wait.h>
       
    20 #include <pthread.h>
       
    21 #include <signal.h>
       
    22 
       
    23 #include "logger.h"
       
    24 #include "comms.h"
       
    25 #include "commsclientendpoint.h"
       
    26 
       
    27 #include "signalhandler.h"
       
    28 #include "signalhandlermessages.h"
       
    29 
       
    30 namespace java
       
    31 {
       
    32 namespace captain
       
    33 {
       
    34 
       
    35 
       
    36 SignalHandler::SignalHandler():mThreadId(0)
       
    37 {
       
    38 }
       
    39 
       
    40 SignalHandler::~SignalHandler()
       
    41 {
       
    42 }
       
    43 
       
    44 void SignalHandler::start()
       
    45 {
       
    46     if (pthread_create(&mThreadId, 0, SignalHandler::signalHandlerThread, NULL))
       
    47     {
       
    48         ELOG(EJavaCaptain, "pthread_create failed for signal handler thread");
       
    49     }
       
    50 }
       
    51 
       
    52 void SignalHandler::stop()
       
    53 {
       
    54     kill(getpid(), SIGINT);
       
    55     pthread_join(mThreadId, NULL);
       
    56 }
       
    57 
       
    58 void* SignalHandler::signalHandlerThread(void* /*aParams*/)
       
    59 {
       
    60 
       
    61     CommsClientEndpoint comms;
       
    62     if (comms.connect(IPC_ADDRESS_JAVA_CAPTAIN_C))
       
    63     {
       
    64         ELOG(EJavaCaptain, "Connect to main message loop failed!");
       
    65         return NULL;
       
    66     }
       
    67 
       
    68     sigset_t    ss;
       
    69     sigemptyset(&ss);
       
    70     sigaddset(&ss, SIGCHLD);
       
    71     sigaddset(&ss, SIGALRM);
       
    72     sigaddset(&ss, SIGINT);
       
    73 
       
    74     int signal = 0;
       
    75     bool keepWaiting = true;
       
    76     while (keepWaiting && !sigwait(&ss, &signal))
       
    77     {
       
    78         CommsMessage msg;
       
    79         switch (signal)
       
    80         {
       
    81         case SIGCHLD:
       
    82             LOG(EJavaCaptain, EInfo, "SIGCHLD received");
       
    83             {
       
    84                 int status = 0;
       
    85                 pid_t pidi = 0;
       
    86                 while ((pidi=waitpid(-1, &status, WNOHANG))  > 0)
       
    87                 {
       
    88                     LOG2(EJavaCaptain, EInfo, "SIGCHLD received - waitpid returns pid=%d status=%d", (int)pidi, status);
       
    89 
       
    90                     LOG1(EJavaCaptain, EInfo, "WIFEXITED(status) returns %d", WIFEXITED(status));
       
    91                     if (WIFEXITED(status))
       
    92                     {
       
    93                         LOG1(EJavaCaptain, EInfo, "WEXITSTATUS(status) returns %d", WEXITSTATUS(status));
       
    94                     }
       
    95 
       
    96                     LOG1(EJavaCaptain, EInfo, "WIFSIGNALED(status) returns %d", WIFSIGNALED(status));
       
    97                     if (WIFSIGNALED(status))
       
    98                     {
       
    99                         LOG1(EJavaCaptain, EInfo, "WTERMSIG(status) returns %d", WTERMSIG(status));
       
   100                     }
       
   101 
       
   102                     LOG1(EJavaCaptain, EInfo, "WIFSTOPPED(status) returns %d", WIFSTOPPED(status));
       
   103                     if (WIFSTOPPED(status))
       
   104                     {
       
   105                         LOG1(EJavaCaptain, EInfo, "WSTOPSIG(status) returns %d", WSTOPSIG(status));
       
   106                     }
       
   107 
       
   108                     LOG2(EJavaCaptain, EInfo, "initiating pmcTerminated event (pid=%d, status=%d)", (int)pidi, status);
       
   109                     setSignalChildMessage(msg, pidi, status);
       
   110                     comms.send(msg);
       
   111                     LOG2(EJavaCaptain, EInfo, "initiated pmcTerminated event (pid=%d, status=%d)", (int)pidi, status);
       
   112                 }
       
   113             }
       
   114             break;
       
   115 
       
   116         case SIGALRM:
       
   117             LOG(EJavaCaptain, EInfo, "SIGALRM received");
       
   118             setSignalAlarmMessage(msg);
       
   119             comms.send(msg);
       
   120             break;
       
   121 
       
   122         case SIGINT:
       
   123             LOG(EJavaCaptain, EInfo, "SIGINT received");
       
   124             setSignalInterruptMessage(msg);
       
   125             comms.send(msg);
       
   126             keepWaiting = false;
       
   127             break;
       
   128 
       
   129         default:
       
   130             ELOG1(EJavaCaptain, "Unknown signal %d sigwaited!", signal);
       
   131         }
       
   132     }
       
   133     return NULL;
       
   134 }
       
   135 
       
   136 } // namespace captain
       
   137 } // namespace java
       
   138