this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fcntl() is complete

+110 -2
+103
src/libSystem/kernel-bsd/fcntl.cpp
··· 2 2 #include <fcntl.h> 3 3 #include <errno.h> 4 4 #include <cstdio> 5 + #include <cstring> 5 6 #include <unistd.h> 6 7 #include "../libc/darwin_errno_codes.h" 7 8 #include "../common/auto.h" 8 9 #include "../common/path.h" 9 10 #include "io.h" 10 11 #include "../libc/errno.h" 12 + 13 + #define REQUIRE_ARG_PTR(arg) { if (!arg) { errno = DARWIN_EINVAL; return -1; } } 11 14 12 15 int __darwin_fcntl(int fd, int cmd, void* arg) 13 16 { ··· 15 18 { 16 19 case DARWIN_F_DUPFD: 17 20 cmd = F_DUPFD; 21 + break; 22 + case DARWIN_F_DUPFD_CLOEXEC: 23 + cmd = F_DUPFD_CLOEXEC; 18 24 break; 19 25 case DARWIN_F_GETFD: // CLOEXEC is compatible 20 26 cmd = F_GETFD; ··· 52 58 break; 53 59 case DARWIN_F_GETPATH: 54 60 { 61 + REQUIRE_ARG_PTR(arg); 62 + 55 63 char procpath[255]; 56 64 int rv; 57 65 ··· 68 76 } 69 77 case DARWIN_F_PREALLOCATE: 70 78 { 79 + REQUIRE_ARG_PTR(arg); 80 + 71 81 __darwin_fallocate* fa = static_cast<__darwin_fallocate*>(arg); 72 82 fa->allocated = 0; 73 83 ··· 120 130 } 121 131 case DARWIN_F_RDADVISE: 122 132 { 133 + REQUIRE_ARG_PTR(arg); 134 + 123 135 const __darwin_rdadvise* adv = static_cast<__darwin_rdadvise*>(arg); 124 136 int err = posix_fadvise(fd, adv->offset, adv->count, POSIX_FADV_WILLNEED); 125 137 ··· 133 145 } 134 146 case DARWIN_F_RDAHEAD: 135 147 { 148 + REQUIRE_ARG_PTR(arg); 149 + 136 150 int advice = (arg) ? POSIX_FADV_NORMAL : POSIX_FADV_RANDOM; 137 151 int err = posix_fadvise(fd, 0, 1, advice); // on Linux, the offset and length doesn't matter for these advices 138 152 ··· 143 157 } 144 158 else 145 159 return 0; 160 + } 161 + case DARWIN_F_NOCACHE: 162 + { 163 + struct stat st; 164 + if (fstat(fd, &st) == -1) 165 + { 166 + errnoOut(); 167 + return -1; 168 + } 169 + 170 + // This is as close as we can get... 171 + 172 + int advice = (arg) ? POSIX_FADV_NORMAL : POSIX_FADV_DONTNEED; 173 + int err = posix_fadvise(fd, 0, 1, advice); 174 + 175 + if (err) // doesn't use errno! 176 + { 177 + errno = errnoLinuxToDarwin(err); 178 + return -1; 179 + } 180 + else 181 + return 0; 182 + } 183 + case DARWIN_F_FULLFSYNC: 184 + { 185 + if (fsync(fd) == -1) 186 + { 187 + errnoOut(); 188 + return -1; 189 + } 190 + 191 + // This is as close as we can get... 192 + // Linux 2.6.39+ 193 + if (syncfs(fd) == -1) 194 + { 195 + errnoOut(); 196 + return -1; 197 + } 198 + return 0; 199 + } 200 + case DARWIN_F_GETLK: 201 + { 202 + REQUIRE_ARG_PTR(arg); 203 + 204 + __darwin_flock* lock = static_cast<__darwin_flock*>(arg); 205 + struct flock native; 206 + 207 + memset(lock, 0, sizeof(*lock)); 208 + 209 + if (fcntl(fd, F_GETLK, &native) == -1) 210 + { 211 + errnoOut(); 212 + return -1; 213 + } 214 + 215 + lock->l_start = native.l_start; 216 + lock->l_len = native.l_len; 217 + lock->l_pid = native.l_pid; 218 + lock->l_type = native.l_type; 219 + lock->l_whence = native.l_whence; 220 + 221 + return 0; 222 + } 223 + case DARWIN_F_SETLK: 224 + case DARWIN_F_SETLKW: 225 + { 226 + REQUIRE_ARG_PTR(arg); 227 + 228 + int ncmd = (cmd == DARWIN_F_SETLK) ? F_SETLK : F_SETLKW; 229 + const __darwin_flock* lock = static_cast<__darwin_flock*>(arg); 230 + struct flock native; 231 + 232 + native.l_start = lock->l_start; 233 + native.l_len = lock->l_len; 234 + native.l_pid = lock->l_pid; 235 + native.l_type = lock->l_type; 236 + native.l_whence = lock->l_whence; 237 + 238 + return AutoErrno<int>(fcntl, fd, ncmd, &native); 239 + } 240 + case DARWIN_F_SETNOSIGPIPE: 241 + case DARWIN_F_GETNOSIGPIPE: 242 + case DARWIN_F_READBOOTSTRAP: 243 + case DARWIN_F_WRITEBOOTSTRAP: 244 + case DARWIN_F_LOG2PHYS: 245 + case DARWIN_F_LOG2PHYS_EXT: 246 + { 247 + errno = DARWIN_ENOSYS; 248 + return -1; 146 249 } 147 250 // TODO: other values 148 251
+7 -2
src/libSystem/kernel-bsd/fcntl.h
··· 4 4 #include <sys/types.h> 5 5 6 6 #define DARWIN_F_DUPFD 0 7 + #define DARWIN_F_DUPFD_CLOEXEC 67 7 8 #define DARWIN_F_GETFD 1 8 9 #define DARWIN_F_SETFD 2 9 10 #define DARWIN_F_GETFL 3 10 11 #define DARWIN_F_SETFL 4 11 12 #define DARWIN_F_GETOWN 5 12 13 #define DARWIN_F_SETOWN 6 14 + #define DARWIN_F_GETLK 7 15 + #define DARWIN_F_SETLK 8 16 + #define DARWIN_F_SETLKW 9 13 17 #define DARWIN_F_GETPATH 50 14 18 #define DARWIN_F_PREALLOCATE 42 15 19 #define DARWIN_F_SETSIZE 43 ··· 19 23 #define DARWIN_F_WRITEBOOTSTRAP 47 20 24 #define DARWIN_F_NOCACHE 48 21 25 #define DARWIN_F_LOG2PHYS 49 26 + #define DARWIN_F_LOG2PHYS_EXT 65 22 27 #define DARWIN_F_FULLFSYNC 51 23 28 #define DARWIN_F_SETNOSIGPIPE 73 24 29 #define DARWIN_F_GETNOSIGPIPE 74 ··· 36 41 int64_t l_start; 37 42 int64_t l_len; 38 43 pid_t l_pid; 39 - short l_type; 40 - short l_whence; 44 + short l_type; 45 + short l_whence; 41 46 }; 42 47 43 48 struct __darwin_fallocate