-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathcommon.c
121 lines (109 loc) · 2.64 KB
/
common.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
/*
* common.c
*
***************************************************************
* This program is part of the source code released for the book
* "Hands-on System Programming with Linux"
* (c) Author: Kaiwan N Billimoria
* Publisher: Packt
*
* From:
* 'Common' code.
****************************************************************
* Brief Description:
* This is the 'common' code that gets compiled into all binary
* executables.
*/
#define _GNU_SOURCE
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
/*---------------- Globals, Macros ----------------------------*/
/*---------------- Typedef's, constants, etc ------------------*/
/*---------------- Functions ----------------------------------*/
/*
* function r _ s l e e p
*
* Safe Sleep: wrapper around nanosleep(2) , and in such a way that
* interruption due to non-blocked signal(s) causes the sleep to restart
* for the time remaining.
*/
#ifndef _TIME_H
#include <time.h>
#endif
int r_sleep(time_t sec, long nsec)
{
struct timespec req, rem;
req.tv_sec = sec;
req.tv_nsec = nsec;
while (nanosleep(&req, &rem) == -1) {
if (errno != EINTR)
return -1;
#ifdef DEBUG
/* NOTE! Should never use [f]printf in sig handler - not
* aysnc-signal safe - just shown for demo purpose here (works
* with DEBUG option on).
*/
fprintf(stderr,
"* nanosleep interrupted! rem time: %lu.%lu *\n",
rem.tv_sec, rem.tv_nsec);
#endif
req = rem;
}
return 0;
}
/*
* Signaling: Prints (to stdout) all signal integer values that are
* currently in the Blocked (masked) state.
*/
int show_blocked_signals(void)
{
sigset_t oldset;
int i, none=1;
/* sigprocmask:
* int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
* if 'set' is NULL, the 'how' is ignored, but the
* 'oldset' sigmask value is populated; thus we can query the
* signal mask without altering it.
*/
sigemptyset(&oldset);
if (sigprocmask(SIG_UNBLOCK, 0, &oldset) < 0)
return -1;
printf("[SigBlk: ");
for (i=1; i<=64; i++) {
if (sigismember(&oldset, i)) {
none=0;
printf("%d ", i);
}
}
printf("%s\n", none ? "-none-]" : "]");
fflush(stdout);
return 0;
}
int handle_err(int fatal, const char *fmt, ...)
{
#define ERRSTRMAX 512
char *err_str;
va_list argp;
err_str = malloc(ERRSTRMAX);
if (err_str == NULL)
return -1;
va_start(argp, fmt);
vsnprintf(err_str, ERRSTRMAX-1, fmt, argp);
va_end(argp);
fprintf(stderr, "%s", err_str);
if (errno) {
fprintf(stderr, " ");
perror("kernel says");
}
free(err_str);
if (!fatal)
return 0;
exit(fatal);
}
/* vi: ts=8 */