forked from grate-driver/linux
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
procfs: do not list TID 0 in /proc/<pid>/task
If a task exits concurrently, task_pid_nr_ns may return 0. [[email protected]: coding style tweaks] [[email protected]: test that /proc/*/task doesn't contain "0"] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Florian Weimer <[email protected]> Signed-off-by: Alexey Dobriyan <[email protected]> Acked-by: Christian Brauner <[email protected]> Reviewed-by: Alexey Dobriyan <[email protected]> Cc: Kees Cook <[email protected]> Cc: "Eric W. Biederman" <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
- Loading branch information
1 parent
83c1fd7
commit 0658a09
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2021 Alexey Dobriyan <[email protected]> | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
// Test that /proc/*/task never contains "0". | ||
#include <sys/types.h> | ||
#include <dirent.h> | ||
#include <signal.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <pthread.h> | ||
|
||
static pid_t pid = -1; | ||
|
||
static void atexit_hook(void) | ||
{ | ||
if (pid > 0) { | ||
kill(pid, SIGKILL); | ||
} | ||
} | ||
|
||
static void *f(void *_) | ||
{ | ||
return NULL; | ||
} | ||
|
||
static void sigalrm(int _) | ||
{ | ||
exit(0); | ||
} | ||
|
||
int main(void) | ||
{ | ||
pid = fork(); | ||
if (pid == 0) { | ||
/* child */ | ||
while (1) { | ||
pthread_t pth; | ||
pthread_create(&pth, NULL, f, NULL); | ||
pthread_join(pth, NULL); | ||
} | ||
} else if (pid > 0) { | ||
/* parent */ | ||
atexit(atexit_hook); | ||
|
||
char buf[64]; | ||
snprintf(buf, sizeof(buf), "/proc/%u/task", pid); | ||
|
||
signal(SIGALRM, sigalrm); | ||
alarm(1); | ||
|
||
while (1) { | ||
DIR *d = opendir(buf); | ||
struct dirent *de; | ||
while ((de = readdir(d))) { | ||
if (strcmp(de->d_name, "0") == 0) { | ||
exit(1); | ||
} | ||
} | ||
closedir(d); | ||
} | ||
|
||
return 0; | ||
} else { | ||
perror("fork"); | ||
return 1; | ||
} | ||
} |