From: Nicky Chorley <ndchorley@gmail.com>
To: kernelnewbies@kernelnewbies.org
Subject: Why can't I remove a cgroup?
Date: Tue, 9 Aug 2022 19:30:58 +0100 [thread overview]
Message-ID: <CAG-xSoYopZKAd9jom8NuEigBT==K0Q4ed6_8eCLWk27N04Z8kA@mail.gmail.com> (raw)
Hi folks,
I wrote a small program to start a new process (a shell), create a
cgroup and add the process to it. This all works fine - the process ID
of the shell ends up in the cgroup.procs file and when my program
exits, cgroup.procs ends up empty as expected. If I try and remove the
cgroup directory, though, I get a load of "Operation not permitted"
errors and I'm not sure why. I'm running on openSUSE (kernel 5.3.18)
with cgroups v2.
My program looks like:
#define _GNU_SOURCE
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
int startShell(void *argument) {
char* arguments[] = {"bash", NULL};
char* environment[] = {NULL};
execve("/bin/bash", arguments, environment);
return 0;
}
int main() {
mkdir(
"/sys/fs/cgroup/my_group",
S_IRWXO | S_IRGRP | S_IROTH
);
int stackSizeBytes = 65536;
char *stackStart = malloc(stackSizeBytes);
char *stackEnd = stackStart + stackSizeBytes;
pid_t childPid = clone(
startShell,
stackEnd,
SIGCHLD,
NULL
);
FILE* procsFile = fopen(
"/sys/fs/cgroup/my_group/cgroup.procs",
"w"
);
fprintf(procsFile, "%d\n", childPid);
fclose(procsFile);
waitpid(childPid, NULL, 0);
return 0;
}
and output looks like:
$ su -c ./run
Password:
# echo $$
4382
# cat /sys/fs/cgroup/my_group/cgroup.procs
4382
4522
# exit
$ cat /sys/fs/cgroup/my_group/cgroup.procs
$ su -c "rm -rf /sys/fs/cgroup/my_group"
Password:
rm: cannot remove '/sys/fs/cgroup/my_group/cgroup.events': Operation
not permitted
rm: cannot remove '/sys/fs/cgroup/my_group/memory.events': Operation
not permitted
... other lines omitted for brevity
Could someone please help me understand what's going on here?
Thanks!
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
next reply other threads:[~2022-08-09 18:31 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-09 18:30 Nicky Chorley [this message]
2022-08-09 18:37 ` Why can't I remove a cgroup? Valentin Vidić
2022-08-13 6:04 ` Nicky Chorley
2022-08-10 4:08 ` Jack Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAG-xSoYopZKAd9jom8NuEigBT==K0Q4ed6_8eCLWk27N04Z8kA@mail.gmail.com' \
--to=ndchorley@gmail.com \
--cc=kernelnewbies@kernelnewbies.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).