Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Alexey Dobriyan and committed by
Linus Torvalds
61d47c4e bdf228a2

+49
+1
tools/testing/selftests/proc/.gitignore
··· 9 9 /proc-uptime-001 10 10 /proc-uptime-002 11 11 /read 12 + /self
+2
tools/testing/selftests/proc/Makefile
··· 1 1 CFLAGS += -Wall -O2 -Wno-unused-function 2 + CFLAGS += -D_GNU_SOURCE 2 3 3 4 TEST_GEN_PROGS := 4 5 TEST_GEN_PROGS += fd-001-lookup ··· 13 12 TEST_GEN_PROGS += proc-uptime-001 14 13 TEST_GEN_PROGS += proc-uptime-002 15 14 TEST_GEN_PROGS += read 15 + TEST_GEN_PROGS += self 16 16 17 17 include ../lib.mk
+7
tools/testing/selftests/proc/proc.h
··· 6 6 #include <stdbool.h> 7 7 #include <stdlib.h> 8 8 #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 + } 9 16 10 17 static inline bool streq(const char *s1, const char *s2) 11 18 {
+39
tools/testing/selftests/proc/self.c
··· 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 + }