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

proc: test lseek on /proc/net/dev

This line in tools/testing/selftests/proc/read.c was added to catch
oopses, not to verify lseek correctness:

(void)lseek(fd, 0, SEEK_SET);

Oh, well. Prevent more embarassement with simple test.

Link: https://lkml.kernel.org/r/aKTCfMuRXOpjBXxI@p183
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Alexey Dobriyan and committed by
Andrew Morton
b1e34412 6c609f36

+70
+1
tools/testing/selftests/proc/.gitignore
··· 7 7 /proc-loadavg-001 8 8 /proc-maps-race 9 9 /proc-multiple-procfs 10 + /proc-net-dev-lseek 10 11 /proc-empty-vm 11 12 /proc-pid-vm 12 13 /proc-self-map-files-001
+1
tools/testing/selftests/proc/Makefile
··· 10 10 TEST_GEN_PROGS += proc-2-is-kthread 11 11 TEST_GEN_PROGS += proc-loadavg-001 12 12 TEST_GEN_PROGS += proc-maps-race 13 + TEST_GEN_PROGS += proc-net-dev-lseek 13 14 TEST_GEN_PROGS += proc-empty-vm 14 15 TEST_GEN_PROGS += proc-pid-vm 15 16 TEST_GEN_PROGS += proc-self-map-files-001
+68
tools/testing/selftests/proc/proc-net-dev-lseek.c
··· 1 + /* 2 + * Copyright (c) 2025 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 + #undef _GNU_SOURCE 17 + #define _GNU_SOURCE 18 + #undef NDEBUG 19 + #include <assert.h> 20 + #include <errno.h> 21 + #include <fcntl.h> 22 + #include <string.h> 23 + #include <unistd.h> 24 + #include <sched.h> 25 + /* 26 + * Test that lseek("/proc/net/dev/", 0, SEEK_SET) 27 + * a) works, 28 + * b) does what you think it does. 29 + */ 30 + int main(void) 31 + { 32 + /* /proc/net/dev output is deterministic in fresh netns only. */ 33 + if (unshare(CLONE_NEWNET) == -1) { 34 + if (errno == ENOSYS || errno == EPERM) { 35 + return 4; 36 + } 37 + return 1; 38 + } 39 + 40 + const int fd = open("/proc/net/dev", O_RDONLY); 41 + assert(fd >= 0); 42 + 43 + char buf1[4096]; 44 + const ssize_t rv1 = read(fd, buf1, sizeof(buf1)); 45 + /* 46 + * Not "<=", this file can't be empty: 47 + * there is header, "lo" interface with some zeroes. 48 + */ 49 + assert(0 < rv1); 50 + assert(rv1 <= sizeof(buf1)); 51 + 52 + /* Believe it or not, this line broke one day. */ 53 + assert(lseek(fd, 0, SEEK_SET) == 0); 54 + 55 + char buf2[4096]; 56 + const ssize_t rv2 = read(fd, buf2, sizeof(buf2)); 57 + /* Not "<=", see above. */ 58 + assert(0 < rv2); 59 + assert(rv2 <= sizeof(buf2)); 60 + 61 + /* Test that lseek rewinds to the beginning of the file. */ 62 + assert(rv1 == rv2); 63 + assert(memcmp(buf1, buf2, rv1) == 0); 64 + 65 + /* Contents of the file is not validated: this test is about lseek(). */ 66 + 67 + return 0; 68 + }