-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhalf.c
157 lines (141 loc) · 3.77 KB
/
half.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
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
// fichier half.c
/* Copyright 2005 - 2022 Basile Starynkevitch
This HALF program 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 2, or (at
your option) any later version.
HALF 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.
--
The half program was designed (in 2005) to run something at half load,
on some old crappy laptop (MSI 270, a Turion laptop) which overheated.
The actual hardware issue was faulty RAM modules. It took me a year to
find out that (thanks to Sebastian Pop).
With that program, I was able to build tbe Linux kernel from source
code using the "half make" command. Without half, my then laptop
overheated so much that kernel compilation failed.
Today that program might be perhaps used to simulate bugs, e.g. by
sending SIGSEGV signals to a process group. Just compile it with
-DMY_STOP_SIGNAL=SIGSEGV for example; see the comments near end of file
for more.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <signal.h>
#ifndef MY_STOP_SIGNAL
#define MY_STOP_SIGNAL SIGSTOP
#endif /*MY_STOP_SIGNAL */
pid_t child;
volatile bool running;
void
time_handler (int sig __attribute__((unused)))
{
running = !running;
}
volatile int sendsig;
void
propagate_handler (int sig)
{
sendsig = sig;
}
int
main (int argc, char **argv)
{
int delaymillisec = 250;
struct itimerval itv;
char **progargv;
int progargc;
if (argc <= 1)
{
usage:
fprintf (stderr, "usage: %s [period_millisec] command...\n"
"#this program run at half period the given command..\n"
"#default period is %d milliseconds,\n"
"#... it should be less than 900 and more than 10\n",
argv[0], delaymillisec);
return 1;
};
if (argc == 2 && !strcmp (argv[1], "--help"))
goto usage;
int delarg = atoi (argv[1]);
if (delarg > 0)
{
delaymillisec = delarg;
if (delaymillisec > 900)
delaymillisec = 900;
if (delaymillisec < 10)
delaymillisec = 10;
progargc = argc - 2;
progargv = argv + 2;
}
else
{
progargc = argc - 1;
progargv = argv + 1;
};
if (progargc < 1)
{
fprintf (stderr, "%s missing command\n", argv[0]);
goto usage;
};
child = fork ();
if (!child)
{
usleep (delaymillisec * 500);
setpgrp ();
execvp (progargv[0], progargv);
perror ("cannot exec");
exit (127);
};
close (0);
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = delaymillisec * 1000;
itv.it_value.tv_sec = 0;
itv.it_value.tv_usec = delaymillisec * 1000;
signal (SIGALRM, time_handler);
signal (SIGTERM, propagate_handler);
signal (SIGQUIT, propagate_handler);
signal (SIGINT, propagate_handler);
setitimer (ITIMER_REAL, &itv, 0);
sendsig = 0;
while (1)
{
int status = 0;
int sig2send = 0;
errno = 0;
if (waitpid (child, &status, WNOHANG) > 0)
{
if (WIFEXITED (status))
exit (WEXITSTATUS (status));
else if (WIFSIGNALED (status))
{
fprintf (stderr, "command %s ", *progargv);
fflush (stderr);
psignal (WTERMSIG (status), "interrupted by signal");
exit (126);
}
}
if (running)
killpg (child, SIGCONT);
else
killpg (child, MY_STOP_SIGNAL);
pause ();
sig2send = sendsig;
if (sig2send > 0)
{
sendsig = 0;
killpg (child, sig2send);
}
};
}
// compile with gcc -O -g -Wall half.c -o half