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
|
/* This file is part of genrc
Copyryght (C) 2018 Sergey Poznyakoff
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
*/
#include "genrc.h"
static void
timermul(struct timeval *a, int b)
{
a->tv_sec *= b;
a->tv_usec *= b;
a->tv_sec = a->tv_sec + a->tv_usec / 1000000;
a->tv_usec %= 1000000;
}
#define MIN_POLL_TTW 20000
int
com_stop(void)
{
PIDLIST pids;
struct timeval stoptime, before, after, ttw, maxttw;
int ratio;
pidlist_init(&pids);
if (get_pid_list(genrc_pid_closure, &pids)) {
genrc_error("program status unknown");
return 1;
}
if (pids.pidc == 0) {
genrc_error("%s not running", genrc_program);
return 1;
}
if (genrc_verbose)
printf("Stopping %s\n", genrc_program);
gettimeofday(&stoptime, NULL);
stoptime.tv_sec += genrc_timeout;
ratio = 1;
while (pids.pidc) {
gettimeofday(&before, NULL);
if (timercmp(&before, &stoptime, >))
break;
pidlist_kill(&pids, genrc_signal_stop);
if (get_pid_list(genrc_pid_closure, &pids))
break;
if (pids.pidc == 0)
return 0;
gettimeofday(&after, NULL);
if (timercmp(&after, &stoptime, >=))
break;
if (ratio < 10)
++ratio;
timersub(&stoptime, &after, &maxttw);
timersub(&after, &before, &ttw);
timermul(&ttw, ratio);
if (ttw.tv_sec < 0 || ttw.tv_usec < 0)
ttw.tv_sec = ttw.tv_usec = 0;
if (timercmp(&ttw, &maxttw, >))
ttw = maxttw;
if (ttw.tv_sec == 0 && ttw.tv_usec < MIN_POLL_TTW)
ttw.tv_usec = MIN_POLL_TTW;
if (select(0, NULL, NULL, NULL, &ttw) < 0 && errno != EINTR) {
system_error(errno, "select");
break;
}
}
return 1;
}
|