-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.c
More file actions
84 lines (65 loc) · 1.38 KB
/
handlers.c
File metadata and controls
84 lines (65 loc) · 1.38 KB
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
#include "handlers.h"
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
void
handle_sig(int signo)
{
int status;
pid_t pid;
(void)signo;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
(void)inspect_status(pid, status);
}
}
void
handle_term(int signo)
{
const char *prompt = "\nReceived signal for shutdown\n";
(void)signo;
write(STDOUT_FILENO, prompt, strlen(prompt));
_exit(EXIT_SUCCESS);
}
int
block_sig(int signum, sigset_t *old_mask)
{
sigset_t mask;
if (sigemptyset(&mask) < 0 || sigaddset(&mask, signum) < 0 ||
sigprocmask(SIG_BLOCK, &mask, old_mask) < 0) {
return -1;
}
return 0;
}
int
restore_sig(sigset_t *old_mask)
{
if (sigprocmask(SIG_SETMASK, old_mask, NULL) < 0) {
return -1;
}
return 0;
}
int
inspect_status(pid_t pid, int status)
{
int exit_status;
if (WIFEXITED(status) && (exit_status = WEXITSTATUS(status)) != 0) {
(void)fprintf(stderr, "Process %d exited with status %d\n", pid,
exit_status);
return -1;
}
if (WIFSIGNALED(status)) {
(void)fprintf(stderr, "Process %d was terminated by signal %d\n", pid,
WTERMSIG(status));
return -1;
}
return 0;
}
/* strcasecmp is not case sensitive, like how normal web servers are */
int
fts_compare(const FTSENT **a, const FTSENT **b)
{
return strcasecmp((*a)->fts_name, (*b)->fts_name);
}