ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
(Generate patch)

Comparing libeio/eio.c (file contents):
Revision 1.139 by root, Thu Jun 25 18:14:19 2015 UTC vs.
Revision 1.145 by root, Fri Jun 23 03:10:19 2017 UTC

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
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 EIO_PATH_MIN
331# define EIO_PATH_MIN 8160
332#endif
333
334#define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX)
326 335
327/* buffer size for various temporary buffers */ 336/* buffer size for various temporary buffers */
328#define EIO_BUFSIZE 65536 337#define EIO_BUFSIZE 65536
329 338
330#define dBUF \ 339#define dBUF \
977 986
978 errno = ENOENT; 987 errno = ENOENT;
979 if (!*rel) 988 if (!*rel)
980 return -1; 989 return -1;
981 990
982 res = etp_tmpbuf_get (tmpbuf, PATH_MAX * 3); 991 res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3);
983#ifdef _WIN32 992#ifdef _WIN32
984 if (_access (rel, 4) != 0) 993 if (_access (rel, 4) != 0)
985 return -1; 994 return -1;
986 995
987 symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0); 996 symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0);
988 997
989 errno = ENAMETOOLONG; 998 errno = ENAMETOOLONG;
990 if (symlinks >= PATH_MAX * 3) 999 if (symlinks >= EIO_PATH_MAX * 3)
991 return -1; 1000 return -1;
992 1001
993 errno = EIO; 1002 errno = EIO;
994 if (symlinks <= 0) 1003 if (symlinks <= 0)
995 return -1; 1004 return -1;
996 1005
997 return symlinks; 1006 return symlinks;
998 1007
999#else 1008#else
1000 tmp1 = res + PATH_MAX; 1009 tmp1 = res + EIO_PATH_MAX;
1001 tmp2 = tmp1 + PATH_MAX; 1010 tmp2 = tmp1 + EIO_PATH_MAX;
1002 1011
1003#if 0 /* disabled, the musl way to do things is just too racy */ 1012#if 0 /* disabled, the musl way to do things is just too racy */
1004#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1013#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1005 /* on linux we may be able to ask the kernel */ 1014 /* on linux we may be able to ask the kernel */
1006 { 1015 {
1007 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); 1016 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1008 1017
1009 if (fd >= 0) 1018 if (fd >= 0)
1010 { 1019 {
1011 sprintf (tmp1, "/proc/self/fd/%d", fd); 1020 sprintf (tmp1, "/proc/self/fd/%d", fd);
1012 req->result = readlink (tmp1, res, PATH_MAX); 1021 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 */ 1022 /* here we should probably stat the open file and the disk file, to make sure they still match */
1014 close (fd); 1023 close (fd);
1015 1024
1016 if (req->result > 0) 1025 if (req->result > 0)
1017 goto done; 1026 goto done;
1030 if (wd == EIO_INVALID_WD) 1039 if (wd == EIO_INVALID_WD)
1031 return -1; 1040 return -1;
1032 1041
1033 if (wd == EIO_CWD) 1042 if (wd == EIO_CWD)
1034 { 1043 {
1035 if (!getcwd (res, PATH_MAX)) 1044 if (!getcwd (res, EIO_PATH_MAX))
1036 return -1; 1045 return -1;
1037 1046
1038 len = strlen (res); 1047 len = strlen (res);
1039 } 1048 }
1040 else 1049 else
1087 1096
1088 /* zero-terminate, for readlink */ 1097 /* zero-terminate, for readlink */
1089 res [len + 1] = 0; 1098 res [len + 1] = 0;
1090 1099
1091 /* now check if it's a symlink */ 1100 /* now check if it's a symlink */
1092 linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); 1101 linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX);
1093 1102
1094 if (linklen < 0) 1103 if (linklen < 0)
1095 { 1104 {
1096 if (errno != EINVAL) 1105 if (errno != EINVAL)
1097 return -1; 1106 return -1;
1103 { 1112 {
1104 /* yay, it was a symlink - build new path in tmp2 */ 1113 /* yay, it was a symlink - build new path in tmp2 */
1105 int rellen = strlen (rel); 1114 int rellen = strlen (rel);
1106 1115
1107 errno = ENAMETOOLONG; 1116 errno = ENAMETOOLONG;
1108 if (linklen + 1 + rellen >= PATH_MAX) 1117 if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */
1109 return -1; 1118 return -1;
1110 1119
1111 errno = ELOOP; 1120 errno = ELOOP;
1112 if (!--symlinks) 1121 if (!--symlinks)
1113 return -1; 1122 return -1;
1674 } 1683 }
1675} 1684}
1676 1685
1677#if HAVE_AT 1686#if HAVE_AT
1678 1687
1688static int
1689eio__renameat2 (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags)
1690{
1691#if HAVE_RENAMEAT2
1692 return syscall (SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags);
1693#else
1694 if (flags)
1695 return EIO_ENOSYS ();
1696
1697 return renameat (olddirfd, oldpath, newdirfd, newpath);
1698#endif
1699}
1700
1679/* they forgot these */ 1701/* they forgot these */
1680 1702
1681static int 1703static int
1682eio__truncateat (int dirfd, const char *path, off_t length) 1704eio__truncateat (int dirfd, const char *path, off_t length)
1683{ 1705{
1816 : read (req->int1, req->ptr2, req->size); break; 1838 : read (req->int1, req->ptr2, req->size); break;
1817 case EIO_WRITE: req->result = req->offs >= 0 1839 case EIO_WRITE: req->result = req->offs >= 0
1818 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 1840 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
1819 : write (req->int1, req->ptr2, req->size); break; 1841 : write (req->int1, req->ptr2, req->size); break;
1820 1842
1843 case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break;
1844 case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break;
1845
1821 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 1846 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; 1847 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
1823 1848
1824#if HAVE_AT 1849#if HAVE_AT
1825 1850
1836 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */ 1861 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
1837 req->result = req->wd && SINGLEDOT (req->ptr1) 1862 req->result = req->wd && SINGLEDOT (req->ptr1)
1838 ? rmdir (req->wd->str) 1863 ? rmdir (req->wd->str)
1839 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 1864 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
1840 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 1865 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
1866 case EIO_RENAME: req->result = eio__renameat2 (
1867 dirfd,
1841 case EIO_RENAME: /* complications arise because "." cannot be renamed, so we might have to expand */ 1868 /* complications arise because "." cannot be renamed, so we might have to expand */
1842 req->result = req->wd && SINGLEDOT (req->ptr1) 1869 req->wd && SINGLEDOT (req->ptr1) ? req->wd->str : req->ptr1,
1843 ? rename (req->wd->str, req->ptr2) 1870 WD2FD ((eio_wd)req->int3),
1844 : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; 1871 req->ptr2,
1872 req->int2
1873 );
1874 break;
1845 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; 1875 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; 1876 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; 1877 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)); 1878 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
1851 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; 1879 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break;
1880 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
1881 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX);
1882 if (req->result == EIO_PATH_MAX)
1883 {
1884 req->result = -1;
1885 errno = ENAMETOOLONG;
1886 }
1887 break;
1852 case EIO_UTIME: 1888 case EIO_UTIME:
1853 case EIO_FUTIME: 1889 case EIO_FUTIME:
1854 { 1890 {
1855 struct timespec ts[2]; 1891 struct timespec ts[2];
1856 struct timespec *times; 1892 struct timespec *times;
1885 case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break; 1921 case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break;
1886 1922
1887 case EIO_UNLINK: req->result = unlink (path ); break; 1923 case EIO_UNLINK: req->result = unlink (path ); break;
1888 case EIO_RMDIR: req->result = rmdir (path ); break; 1924 case EIO_RMDIR: req->result = rmdir (path ); break;
1889 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; 1925 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break;
1890 case EIO_RENAME: req->result = rename (path , req->ptr2); break; 1926 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; 1927 case EIO_LINK: req->result = link (path , req->ptr2); break;
1892 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; 1928 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; 1929 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)); 1930 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
1897 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; 1931 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break;
1932 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
1933 req->result = readlink (path, req->ptr2, EIO_PATH_MAX);
1934 if (req->result == EIO_PATH_MAX)
1935 {
1936 req->result = -1;
1937 errno = ENAMETOOLONG;
1938 }
1939 break;
1898 1940
1899 case EIO_UTIME: 1941 case EIO_UTIME:
1900 case EIO_FUTIME: 1942 case EIO_FUTIME:
1901 { 1943 {
1902 struct timeval tv[2]; 1944 struct timeval tv[2];
2085eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2127eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2086{ 2128{
2087 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2129 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2088} 2130}
2089 2131
2132eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data)
2133{
2134 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND;
2135}
2136
2137eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data)
2138{
2139 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND;
2140}
2141
2090eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) 2142eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
2091{ 2143{
2092 REQ (EIO_FSTAT); req->int1 = fd; SEND; 2144 REQ (EIO_FSTAT); req->int1 = fd; SEND;
2093} 2145}
2094 2146

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines