jingfei195887 opened a new pull request, #22543:
URL: https://github.com/apache/nuttx/pull/22543
## Summary
Add TMPFS `link`, `symlink`, `readlink` and `lstat` operations under
`CONFIG_FS_LINKS`. Hard links share file contents, inode identity and a bounded
link count. Removing or replacing one name preserves the other aliases and any
open-file or mapping references. Symbolic links support relative targets and
absolute targets within the same mounted volume, with bounded traversal and
final-component nofollow handling.
## Impact
- Feature: enables the four link operations and link metadata. Regular-file
`st_nlink` also changes from zero to one when links are disabled, and becomes
zero after its last name is removed.
- Compatibility: no public signature or on-disk format change. Directories
cannot be hard linked. Cross-volume targets can be stored and read, but
following them returns `EXDEV`; relative traversal outside the volume does too.
Absolute targets retain existing VFS path normalization, including its handling
of `..` before TMPFS link resolution.
- Allocation: aliases share object/data storage but each name consumes
memory. `statfs` counts the shared object once; `FIOC_FILEPATH` can select any
surviving name. Failed destination allocation preserves the rename source.
- Build process, hardware and drivers: unchanged. The new callbacks use the
existing `CONFIG_FS_LINKS` option.
- Documentation: describes link behavior, limits and permissions in
`tmpfs.rst`.
## Testing
Local `sim:nsh` tests use dedicated TMPFS mounts and the standalone apps
fixture below. The same source and byte-identical effective configuration are
used for every before/after pair. Each uses a 512 KiB independent FS heap.
Runtime comparisons:
| Configuration | Baseline | This PR |
|---|---|---|
| 64-bit | 260 checks, 111 failures, 3 prerequisite skips | 625 checks, 0
failures, 0 skips |
| 32-bit / largefile | 260 checks, 111 failures, 3 prerequisite skips | 625
checks, 0 failures, 0 skips |
| 64-bit / permissions and task identity | 284 checks, 122 failures, 3
prerequisite skips | 651 checks, 0 failures, 0 skips |
| 64-bit / links disabled | 13 checks, 1 failure, 0 prerequisite skips | 13
checks, 0 failures, 0 skips |
Baseline failures include unsupported operations and zero link counts.
Dependent tests stop when setup is unavailable, so the counts differ. The
links-disabled baseline has one failure: a named regular file reports zero
links. The test expectation remains one on both revisions. All branch
applications return status 0.
Coverage includes alias identity/content/counts, last-name removal, rename
replacement, mapped-file lifetime, relative/absolute/dangling targets,
nofollow/exclusive opens, loops, link metadata, name/link limits, permissions,
repeated mounts and allocation exhaustion. Each enabled branch run also
performs 2,000 getpath/fstat reads alongside 200 alias-parent mutation cycles,
with zero errors. This runs on the single simulated CPU; it is not SMP
validation.
The detached mapping case explicitly uses cached internal callbacks to
inspect and release the retained TMPFS object after unmount. It verifies
object/mapping lifetime and the mount guard, **not** public-fd or
open-directory operation safety after unmount. Generic VFS handling of those
operations is outside this change.
Protected `qemu-armv7r:pnsh` and kernel `qemu-armv8a:knsh` complete images
build with TMPFS, links, permissions and task identity enabled; both contain
all four link callbacks and the permission callback. Make, CMake and OSTest
pass, and the CMake-built fixture also passes. No physical hardware,
protected/kernel runtime, chroot, SMP or concurrent-unmount testing was
performed. The NSH startup `/tmp` mount ENOTDIR message is retained in the
logs; the dedicated test mounts succeed.
Baseline `f9d3eb13c3f412426a2658227f2b332e21e3a526`; tested commit
`f0d414f74911c426a07bb314154e32ab751bc677`. Total branch matrix: 1914 checks;
CMake fixture: 651 checks.
Footprint uses 64-bit sim:nsh with `DEFAULT_SMALL`, full optimization, TMPFS
linked, coverage and debug symbols disabled. Compared with the same baseline
configuration:
| Links | text | data | bss |
|---|---:|---:|---:|
| Enabled | +5408 bytes | +0 bytes | +0 bytes |
| Disabled | +2752 bytes | +0 bytes | +0 bytes |
<details>
<summary>Before: full 64-bit baseline runtime log</summary>
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> tmpfslinkscheck
FAIL new file has one link errno=0
FAIL create hardlink errno=38
SKIP hardlink lifetime and limit checks: prerequisite failed
FAIL create symbolic link errno=38
SKIP symbolic link resolution and lifecycle checks: prerequisite failed
FAIL create mapped file alias errno=38
SKIP mapping after unlink: prerequisite failed
FAIL create victim alias errno=38
FAIL destination retains source identity and count errno=38
FAIL victim alias retains its own identity and count errno=2
FAIL open through path errno=2
FAIL replacement retains two distinct file objects errno=2
FAIL unlink victim final alias errno=2
FAIL create edge symlink errno=38
FAIL readlink rejects a regular file errno=38
FAIL readlink rejects a directory errno=38
FAIL change link timestamps errno=2
FAIL lutimens changes link itself errno=2
FAIL change target through link errno=2
FAIL utimens follows link errno=2
FAIL readdir includes symlink errno=2
FAIL reject directory hardlink errno=38
FAIL rmdir rejects a symbolic link errno=2
FAIL symlink does not replace an existing link errno=38
FAIL create edge dangling link errno=38
FAIL exclusive dangling create fails errno=0
FAIL create edge directory link errno=38
FAIL create link to absent intermediate path errno=38
FAIL dangling create needs existing parent errno=0
FAIL create bounded link chain errno=38
FAIL create bounded link chain errno=38
FAIL create bounded link chain errno=38
FAIL create bounded link chain errno=38
FAIL create bounded link chain errno=38
FAIL create bounded link chain errno=38
FAIL create bounded link chain errno=38
FAIL create bounded link chain errno=38
FAIL open through path errno=2
FAIL create link beyond traversal limit errno=38
FAIL reject traversal beyond SYMLOOP_MAX errno=2
FAIL reject oversized link target errno=38
FAIL accept SYMLINK_MAX target length errno=38
FAIL read maximum target length errno=38
FAIL rename symbolic link itself errno=2
FAIL renamed link retains type errno=2
FAIL open through path errno=2
FAIL link destination into source tree errno=38
FAIL reject rename into resolved descendant errno=2
FAIL repeat hardlink errno=38
FAIL repeat symlink errno=38
FAIL repeat hardlink to symlink errno=38
FAIL open through path errno=2
FAIL repeat hardlink errno=38
FAIL repeat symlink errno=38
FAIL repeat hardlink to symlink errno=38
FAIL open through path errno=2
FAIL repeat hardlink errno=38
FAIL repeat symlink errno=38
FAIL repeat hardlink to symlink errno=38
FAIL open through path errno=2
FAIL repeat hardlink errno=38
FAIL repeat symlink errno=38
FAIL repeat hardlink to symlink errno=38
FAIL open through path errno=2
FAIL repeat hardlink errno=38
FAIL repeat symlink errno=38
FAIL repeat hardlink to symlink errno=38
FAIL open through path errno=2
FAIL repeat hardlink errno=38
FAIL repeat symlink errno=38
FAIL repeat hardlink to symlink errno=38
FAIL open through path errno=2
FAIL repeat hardlink errno=38
FAIL repeat symlink errno=38
FAIL repeat hardlink to symlink errno=38
FAIL open through path errno=2
FAIL repeat hardlink errno=38
FAIL repeat symlink errno=38
FAIL repeat hardlink to symlink errno=38
FAIL open through path errno=2
FAIL absolute link to mount root errno=38
FAIL resolve absolute mount root errno=2
FAIL open through path errno=2
FAIL relative dot target errno=38
FAIL open through path errno=2
FAIL store absolute cross-volume target errno=38
FAIL cross-volume resolution reports EXDEV errno=2
FAIL readlink preserves cross-volume target text errno=38
FAIL store relative target leaving volume errno=38
FAIL relative root escape reports EXDEV errno=2
FAIL store absolute dangling target errno=38
FAIL absolute create uses target name errno=2
FAIL rmdir does not follow root symlink errno=2
FAIL rename root symlink itself errno=2
FAIL renamed root link retains link type errno=2
FAIL unlink root link itself errno=2
FAIL create relative symlink in first parent errno=38
FAIL hardlink symlink into different parent errno=38
FAIL symlink aliases share identity across parents errno=2
FAIL open through path errno=2
FAIL open through path errno=2
FAIL accept NAME_MAX alias errno=38
FAIL open through path errno=2
FAIL oversized names preserve source and count errno=36
TMPFSLINKS RACE reads=2000 cycles=0 errors=2001
FAIL concurrent getpath and alias parent removal errno=38
FAIL rename reports allocation failure errno=38
FAIL link reports allocation failure errno=38
FAIL symlink reports allocation failure errno=38
FAIL allocation failures preserve source identity and count errno=2
FAIL failed rename leaves destination absent errno=0
FAIL hardlink succeeds after releasing heap errno=38
FAIL symlink succeeds after releasing heap errno=38
FAIL rename succeeds after releasing heap errno=2
FAIL open through path errno=2
TMPFSLINKS RESULT checks=260 failures=111 skips=3
nsh> echo status=$?
status=1
nsh> poweroff
```
</details>
<details>
<summary>Other runtime logs (baseline summary excerpts where
indicated)</summary>
final-64-branch
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> tmpfslinkscheck
TMPFSLINKS RACE reads=2000 cycles=200 errors=0
TMPFSLINKS RESULT checks=625 failures=0 skips=0
nsh> echo status=$?
status=0
nsh> poweroff
```
final-32-base
```text
TMPFSLINKS RACE reads=2000 cycles=0 errors=2001
TMPFSLINKS RESULT checks=260 failures=111 skips=3
status=1
```
final-32-branch
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> tmpfslinkscheck
TMPFSLINKS RACE reads=2000 cycles=200 errors=0
TMPFSLINKS RESULT checks=625 failures=0 skips=0
nsh> echo status=$?
status=0
nsh> poweroff
```
final-permissions-base
```text
TMPFSLINKS RACE reads=2000 cycles=0 errors=2001
TMPFSLINKS RESULT checks=284 failures=122 skips=3
status=1
```
final-permissions-branch
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> tmpfslinkscheck
TMPFSLINKS RACE reads=2000 cycles=200 errors=0
TMPFSLINKS RESULT checks=651 failures=0 skips=0
nsh> echo status=$?
status=0
nsh> poweroff
```
final-off-base
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> tmpfslinkscheck
FAIL regular file link count with links disabled errno=0
TMPFSLINKS RESULT checks=13 failures=1 skips=0
nsh> echo status=$?
status=1
nsh> poweroff
```
final-off-branch
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> tmpfslinkscheck
TMPFSLINKS RESULT checks=13 failures=0 skips=0
nsh> echo status=$?
status=0
nsh> poweroff
```
CMake fixture
```text
Error mount tmpfs '' on '/tmp' failed: 20
nsh> [Ktmpfslinkscheck
TMPFSLINKS RACE reads=2000 cycles=200 errors=0
TMPFSLINKS RESULT checks=651 failures=0 skips=0
nsh> [Kecho status=$?
status=0
nsh> [Kpoweroff
```
</details>
<details>
<summary>Build, footprint and final verification logs</summary>
final-64-base
```text
Pac SIM with dynamic libs..
'/lib/x86_64-linux-gnu/libz.so.1' -> 'sim-pac/libs/libz.so.1'
'/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
'/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
SIM elf with dynamic libs archive in nuttx.tgz
```
final-64-branch
```text
Pac SIM with dynamic libs..
'/lib/x86_64-linux-gnu/libz.so.1' -> 'sim-pac/libs/libz.so.1'
'/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
'/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
SIM elf with dynamic libs archive in nuttx.tgz
```
final-protected
```text
kflash: 181280 B 512 KB 34.58%
uflash: 0 GB 512 KB 0.00%
ksram: 12 KB 8 MB 0.15%
usram: 0 GB 8 MB 0.00%
CP: nuttx.bin
```
final-kernel
```text
Memory region Used Size Region Size %age Used
dram: 320 KB 2 MB 15.62%
pgram: 0 B 4 MB 0.00%
CP: nuttx.hex
CP: nuttx.bin
```
final-protected
```text
arm-none-eabi-gcc (Arm GNU Toolchain 13.2.rel1 (Build arm-13.7)) 13.2.1
20231009
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
qemu-armv7r:pnsh build PASS
```
final-kernel
```text
aarch64-none-elf-gcc (GCC) 13.4.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
qemu-armv8a:knsh build PASS
```
final-size-on-base
```text
text data bss dec hex filename
567045 2800 13056 582901 8e4f5 nuttx
```
final-size-on-branch
```text
text data bss dec hex filename
572453 2800 13056 588309 8fa15 nuttx
```
final-size-off-base
```text
text data bss dec hex filename
559732 2768 13056 575556 8c844 nuttx
```
final-size-off-branch
```text
text data bss dec hex filename
562484 2768 13056 578308 8d304 nuttx
```
```text
target : nuttx
branch : fs/tmpfs-links
head : f0d414f74911c426a07bb314154e32ab751bc677
subject : fs/tmpfs: add link,symlink,readlink,lstat for tmpfs
commits : 1
base : f9d3eb13c3 (merge-base with upstream/master)
behind : 0 commits of upstream/master
nuttx : f0d414f749
apps : 0a37af9866
options : CONFIG_FS_TMPFS CONFIG_FS_LINKS CONFIG_SCHED_USER_IDENTITY
CONFIG_PSEUDOFS_ATTRIBUTES CONFIG_FS_PERMISSION CONFIG_FS_HEAPSIZE=524288
CONFIG_EXAMPLES_TMPFSLINKSCHECK CONFIG_TESTING_OSTEST
host : Linux x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
checkpatch: rc=0
signer : jingfei <[email protected]>
commits missing your Signed-off-by: 0
make build: rc=0
warnings in changed files: 0
ostest: Exiting with status 0
cmake reconfigure: rc=0
cmake build: rc=0
worktree tracked changes after: 0
RESULT: PASS
```
</details>
<details>
<summary>Standalone regression fixture</summary>
Place these files in `apps/examples/tmpfslinkscheck/`. Configure `sim:nsh`,
enable `FS_TMPFS`, `FS_LINKS`, `EXAMPLES_TMPFSLINKSCHECK`, and set
`FS_HEAPSIZE=524288`. Run `make olddefconfig`, build, then run
`tmpfslinkscheck` in NSH. For the permission configuration, also enable
`SCHED_USER_IDENTITY`, `PSEUDOFS_ATTRIBUTES` and `FS_PERMISSION`. The 32-bit
run enables `SIM_M32` and `FS_LARGEFILE`. The disabled run clears `FS_LINKS`.
This is a standalone local regression fixture; no separate apps PR is included.
Kconfig
```text
config EXAMPLES_TMPFSLINKSCHECK
tristate "TMPFS links regression"
default n
depends on FS_TMPFS && FS_RAMMAP
```
Make.defs
```text
ifneq ($(CONFIG_EXAMPLES_TMPFSLINKSCHECK),)
CONFIGURED_APPS += $(APPDIR)/examples/tmpfslinkscheck
endif
```
Makefile
```text
include $(APPDIR)/Make.defs
PROGNAME = tmpfslinkscheck
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 16384
MODULE = $(CONFIG_EXAMPLES_TMPFSLINKSCHECK)
MAINSRC = tmpfslinkscheck_main.c
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)/fs
include $(APPDIR)/Application.mk
```
CMakeLists.txt
```text
#
##############################################################################
# apps/examples/tmpfslinkscheck/CMakeLists.txt
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more
contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses
this
# file to you under the Apache License, Version 2.0 (the "License"); you may
not
# use this file except in compliance with the License. You may obtain a
copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
under
# the License.
#
#
##############################################################################
if(CONFIG_EXAMPLES_TMPFSLINKSCHECK)
nuttx_add_application(
NAME tmpfslinkscheck
SRCS tmpfslinkscheck_main.c
STACKSIZE 16384
PRIORITY 100
MODULE ${CONFIG_EXAMPLES_TMPFSLINKSCHECK}
INCLUDE_DIRECTORIES ${NUTTX_DIR}/fs)
endif()
```
tmpfslinkscheck_main.c
```c
/****************************************************************************
* apps/examples/tmpfslinkscheck/tmpfslinkscheck_main.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/ioctl.h>
#if CONFIG_FS_HEAPSIZE > 0
# include "fs_heap.h"
#endif
/****************************************************************************
* Private Data
****************************************************************************/
static unsigned int g_checks;
static unsigned int g_failures;
static unsigned int g_skips;
/****************************************************************************
* Private Functions
****************************************************************************/
static bool check(bool pass, FAR const char *label)
{
g_checks++;
if (!pass)
{
g_failures++;
printf("FAIL %s errno=%d\n", label, errno);
}
return pass;
}
#ifdef CONFIG_FS_LINKS
static void skip(FAR const char *label)
{
g_skips++;
printf("SKIP %s: prerequisite failed\n", label);
}
#endif
static int create_file(FAR const char *path)
{
int fd = open(path, O_CREAT | O_RDWR | O_EXCL, 0600);
check(fd >= 0, "create file");
if (fd >= 0)
{
check(write(fd, "original", 8) == 8, "write original contents");
}
return fd;
}
static void check_contents(FAR const char *path, FAR const char *expected)
{
char data[16];
size_t len = strlen(expected);
int fd = open(path, O_RDONLY);
check(fd >= 0, "open through path");
if (fd >= 0)
{
memset(data, 0xa5, sizeof(data));
check(read(fd, data, len) == len && !memcmp(data, expected, len),
"contents through path");
check(close(fd) == 0, "close path reader");
}
}
#ifdef CONFIG_FS_LINKS
static void hardlinks(void)
{
struct stat first;
struct stat other;
struct statfs before;
struct statfs after;
char path[PATH_MAX];
int fd;
int i;
int ret;
if (!check(mount(NULL, "/tpl-hard", "tmpfs", 0, NULL) == 0,
"mount hardlink fixture"))
{
return;
}
check(mkdir("/tpl-hard/a", 0700) == 0, "mkdir original parent");
check(mkdir("/tpl-hard/b", 0700) == 0, "mkdir alias parent");
fd = create_file("/tpl-hard/a/file");
if (fd < 0)
{
goto unmount;
}
check(fstat(fd, &first) == 0 && first.st_nlink == 1,
"new file has one link");
check(statfs("/tpl-hard", &before) == 0, "statfs before alias");
if (!check(link("/tpl-hard/a/file", "/tpl-hard/b/alias") == 0,
"create hardlink"))
{
skip("hardlink lifetime and limit checks");
goto close_file;
}
check(stat("/tpl-hard/b/alias", &other) == 0 &&
other.st_ino == first.st_ino && other.st_nlink == 2,
"hardlink shares inode and link count");
check(statfs("/tpl-hard", &after) == 0 &&
after.f_files == before.f_files, "statfs counts shared file once");
check(pwrite(fd, "modified", 8, 0) == 8, "modify through original fd");
check_contents("/tpl-hard/b/alias", "modified");
check(link("/tpl-hard/b/alias", "/tpl-hard/b/second") == 0,
"link an existing alias");
check(rename("/tpl-hard/b/alias", "/tpl-hard/b/second") == 0,
"rename two aliases of same file");
check(stat("/tpl-hard/b/alias", &other) == 0 && other.st_nlink == 3,
"same-inode rename preserves both names");
check(unlink("/tpl-hard/a/file") == 0, "unlink original name");
check(rmdir("/tpl-hard/a") == 0, "remove original parent");
check(fstat(fd, &other) == 0 && other.st_nlink == 2 &&
other.st_ino == first.st_ino,
"open identity survives parent removal");
memset(path, 0, sizeof(path));
ret = ioctl(fd, FIOC_FILEPATH, (unsigned long)path);
check(ret == 0 && stat(path, &other) == 0 &&
other.st_ino == first.st_ino, "getpath returns surviving alias");
check(unlink("/tpl-hard/b/alias") == 0, "unlink first alias");
check(rename("/tpl-hard/b/second", "/tpl-hard/last") == 0,
"move last alias");
check(rmdir("/tpl-hard/b") == 0, "remove second parent");
check(fstat(fd, &other) == 0 && other.st_nlink == 1,
"one surviving link");
for (i = 1; i < LINK_MAX; i++)
{
snprintf(path, sizeof(path), "/tpl-hard/alias%d", i);
if (!check(link("/tpl-hard/last", path) == 0, "add link below limit"))
{
break;
}
}
if (i == LINK_MAX)
{
errno = 0;
check(link("/tpl-hard/last", "/tpl-hard/overflow") == -1 &&
errno == EMLINK, "reject link beyond LINK_MAX");
check(fstat(fd, &other) == 0 && other.st_nlink == LINK_MAX,
"link limit preserves count");
}
while (--i > 0)
{
snprintf(path, sizeof(path), "/tpl-hard/alias%d", i);
check(unlink(path) == 0, "remove limit fixture alias");
}
check(unlink("/tpl-hard/last") == 0, "remove last link while open");
check(fstat(fd, &other) == 0 && other.st_nlink == 0,
"open unlinked file has zero links");
errno = 0;
check(ioctl(fd, FIOC_FILEPATH, (unsigned long)path) == -1 &&
errno == ENOENT, "unlinked file has no pathname");
check(pwrite(fd, "retained", 8, 0) == 8, "write unlinked open file");
close_file:
check(close(fd) == 0, "close original descriptor");
unmount:
check(umount("/tpl-hard") == 0, "unmount hardlink fixture");
}
static void symlinks(void)
{
struct stat target;
struct stat linkst;
char data[16];
int fd;
int ret;
if (!check(mount(NULL, "/tpl-sym", "tmpfs", 0, NULL) == 0,
"mount symlink fixture"))
{
return;
}
check(mkdir("/tpl-sym/dir", 0700) == 0, "mkdir symlink parent");
fd = create_file("/tpl-sym/dir/data");
if (fd < 0)
{
goto unmount;
}
check(fstat(fd, &target) == 0, "stat target");
check(close(fd) == 0, "close target");
if (!check(symlink("data", "/tpl-sym/dir/sym") == 0,
"create symbolic link"))
{
skip("symbolic link resolution and lifecycle checks");
goto unmount;
}
check(lstat("/tpl-sym/dir/sym", &linkst) == 0 &&
S_ISLNK(linkst.st_mode) && linkst.st_size == 4 &&
linkst.st_ino != target.st_ino, "lstat describes symbolic link");
check(stat("/tpl-sym/dir/sym", &linkst) == 0 &&
linkst.st_ino == target.st_ino, "stat follows symbolic link");
memset(data, 'x', sizeof(data));
check(readlink("/tpl-sym/dir/sym", data, 2) == 2 &&
!memcmp(data, "da", 2) && data[2] == 'x', "readlink short buffer");
check(readlink("/tpl-sym/dir/sym", data, sizeof(data)) == 4 &&
!memcmp(data, "data", 4) && data[4] == 'x',
"readlink no terminator");
check_contents("/tpl-sym/dir/sym", "original");
errno = 0;
fd = open("/tpl-sym/dir/sym", O_RDONLY | O_NOFOLLOW);
check(fd == -1 && errno == ELOOP, "O_NOFOLLOW rejects final symlink");
if (fd >= 0)
{
close(fd);
}
errno = 0;
fd = open("/tpl-sym/dir/sym", O_CREAT | O_EXCL | O_WRONLY, 0600);
check(fd == -1 && errno == EEXIST,
"exclusive create does not follow link");
if (fd >= 0)
{
close(fd);
}
check(symlink("dir", "/tpl-sym/dirlink") == 0,
"create intermediate directory link");
check_contents("/tpl-sym/dirlink/data", "original");
check(symlink("../dir/data", "/tpl-sym/dir/parent") == 0,
"create parent-relative link");
check_contents("/tpl-sym/dir/parent", "original");
check(symlink("/tpl-sym/dir/data", "/tpl-sym/absolute") == 0,
"create same-mount absolute link");
check_contents("/tpl-sym/absolute", "original");
check(symlink("missing", "/tpl-sym/dangling") == 0,
"create dangling symbolic link");
errno = 0;
check(stat("/tpl-sym/dangling", &linkst) == -1 && errno == ENOENT,
"stat dangling link fails");
check(lstat("/tpl-sym/dangling", &linkst) == 0 &&
S_ISLNK(linkst.st_mode), "lstat dangling link succeeds");
fd = open("/tpl-sym/dangling", O_CREAT | O_RDWR, 0600);
check(fd >= 0, "create through dangling link");
if (fd >= 0)
{
check(close(fd) == 0, "close newly created target");
}
check(stat("/tpl-sym/missing", &linkst) == 0 && S_ISREG(linkst.st_mode),
"dangling create uses target pathname");
check(symlink("cycle", "/tpl-sym/cycle") == 0, "create link cycle");
errno = 0;
check(stat("/tpl-sym/cycle", &linkst) == -1 && errno == ELOOP,
"stat bounds symbolic link traversal");
errno = 0;
fd = open("/tpl-sym/cycle", O_RDONLY);
check(fd == -1 && errno == ELOOP, "open bounds symbolic link traversal");
if (fd >= 0)
{
close(fd);
}
ret = link("/tpl-sym/dir/sym", "/tpl-sym/dir/hardsym");
check(ret == 0, "hardlink a symbolic link");
if (ret == 0)
{
check(lstat("/tpl-sym/dir/hardsym", &linkst) == 0 &&
S_ISLNK(linkst.st_mode) && linkst.st_nlink == 2,
"hardlink preserves symbolic link inode");
check(unlink("/tpl-sym/dir/sym") == 0,
"unlink original symlink name");
check_contents("/tpl-sym/dir/hardsym", "original");
check(unlink("/tpl-sym/dir/hardsym") == 0,
"unlink symbolic link alias");
check_contents("/tpl-sym/dir/data", "original");
}
unmount:
check(umount("/tpl-sym") == 0, "unmount symlink fixture");
}
static void mapped_links(void)
{
FAR char *memory;
int fd;
if (!check(mount(NULL, "/tpl-map", "tmpfs", 0, NULL) == 0,
"mount mapping fixture"))
{
return;
}
fd = create_file("/tpl-map/file");
if (fd < 0)
{
goto unmount;
}
if (!check(link("/tpl-map/file", "/tpl-map/alias") == 0,
"create mapped file alias"))
{
skip("mapping after unlink");
close(fd);
goto unmount;
}
memory = mmap(NULL, 8, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
check(memory != MAP_FAILED, "map aliased file");
check(close(fd) == 0, "close mapped descriptor");
check(unlink("/tpl-map/file") == 0, "remove mapped original");
check(unlink("/tpl-map/alias") == 0, "remove mapped final alias");
if (memory != MAP_FAILED)
{
check(!memcmp(memory, "original", 8), "mapping survives last unlink");
memory[0] = 'O';
check(memory[0] == 'O', "mapping remains writable");
check(munmap(memory, 8) == 0, "release unlinked mapping");
}
unmount:
check(umount("/tpl-map") == 0, "unmount mapping fixture");
}
static void replace_aliases(void)
{
struct stat source;
struct stat victim;
struct stat now;
struct statfs info;
int src;
int dst;
if (!check(mount(NULL, "/tpl-replace", "tmpfs", 0, NULL) == 0,
"mount replacement fixture"))
{
return;
}
src = create_file("/tpl-replace/source");
dst = create_file("/tpl-replace/victim");
if (src >= 0 && dst >= 0)
{
check(fstat(src, &source) == 0, "stat rename source");
check(fstat(dst, &victim) == 0, "stat rename victim");
check(pwrite(dst, "previous", 8, 0) == 8, "set victim contents");
check(link("/tpl-replace/victim", "/tpl-replace/alias") == 0,
"create victim alias");
check(rename("/tpl-replace/source", "/tpl-replace/victim") == 0,
"replace a file with another surviving alias");
check(stat("/tpl-replace/victim", &now) == 0 &&
now.st_ino == source.st_ino && now.st_nlink == 1,
"destination retains source identity and count");
check(stat("/tpl-replace/alias", &now) == 0 &&
now.st_ino == victim.st_ino && now.st_nlink == 1,
"victim alias retains its own identity and count");
check_contents("/tpl-replace/victim", "original");
check_contents("/tpl-replace/alias", "previous");
check(statfs("/tpl-replace", &info) == 0 && info.f_files == 2,
"replacement retains two distinct file objects");
check(unlink("/tpl-replace/alias") == 0, "unlink victim final alias");
check(fstat(dst, &now) == 0 && now.st_nlink == 0,
"open victim survives replacement and unlink");
}
if (src >= 0)
{
check(close(src) == 0, "close replacement source");
}
if (dst >= 0)
{
check(close(dst) == 0, "close replacement victim");
}
check(umount("/tpl-replace") == 0, "unmount replacement fixture");
}
static void link_edges(void)
{
struct timespec times[2];
struct stat before;
struct stat now;
FAR struct dirent *entry;
FAR DIR *dir;
char target[SYMLINK_MAX + 2];
char path[64];
char next[32];
bool found = false;
int fd;
int i;
if (!check(mount(NULL, "/tpl-edge", "tmpfs", 0, NULL) == 0,
"mount link edge fixture"))
{
return;
}
fd = create_file("/tpl-edge/data");
if (fd >= 0)
{
check(close(fd) == 0, "close edge file");
}
check(mkdir("/tpl-edge/dir", 0700) == 0, "create edge directory");
check(symlink("data", "/tpl-edge/sym") == 0, "create edge symlink");
errno = 0;
check(readlink("/tpl-edge/data", target, sizeof(target)) == -1 &&
errno == EINVAL, "readlink rejects a regular file");
errno = 0;
check(readlink("/tpl-edge/dir", target, sizeof(target)) == -1 &&
errno == EINVAL, "readlink rejects a directory");
check(stat("/tpl-edge/data", &before) == 0, "save target timestamps");
times[0].tv_sec = 123;
times[0].tv_nsec = 0;
times[1].tv_sec = 456;
times[1].tv_nsec = 0;
check(lutimens("/tpl-edge/sym", times) == 0, "change link timestamps");
check(lstat("/tpl-edge/sym", &now) == 0 &&
now.st_atim.tv_sec == 123 && now.st_mtim.tv_sec == 456,
"lutimens changes link itself");
check(stat("/tpl-edge/data", &now) == 0 &&
now.st_mtim.tv_sec == before.st_mtim.tv_sec &&
now.st_mtim.tv_nsec == before.st_mtim.tv_nsec,
"lutimens preserves target timestamps");
times[1].tv_sec = 789;
check(utimens("/tpl-edge/sym", times) == 0, "change target through link");
check(stat("/tpl-edge/data", &now) == 0 && now.st_mtim.tv_sec == 789,
"utimens follows link");
dir = opendir("/tpl-edge");
check(dir != NULL, "open edge directory");
if (dir != NULL)
{
while ((entry = readdir(dir)) != NULL)
{
if (!strcmp(entry->d_name, "sym"))
{
found = true;
check(entry->d_type == DTYPE_LINK, "readdir symlink type");
}
}
check(found, "readdir includes symlink");
check(closedir(dir) == 0, "close edge directory");
}
errno = 0;
check(link("/tpl-edge/dir", "/tpl-edge/harddir") == -1 &&
errno == EPERM, "reject directory hardlink");
errno = 0;
check(rmdir("/tpl-edge/sym") == -1 && errno == ENOTDIR,
"rmdir rejects a symbolic link");
errno = 0;
check(symlink("data", "/tpl-edge/sym") == -1 && errno == EEXIST,
"symlink does not replace an existing link");
check(symlink("uncreated", "/tpl-edge/dangling") == 0,
"create edge dangling link");
errno = 0;
fd = open("/tpl-edge/dangling", O_CREAT | O_EXCL | O_RDWR, 0600);
check(fd == -1 && errno == EEXIST, "exclusive dangling create fails");
if (fd >= 0)
{
close(fd);
}
errno = 0;
check(stat("/tpl-edge/uncreated", &now) == -1 && errno == ENOENT,
"exclusive create leaves target absent");
check(symlink("dir", "/tpl-edge/dirlink") == 0,
"create edge directory link");
errno = 0;
fd = open("/tpl-edge/dirlink/missing/leaf", O_CREAT | O_RDWR, 0600);
check(fd == -1 && errno == ENOENT, "missing intermediate after link");
if (fd >= 0)
{
close(fd);
}
errno = 0;
check(stat("/tpl-edge/dir/missing", &now) == -1 && errno == ENOENT,
"failed lookup does not create intermediate component");
check(symlink("dir/missing/leaf", "/tpl-edge/missingpath") == 0,
"create link to absent intermediate path");
errno = 0;
fd = open("/tpl-edge/missingpath", O_CREAT | O_RDWR, 0600);
check(fd == -1 && errno == ENOENT,
"dangling create needs existing parent");
if (fd >= 0)
{
close(fd);
}
for (i = SYMLOOP_MAX - 1; i >= 0; i--)
{
snprintf(path, sizeof(path), "/tpl-edge/chain%d", i);
if (i == SYMLOOP_MAX - 1)
{
strlcpy(next, "data", sizeof(next));
}
else
{
snprintf(next, sizeof(next), "chain%d", i + 1);
}
check(symlink(next, path) == 0, "create bounded link chain");
}
check_contents("/tpl-edge/chain0", "original");
check(symlink("chain0", "/tpl-edge/extra") == 0,
"create link beyond traversal limit");
errno = 0;
check(stat("/tpl-edge/extra", &now) == -1 && errno == ELOOP,
"reject traversal beyond SYMLOOP_MAX");
memset(target, 'a', SYMLINK_MAX + 1);
target[SYMLINK_MAX + 1] = '\0';
errno = 0;
check(symlink(target, "/tpl-edge/long") == -1 &&
errno == ENAMETOOLONG, "reject oversized link target");
target[SYMLINK_MAX] = '\0';
check(symlink(target, "/tpl-edge/max") == 0,
"accept SYMLINK_MAX target length");
memset(target, 0, sizeof(target));
check(readlink("/tpl-edge/max", target, sizeof(target)) == SYMLINK_MAX,
"read maximum target length");
check(rename("/tpl-edge/sym", "/tpl-edge/moved") == 0,
"rename symbolic link itself");
check(lstat("/tpl-edge/moved", &now) == 0 && S_ISLNK(now.st_mode),
"renamed link retains type");
check_contents("/tpl-edge/moved", "original");
check(mkdir("/tpl-edge/tree", 0700) == 0, "create rename tree");
check(mkdir("/tpl-edge/tree/child", 0700) == 0, "create rename child");
check(symlink("tree/child", "/tpl-edge/inside") == 0,
"link destination into source tree");
errno = 0;
check(rename("/tpl-edge/tree", "/tpl-edge/inside/moved") == -1 &&
errno == EINVAL, "reject rename into resolved descendant");
check(stat("/tpl-edge/tree/child", &now) == 0 && S_ISDIR(now.st_mode),
"rejected rename preserves tree");
check(umount("/tpl-edge") == 0, "unmount edge fixture with aliases");
}
static void repeat_mounts(void)
{
int fd;
int i;
for (i = 0; i < 8; i++)
{
if (!check(mount(NULL, "/tpl-repeat", "tmpfs", 0, NULL) == 0,
"repeat mount"))
{
return;
}
fd = create_file("/tpl-repeat/file");
if (fd >= 0)
{
check(close(fd) == 0, "close repeat file");
}
check(link("/tpl-repeat/file", "/tpl-repeat/hard") == 0,
"repeat hardlink");
check(symlink("hard", "/tpl-repeat/soft") == 0, "repeat symlink");
check(link("/tpl-repeat/soft", "/tpl-repeat/hardsoft") == 0,
"repeat hardlink to symlink");
check_contents("/tpl-repeat/hardsoft", "original");
check(umount("/tpl-repeat") == 0, "repeat unmount populated tree");
}
}
#if CONFIG_FS_HEAPSIZE > 0
static void allocation_failures(void)
{
static FAR void *blocks[1024];
const size_t sizes[] =
{
4096, 256, 1
};
struct stat before;
struct stat after;
bool exhausted = false;
int count = 0;
int renret;
int renerr;
int linkret;
int linkerr;
int symret;
int symerr;
int fd;
int i;
if (!check(mount(NULL, "/tpl-oom", "tmpfs", 0, NULL) == 0,
"mount allocation fixture"))
{
return;
}
fd = create_file("/tpl-oom/file");
if (fd < 0)
{
goto unmount;
}
check(close(fd) == 0, "close allocation source");
check(stat("/tpl-oom/file", &before) == 0,
"save allocation source identity");
for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++)
{
while (count < sizeof(blocks) / sizeof(blocks[0]))
{
blocks[count] = fs_heap_malloc(sizes[i]);
if (blocks[count] == NULL)
{
exhausted = sizes[i] == 1;
break;
}
count++;
}
}
errno = 0;
renret = rename("/tpl-oom/file", "/tpl-oom/moved");
renerr = errno;
errno = 0;
linkret = link("/tpl-oom/file", "/tpl-oom/hard");
linkerr = errno;
errno = 0;
symret = symlink("file", "/tpl-oom/soft");
symerr = errno;
while (count > 0)
{
fs_heap_free(blocks[--count]);
}
check(exhausted, "FS heap exhausted down to one-byte requests");
check(renret == -1 && renerr == ENOMEM,
"rename reports allocation failure");
check(linkret == -1 && linkerr == ENOMEM,
"link reports allocation failure");
check(symret == -1 && symerr == ENOMEM,
"symlink reports allocation failure");
check(stat("/tpl-oom/file", &after) == 0 &&
after.st_ino == before.st_ino && after.st_nlink == 1,
"allocation failures preserve source identity and count");
errno = 0;
check(lstat("/tpl-oom/moved", &after) == -1 && errno == ENOENT,
"failed rename leaves destination absent");
errno = 0;
check(lstat("/tpl-oom/hard", &after) == -1 && errno == ENOENT,
"failed hardlink leaves destination absent");
errno = 0;
check(lstat("/tpl-oom/soft", &after) == -1 && errno == ENOENT,
"failed symlink leaves destination absent");
check(link("/tpl-oom/file", "/tpl-oom/hard") == 0,
"hardlink succeeds after releasing heap");
check(symlink("hard", "/tpl-oom/soft") == 0,
"symlink succeeds after releasing heap");
check(rename("/tpl-oom/file", "/tpl-oom/moved") == 0,
"rename succeeds after releasing heap");
check_contents("/tpl-oom/soft", "original");
unmount:
check(umount("/tpl-oom") == 0, "unmount allocation fixture");
}
#endif
#ifndef CONFIG_DISABLE_PTHREAD
struct race_context_s
{
pthread_barrier_t start;
int fd;
ino_t ino;
unsigned int reads;
unsigned int errors;
};
static FAR void *read_alias_path(FAR void *arg)
{
FAR struct race_context_s *ctx = arg;
struct stat info;
char path[PATH_MAX];
int i;
pthread_barrier_wait(&ctx->start);
for (i = 0; i < 2000; i++)
{
if (ioctl(ctx->fd, FIOC_FILEPATH, (unsigned long)path) != 0 ||
strncmp(path, "/tpl-race/", 10) != 0)
{
ctx->errors++;
}
if (fstat(ctx->fd, &info) != 0 || info.st_ino != ctx->ino ||
info.st_nlink < 1 || info.st_nlink > 2)
{
ctx->errors++;
}
ctx->reads++;
sched_yield();
}
return NULL;
}
static void concurrent_aliases(void)
{
struct race_context_s ctx;
struct stat info;
pthread_t reader;
unsigned int errors = 0;
int i;
int ret;
if (!check(mount(NULL, "/tpl-race", "tmpfs", 0, NULL) == 0,
"mount concurrent fixture"))
{
return;
}
memset(&ctx, 0, sizeof(ctx));
ctx.fd = create_file("/tpl-race/file");
if (ctx.fd < 0)
{
goto unmount;
}
check(fstat(ctx.fd, &info) == 0, "save concurrent inode identity");
ctx.ino = info.st_ino;
ret = pthread_barrier_init(&ctx.start, NULL, 2);
if (!check(ret == 0, "initialize concurrent start barrier"))
{
goto close_file;
}
ret = pthread_create(&reader, NULL, read_alias_path, &ctx);
if (!check(ret == 0, "create getpath reader"))
{
pthread_barrier_destroy(&ctx.start);
goto close_file;
}
pthread_barrier_wait(&ctx.start);
for (i = 0; i < 200; i++)
{
if (mkdir("/tpl-race/dir", 0700) != 0 ||
link("/tpl-race/file", "/tpl-race/dir/alias") != 0)
{
errors++;
break;
}
sched_yield();
if (rename("/tpl-race/dir/alias", "/tpl-race/moved") != 0 ||
rmdir("/tpl-race/dir") != 0 || unlink("/tpl-race/moved") != 0)
{
errors++;
break;
}
sched_yield();
}
check(pthread_join(reader, NULL) == 0, "join getpath reader");
check(pthread_barrier_destroy(&ctx.start) == 0, "destroy start barrier");
printf("TMPFSLINKS RACE reads=%u cycles=%d errors=%u\n",
ctx.reads, i, errors + ctx.errors);
check(i == 200 && ctx.reads == 2000 && errors + ctx.errors == 0,
"concurrent getpath and alias parent removal");
close_file:
check(close(ctx.fd) == 0, "close concurrent file");
unmount:
check(umount("/tpl-race") == 0, "unmount concurrent fixture");
}
#endif
#ifdef CONFIG_FS_PERMISSION
static void link_permissions(void)
{
struct stat info;
char target[32];
int fd;
if (!check(geteuid() == 0, "permission fixture starts privileged") ||
!check(mount(NULL, "/tpl-perm", "tmpfs", 0, NULL) == 0,
"mount permission fixture"))
{
return;
}
check(mkdir("/tpl-perm/private", 0700) == 0, "private target directory");
check(mkdir("/tpl-perm/public", 0777) == 0, "public link directory");
check(chmod("/tpl-perm/public", 0777) == 0, "set public directory mode");
check(mkdir("/tpl-perm/readonly", 0755) == 0, "readonly link directory");
fd = create_file("/tpl-perm/private/data");
if (fd >= 0)
{
check(close(fd) == 0, "close permission target");
}
check(symlink("../private/data", "/tpl-perm/public/sym") == 0,
"link into protected directory");
check(symlink("../private/new", "/tpl-perm/public/create") == 0,
"dangling link into protected directory");
check(symlink("../public/new", "/tpl-perm/readonly/create") == 0,
"readonly link to writable target parent");
check(chmod("/tpl-perm/readonly", 0555) == 0,
"remove write access after creating link");
if (!check(seteuid(1001) == 0, "switch effective identity"))
{
goto unmount;
}
check(lstat("/tpl-perm/public/sym", &info) == 0 &&
S_ISLNK(info.st_mode), "lstat needs only link parent access");
check(readlink("/tpl-perm/public/sym", target, sizeof(target)) == 15,
"readlink needs only link parent access");
errno = 0;
check(stat("/tpl-perm/public/sym", &info) == -1 && errno == EACCES,
"stat checks target directory search access");
errno = 0;
fd = open("/tpl-perm/public/sym", O_RDONLY);
check(fd == -1 && errno == EACCES, "open checks target directory access");
if (fd >= 0)
{
close(fd);
}
errno = 0;
fd = open("/tpl-perm/public/create", O_CREAT | O_RDWR, 0600);
check(fd == -1 && errno == EACCES, "create checks resolved target parent");
if (fd >= 0)
{
close(fd);
}
errno = 0;
check(link("/tpl-perm/private/data", "/tpl-perm/public/alias") == -1 &&
errno == EACCES, "hardlink checks source parent search access");
errno = 0;
check(symlink("data", "/tpl-perm/private/sym") == -1 &&
errno == EACCES, "symlink checks destination parent access");
fd = open("/tpl-perm/readonly/create", O_CREAT | O_RDWR, 0600);
check(fd >= 0, "create through readonly link parent into writable target");
if (fd >= 0)
{
check(fstat(fd, &info) == 0 && info.st_uid == 1001,
"created target records caller identity");
check(close(fd) == 0, "close unprivileged target");
}
check(seteuid(0) == 0, "restore effective identity");
unmount:
check(umount("/tpl-perm") == 0, "unmount permission fixture");
}
#endif
static void path_boundaries(void)
{
struct stat first;
struct stat second;
char name[NAME_MAX + 2];
char path[PATH_MAX];
char target[PATH_MAX];
int fd;
if (!check(mount(NULL, "/tpl-bound", "tmpfs", 0, NULL) == 0,
"mount boundary fixture"))
{
return;
}
if (!check(mount(NULL, "/tpl-other", "tmpfs", 0, NULL) == 0,
"mount cross-volume target"))
{
goto unmount;
}
fd = create_file("/tpl-bound/data");
if (fd >= 0)
{
check(close(fd) == 0, "close boundary file");
}
fd = create_file("/tpl-other/data");
if (fd >= 0)
{
check(close(fd) == 0, "close cross-volume file");
}
check(symlink("/tpl-bound", "/tpl-bound/root") == 0,
"absolute link to mount root");
check(stat("/tpl-bound/root", &first) == 0 && S_ISDIR(first.st_mode),
"resolve absolute mount root");
check_contents("/tpl-bound/root/data", "original");
check(symlink(".", "/tpl-bound/dot") == 0, "relative dot target");
check_contents("/tpl-bound/dot/data", "original");
check(symlink("/tpl-other/data", "/tpl-bound/cross") == 0,
"store absolute cross-volume target");
errno = 0;
check(stat("/tpl-bound/cross", &first) == -1 && errno == EXDEV,
"cross-volume resolution reports EXDEV");
check(readlink("/tpl-bound/cross", target, sizeof(target)) == 15 &&
!memcmp(target, "/tpl-other/data", 15),
"readlink preserves cross-volume target text");
check(symlink("../tpl-bound/data", "/tpl-bound/outside") == 0,
"store relative target leaving volume");
errno = 0;
check(stat("/tpl-bound/outside", &first) == -1 && errno == EXDEV,
"relative root escape reports EXDEV");
check(symlink("/tpl-bound/missing", "/tpl-bound/absolute") == 0,
"store absolute dangling target");
errno = 0;
check(stat("/tpl-bound/absolute", &first) == -1 && errno == ENOENT,
"absolute dangling stat");
fd = open("/tpl-bound/absolute", O_CREAT | O_RDWR, 0600);
check(fd >= 0, "create absolute dangling target");
if (fd >= 0)
{
check(close(fd) == 0, "close absolute target");
}
check(stat("/tpl-bound/missing", &first) == 0 && S_ISREG(first.st_mode),
"absolute create uses target name");
check(rmdir("/tpl-bound/root") == -1 && errno == ENOTDIR,
"rmdir does not follow root symlink");
check(rename("/tpl-bound/root", "/tpl-bound/movedroot") == 0,
"rename root symlink itself");
check(lstat("/tpl-bound/movedroot", &first) == 0 &&
S_ISLNK(first.st_mode), "renamed root link retains link type");
check(unlink("/tpl-bound/movedroot") == 0, "unlink root link itself");
check_contents("/tpl-bound/data", "original");
check(mkdir("/tpl-bound/a", 0700) == 0, "first relative link parent");
check(mkdir("/tpl-bound/b", 0700) == 0, "second relative link parent");
fd = create_file("/tpl-bound/a/data");
if (fd >= 0)
{
close(fd);
}
fd = create_file("/tpl-bound/b/data");
if (fd >= 0)
{
check(pwrite(fd, "sibling!", 8, 0) == 8, "set second parent contents");
check(close(fd) == 0, "close second parent file");
}
check(symlink("data", "/tpl-bound/a/sym") == 0,
"create relative symlink in first parent");
check(link("/tpl-bound/a/sym", "/tpl-bound/b/sym") == 0,
"hardlink symlink into different parent");
check(lstat("/tpl-bound/a/sym", &first) == 0 &&
lstat("/tpl-bound/b/sym", &second) == 0 &&
first.st_ino == second.st_ino && first.st_nlink == 2,
"symlink aliases share identity across parents");
check_contents("/tpl-bound/a/sym", "original");
check_contents("/tpl-bound/b/sym", "sibling!");
memset(name, 'n', NAME_MAX + 1);
name[NAME_MAX] = '\0';
snprintf(path, sizeof(path), "/tpl-bound/%s", name);
check(link("/tpl-bound/data", path) == 0, "accept NAME_MAX alias");
check_contents(path, "original");
name[NAME_MAX] = 'n';
name[NAME_MAX + 1] = '\0';
snprintf(path, sizeof(path), "/tpl-bound/%s", name);
errno = 0;
check(link("/tpl-bound/data", path) == -1 && errno == ENAMETOOLONG,
"reject oversized hardlink name");
errno = 0;
check(symlink("data", path) == -1 && errno == ENAMETOOLONG,
"reject oversized symbolic link name");
errno = 0;
check(rename("/tpl-bound/data", path) == -1 && errno == ENAMETOOLONG,
"reject oversized rename destination");
check(stat("/tpl-bound/data", &first) == 0 && first.st_nlink == 2,
"oversized names preserve source and count");
check(umount("/tpl-other") == 0, "unmount cross-volume target");
unmount:
check(umount("/tpl-bound") == 0, "unmount boundary fixture");
}
static void detached_mapping(void)
{
FAR const struct mountpt_operations *ops;
struct file file;
struct stat info;
FAR void *memory = MAP_FAILED;
char path[PATH_MAX];
int ret;
if (!check(mount(NULL, "/tpl-detach", "tmpfs", 0, NULL) == 0,
"mount detached mapping fixture"))
{
return;
}
memset(&file, 0, sizeof(file));
if (!check(file_open(&file, "/tpl-detach/file", O_CREAT | O_RDWR,
0600) == 0, "open detached file object"))
{
umount("/tpl-detach");
return;
}
ops = file.f_inode->u.i_mops;
check(file_write(&file, "original", 8) == 8, "write detached data");
if (ops->link != NULL)
{
check(link("/tpl-detach/file", "/tpl-detach/alias") == 0,
"create detached alias");
}
ret = file_mmap(&file, NULL, 8, PROT_READ | PROT_WRITE,
MAP_SHARED, 0, &memory);
if (!check(ret == 0, "map detached data"))
{
memory = MAP_FAILED;
}
if (ops->lstat != NULL)
{
check(ops->symlink(file.f_inode, ".", "rootlink") == 0,
"create root link for callback boundary");
check(ops->rmdir(file.f_inode, "rootlink/") == -ENOTDIR,
"rmdir callback does not remove a link target");
check(ops->rmdir(file.f_inode, "") == -EBUSY,
"rmdir callback rejects mount root");
check(ops->rename(file.f_inode, "", "newroot") == -EBUSY,
"rename callback rejects mount root");
}
ret = umount("/tpl-detach");
check(ret == 0, "detach volume with file and mapping retained");
if (ret == 0)
{
check(file.f_inode->u.i_mops == NULL,
"VFS clears callbacks after unmount");
check(ops->fstat(&file, &info) == 0 && S_ISREG(info.st_mode) &&
info.st_size == 8 && info.st_nlink == 0,
"retained file object survives removal of every name");
if (ops->lstat != NULL)
{
check(ops->ioctl(&file, FIOC_FILEPATH,
(unsigned long)path) == -ENOENT,
"retained callback rejects detached mount path");
}
/* VFS clears the callbacks on unmount; release the retained object
* explicitly so this internal lifetime test does not leak it.
*/
check(ops->close(&file) == 0, "release retained object callback");
}
check(file_close(&file) == 0, "release VFS file reference");
if (memory != MAP_FAILED)
{
check(!memcmp(memory, "original", 8),
"mapping survives volume removal");
((FAR char *)memory)[0] = 'O';
check(msync(memory, 8, MS_SYNC) == 0, "sync detached mapping");
check(munmap(memory, 8) == 0, "release detached mapping reference");
}
}
#else
static void no_links(void)
{
struct stat info;
int fd;
if (!check(mount(NULL, "/tpl-off", "tmpfs", 0, NULL) == 0,
"mount without links"))
{
return;
}
fd = create_file("/tpl-off/data");
if (fd >= 0)
{
check(fstat(fd, &info) == 0 && info.st_nlink == 1,
"regular file link count with links disabled");
errno = 0;
check(link("/tpl-off/data", "/tpl-off/alias") == -1 &&
errno == ENOSYS, "disabled hardlink returns ENOSYS");
check(rename("/tpl-off/data", "/tpl-off/moved") == 0,
"rename without links");
check_contents("/tpl-off/moved", "original");
check(unlink("/tpl-off/moved") == 0, "unlink without links");
check(fstat(fd, &info) == 0 && info.st_nlink == 0,
"unlinked open file count without links");
check(close(fd) == 0, "close without links");
}
check(umount("/tpl-off") == 0, "unmount without links");
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, FAR char *argv[])
{
g_checks = 0;
g_failures = 0;
g_skips = 0;
#ifdef CONFIG_FS_LINKS
hardlinks();
symlinks();
mapped_links();
replace_aliases();
link_edges();
repeat_mounts();
path_boundaries();
detached_mapping();
#ifndef CONFIG_DISABLE_PTHREAD
concurrent_aliases();
#endif
#if CONFIG_FS_HEAPSIZE > 0
allocation_failures();
#endif
#ifdef CONFIG_FS_PERMISSION
link_permissions();
#endif
#else
no_links();
#endif
printf("TMPFSLINKS RESULT checks=%u failures=%u skips=%u\n",
g_checks, g_failures, g_skips);
return g_failures == 0 && g_skips == 0 ? 0 : 1;
}
```
</details>
## PR verification Self-Check
- [x] This PR introduces only one functional change.
- [x] I have updated all required description fields above.
- [ ] My PR adheres to all Contributing Guidelines and Documentation
requirements (physical-hardware runtime evidence is unavailable).
- [ ] My PR is still work in progress (not ready for review).
- [x] My PR is ready for review; validation scope and limitations are
disclosed above.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]