• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "le_signal.h"
17 
18 #include <signal.h>
19 #include <stdio.h>
20 #include <sys/signalfd.h>
21 #include <unistd.h>
22 
23 #include "le_loop.h"
24 #include "le_task.h"
25 #include "loop_event.h"
26 
HandleSignalEvent_(const LoopHandle loop,const TaskHandle task,uint32_t oper)27 static LE_STATUS HandleSignalEvent_(const LoopHandle loop, const TaskHandle task, uint32_t oper)
28 {
29     if (!LE_TEST_FLAGS(oper, Event_Read)) {
30         return LE_FAILURE;
31     }
32     struct signalfd_siginfo fdsi;
33     ssize_t s = read(GetSocketFd(task), &fdsi, sizeof(fdsi));
34     LE_CHECK(s == sizeof(fdsi), return LE_FAILURE, "Failed to read sign %d %d", s, errno);
35     SignalTask *sigTask = (SignalTask *)task;
36     if (sigTask->processSignal) {
37         sigTask->processSignal(&fdsi);
38     }
39     return LE_SUCCESS;
40 }
41 
HandleSignalTaskClose_(const LoopHandle loopHandle,const TaskHandle signalHandle)42 static void HandleSignalTaskClose_(const LoopHandle loopHandle, const TaskHandle signalHandle)
43 {
44     BaseTask *task = (BaseTask *)signalHandle;
45     CloseTask(loopHandle, task);
46     close(task->taskId.fd);
47 }
48 
LE_CreateSignalTask(const LoopHandle loopHandle,SignalHandle * signalHandle,LE_ProcessSignal processSignal)49 LE_STATUS LE_CreateSignalTask(const LoopHandle loopHandle, SignalHandle *signalHandle, LE_ProcessSignal processSignal)
50 {
51     LE_CHECK(loopHandle != NULL && signalHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
52     LE_CHECK(processSignal != NULL, return LE_FAILURE, "Invalid parameters processSignal");
53     sigset_t mask;
54     sigemptyset(&mask);
55     int sfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
56     LE_CHECK(sfd > 0, return -1, "Failed to create signal fd");
57     LE_BaseInfo info = {TASK_SIGNAL, NULL};
58     SignalTask *task = (SignalTask *)CreateTask(loopHandle, sfd, &info, sizeof(SignalTask));
59     LE_CHECK(task != NULL, return LE_NO_MEMORY, "Failed to create task");
60     task->base.handleEvent = HandleSignalEvent_;
61     task->base.innerClose = HandleSignalTaskClose_;
62     task->sigNumber = 0;
63     sigemptyset(&task->mask);
64     task->processSignal = processSignal;
65     *signalHandle = (SignalHandle)task;
66     return LE_SUCCESS;
67 }
68 
LE_AddSignal(const LoopHandle loopHandle,const SignalHandle signalHandle,int signal)69 LE_STATUS LE_AddSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal)
70 {
71     LE_CHECK(loopHandle != NULL && signalHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
72     EventLoop *loop = (EventLoop *)loopHandle;
73     SignalTask *task = (SignalTask *)signalHandle;
74     LE_LOGI("LE_AddSignal %d %d", signal, task->sigNumber);
75     if (sigismember(&task->mask, signal)) {
76         return LE_SUCCESS;
77     }
78     sigaddset(&task->mask, signal);
79     sigprocmask(SIG_BLOCK, &task->mask, NULL);
80     int sfd = signalfd(GetSocketFd(signalHandle), &task->mask, SFD_NONBLOCK | SFD_CLOEXEC);
81     LE_CHECK(sfd > 0, return -1, "Failed to create signal fd");
82     if (task->sigNumber == 0) {
83         loop->addEvent(loop, (const BaseTask *)task, Event_Read);
84     } else {
85         loop->modEvent(loop, (const BaseTask *)task, Event_Read);
86     }
87     task->sigNumber++;
88     return LE_SUCCESS;
89 }
90 
LE_RemoveSignal(const LoopHandle loopHandle,const SignalHandle signalHandle,int signal)91 LE_STATUS LE_RemoveSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal)
92 {
93     LE_CHECK(loopHandle != NULL && signalHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
94     EventLoop *loop = (EventLoop *)loopHandle;
95     SignalTask *task = (SignalTask *)signalHandle;
96     LE_LOGI("LE_RemoveSignal %d %d", signal, task->sigNumber);
97     if (!sigismember(&task->mask, signal)) {
98         return LE_SUCCESS;
99     }
100     sigdelset(&task->mask, signal);
101     task->sigNumber--;
102     int sfd = signalfd(GetSocketFd(signalHandle), &task->mask, SFD_NONBLOCK | SFD_CLOEXEC);
103     LE_CHECK(sfd > 0, return -1, "Failed to create signal fd");
104     if (task->sigNumber <= 0) {
105         loop->delEvent(loop, GetSocketFd(signalHandle), Event_Read);
106     }
107     return LE_SUCCESS;
108 }
109 
LE_CloseSignalTask(const LoopHandle loopHandle,const SignalHandle signalHandle)110 void LE_CloseSignalTask(const LoopHandle loopHandle, const SignalHandle signalHandle)
111 {
112     LE_CHECK(loopHandle != NULL && signalHandle != NULL, return, "Invalid parameters");
113     LE_CloseTask(loopHandle, signalHandle);
114 }
115