-
Notifications
You must be signed in to change notification settings - Fork 0
/
wakemeup.c
349 lines (305 loc) · 8.88 KB
/
wakemeup.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* ==========================================================================
*
* This code is copyright Nicolas Appriou, 2012-2013
*
* 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.
*
* ==========================================================================
* Filename: wakemeup.c
* ==========================================================================
*
* Created: 2012-11-14
* Last stable: 2012-11-14
*
* Author: Nicolas Appriou
* Email: [email protected]
*
* ==========================================================================
*
* Be sure to have the loop patch you can find this version on my github
* repository : http://github.com/Nicals/Beep
*
* ==========================================================================
* Changelog
* ==========================================================================
* 2013-02-07
* * The pid file is now set in the user repository.
* * Correcting pid file handling.
* 2012-12-10
* * Added the --robot options usefull for scripting multiples alarms.
*
*/
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
/*
* some global stuff
*/
#define VERSION "wakemeup 1.0"
#define MIN_CHALLENGE_NB 1000
#define MAX_CHALLENGE_NB 9999
#define RAND_MIN_MAX(min, max) ((rand() % (max - min + 1)) + min)
/* need it global to be sure to free it */
char* pid_filepath;
/*
* a few structures
*/
enum _switch {
ENABLED,
DISABLED
};
/* command line parameters */
struct _params {
/* setting up a challenge to kill a beep process */
enum _switch skip_challenge;
/* don't launch any challenge if a previous wakemeup launch have set up an alarm
* and if this one is still running */
enum _switch im_a_robot;
};
/*
* functions
*/
void termination_handler(int sig_num);
int challenge();
void start_alarm();
void stop_alarm(pid_t beep_pid);
void read_param(int argc, char* argv[], struct _params* params);
void usage(const char* prg_name);
/*
* Be sure to free all dynamically allocated variable on SIGINT
*/
void termination_handler(int sig_num) {
if (sig_num == SIGINT) {
free(pid_filepath);
exit(0);
}
}
/*
* Challenge the user before shutting down the running beep process.
* Returns 0 if the challenge is succesfully solved.
* Returns -1 if the user aborted the challenge.
*/
int challenge() {
unsigned int seed = (unsigned int)(time(NULL));
unsigned int a, b, result;
char user_input[49];
srand(seed);
a = RAND_MIN_MAX(MIN_CHALLENGE_NB, MAX_CHALLENGE_NB);
b = RAND_MIN_MAX(MIN_CHALLENGE_NB, MAX_CHALLENGE_NB);
result = a + b;
/* Only gods are allowed to skip this ! */
#ifdef GOD_MOD
fprintf(stdout, "Enter 'q' to exit the challenge\n");
#endif
do {
fprintf(stdout, "%u + %u ?\n", a, b);
fgets(user_input, 49, stdin);
#ifdef GOD_MOD
} while (atoi(user_input) != result && strncmp(user_input, "q", 49) == 0);
#else
} while (atoi(user_input) != result);
#endif
return atoi(user_input) == result ? 0 : -1;
}
/*
* Start alarm and set PID
* returns 0 on success and -1 on failure
*/
void start_alarm() {
FILE* pid_file;
pid_t beep_pid;
/* this is the apollo spacecraft alarm system */
char* args[] = {"beep", "-f750", "-l400", "-n", "-f2000", "-l400", "-i", NULL};
beep_pid = fork();
if (beep_pid == 0) {
execve("/usr/bin/beep", args, NULL);
fprintf(stderr, "ERROR: Fail to exec beep\n");
exit(3);
}
else if (beep_pid > 0) {
/* save pid file */
pid_file = fopen(pid_filepath, "w");
if (pid_file) {
fprintf(pid_file, "%u\n", beep_pid);
fclose(pid_file);
/* set some permission here */
chmod(pid_filepath, 0500);
fprintf(stdout, "INFO: Process [%u] started.\n", beep_pid);
}
else {
fprintf(stderr, "ERROR: Cannot write pid file %s (%s)",
pid_filepath, strerror(errno));
kill(beep_pid, SIGTERM);
}
}
else {
fprintf(stderr, "ERROR: Fail to fork.\n");
}
}
/*
* Kill the beep child process launch early and delete
* the PID file.
*/
void stop_alarm(pid_t beep_pid) {
if (kill(beep_pid, SIGTERM) == 0) {
fprintf(stdout, "INFO: Process [%u] killed.\n", beep_pid);
}
else {
fprintf(stdout, "INFO: Cannot kill process [%u]\n", beep_pid);
}
unlink(pid_filepath);
}
/*
* If the returned value is not below 0, the program will
* exit with this return code.
*/
void read_param(int argc, char* argv[], struct _params* params) {
int next_option;
const char* const short_options = "chrv";
const struct option long_options[] = {
{"robot", no_argument, NULL, 'r'},
{"challenge", no_argument, NULL, 'c'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{0, 0, 0, 0}
};
do {
next_option = getopt_long(argc, argv, short_options, long_options, NULL);
switch (next_option) {
case 'c':
#ifdef GOD_MOD
params->skip_challenge = ENABLED;
#endif
break;
case 'r':
params->im_a_robot = ENABLED;
break;
case 'h':
usage(argv[0]);
exit(0);
break;
case 'v':
printf("%s\n", VERSION);
printf("god mod: %s\n",
#ifdef GOD_MOD
"enabled"
#else
"disabled"
#endif
);
exit(0);
break;
case '?':
fprintf(stderr, "Unknown option -%c\nUse %s for more informations.\n\n", next_option, argv[0]);
exit(1);
break;
default:
break;
}
} while (next_option != -1);
}
/*
* Obvious function
*/
void usage(const char* prg_name) {
printf("\nUsage: %s [-r] [-c] [-h] [-v]"
"\n\n"
"Launch an alarm on your computer to wake up early on the morning.\n\n"
"optional arguments:\n"
"\t -c --challenge switch challenge off. Only with god mod enabled ad compile time\n"
"\t -r --robot don't launch the challenge, don't stop the current alarm\n"
"\t -h --help print this help\n"
"\t -v --version get version and compile option\n"
"\n"
,
prg_name);
}
/*
* Entry point
*/
int main(int argc, char* argv[]) {
struct _params params;
enum _switch stop_alarm_ok = DISABLED;
FILE* pid_file;
pid_t beep_pid = 0;
char* home_dir;
const char* pid_filename = "/.wakemeup.pid";
struct sigaction sig_handler;
/* set default parameters */
params.skip_challenge = DISABLED;
params.im_a_robot = DISABLED;
read_param(argc, argv, ¶ms);
/* build full pid pathname from $HOME/<pid_filename> */
home_dir = getenv("HOME");
if (!home_dir) {
fprintf(stderr, "ERR: HOME environment variable not defined.\n");
return 2;
}
pid_filepath = malloc(sizeof(char) * (strlen(pid_filename) + strlen(home_dir) + 1));
if (!pid_filepath) {
fprintf(stderr, "ERR: Maloc error.\n");
return 3;
}
strcpy(pid_filepath, home_dir);
strcat(pid_filepath, pid_filename);
/* now we've malloc the stuff, make sur we will destroy it in any case */
memset(&sig_handler, 0x00, sizeof(struct sigaction));
sig_handler.sa_handler = termination_handler;
sigaction(SIGINT, &sig_handler, NULL);
/* Check if the pid file exists.
* If it doesn't exists, create it and launch a beep alarm.
* If it exists, get the pid written in it and kill the
* process.
*/
if (!(pid_file = fopen(pid_filepath, "r"))) {
start_alarm();
}
else {
/* the file contains only one line : the pid of the beep alarm
* currently running.
*/
fscanf(pid_file, "%u\n", &beep_pid);
fclose(pid_file);
/* check if the process is still alive */
if (kill(beep_pid, 0) < 0) {
if (errno == ESRCH) {
/* If we get there, it may be because a pid file
* is still on hard drive but the process isn't running.
*/
unlink(pid_filepath);
fprintf(stdout, "INFO: deleting an obsolete PID file [%u].\n", beep_pid);
start_alarm();
}
else if (errno == EPERM) {
fprintf(stderr, "ERR: don't have the permission to contact running beep instance [%u].\n", beep_pid);
}
}
else {
/* only on god mod */
if (params.skip_challenge == ENABLED)
stop_alarm_ok = ENABLED;
else if (params.im_a_robot == DISABLED)
stop_alarm_ok = (challenge() == 0 ? ENABLED : DISABLED);
if (stop_alarm_ok == ENABLED)
stop_alarm(beep_pid);
}
}
free(pid_filepath);
return 0;
}