1 /* Test for the memfd_create system call.
2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
25 #include <support/check.h>
26 #include <support/support.h>
27 #include <support/test-driver.h>
28 #include <support/xunistd.h>
31 /* Return true if the descriptor has the FD_CLOEXEC flag set. */
35 int flags
= fcntl (fd
, F_GETFD
);
36 TEST_VERIFY (flags
>= 0);
37 return flags
& FD_CLOEXEC
;
40 /* Return the seals set on FD. */
44 int flags
= fcntl (fd
, F_GET_SEALS
);
45 TEST_VERIFY (flags
>= 0);
49 /* Return true if the F_SEAL_SEAL flag is set on the descriptor. */
53 return get_seals (fd
) & F_SEAL_SEAL
;
59 /* Initialized by the first call to memfd_create to 0 (memfd_create
60 unsupported) or 1 (memfd_create is implemented in the kernel).
61 Subsequent iterations check that the success/failure state is
65 for (int do_cloexec
= 0; do_cloexec
< 2; ++do_cloexec
)
66 for (int do_sealing
= 0; do_sealing
< 2; ++do_sealing
)
72 flags
|= MFD_ALLOW_SEALING
;
74 printf ("info: memfd_create with flags=0x%x\n", flags
);
75 int fd
= memfd_create ("tst-memfd_create", flags
);
82 printf ("warning: memfd_create is unsupported\n");
86 TEST_VERIFY (supported
== 0);
90 FAIL_EXIT1 ("memfd_create: %m");
94 TEST_VERIFY (supported
> 0);
96 char *fd_path
= xasprintf ("/proc/self/fd/%d", fd
);
97 char *link
= xreadlink (fd_path
);
99 printf ("info: memfd link: %s\n", link
);
100 TEST_VERIFY (strcmp (link
, "memfd:tst-memfd_create (deleted)"));
101 TEST_VERIFY (is_cloexec (fd
) == do_cloexec
);
102 TEST_VERIFY (is_sealed (fd
) == !do_sealing
);
105 TEST_VERIFY (fcntl (fd
, F_ADD_SEALS
, F_SEAL_WRITE
) == 0);
106 TEST_VERIFY (!is_sealed (fd
));
107 TEST_VERIFY (get_seals (fd
) & F_SEAL_WRITE
);
108 TEST_VERIFY (fcntl (fd
, F_ADD_SEALS
, F_SEAL_SEAL
) == 0);
109 TEST_VERIFY (is_sealed (fd
));
117 return EXIT_UNSUPPORTED
;
121 #include <support/test-driver.c>