1 | /* |
1 | /* |
2 | * libeio implementation |
2 | * libeio implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann <libeio@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013,2016,2017,2018,2019 Marc Alexander Lehmann <libeio@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
42 | #endif |
42 | #endif |
43 | |
43 | |
44 | #include "eio.h" |
44 | #include "eio.h" |
45 | #include "ecb.h" |
45 | #include "ecb.h" |
46 | |
46 | |
47 | #ifdef EIO_STACKSIZE |
|
|
48 | # define X_STACKSIZE EIO_STACKSIZE |
|
|
49 | #endif |
|
|
50 | #include "xthread.h" |
|
|
51 | |
|
|
52 | #include <errno.h> |
47 | #include <errno.h> |
53 | #include <stddef.h> |
48 | #include <stddef.h> |
54 | #include <stdlib.h> |
49 | #include <stdlib.h> |
55 | #include <string.h> |
50 | #include <string.h> |
56 | #include <errno.h> |
51 | #include <errno.h> |
… | |
… | |
122 | #define chmod(path,mode) _chmod (path, mode) |
117 | #define chmod(path,mode) _chmod (path, mode) |
123 | #define dup(fd) _dup (fd) |
118 | #define dup(fd) _dup (fd) |
124 | #define dup2(fd1,fd2) _dup2 (fd1, fd2) |
119 | #define dup2(fd1,fd2) _dup2 (fd1, fd2) |
125 | #define pipe(fds) _pipe (fds, 4096, O_BINARY) |
120 | #define pipe(fds) _pipe (fds, 4096, O_BINARY) |
126 | |
121 | |
|
|
122 | #define fcntl(fd,cmd,arg) EIO_ENOSYS () |
|
|
123 | #define ioctl(fd,cmd,arg) EIO_ENOSYS () |
127 | #define fchmod(fd,mode) EIO_ENOSYS () |
124 | #define fchmod(fd,mode) EIO_ENOSYS () |
128 | #define chown(path,uid,gid) EIO_ENOSYS () |
125 | #define chown(path,uid,gid) EIO_ENOSYS () |
129 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
126 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
130 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
127 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
131 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
128 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
… | |
… | |
204 | /* we could even stat and see if it exists */ |
201 | /* we could even stat and see if it exists */ |
205 | static int |
202 | static int |
206 | symlink (const char *old, const char *neu) |
203 | symlink (const char *old, const char *neu) |
207 | { |
204 | { |
208 | #if WINVER >= 0x0600 |
205 | #if WINVER >= 0x0600 |
|
|
206 | int flags; |
|
|
207 | |
|
|
208 | /* This tries out all combinations of SYMBOLIC_LINK_FLAG_DIRECTORY |
|
|
209 | * and SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE, |
|
|
210 | * with directory first. |
|
|
211 | */ |
|
|
212 | for (flags = 3; flags >= 0; --flags) |
209 | if (CreateSymbolicLink (neu, old, 1)) |
213 | if (CreateSymbolicLink (neu, old, flags)) |
210 | return 0; |
214 | return 0; |
211 | |
|
|
212 | if (CreateSymbolicLink (neu, old, 0)) |
|
|
213 | return 0; |
|
|
214 | #endif |
215 | #endif |
215 | |
216 | |
216 | return EIO_ERRNO (ENOENT, -1); |
217 | return EIO_ERRNO (ENOENT, -1); |
217 | } |
218 | } |
218 | |
219 | |
… | |
… | |
230 | #define D_NAME(entp) entp.cFileName |
231 | #define D_NAME(entp) entp.cFileName |
231 | #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG) |
232 | #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG) |
232 | |
233 | |
233 | #else |
234 | #else |
234 | |
235 | |
|
|
236 | #include <sys/ioctl.h> |
235 | #include <sys/time.h> |
237 | #include <sys/time.h> |
236 | #include <sys/select.h> |
238 | #include <sys/select.h> |
237 | #include <unistd.h> |
239 | #include <unistd.h> |
238 | #include <signal.h> |
240 | #include <signal.h> |
239 | #include <dirent.h> |
241 | #include <dirent.h> |
… | |
… | |
281 | # include <utime.h> |
283 | # include <utime.h> |
282 | #endif |
284 | #endif |
283 | |
285 | |
284 | #if HAVE_SYS_SYSCALL_H |
286 | #if HAVE_SYS_SYSCALL_H |
285 | # include <sys/syscall.h> |
287 | # include <sys/syscall.h> |
286 | #endif |
|
|
287 | |
|
|
288 | #if HAVE_SYS_PRCTL_H |
|
|
289 | # include <sys/prctl.h> |
|
|
290 | #endif |
288 | #endif |
291 | |
289 | |
292 | #if HAVE_SENDFILE |
290 | #if HAVE_SENDFILE |
293 | # if __linux |
291 | # if __linux |
294 | # include <sys/sendfile.h> |
292 | # include <sys/sendfile.h> |
… | |
… | |
302 | # else |
300 | # else |
303 | # error sendfile support requested but not available |
301 | # error sendfile support requested but not available |
304 | # endif |
302 | # endif |
305 | #endif |
303 | #endif |
306 | |
304 | |
|
|
305 | #if HAVE_RENAMEAT2 |
|
|
306 | # include <sys/syscall.h> |
|
|
307 | # include <linux/fs.h> |
|
|
308 | #endif |
|
|
309 | |
307 | #ifndef D_TYPE |
310 | #ifndef D_TYPE |
308 | # define D_TYPE(de) 0 |
311 | # define D_TYPE(de) 0 |
309 | #endif |
312 | #endif |
310 | #ifndef D_INO |
313 | #ifndef D_INO |
311 | # define D_INO(de) 0 |
314 | # define D_INO(de) 0 |
… | |
… | |
319 | # define NAME_MAX 4096 |
322 | # define NAME_MAX 4096 |
320 | #endif |
323 | #endif |
321 | |
324 | |
322 | /* used for readlink etc. */ |
325 | /* used for readlink etc. */ |
323 | #ifndef PATH_MAX |
326 | #ifndef PATH_MAX |
324 | # define PATH_MAX 4096 |
327 | # define PATH_MAX 0 |
325 | #endif |
328 | #endif |
|
|
329 | |
|
|
330 | #ifndef O_CLOEXEC |
|
|
331 | #define O_CLOEXEC 0 |
|
|
332 | #endif |
|
|
333 | |
|
|
334 | #ifndef O_NONBLOCK |
|
|
335 | #define O_NONBLOCK 0 |
|
|
336 | #endif |
|
|
337 | |
|
|
338 | #ifndef O_SEARCH |
|
|
339 | #define O_SEARCH O_RDONLY |
|
|
340 | #endif |
|
|
341 | |
|
|
342 | #ifndef O_DIRECTORY |
|
|
343 | #define O_DIRECTORY 0 |
|
|
344 | #endif |
|
|
345 | |
|
|
346 | #ifndef EIO_PATH_MIN |
|
|
347 | # define EIO_PATH_MIN 8160 |
|
|
348 | #endif |
|
|
349 | |
|
|
350 | #if defined(O_PATH) |
|
|
351 | # define EIO_O_PATH O_PATH |
|
|
352 | #elif defined(O_SEARCH) |
|
|
353 | # define EIO_O_PATH O_SEARCH |
|
|
354 | #else |
|
|
355 | # define EIO_O_PATH 0 |
|
|
356 | #endif |
|
|
357 | |
|
|
358 | #define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX) |
326 | |
359 | |
327 | /* buffer size for various temporary buffers */ |
360 | /* buffer size for various temporary buffers */ |
328 | #define EIO_BUFSIZE 65536 |
361 | #define EIO_BUFSIZE 65536 |
329 | |
362 | |
330 | #define dBUF \ |
363 | #define dBUF \ |
… | |
… | |
341 | struct etp_tmpbuf; |
374 | struct etp_tmpbuf; |
342 | |
375 | |
343 | #if _POSIX_VERSION >= 200809L |
376 | #if _POSIX_VERSION >= 200809L |
344 | #define HAVE_AT 1 |
377 | #define HAVE_AT 1 |
345 | #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) |
378 | #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) |
346 | #ifndef O_SEARCH |
|
|
347 | #define O_SEARCH O_RDONLY |
|
|
348 | #endif |
|
|
349 | #else |
379 | #else |
350 | #define HAVE_AT 0 |
380 | #define HAVE_AT 0 |
351 | static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path); |
381 | static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path); |
352 | #endif |
382 | #endif |
353 | |
383 | |
… | |
… | |
543 | return etp_poll (EIO_POOL); |
573 | return etp_poll (EIO_POOL); |
544 | } |
574 | } |
545 | |
575 | |
546 | /*****************************************************************************/ |
576 | /*****************************************************************************/ |
547 | /* work around various missing functions */ |
577 | /* work around various missing functions */ |
|
|
578 | |
|
|
579 | #if HAVE_POSIX_CLOSE && !__linux |
|
|
580 | # define eio__close(fd) posix_close (fd, 0) |
|
|
581 | #else |
|
|
582 | # define eio__close(fd) close (fd) |
|
|
583 | #endif |
|
|
584 | |
|
|
585 | /* close() without disturbing errno */ |
|
|
586 | static void |
|
|
587 | silent_close (int fd) |
|
|
588 | { |
|
|
589 | int saved_errno = errno; |
|
|
590 | eio__close (fd); |
|
|
591 | errno = saved_errno; |
|
|
592 | } |
548 | |
593 | |
549 | #ifndef HAVE_UTIMES |
594 | #ifndef HAVE_UTIMES |
550 | |
595 | |
551 | # undef utimes |
596 | # undef utimes |
552 | # define utimes(path,times) eio__utimes (path, times) |
597 | # define utimes(path,times) eio__utimes (path, times) |
… | |
… | |
865 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
910 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
866 | extern int mallopt (int, int); |
911 | extern int mallopt (int, int); |
867 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
912 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
868 | #endif |
913 | #endif |
869 | |
914 | |
|
|
915 | #ifndef MCL_ONFAULT |
|
|
916 | if (flags & EIO_MCL_ONFAULT) |
|
|
917 | return EIO_ERRNO (EINVAL, -1); |
|
|
918 | #define MCL_ONFAULT 4 |
|
|
919 | #endif |
|
|
920 | |
870 | if (EIO_MCL_CURRENT != MCL_CURRENT |
921 | if (EIO_MCL_CURRENT != MCL_CURRENT |
871 | || EIO_MCL_FUTURE != MCL_FUTURE) |
922 | || EIO_MCL_FUTURE != MCL_FUTURE |
|
|
923 | || EIO_MCL_ONFAULT != MCL_ONFAULT) |
872 | { |
924 | { |
873 | flags = 0 |
925 | flags = 0 |
874 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
926 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
875 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); |
927 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0) |
|
|
928 | | (flags & EIO_MCL_ONFAULT ? MCL_ONFAULT : 0) |
|
|
929 | ; |
876 | } |
930 | } |
877 | |
931 | |
878 | return mlockall (flags); |
932 | return mlockall (flags); |
879 | } |
933 | } |
880 | #endif |
934 | #endif |
… | |
… | |
931 | intptr_t end = addr + len; |
985 | intptr_t end = addr + len; |
932 | intptr_t page = eio_pagesize (); |
986 | intptr_t page = eio_pagesize (); |
933 | |
987 | |
934 | if (addr < end) |
988 | if (addr < end) |
935 | if (flags & EIO_MT_MODIFY) /* modify */ |
989 | if (flags & EIO_MT_MODIFY) /* modify */ |
936 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
990 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < end && !EIO_CANCELLED (req)); |
937 | else |
991 | else |
938 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
992 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < end && !EIO_CANCELLED (req)); |
939 | } |
993 | } |
940 | |
994 | |
941 | return 0; |
995 | return 0; |
942 | } |
996 | } |
943 | |
997 | |
… | |
… | |
977 | |
1031 | |
978 | errno = ENOENT; |
1032 | errno = ENOENT; |
979 | if (!*rel) |
1033 | if (!*rel) |
980 | return -1; |
1034 | return -1; |
981 | |
1035 | |
982 | res = etp_tmpbuf_get (tmpbuf, PATH_MAX * 3); |
1036 | res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3); |
983 | #ifdef _WIN32 |
1037 | #ifdef _WIN32 |
984 | if (_access (rel, 4) != 0) |
1038 | if (_access (rel, 4) != 0) |
985 | return -1; |
1039 | return -1; |
986 | |
1040 | |
987 | symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0); |
1041 | symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0); |
988 | |
1042 | |
989 | errno = ENAMETOOLONG; |
1043 | errno = ENAMETOOLONG; |
990 | if (symlinks >= PATH_MAX * 3) |
1044 | if (symlinks >= EIO_PATH_MAX * 3) |
991 | return -1; |
1045 | return -1; |
992 | |
1046 | |
993 | errno = EIO; |
1047 | errno = EIO; |
994 | if (symlinks <= 0) |
1048 | if (symlinks <= 0) |
995 | return -1; |
1049 | return -1; |
996 | |
1050 | |
997 | return symlinks; |
1051 | return symlinks; |
998 | |
1052 | |
999 | #else |
1053 | #else |
1000 | tmp1 = res + PATH_MAX; |
1054 | tmp1 = res + EIO_PATH_MAX; |
1001 | tmp2 = tmp1 + PATH_MAX; |
1055 | tmp2 = tmp1 + EIO_PATH_MAX; |
1002 | |
1056 | |
1003 | #if 0 /* disabled, the musl way to do things is just too racy */ |
1057 | #if 0 /* disabled, the musl way to do things is just too racy */ |
1004 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
1058 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
1005 | /* on linux we may be able to ask the kernel */ |
1059 | /* on linux we may be able to ask the kernel */ |
1006 | { |
1060 | { |
1007 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
1061 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
1008 | |
1062 | |
1009 | if (fd >= 0) |
1063 | if (fd >= 0) |
1010 | { |
1064 | { |
1011 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
1065 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
1012 | req->result = readlink (tmp1, res, PATH_MAX); |
1066 | req->result = readlink (tmp1, res, EIO_PATH_MAX); |
1013 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
1067 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
1014 | close (fd); |
1068 | eio__close (fd); |
1015 | |
1069 | |
1016 | if (req->result > 0) |
1070 | if (req->result > 0) |
1017 | goto done; |
1071 | goto done; |
1018 | } |
1072 | } |
1019 | else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) |
1073 | else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) |
… | |
… | |
1030 | if (wd == EIO_INVALID_WD) |
1084 | if (wd == EIO_INVALID_WD) |
1031 | return -1; |
1085 | return -1; |
1032 | |
1086 | |
1033 | if (wd == EIO_CWD) |
1087 | if (wd == EIO_CWD) |
1034 | { |
1088 | { |
1035 | if (!getcwd (res, PATH_MAX)) |
1089 | if (!getcwd (res, EIO_PATH_MAX)) |
1036 | return -1; |
1090 | return -1; |
1037 | |
1091 | |
1038 | len = strlen (res); |
1092 | len = strlen (res); |
1039 | } |
1093 | } |
1040 | else |
1094 | else |
… | |
… | |
1087 | |
1141 | |
1088 | /* zero-terminate, for readlink */ |
1142 | /* zero-terminate, for readlink */ |
1089 | res [len + 1] = 0; |
1143 | res [len + 1] = 0; |
1090 | |
1144 | |
1091 | /* now check if it's a symlink */ |
1145 | /* now check if it's a symlink */ |
1092 | linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); |
1146 | linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX); |
1093 | |
1147 | |
1094 | if (linklen < 0) |
1148 | if (linklen < 0) |
1095 | { |
1149 | { |
1096 | if (errno != EINVAL) |
1150 | if (errno != EINVAL) |
1097 | return -1; |
1151 | return -1; |
… | |
… | |
1103 | { |
1157 | { |
1104 | /* yay, it was a symlink - build new path in tmp2 */ |
1158 | /* yay, it was a symlink - build new path in tmp2 */ |
1105 | int rellen = strlen (rel); |
1159 | int rellen = strlen (rel); |
1106 | |
1160 | |
1107 | errno = ENAMETOOLONG; |
1161 | errno = ENAMETOOLONG; |
1108 | if (linklen + 1 + rellen >= PATH_MAX) |
1162 | if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */ |
1109 | return -1; |
1163 | return -1; |
1110 | |
1164 | |
1111 | errno = ELOOP; |
1165 | errno = ELOOP; |
1112 | if (!--symlinks) |
1166 | if (!--symlinks) |
1113 | return -1; |
1167 | return -1; |
… | |
… | |
1367 | return; |
1421 | return; |
1368 | } |
1422 | } |
1369 | } |
1423 | } |
1370 | #else |
1424 | #else |
1371 | #if HAVE_AT |
1425 | #if HAVE_AT |
1372 | if (req->wd) |
|
|
1373 | { |
1426 | { |
1374 | int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY); |
1427 | int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_RDONLY | O_DIRECTORY | O_NONBLOCK); |
1375 | |
1428 | |
1376 | if (fd < 0) |
1429 | if (fd < 0) |
|
|
1430 | return; |
|
|
1431 | |
|
|
1432 | dirp = fdopendir (fd); |
|
|
1433 | |
|
|
1434 | if (!dirp) |
|
|
1435 | { |
|
|
1436 | silent_close (fd); |
1377 | return; |
1437 | return; |
1378 | |
|
|
1379 | dirp = fdopendir (fd); |
|
|
1380 | |
|
|
1381 | if (!dirp) |
|
|
1382 | close (fd); |
|
|
1383 | } |
1438 | } |
1384 | else |
1439 | } |
1385 | dirp = opendir (req->ptr1); |
|
|
1386 | #else |
1440 | #else |
1387 | dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); |
1441 | dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); |
|
|
1442 | |
|
|
1443 | if (!dirp) |
|
|
1444 | return; |
1388 | #endif |
1445 | #endif |
1389 | |
|
|
1390 | if (!dirp) |
|
|
1391 | return; |
|
|
1392 | #endif |
1446 | #endif |
1393 | |
1447 | |
1394 | if (req->flags & EIO_FLAG_PTR1_FREE) |
1448 | if (req->flags & EIO_FLAG_PTR1_FREE) |
1395 | free (req->ptr1); |
1449 | free (req->ptr1); |
1396 | |
1450 | |
… | |
… | |
1593 | /* keep the absolute path in string form at all times */ |
1647 | /* keep the absolute path in string form at all times */ |
1594 | /* fuck yeah. */ |
1648 | /* fuck yeah. */ |
1595 | |
1649 | |
1596 | #if !HAVE_AT |
1650 | #if !HAVE_AT |
1597 | |
1651 | |
1598 | /* a bit like realpath, but usually faster because it doesn'T have to return */ |
1652 | /* a bit like realpath, but usually faster because it doesn't have to return */ |
1599 | /* an absolute or canonical path */ |
1653 | /* an absolute or canonical path */ |
1600 | static const char * |
1654 | static const char * |
1601 | wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1655 | wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1602 | { |
1656 | { |
1603 | if (!wd || *path == '/') |
1657 | if (!wd || *path == '/') |
… | |
… | |
1631 | |
1685 | |
1632 | if (len < 0) |
1686 | if (len < 0) |
1633 | return EIO_INVALID_WD; |
1687 | return EIO_INVALID_WD; |
1634 | |
1688 | |
1635 | #if HAVE_AT |
1689 | #if HAVE_AT |
1636 | fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); |
1690 | fd = openat (WD2FD (wd), path, EIO_O_PATH | O_DIRECTORY | O_NONBLOCK | O_CLOEXEC); |
|
|
1691 | |
|
|
1692 | /* 0 is a valid fd, but we use it for EIO_CWD, so in the very unlikely */ |
|
|
1693 | /* case of fd 0 being available (almost certainly an a pplication bug) */ |
|
|
1694 | /* make sure we use another fd value */ |
|
|
1695 | #if EIO_CWD |
|
|
1696 | error EIO_CWD must be 0 |
|
|
1697 | #endif |
|
|
1698 | if (ecb_expect_false (fd == 0)) |
|
|
1699 | { |
|
|
1700 | int fd2 = fcntl (fd, F_DUPFD_CLOEXEC ? F_DUPFD_CLOEXEC : F_DUPFD); |
|
|
1701 | fcntl (fd2, F_SETFD, FD_CLOEXEC); |
|
|
1702 | eio__close (fd); |
|
|
1703 | fd = fd2; |
|
|
1704 | } |
1637 | |
1705 | |
1638 | if (fd < 0) |
1706 | if (fd < 0) |
1639 | return EIO_INVALID_WD; |
1707 | return EIO_INVALID_WD; |
1640 | #endif |
1708 | #endif |
1641 | |
1709 | |
… | |
… | |
1653 | } |
1721 | } |
1654 | |
1722 | |
1655 | eio_wd |
1723 | eio_wd |
1656 | eio_wd_open_sync (eio_wd wd, const char *path) |
1724 | eio_wd_open_sync (eio_wd wd, const char *path) |
1657 | { |
1725 | { |
1658 | struct etp_tmpbuf tmpbuf = { }; |
1726 | struct etp_tmpbuf tmpbuf = { 0 }; |
1659 | wd = eio__wd_open_sync (&tmpbuf, wd, path); |
1727 | wd = eio__wd_open_sync (&tmpbuf, wd, path); |
1660 | free (tmpbuf.ptr); |
1728 | free (tmpbuf.ptr); |
1661 | |
1729 | |
1662 | return wd; |
1730 | return wd; |
1663 | } |
1731 | } |
… | |
… | |
1666 | eio_wd_close_sync (eio_wd wd) |
1734 | eio_wd_close_sync (eio_wd wd) |
1667 | { |
1735 | { |
1668 | if (wd != EIO_INVALID_WD && wd != EIO_CWD) |
1736 | if (wd != EIO_INVALID_WD && wd != EIO_CWD) |
1669 | { |
1737 | { |
1670 | #if HAVE_AT |
1738 | #if HAVE_AT |
1671 | close (wd->fd); |
1739 | eio__close (wd->fd); |
1672 | #endif |
1740 | #endif |
1673 | free (wd); |
1741 | free (wd); |
1674 | } |
1742 | } |
1675 | } |
1743 | } |
1676 | |
1744 | |
1677 | #if HAVE_AT |
1745 | #if HAVE_AT |
1678 | |
1746 | |
|
|
1747 | static int |
|
|
1748 | eio__renameat2 (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags) |
|
|
1749 | { |
|
|
1750 | #if HAVE_RENAMEAT2 |
|
|
1751 | return syscall (SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags); |
|
|
1752 | #else |
|
|
1753 | if (flags) |
|
|
1754 | return EIO_ENOSYS (); |
|
|
1755 | |
|
|
1756 | return renameat (olddirfd, oldpath, newdirfd, newpath); |
|
|
1757 | #endif |
|
|
1758 | } |
|
|
1759 | |
1679 | /* they forgot these */ |
1760 | /* they forgot these */ |
1680 | |
1761 | |
1681 | static int |
1762 | static int |
1682 | eio__truncateat (int dirfd, const char *path, off_t length) |
1763 | eio__truncateat (int dirfd, const char *path, off_t length) |
1683 | { |
1764 | { |
1684 | int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); |
1765 | int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC | O_NONBLOCK); |
1685 | int res; |
1766 | int res; |
1686 | |
1767 | |
1687 | if (fd < 0) |
1768 | if (fd < 0) |
1688 | return fd; |
1769 | return fd; |
1689 | |
1770 | |
1690 | res = ftruncate (fd, length); |
1771 | res = ftruncate (fd, length); |
1691 | close (fd); |
1772 | silent_close (fd); |
1692 | return res; |
1773 | return res; |
1693 | } |
1774 | } |
1694 | |
1775 | |
1695 | static int |
1776 | static int |
1696 | eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) |
1777 | eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) |
1697 | { |
1778 | { |
1698 | int fd = openat (dirfd, path, O_SEARCH | O_CLOEXEC); |
1779 | int fd = openat (dirfd, path, EIO_O_PATH | O_CLOEXEC | O_NONBLOCK); |
1699 | int res; |
1780 | int res; |
1700 | |
1781 | |
1701 | if (fd < 0) |
1782 | if (fd < 0) |
1702 | return fd; |
1783 | return fd; |
1703 | |
1784 | |
1704 | res = fstatvfs (fd, buf); |
1785 | res = fstatvfs (fd, buf); |
1705 | close (fd); |
1786 | silent_close (fd); |
1706 | return res; |
1787 | return res; |
1707 | |
|
|
1708 | } |
1788 | } |
1709 | |
1789 | |
1710 | #endif |
1790 | #endif |
1711 | |
1791 | |
1712 | /*****************************************************************************/ |
1792 | /*****************************************************************************/ |
… | |
… | |
1720 | req->ptr2 = malloc (len); \ |
1800 | req->ptr2 = malloc (len); \ |
1721 | if (!req->ptr2) \ |
1801 | if (!req->ptr2) \ |
1722 | { \ |
1802 | { \ |
1723 | errno = ENOMEM; \ |
1803 | errno = ENOMEM; \ |
1724 | req->result = -1; \ |
1804 | req->result = -1; \ |
1725 | break; \ |
1805 | goto alloc_fail; \ |
1726 | } \ |
1806 | } \ |
1727 | } |
1807 | } |
1728 | |
1808 | |
1729 | /*****************************************************************************/ |
1809 | /*****************************************************************************/ |
|
|
1810 | |
|
|
1811 | static void |
|
|
1812 | eio__slurp (int fd, eio_req *req) |
|
|
1813 | { |
|
|
1814 | req->result = fd; |
|
|
1815 | |
|
|
1816 | if (fd < 0) |
|
|
1817 | return; |
|
|
1818 | |
|
|
1819 | if (req->offs < 0 || !req->size) /* do we need the size? */ |
|
|
1820 | { |
|
|
1821 | off_t size = lseek (fd, 0, SEEK_END); |
|
|
1822 | |
|
|
1823 | if (req->offs < 0) |
|
|
1824 | req->offs += size; |
|
|
1825 | |
|
|
1826 | if (!req->size) |
|
|
1827 | req->size = size - req->offs; |
|
|
1828 | } |
|
|
1829 | |
|
|
1830 | ALLOC (req->size); |
|
|
1831 | req->result = pread (fd, req->ptr2, req->size, req->offs); |
|
|
1832 | |
|
|
1833 | silent_close (fd); |
|
|
1834 | |
|
|
1835 | alloc_fail: |
|
|
1836 | ; |
|
|
1837 | } |
1730 | |
1838 | |
1731 | int ecb_cold |
1839 | int ecb_cold |
1732 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1840 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1733 | { |
1841 | { |
1734 | eio_want_poll_cb = want_poll; |
1842 | eio_want_poll_cb = want_poll; |
… | |
… | |
1816 | : read (req->int1, req->ptr2, req->size); break; |
1924 | : read (req->int1, req->ptr2, req->size); break; |
1817 | case EIO_WRITE: req->result = req->offs >= 0 |
1925 | case EIO_WRITE: req->result = req->offs >= 0 |
1818 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1926 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1819 | : write (req->int1, req->ptr2, req->size); break; |
1927 | : write (req->int1, req->ptr2, req->size); break; |
1820 | |
1928 | |
|
|
1929 | case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break; |
|
|
1930 | case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break; |
|
|
1931 | |
1821 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1932 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1822 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1933 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1823 | |
1934 | |
1824 | #if HAVE_AT |
1935 | #if HAVE_AT |
1825 | |
1936 | |
… | |
… | |
1829 | req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; |
1940 | req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; |
1830 | case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break; |
1941 | case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break; |
1831 | case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; |
1942 | case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; |
1832 | case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; |
1943 | case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; |
1833 | case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; |
1944 | case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; |
|
|
1945 | case EIO_SLURP: eio__slurp ( openat (dirfd, req->ptr1, O_RDONLY | O_CLOEXEC), req); break; |
1834 | |
1946 | |
1835 | case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; |
1947 | case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; |
1836 | case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */ |
1948 | case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */ |
1837 | req->result = req->wd && SINGLEDOT (req->ptr1) |
1949 | req->result = req->wd && SINGLEDOT (req->ptr1) |
1838 | ? rmdir (req->wd->str) |
1950 | ? rmdir (req->wd->str) |
1839 | : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; |
1951 | : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; |
1840 | case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; |
1952 | case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; |
|
|
1953 | case EIO_RENAME: req->result = eio__renameat2 ( |
|
|
1954 | dirfd, |
1841 | case EIO_RENAME: /* complications arise because "." cannot be renamed, so we might have to expand */ |
1955 | /* complications arise because "." cannot be renamed, so we might have to expand */ |
1842 | req->result = req->wd && SINGLEDOT (req->ptr1) |
1956 | req->wd && SINGLEDOT (req->ptr1) ? req->wd->str : req->ptr1, |
1843 | ? rename (req->wd->str, req->ptr2) |
1957 | WD2FD ((eio_wd)req->int3), |
1844 | : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; |
1958 | req->ptr2, |
|
|
1959 | req->int2 |
|
|
1960 | ); |
|
|
1961 | break; |
1845 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; |
1962 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; |
1846 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
1963 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
1847 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1964 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1848 | case EIO_READLINK: ALLOC (PATH_MAX); |
|
|
1849 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; |
|
|
1850 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1965 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1851 | req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; |
1966 | req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; |
|
|
1967 | case EIO_READLINK: ALLOC (EIO_PATH_MAX); |
|
|
1968 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX); |
|
|
1969 | if (req->result == EIO_PATH_MAX) |
|
|
1970 | { |
|
|
1971 | req->result = -1; |
|
|
1972 | errno = ENAMETOOLONG; |
|
|
1973 | } |
|
|
1974 | break; |
1852 | case EIO_UTIME: |
1975 | case EIO_UTIME: |
1853 | case EIO_FUTIME: |
1976 | case EIO_FUTIME: |
1854 | { |
1977 | { |
1855 | struct timespec ts[2]; |
1978 | struct timespec ts[2]; |
1856 | struct timespec *times; |
1979 | struct timespec *times; |
… | |
… | |
1881 | req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; |
2004 | req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; |
1882 | case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; |
2005 | case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; |
1883 | case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break; |
2006 | case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break; |
1884 | case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; |
2007 | case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; |
1885 | case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break; |
2008 | case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break; |
|
|
2009 | case EIO_SLURP: eio__slurp ( open (path , O_RDONLY | O_CLOEXEC), req); break; |
1886 | |
2010 | |
1887 | case EIO_UNLINK: req->result = unlink (path ); break; |
2011 | case EIO_UNLINK: req->result = unlink (path ); break; |
1888 | case EIO_RMDIR: req->result = rmdir (path ); break; |
2012 | case EIO_RMDIR: req->result = rmdir (path ); break; |
1889 | case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; |
2013 | case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; |
1890 | case EIO_RENAME: req->result = rename (path , req->ptr2); break; |
2014 | case EIO_RENAME: req->result = req->int2 ? EIO_ENOSYS () : rename (path, req->ptr2); break; |
1891 | case EIO_LINK: req->result = link (path , req->ptr2); break; |
2015 | case EIO_LINK: req->result = link (path , req->ptr2); break; |
1892 | case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; |
2016 | case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; |
1893 | case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; |
2017 | case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; |
1894 | case EIO_READLINK: ALLOC (PATH_MAX); |
|
|
1895 | req->result = readlink (path, req->ptr2, PATH_MAX); break; |
|
|
1896 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
2018 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1897 | req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; |
2019 | req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; |
|
|
2020 | case EIO_READLINK: ALLOC (EIO_PATH_MAX); |
|
|
2021 | req->result = readlink (path, req->ptr2, EIO_PATH_MAX); |
|
|
2022 | if (req->result == EIO_PATH_MAX) |
|
|
2023 | { |
|
|
2024 | req->result = -1; |
|
|
2025 | errno = ENAMETOOLONG; |
|
|
2026 | } |
|
|
2027 | break; |
1898 | |
2028 | |
1899 | case EIO_UTIME: |
2029 | case EIO_UTIME: |
1900 | case EIO_FUTIME: |
2030 | case EIO_FUTIME: |
1901 | { |
2031 | { |
1902 | struct timeval tv[2]; |
2032 | struct timeval tv[2]; |
… | |
… | |
1937 | |
2067 | |
1938 | case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
2068 | case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
1939 | case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; |
2069 | case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; |
1940 | case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
2070 | case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
1941 | |
2071 | |
1942 | case EIO_CLOSE: req->result = close (req->int1); break; |
2072 | case EIO_CLOSE: req->result = eio__close (req->int1); break; |
1943 | case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; |
2073 | case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; |
1944 | case EIO_SYNC: req->result = 0; sync (); break; |
2074 | case EIO_SYNC: req->result = 0; sync (); break; |
1945 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
2075 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
1946 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
2076 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
1947 | case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; |
2077 | case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; |
… | |
… | |
1985 | default: |
2115 | default: |
1986 | req->result = EIO_ENOSYS (); |
2116 | req->result = EIO_ENOSYS (); |
1987 | break; |
2117 | break; |
1988 | } |
2118 | } |
1989 | |
2119 | |
|
|
2120 | alloc_fail: |
1990 | req->errorno = errno; |
2121 | req->errorno = errno; |
1991 | } |
2122 | } |
1992 | |
2123 | |
1993 | #ifndef EIO_NO_WRAPPERS |
2124 | #ifndef EIO_NO_WRAPPERS |
1994 | |
2125 | |
… | |
… | |
2083 | } |
2214 | } |
2084 | |
2215 | |
2085 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2216 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2086 | { |
2217 | { |
2087 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2218 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
|
|
2219 | } |
|
|
2220 | |
|
|
2221 | eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data) |
|
|
2222 | { |
|
|
2223 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND; |
|
|
2224 | } |
|
|
2225 | |
|
|
2226 | eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data) |
|
|
2227 | { |
|
|
2228 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND; |
2088 | } |
2229 | } |
2089 | |
2230 | |
2090 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2231 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2091 | { |
2232 | { |
2092 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
2233 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
… | |
… | |
2237 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
2378 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
2238 | { |
2379 | { |
2239 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
2380 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
2240 | } |
2381 | } |
2241 | |
2382 | |
|
|
2383 | eio_req *eio_slurp (const char *path, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
|
|
2384 | { |
|
|
2385 | REQ (EIO_SLURP); PATH; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
|
|
2386 | } |
|
|
2387 | |
2242 | eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) |
2388 | eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) |
2243 | { |
2389 | { |
2244 | REQ (EIO_CUSTOM); req->feed = execute; SEND; |
2390 | REQ (EIO_CUSTOM); req->feed = execute; SEND; |
2245 | } |
2391 | } |
2246 | |
2392 | |
… | |
… | |
2303 | eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
2449 | eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
2304 | { |
2450 | { |
2305 | return eio__sendfile (ofd, ifd, offset, count); |
2451 | return eio__sendfile (ofd, ifd, offset, count); |
2306 | } |
2452 | } |
2307 | |
2453 | |
|
|
2454 | int eio_mlockall_sync (int flags) |
|
|
2455 | { |
|
|
2456 | return eio__mlockall (flags); |
|
|
2457 | } |
|
|
2458 | |