-
Notifications
You must be signed in to change notification settings - Fork 13
/
util.c
83 lines (74 loc) · 2.56 KB
/
util.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
/* vim: set ts=8 sw=4 sts=4 et: */
/*======================================================================
Copyright (C) 2008,2009,2014 OSSO B.V. <[email protected]>
This file is part of RTPSniff.
RTPSniff is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
RTPSniff is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with RTPSniff. If not, see <http://www.gnu.org/licenses/>.
======================================================================*/
#include "rtpsniff.h"
#include "endian.h"
#include <signal.h>
#include <stdio.h>
/* Includes for select(2) when usleep(3) is unavailable */
#if !(_BSD_SOURCE || _XOPEN_SOURCE >= 500)
# include <sys/time.h>
# include <sys/types.h>
# include <unistd.h>
#endif /* !(_BSD_SOURCE || _XOPEN_SOURCE >= 500) */
/* Fix non-bsd signal(2) behaviour when sigaction(2) is unavailable */
#if !(__USE_POSIX || __USE_BSD)
# ifndef _NSIG
# define _NSIG 65
# endif
typedef void (*sighandler_t)(int);
static sighandler_t util__sighandlers[_NSIG];
void util__signal_helper(int signum) {
util__sighandlers[signum](signum);
if (signal(signum, util__signal_helper) == SIG_ERR)
perror("signal");
}
#endif /* !(__USE_POSIX || __USE_BSD) */
int util_signal_set(int signum, void (*handler)(int)) {
#ifdef __USE_POSIX
int ret;
struct sigaction action;
action.sa_handler = handler;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
if ((ret = sigaction(signum, &action, NULL)) != 0) {
perror("sigaction");
return -1;
}
#else
# ifndef __USE_BSD
if (handler != SIG_IGN && handler != SIG_DFL) {
assert(signum < _NSIG);
util__sighandlers[signum] = handler;
handler = &util__signal_helper;
}
# endif /* !__USE_BSD */
if (signal(signum, handler) == SIG_ERR) {
perror("signal");
return -1;
}
#endif /* !__USE_POSIX && !__USE_BSD */
return 0;
}
#if !(_BSD_SOURCE || _XOPEN_SOURCE >= 500)
int usleep(unsigned usec) {
struct timeval timeout;
timeout.tv_sec = usec / 1000000;
timeout.tv_usec = usec;
if (select(0, NULL, NULL, NULL, &timeout) == 0)
return 0;
return -1;
}
#endif /* !(_BSD_SOURCE || _XOPEN_SOURCE >= 500) */