-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.c
540 lines (408 loc) · 13.2 KB
/
daemon.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <X11/extensions/scrnsaver.h>
#include <argp.h>
#include <signal.h>
#include <syslog.h>
#include <fcntl.h>
#include <gio/gio.h>
#include "config.h"
#include "measurement.pb-c.h"
#define PARENT_COUNT 15
static unsigned int foreground = 0;
static unsigned int verbose = 0;
static unsigned int verbose_level = 0;
static unsigned int interval = 60; // seconds, should be called measurement interval
Measurement measurements[PARENT_COUNT]; // the number of measurements within a flush
int idx = 0;
Measurements parent;
void *parent_buf;
unsigned parent_len;
char pipeFileName[1032];
void create_measurements(unsigned int interval, const char* zone) {
Measurements msgs = MEASUREMENTS__INIT;
msgs.interval = interval;
size_t size = strlen(zone);
msgs.zone = malloc(sizeof(char) * size + 1);
strcpy(msgs.zone, zone);
msgs.n_measurements = idx;
msgs.measurements = malloc(sizeof(Measurement) * idx);
for (int i = 0; i < idx; i++) {
msgs.measurements[i] = malloc(sizeof(Measurement));
measurement__init(msgs.measurements[i]);
msgs.measurements[i]->kind = measurements[i].kind;
msgs.measurements[i]->idle = measurements[i].idle;
msgs.measurements[i]->timestamp = measurements[i].timestamp;
}
parent_len = measurements__get_packed_size(&msgs);
parent_buf = malloc(parent_len);
measurements__pack(&msgs, parent_buf);
parent = msgs;
}
void internal_log(int pri, const char *fmt, ...) {
va_list vl;
va_start(vl, fmt);
if (foreground) {
vprintf(fmt, vl);
printf("\n");
fflush(stdout);
} else {
vsyslog(pri, fmt, vl);
}
va_end(vl);
}
void flush_pipe() {
if (verbose) internal_log(LOG_DEBUG, "Writing message to pipe.");
// Print a message on the named pipe
int pipe = open(pipeFileName, O_RDWR); // Not O_WRONLY or we will hang if no reader
ssize_t size = write(pipe, "FLUSHED\n", 8);
if (size != 8) {
internal_log(LOG_ERR, "Unexpected write size %ld.\n", size);
}
close(pipe);
}
void flush_measurements(int pipe) {
if (idx == 0) {
if (pipe) flush_pipe();
if (verbose) internal_log(LOG_DEBUG, "No measurements to flush.");
return; // Nothing to flush
}
if (verbose) internal_log(LOG_DEBUG, "Flushing measurements at index %d.", idx);
const time_t now = time(NULL);
const struct tm *local_now = localtime(&now);
char formatTimeBuffer[20];
strftime(formatTimeBuffer, 20, "%FT%TZ", local_now);
char* home = getenv("HOME");
char fileName[1024];
snprintf(fileName, 1024, "%s/.flextime/data/%s.bin", home, formatTimeBuffer);
if(access(fileName, F_OK ) != -1) {
// The file already exists. Since the name of the file is the time in
// ISO8601 format resolved to seconds, it means that the data is flushed
// already.
if (idx == 1) {
// This can happen, 1/60 of the times we flush within the same second
internal_log(LOG_WARNING, "One measurement is discarded for file %s.", fileName);
}
if (idx > 1) {
// This can never happen with a measurement interval of one minute
internal_log(LOG_ERR, "%d measurements are discarded for file %s.", idx, fileName);
}
if (idx == 0) {
// This should never happen
internal_log(LOG_WARNING, "Zero measurements are discarded for file %s.", fileName);
}
return;
}
const char* zone = local_now->tm_zone;
create_measurements(interval, zone);
FILE *file = fopen(fileName, "w+");
fwrite(parent_buf, parent_len, 1, file);
fclose(file);
if (pipe) flush_pipe();
if (verbose) internal_log(LOG_DEBUG, "Flushed measurements to file %s.", fileName);
}
void initialize_measurements() {
for (int i = 0; i < PARENT_COUNT; i++) {
Measurement msg = MEASUREMENT__INIT;
parent_len = measurement__get_packed_size(&msg);
parent_buf = malloc(parent_len);
measurement__pack(&msg, parent_buf);
measurements[i] = msg;
}
}
#if defined USE_GIO && USE_GIO == 1
long
get_idle_time_internal (GDBusProxy *proxy)
{
guint64 user_idle_time;
gchar *method = "GetIdletime";
GError *error = NULL;
GVariant *ret = NULL;
ret = g_dbus_proxy_call_sync(proxy,
method,
NULL,
G_DBUS_CALL_FLAGS_NONE, -1,
NULL, &error);
if (!ret) {
g_dbus_error_strip_remote_error (error);
if (verbose) internal_log(LOG_DEBUG, "GetIdletime returned %s.", error->message);
g_error_free (error);
return -1;
}
g_variant_get (ret, "(t)", &user_idle_time);
g_variant_unref (ret);
return user_idle_time / 1000;
}
long
get_idle_time ()
{
GDBusProxy *proxy = NULL;
gchar *name = "org.gnome.Mutter.IdleMonitor";
gchar *object_path = "/org/gnome/Mutter/IdleMonitor/Core";
gchar *interface_name = "org.gnome.Mutter.IdleMonitor";
/* Create a D-Bus proxy */
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
name,
object_path,
interface_name,
NULL, NULL);
g_assert (proxy != NULL);
long result = get_idle_time_internal (proxy);
g_object_unref (proxy);
return result;
}
#else /* USE_GIO */
// https://stackoverflow.com/a/4702411
long get_idle_time() {
time_t idle_time;
static XScreenSaverInfo *mit_info;
Display *display;
int screen;
mit_info = XScreenSaverAllocInfo();
if((display=XOpenDisplay(NULL)) == NULL) {
return(-1);
}
screen = DefaultScreen(display);
XScreenSaverQueryInfo(display, RootWindow(display,screen), mit_info);
idle_time = (mit_info->idle) / 1000;
XFree(mit_info);
XCloseDisplay(display);
return idle_time;
}
#endif /* USE_GIO */
int finishing = 0;
// sleep(3) can be interrupted by signals
void sleep_with_interrupt(unsigned int seconds) {
unsigned int left = seconds;
while ((left > 0) && (!finishing)) {
left = sleep (left);
}
}
void create_measurement(Measurement__Kind kind, unsigned int idle) {
time_t now = time(NULL);
measurements[idx].kind = kind;
measurements[idx].idle = idle;
measurements[idx].timestamp = (intmax_t)now;
idx++;
if (verbose) internal_log(LOG_DEBUG, "Created measurement; index now at %d (of %d).", idx, PARENT_COUNT);
if (idx == PARENT_COUNT) {
flush_measurements(0);
idx = 0;
}
}
void create_measurement_maybe() {
long idle = get_idle_time();
// If the idle time is larger than the interval, we do not create a
// measurement since there is nothing to add to the already created
// measurements. The time between measurement is not perfect, so be on
// the safe side, we compared to a somewhat larger value.
if (idle > interval * 1.5) {
if (verbose) internal_log(LOG_DEBUG, "Did not create measurement since idle is too large (%d compared to %f).", idle, interval * 1.5);
return;
}
create_measurement(MEASUREMENT__KIND__MEASUREMENT, idle);
}
void sighandler(int signum) {
// In general, our approach to flushing data in a signal handler is
// wrong. If the main program is in the process of writing data to
// files, then since the signal handler is also writing to files, the
// state of the allocated data buffers are simply wrong.
//
// We need to fix that, and avoid using stdio functions from the signal
// handler. In fact, only functions listed in signal-safety(7) can be
// used in a signal handler.
//
// https://stackoverflow.com/a/55179208
// https://man7.org/linux/man-pages/man7/signal-safety.7.html
internal_log(LOG_NOTICE, "Caught signal %d, flushing %d measurements.", signum, idx);
int shutdown = signum != SIGUSR1;
if (shutdown) {
if (verbose) internal_log(LOG_DEBUG, "Shutdown measurement created.");
create_measurement(MEASUREMENT__KIND__STOP, 0);
}
if (shutdown)
flush_measurements(0);
else
flush_measurements(1);
idx = 0;
if (shutdown) {
closelog();
internal_log (LOG_NOTICE, "Flextime daemon %s terminated.", PACKAGE_VERSION);
_exit(0);
}
}
// https://github.com/pasce/daemon-skeleton-linux-c
//
// This might all be wrong-headed, and we should not do it at all:
// https://unix.stackexchange.com/a/177361
static void skeleton_daemon()
{
if (verbose) internal_log(LOG_DEBUG, "Forking a daemon process.");
pid_t pid;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
/* On success: The child process becomes session leader */
if (setsid() < 0)
exit(EXIT_FAILURE);
/* Catch, ignore and handle signals */
/*TODO: Implement a working signal handler */
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
/* Fork off for the second time*/
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
/* Set new file permissions */
umask(0);
/* Change the working directory to the root directory */
/* or another appropriated directory */
int result = chdir("/");
if (result != 0) {
internal_log(LOG_ERR, "Unable to change to root directory (%d).", result);
}
/* Close all open file descriptors */
int x;
for (x = sysconf(_SC_OPEN_MAX); x>=0; x--)
{
close (x);
}
/* Open the log file */
openlog ("flextime", LOG_PID, LOG_DAEMON);
}
const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
static char doc[] = "Flextime -- tracking working hours";
/* A description of the arguments we accept. */
static char args_doc[] = "ARG1 ARG2";
/* The options we understand. */
static struct argp_option options[] = {
{"foreground", 'f', 0, 0, "Run in the foreground, not as a daemon" },
{"verbose", 'v', 0, 0, "Print verbose output" },
{ 0 }
};
/* Used by main to communicate with parse_opt. */
struct arguments
{
char *args[2]; /* arg1 & arg2 */
int foreground;
int verbose;
int verbose_level;
};
/* Parse a single option. */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
switch (key)
{
case 'f':
arguments->foreground = 1;
break;
case 'v':
arguments->verbose = 1;
arguments->verbose_level += 1;
break;
case ARGP_KEY_ARG:
if (state->arg_num >= 2)
/* Too many arguments. */
argp_usage (state);
arguments->args[state->arg_num] = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 0)
/* Not enough arguments. */
argp_usage (state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Our argp parser. */
static struct argp argp = { options, parse_opt, args_doc, doc };
int main(int argc, char **argv)
{
struct arguments arguments;
/* Default values. */
arguments.foreground = 0;
arguments.verbose = 0;
arguments.verbose_level = 0;
/* Parse our arguments; every option seen by parse_opt will
be reflected in arguments. */
argp_parse (&argp, argc, argv, 0, 0, &arguments);
foreground = arguments.foreground;
verbose = arguments.verbose;
verbose_level = arguments.verbose_level;
char* home = getenv("HOME");
if (!foreground) {
skeleton_daemon();
internal_log(LOG_NOTICE, "Flextime daemon %s started.", PACKAGE_VERSION);
} else {
if (verbose) internal_log(LOG_DEBUG, "Running in the foreground.");
}
if (verbose_level > 1) {
internal_log(LOG_INFO, "Verbose level %d specified.", verbose_level);
}
if (verbose && (verbose_level > 1)) {
#if defined USE_GIO && USE_GIO == 1
internal_log(LOG_DEBUG, "Collecting measurements using D-Bus.");
#else
internal_log(LOG_DEBUG, "Collecting measurements using X11.");
#endif /* USE_GIO */
}
char topFolderName[1024];
snprintf(topFolderName, 1024, "%s/.flextime", home);
char dataFolderName[1024];
snprintf(dataFolderName, 1024, "%s/.flextime/data", home);
if (0 == mkdir(topFolderName, 0750)) {
mkdir(dataFolderName, 0750);
internal_log(LOG_NOTICE, "Created folder %s.", dataFolderName);
}
snprintf(pipeFileName, 1032, "%s/flushed", topFolderName);
int pipeFileResult = mkfifo(pipeFileName, S_IRWXU | S_IRGRP);
if (pipeFileResult != 0) {
if (errno != EEXIST) {
// Log a warning and keep going
internal_log(LOG_WARNING, "Error %d when creating pipe %s (continuing).", errno, pipeFileName);
}
}
signal(SIGINT, sighandler);
signal(SIGHUP, sighandler);
signal(SIGQUIT, sighandler);
signal(SIGABRT, sighandler);
signal(SIGTSTP, sighandler);
signal(SIGTERM, sighandler);
signal(SIGUSR1, sighandler);
initialize_measurements();
create_measurement(MEASUREMENT__KIND__START, 0);
sleep_with_interrupt(interval);
while (1) {
create_measurement_maybe();
sleep_with_interrupt(interval);
}
if (!foreground) {
internal_log(LOG_NOTICE, "Flextime daemon %s terminated (should never happen in main).", PACKAGE_VERSION);
closelog();
}
return 1; // We never terminate from main
}