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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
|
/*
* priod.c: process reprioritizing daemon
*
* Thanks to http://netsplit.com/the-proc-connector-and-socket-filters
* for showing the way around the proc connector and BPF.
*
* Copyright (c) 2017, Přemysl Janouch <p.janouch@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#define _GNU_SOURCE
#define LIBERTY_WANT_POLLER
#include "config.h"
#undef PROGRAM_NAME
#define PROGRAM_NAME "priod"
#include "liberty/liberty.c"
#include <dirent.h>
#include <linux/cn_proc.h>
#include <linux/netlink.h>
#include <linux/connector.h>
#include <linux/filter.h>
#include <sys/resource.h>
#include <sys/syscall.h>
// --- Main program ------------------------------------------------------------
#define RULE_UNSET INT_MIN
struct rule
{
char *program_name; ///< Program name to match against
int oom_score_adj; ///< For /proc/%/oom_score_adj
int prio; ///< For setpriority()
int ioprio; ///< For SYS_ioprio_set
};
struct app_context
{
struct poller poller; ///< Poller
bool polling; ///< The event loop is running
int proc_fd; ///< Proc connector FD
struct poller_fd proc_event; ///< Proc connector read event
struct rule *rules; ///< Rules
size_t rules_len; ///< Number of rules
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
log_message_custom (void *user_data, const char *quote, const char *fmt,
va_list ap)
{
(void) user_data;
FILE *stream = stdout;
// TODO: sd-daemon.h log level prefixes?
fputs (quote, stream);
vfprintf (stream, fmt, ap);
fputs ("\n", stream);
}
// --- Configuration -----------------------------------------------------------
static bool
load_integer (struct str_map *root, const char *key, int min, int max,
int *value, struct error **e)
{
*value = RULE_UNSET;
struct config_item *item;
if (!(item = str_map_find (root, key)))
return true;
if (item->type != CONFIG_ITEM_INTEGER
|| item->value.integer < min || item->value.integer > max)
return error_set (e, "%s: must be an integer (%d..%d)", key, min, max);
*value = item->value.integer;
return true;
}
static bool
load_rule (const char *name, struct str_map *m, struct rule *r,
struct error **e)
{
r->program_name = xstrdup (name);
if (!load_integer (m, "oom_score_adj", -1000, 1000, &r->oom_score_adj, e)
|| !load_integer (m, "prio", -20, 19, &r->prio, e)
|| !load_integer (m, "ioprio", 0, 7, &r->ioprio, e))
return false;
return true;
}
static struct rule *
find_rule (struct app_context *ctx, const char *program_name)
{
for (size_t i = 0; i < ctx->rules_len; i++)
if (!strcmp (ctx->rules[i].program_name, program_name))
return ctx->rules + i;
return NULL;
}
static void
load_configuration (struct app_context *ctx, const char *config_path)
{
struct error *e = NULL;
struct config_item *root = config_read_from_file (config_path, &e);
if (e)
{
print_error ("error loading configuration: %s", e->message);
error_free (e);
exit (EXIT_FAILURE);
}
struct str_map_iter iter;
str_map_iter_init (&iter, &root->value.object);
ctx->rules = xcalloc (iter.map->len, sizeof *ctx->rules);
ctx->rules_len = 0;
struct config_item *subtree;
while ((subtree = str_map_iter_next (&iter)))
{
const char *path = iter.link->key;
if (subtree->type != CONFIG_ITEM_OBJECT)
exit_fatal ("rule `%s' in configuration is not an object", path);
if (!load_rule (path, &subtree->value.object,
&ctx->rules[ctx->rules_len++], &e))
exit_fatal ("rule `%s': %s", path, e->message);
}
}
// --- Signals -----------------------------------------------------------------
static int g_signal_pipe[2]; ///< A pipe used to signal... signals
static void
sigterm_handler (int signum)
{
(void) signum;
int original_errno = errno;
if (write (g_signal_pipe[1], "", 1) == -1)
soft_assert (errno == EAGAIN);
errno = original_errno;
}
static void
setup_signal_handlers (void)
{
if (pipe (g_signal_pipe) == -1)
exit_fatal ("%s: %s", "pipe", strerror (errno));
set_cloexec (g_signal_pipe[0]);
set_cloexec (g_signal_pipe[1]);
// So that the pipe cannot overflow; it would make write() block within
// the signal handler, which is something we really don't want to happen.
// The same holds true for read().
set_blocking (g_signal_pipe[0], false);
set_blocking (g_signal_pipe[1], false);
(void) signal (SIGPIPE, SIG_IGN);
struct sigaction sa;
sa.sa_flags = SA_RESTART;
sa.sa_handler = sigterm_handler;
sigemptyset (&sa.sa_mask);
if (sigaction (SIGINT, &sa, NULL) == -1
|| sigaction (SIGTERM, &sa, NULL) == -1)
exit_fatal ("sigaction: %s", strerror (errno));
}
// --- Main program ------------------------------------------------------------
// IO priorities are a sort-of-private kernel API with no proper headers
enum
{
IOPRIO_CLASS_NONE,
IOPRIO_CLASS_RT,
IOPRIO_CLASS_BE,
IOPRIO_CLASS_IDLE,
};
enum
{
IOPRIO_WHO_PROCESS = 1,
IOPRIO_WHO_PGRP,
IOPRIO_WHO_USER,
};
#define IOPRIO_CLASS_SHIFT 13
static void
adj_oom_score (int pid, const char *program_name, int score)
{
char buf[16]; snprintf (buf, sizeof buf, "%d\n", score);
char *path = xstrdup_printf ("/proc/%d/oom_score_adj", pid);
struct error *e = NULL;
if (!write_file (path, buf, strlen (buf), &e))
{
print_error ("%d (%s): %s", pid, program_name, e->message);
error_free (e);
}
free (path);
}
static bool
reprioritize (int pid, const char *program_name, DIR *dir, struct rule *rule,
struct str_map *set)
{
size_t not_previously_visited = 0;
struct dirent *iter;
while ((errno = 0, iter = readdir (dir)))
{
int tid = atoi (iter->d_name);
if (!tid || str_map_find (set, iter->d_name))
continue;
print_debug (" - thread %d", tid);
str_map_set (set, iter->d_name, (void *) ++not_previously_visited);
if (RULE_UNSET != rule->prio
&& setpriority (PRIO_PROCESS, pid, rule->prio))
print_error ("%d (%s): thread %d: setpriority: %s",
pid, program_name, tid, strerror (errno));
if (RULE_UNSET != rule->ioprio
&& syscall (SYS_ioprio_set, IOPRIO_WHO_PROCESS, tid,
IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT | rule->ioprio))
print_error ("%d (%s): thread %d: ioprio_set: %s",
pid, program_name, tid, strerror (errno));
}
if (errno)
{
print_error ("%d (%s): readdir: %s",
pid, program_name, strerror (errno));
}
return not_previously_visited == 0;
}
static void
on_exec_name (struct app_context *ctx, int pid, const char *program_name)
{
// TODO: we might want to at least provide more criteria to match on,
// so as to not blindly trust everything, despite these priorities being
// relatively harmless if you overlook possible "denial of service"
struct rule *rule = find_rule (ctx, program_name);
const char *slash = strrchr (program_name, '/');
if (!rule && (!slash || !(rule = find_rule (ctx, slash + 1))))
return;
print_debug ("%d (%s) matched", pid, program_name);
if (RULE_UNSET != rule->oom_score_adj)
adj_oom_score (pid, program_name, rule->oom_score_adj);
// Priority APIs are strictly per-thread (i.e. Linux "task"), so we must
// iterate through all tasks within a thread group
char *path = xstrdup_printf ("/proc/%d/task", pid);
DIR *dir = opendir (path);
free (path);
if (!dir)
{
print_error ("%d (%s): opendir: %s",
pid, program_name, strerror (errno));
return;
}
struct str_map set;
str_map_init (&set);
// This has an inherent race condition, but let's give it a try
for (size_t retries = 3; retries--; )
if (reprioritize (pid, program_name, dir, rule, &set))
break;
str_map_free (&set);
closedir (dir);
}
static void
on_exec (struct app_context *ctx, int pid)
{
// This is inherently racy but there seems to be no better way to do it
char *path = xstrdup_printf ("/proc/%d/cmdline", pid);
struct str cmdline;
str_init (&cmdline);
struct error *e = NULL;
if (read_file (path, &cmdline, &e))
on_exec_name (ctx, pid, cmdline.str);
else
{
print_debug ("%s", e->message);
error_free (e);
}
free (path);
str_free (&cmdline);
}
static void
preapply_rules (struct app_context *ctx)
{
DIR *dir = opendir ("/proc");
if (!dir)
{
print_error ("opendir: %s: %s", "/proc", strerror (errno));
return;
}
// We don't care about processes deleted or created during this loop
struct dirent *iter;
while ((errno = 0, iter = readdir (dir)))
{
int pid = atoi (iter->d_name);
if (pid && (iter->d_type == DT_UNKNOWN || iter->d_type == DT_DIR))
on_exec (ctx, pid);
}
closedir (dir);
}
static void
on_netlink_message (struct app_context *ctx, struct nlmsghdr *mh)
{
// In practice the kernel connector never sends multipart messages
if (!soft_assert (mh->nlmsg_type != 0)
|| !soft_assert (mh->nlmsg_flags == 0)
|| mh->nlmsg_type != NLMSG_DONE)
return;
struct cn_msg *m = NLMSG_DATA (mh);
if (m->id.idx != CN_IDX_PROC
|| m->id.val != CN_VAL_PROC)
return;
// XXX: potential alignment issues
struct proc_event *e = (struct proc_event *) m->data;
if (e->what == PROC_EVENT_EXEC)
on_exec (ctx, e->event_data.exit.process_tgid);
}
static void
on_event (const struct pollfd *pfd, struct app_context *ctx)
{
char buf[sysconf (_SC_PAGESIZE)];
struct sockaddr_nl addr;
while (true)
{
socklen_t addr_len = sizeof addr;
ssize_t len = recvfrom (pfd->fd, buf, sizeof buf, 0,
(struct sockaddr *) &addr, &addr_len);
if (len == 0)
exit_fatal ("socket closed");
if (len < 0 && errno == EAGAIN)
return;
if (len < 0)
exit_fatal ("recvfrom: %s", strerror (errno));
// Make sure it comes from the kernel
if (addr.nl_pid)
continue;
// In practice the kernel connector always sends one per dgram
for (struct nlmsghdr *mh = (struct nlmsghdr *) buf;
NLMSG_OK (mh, len); mh = NLMSG_NEXT (mh, len))
on_netlink_message (ctx, mh);
}
}
static void
on_signal_pipe_readable (const struct pollfd *fd, struct app_context *ctx)
{
char id = 0;
(void) read (fd->fd, &id, 1);
ctx->polling = false;
}
static const char *
parse_program_arguments (int argc, char **argv)
{
static const struct opt opts[] =
{
{ 'd', "debug", NULL, 0, "run in debug mode" },
{ 'h', "help", NULL, 0, "display this help and exit" },
{ 'V', "version", NULL, 0, "output version information and exit" },
{ 0, NULL, NULL, 0, NULL }
};
struct opt_handler oh;
opt_handler_init (&oh, argc, argv, opts, "CONFIG",
"Process reprioritizing daemon.");
int c;
while ((c = opt_handler_get (&oh)) != -1)
switch (c)
{
case 'd':
g_debug_mode = true;
break;
case 'h':
opt_handler_usage (&oh, stdout);
exit (EXIT_SUCCESS);
case 'V':
printf (PROGRAM_NAME " " PROGRAM_VERSION "\n");
exit (EXIT_SUCCESS);
default:
print_error ("wrong options");
opt_handler_usage (&oh, stderr);
exit (EXIT_FAILURE);
}
argc -= optind;
argv += optind;
if (argc != 1)
{
opt_handler_usage (&oh, stderr);
exit (EXIT_FAILURE);
}
opt_handler_free (&oh);
return argv[0];
}
/// Sets up a filter so that we're only woken up by the kernel on exec() events
static void
setup_exec_filter (int fd)
{
struct incoming
{
union { struct nlmsghdr netlink; char align[NLMSG_HDRLEN]; };
struct cn_msg connector;
struct proc_event event;
}
__attribute__ ((packed));
// Byteswapping is needed because the netlink protocol is host-endian
struct sock_filter filter[] =
{
// Only continue filtering dgrams with one "proc_event" message in them
BPF_STMT (BPF_LD | BPF_W | BPF_LEN, 0),
BPF_JUMP (BPF_JMP | BPF_JEQ | BPF_K, sizeof (struct incoming), 0, 9),
BPF_STMT (BPF_LD | BPF_H | BPF_ABS,
offsetof (struct incoming, netlink.nlmsg_type)),
BPF_JUMP (BPF_JMP | BPF_JEQ | BPF_K, htons (NLMSG_DONE), 0, 7),
BPF_STMT (BPF_LD | BPF_W | BPF_ABS,
offsetof (struct incoming, connector.id.idx)),
BPF_JUMP (BPF_JMP | BPF_JEQ | BPF_K, htonl (CN_IDX_PROC), 0, 5),
BPF_STMT (BPF_LD | BPF_W | BPF_ABS,
offsetof (struct incoming, connector.id.val)),
BPF_JUMP (BPF_JMP | BPF_JEQ | BPF_K, htonl (CN_VAL_PROC), 0, 3),
BPF_STMT (BPF_LD | BPF_W | BPF_ABS,
offsetof (struct incoming, event.what)),
BPF_JUMP (BPF_JMP | BPF_JEQ | BPF_K, htonl (PROC_EVENT_EXEC), 1, 0),
BPF_STMT (BPF_RET | BPF_K, 0),
BPF_STMT (BPF_RET | BPF_K, 0xffffffff),
};
struct sock_fprog fprog = { .filter = filter, .len = N_ELEMENTS (filter) };
const int yes = 1;
if (setsockopt (fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof fprog) < 0
|| setsockopt (fd, SOL_NETLINK, NETLINK_NO_ENOBUFS, &yes, sizeof yes) < 0)
print_error ("setsockopt: %s", strerror (errno));
}
int
main (int argc, char *argv[])
{
g_log_message_real = log_message_custom;
const char *config_path = parse_program_arguments (argc, argv);
struct app_context ctx;
memset (&ctx, 0, sizeof ctx);
poller_init (&ctx.poller);
setup_signal_handlers ();
struct poller_fd signal_event;
poller_fd_init (&signal_event, &ctx.poller, g_signal_pipe[0]);
signal_event.dispatcher = (poller_fd_fn) on_signal_pipe_readable;
signal_event.user_data = &ctx;
poller_fd_set (&signal_event, POLLIN);
load_configuration (&ctx, config_path);
ctx.proc_fd = socket (PF_NETLINK,
SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_CONNECTOR);
if (ctx.proc_fd < 0)
exit_fatal ("cannot make a proc connector: %s", strerror (errno));
setup_exec_filter (ctx.proc_fd);
struct sockaddr_nl addr = { .nl_family = AF_NETLINK, .nl_pid = getpid (),
.nl_groups = CN_IDX_PROC };
if (bind (ctx.proc_fd, (struct sockaddr *) &addr, sizeof addr) < 0)
exit_fatal ("cannot make a proc connector: %s", strerror (errno));
struct
{
union { struct nlmsghdr netlink; char align[NLMSG_HDRLEN]; };
struct cn_msg connector;
enum proc_cn_mcast_op op;
}
__attribute__ ((packed)) subscription =
{
.netlink.nlmsg_len = sizeof subscription,
.netlink.nlmsg_type = NLMSG_DONE,
.netlink.nlmsg_pid = getpid (),
.connector.id.idx = CN_IDX_PROC,
.connector.id.val = CN_VAL_PROC,
.connector.len = sizeof subscription.op,
.op = PROC_CN_MCAST_LISTEN,
};
if (write (ctx.proc_fd, &subscription, sizeof subscription) < 0)
exit_fatal ("failed to subscribe for events: %s", strerror (errno));
poller_fd_init (&ctx.proc_event, &ctx.poller, ctx.proc_fd);
ctx.proc_event.dispatcher = (poller_fd_fn) on_event;
ctx.proc_event.user_data = &ctx;
poller_fd_set (&ctx.proc_event, POLLIN);
// While new events are being queued, we can apply rules to already
// existing processes, so that we don't miss anything except for obvious
// cases when a process re-execs to something else after a match.
// It would inherit the same values anyway, so it seems to be mostly okay.
preapply_rules (&ctx);
ctx.polling = true;
while (ctx.polling)
poller_run (&ctx.poller);
poller_free (&ctx.poller);
xclose (ctx.proc_fd);
for (size_t i = 0; i < ctx.rules_len; i++)
free (ctx.rules[i].program_name);
free (ctx.rules);
return EXIT_SUCCESS;
}
|