-
Notifications
You must be signed in to change notification settings - Fork 1
/
ms_signal.c
132 lines (122 loc) · 4.07 KB
/
ms_signal.c
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
#include "ms_signal.h"
/***********************************************************
* @Func : ms_signal_toname()
* @Author : lwp
* @Brief : 获取信号的名称。
* @Param : [in] signal
* @Return : 字符串指针
* @Note :
***********************************************************/
const char *ms_signal_toname(int signal)
{
switch (signal)
{
case SIGHUP : return "SIGHUP";
case SIGINT : return "SIGINT";
case SIGQUIT : return "SIGQUIT";
case SIGILL : return "SIGILL";
case SIGABRT : return "SIGABRT";
case SIGFPE : return "SIGFPE";
case SIGKILL : return "SIGKILL";
case SIGSEGV : return "SIGSEGV";
case SIGPIPE : return "SIGPIPE";
case SIGALRM : return "SIGALRM";
case SIGTERM : return "SIGTERM";
case SIGUSR1 : return "SIGUSR1";
case SIGUSR2 : return "SIGUSR2";
case SIGCHLD : return "SIGCHLD";
case SIGCONT : return "SIGCONT";
case SIGSTOP : return "SIGSTOP";
case SIGTSTP : return "SIGTSTP";
case SIGTTIN : return "SIGTTIN";
case SIGTTOU : return "SIGTTOU";
case SIGBUS : return "SIGBUS";
case SIGPOLL : return "SIGPOLL";
case SIGPROF : return "SIGPROF";
case SIGTRAP : return "SIGTRAP";
case SIGURG : return "SIGURG";
case SIGVTALRM : return "SIGVTALRM";
case SIGXCPU : return "SIGXCPU";
case SIGXFSZ : return "SIGXFSZ";
case SIGWINCH : return "SIGWINCH";
case SIGSTKFLT : return "SIGSTKFLT";
case SIGPWR : return "SIGPWR";
case SIGUNUSED : return "SIGUNUSED";
// case SIGSYS : return "SIGSYS";
// case Synonym : return "Synonym";
// case SIGIOT : return "SIGIOT";
// case SIGEMT : return "SIGEMT";
// case SIGIO : return "SIGIO";
// case SIGCLD : return "SIGCLD";
// case SIGINFO : return "SIGINFO";
// case SIGLOST : return "SIGLOST";
default : return "UNKNOWN";
}
}
// @ms_signal_toname() ok
/***********************************************************
* @Func : ms_signal_init()
* @Author : lwp
* @Brief : 初始化信号 handler。
* @Param : [in] signal_t : ms_signal_t 结构体数组
* @Return : MS_ERROR : 失败
* MS_OK : 成功
* @Note :
***********************************************************/
int ms_signal_init(ms_signal_t *signal_t)
{
struct sigaction act;
// 注册信号所对应的 handler
for (ms_signal_t *t = signal_t; t->signal != -1; t++)
{
memset(&act, 0, sizeof(act));
act.sa_handler = t->handler;
if (sigemptyset(&act.sa_mask) == -1)
{
ms_errlog(MS_ERRLOG_ERR, errno, "sigemptyset() failed");
return MS_ERROR;
}
if (sigaction(t->signal, &act, NULL) == -1)
{
ms_errlog(MS_ERRLOG_ERR, errno, "sigaction(\"%s\") failed",
ms_signal_toname(t->signal));
return MS_ERROR;
}
}
return MS_OK;
}
// @ms_signal_init() ok
/***********************************************************
* @Func : ms_signal_register()
* @Author : lwp
* @Brief : 当前进程注册其关心的信号。
* @Param : [in] signals : 信号数组
* @Return : MS_ERROR : 失败
* MS_OK : 成功
* @Note :
***********************************************************/
int ms_signal_register(int *signals)
{
sigset_t sigset;
if (sigfillset(&sigset) == -1)
{
ms_errlog(MS_ERRLOG_ERR, errno, "sigfillset() failed");
return MS_ERROR;
}
for (int *t = signals; *t != -1; t++)
{
if (sigdelset(&sigset, *t) == -1)
{
ms_errlog(MS_ERRLOG_ERR, errno, "sigdelset(\"%s\") failed",
ms_signal_toname(*t));
return MS_ERROR;
}
}
if (sigprocmask(SIG_BLOCK, &sigset, NULL) == -1)
{
ms_errlog(MS_ERRLOG_ERR, errno, "sigprocmask(SIG_BLOCK) failed");
return MS_ERROR;
}
return MS_OK;
}
// @ms_signal_register() ok