| 1 |
/* COVERAGE: open openat close creat */ |
| 2 |
#define _GNU_SOURCE |
| 3 |
#include <stdio.h> |
| 4 |
#include <sys/types.h> |
| 5 |
#include <sys/stat.h> |
| 6 |
#include <sys/syscall.h> |
| 7 |
#include <fcntl.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <unistd.h> |
| 10 |
#include <sys/mman.h> |
| 11 |
|
| 12 |
// To test for glibc support for openat(): |
| 13 |
// |
| 14 |
// Since glibc 2.10: |
| 15 |
// _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L |
| 16 |
// Before glibc 2.10: |
| 17 |
// _ATFILE_SOURCE |
| 18 |
|
| 19 |
#define GLIBC_SUPPORT \ |
| 20 |
(_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L \ |
| 21 |
|| defined(_ATFILE_SOURCE)) |
| 22 |
|
| 23 |
#if GLIBC_SUPPORT |
| 24 |
static inline int __open(const char* pathname, int flags, mode_t mode) |
| 25 |
{ |
| 26 |
#if defined(__aarch64__) && defined(SYS_openat) |
| 27 |
return syscall(SYS_openat, pathname, flags, mode); |
| 28 |
#else |
| 29 |
return syscall(SYS_open, pathname, flags, mode); |
| 30 |
#endif |
| 31 |
} |
| 32 |
#endif |
| 33 |
|
| 34 |
int main() |
| 35 |
{ |
| 36 |
int fd1, fd2; |
| 37 |
|
| 38 |
mlockall(MCL_CURRENT); |
| 39 |
|
| 40 |
fd2 = creat("foobar1",S_IREAD|S_IWRITE); |
| 41 |
//staptest// [[[[[[[[open (!!!!openat (AT_FDCWD, ]]]]"foobar1", O_WRONLY|O_CREAT[[[[.O_LARGEFILE]]]]?|O_TRUNC!!!!creat ("foobar1"]]]], 0600) = NNNN |
| 42 |
|
| 43 |
creat((char *)-1, S_IREAD|S_IWRITE); |
| 44 |
#ifdef __s390__ |
| 45 |
//staptest// [[[[open (0x[7]?[f]+, O_WRONLY|O_CREAT[[[[.O_LARGEFILE]]]]?|O_TRUNC!!!!creat (0x[7]?[f]+]]]], 0600) = NNNN |
| 46 |
#else |
| 47 |
//staptest// [[[[[[[[open (!!!!openat (AT_FDCWD, ]]]]0x[f]+, O_WRONLY|O_CREAT[[[[.O_LARGEFILE]]]]?|O_TRUNC!!!!creat (0x[f]+]]]], 0600) = NNNN |
| 48 |
#endif |
| 49 |
|
| 50 |
creat("foobarX", -1); |
| 51 |
//staptest// [[[[[[[[open (!!!!openat (AT_FDCWD, ]]]]"foobarX", O_WRONLY|O_CREAT[[[[.O_LARGEFILE]]]]?|O_TRUNC!!!!creat ("foobarX"]]]], 037777777777) = |
| 52 |
|
| 53 |
fd1 = open("foobar2",O_WRONLY|O_CREAT, S_IRWXU); |
| 54 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]"foobar2", O_WRONLY|O_CREAT[[[[.O_LARGEFILE]]]]?, 0700) = NNNN |
| 55 |
close(fd1); |
| 56 |
//staptest// close (NNNN) = 0 |
| 57 |
|
| 58 |
#if GLIBC_SUPPORT |
| 59 |
fd1 = openat(AT_FDCWD, "foobar2", O_WRONLY|O_CREAT, S_IRWXU); |
| 60 |
//staptest// openat (AT_FDCWD, "foobar2", O_WRONLY|O_CREAT[[[[.O_LARGEFILE]]]]?, 0700) = NNNN |
| 61 |
close(fd1); |
| 62 |
//staptest// close (NNNN) = 0 |
| 63 |
#endif |
| 64 |
|
| 65 |
fd1 = open("foobar2",O_RDONLY); |
| 66 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]"foobar2", O_RDONLY[[[[.O_LARGEFILE]]]]?) = NNNN |
| 67 |
close(fd1); |
| 68 |
//staptest// close (NNNN) = 0 |
| 69 |
|
| 70 |
#if GLIBC_SUPPORT |
| 71 |
fd1 = openat(AT_FDCWD, "foobar2", O_RDONLY); |
| 72 |
//staptest// openat (AT_FDCWD, "foobar2", O_RDONLY[[[[.O_LARGEFILE]]]]?) = NNNN |
| 73 |
close(fd1); |
| 74 |
//staptest// close (NNNN) = 0 |
| 75 |
#endif |
| 76 |
|
| 77 |
fd1 = open("foobar2",O_RDWR); |
| 78 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]"foobar2", O_RDWR[[[[.O_LARGEFILE]]]]?) = NNNN |
| 79 |
close(fd1); |
| 80 |
//staptest// close (NNNN) = 0 |
| 81 |
|
| 82 |
#if GLIBC_SUPPORT |
| 83 |
fd1 = openat(AT_FDCWD, "foobar2", O_RDWR); |
| 84 |
//staptest// openat (AT_FDCWD, "foobar2", O_RDWR[[[[.O_LARGEFILE]]]]?) = NNNN |
| 85 |
close(fd1); |
| 86 |
//staptest// close (NNNN) = 0 |
| 87 |
#endif |
| 88 |
|
| 89 |
fd1 = open("foobar2",O_APPEND|O_WRONLY); |
| 90 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]"foobar2", O_WRONLY|O_APPEND[[[[.O_LARGEFILE]]]]?) = NNNN |
| 91 |
close(fd1); |
| 92 |
//staptest// close (NNNN) = 0 |
| 93 |
|
| 94 |
#if GLIBC_SUPPORT |
| 95 |
fd1 = openat(AT_FDCWD, "foobar2", O_APPEND|O_WRONLY); |
| 96 |
//staptest// openat (AT_FDCWD, "foobar2", O_WRONLY|O_APPEND[[[[.O_LARGEFILE]]]]?) = NNNN |
| 97 |
close(fd1); |
| 98 |
//staptest// close (NNNN) = 0 |
| 99 |
#endif |
| 100 |
|
| 101 |
fd1 = open("foobar2",O_DIRECT|O_RDWR); |
| 102 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]"foobar2", O_RDWR|O_DIRECT[[[[.O_LARGEFILE]]]]?) = NNNN |
| 103 |
close(fd1); |
| 104 |
//staptest// close (NNNN) = 0 |
| 105 |
|
| 106 |
#if GLIBC_SUPPORT |
| 107 |
fd1 = openat(AT_FDCWD, "foobar2", O_DIRECT|O_RDWR); |
| 108 |
//staptest// openat (AT_FDCWD, "foobar2", O_RDWR|O_DIRECT[[[[.O_LARGEFILE]]]]?) = NNNN |
| 109 |
close(fd1); |
| 110 |
//staptest// close (NNNN) = 0 |
| 111 |
#endif |
| 112 |
|
| 113 |
fd1 = open("foobar2",O_NOATIME|O_SYNC|O_RDWR); |
| 114 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]"foobar2", O_RDWR[[[[.O_LARGEFILE]]]]?|O_SYNC|O_NOATIME) = NNNN |
| 115 |
close(fd1); |
| 116 |
//staptest// close (NNNN) = 0 |
| 117 |
|
| 118 |
#if GLIBC_SUPPORT |
| 119 |
fd1 = openat(AT_FDCWD, "foobar2", O_NOATIME|O_SYNC|O_RDWR); |
| 120 |
//staptest// openat (AT_FDCWD, "foobar2", O_RDWR[[[[.O_LARGEFILE]]]]?|O_SYNC|O_NOATIME) = NNNN |
| 121 |
close(fd1); |
| 122 |
//staptest// close (NNNN) = 0 |
| 123 |
#endif |
| 124 |
|
| 125 |
/* Now test some bad opens */ |
| 126 |
fd1 = open("/",O_WRONLY); |
| 127 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]"/", O_WRONLY[[[[.O_LARGEFILE]]]]?) = -NNNN (EISDIR) |
| 128 |
close (fd1); |
| 129 |
//staptest// close (NNNN) = -NNNN (EBADF) |
| 130 |
|
| 131 |
#if GLIBC_SUPPORT |
| 132 |
fd1 = openat(AT_FDCWD, "/", O_WRONLY); |
| 133 |
//staptest// openat (AT_FDCWD, "/", O_WRONLY[[[[.O_LARGEFILE]]]]?) = -NNNN (EISDIR) |
| 134 |
close (fd1); |
| 135 |
//staptest// close (NNNN) = -NNNN (EBADF) |
| 136 |
#endif |
| 137 |
|
| 138 |
fd1 = open("foobar2",O_WRONLY|O_CREAT|O_EXCL, S_IRWXU); |
| 139 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]"foobar2", O_WRONLY|O_CREAT|O_EXCL[[[[.O_LARGEFILE]]]]?, 0700) = -NNNN (EEXIST) |
| 140 |
|
| 141 |
#if GLIBC_SUPPORT |
| 142 |
fd1 = openat(AT_FDCWD, "foobar2",O_WRONLY|O_CREAT|O_EXCL, S_IRWXU); |
| 143 |
//staptest// openat (AT_FDCWD, "foobar2", O_WRONLY|O_CREAT|O_EXCL[[[[.O_LARGEFILE]]]]?, 0700) = -NNNN (EEXIST) |
| 144 |
#endif |
| 145 |
|
| 146 |
open((char *)-1, O_WRONLY|O_CREAT|O_EXCL, S_IRWXU); |
| 147 |
#ifdef __s390__ |
| 148 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]0x[7]?[f]+, O_WRONLY|O_CREAT|O_EXCL[[[[.O_LARGEFILE]]]]?, 0700) = |
| 149 |
#else |
| 150 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]0x[f]+, O_WRONLY|O_CREAT|O_EXCL[[[[.O_LARGEFILE]]]]?, 0700) = |
| 151 |
#endif |
| 152 |
|
| 153 |
#if GLIBC_SUPPORT |
| 154 |
openat(-1, "foobarX", O_WRONLY|O_CREAT|O_EXCL, S_IRWXU); |
| 155 |
//staptest// openat (-1, "foobarX", O_WRONLY|O_CREAT|O_EXCL[[[[.O_LARGEFILE]]]]?, 0700) = |
| 156 |
|
| 157 |
openat(AT_FDCWD, (char *)-1, O_WRONLY|O_CREAT|O_EXCL, S_IRWXU); |
| 158 |
#ifdef __s390__ |
| 159 |
//staptest// openat (AT_FDCWD, 0x[7]?[f]+, O_WRONLY|O_CREAT|O_EXCL[[[[.O_LARGEFILE]]]]?, 0700) = |
| 160 |
#else |
| 161 |
//staptest// openat (AT_FDCWD, 0x[f]+, O_WRONLY|O_CREAT|O_EXCL[[[[.O_LARGEFILE]]]]?, 0700) = |
| 162 |
#endif |
| 163 |
#endif |
| 164 |
|
| 165 |
open("foobarX", -1, S_IRWXU); |
| 166 |
//staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]"foobarX", O_[^ ]+|XXXX, 0700) = |
| 167 |
|
| 168 |
fd1 = open("foobarY", O_WRONLY|O_CREAT, -1); |
| 169 |
//staptest// [[[open (!!!!openat (AT_FDCWD, ]]]]"foobarY", O_WRONLY|O_CREAT[[[[|O_LARGEFILE]]]]?, 037777777777) = NNNN |
| 170 |
|
| 171 |
close(fd1); |
| 172 |
//staptest// close (NNNN) = 0 |
| 173 |
|
| 174 |
#if GLIBC_SUPPORT |
| 175 |
openat(AT_FDCWD, "foobarX", -1, S_IRWXU); |
| 176 |
//staptest// openat (AT_FDCWD, "foobarX", O_[^ ]+|XXXX, 0700) = |
| 177 |
|
| 178 |
fd1 = openat(AT_FDCWD, "foobarZ", O_WRONLY|O_CREAT, -1); |
| 179 |
//staptest// openat (AT_FDCWD, "foobarZ", O_WRONLY|O_CREAT[[[[|O_LARGEFILE]]]]?, 037777777777) = NNNN |
| 180 |
|
| 181 |
close(fd1); |
| 182 |
//staptest// close (NNNN) = 0 |
| 183 |
#endif |
| 184 |
|
| 185 |
close(-1); |
| 186 |
//staptest// close (-1) = -NNNN (EBADF) |
| 187 |
|
| 188 |
// Since glibc 2.26, the wrapper function for open() calls openat() so we need |
| 189 |
// to run some extra tests using our own wrapper to make sure open() works. |
| 190 |
#if __GLIBC_PREREQ(2, 26) |
| 191 |
fd1 = __open("foobar2", O_WRONLY|O_CREAT, S_IRWXU); |
| 192 |
//staptest// open ("foobar2", O_WRONLY|O_CREAT[[[[.O_LARGEFILE]]]]?, 0700) = NNNN |
| 193 |
close(fd1); |
| 194 |
//staptest// close (NNNN) = 0 |
| 195 |
|
| 196 |
__open((char *)-1, O_WRONLY|O_CREAT|O_EXCL, S_IRWXU); |
| 197 |
#ifdef __s390__ |
| 198 |
//staptest// open (0x[7]?[f]+, O_WRONLY|O_CREAT|O_EXCL[[[[.O_LARGEFILE]]]]?, 0700) = |
| 199 |
#else |
| 200 |
//staptest// open (0x[f]+, O_WRONLY|O_CREAT|O_EXCL[[[[.O_LARGEFILE]]]]?, 0700) = |
| 201 |
#endif |
| 202 |
|
| 203 |
__open("foobarX", -1, S_IRWXU); |
| 204 |
//staptest// open ("foobarX", O_[^ ]+|XXXX, 0700) = |
| 205 |
|
| 206 |
fd1 = __open("foobarY", O_WRONLY|O_CREAT, -1); |
| 207 |
//staptest// open ("foobarY", O_WRONLY|O_CREAT[[[[|O_LARGEFILE]]]]?, 037777777777) = NNNN |
| 208 |
close(fd1); |
| 209 |
//staptest// close (NNNN) = 0 |
| 210 |
#endif |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |