Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright © 2018 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(/proc/*/fd/*) opens the same file.
0017 #undef NDEBUG
0018 #include <assert.h>
0019 #include <stdio.h>
0020 #include <sys/types.h>
0021 #include <sys/stat.h>
0022 #include <fcntl.h>
0023 #include <unistd.h>
0024 
0025 int main(void)
0026 {
0027     int fd0, fd1, fd2;
0028     struct stat st0, st1, st2;
0029     char buf[64];
0030     int rv;
0031 
0032     fd0 = open("/", O_DIRECTORY|O_RDONLY);
0033     assert(fd0 >= 0);
0034 
0035     snprintf(buf, sizeof(buf), "/proc/self/fd/%u", fd0);
0036     fd1 = open(buf, O_RDONLY);
0037     assert(fd1 >= 0);
0038 
0039     snprintf(buf, sizeof(buf), "/proc/thread-self/fd/%u", fd0);
0040     fd2 = open(buf, O_RDONLY);
0041     assert(fd2 >= 0);
0042 
0043     rv = fstat(fd0, &st0);
0044     assert(rv == 0);
0045     rv = fstat(fd1, &st1);
0046     assert(rv == 0);
0047     rv = fstat(fd2, &st2);
0048     assert(rv == 0);
0049 
0050     assert(st0.st_dev == st1.st_dev);
0051     assert(st0.st_ino == st1.st_ino);
0052 
0053     assert(st0.st_dev == st2.st_dev);
0054     assert(st0.st_ino == st2.st_ino);
0055 
0056     return 0;
0057 }