summaryrefslogtreecommitdiffstats
path: root/cmds/runtime/SignalHandler.cpp
blob: cccaabfc57ad4d92d1b6a7c841e55973361dde86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// Copyright 2005 The Android Open Source Project
//

#define LOG_TAG "SignalHandler"

#include "SignalHandler.h"

#include <utils/Atomic.h>
#include <utils/Debug.h>
#include <utils/Log.h>

#include <errno.h>
#include <sys/wait.h>
#include <unistd.h>

namespace android {

class SignalHandler::ProcessThread : public Thread
{
public:
    ProcessThread(SignalHandler& sh)
        : Thread(false)
        , mOwner(sh)
    {
    }

    virtual bool threadLoop()
    {
        char buffer[32];
        read(mOwner.mAvailMsg[0], buffer, sizeof(buffer));

        LOGV("Signal command processing thread woke up!");

        if (mOwner.mLostCommands) {
            LOGE("Lost %d signals!", mOwner.mLostCommands);
            mOwner.mLostCommands = 0;
        }

        int cur;
        while ((cur=mOwner.mCommandBottom) != mOwner.mCommandTop) {
            if (mOwner.mCommands[cur].filled == 0) {
                LOGV("Command at %d is not yet filled", cur);
                break;
            }

            LOGV("Processing command at %d, top is %d",
                 cur, mOwner.mCommandTop);
            processCommand(mOwner.mCommands[cur]);
            mOwner.mCommands[cur].filled = 0;

            int next = mOwner.mCommandBottom+1;
            if (next >= COMMAND_QUEUE_SIZE) {
                next = 0;
            }

            mOwner.mCommandBottom = next;
        }

        return true;
    }

    void processCommand(const CommandEntry& entry)
    {
        switch (entry.signum) {
        case SIGCHLD: {
            mOwner.mLock.lock();
            ssize_t i = mOwner.mChildHandlers.indexOfKey(entry.info.si_pid);
            ChildHandler ch;
            if (i >= 0) {
                ch = mOwner.mChildHandlers.valueAt(i);
                mOwner.mChildHandlers.removeItemsAt(i);
            }
            mOwner.mLock.unlock();

            LOGD("SIGCHLD: pid=%d, handle index=%d", entry.info.si_pid, i);

            if (i >= 0) {
                int res = waitpid(entry.info.si_pid, NULL, WNOHANG);
                LOGW_IF(res == 0,
                        "Received SIGCHLD, but pid %d is not yet stopped",
                        entry.info.si_pid);
                if (ch.handler) {
                    ch.handler(entry.info.si_pid, ch.userData);
                }
            } else {
                LOGW("Unhandled SIGCHLD for pid %d", entry.info.si_pid);
            }
        } break;
        }
    }

    SignalHandler& mOwner;
};


Mutex SignalHandler::mInstanceLock;
SignalHandler* SignalHandler::mInstance = NULL;

status_t SignalHandler::setChildHandler(pid_t childPid,
                                        int tag,
                                        child_callback_t handler,
                                        void* userData)
{
    SignalHandler* const self = getInstance();

    self->mLock.lock();

    // First make sure this child hasn't already exited.
    pid_t res = waitpid(childPid, NULL, WNOHANG);
    if (res != 0) {
        if (res < 0) {
            LOGW("setChildHandler waitpid of %d failed: %d (%s)",
                 childPid, res, strerror(errno));
        } else {
            LOGW("setChildHandler waitpid of %d said %d already dead",
                 childPid, res);
        }

        // Some kind of error...  just handle the exit now.
        self->mLock.unlock();

        if (handler) {
            handler(childPid, userData);
        }

        // Return an error code -- 0 means it already exited.
        return (status_t)res;
    }

    ChildHandler entry;
    entry.childPid = childPid;
    entry.tag = tag;
    entry.handler = handler;
    entry.userData = userData;

    // Note: this replaces an existing entry for this pid, if there already
    // is one.  This is the required behavior.
    LOGD("setChildHandler adding pid %d, tag %d, handler %p, data %p",
         childPid, tag, handler, userData);
    self->mChildHandlers.add(childPid, entry);

    self->mLock.unlock();

    return NO_ERROR;
}

void SignalHandler::killAllChildren(int tag)
{
    SignalHandler* const self = getInstance();

    AutoMutex _l (self->mLock);
    const size_t N = self->mChildHandlers.size();
    for (size_t i=0; i<N; i++) {
        const ChildHandler& ch(self->mChildHandlers.valueAt(i));
        if (tag == 0 || ch.tag == tag) {
            const pid_t pid = ch.childPid;
            LOGI("Killing child %d (tag %d)\n", pid, ch.tag);
            kill(pid, SIGKILL);
        }
    }
}

SignalHandler::SignalHandler()
    : mCommandTop(0)
    , mCommandBottom(0)
    , mLostCommands(0)
{
    memset(mCommands, 0, sizeof(mCommands));

    int res = pipe(mAvailMsg);
    LOGE_IF(res != 0, "Unable to create signal handler pipe: %s", strerror(errno));

    mProcessThread = new ProcessThread(*this);
    mProcessThread->run("SignalHandler", PRIORITY_HIGHEST);

    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_sigaction = sigAction;
    sa.sa_flags = SA_NOCLDSTOP|SA_SIGINFO;
    sigaction(SIGCHLD, &sa, NULL);
}

SignalHandler::~SignalHandler()
{
}

SignalHandler* SignalHandler::getInstance()
{
    AutoMutex _l(mInstanceLock);
    if (mInstance == NULL) {
        mInstance = new SignalHandler();
    }
    return mInstance;
}

void SignalHandler::sigAction(int signum, siginfo_t* info, void*)
{
    static const char wakeupMsg[1] = { 0xff };

    // If our signal handler is being called, then we know we have
    // already initialized the SignalHandler class and thus mInstance
    // is valid.
    SignalHandler* const self = mInstance;

    // XXX This is not safe!
    #if 0
    LOGV("Signal %d: signo=%d, errno=%d, code=%d, pid=%d\n",
           signum,
           info->si_signo, info->si_errno, info->si_code,
           info->si_pid);
    #endif

    int32_t oldTop, newTop;

    // Find the next command slot...
    do {
        oldTop = self->mCommandTop;

        newTop = oldTop + 1;
        if (newTop >= COMMAND_QUEUE_SIZE) {
            newTop = 0;
        }

        if (newTop == self->mCommandBottom) {
            // The buffer is filled up!  Ouch!
            // XXX This is not safe!
            #if 0
            LOGE("Command buffer overflow!  newTop=%d\n", newTop);
            #endif
            android_atomic_add(1, &self->mLostCommands);
            write(self->mAvailMsg[1], wakeupMsg, sizeof(wakeupMsg));
            return;
        }
    } while(android_atomic_cmpxchg(oldTop, newTop, &(self->mCommandTop)));

    // Fill in the command data...
    self->mCommands[oldTop].signum = signum;
    self->mCommands[oldTop].info = *info;

    // And now make this command available.
    self->mCommands[oldTop].filled = 1;

    // Wake up the processing thread.
    write(self->mAvailMsg[1], wakeupMsg, sizeof(wakeupMsg));
}

}; // namespace android