-
Notifications
You must be signed in to change notification settings - Fork 0
/
rundispoff.c
174 lines (150 loc) · 4.71 KB
/
rundispoff.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
rundispoff - tool for running a background command while the display is off
Copyright (c) 2017 Brian Mayton <[email protected]>
This 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <signal.h>
#include <sys/wait.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/dpms.h>
Display *dpy;
pid_t child;
char **args;
bool awake;
bool start_subprocess() {
if(child != 0) {
fprintf(stderr, "rundispoff: tried to start child when already running");
return false;
}
child = vfork();
if(child == 0) {
if(execvp(args[0], args) != 0) {
perror("rundispoff: failed to execute command");
exit(1);
}
} else if(child == -1) {
perror("rundispoff: vfork failed:");
child = 0;
return false;
}
fprintf(stderr, "rundispoff: child process %d started\n", child);
return true;
}
bool stop_subprocess() {
int status;
if(child > 0) {
fprintf(stderr, "rundispoff: sending SIGINT to child\n");
int ret = kill(child, SIGINT);
if(ret) {
perror("rundispoff: failed to send SIGINT to child");
return false;
}
usleep(10000);
for(int i=10; i>0; i--) {
int status;
pid_t pidret = waitpid(child, &status, WNOHANG);
if(pidret == child) {
fprintf(stderr, "rundispoff: child %d exited with status %d\n",
child, WEXITSTATUS(status));
child = 0;
return true;
} else if(pidret == -1) {
perror("rundispoff: failed to wait for child process");
}
fprintf(stderr, "rundispoff: waiting for child to exit\n");
sleep(1);
}
fprintf(stderr, "rundispoff: sending SIGKILL to child\n");
ret = kill(child, SIGKILL);
pid_t pidret = waitpid(child, &status, 0);
if(pidret != child) {
perror("rundispoff: failed to kill child");
return false;
}
fprintf(stderr, "rundispoff: child %d killed\n",
child);
child = 0;
return true;
} else {
fprintf(stderr, "rundispoff: stop child called, but not running\n");
}
return true;
}
void sigint_handler(int signum) {
fprintf(stderr, "rundispoff: caught SIGINT, exiting\n");
if(child > 0) {
stop_subprocess();
}
exit(0);
}
int main(int argc, char **argv) {
child = 0;
awake = true;
int backoff = 10;
int n = 0;
if(argc < 2) {
fprintf(stderr, "usage: %s <command> [arguments]\n",
argv[0]);
exit(2);
}
args = &argv[1];
dpy = XOpenDisplay(NULL);
if(!dpy) {
fprintf(stderr, "rundispoff: failed to open display.\n");
exit(1);
}
signal(SIGINT, sigint_handler);
int status;
while(true) {
if(child > 0) {
pid_t pidret = waitpid(child, &status, WNOHANG);
if(pidret == child) {
fprintf(stderr, "rundispoff: child exited with status %d\n",
WEXITSTATUS(status));
child = 0;
}
}
CARD16 power_level;
BOOL dpms_state;
DPMSInfo(dpy, &power_level, &dpms_state);
if(power_level == DPMSModeOn) {
awake = true;
backoff = 10;
n = 0;
if(child > 0) {
fprintf(stderr, "rundispoff: display woke up, stopping child\n");
stop_subprocess();
}
} else {
if(child == 0) {
if(awake) {
fprintf(stderr, "rundispoff: display is asleep, starting child\n");
start_subprocess();
awake = false;
} else if(++n >= backoff) {
n = 0;
backoff *= 2;
if(backoff > 300) backoff = 300;
fprintf(stderr, "rundispoff: restarting process\n");
start_subprocess();
}
}
}
sleep(1);
}
return 0;
}