summaryrefslogtreecommitdiffstats
path: root/collectors
diff options
context:
space:
mode:
authorVladimir Kobal <vlad@prokk.net>2022-03-30 08:34:26 +0200
committerGitHub <noreply@github.com>2022-03-30 09:34:26 +0300
commitffee2317885bf8ceab7224ba23aad08421986cd5 (patch)
tree75fe0c056db5f674e92839cdadcc007f724f93a2 /collectors
parent6564063846951d2e6c8230036b409dae5bfd75e4 (diff)
Unblock cgroup version detection with systemd (#12553)
Diffstat (limited to 'collectors')
-rw-r--r--collectors/cgroups.plugin/sys_fs_cgroup.c44
1 files changed, 32 insertions, 12 deletions
diff --git a/collectors/cgroups.plugin/sys_fs_cgroup.c b/collectors/cgroups.plugin/sys_fs_cgroup.c
index 9f21df4349..4ee17e46ef 100644
--- a/collectors/cgroups.plugin/sys_fs_cgroup.c
+++ b/collectors/cgroups.plugin/sys_fs_cgroup.c
@@ -116,21 +116,41 @@ static enum cgroups_systemd_setting cgroups_detect_systemd(const char *exec)
if (!f)
return retval;
- while (fgets(buf, MAXSIZE_PROC_CMDLINE, f) != NULL) {
- if ((begin = strstr(buf, SYSTEMD_HIERARCHY_STRING))) {
- end = begin = begin + strlen(SYSTEMD_HIERARCHY_STRING);
- if (!*begin)
- break;
- while (isalpha(*end))
- end++;
- *end = 0;
- for (int i = 0; cgroups_systemd_options[i].name; i++) {
- if (!strcmp(begin, cgroups_systemd_options[i].name)) {
- retval = cgroups_systemd_options[i].setting;
+ fd_set rfds;
+ struct timeval timeout;
+ int fd = fileno(f);
+ int ret = -1;
+
+ FD_ZERO(&rfds);
+ FD_SET(fd, &rfds);
+ timeout.tv_sec = 3;
+ timeout.tv_usec = 0;
+
+ if (fd != -1) {
+ ret = select(fd + 1, &rfds, NULL, NULL, &timeout);
+ }
+
+ if (ret == -1) {
+ error("Failed to get the output of \"%s\"", exec);
+ } else if (ret == 0) {
+ info("Cannot get the output of \"%s\" within %"PRId64" seconds", exec, (int64_t)timeout.tv_sec);
+ } else {
+ while (fgets(buf, MAXSIZE_PROC_CMDLINE, f) != NULL) {
+ if ((begin = strstr(buf, SYSTEMD_HIERARCHY_STRING))) {
+ end = begin = begin + strlen(SYSTEMD_HIERARCHY_STRING);
+ if (!*begin)
break;
+ while (isalpha(*end))
+ end++;
+ *end = 0;
+ for (int i = 0; cgroups_systemd_options[i].name; i++) {
+ if (!strcmp(begin, cgroups_systemd_options[i].name)) {
+ retval = cgroups_systemd_options[i].setting;
+ break;
+ }
}
+ break;
}
- break;
}
}