@cryptotaxi247 / netdata-1 / commits / 8463380cd

fix apps.plugin security issues identified by Synacktiv

Costa Tsaousis (ktsaou) committed Apr 17, 2018 at 22:31 UTC 8463380cd1f2bcd25c38a2c401207ede4ff65486
2 files changed +174 -173
netdata-installer.sh
+2 -2
@@ -991,8 +991,8 @@ To run apps.plugin with escalated capabilities:
991
992 or, to run apps.plugin as root:
993
994 - ${TPUT_YELLOW}${TPUT_BOLD}sudo chown root \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
995 - ${TPUT_YELLOW}${TPUT_BOLD}sudo chmod 4755 \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
994 + ${TPUT_YELLOW}${TPUT_BOLD}sudo chown root:${NETDATA_GROUP} \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
995 + ${TPUT_YELLOW}${TPUT_BOLD}sudo chmod 4750 \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
996
997 apps.plugin is performing a hard-coded function of data collection for all
998 running processes. It cannot be instructed from the netdata daemon to perform
src/apps_plugin.c
+172 -171
@@ -7,6 +7,35 @@
7
8 #include "common.h"
9
10 +// ----------------------------------------------------------------------------
11 +// debugging
12 +
13 +static int debug_enabled = 0;
14 +static inline void debug_log_int(const char *fmt, ... ) {
15 + va_list args;
16 +
17 + fprintf( stderr, "apps.plugin: ");
18 + va_start( args, fmt );
19 + vfprintf( stderr, fmt, args );
20 + va_end( args );
21 +
22 + fputc('\n', stderr);
23 +}
24 +
25 +#ifdef NETDATA_INTERNAL_CHECKS
26 +
27 +#define debug_log(fmt, args...) do { if(unlikely(debug_enabled)) debug_log_int(fmt, ##args); } while(0)
28 +
29 +#else
30 +
31 +static inline void debug_log_dummy(void) {}
32 +#define debug_log(fmt, args...) debug_log_dummy()
33 +
34 +#endif
35 +
36 +
37 +// ----------------------------------------------------------------------------
38 +
39 #ifdef __FreeBSD__
40 #include <sys/user.h>
41 #endif
@@ -57,7 +86,6 @@
86 // command line options
87
88 static int
60 - debug = 0,
89 update_every = 1,
90 enable_guest_charts = 0,
91 #ifdef __FreeBSD__
@@ -193,7 +221,7 @@ struct target {
221 unsigned int processes; // how many processes have been merged to this
222 int exposed; // if set, we have sent this to netdata
223 int hidden; // if set, we set the hidden flag on the dimension
196 - int debug;
224 + int debug_enabled;
225 int ends_with;
226 int starts_with; // if set, the compare string matches only the
227 // beginning of the command
@@ -452,8 +480,7 @@ static struct target *get_users_target(uid_t uid) {
480 w->next = users_root_target;
481 users_root_target = w;
482
455 - if(unlikely(debug))
456 - fprintf(stderr, "apps.plugin: added uid %u ('%s') target\n", w->uid, w->name);
483 + debug_log("added uid %u ('%s') target", w->uid, w->name);
484
485 return w;
486 }
@@ -485,8 +512,7 @@ struct target *get_groups_target(gid_t gid)
512 w->next = groups_root_target;
513 groups_root_target = w;
514
488 - if(unlikely(debug))
489 - fprintf(stderr, "apps.plugin: added gid %u ('%s') target\n", w->gid, w->name);
515 + debug_log("added gid %u ('%s') target", w->gid, w->name);
516
517 return w;
518 }
@@ -527,11 +553,11 @@ static struct target *get_apps_groups_target(const char *id, struct target *targ
553 break;
554 }
555
530 - if(unlikely(debug)) {
556 + if(unlikely(debug_enabled)) {
557 if(unlikely(target))
532 - fprintf(stderr, "apps.plugin: REUSING TARGET NAME '%s' on ID '%s'\n", target->name, target->id);
558 + debug_log("REUSING TARGET NAME '%s' on ID '%s'", target->name, target->id);
559 else
534 - fprintf(stderr, "apps.plugin: NEW TARGET NAME '%s' on ID '%s'\n", name, id);
560 + debug_log("NEW TARGET NAME '%s' on ID '%s'", name, id);
561 }
562 }
563
@@ -564,21 +590,25 @@ static struct target *get_apps_groups_target(const char *id, struct target *targ
590 w->comparelen = strlen(w->compare);
591
592 w->hidden = thidden;
567 - w->debug = tdebug;
593 +#ifdef NETDATA_INTERNAL_CHECKS
594 + w->debug_enabled = tdebug;
595 +#else
596 + if(tdebug)
597 + fprintf(stderr, "apps.plugin has been compiled without debugging\n");
598 +#endif
599 w->target = target;
600
601 // append it, to maintain the order in apps_groups.conf
602 if(last) last->next = w;
603 else apps_groups_root_target = w;
604
574 - if(unlikely(debug))
575 - fprintf(stderr, "apps.plugin: ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
576 - , w->id
577 - , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
578 - , w->target?w->target->name:w->name
579 - , (w->hidden)?"hidden":"-"
580 - , (w->debug)?"debug":"-"
581 - );
605 + debug_log("ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s"
606 + , w->id
607 + , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
608 + , w->target?w->target->name:w->name
609 + , (w->hidden)?"hidden":"-"
610 + , (w->debug_enabled)?"debug":"-"
611 + );
612
613 return w;
614 }
@@ -590,8 +620,7 @@ static int read_apps_groups_conf(const char *file)
620
621 snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, file);
622
593 - if(unlikely(debug))
594 - fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
623 + debug_log("process groups file: '%s'", filename);
624
625 // ----------------------------------------
626
@@ -685,8 +714,7 @@ static inline void del_pid_entry(pid_t pid) {
714 return;
715 }
716
688 - if(unlikely(debug))
689 - fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, p->comm);
717 + debug_log("process %d %s exited, deleting it.", pid, p->comm);
718
719 if(root_of_pids == p)
720 root_of_pids = p->next;
@@ -716,8 +744,8 @@ static inline int managed_log(struct pid_stat *p, uint32_t log, int status) {
744 if(unlikely(!status)) {
745 // error("command failed log %u, errno %d", log, errno);
746
719 - if(unlikely(debug || errno != ENOENT)) {
720 - if(unlikely(debug || !(p->log_thrown & log))) {
747 + if(unlikely(debug_enabled || errno != ENOENT)) {
748 + if(unlikely(debug_enabled || !(p->log_thrown & log))) {
749 p->log_thrown |= log;
750 switch(log) {
751 case PID_LOG_IO:
@@ -779,7 +807,7 @@ static inline void assign_target_to_pid(struct pid_stat *p) {
807
808 struct target *w;
809 for(w = apps_groups_root_target; w ; w = w->next) {
782 - // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
810 + // if(debug_enabled || (p->target && p->target->debug_enabled)) debug_log("\t\tcomparing '%s' with '%s'", w->compare, p->comm);
811
812 // find it - 4 cases:
813 // 1. the target is not a pattern
@@ -796,8 +824,8 @@ static inline void assign_target_to_pid(struct pid_stat *p) {
824 if(w->target) p->target = w->target;
825 else p->target = w;
826
799 - if(debug || (p->target && p->target->debug))
800 - fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
827 + if(debug_enabled || (p->target && p->target->debug_enabled))
828 + debug_log("%s linked to target %s", p->comm, p->target->name);
829
830 break;
831 }
@@ -843,8 +871,7 @@ static inline int read_proc_pid_cmdline(struct pid_stat *p) {
871
872 p->cmdline = strdupz(cmdline);
873
846 - if(unlikely(debug))
847 - fprintf(stderr, "Read file '%s' contents: %s\n", p->cmdline_filename, p->cmdline);
874 + debug_log("Read file '%s' contents: %s", p->cmdline_filename, p->cmdline);
875
876 return 1;
877
@@ -1004,7 +1031,7 @@ static inline int read_proc_pid_status(struct pid_stat *p, void *ptr) {
1031 arl_begin(p->status_arl);
1032
1033 for(l = 0; l < lines ;l++) {
1007 - // fprintf(stderr, "CHECK: line %zu of %zu, key '%s' = '%s'\n", l, lines, procfile_lineword(ff, l, 0), procfile_lineword(ff, l, 1));
1034 + // debug_log("CHECK: line %zu of %zu, key '%s' = '%s'", l, lines, procfile_lineword(ff, l, 0), procfile_lineword(ff, l, 1));
1035 arl_ptr.line = l;
1036 if(unlikely(arl_check(p->status_arl,
1037 procfile_lineword(ff, l, 0),
@@ -1013,7 +1040,7 @@ static inline int read_proc_pid_status(struct pid_stat *p, void *ptr) {
1040
1041 p->status_vmshared = p->status_rssfile + p->status_rssshmem;
1042
1016 - // fprintf(stderr, "%s uid %d, gid %d, VmSize %zu, VmRSS %zu, RssFile %zu, RssShmem %zu, shared %zu\n", p->comm, (int)p->uid, (int)p->gid, p->status_vmsize, p->status_vmrss, p->status_rssfile, p->status_rssshmem, p->status_vmshared);
1043 + // debug_log("%s uid %d, gid %d, VmSize %zu, VmRSS %zu, RssFile %zu, RssShmem %zu, shared %zu", p->comm, (int)p->uid, (int)p->gid, p->status_vmsize, p->status_vmrss, p->status_rssfile, p->status_rssshmem, p->status_vmshared);
1044
1045 return 1;
1046 #endif
@@ -1072,11 +1099,11 @@ static inline int read_proc_pid_stat(struct pid_stat *p, void *ptr) {
1099 #endif
1100
1101 if(strcmp(p->comm, comm) != 0) {
1075 - if(unlikely(debug)) {
1102 + if(unlikely(debug_enabled)) {
1103 if(p->comm[0])
1077 - fprintf(stderr, "apps.plugin: \tpid %d (%s) changed name to '%s'\n", p->pid, p->comm, comm);
1104 + debug_log("\tpid %d (%s) changed name to '%s'", p->pid, p->comm, comm);
1105 else
1079 - fprintf(stderr, "apps.plugin: \tJust added %d (%s)\n", p->pid, comm);
1106 + debug_log("\tJust added %d (%s)", p->pid, comm);
1107 }
1108
1109 strncpyz(p->comm, comm, MAX_COMPARE_NAME);
@@ -1152,8 +1179,8 @@ static inline int read_proc_pid_stat(struct pid_stat *p, void *ptr) {
1179 }
1180 #endif
1181
1155 - if(unlikely(debug || (p->target && p->target->debug)))
1156 - fprintf(stderr, "apps.plugin: READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT ", threads=%d\n", netdata_configured_host_prefix, p->pid, p->comm, (p->target)?p->target->name:"UNSET", p->stat_collected_usec - p->last_stat_collected_usec, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
1182 + if(unlikely(debug_enabled || (p->target && p->target->debug_enabled)))
1183 + debug_log("READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT ", threads=%d", netdata_configured_host_prefix, p->pid, p->comm, (p->target)?p->target->name:"UNSET", p->stat_collected_usec - p->last_stat_collected_usec, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
1184
1185 if(unlikely(global_iterations_counter == 1)) {
1186 p->minflt = 0;
@@ -1368,15 +1395,13 @@ static inline void file_descriptor_not_used(int id)
1395 }
1396 #endif /* NETDATA_INTERNAL_CHECKS */
1397
1371 - if(unlikely(debug))
1372 - fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
1398 + debug_log("decreasing slot %d (count = %d).", id, all_files[id].count);
1399
1400 if(all_files[id].count > 0) {
1401 all_files[id].count--;
1402
1403 if(!all_files[id].count) {
1378 - if(unlikely(debug))
1379 - fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
1404 + debug_log(" >> slot %d is empty.", id);
1405
1406 if(unlikely(file_descriptor_remove(&all_files[id]) != (void *)&all_files[id]))
1407 error("INTERNAL ERROR: removal of unused fd from index, removed a different fd");
@@ -1398,8 +1423,7 @@ static inline void all_files_grow() {
1423 int i;
1424
1425 // there is no empty slot
1401 - if(unlikely(debug))
1402 - fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1426 + debug_log("extending fd array to %d entries", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1427
1428 all_files = reallocz(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1429
@@ -1407,8 +1431,7 @@ static inline void all_files_grow() {
1431 // since all pointers are now invalid
1432
1433 if(unlikely(old && old != (void *)all_files)) {
1410 - if(unlikely(debug))
1411 - fprintf(stderr, "apps.plugin: >> re-indexing.\n");
1434 + debug_log(" >> re-indexing.");
1435
1436 all_files_index.root = NULL;
1437 for(i = 0; i < all_files_size; i++) {
@@ -1417,8 +1440,7 @@ static inline void all_files_grow() {
1440 error("INTERNAL ERROR: duplicate indexing of fd during realloc.");
1441 }
1442
1420 - if(unlikely(debug))
1421 - fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
1443 + debug_log(" >> re-indexing done.");
1444 }
1445
1446 // initialize the newly added entries
@@ -1441,8 +1463,7 @@ static inline int file_descriptor_set_on_empty_slot(const char *name, uint32_t h
1463 if(!all_files || all_files_len == all_files_size)
1464 all_files_grow();
1465
1444 - if(unlikely(debug))
1445 - fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
1466 + debug_log(" >> searching for empty slot.");
1467
1468 // search for an empty slot
1469
@@ -1453,16 +1474,14 @@ static inline int file_descriptor_set_on_empty_slot(const char *name, uint32_t h
1474 if(c == 0) continue;
1475
1476 if(!all_files[c].count) {
1456 - if(unlikely(debug))
1457 - fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
1477 + debug_log(" >> Examining slot %d.", c);
1478
1479 #ifdef NETDATA_INTERNAL_CHECKS
1480 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1461 - error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1481 + error("fd on position %d is not cleared properly. It still has %s in it.", c, all_files[c].name);
1482 #endif /* NETDATA_INTERNAL_CHECKS */
1483
1464 - if(unlikely(debug))
1465 - fprintf(stderr, "apps.plugin: >> %s fd position %d for %s (last name: %s)\n", all_files[c].name?"re-using":"using", c, name, all_files[c].name);
1484 + debug_log(" >> %s fd position %d for %s (last name: %s)", all_files[c].name?"re-using":"using", c, name, all_files[c].name);
1485
1486 freez((void *)all_files[c].name);
1487 all_files[c].name = NULL;
@@ -1479,8 +1498,7 @@ static inline int file_descriptor_set_on_empty_slot(const char *name, uint32_t h
1498 }
1499 // else we have an empty slot in 'c'
1500
1482 - if(unlikely(debug))
1483 - fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1501 + debug_log(" >> updating slot %d.", c);
1502
1503 all_files[c].name = strdupz(name);
1504 all_files[c].hash = hash;
@@ -1493,8 +1511,7 @@ static inline int file_descriptor_set_on_empty_slot(const char *name, uint32_t h
1511 if(unlikely(file_descriptor_add(&all_files[c]) != (void *)&all_files[c]))
1512 error("INTERNAL ERROR: duplicate indexing of fd.");
1513
1496 - if(unlikely(debug))
1497 - fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1514 + debug_log("using fd position %d (name: %s)", c, all_files[c].name);
1515
1516 return c;
1517 }
@@ -1503,14 +1520,12 @@ static inline int file_descriptor_find_or_add(const char *name)
1520 {
1521 uint32_t hash = simple_hash(name);
1522
1506 - if(unlikely(debug))
1507 - fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1523 + debug_log("adding or finding name '%s' with hash %u", name, hash);
1524
1525 struct file_descriptor *fd = file_descriptor_find(name, hash);
1526 if(fd) {
1527 // found
1512 - if(unlikely(debug))
1513 - fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
1528 + debug_log(" >> found on slot %d", fd->pos);
1529
1530 fd->count++;
1531 return fd->pos;
@@ -1530,17 +1545,13 @@ static inline int file_descriptor_find_or_add(const char *name)
1545 else if(strcmp(t, "[timerfd]") == 0) type = FILETYPE_TIMERFD;
1546 else if(strcmp(t, "[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1547 else {
1533 - if(unlikely(debug))
1534 - fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1535 -
1548 + debug_log("FIXME: unknown anonymous inode: %s", name);
1549 type = FILETYPE_OTHER;
1550 }
1551 }
1552 else if(likely(strcmp(name, "inotify") == 0)) type = FILETYPE_INOTIFY;
1553 else {
1541 - if(unlikely(debug))
1542 - fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1543 -
1554 + debug_log("FIXME: cannot understand linkname: %s", name);
1555 type = FILETYPE_OTHER;
1556 }
1557
@@ -1625,8 +1636,7 @@ static inline int read_pid_file_descriptors(struct pid_stat *p, void *ptr) {
1636 if (unlikely(fdid >= p->fds_size)) {
1637 // it is small, extend it
1638
1628 - if (unlikely(debug))
1629 - fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + MAX_SPARE_FDS);
1639 + debug_log("extending fd memory slots for %s from %d to %d", p->comm, p->fds_size, fdid + MAX_SPARE_FDS);
1640
1641 p->fds = reallocz(p->fds, (fdid + MAX_SPARE_FDS) * sizeof(int));
1642
@@ -1736,13 +1746,11 @@ static inline int read_pid_file_descriptors(struct pid_stat *p, void *ptr) {
1746 if(unlikely(fdid >= p->fds_size)) {
1747 // it is small, extend it
1748
1739 - if(unlikely(debug))
1740 - fprintf(stderr
1741 - , "apps.plugin: extending fd memory slots for %s from %d to %d\n"
1742 - , p->comm
1743 - , p->fds_size
1744 - , fdid + MAX_SPARE_FDS
1745 - );
1749 + debug_log("extending fd memory slots for %s from %d to %d"
1750 + , p->comm
1751 + , p->fds_size
1752 + , fdid + MAX_SPARE_FDS
1753 + );
1754
1755 p->fds = reallocz(p->fds, (fdid + MAX_SPARE_FDS) * sizeof(int));
1756
@@ -1757,10 +1765,9 @@ static inline int read_pid_file_descriptors(struct pid_stat *p, void *ptr) {
1765 sprintf(fdname, "%s/proc/%d/fd/%s", netdata_configured_host_prefix, p->pid, de->d_name);
1766 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1767 if(unlikely(l == -1)) {
1760 - if(debug || (p->target && p->target->debug)) {
1761 - if(debug || (p->target && p->target->debug))
1762 - error("Cannot read link %s", fdname);
1763 - }
1768 + if(debug_enabled || (p->target && p->target->debug_enabled))
1769 + error("Cannot read link %s", fdname);
1770 +
1771 continue;
1772 }
1773 else
@@ -1791,12 +1798,12 @@ static inline int read_pid_file_descriptors(struct pid_stat *p, void *ptr) {
1798
1799 // ----------------------------------------------------------------------------
1800
1794 -static inline int print_process_and_parents(struct pid_stat *p, usec_t time) {
1801 +static inline int debug_print_process_and_parents(struct pid_stat *p, usec_t time) {
1802 char *prefix = "\\_ ";
1803 int indent = 0;
1804
1805 if(p->parent)
1799 - indent = print_process_and_parents(p->parent, p->stat_collected_usec);
1806 + indent = debug_print_process_and_parents(p->parent, p->stat_collected_usec);
1807 else
1808 prefix = " > ";
1809
@@ -1830,12 +1837,12 @@ static inline int print_process_and_parents(struct pid_stat *p, usec_t time) {
1837 return indent + 1;
1838 }
1839
1833 -static inline void print_process_tree(struct pid_stat *p, char *msg) {
1834 - fprintf(stderr, "%s: process %s (%d, %s) with parents:\n", msg, p->comm, p->pid, p->updated?"running":"exited");
1835 - print_process_and_parents(p, p->stat_collected_usec);
1840 +static inline void debug_print_process_tree(struct pid_stat *p, char *msg) {
1841 + debug_log("%s: process %s (%d, %s) with parents:", msg, p->comm, p->pid, p->updated?"running":"exited");
1842 + debug_print_process_and_parents(p, p->stat_collected_usec);
1843 }
1844
1838 -static inline void find_lost_child_debug(struct pid_stat *pe, kernel_uint_t lost, int type) {
1845 +static inline void debug_find_lost_child(struct pid_stat *pe, kernel_uint_t lost, int type) {
1846 int found = 0;
1847 struct pid_stat *p = NULL;
1848
@@ -1938,8 +1945,8 @@ static inline void process_exited_processes() {
1945 if(utime + stime + gtime + minflt + majflt == 0)
1946 continue;
1947
1941 - if(unlikely(debug)) {
1942 - fprintf(stderr, "Absorb %s (%d %s total resources: utime=" KERNEL_UINT_FORMAT " stime=" KERNEL_UINT_FORMAT " gtime=" KERNEL_UINT_FORMAT " minflt=" KERNEL_UINT_FORMAT " majflt=" KERNEL_UINT_FORMAT ")\n"
1948 + if(unlikely(debug_enabled)) {
1949 + debug_log("Absorb %s (%d %s total resources: utime=" KERNEL_UINT_FORMAT " stime=" KERNEL_UINT_FORMAT " gtime=" KERNEL_UINT_FORMAT " minflt=" KERNEL_UINT_FORMAT " majflt=" KERNEL_UINT_FORMAT ")"
1950 , p->comm
1951 , p->pid
1952 , p->updated?"running":"exited"
@@ -1949,7 +1956,7 @@ static inline void process_exited_processes() {
1956 , minflt
1957 , majflt
1958 );
1952 - print_process_tree(p, "Searching parents");
1959 + debug_print_process_tree(p, "Searching parents");
1960 }
1961
1962 struct pid_stat *pp;
@@ -1958,59 +1965,57 @@ static inline void process_exited_processes() {
1965
1966 kernel_uint_t absorbed;
1967 absorbed = remove_exited_child_from_parent(&utime, &pp->cutime);
1961 - if(unlikely(debug && absorbed))
1962 - fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " utime (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
1968 + if(unlikely(debug_enabled && absorbed))
1969 + debug_log(" > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " utime (remaining: " KERNEL_UINT_FORMAT ")", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
1970
1971 absorbed = remove_exited_child_from_parent(&stime, &pp->cstime);
1965 - if(unlikely(debug && absorbed))
1966 - fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " stime (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
1972 + if(unlikely(debug_enabled && absorbed))
1973 + debug_log(" > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " stime (remaining: " KERNEL_UINT_FORMAT ")", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
1974
1975 absorbed = remove_exited_child_from_parent(&gtime, &pp->cgtime);
1969 - if(unlikely(debug && absorbed))
1970 - fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " gtime (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
1976 + if(unlikely(debug_enabled && absorbed))
1977 + debug_log(" > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " gtime (remaining: " KERNEL_UINT_FORMAT ")", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
1978
1979 absorbed = remove_exited_child_from_parent(&minflt, &pp->cminflt);
1973 - if(unlikely(debug && absorbed))
1974 - fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " minflt (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
1980 + if(unlikely(debug_enabled && absorbed))
1981 + debug_log(" > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " minflt (remaining: " KERNEL_UINT_FORMAT ")", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
1982
1983 absorbed = remove_exited_child_from_parent(&majflt, &pp->cmajflt);
1977 - if(unlikely(debug && absorbed))
1978 - fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " majflt (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
1984 + if(unlikely(debug_enabled && absorbed))
1985 + debug_log(" > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " majflt (remaining: " KERNEL_UINT_FORMAT ")", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
1986 }
1987
1988 if(unlikely(utime + stime + gtime + minflt + majflt > 0)) {
1982 - if(unlikely(debug)) {
1983 - if(utime) find_lost_child_debug(p, utime, 3);
1984 - if(stime) find_lost_child_debug(p, stime, 4);
1985 - if(gtime) find_lost_child_debug(p, gtime, 5);
1986 - if(minflt) find_lost_child_debug(p, minflt, 1);
1987 - if(majflt) find_lost_child_debug(p, majflt, 2);
1989 + if(unlikely(debug_enabled)) {
1990 + if(utime) debug_find_lost_child(p, utime, 3);
1991 + if(stime) debug_find_lost_child(p, stime, 4);
1992 + if(gtime) debug_find_lost_child(p, gtime, 5);
1993 + if(minflt) debug_find_lost_child(p, minflt, 1);
1994 + if(majflt) debug_find_lost_child(p, majflt, 2);
1995 }
1996
1997 p->keep = 1;
1998
1992 - if(unlikely(debug))
1993 - fprintf(stderr, " > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=" KERNEL_UINT_FORMAT " stime=" KERNEL_UINT_FORMAT " gtime=" KERNEL_UINT_FORMAT " minflt=" KERNEL_UINT_FORMAT " majflt=" KERNEL_UINT_FORMAT ")\n"
1994 - , p->comm
1995 - , p->pid
1996 - , p->updated?"running":"exited"
1997 - , utime
1998 - , stime
1999 - , gtime
2000 - , minflt
2001 - , majflt
2002 - );
1999 + debug_log(" > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=" KERNEL_UINT_FORMAT " stime=" KERNEL_UINT_FORMAT " gtime=" KERNEL_UINT_FORMAT " minflt=" KERNEL_UINT_FORMAT " majflt=" KERNEL_UINT_FORMAT ")"
2000 + , p->comm
2001 + , p->pid
2002 + , p->updated?"running":"exited"
2003 + , utime
2004 + , stime
2005 + , gtime
2006 + , minflt
2007 + , majflt
2008 + );
2009
2010 for(pp = p->parent; pp ; pp = pp->parent) {
2011 if(pp->updated) break;
2012 pp->keep = 1;
2013
2008 - if(unlikely(debug))
2009 - fprintf(stderr, " > - KEEP - parent for another loop: %s (%d %s)\n"
2010 - , pp->comm
2011 - , pp->pid
2012 - , pp->updated?"running":"exited"
2013 - );
2014 + debug_log(" > - KEEP - parent for another loop: %s (%d %s)"
2015 + , pp->comm
2016 + , pp->pid
2017 + , pp->updated?"running":"exited"
2018 + );
2019 }
2020
2021 p->utime_raw = utime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
@@ -2020,16 +2025,14 @@ static inline void process_exited_processes() {
2025 p->majflt_raw = majflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
2026 p->cutime_raw = p->cstime_raw = p->cgtime_raw = p->cminflt_raw = p->cmajflt_raw = 0;
2027
2023 - if(unlikely(debug))
2024 - fprintf(stderr, "\n");
2028 + debug_log(" ");
2029 }
2026 - else if(unlikely(debug)) {
2027 - fprintf(stderr, " > totally absorbed - DONE - %s (%d %s)\n"
2030 + else
2031 + debug_log(" > totally absorbed - DONE - %s (%d %s)"
2032 , p->comm
2033 , p->pid
2034 , p->updated?"running":"exited"
2031 - );
2032 - }
2035 + );
2036 }
2037 }
2038
@@ -2054,8 +2057,8 @@ static inline void link_all_processes_to_their_parents(void) {
2057 p->parent = pp;
2058 pp->children_count++;
2059
2057 - if(unlikely(debug || (p->target && p->target->debug)))
2058 - fprintf(stderr, "apps.plugin: \tchild %d (%s, %s) on target '%s' has parent %d (%s, %s). Parent: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", gtime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", cgtime=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT "\n", p->pid, p->comm, p->updated?"running":"exited", (p->target)?p->target->name:"UNSET", pp->pid, pp->comm, pp->updated?"running":"exited", pp->utime, pp->stime, pp->gtime, pp->minflt, pp->majflt, pp->cutime, pp->cstime, pp->cgtime, pp->cminflt, pp->cmajflt);
2060 + if(unlikely(debug_enabled || (p->target && p->target->debug_enabled)))
2061 + debug_log("child %d (%s, %s) on target '%s' has parent %d (%s, %s). Parent: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", gtime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", cgtime=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT "", p->pid, p->comm, p->updated?"running":"exited", (p->target)?p->target->name:"UNSET", pp->pid, pp->comm, pp->updated?"running":"exited", pp->utime, pp->stime, pp->gtime, pp->minflt, pp->majflt, pp->cutime, pp->cstime, pp->cgtime, pp->cminflt, pp->cmajflt);
2062 }
2063 else {
2064 p->parent = NULL;
@@ -2105,7 +2108,7 @@ static inline int collect_data_for_pid(pid_t pid, void *ptr) {
2108 if(unlikely(!p || p->read)) return 0;
2109 p->read = 1;
2110
2108 - // fprintf(stderr, "Reading process %d (%s), sortlist %d\n", p->pid, p->comm, p->sortlist);
2111 + // debug_log("Reading process %d (%s), sortlist %d", p->pid, p->comm, p->sortlist);
2112
2113 // --------------------------------------------------------------------
2114 // /proc/<pid>/stat
@@ -2141,8 +2144,8 @@ static inline int collect_data_for_pid(pid_t pid, void *ptr) {
2144 // --------------------------------------------------------------------
2145 // done!
2146
2144 - if(unlikely(debug && include_exited_childs && all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
2145 - fprintf(stderr, "Read process %d (%s) sortlisted %d, but its parent %d (%s) sortlisted %d, is not read\n", p->pid, p->comm, p->sortlist, all_pids[p->ppid]->pid, all_pids[p->ppid]->comm, all_pids[p->ppid]->sortlist);
2147 + if(unlikely(debug_enabled && include_exited_childs && all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
2148 + debug_log("Read process %d (%s) sortlisted %d, but its parent %d (%s) sortlisted %d, is not read", p->pid, p->comm, p->sortlist, all_pids[p->ppid]->pid, all_pids[p->ppid]->comm, all_pids[p->ppid]->sortlist);
2149
2150 // mark it as updated
2151 p->updated = 1;
@@ -2307,8 +2310,8 @@ static void cleanup_exited_pids(void) {
2310
2311 for(p = root_of_pids; p ;) {
2312 if(!p->updated && (!p->keep || p->keeploops > 0)) {
2310 - if(unlikely(debug && (p->keep || p->keeploops)))
2311 - fprintf(stderr, " > CLEANUP cannot keep exited process %d (%s) anymore - removing it.\n", p->pid, p->comm);
2313 + if(unlikely(debug_enabled && (p->keep || p->keeploops)))
2314 + debug_log(" > CLEANUP cannot keep exited process %d (%s) anymore - removing it.", p->pid, p->comm);
2315
2316 for(c = 0; c < p->fds_size; c++)
2317 if(p->fds[c] > 0) {
@@ -2335,7 +2338,7 @@ static void apply_apps_groups_targets_inheritance(void) {
2338 // inherit their target from their parent
2339 int found = 1, loops = 0;
2340 while(found) {
2338 - if(unlikely(debug)) loops++;
2341 + if(unlikely(debug_enabled)) loops++;
2342 found = 0;
2343 for(p = root_of_pids; p ; p = p->next) {
2344 // if this process does not have a target
@@ -2346,8 +2349,8 @@ static void apply_apps_groups_targets_inheritance(void) {
2349 p->target = p->parent->target;
2350 found++;
2351
2349 - if(debug || (p->target && p->target->debug))
2350 - fprintf(stderr, "apps.plugin: \t\tTARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s).\n", p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
2352 + if(debug_enabled || (p->target && p->target->debug_enabled))
2353 + debug_log("TARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s).", p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
2354 }
2355 }
2356 }
@@ -2357,7 +2360,7 @@ static void apply_apps_groups_targets_inheritance(void) {
2360 int sortlist = 1;
2361 found = 1;
2362 while(found) {
2360 - if(unlikely(debug)) loops++;
2363 + if(unlikely(debug_enabled)) loops++;
2364 found = 0;
2365
2366 for(p = root_of_pids; p ; p = p->next) {
@@ -2382,16 +2385,15 @@ static void apply_apps_groups_targets_inheritance(void) {
2385 if(unlikely(p->target && !p->parent->target)) {
2386 p->parent->target = p->target;
2387
2385 - if(debug || (p->target && p->target->debug))
2386 - fprintf(stderr, "apps.plugin: \t\tTARGET INHERITANCE: %s is inherited by %d (%s) from its child %d (%s).\n", p->target->name, p->parent->pid, p->parent->comm, p->pid, p->comm);
2388 + if(debug_enabled || (p->target && p->target->debug_enabled))
2389 + debug_log("TARGET INHERITANCE: %s is inherited by %d (%s) from its child %d (%s).", p->target->name, p->parent->pid, p->parent->comm, p->pid, p->comm);
2390 }
2391
2392 found++;
2393 }
2394 }
2395
2393 - if(unlikely(debug))
2394 - fprintf(stderr, "apps.plugin: TARGET INHERITANCE: merged %d processes\n", found);
2396 + debug_log("TARGET INHERITANCE: merged %d processes", found);
2397 }
2398
2399 // init goes always to default target
@@ -2403,7 +2405,7 @@ static void apply_apps_groups_targets_inheritance(void) {
2405 all_pids[0]->target = apps_groups_default_target;
2406
2407 // give a default target on all top level processes
2406 - if(unlikely(debug)) loops++;
2408 + if(unlikely(debug_enabled)) loops++;
2409 for(p = root_of_pids; p ; p = p->next) {
2410 // if the process is not merged itself
2411 // then is is a top level process
@@ -2421,21 +2423,20 @@ static void apply_apps_groups_targets_inheritance(void) {
2423 // give a target to all merged child processes
2424 found = 1;
2425 while(found) {
2424 - if(unlikely(debug)) loops++;
2426 + if(unlikely(debug_enabled)) loops++;
2427 found = 0;
2428 for(p = root_of_pids; p ; p = p->next) {
2429 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
2430 p->target = p->parent->target;
2431 found++;
2432
2431 - if(debug || (p->target && p->target->debug))
2432 - fprintf(stderr, "apps.plugin: \t\tTARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s) at phase 2.\n", p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
2433 + if(debug_enabled || (p->target && p->target->debug_enabled))
2434 + debug_log("TARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s) at phase 2.", p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
2435 }
2436 }
2437 }
2438
2437 - if(unlikely(debug))
2438 - fprintf(stderr, "apps.plugin: apply_apps_groups_targets_inheritance() made %d loops on the process tree\n", loops);
2439 + debug_log("apply_apps_groups_targets_inheritance() made %d loops on the process tree", loops);
2440 }
2441
2442 static size_t zero_all_targets(struct target *root) {
@@ -2628,8 +2629,8 @@ static inline void aggregate_pid_on_target(struct target *w, struct pid_stat *p,
2629 w->processes++;
2630 w->num_threads += p->num_threads;
2631
2631 - if(unlikely(debug || w->debug))
2632 - fprintf(stderr, "apps.plugin: \taggregating '%s' pid %d on target '%s' utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", gtime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", cgtime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT "\n", p->comm, p->pid, w->name, p->utime, p->stime, p->gtime, p->cutime, p->cstime, p->cgtime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
2632 + if(unlikely(debug_enabled || w->debug_enabled))
2633 + debug_log("aggregating '%s' pid %d on target '%s' utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", gtime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", cgtime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT "", p->comm, p->pid, w->name, p->utime, p->stime, p->gtime, p->cutime, p->cstime, p->cgtime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
2634 }
2635
2636 static void calculate_netdata_statistics(void) {
@@ -2660,8 +2661,8 @@ static void calculate_netdata_statistics(void) {
2661 if(likely(p->user_target && p->user_target->uid == p->uid))
2662 w = p->user_target;
2663 else {
2663 - if(unlikely(debug && p->user_target))
2664 - fprintf(stderr, "apps.plugin: \t\tpid %d (%s) switched user from %u (%s) to %u.\n", p->pid, p->comm, p->user_target->uid, p->user_target->name, p->uid);
2664 + if(unlikely(debug_enabled && p->user_target))
2665 + debug_log("pid %d (%s) switched user from %u (%s) to %u.", p->pid, p->comm, p->user_target->uid, p->user_target->name, p->uid);
2666
2667 w = p->user_target = get_users_target(p->uid);
2668 }
@@ -2676,8 +2677,8 @@ static void calculate_netdata_statistics(void) {
2677 if(likely(p->group_target && p->group_target->gid == p->gid))
2678 w = p->group_target;
2679 else {
2679 - if(unlikely(debug && p->group_target))
2680 - fprintf(stderr, "apps.plugin: \t\tpid %d (%s) switched group from %u (%s) to %u.\n", p->pid, p->comm, p->group_target->gid, p->group_target->name, p->gid);
2680 + if(unlikely(debug_enabled && p->group_target))
2681 + debug_log("pid %d (%s) switched group from %u (%s) to %u.", p->pid, p->comm, p->group_target->gid, p->group_target->name, p->gid);
2682
2683 w = p->group_target = get_groups_target(p->gid);
2684 }
@@ -2957,14 +2958,12 @@ static void normalize_utilization(struct target *root) {
2958
2959 // the report
2960
2960 - if(unlikely(debug)) {
2961 - fprintf(stderr,
2961 + debug_log(
2962 "SYSTEM: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " "
2963 "COLLECTED: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " cu=" KERNEL_UINT_FORMAT " cs=" KERNEL_UINT_FORMAT " cg=" KERNEL_UINT_FORMAT " "
2964 "DELTA: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " "
2965 "FIX: u=%0.2f s=%0.2f g=%0.2f cu=%0.2f cs=%0.2f cg=%0.2f "
2966 "FINALLY: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " cu=" KERNEL_UINT_FORMAT " cs=" KERNEL_UINT_FORMAT " cg=" KERNEL_UINT_FORMAT " "
2967 - "\n"
2967 , global_utime
2968 , global_stime
2969 , global_gtime
@@ -2989,8 +2988,7 @@ static void normalize_utilization(struct target *root) {
2988 , (kernel_uint_t)(cutime * cutime_fix_ratio)
2989 , (kernel_uint_t)(cstime * cstime_fix_ratio)
2990 , (kernel_uint_t)(cgtime * cgtime_fix_ratio)
2992 - );
2993 - }
2991 + );
2992 }
2993 #else // ALL_PIDS_ARE_READ_INSTANTLY == 1
2994 static void normalize_utilization(struct target *root) {
@@ -3151,7 +3149,8 @@ static void send_charts_updates_to_netdata(struct target *root, const char *type
3149 if (!w->exposed && w->processes) {
3150 newly_added++;
3151 w->exposed = 1;
3154 - if (debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
3152 + if (debug_enabled || w->debug_enabled)
3153 + debug_log("%s just added - regenerating charts.", w->name);
3154 }
3155 }
3156
@@ -3341,8 +3340,11 @@ static void parse_args(int argc, char **argv)
3340 }
3341
3342 if(strcmp("debug", argv[i]) == 0) {
3344 - debug = 1;
3345 - // debug_flags = 0xffffffff;
3343 +#ifdef NETDATA_INTERNAL_CHECKS
3344 + debug_enabled = 1;
3345 +#else
3346 + fprintf(stderr, "apps.plugin has been compiled without debugging\n");
3347 +#endif
3348 continue;
3349 }
3350
@@ -3436,7 +3438,7 @@ static void parse_args(int argc, char **argv)
3438 }
3439
3440 if(freq > 0) update_every = freq;
3439 - if(!name) name = "groups";
3441 + if(!name || !*name) name = "groups";
3442
3443 if(read_apps_groups_conf(name)) {
3444 error("Cannot read process groups '%s/apps_%s.conf'. There are no internal defaults. Failing.", config_dir, name);
@@ -3448,11 +3450,11 @@ static int am_i_running_as_root() {
3450 uid_t uid = getuid(), euid = geteuid();
3451
3452 if(uid == 0 || euid == 0) {
3451 - if(debug) info("I am running with escalated privileges, uid = %u, euid = %u.", uid, euid);
3453 + if(debug_enabled) info("I am running with escalated privileges, uid = %u, euid = %u.", uid, euid);
3454 return 1;
3455 }
3456
3455 - if(debug) info("I am not running with escalated privileges, uid = %u, euid = %u.", uid, euid);
3457 + if(debug_enabled) info("I am not running with escalated privileges, uid = %u, euid = %u.", uid, euid);
3458 return 0;
3459 }
3460
@@ -3463,7 +3465,7 @@ static int check_capabilities() {
3465 error("Cannot get current capabilities.");
3466 return 0;
3467 }
3466 - else if(debug)
3468 + else if(debug_enabled)
3469 info("Received my capabilities from the system.");
3470
3471 int ret = 1;
@@ -3478,7 +3480,7 @@ static int check_capabilities() {
3480 error("apps.plugin should run with CAP_DAC_READ_SEARCH.");
3481 ret = 0;
3482 }
3481 - else if(debug)
3483 + else if(debug_enabled)
3484 info("apps.plugin runs with CAP_DAC_READ_SEARCH.");
3485 }
3486
@@ -3492,7 +3494,7 @@ static int check_capabilities() {
3494 error("apps.plugin should run with CAP_SYS_PTRACE.");
3495 ret = 0;
3496 }
3495 - else if(debug)
3497 + else if(debug_enabled)
3498 info("apps.plugin runs with CAP_SYS_PTRACE.");
3499 }
3500
@@ -3630,8 +3632,7 @@ int main(int argc, char **argv) {
3632
3633 show_guest_time_old = show_guest_time;
3634
3633 - if(unlikely(debug))
3634 - fprintf(stderr, "apps.plugin: done Loop No %zu\n", global_iterations_counter);
3635 + debug_log("done Loop No %zu", global_iterations_counter);
3636
3637 // restart check (14400 seconds)
3638 if(now_monotonic_sec() - started_t > 14400) exit(0);