Merge regression tests: clean up userns_setns.c

Initialize 'ret' variable to EXIT_FAILURE instead of doing so
in every error handling. Also fixes error handling in the case that mkfifio() fails.

Signed-off-by: Leesoo Ahn <lsahn@ooseel.net>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1245
Approved-by: Steve Beattie <steve+gitlab@nxnw.org>
Merged-by: Steve Beattie <steve+gitlab@nxnw.org>
This commit is contained in:
Steve Beattie 2024-06-03 03:20:41 +00:00
commit 32dc6cdbab

View file

@ -10,7 +10,7 @@
int main(int argc, char *argv[])
{
int ret;
int ret = EXIT_FAILURE;
char *pipename = "/tmp/userns_pipe";
char *parentpipe = NULL, *childpipe = NULL;
int childpipefd;
@ -20,34 +20,31 @@ int main(int argc, char *argv[])
if (get_pipes(pipename, &parentpipe, &childpipe) == -1) {
fprintf(stderr, "FAIL - failed to allocate pipes\n");
ret = EXIT_FAILURE;
goto out;
}
if (mkfifo(childpipe, 0666) == -1)
if (mkfifo(childpipe, 0666) == -1) {
perror("FAIL - setns child mkfifo");
goto out;
}
childpipefd = open_read_pipe(childpipe);
if (childpipefd == -1) {
fprintf(stderr, "FAIL - couldn't open child pipe\n");
ret = EXIT_FAILURE;
goto out;
}
if (unshare(CLONE_NEWUSER) == -1) {
perror("FAIL - unshare");
ret = EXIT_FAILURE;
goto out;
}
if (write_to_pipe(parentpipe) == -1) { // let parent know user namespace is created
fprintf(stderr, "FAIL - child could not write in pipe\n");
ret = EXIT_FAILURE;
goto out;
}
if (read_from_pipe(childpipefd) == -1) { // wait for parent tell child can finish
fprintf(stderr, "FAIL - child could not read from pipe\n");
ret = EXIT_FAILURE;
goto out;
}