#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/mount.h>
+#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
return NULL;
}
- while (fscanf(fp, "%*s %4096s %127s %*s %*d %*d\n",
+ while (fscanf(fp, "%*s %4095s %127s %*s %*d %*d\n",
path, fstype) == 2) {
if (strcmp(fstype, fs_to_find) == 0) {
mnt = strdup(path);
if (mount("none", mnt, CGROUP2_FS_NAME, 0, NULL)) {
/* EBUSY means already mounted */
- if (errno != EBUSY) {
+ if (errno == EBUSY)
+ goto out;
+
+ if (errno == ENODEV) {
fprintf(stderr,
"Failed to mount cgroup2. Are CGROUPS enabled in your kernel?\n");
- free(mnt);
- return NULL;
+ } else {
+ fprintf(stderr,
+ "Failed to mount cgroup2: %s\n",
+ strerror(errno));
}
+ free(mnt);
+ return NULL;
}
+out:
return mnt;
}
int make_path(const char *path, mode_t mode)
{
char *dir, *delim;
- struct stat sbuf;
int rc = -1;
delim = dir = strdup(path);
if (delim)
*delim = '\0';
- if (stat(dir, &sbuf) != 0) {
- if (errno != ENOENT) {
- fprintf(stderr,
- "stat failed for %s: %s\n",
- dir, strerror(errno));
- goto out;
- }
-
- if (mkdir(dir, mode) != 0) {
- fprintf(stderr,
- "mkdir failed for %s: %s",
- dir, strerror(errno));
- goto out;
- }
+ rc = mkdir(dir, mode);
+ if (mkdir(dir, mode) != 0 && errno != EEXIST) {
+ fprintf(stderr, "mkdir failed for %s: %s\n",
+ dir, strerror(errno));
+ goto out;
}
if (delim == NULL)
return rc;
}
+
+int get_command_name(const char *pid, char *comm, size_t len)
+{
+ char path[PATH_MAX];
+ char line[128];
+ FILE *fp;
+
+ if (snprintf(path, sizeof(path),
+ "/proc/%s/status", pid) >= sizeof(path)) {
+ return -1;
+ }
+
+ fp = fopen(path, "r");
+ if (!fp)
+ return -1;
+
+ comm[0] = '\0';
+ while (fgets(line, sizeof(line), fp)) {
+ char *nl, *name;
+
+ name = strstr(line, "Name:");
+ if (!name)
+ continue;
+
+ name += 5;
+ while (isspace(*name))
+ name++;
+
+ nl = strchr(name, '\n');
+ if (nl)
+ *nl = '\0';
+
+ strlcpy(comm, name, len);
+ break;
+ }
+
+ fclose(fp);
+
+ return 0;
+}