• 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     DelTask((EventLoop *)loopHandle, task);
47     close(task->taskId.fd);
48 }
49 
LE_CreateSignalTask(const LoopHandle loopHandle,SignalHandle * signalHandle,LE_ProcessSignal processSignal)50 LE_STATUS LE_CreateSignalTask(const LoopHandle loopHandle, SignalHandle *signalHandle, LE_ProcessSignal processSignal)
51 {
52     LE_CHECK(loopHandle != NULL && signalHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
53     LE_CHECK(processSignal != NULL, return LE_FAILURE, "Invalid parameters processSignal");
54     sigset_t mask;
55     sigemptyset(&mask);
56     int sfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
57     LE_CHECK(sfd > 0, return -1, "Failed to create signal fd");
58     LE_BaseInfo info = {TASK_SIGNAL, NULL};
59     SignalTask *task = (SignalTask *)CreateTask(loopHandle, sfd, &info, sizeof(SignalTask));
60     LE_CHECK(task != NULL, return LE_NO_MEMORY, "Failed to create task");
61     task->base.handleEvent = HandleSignalEvent_;
62     task->base.innerClose = HandleSignalTaskClose_;
63     task->sigNumber = 0;
64     sigemptyset(&task->mask);
65     task->processSignal = processSignal;
66     *signalHandle = (SignalHandle)task;
67     return LE_SUCCESS;
68 }
69 
LE_AddSignal(const LoopHandle loopHandle,const SignalHandle signalHandle,int signal)70 LE_STATUS LE_AddSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal)
71 {
72     LE_CHECK(loopHandle != NULL && signalHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
73     EventLoop *loop = (EventLoop *)loopHandle;
74     SignalTask *task = (SignalTask *)signalHandle;
75     LE_LOGI("LE_AddSignal %d %d", signal, task->sigNumber);
76     if (sigismember(&task->mask, signal)) {
77         return LE_SUCCESS;
78     }
79     sigaddset(&task->mask, signal);
80     sigprocmask(SIG_BLOCK, &task->mask, NULL);
81     int sfd = signalfd(GetSocketFd(signalHandle), &task->mask, SFD_NONBLOCK | SFD_CLOEXEC);
82     LE_CHECK(sfd > 0, return -1, "Failed to create signal fd");
83     if (task->sigNumber == 0) {
84         loop->addEvent(loop, (const BaseTask *)task, Event_Read);
85     } else {
86         loop->modEvent(loop, (const BaseTask *)task, Event_Read);
87     }
88     task->sigNumber++;
89     return LE_SUCCESS;
90 }
91 
LE_RemoveSignal(const LoopHandle loopHandle,const SignalHandle signalHandle,int signal)92 LE_STATUS LE_RemoveSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal)
93 {
94     LE_CHECK(loopHandle != NULL && signalHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
95     EventLoop *loop = (EventLoop *)loopHandle;
96     SignalTask *task = (SignalTask *)signalHandle;
97     LE_LOGI("LE_RemoveSignal %d %d", signal, task->sigNumber);
98     if (!sigismember(&task->mask, signal)) {
99         return LE_SUCCESS;
100     }
101     sigdelset(&task->mask, signal);
102     task->sigNumber--;
103     int sfd = signalfd(GetSocketFd(signalHandle), &task->mask, SFD_NONBLOCK | SFD_CLOEXEC);
104     LE_CHECK(sfd > 0, return -1, "Failed to create signal fd");
105     if (task->sigNumber <= 0) {
106         loop->delEvent(loop, GetSocketFd(signalHandle), Event_Read);
107     }
108     return LE_SUCCESS;
109 }
110 
LE_CloseSignalTask(const LoopHandle loopHandle,const SignalHandle signalHandle)111 void LE_CloseSignalTask(const LoopHandle loopHandle, const SignalHandle signalHandle)
112 {
113     LE_CHECK(loopHandle != NULL && signalHandle != NULL, return, "Invalid parameters");
114     LE_CloseTask(loopHandle, signalHandle);
115 }
116