Skip to content

Commit 61d47c4

Browse files
Alexey Dobriyantorvalds
authored andcommitted
proc: test /proc/self symlink
There are plans to change how /proc/self result is calculated, for that a test is necessary. Use direct system call because of this whole getpid caching story. Link: http://lkml.kernel.org/r/20180627195103.GB18113@avx2 Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Cc: Shuah Khan <shuahkh@osg.samsung.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bdf228a commit 61d47c4

4 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎tools/testing/selftests/proc/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
/proc-uptime-001
1010
/proc-uptime-002
1111
/read
12+
/self

‎tools/testing/selftests/proc/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CFLAGS += -Wall -O2 -Wno-unused-function
2+
CFLAGS += -D_GNU_SOURCE
23

34
TEST_GEN_PROGS :=
45
TEST_GEN_PROGS += fd-001-lookup
@@ -12,5 +13,6 @@ TEST_GEN_PROGS += proc-self-wchan
1213
TEST_GEN_PROGS += proc-uptime-001
1314
TEST_GEN_PROGS += proc-uptime-002
1415
TEST_GEN_PROGS += read
16+
TEST_GEN_PROGS += self
1517

1618
include ../lib.mk

‎tools/testing/selftests/proc/proc.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
#include <stdbool.h>
77
#include <stdlib.h>
88
#include <string.h>
9+
#include <unistd.h>
10+
#include <sys/syscall.h>
11+
12+
static inline pid_t sys_getpid(void)
13+
{
14+
return syscall(SYS_getpid);
15+
}
916

1017
static inline bool streq(const char *s1, const char *s2)
1118
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
// Test that /proc/self gives correct TGID.
17+
#undef NDEBUG
18+
#include <assert.h>
19+
#include <stdio.h>
20+
#include <unistd.h>
21+
22+
#include "proc.h"
23+
24+
int main(void)
25+
{
26+
char buf1[64], buf2[64];
27+
pid_t pid;
28+
ssize_t rv;
29+
30+
pid = sys_getpid();
31+
snprintf(buf1, sizeof(buf1), "%u", pid);
32+
33+
rv = readlink("/proc/self", buf2, sizeof(buf2));
34+
assert(rv == strlen(buf1));
35+
buf2[rv] = '\0';
36+
assert(streq(buf1, buf2));
37+
38+
return 0;
39+
}

0 commit comments

Comments
 (0)