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
| #include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/time.h>
void sig_alarm(int signo)
{
printf("catch SIGALRM in sleep_cmd\n");
}
void sig_real_alarm(int signo)
{
printf("catch real SIGALRM\n");
}
int system_cmd(const char *pCmd)
{
pid_t pid;
int status;
sigset_t newMask, oldMask;
struct sigaction currAction, oldAction;
if (pCmd == NULL)
{
return 1;
}
currAction.sa_handler = SIG_IGN;
currAction.sa_flags = 0;
sigemptyset(&currAction.sa_mask);
if (sigaction(SIGINT, &currAction, &oldAction) < 0)
{
return (-1);
}
if (sigaction(SIGQUIT,&currAction, &oldAction) < 0)
{
return (-1);
}
sigemptyset(&newMask);
sigemptyset(&oldMask);
sigaddset(&newMask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &newMask, &oldMask) < 0)
{
return (-1);
}
if ( (pid = fork()) < 0)
{
status = -1;
}
else if (pid == 0)
{
sigaction(SIGINT, &oldAction, NULL);
sigaction(SIGQUIT, &oldAction, NULL);
sigprocmask(SIG_SETMASK, &oldMask, NULL);
execl("/bin/bash", "sh", "-c", pCmd, (char*)0);
_exit(127);
}
else
{
while (waitpid(pid, &status, 0) < 0)
{
if (errno != EINTR)
{
status = -1;
break;
}
}
}
if (sigaction(SIGINT, &oldAction, NULL) < 0)
{
return (-1);
}
if (sigaction(SIGQUIT, &oldAction, NULL) < 0)
{
return (-1);
}
if (sigprocmask(SIG_SETMASK, &oldMask, NULL) < 0)
{
return (-1);
}
return (status);
}
//sleep_cmd未处理与以前设置的闹钟的交互作用, 现在的处理会使之前设置的闹钟失效
unsigned int sleep_cmd(unsigned int nsec)
{
sigset_t newMask, oldMask, suspendMask;
struct sigaction newAction, oldAction;
unsigned int uslept;
newAction.sa_handler = sig_alarm;
sigemptyset(&newAction.sa_mask);
newAction.sa_flags = 0;
if (sigaction(SIGALRM, &newAction, &oldAction) < 0)
{
printf("set SIGALRM new action error\n");
}
sigemptyset(&newMask);
sigaddset(&newMask, SIGALRM);
sigprocmask(SIG_BLOCK, &newMask, &oldMask);
alarm(nsec);
suspendMask = oldMask;
sigdelset(&suspendMask, SIGALRM); //不阻塞SIGALRM
sigsuspend(&suspendMask);
uslept = alarm(0);
if (sigaction(SIGALRM, &oldAction, NULL) < 0)
{
printf("set SIGALRM old Action error\n");
}
sigprocmask(SIG_SETMASK, &oldMask, NULL);
return uslept;
}
int main(int argc, char **argv)
{
int index = 1;
int cmdLen = 0;
unsigned int remainSec = 0;
char cmdBuff[1024] = {0};
//struct itimerval its;
struct itimerspec itms;
struct sigaction sigAction;
timer_t timerid;
struct sigevent sev;
if (argc <= 1)
{
printf("no cmd of argv\n");
return 0;
}
for (index = 1; index < argc; index++)
{
sprintf(cmdBuff + cmdLen, "%s ", argv[index]);
cmdLen += strlen(argv[index]) + 1;
}
cmdBuff[cmdLen] = '\0';
printf("%s\n", cmdBuff);
system_cmd(cmdBuff);
sigAction.sa_handler = sig_real_alarm;
sigemptyset(&sigAction.sa_mask);
sigAction.sa_flags = 0;
sigaction(SIGALRM, &sigAction, NULL);
/*
its.it_value.tv_sec = 1;
its.it_value.tv_usec = 0;
its.it_interval.tv_sec = 1;
its.it_interval.tv_usec = 0;
if (setitimer(ITIMER_REAL, &its, NULL))
{
printf("set timer error\n");
}*/
//setitimer 与 alarm共用一个定时器,互相干扰。
//创建定时器,发送的信号为SIGALRM
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGALRM;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1)
{
printf("timer_create error\n");
}
itms.it_value.tv_sec = 1;
itms.it_value.tv_nsec = 0;
itms.it_interval.tv_sec = itms.it_value.tv_sec;
itms.it_interval.tv_nsec = itms.it_value.tv_nsec;
//设置定时器
if (timer_settime(timerid, 0, &itms, NULL) == -1)
{
printf("timer_settime error\n");
}
remainSec = sleep_cmd(5);
printf("remain second: %d\n", remainSec);
/*setitimer(ITIMER_REAL, &its, NULL);*/
while(1)
{
sleep(10);
raise(SIGKILL);
}
return 0;
}
|