-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyscall.c
83 lines (67 loc) · 1.27 KB
/
syscall.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
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static bool loop = true;
static size_t
sys_getpid(void)
{
size_t counter;
for (counter = 0; loop; counter++)
getpid();
return counter;
}
static void
signal_handler(int sig)
{
if (sig == SIGALRM)
loop = false;
}
static void
usage(void)
{
fputs("syscall [-s sec] syscall\n", stderr);
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
size_t counter = 0;
const char *errstr;
unsigned int seconds = 5;
int ch;
if (argc == 1)
_exit(0);
/* parameter handling */
while ((ch = getopt(argc, argv, "Ee:fs:t")) != -1) {
switch (ch) {
case 's':
seconds = strtonum(optarg, 1, UINT_MAX, &errstr);
if (errstr != NULL)
errx(EXIT_FAILURE, "strtonum: %s", errstr);
break;
case 'h':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc == 0)
usage();
/* signal handling */
signal(SIGALRM, signal_handler);
/* set timer */
if (alarm(seconds) == (unsigned int)-1)
err(EXIT_FAILURE, "alarm");
if (strcmp(argv[0], "getpid") == 0)
counter = sys_getpid();
printf("%6zu %s\n", counter / seconds, argv[0]);
return EXIT_SUCCESS;
}