forked from heftig/rtkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtkit-daemon.c
2347 lines (1833 loc) · 82.4 KB
/
rtkit-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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of RealtimeKit.
Copyright 2009 Lennart Poettering
Copyright 2010 Maarten Lankhorst
RealtimeKit 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.
RealtimeKit 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 RealtimeKit. If not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <sched.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <inttypes.h>
#include <dbus/dbus.h>
#include <pwd.h>
#include <sys/capability.h>
#include <sys/prctl.h>
#include <time.h>
#include <assert.h>
#include <getopt.h>
#include <signal.h>
#include <sys/poll.h>
#include <sys/eventfd.h>
#include <pthread.h>
#include <dirent.h>
#include <syslog.h>
#include <grp.h>
#ifdef HAVE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif
#include "rtkit.h"
#ifndef __linux__
#error "This stuff only works on Linux!"
#endif
#ifndef SCHED_RESET_ON_FORK
/* "Your libc lacks the definition of SCHED_RESET_ON_FORK. We'll now define it ourselves, however make sure your kernel is new enough! */
#define SCHED_RESET_ON_FORK 0x40000000
#endif
#ifndef RLIMIT_RTTIME
#define RLIMIT_RTTIME 15
#endif
/* The introspection XML from org.freedesktop.RealtimeKit1.xml */
static const char introspect_xml[] = {
#include "xml-introspection.h"
,0x00};
/* Similar to assert(), but has side effects, and hence shall never be optimized away, regardless of NDEBUG */
#define assert_se(expr) \
do { \
if (__builtin_expect(!(expr), 0)) { \
fprintf(stderr, "Assertion %s failed at %s:%u, function %s(). Aborting.\n", #expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
abort(); \
} \
} while(0)
#define ELEMENTSOF(x) (sizeof(x)/sizeof(x[0]))
#define TIMESPEC_MSEC(ts) (((int64_t) (ts).tv_sec * 1000LL) + ((int64_t) (ts).tv_nsec / 1000000LL))
/* If we actually execute a request we temporarily upgrade our realtime priority to this level */
static unsigned our_realtime_priority = 21;
/* Normally we run at this nice level */
static int our_nice_level = 1;
/* The maximum realtime priority to hand out */
static unsigned max_realtime_priority = 20;
/* The minimum nice level to hand out */
static int min_nice_level = -15;
/* Username we shall run under */
static const char *username = "rtkit";
/* Enforce that clients have RLIMIT_RTTIME set to a value <= this */
static unsigned long long rttime_usec_max = 200000ULL; /* 200 ms */
/* How many users do we supervise at max? */
static unsigned users_max = 2048;
/* How many processes of a single user do we supervise at max? */
static unsigned processes_per_user_max = 15;
/* How many threads of a single user do we supervise at max? */
static unsigned threads_per_user_max = 25;
/* Refuse further requests if one user issues more than ACTIONS_PER_BURST_MAX in this time */
static unsigned actions_burst_sec = 20;
/* Refuse further requests if one user issues more than this many in ACTIONS_BURST_SEC time */
static unsigned actions_per_burst_max = 25;
/* Drop privileges */
static bool do_drop_privileges = TRUE;
/* Change root directory to /proc */
static bool do_chroot = TRUE;
/* Limit resources */
static bool do_limit_resources = TRUE;
/* Run a canary watchdog */
static bool do_canary = TRUE;
/* Canary cheep interval */
static unsigned canary_cheep_msec = 5000; /* 5s */
/* Canary watchdog interval */
static unsigned canary_watchdog_msec = 10000; /* 10s */
/* Watchdog realtime priority */
static unsigned canary_watchdog_realtime_priority = 99;
/* How long after the canary died shall we refuse further RT requests? */
static unsigned canary_refusal_sec = 5*60;
/* Demote root processes? */
static bool canary_demote_root = FALSE;
/* Demote unknown processes? */
static bool canary_demote_unknown = FALSE;
/* Log to stderr? */
static bool log_stderr = FALSE;
/* Scheduling policy to use */
static int sched_policy = SCHED_RR;
struct thread {
/* We use the thread id plus its starttime as a unique identifier for threads */
pid_t pid;
unsigned long long starttime;
struct thread *next;
};
struct process {
/* We use the process id plus its starttime as a unique identifier for processes */
pid_t pid;
unsigned long long starttime;
struct thread *threads;
struct process *next;
};
struct rtkit_user {
uid_t uid;
time_t timestamp;
unsigned n_actions;
struct process *processes;
unsigned n_processes;
unsigned n_threads;
struct rtkit_user *next;
};
static struct rtkit_user *users = NULL;
static unsigned n_users = 0;
static unsigned n_total_processes = 0;
static unsigned n_total_threads = 0;
static const char *proc = NULL;
static int quit_fd = -1, canary_fd = -1;
static pthread_t canary_thread_id = 0, watchdog_thread_id = 0;
static volatile uint32_t refuse_until = 0;
static const char *get_proc_path(void) {
/* Useful for chroot environments */
if (proc)
return proc;
return "/proc";
}
static char* get_user_name(uid_t uid, char *user, size_t len) {
struct passwd *pw;
if ((pw = getpwuid(uid))) {
strncpy(user, pw->pw_name, len-1);
user[len-1] = 0;
return user;
}
snprintf(user, len-1, "%llu", (unsigned long long) uid);
user[len-1] = 0;
return user;
}
static int read_starttime(pid_t pid, pid_t tid, unsigned long long *st) {
char fn[128], line[256], *p;
int r;
FILE *f;
if (tid != 0)
assert_se(snprintf(fn, sizeof(fn)-1, "%s/%llu/task/%llu/stat", get_proc_path(), (unsigned long long) pid, (unsigned long long) tid) < (int) (sizeof(fn)-1));
else
assert_se(snprintf(fn, sizeof(fn)-1, "%s/%llu/stat", get_proc_path(), (unsigned long long) pid) < (int) (sizeof(fn)-1));
fn[sizeof(fn)-1] = 0;
if (!(f = fopen(fn, "r")))
return -errno;
if (!(fgets(line, sizeof(line), f))) {
r = -errno;
fclose(f);
return r;
}
fclose(f);
/* Let's skip the pid and comm fields. The latter is enclosed
* in () but does not escape any () in its value, so let's
* skip over it manually */
if (!(p = strrchr(line, ')')))
return -EIO;
p++;
if (sscanf(p, " "
"%*c " /* state */
"%*d " /* ppid */
"%*d " /* pgrp */
"%*d " /* session */
"%*d " /* tty_nr */
"%*d " /* tpgid */
"%*u " /* flags */
"%*u " /* minflt */
"%*u " /* cminflt */
"%*u " /* majflt */
"%*u " /* cmajflt */
"%*u " /* utime */
"%*u " /* stime */
"%*d " /* cutime */
"%*d " /* cstime */
"%*d " /* priority */
"%*d " /* nice */
"%*d " /* num_threads */
"%*d " /* itrealvalue */
"%llu " /* starttime */,
st) != 1)
return -EIO;
return 0;
}
static void free_thread(struct thread *t) {
free(t);
}
static void free_process(struct process *p) {
struct thread *t;
while ((t = p->threads)) {
p->threads = t->next;
free_thread(t);
}
free(p);
}
static void free_user(struct rtkit_user *u) {
struct process *p;
while ((p = u->processes)) {
u->processes = p->next;
free_process(p);
}
free(u);
}
static bool user_in_burst(struct rtkit_user *u) {
time_t now = time(NULL);
return now < u->timestamp + (time_t) actions_burst_sec;
}
static bool verify_burst(struct rtkit_user *u) {
if (!user_in_burst(u)) {
/* Restart burst phase */
time(&u->timestamp);
u->n_actions = 0;
return true;
}
if (u->n_actions >= actions_per_burst_max) {
char user[64];
syslog(LOG_WARNING, "Warning: Reached burst limit for user '%s', denying request.\n", get_user_name(u->uid, user, sizeof(user)));
return false;
}
u->n_actions++;
return true;
}
static int find_user(struct rtkit_user **_u, uid_t uid) {
struct rtkit_user *u;
for (u = users; u; u = u->next)
if (u->uid == uid) {
*_u = u;
return 0;
}
if (n_users >= users_max) {
syslog(LOG_WARNING, "Warning: Reached maximum concurrent user limit, denying request.\n");
return -EBUSY;
}
if (!(u = malloc(sizeof(struct rtkit_user))))
return -ENOMEM;
u->uid = uid;
u->timestamp = time(NULL);
u->n_actions = 0;
u->processes = NULL;
u->n_processes = u->n_threads = 0;
u->next = users;
users = u;
n_users++;
*_u = u;
return 0;
}
static int find_process(struct process** _p, struct rtkit_user *u, pid_t pid, unsigned long long starttime) {
struct process *p;
for (p = u->processes; p; p = p->next)
if (p->pid == pid && p->starttime == starttime) {
*_p = p;
return 0;
}
if (u->n_processes >= processes_per_user_max) {
char user[64];
syslog(LOG_WARNING, "Warning: Reached maximum concurrent process limit for user '%s', denying request.\n", get_user_name(u->uid, user, sizeof(user)));
return -EBUSY;
}
if (!(p = malloc(sizeof(struct process))))
return -ENOMEM;
p->pid = pid;
p->starttime = starttime;
p->threads = NULL;
p->next = u->processes;
u->processes = p;
u->n_processes++;
n_total_processes++;
*_p = p;
return 0;
}
static int find_thread(struct thread** _t, struct rtkit_user *u, struct process *p, pid_t pid, unsigned long long starttime) {
struct thread *t;
for (t = p->threads; t; t = t->next)
if (t->pid == pid && t->starttime == starttime) {
*_t = t;
return 0;
}
if (u->n_threads >= threads_per_user_max) {
char user[64];
syslog(LOG_WARNING, "Warning: Reached maximum concurrent threads limit for user '%s', denying request.\n", get_user_name(u->uid, user, sizeof(user)));
return -EBUSY;
}
if (!(t = malloc(sizeof(struct thread))))
return -ENOMEM;
t->pid = pid;
t->starttime = starttime;
t->next = p->threads;
p->threads = t;
u->n_threads++;
n_total_threads++;
*_t = t;
return 0;
}
static bool thread_relevant(struct process *p, struct thread *t) {
unsigned long long st;
int r;
/* This checks if a thread still matters to us, i.e. if its
* PID still refers to the same thread and if it is still high
* priority or real time */
if ((r = read_starttime(p->pid, t->pid, &st)) < 0) {
/* Did the thread die? */
if (r == -ENOENT)
return FALSE;
syslog(LOG_WARNING, "Warning: failed to read start time: %s\n", strerror(-r));
return FALSE;
}
/* Did the thread get replaced by another thread? */
if (st != t->starttime)
return FALSE;
if ((r = sched_getscheduler(t->pid)) < 0) {
/* Maybe it died right now? */
if (errno == ESRCH)
return FALSE;
syslog(LOG_WARNING, "Warning: failed to read scheduler policy: %s\n", strerror(errno));
return FALSE;
}
/* Is this a realtime thread? */
r &= ~SCHED_RESET_ON_FORK;
if (r == SCHED_RR || r == SCHED_FIFO)
return TRUE;
errno = 0;
r = getpriority(PRIO_PROCESS, t->pid);
if (errno != 0) {
/* Maybe it died right now? */
if (errno == ESRCH)
return FALSE;
syslog(LOG_WARNING, "Warning: failed to read nice level: %s\n", strerror(errno));
return FALSE;
}
/* Is this a high priority thread? */
if (r < 0)
return TRUE;
return FALSE;
}
static void thread_gc(struct rtkit_user *u, struct process *p) {
struct thread *t, *n, *l;
/* Cleanup dead theads of a specific user we don't need to keep track about anymore */
for (l = NULL, t = p->threads; t; t = n) {
n = t->next;
if (!thread_relevant(p, t)) {
free_thread(t);
if (l)
l->next = n;
else
p->threads = n;
assert(u->n_threads >= 1);
u->n_threads--;
assert(n_total_threads >= 1);
n_total_threads--;
} else
l = t;
}
assert(!p->threads || u->n_threads);
}
static void process_gc(struct rtkit_user *u) {
struct process *p, *n, *l;
/* Cleanup dead processes of a specific user we don't need to keep track about anymore */
for (l = NULL, p = u->processes; p; p = n) {
n = p->next;
thread_gc(u, p);
if (!p->threads) {
free_process(p);
if (l)
l->next = n;
else
u->processes = n;
assert(u->n_processes >= 1);
u->n_processes--;
assert(n_total_processes >= 1);
n_total_processes--;
} else
l = p;
}
assert(!u->processes == !u->n_processes);
}
static void user_gc(void) {
struct rtkit_user *u, *n, *l;
/* Cleanup all users we don't need to keep track about anymore */
for (l = NULL, u = users; u; u = n) {
n = u->next;
process_gc(u);
if (!u->processes && !user_in_burst(u)) {
free_user(u);
if (l)
l->next = n;
else
users = n;
assert(n_users >= 1);
n_users--;
} else
l = u;
}
assert(!users == !n_users);
}
static bool startswith(const char *s, const char *prefix) {
return strncmp(s, prefix, strlen(prefix)) == 0;
}
static int self_set_realtime(unsigned priority) {
struct sched_param param;
int r;
memset(¶m, 0, sizeof(param));
param.sched_priority = priority;
if (sched_setscheduler(0, sched_policy|SCHED_RESET_ON_FORK, ¶m) < 0) {
r = -errno;
syslog(LOG_ERR, "Failed to make ourselves RT: %s\n", strerror(errno));
goto finish;
}
r = 0;
finish:
return r;
}
static void self_drop_realtime(int nice_level) {
struct sched_param param;
memset(¶m, 0, sizeof(param));
if (sched_setscheduler(0, SCHED_OTHER, ¶m) < 0)
syslog(LOG_WARNING, "Warning: Failed to reset scheduling to SCHED_OTHER: %s\n", strerror(errno));
if (setpriority(PRIO_PROCESS, 0, nice_level) < 0)
syslog(LOG_WARNING, "Warning: Failed to reset nice level to %u: %s\n", our_nice_level, strerror(errno));
}
/* Verifies that RLIMIT_RTTIME is set for the specified process */
static int verify_process_rttime(struct process *p) {
char fn[128];
FILE *f;
int r;
bool good = false;
assert_se(snprintf(fn, sizeof(fn)-1, "%s/%llu/limits", get_proc_path(), (unsigned long long) p->pid) < (int) (sizeof(fn)-1));
fn[sizeof(fn)-1] = 0;
if (!(f = fopen(fn, "r"))) {
r = -errno;
syslog(LOG_ERR, "Failed to open '%s': %s\n", fn, strerror(errno));
return r;
}
for (;;) {
char line[128];
char soft[32], hard[32];
unsigned long long rttime;
char *e = NULL;
if (!fgets(line, sizeof(line), f))
break;
if (!startswith(line, "Max realtime timeout"))
continue;
if (sscanf(line + 20, "%s %s", soft, hard) != 2) {
syslog(LOG_WARNING, "Warning: parse failure in %s.\n", fn);
break;
}
errno = 0;
rttime = strtoll(hard, &e, 10);
if (errno != 0 || !e || *e != 0)
break;
if (rttime <= rttime_usec_max)
good = true;
break;
}
fclose(f);
return good ? 0 : -EPERM;
}
static int verify_process_user(struct rtkit_user *u, struct process *p) {
char fn[128];
int r;
struct stat st;
assert_se(snprintf(fn, sizeof(fn)-1, "%s/%llu", get_proc_path(), (unsigned long long) p->pid) < (int) (sizeof(fn)-1));
fn[sizeof(fn)-1] = 0;
memset(&st, 0, sizeof(st));
if (stat(fn, &st) < 0) {
r = -errno;
if (r != -ENOENT)
syslog(LOG_WARNING, "Warning: Failed to stat() file '%s': %s\n", fn, strerror(-r));
return r;
}
return st.st_uid == u->uid ? 0 : -EPERM;
}
static int verify_process_starttime(struct process *p) {
unsigned long long st;
int r;
if ((r = read_starttime(p->pid, 0, &st)) < 0) {
if (r != -ENOENT)
syslog(LOG_WARNING, "Warning: Failed to read start time of process %llu: %s\n", (unsigned long long) p->pid, strerror(-r));
return r;
}
return st == p->starttime ? 0 : -EPERM;
}
static int verify_thread_starttime(struct process *p, struct thread *t) {
unsigned long long st;
int r;
if ((r = read_starttime(p->pid, t->pid, &st)) < 0) {
if (r != -ENOENT)
syslog(LOG_WARNING, "Warning: Failed to read start time of thread %llu: %s\n", (unsigned long long) t->pid, strerror(-r));
return r;
}
return st == t->starttime ? 0 : -EPERM;
}
static int thread_reset(pid_t tid) {
struct sched_param param;
int r = 0;
memset(¶m, 0, sizeof(param));
param.sched_priority = 0;
if (sched_setscheduler(tid, SCHED_OTHER, ¶m) < 0) {
if (errno != ESRCH)
syslog(LOG_WARNING, "Warning: Failed to reset scheduling to SCHED_OTHER for thread %llu: %s\n", (unsigned long long) tid, strerror(errno));
r = -1;
}
if (setpriority(PRIO_PROCESS, tid, 0) < 0) {
if (errno != ESRCH)
syslog(LOG_WARNING, "Warning: Failed to reset nice level to 0 for thread %llu: %s\n", (unsigned long long) tid, strerror(errno));
r = -1;
}
return r;
}
static int process_set_realtime(struct rtkit_user *u, struct process *p, struct thread *t, unsigned priority) {
int r;
struct sched_param param;
char user[64];
if ((int) priority < sched_get_priority_min(sched_policy) ||
(int) priority > sched_get_priority_max(sched_policy))
return -EINVAL;
/* We always want to be able to get a higher RT priority than
* the client */
if (priority >= our_realtime_priority ||
priority > max_realtime_priority)
return -EPERM;
/* Make sure users don't flood us with requests */
if (!verify_burst(u))
return -EBUSY;
/* Temporarily become a realtime process. We do this to make
* sure that our verification code is not preempted by an evil
* client's code which might have gotten RT through
* us. */
if ((r = self_set_realtime(our_realtime_priority)) < 0)
return r;
/* Let's make sure that everything is alright before we make
* the process realtime */
if ((r = verify_process_user(u, p)) < 0 ||
(r = verify_process_starttime(p)) < 0 ||
(r = verify_process_rttime(p)) < 0 ||
(r = verify_thread_starttime(p, t)) < 0)
goto finish;
/* Ok, everything seems to be in order, now, let's do it */
memset(¶m, 0, sizeof(param));
param.sched_priority = (int) priority;
if (sched_setscheduler(t->pid, sched_policy|SCHED_RESET_ON_FORK, ¶m) < 0) {
r = -errno;
syslog(LOG_ERR, "Failed to make thread %llu RT: %s\n", (unsigned long long) t->pid, strerror(errno));
goto finish;
}
/* We do some sanity checks afterwards, to verify that the
* caller didn't play games with us and replaced the process
* behind the PID */
if ((r = verify_thread_starttime(p, t)) < 0 ||
(r = verify_process_rttime(p)) < 0 ||
(r = verify_process_starttime(p)) < 0 ||
(r = verify_process_user(u, p)) < 0) {
thread_reset(t->pid);
goto finish;
}
syslog(LOG_INFO, "Successfully made thread %llu of process %llu owned by '%s' RT at priority %u.\n",
(unsigned long long) t->pid,
(unsigned long long) p->pid,
get_user_name(u->uid, user, sizeof(user)),
priority);
r = 0;
finish:
self_drop_realtime(our_nice_level);
return r;
}
static int process_set_high_priority(struct rtkit_user *u, struct process *p, struct thread *t, int priority) {
int r;
struct sched_param param;
char user[64];
if (priority < PRIO_MIN || priority >= PRIO_MAX)
return -EINVAL;
if (priority < min_nice_level)
return -EPERM;
/* Make sure users don't flood us with requests */
if (!verify_burst(u))
return -EBUSY;
/* Temporarily become a realtime process */
if ((r = self_set_realtime(our_realtime_priority)) < 0)
return r;
/* Let's make sure that everything is alright before we make
* the process high priority */
if ((r = verify_process_user(u, p)) < 0 ||
(r = verify_process_starttime(p)) < 0 ||
(r = verify_thread_starttime(p, t)) < 0)
goto finish;
/* Ok, everything seems to be in order, now, let's do it */
memset(¶m, 0, sizeof(param));
param.sched_priority = 0;
if (sched_setscheduler(t->pid, SCHED_OTHER|SCHED_RESET_ON_FORK, ¶m) < 0) {
r = -errno;
syslog(LOG_ERR, "Failed to make process %llu SCHED_NORMAL: %s\n", (unsigned long long) t->pid, strerror(errno));
goto finish;
}
if (setpriority(PRIO_PROCESS, t->pid, priority) < 0) {
r = -errno;
syslog(LOG_ERR, "Failed to set nice level of process %llu to %i: %s\n", (unsigned long long) t->pid, priority, strerror(errno));
goto finish;
}
if ((r = verify_thread_starttime(p, t)) < 0 ||
(r = verify_process_starttime(p)) < 0 ||
(r = verify_process_user(u, p)) < 0) {
thread_reset(t->pid);
goto finish;
}
syslog(LOG_INFO, "Successfully made thread %llu of process %llu owned by '%s' high priority at nice level %i.\n",
(unsigned long long) t->pid,
(unsigned long long) p->pid,
get_user_name(u->uid, user, sizeof(user)),
priority);
r = 0;
finish:
self_drop_realtime(our_nice_level);
return r;
}
static void reset_known(void) {
struct rtkit_user *u;
struct process *p;
struct thread *t;
unsigned n_demoted = 0;
syslog(LOG_INFO, "Demoting known real-time threads.\n");
for (u = users; u; u = u->next)
for (p = u->processes; p; p = p->next)
for (t = p->threads; t; t = t->next)
if (verify_process_user(u, p) >= 0 &&
verify_process_starttime(p) >= 0 &&
verify_thread_starttime(p, t) >= 0)
if (thread_reset(t->pid) >= 0) {
syslog(LOG_NOTICE, "Successfully demoted thread %llu of process %llu.\n",
(unsigned long long) t->pid,
(unsigned long long) p->pid);
n_demoted++;
}
syslog(LOG_NOTICE, "Demoted %u threads.\n", n_demoted);
}
static int reset_all(void) {
DIR *pd;
int r;
unsigned n_demoted = 0;
/* Goes through /proc and demotes *all* threads to
* SCHED_OTHER */
syslog(LOG_INFO, "Demoting known and unknown real-time threads.\n");
if (!(pd = opendir(get_proc_path()))) {
r = -errno;
syslog(LOG_ERR, "opendir(%s) failed: %s\n", get_proc_path(), strerror(errno));
return r;
}
for (;;) {
const struct dirent *pde;
char *e = NULL;
unsigned long long pid;
char fn[128];
DIR *td;
struct stat st;
if (!(pde = readdir(pd)))
break;
if (!(pde->d_type & DT_DIR))
continue;
errno = 0;
pid = strtoull(pde->d_name, &e, 10);
if (errno != 0 || !e || *e != 0)
continue;
if ((pid_t) pid == getpid() ||
pid == 1)
continue;
assert_se(snprintf(fn, sizeof(fn)-1, "%s/%llu", get_proc_path(), pid) < (int) (sizeof(fn)-1));
fn[sizeof(fn)-1] = 0;
if (stat(fn, &st) < 0) {
if (errno != ENOENT)
syslog(LOG_WARNING, "Warning: stat(%s) failed: %s\n", fn, strerror(errno));
continue;
}
if (!S_ISDIR(st.st_mode))
continue;
if (!canary_demote_root && st.st_uid == 0)
continue;
assert_se(snprintf(fn, sizeof(fn)-1, "%s/%llu/task", get_proc_path(), pid) < (int) (sizeof(fn)-1));
fn[sizeof(fn)-1] = 0;
if (!(td = opendir(fn)))
continue;
for (;;) {
const struct dirent *tde;
unsigned long long tid;
if (!(tde = readdir(td)))
break;
if (!(tde->d_type & DT_DIR))
continue;
e = NULL;
errno = 0;
tid = strtoull(tde->d_name, &e, 10);
if (errno != 0 || !e || *e != 0)
continue;
if ((r = sched_getscheduler(tid)) < 0) {
if (errno != ESRCH)
syslog(LOG_WARNING, "Warning: sched_getscheduler() failed: %s\n", strerror(errno));
continue;
}
if (r == SCHED_FIFO || r == SCHED_RR ||
r == (SCHED_FIFO|SCHED_RESET_ON_FORK) || r == (SCHED_RR|SCHED_RESET_ON_FORK))
if (thread_reset((pid_t) tid) >= 0) {
syslog(LOG_NOTICE, "Successfully demoted thread %llu of process %llu.\n",
(unsigned long long) tid,
(unsigned long long) pid);
n_demoted++;
}
}
closedir(td);
}
closedir(pd);
syslog(LOG_NOTICE, "Demoted %u threads.\n", n_demoted);
return 0;
}
/* This mimics dbus_bus_get_unix_user() */
static unsigned long get_unix_process_id(
DBusConnection *connection,
const char *name,
DBusError *error) {
DBusMessage *m, *r;
uint32_t pid = (uint32_t) -1;
assert_se(m = dbus_message_new_method_call(
DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"GetConnectionUnixProcessID"));
assert_se(dbus_message_append_args(
m,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID));
r = dbus_connection_send_with_reply_and_block(connection, m, -1, error);
dbus_message_unref (m);
if (!r)
goto finish;
if (dbus_set_error_from_message(error, r))
goto finish;