Unblock cgroup version detection with systemd (#12553)
Vladimir Kobal committed
Mar 30, 2022 at 08:34 UTC
ffee2317885bf8ceab7224ba23aad08421986cd5
1 file changed
+32
-12
collectors/cgroups.plugin/sys_fs_cgroup.c
+32
-12
@@ -116,21 +116,41 @@ static enum cgroups_systemd_setting cgroups_detect_systemd(const char *exec)
116
if (!f)
117
return retval;
118
119
- while (fgets(buf, MAXSIZE_PROC_CMDLINE, f) != NULL) {
120
- if ((begin = strstr(buf, SYSTEMD_HIERARCHY_STRING))) {
121
- end = begin = begin + strlen(SYSTEMD_HIERARCHY_STRING);
122
- if (!*begin)
123
- break;
124
- while (isalpha(*end))
125
- end++;
126
- *end = 0;
127
- for (int i = 0; cgroups_systemd_options[i].name; i++) {
128
- if (!strcmp(begin, cgroups_systemd_options[i].name)) {
129
- retval = cgroups_systemd_options[i].setting;
119
+ fd_set rfds;
120
+ struct timeval timeout;
121
+ int fd = fileno(f);
122
+ int ret = -1;
123
+
124
+ FD_ZERO(&rfds);
125
+ FD_SET(fd, &rfds);
126
+ timeout.tv_sec = 3;
127
+ timeout.tv_usec = 0;
128
+
129
+ if (fd != -1) {
130
+ ret = select(fd + 1, &rfds, NULL, NULL, &timeout);
131
+ }
132
+
133
+ if (ret == -1) {
134
+ error("Failed to get the output of \"%s\"", exec);
135
+ } else if (ret == 0) {
136
+ info("Cannot get the output of \"%s\" within %"PRId64" seconds", exec, (int64_t)timeout.tv_sec);
137
+ } else {
138
+ while (fgets(buf, MAXSIZE_PROC_CMDLINE, f) != NULL) {
139
+ if ((begin = strstr(buf, SYSTEMD_HIERARCHY_STRING))) {
140
+ end = begin = begin + strlen(SYSTEMD_HIERARCHY_STRING);
141
+ if (!*begin)
142
break;
143
+ while (isalpha(*end))
144
+ end++;
145
+ *end = 0;
146
+ for (int i = 0; cgroups_systemd_options[i].name; i++) {
147
+ if (!strcmp(begin, cgroups_systemd_options[i].name)) {
148
+ retval = cgroups_systemd_options[i].setting;
149
+ break;
150
+ }
151
}
152
+ break;
153
}
133
- break;
154
}
155
}
156