1 | /* |
1 | /* |
2 | * libeio implementation |
2 | * libeio implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libeio@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011 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 | * |
… | |
… | |
79 | # include <unistd.h> |
79 | # include <unistd.h> |
80 | # include <utime.h> |
80 | # include <utime.h> |
81 | # include <signal.h> |
81 | # include <signal.h> |
82 | # include <dirent.h> |
82 | # include <dirent.h> |
83 | |
83 | |
84 | #if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES |
84 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
85 | # include <sys/mman.h> |
85 | # include <sys/mman.h> |
86 | #endif |
86 | #endif |
87 | |
87 | |
88 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
88 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
89 | # if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
89 | # if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
… | |
… | |
816 | |
816 | |
817 | return res; |
817 | return res; |
818 | } |
818 | } |
819 | #endif |
819 | #endif |
820 | |
820 | |
821 | #ifndef HAVE_FUTIMES |
821 | #ifndef HAVE_UTIMES |
822 | |
822 | |
823 | # undef utimes |
823 | # undef utimes |
824 | # undef futimes |
|
|
825 | # define utimes(path,times) eio__utimes (path, times) |
824 | # define utimes(path,times) eio__utimes (path, times) |
826 | # define futimes(fd,times) eio__futimes (fd, times) |
|
|
827 | |
825 | |
828 | static int |
826 | static int |
829 | eio__utimes (const char *filename, const struct timeval times[2]) |
827 | eio__utimes (const char *filename, const struct timeval times[2]) |
830 | { |
828 | { |
831 | if (times) |
829 | if (times) |
… | |
… | |
838 | return utime (filename, &buf); |
836 | return utime (filename, &buf); |
839 | } |
837 | } |
840 | else |
838 | else |
841 | return utime (filename, 0); |
839 | return utime (filename, 0); |
842 | } |
840 | } |
|
|
841 | |
|
|
842 | #endif |
|
|
843 | |
|
|
844 | #ifndef HAVE_FUTIMES |
|
|
845 | |
|
|
846 | # undef futimes |
|
|
847 | # define futimes(fd,times) eio__futimes (fd, times) |
843 | |
848 | |
844 | static int eio__futimes (int fd, const struct timeval tv[2]) |
849 | static int eio__futimes (int fd, const struct timeval tv[2]) |
845 | { |
850 | { |
846 | errno = ENOSYS; |
851 | errno = ENOSYS; |
847 | return -1; |
852 | return -1; |
… | |
… | |
1426 | /* round up length */ |
1431 | /* round up length */ |
1427 | *length = (*length + mask) & ~mask; |
1432 | *length = (*length + mask) & ~mask; |
1428 | } |
1433 | } |
1429 | |
1434 | |
1430 | #if !_POSIX_MEMLOCK |
1435 | #if !_POSIX_MEMLOCK |
1431 | # define eio__mlock(a,b) ((errno = ENOSYS), -1) |
|
|
1432 | # define eio__mlockall(a) ((errno = ENOSYS), -1) |
1436 | # define eio__mlockall(a) ((errno = ENOSYS), -1) |
1433 | #else |
1437 | #else |
1434 | |
|
|
1435 | static int |
|
|
1436 | eio__mlock (void *addr, size_t length) |
|
|
1437 | { |
|
|
1438 | eio_page_align (&addr, &length); |
|
|
1439 | |
|
|
1440 | mlock (addr, length); |
|
|
1441 | } |
|
|
1442 | |
1438 | |
1443 | static int |
1439 | static int |
1444 | eio__mlockall (int flags) |
1440 | eio__mlockall (int flags) |
1445 | { |
1441 | { |
1446 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
1442 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
… | |
… | |
1454 | flags = 0 |
1450 | flags = 0 |
1455 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
1451 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
1456 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); |
1452 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); |
1457 | } |
1453 | } |
1458 | |
1454 | |
1459 | mlockall (flags); |
1455 | return mlockall (flags); |
1460 | } |
1456 | } |
|
|
1457 | #endif |
|
|
1458 | |
|
|
1459 | #if !_POSIX_MEMLOCK_RANGE |
|
|
1460 | # define eio__mlock(a,b) ((errno = ENOSYS), -1) |
|
|
1461 | #else |
|
|
1462 | |
|
|
1463 | static int |
|
|
1464 | eio__mlock (void *addr, size_t length) |
|
|
1465 | { |
|
|
1466 | eio_page_align (&addr, &length); |
|
|
1467 | |
|
|
1468 | return mlock (addr, length); |
|
|
1469 | } |
|
|
1470 | |
1461 | #endif |
1471 | #endif |
1462 | |
1472 | |
1463 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
1473 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
1464 | # define eio__msync(a,b,c) ((errno = ENOSYS), -1) |
1474 | # define eio__msync(a,b,c) ((errno = ENOSYS), -1) |
1465 | #else |
1475 | #else |
… | |
… | |
1630 | return 0; \ |
1640 | return 0; \ |
1631 | } |
1641 | } |
1632 | |
1642 | |
1633 | static void eio_execute (etp_worker *self, eio_req *req) |
1643 | static void eio_execute (etp_worker *self, eio_req *req) |
1634 | { |
1644 | { |
1635 | errno = 0; |
|
|
1636 | |
|
|
1637 | switch (req->type) |
1645 | switch (req->type) |
1638 | { |
1646 | { |
1639 | case EIO_READ: ALLOC (req->size); |
1647 | case EIO_READ: ALLOC (req->size); |
1640 | req->result = req->offs >= 0 |
1648 | req->result = req->offs >= 0 |
1641 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1649 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
… | |
… | |
1673 | case EIO_RMDIR: req->result = rmdir (req->ptr1); break; |
1681 | case EIO_RMDIR: req->result = rmdir (req->ptr1); break; |
1674 | case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; |
1682 | case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; |
1675 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
1683 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
1676 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
1684 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
1677 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
1685 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
1678 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break; |
1686 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1679 | |
1687 | |
1680 | case EIO_READLINK: ALLOC (PATH_MAX); |
1688 | case EIO_READLINK: ALLOC (PATH_MAX); |
1681 | req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; |
1689 | req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; |
1682 | |
1690 | |
1683 | case EIO_SYNC: req->result = 0; sync (); break; |
1691 | case EIO_SYNC: req->result = 0; sync (); break; |
… | |
… | |
1691 | |
1699 | |
1692 | case EIO_READDIR: eio__scandir (req, self); break; |
1700 | case EIO_READDIR: eio__scandir (req, self); break; |
1693 | |
1701 | |
1694 | case EIO_BUSY: |
1702 | case EIO_BUSY: |
1695 | #ifdef _WIN32 |
1703 | #ifdef _WIN32 |
1696 | Sleep (req->nv1 * 1000.); |
1704 | Sleep (req->nv1 * 1e3); |
1697 | #else |
1705 | #else |
1698 | { |
1706 | { |
1699 | struct timeval tv; |
1707 | struct timeval tv; |
1700 | |
1708 | |
1701 | tv.tv_sec = req->nv1; |
1709 | tv.tv_sec = req->nv1; |
1702 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
1710 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1e6; |
1703 | |
1711 | |
1704 | req->result = select (0, 0, 0, 0, &tv); |
1712 | req->result = select (0, 0, 0, 0, &tv); |
1705 | } |
1713 | } |
1706 | #endif |
1714 | #endif |
1707 | break; |
1715 | break; |
… | |
… | |
1722 | times = tv; |
1730 | times = tv; |
1723 | } |
1731 | } |
1724 | else |
1732 | else |
1725 | times = 0; |
1733 | times = 0; |
1726 | |
1734 | |
1727 | |
|
|
1728 | req->result = req->type == EIO_FUTIME |
1735 | req->result = req->type == EIO_FUTIME |
1729 | ? futimes (req->int1, times) |
1736 | ? futimes (req->int1, times) |
1730 | : utimes (req->ptr1, times); |
1737 | : utimes (req->ptr1, times); |
1731 | } |
1738 | } |
1732 | break; |
1739 | break; |
… | |
… | |
1741 | case EIO_CUSTOM: |
1748 | case EIO_CUSTOM: |
1742 | ((void (*)(eio_req *))req->feed) (req); |
1749 | ((void (*)(eio_req *))req->feed) (req); |
1743 | break; |
1750 | break; |
1744 | |
1751 | |
1745 | default: |
1752 | default: |
|
|
1753 | errno = ENOSYS; |
1746 | req->result = -1; |
1754 | req->result = -1; |
1747 | break; |
1755 | break; |
1748 | } |
1756 | } |
1749 | |
1757 | |
1750 | req->errorno = errno; |
1758 | req->errorno = errno; |
… | |
… | |
1933 | REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; |
1941 | REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; |
1934 | } |
1942 | } |
1935 | |
1943 | |
1936 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1944 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1937 | { |
1945 | { |
1938 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; |
1946 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->offs = (off_t)dev; SEND; |
1939 | } |
1947 | } |
1940 | |
1948 | |
1941 | static eio_req * |
1949 | static eio_req * |
1942 | eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1950 | eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1943 | { |
1951 | { |