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 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 | |
… | |
… | |
281 | # include <utime.h> |
282 | # include <utime.h> |
282 | #endif |
283 | #endif |
283 | |
284 | |
284 | #if HAVE_SYS_SYSCALL_H |
285 | #if HAVE_SYS_SYSCALL_H |
285 | # include <sys/syscall.h> |
286 | # include <sys/syscall.h> |
286 | #endif |
|
|
287 | |
|
|
288 | #if HAVE_SYS_PRCTL_H |
|
|
289 | # include <sys/prctl.h> |
|
|
290 | #endif |
287 | #endif |
291 | |
288 | |
292 | #if HAVE_SENDFILE |
289 | #if HAVE_SENDFILE |
293 | # if __linux |
290 | # if __linux |
294 | # include <sys/sendfile.h> |
291 | # include <sys/sendfile.h> |
… | |
… | |
319 | # define NAME_MAX 4096 |
316 | # define NAME_MAX 4096 |
320 | #endif |
317 | #endif |
321 | |
318 | |
322 | /* used for readlink etc. */ |
319 | /* used for readlink etc. */ |
323 | #ifndef PATH_MAX |
320 | #ifndef PATH_MAX |
324 | # define PATH_MAX 4096 |
321 | # define PATH_MAX 0 |
325 | #endif |
322 | #endif |
|
|
323 | |
|
|
324 | #ifndef EIO_PATH_MIN |
|
|
325 | # define EIO_PATH_MIN 8160 |
|
|
326 | #endif |
|
|
327 | |
|
|
328 | #define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX) |
326 | |
329 | |
327 | /* buffer size for various temporary buffers */ |
330 | /* buffer size for various temporary buffers */ |
328 | #define EIO_BUFSIZE 65536 |
331 | #define EIO_BUFSIZE 65536 |
329 | |
332 | |
330 | #define dBUF \ |
333 | #define dBUF \ |
… | |
… | |
366 | #define ETP_PRI_MAX EIO_PRI_MAX |
369 | #define ETP_PRI_MAX EIO_PRI_MAX |
367 | |
370 | |
368 | #define ETP_TYPE_QUIT -1 |
371 | #define ETP_TYPE_QUIT -1 |
369 | #define ETP_TYPE_GROUP EIO_GROUP |
372 | #define ETP_TYPE_GROUP EIO_GROUP |
370 | |
373 | |
|
|
374 | static void eio_nop_callback (void) { } |
|
|
375 | static void (*eio_want_poll_cb)(void) = eio_nop_callback; |
|
|
376 | static void (*eio_done_poll_cb)(void) = eio_nop_callback; |
|
|
377 | |
|
|
378 | #define ETP_WANT_POLL(pool) eio_want_poll_cb () |
|
|
379 | #define ETP_DONE_POLL(pool) eio_done_poll_cb () |
|
|
380 | |
371 | struct etp_worker; |
381 | struct etp_worker; |
372 | |
|
|
373 | #define ETP_REQ eio_req |
382 | #define ETP_REQ eio_req |
374 | #define ETP_DESTROY(req) eio_destroy (req) |
383 | #define ETP_DESTROY(req) eio_destroy (req) |
375 | static int eio_finish (eio_req *req); |
384 | static int eio_finish (eio_req *req); |
376 | #define ETP_FINISH(req) eio_finish (req) |
385 | #define ETP_FINISH(req) eio_finish (req) |
377 | static void eio_execute (struct etp_worker *self, eio_req *req); |
386 | static void eio_execute (struct etp_worker *self, eio_req *req); |
… | |
… | |
971 | |
980 | |
972 | errno = ENOENT; |
981 | errno = ENOENT; |
973 | if (!*rel) |
982 | if (!*rel) |
974 | return -1; |
983 | return -1; |
975 | |
984 | |
976 | res = etp_tmpbuf_get (tmpbuf, PATH_MAX * 3); |
985 | res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3); |
977 | #ifdef _WIN32 |
986 | #ifdef _WIN32 |
978 | if (_access (rel, 4) != 0) |
987 | if (_access (rel, 4) != 0) |
979 | return -1; |
988 | return -1; |
980 | |
989 | |
981 | symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0); |
990 | symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0); |
982 | |
991 | |
983 | errno = ENAMETOOLONG; |
992 | errno = ENAMETOOLONG; |
984 | if (symlinks >= PATH_MAX * 3) |
993 | if (symlinks >= EIO_PATH_MAX * 3) |
985 | return -1; |
994 | return -1; |
986 | |
995 | |
987 | errno = EIO; |
996 | errno = EIO; |
988 | if (symlinks <= 0) |
997 | if (symlinks <= 0) |
989 | return -1; |
998 | return -1; |
990 | |
999 | |
991 | return symlinks; |
1000 | return symlinks; |
992 | |
1001 | |
993 | #else |
1002 | #else |
994 | tmp1 = res + PATH_MAX; |
1003 | tmp1 = res + EIO_PATH_MAX; |
995 | tmp2 = tmp1 + PATH_MAX; |
1004 | tmp2 = tmp1 + EIO_PATH_MAX; |
996 | |
1005 | |
997 | #if 0 /* disabled, the musl way to do things is just too racy */ |
1006 | #if 0 /* disabled, the musl way to do things is just too racy */ |
998 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
1007 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
999 | /* on linux we may be able to ask the kernel */ |
1008 | /* on linux we may be able to ask the kernel */ |
1000 | { |
1009 | { |
1001 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
1010 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
1002 | |
1011 | |
1003 | if (fd >= 0) |
1012 | if (fd >= 0) |
1004 | { |
1013 | { |
1005 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
1014 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
1006 | req->result = readlink (tmp1, res, PATH_MAX); |
1015 | req->result = readlink (tmp1, res, EIO_PATH_MAX); |
1007 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
1016 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
1008 | close (fd); |
1017 | close (fd); |
1009 | |
1018 | |
1010 | if (req->result > 0) |
1019 | if (req->result > 0) |
1011 | goto done; |
1020 | goto done; |
… | |
… | |
1024 | if (wd == EIO_INVALID_WD) |
1033 | if (wd == EIO_INVALID_WD) |
1025 | return -1; |
1034 | return -1; |
1026 | |
1035 | |
1027 | if (wd == EIO_CWD) |
1036 | if (wd == EIO_CWD) |
1028 | { |
1037 | { |
1029 | if (!getcwd (res, PATH_MAX)) |
1038 | if (!getcwd (res, EIO_PATH_MAX)) |
1030 | return -1; |
1039 | return -1; |
1031 | |
1040 | |
1032 | len = strlen (res); |
1041 | len = strlen (res); |
1033 | } |
1042 | } |
1034 | else |
1043 | else |
… | |
… | |
1081 | |
1090 | |
1082 | /* zero-terminate, for readlink */ |
1091 | /* zero-terminate, for readlink */ |
1083 | res [len + 1] = 0; |
1092 | res [len + 1] = 0; |
1084 | |
1093 | |
1085 | /* now check if it's a symlink */ |
1094 | /* now check if it's a symlink */ |
1086 | linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); |
1095 | linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX); |
1087 | |
1096 | |
1088 | if (linklen < 0) |
1097 | if (linklen < 0) |
1089 | { |
1098 | { |
1090 | if (errno != EINVAL) |
1099 | if (errno != EINVAL) |
1091 | return -1; |
1100 | return -1; |
… | |
… | |
1097 | { |
1106 | { |
1098 | /* yay, it was a symlink - build new path in tmp2 */ |
1107 | /* yay, it was a symlink - build new path in tmp2 */ |
1099 | int rellen = strlen (rel); |
1108 | int rellen = strlen (rel); |
1100 | |
1109 | |
1101 | errno = ENAMETOOLONG; |
1110 | errno = ENAMETOOLONG; |
1102 | if (linklen + 1 + rellen >= PATH_MAX) |
1111 | if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */ |
1103 | return -1; |
1112 | return -1; |
1104 | |
1113 | |
1105 | errno = ELOOP; |
1114 | errno = ELOOP; |
1106 | if (!--symlinks) |
1115 | if (!--symlinks) |
1107 | return -1; |
1116 | return -1; |
… | |
… | |
1706 | /*****************************************************************************/ |
1715 | /*****************************************************************************/ |
1707 | |
1716 | |
1708 | #define ALLOC(len) \ |
1717 | #define ALLOC(len) \ |
1709 | if (!req->ptr2) \ |
1718 | if (!req->ptr2) \ |
1710 | { \ |
1719 | { \ |
1711 | X_LOCK (wrklock); \ |
1720 | X_LOCK (EIO_POOL->wrklock); \ |
1712 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
1721 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
1713 | X_UNLOCK (wrklock); \ |
1722 | X_UNLOCK (EIO_POOL->wrklock); \ |
1714 | req->ptr2 = malloc (len); \ |
1723 | req->ptr2 = malloc (len); \ |
1715 | if (!req->ptr2) \ |
1724 | if (!req->ptr2) \ |
1716 | { \ |
1725 | { \ |
1717 | errno = ENOMEM; \ |
1726 | errno = ENOMEM; \ |
1718 | req->result = -1; \ |
1727 | req->result = -1; \ |
… | |
… | |
1723 | /*****************************************************************************/ |
1732 | /*****************************************************************************/ |
1724 | |
1733 | |
1725 | int ecb_cold |
1734 | int ecb_cold |
1726 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1735 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1727 | { |
1736 | { |
1728 | return etp_init (EIO_POOL, want_poll, done_poll); |
1737 | eio_want_poll_cb = want_poll; |
|
|
1738 | eio_done_poll_cb = done_poll; |
|
|
1739 | |
|
|
1740 | return etp_init (EIO_POOL, 0, 0, 0); |
1729 | } |
1741 | } |
1730 | |
1742 | |
1731 | ecb_inline void |
1743 | ecb_inline void |
1732 | eio_api_destroy (eio_req *req) |
1744 | eio_api_destroy (eio_req *req) |
1733 | { |
1745 | { |
… | |
… | |
1806 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1818 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1807 | : read (req->int1, req->ptr2, req->size); break; |
1819 | : read (req->int1, req->ptr2, req->size); break; |
1808 | case EIO_WRITE: req->result = req->offs >= 0 |
1820 | case EIO_WRITE: req->result = req->offs >= 0 |
1809 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1821 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1810 | : write (req->int1, req->ptr2, req->size); break; |
1822 | : write (req->int1, req->ptr2, req->size); break; |
|
|
1823 | |
|
|
1824 | case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break; |
|
|
1825 | case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break; |
1811 | |
1826 | |
1812 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1827 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1813 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1828 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1814 | |
1829 | |
1815 | #if HAVE_AT |
1830 | #if HAVE_AT |
… | |
… | |
1834 | ? rename (req->wd->str, req->ptr2) |
1849 | ? rename (req->wd->str, req->ptr2) |
1835 | : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; |
1850 | : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; |
1836 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; |
1851 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; |
1837 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
1852 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
1838 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1853 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1839 | case EIO_READLINK: ALLOC (PATH_MAX); |
|
|
1840 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; |
|
|
1841 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1854 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1842 | req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; |
1855 | req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; |
|
|
1856 | case EIO_READLINK: ALLOC (EIO_PATH_MAX); |
|
|
1857 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX); |
|
|
1858 | if (req->result == EIO_PATH_MAX) |
|
|
1859 | { |
|
|
1860 | req->result = -1; |
|
|
1861 | errno = ENAMETOOLONG; |
|
|
1862 | } |
|
|
1863 | break; |
1843 | case EIO_UTIME: |
1864 | case EIO_UTIME: |
1844 | case EIO_FUTIME: |
1865 | case EIO_FUTIME: |
1845 | { |
1866 | { |
1846 | struct timespec ts[2]; |
1867 | struct timespec ts[2]; |
1847 | struct timespec *times; |
1868 | struct timespec *times; |
… | |
… | |
1880 | case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; |
1901 | case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; |
1881 | case EIO_RENAME: req->result = rename (path , req->ptr2); break; |
1902 | case EIO_RENAME: req->result = rename (path , req->ptr2); break; |
1882 | case EIO_LINK: req->result = link (path , req->ptr2); break; |
1903 | case EIO_LINK: req->result = link (path , req->ptr2); break; |
1883 | case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; |
1904 | case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; |
1884 | case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; |
1905 | case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; |
1885 | case EIO_READLINK: ALLOC (PATH_MAX); |
|
|
1886 | req->result = readlink (path, req->ptr2, PATH_MAX); break; |
|
|
1887 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1906 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1888 | req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; |
1907 | req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; |
|
|
1908 | case EIO_READLINK: ALLOC (EIO_PATH_MAX); |
|
|
1909 | req->result = readlink (path, req->ptr2, EIO_PATH_MAX); |
|
|
1910 | if (req->result == EIO_PATH_MAX) |
|
|
1911 | { |
|
|
1912 | req->result = -1; |
|
|
1913 | errno = ENAMETOOLONG; |
|
|
1914 | } |
|
|
1915 | break; |
1889 | |
1916 | |
1890 | case EIO_UTIME: |
1917 | case EIO_UTIME: |
1891 | case EIO_FUTIME: |
1918 | case EIO_FUTIME: |
1892 | { |
1919 | { |
1893 | struct timeval tv[2]; |
1920 | struct timeval tv[2]; |
… | |
… | |
2076 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2103 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2077 | { |
2104 | { |
2078 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2105 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2079 | } |
2106 | } |
2080 | |
2107 | |
|
|
2108 | eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data) |
|
|
2109 | { |
|
|
2110 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND; |
|
|
2111 | } |
|
|
2112 | |
|
|
2113 | eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data) |
|
|
2114 | { |
|
|
2115 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND; |
|
|
2116 | } |
|
|
2117 | |
2081 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2118 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2082 | { |
2119 | { |
2083 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
2120 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
2084 | } |
2121 | } |
2085 | |
2122 | |