Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2019 Alexey Dobriyan <adobriyan@gmail.com>
0003  *
0004  * Permission to use, copy, modify, and distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 /* Test that open(O_TMPFILE), linkat() doesn't screw accounting. */
0017 #include <errno.h>
0018 #include <sched.h>
0019 #include <stdio.h>
0020 #include <sys/types.h>
0021 #include <sys/stat.h>
0022 #include <fcntl.h>
0023 #include <sys/mount.h>
0024 #include <unistd.h>
0025 
0026 int main(void)
0027 {
0028     int fd;
0029 
0030     if (unshare(CLONE_NEWNS) == -1) {
0031         if (errno == ENOSYS || errno == EPERM) {
0032             fprintf(stderr, "error: unshare, errno %d\n", errno);
0033             return 4;
0034         }
0035         fprintf(stderr, "error: unshare, errno %d\n", errno);
0036         return 1;
0037     }
0038     if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
0039         fprintf(stderr, "error: mount '/', errno %d\n", errno);
0040         return 1;
0041     }
0042 
0043     /* Our heroes: 1 root inode, 1 O_TMPFILE inode, 1 permanent inode. */
0044     if (mount(NULL, "/tmp", "tmpfs", 0, "nr_inodes=3") == -1) {
0045         fprintf(stderr, "error: mount tmpfs, errno %d\n", errno);
0046         return 1;
0047     }
0048 
0049     fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
0050     if (fd == -1) {
0051         fprintf(stderr, "error: open 1, errno %d\n", errno);
0052         return 1;
0053     }
0054     if (linkat(fd, "", AT_FDCWD, "/tmp/1", AT_EMPTY_PATH) == -1) {
0055         fprintf(stderr, "error: linkat, errno %d\n", errno);
0056         return 1;
0057     }
0058     close(fd);
0059 
0060     fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
0061     if (fd == -1) {
0062         fprintf(stderr, "error: open 2, errno %d\n", errno);
0063         return 1;
0064     }
0065 
0066     return 0;
0067 }