pax_global_header00006660000000000000000000000064137006312400014506gustar00rootroot0000000000000052 comment=3a52cf8f2907d770217de8e7bea51abc90cf10ec libvpoll-eventfd-0.1/000077500000000000000000000000001370063124000146225ustar00rootroot00000000000000libvpoll-eventfd-0.1/CMakeLists.txt000066400000000000000000000022631370063124000173650ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.12) project(vpoll VERSION 0.0.1 DESCRIPTION "generate synthetic events for poll/select/ppoll/pselect/epoll" HOMEPAGE_URL "https://github.com/rd235/libvpoll-eventfd" LANGUAGES C) include(GNUInstallDirs) add_definitions(-D_GNU_SOURCE) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic -O2 -D_FORTIFY_SOURCE=2") include_directories(${CMAKE_CURRENT_SOURCE_DIR}) if (PROJECT_INCLUDEDIR) include_directories(${PROJECT_INCLUDEDIR}) execute_process(COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/vpoll.h ${PROJECT_INCLUDEDIR}/.) endif (PROJECT_INCLUDEDIR) set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) set(VPOLL_SOURCES vpoll.c) add_library(vpoll SHARED ${VPOLL_SOURCES}) target_link_libraries(vpoll fduserdata) set_target_properties(vpoll PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}) set(PROJECT_PC_REQUIRES "fduserdata") configure_file(vpoll.pc.in vpoll.pc @ONLY) install(TARGETS vpoll LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES vpoll.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/vpoll.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) add_subdirectory(man) libvpoll-eventfd-0.1/COPYING000066400000000000000000000635041370063124000156650ustar00rootroot00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. [This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Libraries If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). To apply these terms, attach the following notices to the library. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the library `Frob' (a library for tweaking knobs) written by James Random Hacker. , 1 April 1990 Ty Coon, President of Vice That's all there is to it! libvpoll-eventfd-0.1/README.md000066400000000000000000000363701370063124000161120ustar00rootroot00000000000000## libvpoll-eventfd **generate synthetic events for poll/select/ppoll/pselect/epoll.** This repository includes two components: * a patch for the Linux kernel to provide a new feature: eventfd-vpoll * a library providing a nice interface to the new feature as well as an emulator in user space (which supports only some of the events, it is for backwards compatibility). ## the problem: This patch implements an extension of eventfd (`EFD_VPOLL`) to define file descriptors whose I/O events can be generated at user level. These file descriptors trigger notifications for [p]select/[p]poll/epoll. This feature is useful for user-level implementations of network stacks or virtual device drivers as libraries. Networking programs use system calls implementing the Berkeley sockets API: `socket, accept, connect, listen, recv*, send*` etc. Programs dealing with a device use system calls like `open, read, write, ioctl` etc. When somebody wants to write a library able to behave like a network stack (say lwipv6, picotcp) or a device, they can implement functions like `my_socket, my_accept, my_open or my_ioctl`, as drop-in replacement of their system call counterpart. (It is also possible to use dynamic library magic to rename/divert the system call requests to use their 'virtual' implementation provided by the library: `socket` maps to `my_socket`, `recv` to `my_recv` etc). In this way portability and compatibility is easier, using a well known API instead of inventing new ones. Unfortunately this approach cannot be applied to `poll/select/ppoll/pselect/epoll`. These system calls can refer at the same time to file descriptors created by *real* system calls like `socket, open, signalfd`... and to file descriptors returned by `my_open`, `your_socket`. While it is possible to provide a partial support (e.g. using pipes or socketpairs), a clean and complete solution is still missing (as far as I have seen); e.g. I have not seen any clean way to generate `EPOLLPRI`, `EPOLLERR`, etc. Example: Let us suppose there is an application waiting for a TCP OOB message. It uses `poll` to wait for POLLPRI and then reads the message (e.g. by `recv`). If I want to port that application to use a network stack implemented as a library I have to rewrite the code about `poll` as it is not possible to receive a `POLLPRI`. From a pipe I can just receive a `POLLIN`, I have to encode in an external data structure any further information. Using `EFD_VPOLL` the solution is straightforward: the function `mysocket` (used in place of `socket` to create a file descriptor behaves as a *real* `socket`) returns a file descriptor created by `eventfd/EFD_VPOLL`, so the `poll` system call can be left unmodified in the code. When the OOB message is available the library can trigger an `EPOLLPRI` and the message can be received using `my_recv`. ## The Linux kernel patch This proposal is based on a new tag for `eventfd2(2)`: `EFD_VPOLL`. This statement: fd = eventfd(EPOLLOUT, EFD_VPOLL | EFD_CLOEXEC); creates a file descriptor for I/O event generation. In this case `EPOLLOUT` is initially true. Likewise all the other eventfs services, `read(2)` and `write(2)` use a 8-byte integer argument. `read(2)` returns the current state of the pending events. The argument of write(2) is an or-composition of a control command (`EFD_VPOLL_ADDEVENTS`, `EFD_VPOLL_DELEVENTS` or `EFD_VPOLL_MODEVENTS`) and the bitmap of events to be added, deleted to the current set of pending events. `EFD_VPOLL_MODEVENTS` completely redefines the set of pending events. e.g.: uint64_t request = EFD_VPOLL_ADDEVENTS | EPOLLIN | EPOLLPRI; write(fd, &request, sizeof(request); adds EPOLLIN and EPOLLPRI to the set of pending events. There can be other approaches than `EFD_VPOLL`: e.g. add two specific new system calls like `vpollfd_create` and `vpollfd_ctl`. Their signature could be: int vpollfd_create(unsigned int init_events, int flags); where flags are the usual `NONBLOCK/CLOEXEC` int vpollfd_ctl(int fd, int op, unsigned int events); where op can be `VPOLL_ADDEVENTS`, `VPOLL_DELEVENTS`, `VPOLL_MODEVENTS` It possible to reimplement the patch this way. It needs the definition of the new system calls. I am proposing just a new tag for eventfd as eventfd purpose is conceptually close to the new feature. Eventfd creates a file descriptor which generates events. The default eventfd mode uses counters while `EFD_VPOLL` uses event flags. The new feature can be implemented on eventfd with a very limited impact on the kernel core code. Instead of syscalls, the `vpollfd_create/vpollfd_ctl` API could be provided by the glibc as (very simple) library functions, as it is the case for `eventfd_read/eventfd_write` in /usr/include/sys/eventfd.h (It is exactly what libvpoll does, including an emulator providing a partial support of vpoll) These are examples of messages asking for a feature like `EFD_VPOLL`: * https://stackoverflow.com/questions/909189/simulating-file-descriptor-in-user-space * https://stackoverflow.com/questions/1648147/running-a-simple-tcp-server-with-poll-how-do-i-trigger-events-artificially * ... and I need it to write networking and device modules for vuos: https://github.com/virtualsquare/vuos (it is the new codebase of ViewOS, see www.virtualsquare.org). ### EXAMPLE of program using `EFD_VPOLL`: The following program creates an `eventfd/EFD_VPOLL` file descriptor and then forks a child process. While the parent waits for events using `epoll_wait` the child generates a sequence of events. When the parent receives an event (or a set of events) it prints it and disarm it. The following shell session shows a sample run of the program: timeout... timeout... GOT event 1 timeout... GOT event 1 timeout... GOT event 3 timeout... GOT event 2 timeout... GOT event 4 timeout... GOT event 10 Program source: ```C #include #include #include #include #include #include /* Definition of uint64_t */ #ifndef EFD_VPOLL #define EFD_VPOLL (1 << 1) #define EFD_VPOLL_ADDEVENTS (1ULL << 32) #define EFD_VPOLL_DELEVENTS (2ULL << 32) #define EFD_VPOLL_MODEVENTS (3ULL << 32) #endif #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) static void vpoll_ctl(int fd, uint64_t request) { ssize_t s; s = write(fd, &request, sizeof(request)); if (s != sizeof(uint64_t)) handle_error("write"); } int main(int argc, char *argv[]) { int efd, epollfd; struct epoll_event ev; ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLOUT | EPOLLHUP | EPOLLPRI; ev.data.u64 = 0; efd = eventfd(0, EFD_VPOLL | EFD_CLOEXEC); if (efd == -1) handle_error("eventfd"); epollfd = epoll_create1(EPOLL_CLOEXEC); if (efd == -1) handle_error("epoll_create1"); if (epoll_ctl(epollfd, EPOLL_CTL_ADD, efd, &ev) == -1) handle_error("epoll_ctl"); switch (fork()) { case 0: sleep(3); vpoll_ctl(efd, EFD_VPOLL_ADDEVENTS | EPOLLIN); sleep(2); vpoll_ctl(efd, EFD_VPOLL_ADDEVENTS | EPOLLIN); sleep(2); vpoll_ctl(efd, EFD_VPOLL_ADDEVENTS | EPOLLIN | EPOLLPRI); sleep(2); vpoll_ctl(efd, EFD_VPOLL_ADDEVENTS | EPOLLPRI); sleep(2); vpoll_ctl(efd, EFD_VPOLL_ADDEVENTS | EPOLLOUT); sleep(2); vpoll_ctl(efd, EFD_VPOLL_ADDEVENTS | EPOLLHUP); exit(EXIT_SUCCESS); default: while (1) { int nfds; nfds = epoll_wait(epollfd, &ev, 1, 1000); if (nfds < 0) handle_error("epoll_wait"); else if (nfds == 0) printf("timeout...\n"); else { printf("GOT event %x\n", ev.events); vpoll_ctl(efd, EFD_VPOLL_DELEVENTS | ev.events); if (ev.events & EPOLLHUP) break; } } case -1: handle_error("fork"); } close(epollfd); close(efd); return 0; } ``` ## `/dev/vpoll`: vpoll as a virtual device (kernel module) This is an alternative implementation of the vpoll support. When the module vpoll.ko is loaded, udev creates the device "/dev/vpoll". This statement: ``` fd = open("/dev/vpoll", O_RDWR | O_CLOEXEC); ``` creates a file descriptor for I/O event generation that can be used in poll/select/epoll system calls. Events can be generated by specific `ioctl`. ```C #define VPOLL_IOC_MAGIC '^' #define VPOLL_IO_ADDEVENTS _IO(VPOLL_IOC_MAGIC, 1) #define VPOLL_IO_DELEVENTS _IO(VPOLL_IOC_MAGIC, 2) #define VPOLL_IO_SETEVENTS _IO(VPOLL_IOC_MAGIC, 3) ``` A statement like `ioctl(fd, VPOLL_IO_ADDEVENTS, EPOLLIN | EPOLLPRI)` generates a the events `EPOLLIN` and `EPOLLPRI`. In a similar manner the ioctl tags `VPOLL_IO_DELEVENTS` and `VPOLL_IO_SETEVENTS` can be used to delete events or change the set of current events, respectively. ### Compile and load the vpoll kernel module Pprecondition: availability of the kernel sources or at least the kernel headers of the currently running kernel. ```bash cd linux_module make sudo insmod vpoll.ko ``` ### EXAMPLE of program using `/dev/vpoll`: The following program opens `/dev/vpoll` and then forks a child process. While the parent waits for events using `epoll_wait` the child generates a sequence of events. When the parent receives an event (or a set of events) it prints it and disarm it. The following shell session shows a sample run of the program: timeout... timeout... GOT event 1 timeout... GOT event 1 timeout... GOT event 3 timeout... GOT event 2 timeout... GOT event 4 timeout... GOT event 10 Program source: ```C #include #include #include #include #include #include #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) #define VPOLL_IOC_MAGIC '^' #define VPOLL_IO_ADDEVENTS _IO(VPOLL_IOC_MAGIC, 1) #define VPOLL_IO_DELEVENTS _IO(VPOLL_IOC_MAGIC, 2) #define VPOLL_IO_SETEVENTS _IO(VPOLL_IOC_MAGIC, 3) int main(int argc, char *argv[]) { int efd, epollfd; struct epoll_event ev; ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLOUT | EPOLLHUP | EPOLLPRI; ev.data.u64 = 0; efd = open("/dev/vpoll", O_RDWR | O_CLOEXEC); if (efd == -1) handle_error("/dev/vpoll"); epollfd = epoll_create1(EPOLL_CLOEXEC); if (efd == -1) handle_error("epoll_create1"); if (epoll_ctl(epollfd, EPOLL_CTL_ADD, efd, &ev) == -1) handle_error("epoll_ctl"); switch (fork()) { case 0: sleep(3); ioctl(efd, VPOLL_IO_ADDEVENTS,EPOLLIN); sleep(2); ioctl(efd, VPOLL_IO_ADDEVENTS, EPOLLIN); sleep(2); ioctl(efd, VPOLL_IO_ADDEVENTS, EPOLLIN | EPOLLPRI); sleep(2); ioctl(efd, VPOLL_IO_ADDEVENTS, EPOLLPRI); sleep(2); ioctl(efd, VPOLL_IO_ADDEVENTS, EPOLLOUT); sleep(2); ioctl(efd, VPOLL_IO_ADDEVENTS, EPOLLHUP); exit(EXIT_SUCCESS); default: while (1) { int nfds; nfds = epoll_wait(epollfd, &ev, 1, 1000); if (nfds < 0) handle_error("epoll_wait"); else if (nfds == 0) printf("timeout...\n"); else { printf("GOT event %x\n", ev.events); ioctl(efd, VPOLL_IO_DELEVENTS, ev.events); if (ev.events & EPOLLHUP) break; } } case -1: handle_error("fork"); } close(epollfd); close(efd); return 0; } ``` ## The libvpoll library This library: * uses `eventfd/VPOLL` if supported by the kernel * uses the vpoll device if available * otherwise it implements an emulator (providing only `EPOLLIN`, `EPOLLOUT` and a non standard version of EPOLLHUP/EPOLLRDHUP, what can be done without a specific kernel support). The API of libvpoll is clean and simple: ``` #define VPOLL_CTL_ADDEVENTS 1 #define VPOLL_CTL_DELEVENTS 2 #define VPOLL_CTL_SETEVENTS 3 int vpoll_create(uint32_t init_events, int flags); int vpoll_ctl(int fd, int op, uint32_t events); int vpoll_close(int fd); ``` * `vpoll_create` returns a file descriptor for vpoll * `vpoll_ctl` adds, deletes or changes the currently active events on fd * `vpoll_close` closes the vpoll file descriptor. ### An example using libvpoll: ``` #define _GNU_SOURCE #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int vpollfd = vpoll_create(0, FD_CLOEXEC); int epfd = epoll_create1(EPOLL_CLOEXEC); struct epoll_event reqevents={EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLOUT | EPOLLHUP | EPOLLPRI}; epoll_ctl(epfd, EPOLL_CTL_ADD, vpollfd, &reqevents); switch (fork()) { case 0: sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLIN); sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLIN); sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLOUT); sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLHUP); sleep(3); exit(0); default: while (1) { struct epoll_event ev; int n = epoll_wait(epfd, &ev, 1, 1000); if (n < 0) { perror("epoll_wait"); break; } if (n > 0) { printf("GOT event %x\n", ev.events); vpoll_ctl(vpollfd, VPOLL_CTL_DELEVENTS, ev.events); if (ev.events & EPOLLHUP) break; } else { printf("timeout\n"); } } break; case -1: printf("fork error\n"); } vpoll_close(vpollfd); close(epfd); return 0; } ``` On a machine running a Linux Kernel providing eventfd/vpoll or /dev/vpoll the output of this program is: ``` timeout timeout GOT event 1 timeout timeout GOT event 1 timeout timeout GOT event 4 timeout timeout GOT event 10 ``` Instead when the demo program runs using the emulation layer the output is: ``` timeout timeout GOT event 1 timeout timeout GOT event 1 timeout timeout GOT event 4 timeout timeout GOT event 2011 ``` In fact, the emulator uses a socketpair to generate the events. Hangup is emulated by closing the other end of the socketpair: this generates EPOLLHUP as well as EPOLLIN and EPOLLRDHUP. ## Install ### Kernel-patch Download from kernel.org or via git a recent version of the Linux kernel tree. Run the following command (the current working directory must be the root of the kernel tree): ``` patch -p 1 < /path/of/this/git/clone/linux_patch/linux_patch_v5.2-rc6 ``` configure, compile and install the kernel as usual. ### libvpoll This library requires fduserdata: https://github.com/rd235/libfduserdata The following sequence of commands that can be used to compile install the library. ``` mkdir build cd build cmake .. make sudo make install ``` libvpoll-eventfd-0.1/linux_module/000077500000000000000000000000001370063124000173265ustar00rootroot00000000000000libvpoll-eventfd-0.1/linux_module/Makefile000066400000000000000000000002341370063124000207650ustar00rootroot00000000000000obj-m += vpoll.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean libvpoll-eventfd-0.1/linux_module/vpoll.c000066400000000000000000000067621370063124000206410ustar00rootroot00000000000000#include #include #include #include #include #include #include #include #include MODULE_LICENSE("GPL"); MODULE_AUTHOR("Renzo Davoli"); MODULE_DESCRIPTION("vpoll: sythesize events for select/poll/epoll"); MODULE_VERSION("0.01"); #define NAME "vpoll" #define VPOLL_IOC_MAGIC '^' #define VPOLL_IO_ADDEVENTS _IO(VPOLL_IOC_MAGIC, 1) #define VPOLL_IO_DELEVENTS _IO(VPOLL_IOC_MAGIC, 2) #define VPOLL_IO_SETEVENTS _IO(VPOLL_IOC_MAGIC, 3) #define EPOLLALLMASK ((__force __poll_t)0x0fffffff) static int major = -1; static struct cdev mycdev; static struct class *vpoll_class = NULL; struct vpoll_data { wait_queue_head_t wqh; __poll_t events; }; static int vpoll_open(struct inode *inode, struct file *file) { struct vpoll_data *vpoll_data; vpoll_data = kmalloc(sizeof(struct vpoll_data), GFP_KERNEL); if (!vpoll_data) return -ENOMEM; vpoll_data->events = 0; init_waitqueue_head(&vpoll_data->wqh); file->private_data = vpoll_data; return 0; } static int vpoll_release(struct inode *inode, struct file *file) { struct vpoll_data *vpoll_data = file->private_data; kfree(vpoll_data); return 0; } static long vpoll_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct vpoll_data *vpoll_data = file->private_data; __poll_t events = arg & EPOLLALLMASK; long res = 0; spin_lock_irq(&vpoll_data->wqh.lock); switch(cmd) { case VPOLL_IO_ADDEVENTS: vpoll_data->events |= events; break; case VPOLL_IO_DELEVENTS: vpoll_data->events &= ~events; break; case VPOLL_IO_SETEVENTS: vpoll_data->events = events; break; default: res = -EINVAL; } if (res >= 0) { res = vpoll_data->events; if (waitqueue_active(&vpoll_data->wqh)) wake_up_locked_poll(&vpoll_data->wqh, vpoll_data->events); } spin_unlock_irq(&vpoll_data->wqh.lock); return res; } static __poll_t vpoll_poll(struct file *file, struct poll_table_struct *wait) { struct vpoll_data *vpoll_data = file->private_data; poll_wait(file, &vpoll_data->wqh, wait); return READ_ONCE(vpoll_data->events); } static const struct file_operations fops = { .owner = THIS_MODULE, .open = vpoll_open, .release = vpoll_release, .unlocked_ioctl = vpoll_ioctl, .poll = vpoll_poll, }; static char *vpoll_devnode(struct device *dev, umode_t *mode) { if (!mode) return NULL; *mode = 0666; return NULL; } static int myinit(void) { int ret; struct device *dev; if ((ret = alloc_chrdev_region(&major, 0, 1, NAME)) < 0) return ret; vpoll_class = class_create(THIS_MODULE, NAME); if (IS_ERR(vpoll_class)) { ret = PTR_ERR(vpoll_class); goto error_unregister_chrdev_region; } vpoll_class->devnode = vpoll_devnode; dev = device_create(vpoll_class, NULL, major, NULL, NAME); if (IS_ERR(dev)) { ret = PTR_ERR(dev); goto error_class_destroy; } cdev_init(&mycdev, &fops); if ((ret = cdev_add(&mycdev, major, 1)) < 0) goto error_device_destroy; printk(KERN_INFO NAME ": loaded\n"); return 0; error_device_destroy: device_destroy(vpoll_class, major); error_class_destroy: class_destroy(vpoll_class); error_unregister_chrdev_region: unregister_chrdev_region(major, 1); return ret; } static void myexit(void) { device_destroy(vpoll_class, major); cdev_del(&mycdev); class_destroy(vpoll_class); unregister_chrdev_region(major, 1); printk(KERN_INFO NAME ": unloaded\n"); } module_init(myinit) module_exit(myexit) libvpoll-eventfd-0.1/linux_patch/000077500000000000000000000000001370063124000171405ustar00rootroot00000000000000libvpoll-eventfd-0.1/linux_patch/linux_patch_v5.2-rc6000066400000000000000000000136221370063124000226470ustar00rootroot00000000000000diff --git a/fs/eventfd.c b/fs/eventfd.c index 8aa0ea8c55e8..6cdb1b854341 100644 --- a/fs/eventfd.c +++ b/fs/eventfd.c @@ -24,18 +24,32 @@ #include #include +#define EPOLLALLMASK64 ((__force __u64)EPOLLALLMASK) + static DEFINE_IDA(eventfd_ida); struct eventfd_ctx { struct kref kref; wait_queue_head_t wqh; /* - * Every time that a write(2) is performed on an eventfd, the - * value of the __u64 being written is added to "count" and a - * wakeup is performed on "wqh". A read(2) will return the "count" - * value to userspace, and will reset "count" to zero. The kernel - * side eventfd_signal() also, adds to the "count" counter and - * issue a wakeup. + * If the EFD_VPOLL flag was NOT set at eventfd creation: + * Every time that a write(2) is performed on an eventfd, the + * value of the __u64 being written is added to "count" and a + * wakeup is performed on "wqh". A read(2) will return the "count" + * value to userspace, and will reset "count" to zero (or decrement + * "count" by 1 if the flag EFD_SEMAPHORE has been set). The kernel + * side eventfd_signal() also, adds to the "count" counter and + * issue a wakeup. + * + * If the EFD_VPOLL flag was set at eventfd creation: + * count is the set of pending EPOLL events. + * read(2) returns the current value of count. + * The argument of write(2) is an 8-byte integer: + * it is an or-composition of a control command (EFD_VPOLL_ADDEVENTS, + * EFD_VPOLL_DELEVENTS or EFD_VPOLL_MODEVENTS) and the bitmap of + * events to be added, deleted to the current set of pending events. + * (i.e. which bits of "count" must be set or reset). + * EFD_VPOLL_MODEVENTS redefines the set of pending events. */ __u64 count; unsigned int flags; @@ -295,6 +309,78 @@ static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c return res; } +static __poll_t eventfd_vpoll_poll(struct file *file, poll_table *wait) +{ + struct eventfd_ctx *ctx = file->private_data; + __poll_t events = 0; + u64 count; + + poll_wait(file, &ctx->wqh, wait); + + count = READ_ONCE(ctx->count); + + events = (((__force __poll_t)count) & EPOLLALLMASK); + + return events; +} + +static ssize_t eventfd_vpoll_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + struct eventfd_ctx *ctx = file->private_data; + ssize_t res; + __u64 ucnt = 0; + + if (count < sizeof(ucnt)) + return -EINVAL; + res = sizeof(ucnt); + ucnt = READ_ONCE(ctx->count); + if (put_user(ucnt, (__u64 __user *)buf)) + return -EFAULT; + + return res; +} + +static ssize_t eventfd_vpoll_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct eventfd_ctx *ctx = file->private_data; + ssize_t res; + __u64 ucnt; + __u32 events; + + if (count < sizeof(ucnt)) + return -EINVAL; + if (copy_from_user(&ucnt, buf, sizeof(ucnt))) + return -EFAULT; + spin_lock_irq(&ctx->wqh.lock); + + events = ucnt & EPOLLALLMASK64; + res = sizeof(ucnt); + switch (ucnt & ~EPOLLALLMASK64) { + case EFD_VPOLL_ADDEVENTS: + ctx->count |= events; + break; + case EFD_VPOLL_DELEVENTS: + ctx->count &= ~(events); + break; + case EFD_VPOLL_MODEVENTS: + ctx->count = (ctx->count & ~EPOLLALLMASK64) | events; + break; + default: + res = -EINVAL; + } + + /* wake up waiting threads */ + if (res >= 0 && waitqueue_active(&ctx->wqh)) + wake_up_locked_poll(&ctx->wqh, ((__force __poll_t)ctx->count) & EPOLLALLMASK); + + spin_unlock_irq(&ctx->wqh.lock); + + return res; + +} + #ifdef CONFIG_PROC_FS static void eventfd_show_fdinfo(struct seq_file *m, struct file *f) { @@ -319,6 +405,17 @@ static const struct file_operations eventfd_fops = { .llseek = noop_llseek, }; +static const struct file_operations eventfd_vpoll_fops = { +#ifdef CONFIG_PROC_FS + .show_fdinfo = eventfd_show_fdinfo, +#endif + .release = eventfd_release, + .poll = eventfd_vpoll_poll, + .read = eventfd_vpoll_read, + .write = eventfd_vpoll_write, + .llseek = noop_llseek, +}; + /** * eventfd_fget - Acquire a reference of an eventfd file descriptor. * @fd: [in] Eventfd file descriptor. @@ -391,6 +488,7 @@ EXPORT_SYMBOL_GPL(eventfd_ctx_fileget); static int do_eventfd(unsigned int count, int flags) { struct eventfd_ctx *ctx; + const struct file_operations *fops = &eventfd_fops; int fd; /* Check the EFD_* constants for consistency. */ @@ -410,7 +508,11 @@ static int do_eventfd(unsigned int count, int flags) ctx->flags = flags; ctx->id = ida_simple_get(&eventfd_ida, 0, 0, GFP_KERNEL); - fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx, + if (flags & EFD_VPOLL) { + fops = &eventfd_vpoll_fops; + ctx->count &= EPOLLALLMASK64; + } + fd = anon_inode_getfd("[eventfd]", fops, ctx, O_RDWR | (flags & EFD_SHARED_FCNTL_FLAGS)); if (fd < 0) eventfd_free_ctx(ctx); diff --git a/include/linux/eventfd.h b/include/linux/eventfd.h index ffcc7724ca21..5b1e6ef56651 100644 --- a/include/linux/eventfd.h +++ b/include/linux/eventfd.h @@ -21,11 +21,16 @@ * shared O_* flags. */ #define EFD_SEMAPHORE (1 << 0) +#define EFD_VPOLL (1 << 1) #define EFD_CLOEXEC O_CLOEXEC #define EFD_NONBLOCK O_NONBLOCK #define EFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK) -#define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE) +#define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE | EFD_VPOLL) + +#define EFD_VPOLL_ADDEVENTS (1ULL << 32) +#define EFD_VPOLL_DELEVENTS (2ULL << 32) +#define EFD_VPOLL_MODEVENTS (3ULL << 32) struct eventfd_ctx; struct file; diff --git a/include/uapi/linux/eventpoll.h b/include/uapi/linux/eventpoll.h index 8a3432d0f0dc..814de6d869c7 100644 --- a/include/uapi/linux/eventpoll.h +++ b/include/uapi/linux/eventpoll.h @@ -41,6 +41,8 @@ #define EPOLLMSG (__force __poll_t)0x00000400 #define EPOLLRDHUP (__force __poll_t)0x00002000 +#define EPOLLALLMASK ((__force __poll_t)0x0fffffff) + /* Set exclusive wakeup mode for the target file descriptor */ #define EPOLLEXCLUSIVE ((__force __poll_t)(1U << 28)) libvpoll-eventfd-0.1/linux_patch/linux_patch_v5.8-rc4000066400000000000000000000137361370063124000226610ustar00rootroot00000000000000diff --git a/fs/eventfd.c b/fs/eventfd.c index df466ef81ddd..a76299ecf034 100644 --- a/fs/eventfd.c +++ b/fs/eventfd.c @@ -27,18 +27,32 @@ DEFINE_PER_CPU(int, eventfd_wake_count); +#define EPOLLALLMASK64 ((__force __u64)EPOLLALLMASK) + static DEFINE_IDA(eventfd_ida); struct eventfd_ctx { struct kref kref; wait_queue_head_t wqh; /* - * Every time that a write(2) is performed on an eventfd, the - * value of the __u64 being written is added to "count" and a - * wakeup is performed on "wqh". A read(2) will return the "count" - * value to userspace, and will reset "count" to zero. The kernel - * side eventfd_signal() also, adds to the "count" counter and - * issue a wakeup. + * If the EFD_VPOLL flag was NOT set at eventfd creation: + * Every time that a write(2) is performed on an eventfd, the + * value of the __u64 being written is added to "count" and a + * wakeup is performed on "wqh". A read(2) will return the "count" + * value to userspace, and will reset "count" to zero (or decrement + * "count" by 1 if the flag EFD_SEMAPHORE has been set). The kernel + * side eventfd_signal() also, adds to the "count" counter and + * issue a wakeup. + * + * If the EFD_VPOLL flag was set at eventfd creation: + * count is the set of pending EPOLL events. + * read(2) returns the current value of count. + * The argument of write(2) is an 8-byte integer: + * it is an or-composition of a control command (EFD_VPOLL_ADDEVENTS, + * EFD_VPOLL_DELEVENTS or EFD_VPOLL_MODEVENTS) and the bitmap of + * events to be added, deleted to the current set of pending events. + * (i.e. which bits of "count" must be set or reset). + * EFD_VPOLL_MODEVENTS redefines the set of pending events. */ __u64 count; unsigned int flags; @@ -308,6 +322,78 @@ static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c return res; } +static __poll_t eventfd_vpoll_poll(struct file *file, poll_table *wait) +{ + struct eventfd_ctx *ctx = file->private_data; + __poll_t events = 0; + u64 count; + + poll_wait(file, &ctx->wqh, wait); + + count = READ_ONCE(ctx->count); + + events = (((__force __poll_t)count) & EPOLLALLMASK); + + return events; +} + +static ssize_t eventfd_vpoll_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + struct eventfd_ctx *ctx = file->private_data; + ssize_t res; + __u64 ucnt = 0; + + if (count < sizeof(ucnt)) + return -EINVAL; + res = sizeof(ucnt); + ucnt = READ_ONCE(ctx->count); + if (put_user(ucnt, (__u64 __user *)buf)) + return -EFAULT; + + return res; +} + +static ssize_t eventfd_vpoll_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct eventfd_ctx *ctx = file->private_data; + ssize_t res; + __u64 ucnt; + __u32 events; + + if (count < sizeof(ucnt)) + return -EINVAL; + if (copy_from_user(&ucnt, buf, sizeof(ucnt))) + return -EFAULT; + spin_lock_irq(&ctx->wqh.lock); + + events = ucnt & EPOLLALLMASK64; + res = sizeof(ucnt); + switch (ucnt & ~EPOLLALLMASK64) { + case EFD_VPOLL_ADDEVENTS: + ctx->count |= events; + break; + case EFD_VPOLL_DELEVENTS: + ctx->count &= ~(events); + break; + case EFD_VPOLL_MODEVENTS: + ctx->count = (ctx->count & ~EPOLLALLMASK64) | events; + break; + default: + res = -EINVAL; + } + + /* wake up waiting threads */ + if (res >= 0 && waitqueue_active(&ctx->wqh)) + wake_up_locked_poll(&ctx->wqh, ((__force __poll_t)ctx->count) & EPOLLALLMASK); + + spin_unlock_irq(&ctx->wqh.lock); + + return res; + +} + #ifdef CONFIG_PROC_FS static void eventfd_show_fdinfo(struct seq_file *m, struct file *f) { @@ -332,6 +418,17 @@ static const struct file_operations eventfd_fops = { .llseek = noop_llseek, }; +static const struct file_operations eventfd_vpoll_fops = { +#ifdef CONFIG_PROC_FS + .show_fdinfo = eventfd_show_fdinfo, +#endif + .release = eventfd_release, + .poll = eventfd_vpoll_poll, + .read = eventfd_vpoll_read, + .write = eventfd_vpoll_write, + .llseek = noop_llseek, +}; + /** * eventfd_fget - Acquire a reference of an eventfd file descriptor. * @fd: [in] Eventfd file descriptor. @@ -404,6 +501,7 @@ EXPORT_SYMBOL_GPL(eventfd_ctx_fileget); static int do_eventfd(unsigned int count, int flags) { struct eventfd_ctx *ctx; + const struct file_operations *fops = &eventfd_fops; struct file *file; int fd; @@ -424,13 +522,17 @@ static int do_eventfd(unsigned int count, int flags) ctx->flags = flags; ctx->id = ida_simple_get(&eventfd_ida, 0, 0, GFP_KERNEL); + if (flags & EFD_VPOLL) { + fops = &eventfd_vpoll_fops; + ctx->count &= EPOLLALLMASK64; + } flags &= EFD_SHARED_FCNTL_FLAGS; flags |= O_RDWR; fd = get_unused_fd_flags(flags); if (fd < 0) goto err; - file = anon_inode_getfile("[eventfd]", &eventfd_fops, ctx, flags); + file = anon_inode_getfile("[eventfd]", fops, ctx, flags); if (IS_ERR(file)) { put_unused_fd(fd); fd = PTR_ERR(file); diff --git a/include/linux/eventfd.h b/include/linux/eventfd.h index dc4fd8a6644d..da8db69f348b 100644 --- a/include/linux/eventfd.h +++ b/include/linux/eventfd.h @@ -23,11 +23,16 @@ * shared O_* flags. */ #define EFD_SEMAPHORE (1 << 0) +#define EFD_VPOLL (1 << 1) #define EFD_CLOEXEC O_CLOEXEC #define EFD_NONBLOCK O_NONBLOCK #define EFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK) -#define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE) +#define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE | EFD_VPOLL) + +#define EFD_VPOLL_ADDEVENTS (1ULL << 32) +#define EFD_VPOLL_DELEVENTS (2ULL << 32) +#define EFD_VPOLL_MODEVENTS (3ULL << 32) struct eventfd_ctx; struct file; diff --git a/include/uapi/linux/eventpoll.h b/include/uapi/linux/eventpoll.h index 8a3432d0f0dc..814de6d869c7 100644 --- a/include/uapi/linux/eventpoll.h +++ b/include/uapi/linux/eventpoll.h @@ -41,6 +41,8 @@ #define EPOLLMSG (__force __poll_t)0x00000400 #define EPOLLRDHUP (__force __poll_t)0x00002000 +#define EPOLLALLMASK ((__force __poll_t)0x0fffffff) + /* Set exclusive wakeup mode for the target file descriptor */ #define EPOLLEXCLUSIVE ((__force __poll_t)(1U << 28)) libvpoll-eventfd-0.1/man/000077500000000000000000000000001370063124000153755ustar00rootroot00000000000000libvpoll-eventfd-0.1/man/CMakeLists.txt000066400000000000000000000020101370063124000201260ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.7) set(RONN_ORGANIZATION "VirtualSquare") set(RONN_ARGS --organization=${RONN_ORGANIZATION}) # ### ronn pages file(GLOB VU_RONN_PAGES ${CMAKE_CURRENT_SOURCE_DIR}/*.[1-8].ronn) set(VU_MAN_FILES) foreach(VU_RONN_PATH IN LISTS VU_RONN_PAGES) # VU_RONNPAGE: basename of VU_RONN_PATH get_filename_component(VU_RONNPAGE ${VU_RONN_PATH} NAME) # VU_MANPAGE: VU_RONNPAGE without the suffix string(REGEX REPLACE "\.ronn$" "" VU_MANPAGE ${VU_RONNPAGE}) list(APPEND VU_MAN_FILES ${VU_MANPAGE}) endforeach(VU_RONN_PATH) add_custom_target(${PROJECT_NAME}_manpages ALL make RONN_ARGS="${RONN_ARGS}" ${VU_MAN_FILES} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) ### man pages file(GLOB VU_MAN_PAGES ${CMAKE_CURRENT_SOURCE_DIR}/*.[1-8]) foreach(VU_MAN_PATH IN LISTS VU_MAN_PAGES) get_filename_component(VU_MANPAGE ${VU_MAN_PATH} NAME) string(REGEX REPLACE ".*\\." "" MAN_CHAPTER ${VU_MANPAGE}) install(FILES ${VU_MAN_PATH} DESTINATION ${CMAKE_INSTALL_MANDIR}/man${MAN_CHAPTER}) endforeach(VU_MAN_PATH) libvpoll-eventfd-0.1/man/Makefile000066400000000000000000000005551370063124000170420ustar00rootroot00000000000000RONN=ronn RONNOK := $(shell command -v ${RONN} 2> /dev/null) none: % : %.ronn ifdef RONNOK # copy copyright notice grep "^\.\\\\\"" $< > $@ || true # run ronn $(RONN) -r ${RONN_ARGS} --pipe $< >> $@ # delete useless trailing "" in .TH sed -i '/^\.TH /s/ ""$$//' $@ else echo "${RONN} is not available. Manpage $@ cannot be updated" >/dev/stderr >&2 endif libvpoll-eventfd-0.1/man/vpoll.3000066400000000000000000000130251370063124000166160ustar00rootroot00000000000000.\" Copyright (C) 2019 VirtualSquare. Project Leader: Renzo Davoli .\" .\" This is free documentation; you can redistribute it and/or .\" modify it under the terms of the GNU General Public License, .\" as published by the Free Software Foundation, either version 2 .\" of the License, or (at your option) any later version. .\" .\" The GNU General Public License's references to "object code" .\" and "executables" are to be interpreted as the output of any .\" document formatting or typesetting system, including .\" intermediate and printed output. .\" .\" This manual is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the .\" GNU General Public License for more details. .\" .\" You should have received a copy of the GNU General Public .\" License along with this manual; if not, write to the Free .\" Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, .\" MA 02110-1301 USA. .\" .\" generated with Ronn-NG/v0.8.0 .\" http://github.com/apjanke/ronn-ng/tree/0.8.0 .TH "VPOLL" "3" "October 2019" "VirtualSquare" .SH "NAME" vpoll_create, vpoll_ctl, vpoll_close \- generate synthetic events for poll/select/ppoll/pselect/epoll .SH "SYNOPSIS" \fB#include \fR .P \fBint vpoll_create(uint32_t\fR \fIinit_events\fR\fB, int\fR \fIflags\fR\fB);\fR .P \fBint vpoll_ctl(int\fR \fIfd\fR\fB, int\fR \fIop\fR\fB, uint32_t\fR \fIevents\fR\fB);\fR .P \fBint vpoll_close(int\fR \fIfd\fR\fB);\fR .SH "DESCRIPTION" This library permits one to create a vpoll file descriptor "vpollfd" that can be used in poll/select/ppoll/pselect/epoll(2) system calls\. The events reported by a can be controlled by \fBvpoll_ctl\fR\. \fBvpoll\fR encodes the events using the same flags EPOLL* defined in \fBepoll_ctl(2)\fR\. .P The library uses the vpoll extension for eventfd if the kernel provides it otherwise it (partially) emulates the feature using socketpair(2)\. The emulation supports only \fBEPOLLIN\fR, \fBEPOLLOUT\fR flags and a non standard version of EPOLLHUP/EPOLLRDHUP\. .TP \fBvpoll_create\fR This function creates a "vpollfd"\. The argument \fIinit_events\fR is used to set the initial state of events\. The following value can be included in \fIflags\fR: .IP \fBFD_CLOEXEC\fR: Set the close\-on\-exec flag on the new file descriptor\. See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful\. .TP \fBvpoll_ctl\fR This function changes the set of pending events reported by a "vpollfd"\. The argument \fIop\fR can take the following values: .IP \fBVPOLL_CTL_ADDEVENTS\fR: the events set in the argument \fIevents\fR are added to the set of pending events\. .IP \fBVPOLL_CTL_DELEVENTS\fR: the events set in the argument \fIevents\fR are deleted from the set of pending events\. .IP \fBVPOLL_CTL_SETEVENTS\fR: the value of the argument \fIevents\fR is assigned to the set of pending events\. .TP \fBvpoll_close\fR This function closes the vpoll file descritor\. .SH "RETURN VALUE" \fBvpoll_create\fR returns the new file descriptor, or \-1 if an error occurred (in which case, errno is set appropriately) .P \fBvpoll_ctl\fR and \fBvpoll_close\fR return zero in case of success\. On error, \-1 is returned, and errno is set appropriately\. .SH "EXAMPLE" .nf #define _GNU_SOURCE #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int vpollfd = vpoll_create(0, FD_CLOEXEC); int epfd = epoll_create1(EPOLL_CLOEXEC); struct epoll_event reqevents={EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLOUT | EPOLLHUP | EPOLLPRI}; epoll_ctl(epfd, EPOLL_CTL_ADD, vpollfd, &reqevents); switch (fork()) { case 0: sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLIN); sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLIN); sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLOUT); sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLHUP); sleep(3); exit(0); default: while (1) { struct epoll_event ev; int n = epoll_wait(epfd, &ev, 1, 1000); if (n < 0) { perror("epoll_wait"); break; } if (n > 0) { printf("GOT event %x\en", ev\.events); vpoll_ctl(vpollfd, VPOLL_CTL_DELEVENTS, ev\.events); if (ev\.events & EPOLLHUP) break; } else { printf("timeout\en"); } } break; case \-1: printf("fork error\en"); } vpoll_close(vpollfd); close(epfd); return 0; } .fi .P On a machine running a Linux Kernel providing eventfd/vpoll the output of this program is: .IP "" 4 .nf timeout timeout GOT event 1 timeout timeout GOT event 1 timeout timeout GOT event 4 timeout timeout GOT event 10 .fi .IP "" 0 .P Instead when the demo program runs using the emulation layer the output is: .IP "" 4 .nf timeout timeout GOT event 1 timeout timeout GOT event 1 timeout timeout GOT event 4 timeout timeout GOT event 2011 .fi .IP "" 0 .P In fact, the emulator uses a socketpair to generate the events\. Hangup is emulated by closing the other end of the socketpair: this generates EPOLLHUP as well as EPOLLIN and EPOLLRDHUP\. .SH "AUTHOR" VirtualSquare\. Project leader: Renzo Davoli\. libvpoll-eventfd-0.1/man/vpoll.3.ronn000066400000000000000000000124131370063124000175710ustar00rootroot00000000000000# ## NAME vpoll_create, vpoll_ctl, vpoll_close - generate synthetic events for poll/select/ppoll/pselect/epoll ## SYNOPSIS `#include ` `int vpoll_create(uint32_t ` _init_events_`, int ` _flags_`);` `int vpoll_ctl(int ` _fd_`, int ` _op_`, uint32_t ` _events_`);` `int vpoll_close(int ` _fd_`);` ## DESCRIPTION This library permits one to create a vpoll file descriptor "vpollfd" that can be used in poll/select/ppoll/pselect/epoll(2) system calls. The events reported by a can be controlled by `vpoll_ctl`. `vpoll` encodes the events using the same flags EPOLL* defined in `epoll_ctl(2)`. The library uses the vpoll extension for eventfd if the kernel provides it otherwise it (partially) emulates the feature using socketpair(2). The emulation supports only `EPOLLIN`, `EPOLLOUT` flags and a non standard version of EPOLLHUP/EPOLLRDHUP. * `vpoll_create`: This function creates a "vpollfd". The argument _init_events_ is used to set the initial state of events. The following value can be included in _flags_: `FD_CLOEXEC`: Set the close-on-exec flag on the new file descriptor. See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful. * `vpoll_ctl`: This function changes the set of pending events reported by a "vpollfd". The argument _op_ can take the following values: `VPOLL_CTL_ADDEVENTS`: the events set in the argument _events_ are added to the set of pending events. `VPOLL_CTL_DELEVENTS`: the events set in the argument _events_ are deleted from the set of pending events. `VPOLL_CTL_SETEVENTS`: the value of the argument _events_ is assigned to the set of pending events. * `vpoll_close`: This function closes the vpoll file descritor. ## RETURN VALUE `vpoll_create` returns the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately) `vpoll_ctl` and `vpoll_close` return zero in case of success. On error, -1 is returned, and errno is set appropriately. ## EXAMPLE ``` #define _GNU_SOURCE #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int vpollfd = vpoll_create(0, FD_CLOEXEC); int epfd = epoll_create1(EPOLL_CLOEXEC); struct epoll_event reqevents={EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLOUT | EPOLLHUP | EPOLLPRI}; epoll_ctl(epfd, EPOLL_CTL_ADD, vpollfd, &reqevents); switch (fork()) { case 0: sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLIN); sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLIN); sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLOUT); sleep(3); vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLHUP); sleep(3); exit(0); default: while (1) { struct epoll_event ev; int n = epoll_wait(epfd, &ev, 1, 1000); if (n < 0) { perror("epoll_wait"); break; } if (n > 0) { printf("GOT event %x\n", ev.events); vpoll_ctl(vpollfd, VPOLL_CTL_DELEVENTS, ev.events); if (ev.events & EPOLLHUP) break; } else { printf("timeout\n"); } } break; case -1: printf("fork error\n"); } vpoll_close(vpollfd); close(epfd); return 0; } ``` On a machine running a Linux Kernel providing eventfd/vpoll the output of this program is: ``` timeout timeout GOT event 1 timeout timeout GOT event 1 timeout timeout GOT event 4 timeout timeout GOT event 10 ``` Instead when the demo program runs using the emulation layer the output is: ``` timeout timeout GOT event 1 timeout timeout GOT event 1 timeout timeout GOT event 4 timeout timeout GOT event 2011 ``` In fact, the emulator uses a socketpair to generate the events. Hangup is emulated by closing the other end of the socketpair: this generates EPOLLHUP as well as EPOLLIN and EPOLLRDHUP. ## AUTHOR VirtualSquare. Project leader: Renzo Davoli. libvpoll-eventfd-0.1/man/vpoll_close.3000077700000000000000000000000001370063124000212152vpoll.3ustar00rootroot00000000000000libvpoll-eventfd-0.1/man/vpoll_create.3000077700000000000000000000000001370063124000213532vpoll.3ustar00rootroot00000000000000libvpoll-eventfd-0.1/man/vpoll_ctl.3000077700000000000000000000000001370063124000206722vpoll.3ustar00rootroot00000000000000libvpoll-eventfd-0.1/vpoll.c000066400000000000000000000117171370063124000161310ustar00rootroot00000000000000/* * Virtual poll/select/epoll events * * Copyright (C) 2019 Renzo Davoli VirtualSquare team. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include #include #define VPOLLDEV "/dev/vpoll" #ifndef EFD_VPOLL #define EFD_VPOLL (1 << 1) #endif #ifndef VPOLL_IOC_MAGIC #define VPOLL_IOC_MAGIC '^' #endif static FDUSERDATA *fdtable = NULL; static int vpolldev = 0; /************************************ emulation mode ************************************/ static int vpollemu_create(uint32_t init_events, int flags); static int vpollemu_close(int fd); static int vpollemu_ctl(int fd, int op, uint32_t events); /************************************ kernel EFD_VPOLL mode ************************************/ int vpoll_create(uint32_t init_events, int flags) { if (__builtin_expect(fdtable != NULL, 0)) return vpollemu_create(init_events, flags); else if (vpolldev) { int fd = open(VPOLLDEV, O_RDWR | (flags & FD_CLOEXEC ? O_CLOEXEC : 0)); if (fd >= 0) ioctl(fd, _IO(VPOLL_IOC_MAGIC, VPOLL_CTL_ADDEVENTS), init_events); return fd; } else return eventfd(0, EFD_VPOLL | (flags & FD_CLOEXEC ? EFD_CLOEXEC : 0)); } int vpoll_close(int fd) { if (__builtin_expect(fdtable != NULL, 0)) return vpollemu_close(fd); else return close(fd); } int vpoll_ctl(int fd, int op, uint32_t events) { if (__builtin_expect(fdtable != NULL, 0)) return vpollemu_ctl(fd, op, events); else if (vpolldev) return 0 - (ioctl(fd, _IO(VPOLL_IOC_MAGIC, op), events) < 0); else { uint64_t request = (((uint64_t) op) << 32) | events; return write(fd, &request, sizeof(request)) >= 0 ? 0 : -1; } } /************************************ emulation mode ************************************/ static inline void ignore_return(int v) { } static void emu_update_events(int fd, int datafd, uint32_t turnon, uint32_t turnoff) { char buf[4]; //printf("%x %x\n", turnon, turnoff); if (turnon & EPOLLIN) ignore_return(write(datafd, "", 1)); if (turnon & EPOLLOUT) ignore_return(read(datafd, buf, 4)); if (turnoff & EPOLLIN) ignore_return(read(fd, buf, 4)); if (turnoff & EPOLLOUT) { ignore_return(write(fd, "", 1)); ignore_return(write(fd, "", 1)); } if (turnon & EPOLLHUP) shutdown(datafd, SHUT_RDWR); else if (turnon & EPOLLRDHUP) shutdown(datafd, SHUT_WR); } static int vpollemu_create(uint32_t init_events, int flags) { int fds[2]; int rv; int buflen=1; int *datafd; rv = socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, fds); if (rv < 0) return rv; if (flags & FD_CLOEXEC) fcntl(fds[0], F_SETFD, FD_CLOEXEC); fcntl(fds[1], F_SETFD, FD_CLOEXEC); setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &buflen, sizeof(buflen)); setsockopt(fds[1], SOL_SOCKET, SO_SNDBUF, &buflen, sizeof(buflen)); datafd = fduserdata_new(fdtable, fds[0], int); if (datafd == NULL) { close(fds[0]); close(fds[1]); return -1; } *datafd = fds[1]; emu_update_events(fds[0], fds[1], init_events & ~EPOLLOUT, ~init_events & EPOLLOUT); fduserdata_put(datafd); return fds[0]; } static int vpollemu_close(int fd) { int *datafd = fduserdata_get(fdtable, fd); if (datafd == NULL) return -1; close(*datafd); close(fd); fduserdata_del(datafd); return 0; } static int vpollemu_ctl(int fd, int op, uint32_t events) { int *datafd = fduserdata_get(fdtable, fd); if (datafd == NULL) return -1; switch (op) { case VPOLL_CTL_ADDEVENTS: emu_update_events(fd, *datafd, events, 0); break; case VPOLL_CTL_DELEVENTS: emu_update_events(fd, *datafd, 0, events); break; case VPOLL_CTL_SETEVENTS: emu_update_events(fd, *datafd, events, ~events); break; default: errno = EINVAL; return -1; } fduserdata_put(datafd); return 0; } /************************************ init/fini ************************************/ __attribute__((constructor)) static void vpollemu_init() { /* use eventfd EFD_VPOLL when available */ int testfd = eventfd(0, EFD_VPOLL | EFD_CLOEXEC); if (testfd >= 0) close(testfd); else { /* switch to device module mode */ int testfd = open(VPOLLDEV, O_RDWR | O_CLOEXEC); if (testfd >= 0) { vpolldev = 1; close(testfd); } else /* switch to emulation mode if EFD_VPOLL or "/dev/vpoll" are not supported(yet). */ fdtable = fduserdata_create(0); } } __attribute__((destructor)) static void vpollemu_fini() { if (fdtable != NULL) fduserdata_destroy(fdtable); } libvpoll-eventfd-0.1/vpoll.h000066400000000000000000000035261370063124000161350ustar00rootroot00000000000000/* * Virtual poll/select/epoll events * * Copyright (C) 2019 Renzo Davoli VirtualSquare team. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Example of usage: * // generate a file descriptor (you'll use this in poll/select/epoll...) * vpollfd = vpoll_create(EPOLLOUT, FD_CLOEXEC); * // when vpollfd need to signal POLLIN to all the interested poll/select/epoll (if any) * vpoll_ctl(vpollfd, VPOLL_CTL_ADDEVENTS, EPOLLIN); * // clear the EPOLLIN event in this way: * vpoll_ctl(vpollfd, VPOLL_CTL_DELEVENTS, EPOLLIN); * // VPOLL_CTL_SETEVENTS sets all the pending event map * // close vpollfd when it is no longer needed: * vpoll_close(vpollfd); * * This library uses the EFD_VPOLL flag of eventfd(2) where available, otherwise it implements * a user-space emulation based on socketpair(2). * While eventfd provide full support to all the EPOLL events (including those not defined yet), * the emulation code supports EPOLLIN, EPOLLOUT, EPOLLHUP only. * (in emulation mode it is not possible to file further events after an EPOLLHUP). */ #ifndef VPOLLEMU_H #define VPOLLEMU_H #include #define VPOLL_CTL_ADDEVENTS 1 #define VPOLL_CTL_DELEVENTS 2 #define VPOLL_CTL_SETEVENTS 3 int vpoll_create(uint32_t init_events, int flags); int vpoll_ctl(int fd, int op, uint32_t events); int vpoll_close(int fd); #endif libvpoll-eventfd-0.1/vpoll.pc.in000066400000000000000000000005511370063124000167100ustar00rootroot00000000000000prefix=@CMAKE_INSTALL_PREFIX@ exec_prefix="${prefix}/@CMAKE_INSTALL_BINDIR@" libdir="${prefix}/@CMAKE_INSTALL_LIBDIR@" includedir="${prefix}/@CMAKE_INSTALL_INCLUDEDIR@" Name: @PROJECT_NAME@ Description: @PROJECT_DESCRIPTION@ Version: @PROJECT_VERSION@ Requires: @PROJECT_PC_REQUIRES@ Cflags: -I${includedir} Libs: -L${libdir} -l@PROJECT_NAME@ @PRIVATE_LIBS@