/
core.c
1871 lines (1608 loc) · 48.1 KB
/
core.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
/*
Copyright 2010,2011 ulatencyd developers
This file is part of ulatencyd.
ulatencyd 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.
ulatencyd 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 ulatencyd. If not, see http://www.gnu.org/licenses/.
*/
#define _GNU_SOURCE
#include "config.h"
#include "ulatency.h"
#include "proc/procps.h"
#include "proc/sysinfo.h"
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <glib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <dlfcn.h>
#include <fnmatch.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <linux/sched.h>
#ifdef ENABLE_DBUS
#include <dbus/dbus-glib.h>
DBusGConnection *U_dbus_connection;
#endif
lua_State *lua_main_state;
GList *filter_list;
GList *filter_fast_list;
GNode *processes_tree;
GHashTable *processes;
u_scheduler scheduler = {NULL};
static int iteration;
static double _last_load;
static double _last_percent;
// flag list of system wide flags
GList *system_flags;
int system_flags_changed;
// delay rules execution
static long int delay;
static GPtrArray *delay_stack;
// profiling timers
struct u_timer timer_filter;
struct u_timer timer_scheduler;
struct u_timer timer_parse;
// delay new processes
struct delay_proc {
struct timespec when;
u_proc *proc;
};
double get_last_load() {
return _last_load;
}
double get_last_percent() {
return _last_percent;
}
guint get_plugin_id() {
static guint last = USER_ACTIVE_AGENT_MODULE;
return ++last;
}
/*************************************************************
* u_proc code
************************************************************/
void filter_block_free(gpointer fb) {
free(fb);
}
void u_head_free(gpointer fb) {
DEC_REF(fb);
}
/**
* remove all child nodes
* @arg proc a #u_proc
*
* Unlinks all child nodes from a #u_proc node. Moving them to the parent
* on @proc and unlinks the node. Makes sure the node is save to remove.
*
* @return none
*/
static void u_proc_remove_child_nodes(u_proc *proc) {
GNode *nparent, *cur;
u_proc *proc_tmp;
if(g_node_n_children(proc->node)) {
// the process which dies has some children. we have to move children
// to a new parent. Try the parent of the dead process first
if(proc->node->parent) {
nparent = proc->node->parent;
} else {
proc_tmp = proc_by_pid(1);
if(proc_tmp && proc_tmp->node) {
nparent = proc_tmp->node;
} else {
// this should not happen, but we have to attach the node somewhere
// this could happen if the netlink messages arrive befor a fill update
g_warning("attach child from dead process to root tree");
nparent = processes_tree;
}
}
g_node_unlink(proc->node);
g_assert(nparent != proc->node);
while((cur = g_node_first_child(proc->node)) != NULL) {
g_node_unlink(cur);
g_node_append(nparent, cur);
}
} else {
g_node_unlink(proc->node);
}
}
/**
* remove pid from delay stack
* @arg pid #pid_t pid
*
* removes process from the delay stack
*
* @return none
*/
static void remove_proc_from_delay_stack(pid_t pid) {
int i = 0;
struct delay_proc *cur;
for(i = 0; i < delay_stack->len;) {
cur = g_ptr_array_index(delay_stack, i);
if(cur->proc->pid == pid) {
u_trace("remove delay %d %d:%d", pid, i, delay_stack->len);
g_ptr_array_remove_index_fast(delay_stack, i);
} else {
i++;
}
}
}
/**
* test if pid is in delay stack
* @arg pid #pid_t pid
*
* @return boolean
*/
static int pid_in_delay_stack(pid_t pid) {
int i = 0;
struct delay_proc *cur;
for(i = 0; i < delay_stack->len; i++) {
cur = g_ptr_array_index(delay_stack, i);
if(cur->proc->pid == pid)
return TRUE;
}
return FALSE;
}
/**
* free u_proc instance
* @arg ptr pointer to #u_proc
*
* free's all memory of a u_proc. This function should never be called directly.
* It as called automaticly when the ref counter drops 0
*
* @return none
*/
void u_proc_free(void *ptr) {
u_proc *proc = ptr;
g_assert(proc->ref == 0);
g_free(proc->cmdfile);
g_free(proc->exe);
g_free(proc->cmdline_match);
g_strfreev(proc->cgroup_origin);
if(proc->environ)
g_hash_table_unref(proc->environ);
if(proc->cmdline)
g_ptr_array_unref(proc->cmdline);
if(proc->lua_data) {
luaL_unref(lua_main_state, LUA_REGISTRYINDEX, proc->lua_data);
}
g_hash_table_destroy (proc->skip_filter);
//if(proc->tasks)
g_ptr_array_free(proc->tasks, TRUE);
u_proc_remove_child_nodes(proc);
g_assert(g_node_n_children(proc->node) == 0);
g_node_destroy(proc->node);
freesupgrp(&(proc->proc));
freeproc_light(&(proc->proc));
g_slice_free(u_proc, proc);
}
void u_proc_free_task(void *ptr) {
u_task *task = ptr;
// the task group owner has the same pointers, so we shall not free them
// when the task is removed
if(task->task.nsupgid > 0 &&
task->task.supgid &&
task->task.supgid != task->proc->proc.supgid) {
free(task->task.supgid);
}
//g_free(proc->supgid);
g_slice_free(u_task, task);
}
/**
* allocate new #u_proc
* @arg proc pointer to #proc_t datastructure
*
* Allocates a new #u_proc. It can be prefiled with a proc_t datastructure.
* If \c proc is NULL, the resulting u_proc will have the state UPROC_NEW, otherwise
* it is UPROC_ALIVE
*
* @return newly allocated #u_proc reference
*/
u_proc* u_proc_new(proc_t *proc) {
u_proc *rv;
rv = g_slice_new0(u_proc);
rv->free_fnk = u_proc_free;
rv->ref = 1;
rv->skip_filter = g_hash_table_new_full(g_direct_hash, g_direct_equal,
NULL, filter_block_free);
//rv->tasks = g_array_new(FALSE, TRUE, sizeof(proc_t));
rv->tasks = g_ptr_array_new_with_free_func(u_proc_free_task);
rv->flags = NULL;
rv->changed = TRUE;
rv->node = g_node_new(rv);
if(proc) {
rv->pid = proc->tid;
U_PROC_SET_STATE(rv,UPROC_ALIVE);
memcpy(&(rv->proc), proc, sizeof(proc_t));
} else {
U_PROC_SET_STATE(rv,UPROC_NEW);
}
return rv;
}
/**
* list all flags from #u_proc
* @arg proc a #u_proc
* @arg recrusive boolean if recrusive flags should be returned, too
*
* Returns a new allocated GList of all flags. Don't forgett to DECREF the
* result items and release the list
*
* @return @glist
*/
GList *u_proc_list_flags (u_proc *proc, gboolean recrusive) {
int i = 1;
u_flag *fl;
GList *cur, *rv = NULL;
do {
cur = g_list_first(proc->flags);
while(cur) {
fl = cur->data;
if(recrusive == 2 && !fl->inherit) {
cur = g_list_next (cur);
continue;
}
INC_REF(fl);
rv = g_list_append(rv, fl);
i++;
cur = g_list_next (cur);
}
if(recrusive) {
if(!proc->node || !proc->node->parent || proc->node->parent == processes_tree) {
proc = NULL;
break;
}
proc = (u_proc *)(proc->node->parent->data);
if(recrusive == 1)
recrusive = 2;
}
} while (recrusive && proc);
return rv;
}
/**
* ensures fields on #u_proc
* @arg proc a #u_proc
* @arg what set of varibles to fill from #ENSURE_WHAT
* @arg update force update
*
* Ensures a set of varibles is filled.
* If update is true, the variable are updated even if they already exist.
*
* @return @success
*/
int u_proc_ensure(u_proc *proc, enum ENSURE_WHAT what, int update) {
if(what == BASIC) {
// make sure process has basic values parsed
if(U_PROC_HAS_STATE(proc,UPROC_BASIC) && !update)
return TRUE;
else
return process_update_pid(proc->pid);
} else if(what == TASKS) {
// FIXME
return TRUE;
} else if(what == ENVIRONMENT) {
if(update && proc->environ) {
g_hash_table_unref(proc->environ);
proc->environ = NULL;
}
if(!proc->environ)
proc->environ = u_read_env_hash (proc->pid);
return (proc->environ != NULL);
} else if(what == CMDLINE) {
if(update && proc->cmdline) {
g_ptr_array_unref(proc->cmdline);
proc->cmdline = NULL;
}
if(!proc->cmdline) {
int i;
gchar *tmp, *tmp2;
g_free(proc->cmdline_match);
proc->cmdline_match = NULL;
GString *match = g_string_new("");
proc->cmdline = u_read_0file (proc->pid, "cmdline");
// update cmd
if(proc->cmdline) {
for(i = 0; i < proc->cmdline->len; i++) {
if(i)
match = g_string_append_c(match, ' ');
match = g_string_append(match, g_ptr_array_index(proc->cmdline, i));
}
proc->cmdline_match = g_string_free(match, FALSE);
// empty command line, for kernel threads for example
if(!proc->cmdline->len)
return FALSE;
if(proc->cmdfile) {
g_free(proc->cmdfile);
proc->cmdfile = NULL;
}
tmp = g_ptr_array_index(proc->cmdline, 0);
if(tmp) {
tmp2 = g_strrstr_len(tmp, -1, "/");
if(tmp2 == NULL) {
proc->cmdfile = g_strdup(tmp);
} else if((tmp2+1-tmp) < strlen(tmp)) {
proc->cmdfile = g_strdup(tmp2+1);
}
}
return TRUE;
} else {
return FALSE;
}
}
return (proc->cmdline != NULL);
} else if(what == EXE) {
char buf[PATH_MAX+1];
ssize_t out;
char *path;
if(update && proc->exe) {
g_free(proc->exe);
proc->exe = NULL;
}
if(!proc->exe) {
path = g_strdup_printf ("/proc/%u/exe", (guint)proc->pid);
out = readlink(path, (char *)&buf, PATH_MAX);
buf[out] = 0;
if(out > 0) {
// strip out the ' (deleted)' suffix
if(out > 10 && !strncmp((char *)&buf[out-10], " (deleted)", 10)) {
buf[out-10] = 0;
out -= 10;
}
proc->exe = g_strndup((char *)&buf, out);
} else {
g_free(path);
return FALSE;
}
g_free(path);
}
return TRUE;
}
return FALSE;
}
/**
* up to date list process tasks
* @arg proc #u_proc to get tasks from
*
* Returns a GArray of #pid_t of all tasks from given #u_proc process
*
* @return none
*/
GArray *u_proc_get_current_task_pids(u_proc *proc) {
if(!U_PROC_SET_STATE(proc, UPROC_ALIVE))
return FALSE;
GArray *rv = g_array_new(TRUE, TRUE, sizeof(pid_t));
DIR *dip;
struct dirent *dit;
pid_t tpid;
char *path = g_strdup_printf("/proc/%d/task", proc->pid);
dip = opendir(path);
if(!dip)
goto out;
while ((dit = readdir(dip)) != NULL) {
if(!strcmp(dit->d_name, ".") || !strcmp(dit->d_name, ".."))
continue;
tpid = (pid_t)atol(dit->d_name);
g_array_append_val(rv, tpid);
}
closedir(dip);
g_free(path);
return rv;
out:
g_free(path);
g_array_unref(rv);
return NULL;
}
/**
* free process
* @arg data a #u_proc pointer
*
* INTERNAL: Called when the process is removed from the process_list
*
* @return none
*/
static void processes_free_value(gpointer data) {
// called when a process is freed from the process list
// this means that the process is not valid anymore and is
// marked as such
u_proc *proc = data;
u_filter *flt;
U_PROC_UNSET_STATE(proc, UPROC_ALIVE);
// run exit hooks
GList *cur = g_list_first(filter_list);
while(cur) {
flt = cur->data;
if(flt->exit)
flt->exit(proc, flt);
cur = cur->next;
}
U_PROC_SET_STATE(proc, UPROC_INVALID);
u_proc_remove_child_nodes(proc);
// remove it from the delay stack
remove_proc_from_delay_stack(proc->pid);
DEC_REF(proc);
}
static int find_parent_caller_stack(GArray *array, pid_t pid) {
int i;
for(i = 0; i < array->len; i++) {
if(g_array_index(array, pid_t, i) == pid)
return TRUE;
}
return FALSE;
}
static int remove_parent_caller_stack(GArray *array, pid_t pid) {
int i;
for(i = 0; i < array->len; i++) {
if(g_array_index(array, pid_t, i) == pid) {
g_array_remove_index(array, i);
return TRUE;
}
}
return FALSE;
}
/**
* returns the parent of process
* @arg parent_pid #pid_t of parent
* @arg child #u_proc of child
*
* INTERNAL: lookup the parent #u_proc of a child. Prints warning when missing.
*
* @return #u_proc of parent
*/
static inline u_proc *parent_proc_by_pid(pid_t parent_pid, u_proc *child) {
pid_t update_pid;
static GArray *updates = NULL;
if(!updates)
updates = g_array_new(FALSE, FALSE, sizeof(pid_t));
u_proc *parent = proc_by_pid(parent_pid);
// this should't happen, but under fork stress init may not have
// collected so the parent does not exist, or the parent just died. we try updating
// the process first and try again.
if(!parent) {
g_debug("parent missing: %d, force update", parent_pid);
if(!find_parent_caller_stack(updates, child->pid)) {
update_pid = child->pid;
g_array_append_val(updates, update_pid);
process_update_pid(update_pid);
remove_parent_caller_stack(updates, update_pid);
} else if(!find_parent_caller_stack(updates, child->proc.ppid)) {
// we try to get the parent as last resort
update_pid = child->proc.ppid;
g_array_append_val(updates, update_pid);
process_update_pid(update_pid);
remove_parent_caller_stack(updates, update_pid);
}
parent = proc_by_pid(child->proc.ppid);
if(!parent) {
g_debug("parent missing, second try: %d parent %d", child->pid, child->proc.ppid);
process_update_pid(child->proc.ppid);
parent = proc_by_pid(child->proc.ppid);
}
}
if(!parent) {
g_warning("pid: %d parent %d missing. attaching to pid 1", child->pid, parent_pid);
return proc_by_pid(1);
}
return parent;
}
/**
* rebuilds the process tree
*
* INTERNAL: completly rebuild the process tree. used when a desync is detected
* on update_processes.
*
* @return none
*/
static void rebuild_tree() {
GHashTableIter iter;
GList *keys, *cur;
gpointer key, value;
u_proc *proc, *parent;
// clear root node
g_node_destroy(processes_tree);
processes_tree = g_node_new(NULL);
// create nodes first
g_hash_table_iter_init (&iter, processes);
while (g_hash_table_iter_next (&iter, &key, &value))
{
proc = (u_proc *)value;
proc->node = g_node_new(proc);
g_node_append(processes_tree, proc->node);
}
// now we can lookup the parents and attach the node to the parent
//g_hash_table_iter_init (&iter, processes);
keys = g_hash_table_get_keys(processes);
cur = g_list_first(keys);
while(cur)
{
proc = (u_proc *)g_hash_table_lookup(processes,cur->data);
g_assert(proc->proc.ppid != proc->pid);
if(proc->proc.ppid) {
// get a parent, hopfully the real one
parent = parent_proc_by_pid(proc->proc.ppid, proc);
U_PROC_SET_STATE(proc, UPROC_HAS_PARENT);
g_assert(parent != proc);
g_assert(parent && parent->node);
g_node_unlink(proc->node);
g_node_append(parent->node, proc->node);
} else {
g_node_unlink(proc->node);
g_node_append(processes_tree, proc->node);
U_PROC_UNSET_STATE(proc, UPROC_HAS_PARENT);
}
cur = cur->next;
}
g_list_free(keys);
}
/**
* detect changes of process
* @arg old *#proc_t of old values
* @arg new *#proc_t of new values
*
* INTERNAL: detect if the changed values of a u_proc.proc structure are sufficient
* enough for the #u_proc.changed flag to be set. When the changed flag is set,
* the scheduler will run again.
*
* @return boolean if a major change detected
*/
static int detect_changed(proc_t *old, proc_t *new) {
// detects changes of main paramenters
if(old->euid != new->euid || old->session != new->session ||
old->egid != new->egid || old->pgrp != new->pgrp ||
old->sched != new->sched || old->rtprio != new->rtprio)
return 1;
return 0;
}
/**
* test if process has changed
* @arg key unused
* @arg value #u_proc pointer
* @arg user_data pointer to int
*
* INTERNAL: detect if the process was changed in the last full update run.
* if not, the process is removed from the process_list
*
* @return boolean TRUE if not changed
*/
static gboolean processes_is_last_changed(gpointer key, gpointer value,
gpointer user_data) {
u_proc *proc = (u_proc *)value;
int last_changed = *(int *)user_data;
return (proc->last_update != last_changed);
}
/**
* remove process
* @arg proc #u_proc to remove
*
* tells the core that a process is not active anymore
*
* @return boolean if the process got removed
*/
int process_remove(u_proc *proc) {
return g_hash_table_remove(processes, GUINT_TO_POINTER(proc->pid));
}
/**
* remove process by pid
* @arg proc #pid_t to remove
*
* same as process_remove execpt with pid
*
* @return boolean if the process got removed
*/
int process_remove_by_pid(pid_t pid) {
return g_hash_table_remove(processes, GUINT_TO_POINTER(pid));
}
/**
* clear all changed flags
*
* INTERNAL: unset the changed flag. called after a full run.
*
* @return none
*/
static void clear_process_changed() {
GHashTableIter iter;
gpointer ikey, value;
u_proc *proc;
g_hash_table_iter_init (&iter, processes);
while (g_hash_table_iter_next (&iter, &ikey, &value))
{
proc = (u_proc *)value;
proc->changed = FALSE;
}
return;
}
// helper for process_clear_filter_block
static gboolean _clear_skip_filters_types(gpointer key, gpointer value, gpointer user_data) {
struct filter_block *fb = value;
int *block_type = user_data;
return !(fb->flags & *block_type);
}
/**
* clears given skip filters
*
* @arg proc #u_proc to change
* @arg block_types remove the matching block types
*
* clears all filter blocks of given types
*
* @return none
*/
void clear_process_skip_filters(u_proc *proc, int block_types) {
g_hash_table_foreach_remove(proc->skip_filter,
_clear_skip_filters_types,
&block_types);
}
// copy the fake value of a parent pid to the child until the real value
// of the child changes from the parent
#define fake_var_fix(FAKE, ORG) \
if(proc->FAKE && ((proc-> FAKE##_old != proc->proc.ORG) || (proc->FAKE == proc->proc.ORG))) { \
/* when real value was set, the fake value disapears. */ \
/*printf("unset fake: %d %d %d %d\n", proc->pid, proc->proc.ORG, proc->FAKE##_old, proc-> FAKE);*/ \
proc-> FAKE = 0; \
proc->FAKE##_old = 0; \
proc->changed = 1; \
} else if(parent-> FAKE && !proc->FAKE && \
parent->proc.ORG == proc->proc.ORG && \
parent-> FAKE != proc->FAKE) { \
proc-> FAKE = parent->FAKE; \
proc->FAKE##_old = proc->proc.ORG; \
proc->changed = 1; \
/*printf("set fake: pid:%d ppid:%d fake:%d fake_old:%d\n", proc->pid, parent->pid, proc->FAKE, proc->FAKE##_old);*/ \
}
/**
* process workarrounds
* @arg proc #u_proc proc
* @arg parent #u_proc parent
*
* INTERNAL: do workarounds for process parameters that can't be changed in the
* system but need to for nice grouping.
*
* @return boolean if the process got removed
*/
static void process_workarrounds(u_proc *proc, u_proc *parent) {
// do various workaround jobs here...
fake_var_fix(fake_pgrp, pgrp);
fake_var_fix(fake_session, session);
}
#undef fake_var_fix
/**
* updates processes
* @arg proctab #PROCTAB
* @arg full boolean indicates that a full run is done
*
* parses the /proc filesystem and updates the internal node structure acordingly.
* This low level function is usually called from wrapper that fill the @proctab
* accordingly.
*
* @return int number of parsed records
*/
int update_processes_run(PROCTAB *proctab, int full) {
proc_t buf;
proc_t buf_task;
u_proc *proc;
u_proc *parent;
time_t timeout = time(NULL);
gboolean full_update = FALSE;
static int run = 0;
int rrt;
int rv = 0;
int i;
GList *updated = NULL;
if(full)
run++;
if(!proctab) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "can't open /proc");
return 1;
}
memset(&buf, 0, sizeof(proc_t));
while(readproc(proctab, &buf)){
proc = proc_by_pid(buf.tid);
if(proc) {
// we need to clear the task array first to detect which dynamic mallocs
// need to be freed as readproc likes to reuse pointers on some dynamic
// allocations
if(proc->tasks->len)
g_ptr_array_remove_range(proc->tasks, 0, proc->tasks->len);
// free all changable allocated buffers
freesupgrp(&(proc->proc));
freeproc_light(&(proc->proc));
} else {
proc = u_proc_new(&buf);
g_hash_table_insert(processes, GUINT_TO_POINTER(proc->pid), proc);
// we save the origin of cgroups for scheduler constrains
}
// must still have the process allocated
// detect change of important parameters that will cause a reschedule
proc->changed = proc->changed | detect_changed(&(proc->proc), &buf);
// remove it from delay stack
remove_proc_from_delay_stack(proc->pid);
if(full)
proc->last_update = run;
//save rt received flag
rrt = proc->received_rt;
memcpy(&(proc->proc), &buf, sizeof(proc_t));
proc->received_rt |= (proc->proc.sched == SCHED_FIFO || proc->proc.sched == SCHED_RR);
while(readtask(proctab,&buf,&buf_task)) {
u_task *task = g_slice_new0(u_task);
task->proc = proc;
memcpy(&(task->task), &buf_task, sizeof(proc_t));
g_ptr_array_add(proc->tasks, task);
proc->received_rt |= (buf_task.sched == SCHED_FIFO || buf_task.sched == SCHED_RR);
}
if(rrt != proc->received_rt)
proc->changed = 1;
if(!proc->cgroup_origin)
proc->cgroup_origin = g_strdupv(proc->proc.cgroup);
U_PROC_UNSET_STATE(proc, UPROC_NEW);
U_PROC_SET_STATE(proc, UPROC_ALIVE);
if((proctab->flags & OPENPROC_FLAGS) == OPENPROC_FLAGS) {
U_PROC_SET_STATE(proc, UPROC_BASIC);
} else
U_PROC_UNSET_STATE(proc, UPROC_BASIC);
u_flag_clear_timeout(proc, timeout);
updated = g_list_append(updated, proc);
rv++;
memset(&buf, 0, sizeof(proc_t));
//g_list_foreach(filter_list, filter_run_for_proc, &buf);
//freesupgrp(&buf);
}
// we update the parent links after all processes are updated
for(i = 0; i < rv; i++) {
proc = g_list_nth_data(updated, i);
if(proc->proc.ppid && proc->proc.ppid != proc->pid) {
parent = g_hash_table_lookup(processes, GUINT_TO_POINTER(proc->proc.ppid));
// the parent should exist. in case it is missing we have to run a full
// tree rebuild then
if(parent && parent->node) {
// current parent is not what it should be
if(proc->node->parent != parent->node) {
g_node_unlink(proc->node);
g_node_append(parent->node, proc->node);
}
process_workarrounds(proc, parent);
} else {
full_update = TRUE;
}
} else {
// this is kinda bad. it is ok for kernel processes and init
if(proc->node->parent != processes_tree) {
if(!G_NODE_IS_ROOT(proc->node))
g_node_unlink(proc->node);
g_node_append(processes_tree, proc->node);
}
}
}
// remove old processes
g_list_free(updated);
if(full) {
g_hash_table_foreach_remove(processes,
processes_is_last_changed,
&run);
// we can completly clean the delay stack as all processes are now processed
// missing so will cause scheduling for dead processes
if(delay_stack->len)
g_ptr_array_remove_range(delay_stack, 0, delay_stack->len);
}
if(full_update) {
rebuild_tree();
}
return rv;
}
/**
* updates all processes
*
* updates all process of the system
*
* @return number of process updated
*/
int process_update_all() {
int rv;
PROCTAB *proctab;
proctab = openproc(OPENPROC_FLAGS);
rv = update_processes_run(proctab, TRUE);
closeproc(proctab);
return rv;
}
// calculated the difference between two timespec values
static struct timespec diff(struct timespec start, struct timespec end)
{
struct timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
/**
* runs process from delay stack
*
* called by timeout to check if processes from the delay stack are old enough
* to be run through the filters and scheduler
*
* @return number of process updated
*/
static int run_new_pid(gpointer ign) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
struct delay_proc *cur;
struct timespec td;
int i;
GArray *targets = NULL;
if(!delay_stack->len)
return TRUE;
targets = g_array_new(TRUE, FALSE, sizeof(pid_t));
for(i = 0; i < delay_stack->len;i++) {
cur = g_ptr_array_index(delay_stack, i);
td = diff(cur->when, now);
//printf("test %d %ld >= %ld\n", cur->proc->pid, (td.tv_sec * 1000000000 + td.tv_nsec), delay);
if((td.tv_sec * 1000000000 + td.tv_nsec) >= delay) {
u_trace("run filter for %d", cur->proc->pid);
g_array_append_val(targets, cur->proc->pid);
// enforce the scheduler on run when moved from the delay queue
cur->proc->changed = TRUE;
}
}
process_new_list(targets, TRUE, FALSE);
// process_new_list removes the entries it processes from the delay stack
// buf it the process is dead already, they stay here in the list. we make
// sure they are removed.
for(i=0; i<targets->len; i++) {
remove_proc_from_delay_stack(g_array_index(targets, pid_t, i));
}
g_array_unref(targets);
return TRUE;
}
/**
* adds a new process via delay stack
* @arg pid new pid to create