1 /*
2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <signal.h>
32 #include <errno.h>
33 #include <pthread.h>
34 #include "los_config.h"
35 #include "los_signal.h"
36 #include "los_task.h"
37 #include "los_tick.h"
38
39 #if (LOSCFG_KERNEL_SIGNAL == 1)
40
TimeSpec2Tick(const struct timespec * tp)41 static inline unsigned int TimeSpec2Tick(const struct timespec *tp)
42 {
43 unsigned long long tick, ns;
44
45 ns = (unsigned long long)tp->tv_sec * OS_SYS_NS_PER_SECOND + tp->tv_nsec;
46 /* round up for ticks */
47 tick = (ns * LOSCFG_BASE_CORE_TICK_PER_SECOND + (OS_SYS_NS_PER_SECOND - 1)) / OS_SYS_NS_PER_SECOND;
48 if (tick > LOS_WAIT_FOREVER) {
49 tick = LOS_WAIT_FOREVER;
50 }
51 return (unsigned int)tick;
52 }
53
raise(int sig)54 int raise(int sig)
55 {
56 unsigned int ret = LOS_SignalSend(LOS_CurTaskIDGet(), sig);
57 if (ret != LOS_OK) {
58 return -1;
59 }
60
61 return 0;
62 }
63
signal(int sig,void (* func)(int))64 void (*signal(int sig, void (*func)(int)))(int)
65 {
66 SIG_HANDLER h = NULL;
67
68 if (!OS_SIGNAL_VALID(sig)) {
69 errno = EINVAL;
70 return SIG_ERR;
71 }
72
73 h = LOS_SignalSet(sig, func);
74 if (h == SIG_ERR) {
75 errno = EINVAL;
76 }
77 return h;
78 }
79
sigprocmask(int how,const sigset_t * set,sigset_t * oset)80 int sigprocmask(int how, const sigset_t *set, sigset_t *oset)
81 {
82 unsigned int ret = LOS_SignalMask(how, set, oset);
83 if (ret != LOS_OK) {
84 errno = EINVAL;
85 return -1;
86 }
87 return 0;
88 }
89
sigaction(int sig,const struct sigaction * act,struct sigaction * oldact)90 int sigaction(int sig, const struct sigaction *act, struct sigaction *oldact)
91 {
92 SIG_HANDLER old = NULL;
93
94 if (!OS_SIGNAL_VALID(sig)) {
95 errno = EINVAL;
96 return -1;
97 }
98
99 if (act != NULL) {
100 old = LOS_SignalSet(sig, act->sa_handler);
101 } else {
102 old = LOS_SignalSet(sig, NULL);
103 (void)LOS_SignalSet(sig, old);
104 }
105
106 if (oldact != NULL) {
107 oldact->sa_handler = old;
108 }
109
110 return 0;
111 }
112
sigwait(const sigset_t * set,int * sig)113 int sigwait(const sigset_t *set, int *sig)
114 {
115 siginfo_t info = {0};
116
117 int ret = LOS_SignalWait(set, &info, 0);
118 if (ret < 0) {
119 errno = EINVAL;
120 return -1;
121 }
122
123 *sig = info.si_signo;
124 return 0;
125 }
126
sigtimedwait(const sigset_t * set,siginfo_t * info,const struct timespec * timeout)127 int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout)
128 {
129 int tick = LOS_WAIT_FOREVER;
130 int ret;
131
132 if (timeout != NULL) {
133 tick = TimeSpec2Tick(timeout);
134 }
135
136 ret = LOS_SignalWait(set, info, tick);
137 if (ret == LOS_OK) {
138 return 0;
139 } else {
140 if (ret == LOS_ERRNO_SIGNAL_PEND_INTERR) {
141 errno = EINTR;
142 } else if (ret == LOS_ERRNO_SIGNAL_TIMEOUT) {
143 errno = EAGAIN;
144 } else {
145 errno = EINVAL;
146 }
147 return -1;
148 }
149 }
150
sigwaitinfo(const sigset_t * set,siginfo_t * info)151 int sigwaitinfo(const sigset_t *set, siginfo_t *info)
152 {
153 return sigtimedwait(set, info, NULL);
154 }
155
pthread_kill(pthread_t pid,int sig)156 int pthread_kill(pthread_t pid, int sig)
157 {
158 unsigned int ret = LOS_SignalSend((unsigned int)pid, sig);
159 if (ret != LOS_OK) {
160 errno = EINVAL;
161 return -1;
162 }
163
164 return 0;
165 }
166
kill(pid_t pid,int sig)167 int kill(pid_t pid, int sig)
168 {
169 unsigned int ret = LOS_SignalSend((unsigned int)pid, sig);
170 if (ret != LOS_OK) {
171 errno = EINVAL;
172 return -1;
173 }
174
175 return 0;
176 }
177 #endif
178