Spectrum Development
Threads by month
- ----- 2026 -----
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- 388 discussions
See <https://reuse.software/>. NLnet are quite keen on this.
---
.editorconfig | 3 +
.gitignore | 7 +-
COPYING | 339 ----------------------------------
LICENSES/CC0-1.0.txt | 119 ++++++++++++
LICENSES/GPL-2.0-only.txt | 319 ++++++++++++++++++++++++++++++++
LICENSES/GPL-2.0-or-later.txt | 319 ++++++++++++++++++++++++++++++++
Makefile | 20 +-
README | 6 +
TODO | 3 +
mktuntap.8 | 16 +-
mktuntap.c | 28 +--
11 files changed, 787 insertions(+), 392 deletions(-)
delete mode 100644 COPYING
create mode 100644 LICENSES/CC0-1.0.txt
create mode 100644 LICENSES/GPL-2.0-only.txt
create mode 100644 LICENSES/GPL-2.0-or-later.txt
diff --git a/.editorconfig b/.editorconfig
index 35c5fb2..49f3188 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -1,3 +1,6 @@
+# SPDX-FileCopyrightText: 2019 Alyssa Ross <hi(a)alyssa.is>
+# SPDX-License-Identifier: CC0-1.0
+
root = true
[*]
diff --git a/.gitignore b/.gitignore
index 7e27746..9106912 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,9 @@
+# SPDX-FileCopyrightText: 2019 Alyssa Ross <hi(a)alyssa.is>
+# SPDX-License-Identifier: CC0-1.0
+
result
result-*
*.o
-/mktuntap
\ No newline at end of file
+/mktuntap
+bindunix
+socketpair
diff --git a/COPYING b/COPYING
deleted file mode 100644
index d159169..0000000
--- a/COPYING
+++ /dev/null
@@ -1,339 +0,0 @@
- GNU GENERAL PUBLIC LICENSE
- Version 2, June 1991
-
- Copyright (C) 1989, 1991 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.
-
- Preamble
-
- The licenses for most software are designed to take away your
-freedom to share and change it. By contrast, the GNU General Public
-License is intended to guarantee your freedom to share and change free
-software--to make sure the software is free for all its users. This
-General Public License applies to most of the Free Software
-Foundation's software and to any other program whose authors commit to
-using it. (Some other Free Software Foundation software is covered by
-the GNU Lesser General Public License instead.) You can apply it to
-your programs, too.
-
- When we speak of free software, we are referring to freedom, 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 or use pieces of it
-in new free programs; and that you know you can do these things.
-
- To protect your rights, we need to make restrictions that forbid
-anyone to deny you these rights or to ask you to surrender the rights.
-These restrictions translate to certain responsibilities for you if you
-distribute copies of the software, or if you modify it.
-
- For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must give the recipients all the rights that
-you have. You must make sure that they, too, receive or can get the
-source code. And you must show them these terms so they know their
-rights.
-
- We protect your rights with two steps: (1) copyright the software, and
-(2) offer you this license which gives you legal permission to copy,
-distribute and/or modify the software.
-
- Also, for each author's protection and ours, we want to make certain
-that everyone understands that there is no warranty for this free
-software. If the software is modified by someone else and passed on, we
-want its recipients to know that what they have is not the original, so
-that any problems introduced by others will not reflect on the original
-authors' reputations.
-
- Finally, any free program is threatened constantly by software
-patents. We wish to avoid the danger that redistributors of a free
-program will individually obtain patent licenses, in effect making the
-program proprietary. To prevent this, we have made it clear that any
-patent must be licensed for everyone's free use or not licensed at all.
-
- The precise terms and conditions for copying, distribution and
-modification follow.
-
- GNU GENERAL PUBLIC LICENSE
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
-
- 0. This License applies to any program or other work which contains
-a notice placed by the copyright holder saying it may be distributed
-under the terms of this General Public License. The "Program", below,
-refers to any such program or work, and a "work based on the Program"
-means either the Program or any derivative work under copyright law:
-that is to say, a work containing the Program or a portion of it,
-either verbatim or with modifications and/or translated into another
-language. (Hereinafter, translation is included without limitation in
-the term "modification".) Each licensee is addressed as "you".
-
-Activities other than copying, distribution and modification are not
-covered by this License; they are outside its scope. The act of
-running the Program is not restricted, and the output from the Program
-is covered only if its contents constitute a work based on the
-Program (independent of having been made by running the Program).
-Whether that is true depends on what the Program does.
-
- 1. You may copy and distribute verbatim copies of the Program's
-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 give any other recipients of the Program a copy of this License
-along with the Program.
-
-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 Program or any portion
-of it, thus forming a work based on the Program, 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) You must cause the modified files to carry prominent notices
- stating that you changed the files and the date of any change.
-
- b) You must cause any work that you distribute or publish, that in
- whole or in part contains or is derived from the Program or any
- part thereof, to be licensed as a whole at no charge to all third
- parties under the terms of this License.
-
- c) If the modified program normally reads commands interactively
- when run, you must cause it, when started running for such
- interactive use in the most ordinary way, to print or display an
- announcement including an appropriate copyright notice and a
- notice that there is no warranty (or else, saying that you provide
- a warranty) and that users may redistribute the program under
- these conditions, and telling the user how to view a copy of this
- License. (Exception: if the Program itself is interactive but
- does not normally print such an announcement, your work based on
- the Program is not required to print an announcement.)
-
-These requirements apply to the modified work as a whole. If
-identifiable sections of that work are not derived from the Program,
-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 Program, 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 Program.
-
-In addition, mere aggregation of another work not based on the Program
-with the Program (or with a work based on the Program) on a volume of
-a storage or distribution medium does not bring the other work under
-the scope of this License.
-
- 3. You may copy and distribute the Program (or a work based on it,
-under Section 2) in object code or executable form under the terms of
-Sections 1 and 2 above provided that you also do one of the following:
-
- a) 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; or,
-
- b) Accompany it with a written offer, valid for at least three
- years, to give any third party, for a charge no more than your
- cost of physically performing source distribution, a complete
- machine-readable copy of the corresponding source code, to be
- distributed under the terms of Sections 1 and 2 above on a medium
- customarily used for software interchange; or,
-
- c) Accompany it with the information you received as to the offer
- to distribute corresponding source code. (This alternative is
- allowed only for noncommercial distribution and only if you
- received the program in object code or executable form with such
- an offer, in accord with Subsection b above.)
-
-The source code for a work means the preferred form of the work for
-making modifications to it. For an executable work, 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 executable. However, as a
-special exception, the source code 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.
-
-If distribution of executable or 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 counts as
-distribution of the source code, even though third parties are not
-compelled to copy the source along with the object code.
-
- 4. You may not copy, modify, sublicense, or distribute the Program
-except as expressly provided under this License. Any attempt
-otherwise to copy, modify, sublicense or distribute the Program 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.
-
- 5. 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 Program or its derivative works. These actions are
-prohibited by law if you do not accept this License. Therefore, by
-modifying or distributing the Program (or any work based on the
-Program), you indicate your acceptance of this License to do so, and
-all its terms and conditions for copying, distributing or modifying
-the Program or works based on it.
-
- 6. Each time you redistribute the Program (or any work based on the
-Program), the recipient automatically receives a license from the
-original licensor to copy, distribute or modify the Program 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 to
-this License.
-
- 7. 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 Program at all. For example, if a patent
-license would not permit royalty-free redistribution of the Program 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 Program.
-
-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.
-
- 8. If the distribution and/or use of the Program is restricted in
-certain countries either by patents or by copyrighted interfaces, the
-original copyright holder who places the Program 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.
-
- 9. The Free Software Foundation may publish revised and/or new versions
-of the 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 Program
-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 Program does not specify a version number of
-this License, you may choose any version ever published by the Free Software
-Foundation.
-
- 10. If you wish to incorporate parts of the Program into other free
-programs whose distribution conditions are different, 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
-
- 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
-FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
-OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
-PROVIDE THE PROGRAM "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 PROGRAM IS WITH YOU. SHOULD THE
-PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
-REPAIR OR CORRECTION.
-
- 12. 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 PROGRAM 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 PROGRAM (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 PROGRAM TO OPERATE WITH ANY OTHER
-PROGRAMS), 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 Programs
-
- If you develop a new program, and you want it to be of the greatest
-possible use to the public, the best way to achieve this is to make it
-free software which everyone can redistribute and change under these terms.
-
- To do so, attach the following notices to the program. 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.
-
- <one line to give the program's name and a brief idea of what it does.>
- Copyright (C) <year> <name of author>
-
- This program is free software; 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.
-
- This program 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 program; 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.
-
-If the program is interactive, make it output a short notice like this
-when it starts in an interactive mode:
-
- Gnomovision version 69, Copyright (C) year name of author
- Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
- This is free software, and you are welcome to redistribute it
- under certain conditions; type `show c' for details.
-
-The hypothetical commands `show w' and `show c' should show the appropriate
-parts of the General Public License. Of course, the commands you use may
-be called something other than `show w' and `show c'; they could even be
-mouse-clicks or menu items--whatever suits your program.
-
-You should also get your employer (if you work as a programmer) or your
-school, if any, to sign a "copyright disclaimer" for the program, if
-necessary. Here is a sample; alter the names:
-
- Yoyodyne, Inc., hereby disclaims all copyright interest in the program
- `Gnomovision' (which makes passes at compilers) written by James Hacker.
-
- <signature of Ty Coon>, 1 April 1989
- Ty Coon, President of Vice
-
-This General Public License does not permit incorporating your program into
-proprietary programs. If your program is a subroutine library, you may
-consider it more useful to permit linking proprietary applications with the
-library. If this is what you want to do, use the GNU Lesser General
-Public License instead of this License.
diff --git a/LICENSES/CC0-1.0.txt b/LICENSES/CC0-1.0.txt
new file mode 100644
index 0000000..a343ccd
--- /dev/null
+++ b/LICENSES/CC0-1.0.txt
@@ -0,0 +1,119 @@
+Creative Commons Legal Code
+
+CC0 1.0 Universal CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES
+NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE
+AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION
+ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE
+OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS
+LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION
+OR WORKS PROVIDED HEREUNDER.
+
+Statement of Purpose
+
+The laws of most jurisdictions throughout the world automatically confer exclusive
+Copyright and Related Rights (defined below) upon the creator and subsequent
+owner(s) (each and all, an "owner") of an original work of authorship and/or
+a database (each, a "Work").
+
+Certain owners wish to permanently relinquish those rights to a Work for the
+purpose of contributing to a commons of creative, cultural and scientific
+works ("Commons") that the public can reliably and without fear of later claims
+of infringement build upon, modify, incorporate in other works, reuse and
+redistribute as freely as possible in any form whatsoever and for any purposes,
+including without limitation commercial purposes. These owners may contribute
+to the Commons to promote the ideal of a free culture and the further production
+of creative, cultural and scientific works, or to gain reputation or greater
+distribution for their Work in part through the use and efforts of others.
+
+For these and/or other purposes and motivations, and without any expectation
+of additional consideration or compensation, the person associating CC0 with
+a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
+and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
+and publicly distribute the Work under its terms, with knowledge of his or
+her Copyright and Related Rights in the Work and the meaning and intended
+legal effect of CC0 on those rights.
+
+1. Copyright and Related Rights. A Work made available under CC0 may be protected
+by copyright and related or neighboring rights ("Copyright and Related Rights").
+Copyright and Related Rights include, but are not limited to, the following:
+
+i. the right to reproduce, adapt, distribute, perform, display, communicate,
+and translate a Work;
+
+ ii. moral rights retained by the original author(s) and/or performer(s);
+
+iii. publicity and privacy rights pertaining to a person's image or likeness
+depicted in a Work;
+
+iv. rights protecting against unfair competition in regards to a Work, subject
+to the limitations in paragraph 4(a), below;
+
+v. rights protecting the extraction, dissemination, use and reuse of data
+in a Work;
+
+vi. database rights (such as those arising under Directive 96/9/EC of the
+European Parliament and of the Council of 11 March 1996 on the legal protection
+of databases, and under any national implementation thereof, including any
+amended or successor version of such directive); and
+
+vii. other similar, equivalent or corresponding rights throughout the world
+based on applicable law or treaty, and any national implementations thereof.
+
+2. Waiver. To the greatest extent permitted by, but not in contravention of,
+applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
+unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
+and Related Rights and associated claims and causes of action, whether now
+known or unknown (including existing as well as future claims and causes of
+action), in the Work (i) in all territories worldwide, (ii) for the maximum
+duration provided by applicable law or treaty (including future time extensions),
+(iii) in any current or future medium and for any number of copies, and (iv)
+for any purpose whatsoever, including without limitation commercial, advertising
+or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the
+benefit of each member of the public at large and to the detriment of Affirmer's
+heirs and successors, fully intending that such Waiver shall not be subject
+to revocation, rescission, cancellation, termination, or any other legal or
+equitable action to disrupt the quiet enjoyment of the Work by the public
+as contemplated by Affirmer's express Statement of Purpose.
+
+3. Public License Fallback. Should any part of the Waiver for any reason be
+judged legally invalid or ineffective under applicable law, then the Waiver
+shall be preserved to the maximum extent permitted taking into account Affirmer's
+express Statement of Purpose. In addition, to the extent the Waiver is so
+judged Affirmer hereby grants to each affected person a royalty-free, non
+transferable, non sublicensable, non exclusive, irrevocable and unconditional
+license to exercise Affirmer's Copyright and Related Rights in the Work (i)
+in all territories worldwide, (ii) for the maximum duration provided by applicable
+law or treaty (including future time extensions), (iii) in any current or
+future medium and for any number of copies, and (iv) for any purpose whatsoever,
+including without limitation commercial, advertising or promotional purposes
+(the "License"). The License shall be deemed effective as of the date CC0
+was applied by Affirmer to the Work. Should any part of the License for any
+reason be judged legally invalid or ineffective under applicable law, such
+partial invalidity or ineffectiveness shall not invalidate the remainder of
+the License, and in such case Affirmer hereby affirms that he or she will
+not (i) exercise any of his or her remaining Copyright and Related Rights
+in the Work or (ii) assert any associated claims and causes of action with
+respect to the Work, in either case contrary to Affirmer's express Statement
+of Purpose.
+
+ 4. Limitations and Disclaimers.
+
+a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered,
+licensed or otherwise affected by this document.
+
+b. Affirmer offers the Work as-is and makes no representations or warranties
+of any kind concerning the Work, express, implied, statutory or otherwise,
+including without limitation warranties of title, merchantability, fitness
+for a particular purpose, non infringement, or the absence of latent or other
+defects, accuracy, or the present or absence of errors, whether or not discoverable,
+all to the greatest extent permissible under applicable law.
+
+c. Affirmer disclaims responsibility for clearing rights of other persons
+that may apply to the Work or any use thereof, including without limitation
+any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims
+responsibility for obtaining any necessary consents, permissions or other
+rights required for any use of the Work.
+
+d. Affirmer understands and acknowledges that Creative Commons is not a party
+to this document and has no duty or obligation with respect to this CC0 or
+use of the Work.
diff --git a/LICENSES/GPL-2.0-only.txt b/LICENSES/GPL-2.0-only.txt
new file mode 100644
index 0000000..0f3d641
--- /dev/null
+++ b/LICENSES/GPL-2.0-only.txt
@@ -0,0 +1,319 @@
+GNU GENERAL PUBLIC LICENSE
+
+Version 2, June 1991
+
+Copyright (C) 1989, 1991 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.
+
+Preamble
+
+The licenses for most software are designed to take away your freedom to share
+and change it. By contrast, the GNU General Public License is intended to
+guarantee your freedom to share and change free software--to make sure the
+software is free for all its users. This General Public License applies to
+most of the Free Software Foundation's software and to any other program whose
+authors commit to using it. (Some other Free Software Foundation software
+is covered by the GNU Lesser General Public License instead.) You can apply
+it to your programs, too.
+
+When we speak of free software, we are referring to freedom, 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 or use pieces of it in new free programs; and that
+you know you can do these things.
+
+To protect your rights, we need to make restrictions that forbid anyone to
+deny you these rights or to ask you to surrender the rights. These restrictions
+translate to certain responsibilities for you if you distribute copies of
+the software, or if you modify it.
+
+For example, if you distribute copies of such a program, whether gratis or
+for a fee, you must give the recipients all the rights that you have. You
+must make sure that they, too, receive or can get the source code. And you
+must show them these terms so they know their rights.
+
+We protect your rights with two steps: (1) copyright the software, and (2)
+offer you this license which gives you legal permission to copy, distribute
+and/or modify the software.
+
+Also, for each author's protection and ours, we want to make certain that
+everyone understands that there is no warranty for this free software. If
+the software is modified by someone else and passed on, we want its recipients
+to know that what they have is not the original, so that any problems introduced
+by others will not reflect on the original authors' reputations.
+
+Finally, any free program is threatened constantly by software patents. We
+wish to avoid the danger that redistributors of a free program will individually
+obtain patent licenses, in effect making the program proprietary. To prevent
+this, we have made it clear that any patent must be licensed for everyone's
+free use or not licensed at all.
+
+The precise terms and conditions for copying, distribution and modification
+follow.
+
+TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+0. This License applies to any program or other work which contains a notice
+placed by the copyright holder saying it may be distributed under the terms
+of this General Public License. The "Program", below, refers to any such program
+or work, and a "work based on the Program" means either the Program or any
+derivative work under copyright law: that is to say, a work containing the
+Program or a portion of it, either verbatim or with modifications and/or translated
+into another language. (Hereinafter, translation is included without limitation
+in the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not covered
+by this License; they are outside its scope. The act of running the Program
+is not restricted, and the output from the Program is covered only if its
+contents constitute a work based on the Program (independent of having been
+made by running the Program). Whether that is true depends on what the Program
+does.
+
+1. You may copy and distribute verbatim copies of the Program's 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 give any other recipients of the Program a copy of this
+License along with the Program.
+
+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 Program or any portion of it,
+thus forming a work based on the Program, 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) You must cause the modified files to carry prominent notices stating that
+you changed the files and the date of any change.
+
+b) You must cause any work that you distribute or publish, that in whole or
+in part contains or is derived from the Program or any part thereof, to be
+licensed as a whole at no charge to all third parties under the terms of this
+License.
+
+c) If the modified program normally reads commands interactively when run,
+you must cause it, when started running for such interactive use in the most
+ordinary way, to print or display an announcement including an appropriate
+copyright notice and a notice that there is no warranty (or else, saying that
+you provide a warranty) and that users may redistribute the program under
+these conditions, and telling the user how to view a copy of this License.
+(Exception: if the Program itself is interactive but does not normally print
+such an announcement, your work based on the Program is not required to print
+an announcement.)
+
+These requirements apply to the modified work as a whole. If identifiable
+sections of that work are not derived from the Program, 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 Program, 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 Program.
+
+In addition, mere aggregation of another work not based on the Program with
+the Program (or with a work based on the Program) on a volume of a storage
+or distribution medium does not bring the other work under the scope of this
+License.
+
+3. You may copy and distribute the Program (or a work based on it, under Section
+2) in object code or executable form under the terms of Sections 1 and 2 above
+provided that you also do one of the following:
+
+a) 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; or,
+
+b) Accompany it with a written offer, valid for at least three years, to give
+any third party, for a charge no more than your cost of physically performing
+source distribution, a complete machine-readable copy of the corresponding
+source code, to be distributed under the terms of Sections 1 and 2 above on
+a medium customarily used for software interchange; or,
+
+c) Accompany it with the information you received as to the offer to distribute
+corresponding source code. (This alternative is allowed only for noncommercial
+distribution and only if you received the program in object code or executable
+form with such an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for making
+modifications to it. For an executable work, 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 executable. However, as a special exception, the source code 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.
+
+If distribution of executable or 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 counts as distribution of the source code,
+even though third parties are not compelled to copy the source along with
+the object code.
+
+4. You may not copy, modify, sublicense, or distribute the Program except
+as expressly provided under this License. Any attempt otherwise to copy, modify,
+sublicense or distribute the Program 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.
+
+5. 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
+Program or its derivative works. These actions are prohibited by law if you
+do not accept this License. Therefore, by modifying or distributing the Program
+(or any work based on the Program), you indicate your acceptance of this License
+to do so, and all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+6. Each time you redistribute the Program (or any work based on the Program),
+the recipient automatically receives a license from the original licensor
+to copy, distribute or modify the Program 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 to this License.
+
+7. 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 Program at all. For example, if a
+patent license would not permit royalty-free redistribution of the Program
+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 Program.
+
+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.
+
+8. If the distribution and/or use of the Program is restricted in certain
+countries either by patents or by copyrighted interfaces, the original copyright
+holder who places the Program 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.
+
+9. The Free Software Foundation may publish revised and/or new versions of
+the 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 Program 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
+Program does not specify a version number of this License, you may choose
+any version ever published by the Free Software Foundation.
+
+10. If you wish to incorporate parts of the Program into other free programs
+whose distribution conditions are different, 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
+
+11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
+THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
+STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM
+"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 PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+12. 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 PROGRAM 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 PROGRAM (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 PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 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 Programs
+
+If you develop a new program, and you want it to be of the greatest possible
+use to the public, the best way to achieve this is to make it free software
+which everyone can redistribute and change under these terms.
+
+To do so, attach the following notices to the program. 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.
+
+<one line to give the program's name and an idea of what it does.>
+
+Copyright (C)< yyyy> <name of author>
+
+This program is free software; 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.
+
+This program 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 program; 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.
+
+If the program is interactive, make it output a short notice like this when
+it starts in an interactive mode:
+
+Gnomovision version 69, Copyright (C) year name of author Gnomovision comes
+with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software,
+and you are welcome to redistribute it under certain conditions; type `show
+c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may be
+called something other than `show w' and `show c'; they could even be mouse-clicks
+or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your school,
+if any, to sign a "copyright disclaimer" for the program, if necessary. Here
+is a sample; alter the names:
+
+Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision'
+(which makes passes at compilers) written by James Hacker.
+
+<signature of Ty Coon >, 1 April 1989 Ty Coon, President of Vice This General
+Public License does not permit incorporating your program into proprietary
+programs. If your program is a subroutine library, you may consider it more
+useful to permit linking proprietary applications with the library. If this
+is what you want to do, use the GNU Lesser General Public License instead
+of this License.
diff --git a/LICENSES/GPL-2.0-or-later.txt b/LICENSES/GPL-2.0-or-later.txt
new file mode 100644
index 0000000..1d80ac3
--- /dev/null
+++ b/LICENSES/GPL-2.0-or-later.txt
@@ -0,0 +1,319 @@
+GNU GENERAL PUBLIC LICENSE
+
+Version 2, June 1991
+
+Copyright (C) 1989, 1991 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.
+
+Preamble
+
+The licenses for most software are designed to take away your freedom to share
+and change it. By contrast, the GNU General Public License is intended to
+guarantee your freedom to share and change free software--to make sure the
+software is free for all its users. This General Public License applies to
+most of the Free Software Foundation's software and to any other program whose
+authors commit to using it. (Some other Free Software Foundation software
+is covered by the GNU Lesser General Public License instead.) You can apply
+it to your programs, too.
+
+When we speak of free software, we are referring to freedom, 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 or use pieces of it in new free programs; and that
+you know you can do these things.
+
+To protect your rights, we need to make restrictions that forbid anyone to
+deny you these rights or to ask you to surrender the rights. These restrictions
+translate to certain responsibilities for you if you distribute copies of
+the software, or if you modify it.
+
+For example, if you distribute copies of such a program, whether gratis or
+for a fee, you must give the recipients all the rights that you have. You
+must make sure that they, too, receive or can get the source code. And you
+must show them these terms so they know their rights.
+
+We protect your rights with two steps: (1) copyright the software, and (2)
+offer you this license which gives you legal permission to copy, distribute
+and/or modify the software.
+
+Also, for each author's protection and ours, we want to make certain that
+everyone understands that there is no warranty for this free software. If
+the software is modified by someone else and passed on, we want its recipients
+to know that what they have is not the original, so that any problems introduced
+by others will not reflect on the original authors' reputations.
+
+Finally, any free program is threatened constantly by software patents. We
+wish to avoid the danger that redistributors of a free program will individually
+obtain patent licenses, in effect making the program proprietary. To prevent
+this, we have made it clear that any patent must be licensed for everyone's
+free use or not licensed at all.
+
+The precise terms and conditions for copying, distribution and modification
+follow.
+
+TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+0. This License applies to any program or other work which contains a notice
+placed by the copyright holder saying it may be distributed under the terms
+of this General Public License. The "Program", below, refers to any such program
+or work, and a "work based on the Program" means either the Program or any
+derivative work under copyright law: that is to say, a work containing the
+Program or a portion of it, either verbatim or with modifications and/or translated
+into another language. (Hereinafter, translation is included without limitation
+in the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not covered
+by this License; they are outside its scope. The act of running the Program
+is not restricted, and the output from the Program is covered only if its
+contents constitute a work based on the Program (independent of having been
+made by running the Program). Whether that is true depends on what the Program
+does.
+
+1. You may copy and distribute verbatim copies of the Program's 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 give any other recipients of the Program a copy of this
+License along with the Program.
+
+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 Program or any portion of it,
+thus forming a work based on the Program, 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) You must cause the modified files to carry prominent notices stating that
+you changed the files and the date of any change.
+
+b) You must cause any work that you distribute or publish, that in whole or
+in part contains or is derived from the Program or any part thereof, to be
+licensed as a whole at no charge to all third parties under the terms of this
+License.
+
+c) If the modified program normally reads commands interactively when run,
+you must cause it, when started running for such interactive use in the most
+ordinary way, to print or display an announcement including an appropriate
+copyright notice and a notice that there is no warranty (or else, saying that
+you provide a warranty) and that users may redistribute the program under
+these conditions, and telling the user how to view a copy of this License.
+(Exception: if the Program itself is interactive but does not normally print
+such an announcement, your work based on the Program is not required to print
+an announcement.)
+
+These requirements apply to the modified work as a whole. If identifiable
+sections of that work are not derived from the Program, 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 Program, 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 Program.
+
+In addition, mere aggregation of another work not based on the Program with
+the Program (or with a work based on the Program) on a volume of a storage
+or distribution medium does not bring the other work under the scope of this
+License.
+
+3. You may copy and distribute the Program (or a work based on it, under Section
+2) in object code or executable form under the terms of Sections 1 and 2 above
+provided that you also do one of the following:
+
+a) 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; or,
+
+b) Accompany it with a written offer, valid for at least three years, to give
+any third party, for a charge no more than your cost of physically performing
+source distribution, a complete machine-readable copy of the corresponding
+source code, to be distributed under the terms of Sections 1 and 2 above on
+a medium customarily used for software interchange; or,
+
+c) Accompany it with the information you received as to the offer to distribute
+corresponding source code. (This alternative is allowed only for noncommercial
+distribution and only if you received the program in object code or executable
+form with such an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for making
+modifications to it. For an executable work, 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 executable. However, as a special exception, the source code 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.
+
+If distribution of executable or 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 counts as distribution of the source code,
+even though third parties are not compelled to copy the source along with
+the object code.
+
+4. You may not copy, modify, sublicense, or distribute the Program except
+as expressly provided under this License. Any attempt otherwise to copy, modify,
+sublicense or distribute the Program 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.
+
+5. 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
+Program or its derivative works. These actions are prohibited by law if you
+do not accept this License. Therefore, by modifying or distributing the Program
+(or any work based on the Program), you indicate your acceptance of this License
+to do so, and all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+6. Each time you redistribute the Program (or any work based on the Program),
+the recipient automatically receives a license from the original licensor
+to copy, distribute or modify the Program 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 to this License.
+
+7. 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 Program at all. For example, if a
+patent license would not permit royalty-free redistribution of the Program
+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 Program.
+
+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.
+
+8. If the distribution and/or use of the Program is restricted in certain
+countries either by patents or by copyrighted interfaces, the original copyright
+holder who places the Program 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.
+
+9. The Free Software Foundation may publish revised and/or new versions of
+the 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 Program 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
+Program does not specify a version number of this License, you may choose
+any version ever published by the Free Software Foundation.
+
+10. If you wish to incorporate parts of the Program into other free programs
+whose distribution conditions are different, 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
+
+11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
+THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
+STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM
+"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 PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+12. 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 PROGRAM 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 PROGRAM (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 PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 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 Programs
+
+If you develop a new program, and you want it to be of the greatest possible
+use to the public, the best way to achieve this is to make it free software
+which everyone can redistribute and change under these terms.
+
+To do so, attach the following notices to the program. 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.
+
+<one line to give the program's name and an idea of what it does.>
+
+Copyright (C) <yyyy> <name of author>
+
+This program is free software; 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.
+
+This program 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 program; 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.
+
+If the program is interactive, make it output a short notice like this when
+it starts in an interactive mode:
+
+Gnomovision version 69, Copyright (C) year name of author Gnomovision comes
+with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software,
+and you are welcome to redistribute it under certain conditions; type `show
+c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may be
+called something other than `show w' and `show c'; they could even be mouse-clicks
+or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your school,
+if any, to sign a "copyright disclaimer" for the program, if necessary. Here
+is a sample; alter the names:
+
+Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision'
+(which makes passes at compilers) written by James Hacker.
+
+<signature of Ty Coon>, 1 April 1989 Ty Coon, President of Vice This General
+Public License does not permit incorporating your program into proprietary
+programs. If your program is a subroutine library, you may consider it more
+useful to permit linking proprietary applications with the library. If this
+is what you want to do, use the GNU Lesser General Public License instead
+of this License.
diff --git a/Makefile b/Makefile
index ff52ac4..9e6c8aa 100644
--- a/Makefile
+++ b/Makefile
@@ -1,21 +1,7 @@
+# SPDX-FileCopyrightText: 2019 Alyssa Ross <hi(a)alyssa.is>
# SPDX-License-Identifier: GPL-2.0-or-later
-# Copyright 2019 Alyssa Ross
-#
-# This program is free software: 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.
-#
-# This program 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 program. If not, see <https://www.gnu.org/licenses/>.
-
-all: mktuntap
+all: bindunix mktuntap socketpair
.PHONY: all
prefix = /usr/local
@@ -32,6 +18,8 @@ MKDIR_P = mkdir -p
CFLAGS = -g
+bindunix socketpair: util.o
+
install-dirs:
$(MKDIR_P) $(DESTDIR)$(bindir) $(DESTDIR)$(man8dir)
.PHONY: install-dirs
diff --git a/README b/README
index 5f19bec..5a62791 100644
--- a/README
+++ b/README
@@ -1,3 +1,6 @@
+SPDX-FileCopyrightText: 2019-2020 Alyssa Ross <hi(a)alyssa.is>
+SPDX-License-Identifier: GPL-2.0-or-later
+
mktuntap
--------
@@ -19,3 +22,6 @@ published by the Free Software Foundation.
Other source files are licensed under 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.
+
+Some utility files (.gitignore, etc.) are licensed under the CC0
+1.0 Universal Public Domain Dedication.
diff --git a/TODO b/TODO
index 7a72fe4..a3dd911 100644
--- a/TODO
+++ b/TODO
@@ -1 +1,4 @@
+SPDX-FileCopyrightText: 2019 Alyssa Ross <hi(a)alyssa.is>
+SPDX-License-Identifier: CC0-1.0
+
* Reimplement tap_alloc for compatibility with newer GPLs
diff --git a/mktuntap.8 b/mktuntap.8
index 9bbfb78..0f9aaf8 100644
--- a/mktuntap.8
+++ b/mktuntap.8
@@ -1,19 +1,5 @@
+.\" SPDX-FileCopyrightText: 2019 Alyssa Ross <hi(a)alyssa.is>
.\" SPDX-License-Identifier: GPL-2.0-or-later
-.\"
-.\" Copyright 2019 Alyssa Ross
-.\"
-.\" This program is free software: 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.
-.\"
-.\" This program 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 program. If not, see <https://www.gnu.org/licenses/>.
.Dd December 15, 2019
.Dt MKTUNTAP 8
.Os Linux
diff --git a/mktuntap.c b/mktuntap.c
index 53a47de..b150dc7 100644
--- a/mktuntap.c
+++ b/mktuntap.c
@@ -1,20 +1,6 @@
+// SPDX-FileCopyrightText: 2019 Alyssa Ross <hi(a)alyssa.is>
// SPDX-License-Identifier: GPL-2.0-only
-/*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program 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 program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
@@ -116,7 +102,7 @@ int main(int argc, char **argv)
char *target_fd_end;
long int target_fd_l = strtol(argv[optind], &target_fd_end, 10);
if (errno) {
- fprintf(stderr, "mktuntap: %s\n", strerror(errno));
+ perror("mktuntap");
return EX_USAGE;
}
if (argv[optind][0] == '\0' || target_fd_end[0] != '\0') {
@@ -139,30 +125,30 @@ int main(int argc, char **argv)
ifr_flags_ |= IFF_VNET_HDR;
int tuntap_fd = tuntap_alloc(ifr_name_, open_flags, ifr_flags_);
if (tuntap_fd < 0) {
- fprintf(stderr, "mktuntap: failed to open tuntap device: %s\n", strerror(errno));
+ perror("mktuntap: failed to open tuntap device");
return EX_IOERR;
}
if (tuntap_fd != target_fd) {
if (dup2(tuntap_fd, target_fd) == -1) {
- fprintf(stderr, "mktuntap: %s\n", strerror(errno));
+ perror("mktuntap");
return EX_IOERR;
}
if (close(tuntap_fd) == -1) {
- fprintf(stderr, "mktuntap: %s\n", strerror(errno));
+ perror("mktuntap");
return EX_IOERR;
}
}
if (name_env) {
if (setenv("TUNTAP_NAME", ifr_name_, 1) == -1) {
- fprintf(stderr, "mktuntap: %s\n", strerror(errno));
+ perror("mktunap");
return EX_OSERR;
}
}
execvp(argv[optind + 1], &argv[optind + 1]);
- fprintf(stderr, "failed to exec: %s\n", strerror(errno));
+ perror("failed to exec");
return EX_OSERR;
}
--
2.27.0
2
3
This patch adds libvirtio_wl, which exposes reimplementations of the
sendmsg(2) and recvmsg(2) for virtio_wl socket fds. Tests for as much
libvirtio_wl functionality as is reasonably possible to test without
requiring a working virtio_wl connection are included. (Testing
further would require running tests in a VM so that they could talk to
the virtio_wl kernel driver, which would be prohibitively complex.
virtio_wl socket fds do not actually point to sockets, but to special
virtio_wl files. Whenever a display socket operation calls sendmsg()
or recvmsg() with a file descriptor and receives an ENOTSOCK error, it
retries the operation with the equivalent libvirtio_wl function, in
case the file descriptor is a virtio_wl. This is the least invasive
way to implement virtio_wl support -- if a normal socket is being
used, there will be no change in behaviour.
Because virtio_wl doesn't implement every socket feature, some
workarounds are currently required to accomodate everything Wayland
expects of sockets:
* virtio_wl_recvmsg implements the MSG_DONTWAIT flag by setting
O_NONBLOCK on the fd, attempting the VIRTWL_IOCTL_RECV operation,
and then restoring the fd's original flags. This is obviously
race-prone, but there is no reasonable alternative at present.
* virtio_wl_sendmsg requires MSG_NOSIGNAL to be set, and ignores it.
This is because I think from looking at the code that virtio_wl does
not generate SIGPIPE signals, nor does it ever return EPIPE. I
could be wrong about this, though.
* virtio_wl does not support credential passing -- what would it even
mean, considering the other end of the connection is on
another (virtual) machine? So wl_client's ucred member will have
pid, uid, and gid all set to -1 for a client connected over
virtio_wl.
* virtio_wl sockets do not support accept(2), so a fallback is used.
A proxy program on the host accept(2) on a host socket. When it
receives a connection, it attaches the connection socket to the VM,
then sends the name of the connected socket over the Wayland display
socket. Wayland then receives this name, looks up the connection
socket, and uses that as the client connection socket.
Additionally, virtio_wl memfd-like file descriptors don't support
mremap(2), so for virtio_wl sockets Wayland will munmap(2) the memfd,
and then mmap(2) it again. This should be at least mostly okay
because Wayland only ever calls mremap with MREMAP_MAYMOVE, but it is
still race-prone. To be able to do this, memfds are no longer closed
after being mmaped, but are kept around in the wl_shm_pool struct, so
that they can be passed to mmap() if required.
---
src/connection.c | 4 +
src/meson.build | 16 +-
src/virtio_wl.c | 344 +++++++++++++++++++++++
src/virtio_wl.h | 23 ++
src/wayland-os.c | 23 ++
src/wayland-os.h | 3 +
src/wayland-server.c | 20 +-
src/wayland-shm.c | 24 +-
tests/meson.build | 1 +
tests/virtio_wl-test.c | 615 +++++++++++++++++++++++++++++++++++++++++
10 files changed, 1064 insertions(+), 9 deletions(-)
create mode 100644 src/virtio_wl.c
create mode 100644 src/virtio_wl.h
create mode 100644 tests/virtio_wl-test.c
diff --git a/src/connection.c b/src/connection.c
index d0c7d9f..1bfbd8c 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -40,6 +40,7 @@
#include <time.h>
#include <ffi.h>
+#include "virtio_wl.h"
#include "wayland-util.h"
#include "wayland-private.h"
#include "wayland-os.h"
@@ -314,6 +315,9 @@ wl_connection_flush(struct wl_connection *connection)
do {
len = sendmsg(connection->fd, &msg,
MSG_NOSIGNAL | MSG_DONTWAIT);
+ if (len == -1 && errno == ENOTSOCK)
+ len = virtio_wl_sendmsg(connection->fd, &msg,
+ MSG_NOSIGNAL | MSG_DONTWAIT);
} while (len == -1 && errno == EINTR);
if (len == -1)
diff --git a/src/meson.build b/src/meson.build
index 2d1485c..cca1d93 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -71,13 +71,26 @@ if get_option('libraries')
mathlib_dep = cc.find_library('m', required: false)
threads_dep = dependency('threads', required: false)
+ virtio_wl = library(
+ 'virtio_wl',
+ sources: [
+ 'virtio_wl.c',
+ ],
+ version: '0.1.0',
+ install: true,
+ )
+
+ virtio_wl_dep = declare_dependency(
+ link_with: virtio_wl,
+ )
+
wayland_private = static_library(
'wayland-private',
sources: [
'connection.c',
'wayland-os.c'
],
- dependencies: [ ffi_dep, ]
+ dependencies: [ ffi_dep, virtio_wl_dep ]
)
wayland_private_dep = declare_dependency(
@@ -230,5 +243,6 @@ if get_option('libraries')
'wayland-server-core.h',
'wayland-client.h',
'wayland-client-core.h',
+ 'virtio_wl.h',
])
endif
diff --git a/src/virtio_wl.c b/src/virtio_wl.c
new file mode 100644
index 0000000..a1ee8ee
--- /dev/null
+++ b/src/virtio_wl.c
@@ -0,0 +1,344 @@
+/* Copyright 2020 Alyssa Ross
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+#define _POSIX_C_SOURCE 200809L
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <linux/virtwl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "virtio_wl.h"
+
+// This is essentially vendored reusable library code, so I consider
+// it exempt from the Wayland style guide. :)
+
+#if defined(__GNUC__) && __GNUC__ >= 4
+#define VIRTIO_WL_EXPORT __attribute__ ((visibility("default")))
+#else
+#define VIRTIO_WL_EXPORT
+#endif
+
+static int
+set_nonblocking(int fd)
+{
+ int fl = fcntl(fd, F_GETFL);
+ if (fl == -1)
+ return -1;
+ if (!(fl & O_NONBLOCK))
+ if (fcntl(fd, F_SETFL, fl | O_NONBLOCK) == -1)
+ return -1;
+ return fl;
+}
+
+// Returns the total size of all buffers in an iovec.
+// A return value of -1 means that the total overflowed.
+static ssize_t
+iov_len(const struct iovec iov[], size_t n)
+{
+ size_t len = 0;
+ for (size_t i = 0; i < n; i++) {
+ if (SSIZE_MAX - len < iov[i].iov_len)
+ return -1;
+ len += iov[i].iov_len;
+ }
+ return len;
+}
+
+// Copies from an iovec array into a buffer.
+// The buffer is assumed to be large enough to hold all the data.
+// This length can be calculated with the iov_len function.
+static size_t
+iov_flatten(void *buf, size_t buflen, const struct iovec *iov, size_t iovlen)
+{
+ size_t off = 0;
+
+ for (size_t index = 0; index < iovlen && off < buflen; index++) {
+ const struct iovec *i = &iov[index];
+ size_t rem = buflen - off;
+ size_t len = i->iov_len < rem ? i->iov_len : rem;
+
+ memcpy((unsigned char *)buf + off, i->iov_base, len);
+ off += len;
+ }
+
+ return off;
+}
+
+// Copies from a buffer into an iovec array.
+// Returns number of bytes copied.
+static size_t
+iov_fill(struct iovec *iov, size_t iovlen, const void *buf, size_t buflen)
+{
+ size_t off = 0;
+
+ for (size_t index = 0; index < iovlen && off < buflen; index++) {
+ struct iovec *i = &iov[index];
+ size_t rem = buflen - off;
+ size_t len = i->iov_len < rem ? i->iov_len : rem;
+
+ memcpy(i->iov_base, (const unsigned char *)buf + off, len);
+ off += len;
+ }
+
+ return off;
+}
+
+static ssize_t
+cmsg_to_fdbuf(int *buf, size_t buflen, const struct msghdr *msg)
+{
+ size_t next_fd = 0;
+
+ for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
+ cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) {
+ size_t size = cmsg->cmsg_len - CMSG_LEN(0);
+
+ // Check the cmsg can be handled.
+ if (cmsg->cmsg_level != SOL_SOCKET ||
+ cmsg->cmsg_type != SCM_RIGHTS ||
+ size % sizeof(int) != 0) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ size_t rem = sizeof(int) * (buflen - next_fd);
+ size_t len = size > rem ? rem : size;
+
+ // Copy the fds to the buffer.
+ memcpy(buf + next_fd, CMSG_DATA(cmsg), len);
+ next_fd += len / sizeof(int);
+
+ if (size > rem)
+ break;
+ }
+
+ return next_fd > SSIZE_MAX ? SSIZE_MAX : next_fd;
+}
+
+static size_t
+fdbuf_to_cmsg(struct msghdr *msg, const int *buf, size_t buflen)
+{
+ // Check msg->msg_control is long enough to fit at least one fd.
+ if (msg->msg_controllen < CMSG_SPACE(sizeof(int))) {
+
+ // If there's at least one fd in buf, set MSG_CTRUNC.
+ size_t i = 0;
+ while (i < buflen && !(msg->msg_flags & MSG_CTRUNC))
+ if (buf[i++] != -1)
+ msg->msg_flags |= MSG_CTRUNC;
+
+ return 0;
+ }
+
+ // cmsg(3):
+ // > When initializing a buffer that will contain a series of cmsghdr
+ // > structures (e.g., to be sent with sendmsg(2)), that buffer should
+ // > first be zero-initialized to en‐ sure the correct operation of
+ // > CMSG_NXTHDR().
+ memset(msg->msg_control, 0, msg->msg_controllen);
+
+ // Set up the cmsg.
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+
+ // Copy as many fds as fit into cmsg.
+ size_t len = 0;
+ for (size_t i = 0; i < buflen; i++) {
+ if (buf[i] == -1)
+ continue;
+
+ if (CMSG_LEN((len + 1) * sizeof(int)) > msg->msg_controllen) {
+ msg->msg_flags |= MSG_CTRUNC;
+ break;
+ }
+
+ memcpy(CMSG_DATA(cmsg) + sizeof(int) * len, &buf[i], sizeof(int));
+ len++;
+ }
+
+ cmsg->cmsg_len = CMSG_LEN(len * sizeof(int));
+ return len;
+}
+
+VIRTIO_WL_EXPORT int
+virtio_wl_connect(const char *name, uint32_t flags)
+{
+ static int wl_fd = -1;
+ if (wl_fd < 0)
+ wl_fd = open("/dev/wl0", O_RDWR | O_CLOEXEC);
+ if (wl_fd < 0)
+ return wl_fd;
+
+ struct virtwl_ioctl_new new_ctx = {
+ .type = name ? VIRTWL_IOCTL_NEW_CTX_NAMED : VIRTWL_IOCTL_NEW_CTX,
+ .fd = -1,
+ .flags = flags,
+ };
+ // Device assumes name 32 bytes long if not null terminated.
+ if (name)
+ strncpy(new_ctx.name, name, sizeof(new_ctx.name));
+
+ if (ioctl(wl_fd, VIRTWL_IOCTL_NEW, &new_ctx))
+ return -1;
+
+ return new_ctx.fd;
+}
+
+VIRTIO_WL_EXPORT int
+virtio_wl_send_raw(int sockfd, struct virtwl_ioctl_txn *ioctl_txn)
+{
+ return ioctl(sockfd, VIRTWL_IOCTL_SEND, ioctl_txn);
+}
+
+static int
+msghdr_to_txn(const struct msghdr *msg, struct virtwl_ioctl_txn **txn)
+{
+ // Make sure that if there's an error and the caller tries to free *txn
+ // anyway it doesn't end up freeing an invalid address.
+ *txn = NULL;
+
+ // Figure out how big the txn needs to be.
+ ssize_t len = iov_len(msg->msg_iov, msg->msg_iovlen);
+ if (len < 0 || len > UINT32_MAX) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ // Allocate the txn.
+ *txn = malloc(sizeof(**txn) + len);
+ if (!*txn)
+ return -1;
+
+ // Set the len member of the txn.
+ (*txn)->len = len;
+
+ // Copy data from the iovec into the transaction.
+ iov_flatten((*txn)->data, len, msg->msg_iov, msg->msg_iovlen);
+
+ // Copy file descriptors to the txn.
+ ssize_t fd_count = cmsg_to_fdbuf((*txn)->fds, VIRTWL_SEND_MAX_ALLOCS, msg);
+ if (fd_count == -1) {
+ free(*txn);
+ *txn = NULL;
+ return -1;
+ }
+
+ // Fill the rest of the fd buffer with -1.
+ while (fd_count < VIRTWL_SEND_MAX_ALLOCS)
+ (*txn)->fds[fd_count++] = -1;
+
+ return 0;
+}
+
+static void
+txn_to_msghdr(struct msghdr *msg, const struct virtwl_ioctl_txn *txn)
+{
+ // Copy txn data to iovecs.
+ iov_fill(msg->msg_iov, msg->msg_iovlen, txn->data, txn->len);
+
+ // Copy fds to cmsg.
+ fdbuf_to_cmsg(msg, txn->fds, VIRTWL_SEND_MAX_ALLOCS);
+}
+
+VIRTIO_WL_EXPORT ssize_t
+virtio_wl_sendmsg(int sockfd, const struct msghdr *msg, int flags)
+{
+ int sockfl;
+
+ if ((flags & ~MSG_DONTWAIT) != MSG_NOSIGNAL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ struct virtwl_ioctl_txn *txn;
+ if (msghdr_to_txn(msg, &txn) == -1)
+ return -1;
+
+ if (flags & MSG_DONTWAIT) {
+ sockfl = set_nonblocking(sockfd);
+ if (sockfl == -1) {
+ free(txn);
+ return -1;
+ }
+ }
+
+ int rv = virtio_wl_send_raw(sockfd, txn);
+
+ if ((flags & MSG_DONTWAIT) && !(sockfl & O_NONBLOCK)) {
+ if (fcntl(sockfd, F_SETFL, sockfl) == -1) {
+ free(txn);
+ return -1;
+ }
+ }
+
+ if (rv != -1)
+ rv = txn->len;
+
+ free(txn);
+ return rv;
+}
+
+VIRTIO_WL_EXPORT int
+virtio_wl_recv_raw(int sockfd, struct virtwl_ioctl_txn *ioctl_txn)
+{
+ return ioctl(sockfd, VIRTWL_IOCTL_RECV, ioctl_txn);
+}
+
+VIRTIO_WL_EXPORT ssize_t
+virtio_wl_recvmsg(int sockfd, struct msghdr *msg, int flags)
+{
+ int sockfl;
+
+ if ((flags & ~MSG_DONTWAIT) != MSG_CMSG_CLOEXEC) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ ssize_t len = iov_len(msg->msg_iov, msg->msg_iovlen);
+ if (len < 0 || len > UINT32_MAX) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ struct virtwl_ioctl_txn *txn = malloc(sizeof(*txn) + len);
+ if (!txn)
+ return -1;
+
+ txn->len = len;
+
+ if (flags & MSG_DONTWAIT) {
+ sockfl = set_nonblocking(sockfd);
+ if (sockfl == -1) {
+ free(txn);
+ return -1;
+ }
+ }
+
+ ssize_t rv = virtio_wl_recv_raw(sockfd, txn);
+
+ if ((flags & MSG_DONTWAIT) && !(sockfl & O_NONBLOCK))
+ if (fcntl(sockfd, F_SETFL, sockfl) == -1)
+ rv = -1;
+
+ if (rv == -1) {
+ for (size_t i = 0; i < VIRTWL_SEND_MAX_ALLOCS; i++)
+ if (txn->fds[i] == -1)
+ close(txn->fds[i]);
+ } else {
+ txn_to_msghdr(msg, txn);
+ rv = txn->len;
+ }
+
+ free(txn);
+ return rv;
+}
diff --git a/src/virtio_wl.h b/src/virtio_wl.h
new file mode 100644
index 0000000..80bcd38
--- /dev/null
+++ b/src/virtio_wl.h
@@ -0,0 +1,23 @@
+/* Copyright 2020 Alyssa Ross
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+#ifndef UTIL_VIRTIO_WL_H
+#define UTIL_VIRTIO_WL_H
+
+#include <sys/types.h>
+
+struct virtwl_ioctl_txn;
+struct msghdr;
+
+int virtio_wl_connect(const char *name, uint32_t flags);
+
+ssize_t virtio_wl_sendmsg(int sockfd, const struct msghdr *, int flags);
+int virtio_wl_send_raw(int sockfd, struct virtwl_ioctl_txn *);
+
+ssize_t virtio_wl_recvmsg(int sockfd, struct msghdr *, int flags);
+int virtio_wl_recv_raw(int sockfd, struct virtwl_ioctl_txn *);
+
+#endif
diff --git a/src/wayland-os.c b/src/wayland-os.c
index 93b6f5f..c92aaae 100644
--- a/src/wayland-os.c
+++ b/src/wayland-os.c
@@ -31,6 +31,9 @@
#include <fcntl.h>
#include <errno.h>
#include <sys/epoll.h>
+#include <virtio_wl.h>
+#include <string.h>
+#include <stdio.h>
#include "../config.h"
#include "wayland-os.h"
@@ -126,6 +129,10 @@ wl_os_recvmsg_cloexec(int sockfd, struct msghdr *msg, int flags)
len = recvmsg(sockfd, msg, flags | MSG_CMSG_CLOEXEC);
if (len >= 0)
return len;
+
+ if (errno == ENOTSOCK)
+ return virtio_wl_recvmsg(sockfd, msg, flags | MSG_CMSG_CLOEXEC);
+
if (errno != EINVAL)
return -1;
@@ -165,3 +172,19 @@ wl_os_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
fd = accept(sockfd, addr, addrlen);
return set_cloexec_or_close(fd);
}
+
+int
+wl_virtio_accept_cloexec(int sockfd)
+{
+ char name[32];
+ size_t offset = 0;
+
+ while (offset < sizeof name) {
+ ssize_t size = read(sockfd, name + offset, sizeof name - offset);
+ if (size < 0)
+ return size;
+ offset += size;
+ }
+
+ return virtio_wl_connect(name, 0);
+}
diff --git a/src/wayland-os.h b/src/wayland-os.h
index f51efaa..899ac8e 100644
--- a/src/wayland-os.h
+++ b/src/wayland-os.h
@@ -41,6 +41,9 @@ wl_os_epoll_create_cloexec(void);
int
wl_os_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
+int
+wl_virtio_accept_cloexec(int sockfd);
+
/*
* The following are for wayland-os.c and the unit tests.
diff --git a/src/wayland-server.c b/src/wayland-server.c
index 3f48dfe..0146ca8 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -531,8 +531,18 @@ wl_client_create(struct wl_display *display, int fd)
len = sizeof client->ucred;
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
- &client->ucred, &len) < 0)
- goto err_source;
+ &client->ucred, &len) < 0 && errno != ENOTSOCK) {
+ if (errno == ENOTSOCK) {
+ // Probably a virtio_wl socket.
+ // We don't have credential information, so fill with
+ // values that should never match real ones.
+ client->ucred.pid = -1;
+ client->ucred.uid = -1;
+ client->ucred.gid = -1;
+ } else {
+ goto err_source;
+ }
+ }
client->connection = wl_connection_create(fd);
if (client->connection == NULL)
@@ -1419,6 +1429,9 @@ socket_data(int fd, uint32_t mask, void *data)
length = sizeof name;
client_fd = wl_os_accept_cloexec(fd, (struct sockaddr *) &name,
&length);
+ if (client_fd < 0 && errno == ENOTSOCK)
+ client_fd = wl_virtio_accept_cloexec(fd);
+
if (client_fd < 0)
wl_log("failed to accept: %s\n", strerror(errno));
else
@@ -1603,7 +1616,8 @@ wl_display_add_socket_fd(struct wl_display *display, int sock_fd)
struct stat buf;
/* Require a valid fd or fail */
- if (sock_fd < 0 || fstat(sock_fd, &buf) < 0 || !S_ISSOCK(buf.st_mode)) {
+ if (sock_fd < 0 || fstat(sock_fd, &buf) < 0 ||
+ ((buf.st_mode & S_IFMT) && !S_ISSOCK(buf.st_mode))) {
return -1;
}
diff --git a/src/wayland-shm.c b/src/wayland-shm.c
index b85e5a7..636ee7a 100644
--- a/src/wayland-shm.c
+++ b/src/wayland-shm.c
@@ -61,6 +61,7 @@ struct wl_shm_pool {
int internal_refcount;
int external_refcount;
char *data;
+ int fd;
int32_t size;
int32_t new_size;
bool sigbus_is_impossible;
@@ -91,14 +92,27 @@ shm_pool_finish_resize(struct wl_shm_pool *pool)
data = mremap(pool->data, pool->size, pool->new_size, MREMAP_MAYMOVE);
if (data == MAP_FAILED) {
- wl_resource_post_error(pool->resource,
- WL_SHM_ERROR_INVALID_FD,
- "failed mremap");
- return;
+ if (errno != EFAULT)
+ goto fail;
+
+ if (munmap(pool->data, pool->size) == -1)
+ goto fail;
+
+ data = mmap(pool->data, pool->new_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, pool->fd, 0);
+ if (data == MAP_FAILED)
+ goto fail;
}
pool->data = data;
pool->size = pool->new_size;
+
+ return;
+
+ fail:
+ wl_resource_post_error(pool->resource,
+ WL_SHM_ERROR_INVALID_FD,
+ "failed mremap");
}
static void
@@ -291,6 +305,7 @@ shm_create_pool(struct wl_client *client, struct wl_resource *resource,
pool->external_refcount = 0;
pool->size = size;
pool->new_size = size;
+ pool->fd = fd;
pool->data = mmap(NULL, size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (pool->data == MAP_FAILED) {
@@ -300,7 +315,6 @@ shm_create_pool(struct wl_client *client, struct wl_resource *resource,
strerror(errno));
goto err_free;
}
- close(fd);
pool->resource =
wl_resource_create(client, &wl_shm_pool_interface, 1, id);
diff --git a/tests/meson.build b/tests/meson.build
index 224f48d..6ee2b49 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -148,6 +148,7 @@ tests = {
'headers-protocol-core-test.c',
],
'os-wrappers-test': [],
+ 'virtio_wl-test': [],
}
foreach test_name, test_extra_sources: tests
diff --git a/tests/virtio_wl-test.c b/tests/virtio_wl-test.c
new file mode 100644
index 0000000..16c7a93
--- /dev/null
+++ b/tests/virtio_wl-test.c
@@ -0,0 +1,615 @@
+#define _GNU_SOURCE
+#include <assert.h>
+
+#include "test-runner.h"
+#include "virtio_wl.c"
+
+TEST(iov_len_fit)
+{
+ int f1[] = { 1, 2 };
+ int f2[] = { 3 };
+
+ struct iovec iov[] = {
+ { .iov_base = f1, .iov_len = sizeof(f1) },
+ { .iov_base = NULL, .iov_len = 0 },
+ { .iov_base = f2, .iov_len = sizeof(f2) },
+ };
+
+ assert(iov_len(iov, sizeof(iov) / sizeof(*iov)) == 3 * sizeof(int));
+}
+
+TEST(iov_len_overflow)
+{
+ struct iovec iov[] = {
+ { .iov_base = NULL, .iov_len = SIZE_MAX },
+ { .iov_base = NULL, .iov_len = SIZE_MAX },
+ };
+
+ assert(iov_len(iov, sizeof(iov) / sizeof(*iov)) == -1);
+}
+
+TEST(iov_flatten_test)
+{
+ int f1[] = { 1 };
+ int f2[] = { 2, 3 };
+
+ struct iovec iov[] = {
+ { .iov_base = f1, .iov_len = sizeof(f1) },
+ { .iov_base = NULL, .iov_len = 0 },
+ { .iov_base = f2, .iov_len = sizeof(f2) },
+ };
+
+ int buf[2];
+ iov_flatten(buf, sizeof(buf), iov, sizeof(iov) / sizeof(*iov));
+ assert(buf[0] == 1);
+ assert(buf[1] == 2);
+}
+
+TEST(iov_flatten_zero_iov)
+{
+ unsigned char buf[] = { 0xFF };
+ assert(iov_flatten(buf, 1, NULL, 0) == 0);
+}
+
+TEST(iov_flatten_null_iov)
+{
+ unsigned char buf[] = { 0xFF };
+ struct iovec iov[] = { { .iov_base = NULL, .iov_len = 0 } };
+
+ assert(iov_flatten(buf, sizeof buf, iov, sizeof(iov) / sizeof(*iov)) == 0);
+}
+
+TEST(iov_flatten_zero_buf)
+{
+ unsigned char f1[] = { 0 };
+ struct iovec iov[] = { { .iov_base = f1, .iov_len = sizeof f1 } };
+
+ assert(iov_flatten(NULL, 0, iov, sizeof(iov) / sizeof(*iov)) == 0);
+}
+
+TEST(iov_flatten_short_iov)
+{
+ int f1[] = { 1 };
+ int f2[] = { 2, 3 };
+
+ struct iovec iov[] = {
+ { .iov_base = f1, .iov_len = sizeof f1 },
+ { .iov_base = NULL, .iov_len = 0 },
+ { .iov_base = f2, .iov_len = sizeof f2 },
+ };
+
+ int buf[] = { 9, 8, 7, 6 };
+
+ assert(iov_flatten(buf, sizeof buf, iov, sizeof(iov) / sizeof(*iov)) == 12);
+ assert(buf[0] == 1);
+ assert(buf[1] == 2);
+ assert(buf[2] == 3);
+ assert(buf[3] == 6);
+}
+
+TEST(iov_flatten_exact_iov)
+{
+ int f1[] = { 1 };
+ int f2[] = { 2, 3 };
+
+ struct iovec iov[] = {
+ { .iov_base = f1, .iov_len = sizeof f1 },
+ { .iov_base = NULL, .iov_len = 0 },
+ { .iov_base = f2, .iov_len = sizeof f2 },
+ };
+
+ int buf[] = { 9, 8, 7 };
+
+ assert(iov_flatten(buf, sizeof buf, iov, sizeof(iov) / sizeof(*iov)) == 12);
+ assert(f1[0] == 1);
+ assert(f2[0] == 2);
+ assert(f2[1] == 3);
+}
+
+TEST(iov_flatten_long_iov)
+{
+ int f1[] = { 1 };
+ int f2[] = { 2, 3, 4 };
+
+ struct iovec iov[] = {
+ { .iov_base = f1, .iov_len = sizeof f1 },
+ { .iov_base = NULL, .iov_len = 0 },
+ { .iov_base = f2, .iov_len = sizeof f2 },
+ };
+
+ int buf[] = { 9, 8, 7 };
+
+ assert(iov_flatten(buf, sizeof buf, iov, sizeof(iov) / sizeof(*iov)) == 12);
+ assert(buf[0] == 1);
+ assert(buf[1] == 2);
+ assert(buf[2] == 3);
+}
+
+TEST(iov_fill_zero_iov)
+{
+ unsigned char buf[] = { 0xFF };
+ assert(iov_fill(NULL, 0, buf, 1) == 0);
+}
+
+TEST(iov_fill_null_iov)
+{
+ unsigned char buf[] = { 0xFF };
+ struct iovec iov[] = { { .iov_base = NULL, .iov_len = 0 } };
+
+ assert(iov_fill(iov, sizeof(iov) / sizeof(*iov), buf, sizeof buf) == 0);
+}
+
+TEST(iov_fill_zero_buf)
+{
+ unsigned char f1[] = { 0 };
+ struct iovec iov[] = { { .iov_base = f1, .iov_len = sizeof f1 } };
+
+ assert(iov_fill(iov, sizeof(iov) / sizeof(*iov), NULL, 0) == 0);
+}
+
+TEST(iov_fill_short_iov)
+{
+ int f1[] = { 1 };
+ int f2[] = { 2, 3 };
+
+ struct iovec iov[] = {
+ { .iov_base = f1, .iov_len = sizeof f1 },
+ { .iov_base = NULL, .iov_len = 0 },
+ { .iov_base = f2, .iov_len = sizeof f2 },
+ };
+
+ int buf[] = { 9, 8, 7, 6 };
+
+ assert(iov_fill(iov, sizeof(iov) / sizeof(*iov), buf, sizeof buf) == 12);
+ assert(f1[0] == 9);
+ assert(f2[0] == 8);
+ assert(f2[1] == 7);
+}
+
+TEST(iov_fill_exact)
+{
+ int f1[] = { 1 };
+ int f2[] = { 2, 3 };
+
+ struct iovec iov[] = {
+ { .iov_base = f1, .iov_len = sizeof f1 },
+ { .iov_base = NULL, .iov_len = 0 },
+ { .iov_base = f2, .iov_len = sizeof f2 },
+ };
+
+ int buf[] = { 9, 8, 7 };
+
+ assert(iov_fill(iov, sizeof(iov) / sizeof(*iov), buf, sizeof buf) == 12);
+ assert(f1[0] == 9);
+ assert(f2[0] == 8);
+ assert(f2[1] == 7);
+}
+
+TEST(iov_fill_long_iov)
+{
+ int f1[] = { 1 };
+ int f2[] = { 2, 3, 4 };
+
+ struct iovec iov[] = {
+ { .iov_base = f1, .iov_len = sizeof f1 },
+ { .iov_base = NULL, .iov_len = 0 },
+ { .iov_base = f2, .iov_len = sizeof f2 },
+ };
+
+ int buf[] = { 9, 8, 7 };
+
+ assert(iov_fill(iov, sizeof(iov) / sizeof(*iov), buf, sizeof buf) == 12);
+ assert(f1[0] == 9);
+ assert(f2[0] == 8);
+ assert(f2[1] == 7);
+ assert(f2[2] == 4);
+}
+
+TEST(cmsg_to_fdbuf_zero_buf)
+{
+ int fds[] = { 0 };
+
+ union {
+ char buf[CMSG_SPACE(sizeof fds)];
+ struct cmsghdr align;
+ } u;
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof fds);
+ memcpy(CMSG_DATA(cmsg), fds, sizeof fds);
+
+ assert(cmsg_to_fdbuf(NULL, 0, &msg) == 0);
+}
+
+TEST(cmsg_to_fdbuf_zero_cmsg)
+{
+ int fds[2] = { 0 };
+ struct msghdr msg = { 0 };
+ assert(cmsg_to_fdbuf(fds, sizeof fds, &msg) == 0);
+}
+
+TEST(cmsg_to_fdbuf_small_buf)
+{
+ int fds[] = { 0, 1 };
+
+ union {
+ char buf[CMSG_SPACE(sizeof fds)];
+ struct cmsghdr align;
+ } u;
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof fds);
+ memcpy(CMSG_DATA(cmsg), fds, sizeof fds);
+
+ int buf[] = { 0xFF };
+ assert(cmsg_to_fdbuf(buf, sizeof(buf) / sizeof(*buf), &msg) == 1);
+ assert(buf[0] == 0);
+}
+
+TEST(cmsg_to_fdbuf_exact)
+{
+ int fds[] = { 0, 1 };
+
+ union {
+ char buf[CMSG_SPACE(sizeof fds)];
+ struct cmsghdr align;
+ } u;
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof fds);
+ memcpy(CMSG_DATA(cmsg), fds, sizeof fds);
+
+ int buf[] = { 0xFF, 0xFE };
+ assert(cmsg_to_fdbuf(buf, sizeof(buf) / sizeof(*buf), &msg) == 2);
+ assert(buf[0] == 0);
+ assert(buf[1] == 1);
+}
+
+TEST(cmsg_to_fdbuf_small_cmsg)
+{
+ int fds[] = { 0 };
+
+ union {
+ char buf[CMSG_SPACE(sizeof fds)];
+ struct cmsghdr align;
+ } u;
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof fds);
+ memcpy(CMSG_DATA(cmsg), fds, sizeof fds);
+
+ int buf[] = { 0xFF, 0xFE };
+ assert(cmsg_to_fdbuf(buf, sizeof(buf) / sizeof(*buf), &msg) == 1);
+ assert(buf[0] == 0);
+ assert(buf[1] == 0xFE);
+}
+
+TEST(cmsg_to_fdbuf_multiple_cmsg)
+{
+ int f1[] = { 0 };
+ int f2[] = { 1 };
+
+ union {
+ char buf[CMSG_SPACE(sizeof f1) + CMSG_SPACE(sizeof f2)];
+ struct cmsghdr align;
+ } u;
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ struct cmsghdr *cmsg1 = CMSG_FIRSTHDR(&msg);
+ cmsg1->cmsg_level = SOL_SOCKET;
+ cmsg1->cmsg_type = SCM_RIGHTS;
+ cmsg1->cmsg_len = CMSG_LEN(sizeof f1);
+ memcpy(CMSG_DATA(cmsg1), f1, sizeof f1);
+
+ struct cmsghdr *cmsg2 = CMSG_NXTHDR(&msg, cmsg1);
+ cmsg2->cmsg_level = SOL_SOCKET;
+ cmsg2->cmsg_type = SCM_RIGHTS;
+ cmsg2->cmsg_len = CMSG_LEN(sizeof f2);
+ memcpy(CMSG_DATA(cmsg2), f2, sizeof f2);
+
+ int buf[] = { 0xFF, 0xFE };
+ assert(cmsg_to_fdbuf(buf, sizeof(buf) / sizeof(*buf), &msg) == 2);
+ assert(buf[0] == 0);
+ assert(buf[1] == 1);
+}
+
+TEST(cmsg_to_fdbuf_other_cmsg)
+{
+ struct ucred cred = {
+ .pid = getpid(),
+ .uid = getuid(),
+ .gid = getgid(),
+ };
+
+ int fds[] = { 0, 1 };
+
+ union {
+ char buf[CMSG_SPACE(sizeof cred) + CMSG_SPACE(sizeof fds)];
+ struct cmsghdr align;
+ } u;
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ struct cmsghdr *cmsg1 = CMSG_FIRSTHDR(&msg);
+ cmsg1->cmsg_level = SOL_SOCKET;
+ cmsg1->cmsg_type = SCM_CREDENTIALS;
+ cmsg1->cmsg_len = CMSG_LEN(sizeof cred);
+ memcpy(CMSG_DATA(cmsg1), &cred, sizeof cred);
+
+ struct cmsghdr *cmsg2 = CMSG_NXTHDR(&msg, cmsg1);
+ cmsg2->cmsg_level = SOL_SOCKET;
+ cmsg2->cmsg_type = SCM_RIGHTS;
+ cmsg2->cmsg_len = CMSG_LEN(sizeof fds);
+ memcpy(CMSG_DATA(cmsg2), fds, sizeof fds);
+
+ int buf[] = { 0xFF, 0xFE };
+ assert(cmsg_to_fdbuf(buf, sizeof(buf) / sizeof(*buf), &msg) == -1);
+ assert(errno == EINVAL);
+}
+
+TEST(cmsg_to_fdbuf_misaligned)
+{
+ int fds[] = { 0, 1 };
+
+ union {
+ char buf[CMSG_SPACE(sizeof fds + 1)];
+ struct cmsghdr align;
+ } u;
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(fds) + 1);
+ memcpy(CMSG_DATA(cmsg), fds, sizeof fds);
+ CMSG_DATA(cmsg)[sizeof(fds)] = 0;
+
+ int buf[] = { 0xFF, 0xFE };
+ assert(cmsg_to_fdbuf(buf, sizeof(buf) / sizeof(*buf), &msg) == -1);
+ assert(errno == EINVAL);
+}
+
+TEST(fdbuf_to_cmsg_null_cmsg)
+{
+ struct msghdr msg = { 0 };
+ int fds[] = { 1 };
+ assert(fdbuf_to_cmsg(&msg, fds, sizeof(fds) / sizeof(*fds)) == 0);
+ assert(msg.msg_flags & MSG_CTRUNC);
+}
+
+TEST(fdbuf_to_cmsg_empty_both)
+{
+ struct msghdr msg = { 0 };
+ int fds[] = { -1 };
+ assert(fdbuf_to_cmsg(&msg, fds, sizeof(fds) / sizeof(*fds)) == 0);
+ assert(!(msg.msg_flags & MSG_CTRUNC));
+}
+
+TEST(fdbuf_to_cmsg_null_buf)
+{
+ union {
+ char buf[CMSG_SPACE(sizeof(int))];
+ struct cmsghdr align;
+ } u = { 0 };
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ assert(fdbuf_to_cmsg(&msg, NULL, 0) == 0);
+ assert(!(msg.msg_flags & MSG_CTRUNC));
+}
+
+TEST(fdbuf_to_cmsg_tiny_msg)
+{
+ union {
+ char buf[1];
+ struct cmsghdr align;
+ } u = { 0 };
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ int fds[] = { 1 };
+ assert(fdbuf_to_cmsg(&msg, fds, sizeof(fds) / sizeof(*fds)) == 0);
+ assert(msg.msg_flags & MSG_CTRUNC);
+}
+
+TEST(fdbuf_to_cmsg_small_msg)
+{
+ union {
+ char buf[CMSG_SPACE(sizeof(int))];
+ struct cmsghdr align;
+ } u = { 0 };
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ int fds[] = { 1, 2, 3 };
+ assert(CMSG_SPACE(sizeof fds) != sizeof u.buf);
+
+ // CMSG_SPACE(sizeof(int)) can equal CMSG_SPACE(sizeof(int) * 2)
+ size_t n = fdbuf_to_cmsg(&msg, fds, sizeof(fds) / sizeof(*fds));
+ assert(CMSG_SPACE(n * sizeof(int)) == sizeof u.buf);
+ assert(msg.msg_flags & MSG_CTRUNC);
+}
+
+TEST(fdbuf_to_cmsg_equal)
+{
+ int fds[] = { 1, 2 };
+
+ union {
+ char buf[CMSG_SPACE(sizeof fds)];
+ struct cmsghdr align;
+ } u = { 0 };
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ assert(fdbuf_to_cmsg(&msg, fds, sizeof(fds) / sizeof(*fds)) == 2);
+ assert(!memcmp(CMSG_DATA(CMSG_FIRSTHDR(&msg)), fds, sizeof fds));
+ assert(!(msg.msg_flags & MSG_CTRUNC));
+}
+
+TEST(fdbuf_to_cmsg_negative_one)
+{
+ int fds[] = { 1, -1, -1, 2 };
+
+ union {
+ char buf[CMSG_SPACE(2 * sizeof(int))];
+ struct cmsghdr align;
+ } u = { 0 };
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ assert(fdbuf_to_cmsg(&msg, fds, sizeof(fds) / sizeof(*fds)) == 2);
+
+ unsigned char *data = CMSG_DATA(CMSG_FIRSTHDR(&msg));
+ assert(!memcmp(data, &fds[0], sizeof(int)));
+ assert(!memcmp(data + sizeof(int), &fds[3], sizeof(int)));
+ assert(!(msg.msg_flags & MSG_CTRUNC));
+}
+
+TEST(fdbuf_to_cmsg_small_buf)
+{
+ int fds[] = { 1 };
+
+ union {
+ char buf[CMSG_SPACE(2 * sizeof fds)];
+ struct cmsghdr align;
+ } u = { 0 };
+
+ struct msghdr msg = { 0 };
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ assert(fdbuf_to_cmsg(&msg, fds, sizeof(fds) / sizeof(*fds)) == 1);
+ assert(!memcmp(CMSG_DATA(CMSG_FIRSTHDR(&msg)), fds, sizeof fds));
+ assert(!(msg.msg_flags & MSG_CTRUNC));
+}
+
+TEST(msghdr_to_txn_test)
+{
+ struct virtwl_ioctl_txn *txn;
+
+ int f1[] = { 1 };
+ int f2[] = { 2, 3 };
+
+ const int fds[] = { 0, 1, 2 };
+
+ struct iovec iov[] = {
+ { .iov_base = f1, .iov_len = sizeof f1 },
+ { .iov_base = f2, .iov_len = sizeof f2 },
+ };
+
+ union {
+ char buf[CMSG_SPACE(sizeof fds)];
+ struct cmsghdr align;
+ } u;
+
+ struct msghdr msg = { 0 };
+ msg.msg_iov = iov;
+ msg.msg_iovlen = sizeof(iov) / sizeof(*iov);
+ msg.msg_control = u.buf;
+ msg.msg_controllen = sizeof u.buf;
+
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof fds);
+ memcpy(CMSG_DATA(cmsg), fds, sizeof fds);
+
+ assert(!msghdr_to_txn(&msg, &txn));
+
+ assert(txn->len == sizeof(f1) + sizeof(f2));
+ assert(!memcmp(txn->fds, fds, sizeof fds));
+ for (size_t i = sizeof(fds) / sizeof(*fds);
+ i < VIRTWL_SEND_MAX_ALLOCS; i++)
+ assert(txn->fds[i] == -1);
+ assert(!memcmp(txn->data, f1, sizeof f1));
+ assert(!memcmp(txn->data + sizeof f1, f2, sizeof f2));
+}
+
+TEST(txn_to_msghdr_test)
+{
+ const int data[] = { 1, 2, 3 };
+
+ size_t len = sizeof(data);
+ struct virtwl_ioctl_txn *txn = malloc(sizeof(*txn) + len);
+ assert(txn);
+
+ txn->len = len;
+
+ const int fds[] = { 0, 1, 2 };
+ memcpy(txn->fds, fds, sizeof fds);
+ for (int i = sizeof(fds) / sizeof(*fds); i < VIRTWL_SEND_MAX_ALLOCS; i++)
+ txn->fds[i] = -1;
+
+ memcpy(txn->data, data, sizeof data);
+
+ int buf1[1], buf2[2];
+
+ struct iovec iov[] = {
+ { .iov_base = buf1, .iov_len = sizeof buf1 },
+ { .iov_base = buf2, .iov_len = sizeof buf2 },
+ };
+
+ unsigned char cmsg_buf[VIRTWL_SEND_MAX_ALLOCS];
+
+ struct msghdr msg = { 0 };
+ msg.msg_iov = iov;
+ msg.msg_iovlen = sizeof(iov) / sizeof(*iov);
+ msg.msg_control = cmsg_buf;
+ msg.msg_controllen = sizeof cmsg_buf;
+
+ txn_to_msghdr(&msg, txn);
+
+ assert(!memcmp(buf1, data, sizeof buf1));
+ assert(!memcmp(buf2, (unsigned char *)data + sizeof buf1, sizeof buf2));
+
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ assert(cmsg->cmsg_level == SOL_SOCKET);
+ assert(cmsg->cmsg_type == SCM_RIGHTS);
+ assert(cmsg->cmsg_len == CMSG_LEN(3 * sizeof(int)));
+ for (int i = 0; i < 3; i++)
+ assert(!memcmp(CMSG_DATA(cmsg), fds, sizeof fds));
+
+ assert(!CMSG_NXTHDR(&msg, cmsg));
+}
--
2.26.2
2
4
Feels like there's a pretty clear path forward. Nice feeling. :)
QEMU
----
Last week, as I wrote TWiS, I had just discovered virtio-vhost-user,
which looked like a very promising mechanism for getting a VM to take
care of networking for other VMs. This week, I've been researching it
further, and trying to test and evaluate it.
The first thing I tried to do, naturally, was to build the patched QEMU
tree and boot a VM with a virtio-vhost-user device attached. This was
not as easy as I'd hoped, because adding the virtio-vhost-user device to
my QEMU command line made the VM kernel panic at boot, with an error
message about an invalid memory access. I spent most of the week trying
to figure this out -- I wasn't doing anything different to the
example[1] on the QEMU wiki, so it should have worked, and it felt like
if I could just get past whatever was going wrong here, it would be
worth it, because virtio-vhost-user otherwise seems so suited for what
we need here. I emailed the patch author[2], but he didn't know what
was up either.
An early breakthrough came when I got frustrated with kernel builds
taking hours on my 8-year-old laptop, and so decided to work on a more
powerful computer instead. Once I got everything set up on that
computer, I started up the VM, and it worked. Perhaps in setting it up
over here I'd done something different? I copied over the exact VM
disk/kernel/initrd/command line that I was running on my laptop, and the
other computer booted it just fine. I had -cpu host in the QEMU command
line, so I thought maybe the different kind of virtual CPU was causing
it. Tried setting it to a specific value on both machines, and still
the laptop VM panicked and the other didn't. So it sounded like whether
it worked or not depended on the host hardware.
I put together a Nix derivation that would automatically build the
custom QEMU and output a script that would run a VM, and then asked
people in #spectrum to test it out on various computers. After getting
some further data, a pattern started to emerge, where Intel processors
Ivy Bridge and older would fail, and Skylake and newer would succeed (I
didn't encounter any AMD processors that failed, nor did I have data at
the time for generations between Ivy Bridge and Skylake). This theory
had a convenient explanation for why nobody else had seen this problem
-- I doubt people at Red Hat are working on 7-year-old hardware.
This was a good clue, but still didn't put me much closer to having a
working system. I do have a more recent laptop around, but for reasons
that are out of scope here it would be very inconvenient to decide to
just move over to it. I could see that the kernel was panicking the
first time it tried to access the PCI BARs of the virtio-vhost-user,
which led me to believe that the problem was probably in how that memory
was being set up. I found the function that did that[3], and stared at
it for a long time. I tried to read the rest of the QEMU code, but it
became clear that my domain knowledge here isn't good enough to be able
to keep track of what's meant to be happening. I added some debug
prints, which were vaguely helpful in making that understanding a little
better.
I was hoping to find the guest address each PCI BAR was mapped to so
that I could check the kernel was trying to write to the right location,
but didn't manage to do that. While attempting to, though, I did add a
debug print that printed the size of each PCI bar as it was allocated.
I noticed that most were small -- 16 MiB at most, but one was huge, at
64 GiB! The code that allocated this BAR was part of the function I'd
been staring at. As far as I could tell, the choice of size was pretty
arbitrary -- this big memory region was used as backing memory for all
sorts of small objects on the fly. On a whim, I tried changing the BAR
size from 1ULL << 36 to 1ULL << 26, and recompiled QEMU. The VM booted.
The comment above the bar_size definition that I'd been looking at for
so long said:
/* TODO If the BAR is too large the guest won't have address space to map
* it!
*/
I don't know if that's exactly what went wrong here, though. I suspect
it's more like the host architecture doesn't have enough address space?
The affected machines all reported 36 bit physical address size, and 48
bit virtual address size. So maybe what's happening is that the
processor interprets PCI addresses in the hardware-assisted VM as
physical addresses, and therefore runs out of space because all of it is
taken up by this one PCI bar? I'm not really sure. Lowering the bar
size to 2^35 or 2^34 (has to be a power of two) depending on the QEMU
version made the problem go away, and that's good enough for now.
I'm not very enthusiastic about this up-front allocation of a huge
amount of memory that might not even fit in the available address space.
I don't know if there's a better way of doing it in this case, but I
certainly hope so. In general I think this perhaps demonstrates why
this code is not considered suitable for "production" yet. The bet I'm
taking here is that by the time Spectrum is further along, things will
have moved on for virtio-vhost-user too. As I said, at some point we
will want to implement it in crosvm to avoid having QEMU in the TCB, but
it would be a bad idea to do that now while virtio-vhost-user is still
going through the back-and-forth of making its way into the Virtio spec.
[1]: https://wiki.qemu.org/Features/VirtioVhostUser
[2]: https://lore.kernel.org/qemu-devel/87h7u1s5k1.fsf@alyssa.is/T/#u
[3]: https://github.com/ndragazis/qemu/blob/f9ab08c0c8/hw/virtio/virtio-vhost-us…
DPDK
----
Once I was able to boot a VM with the virtio-vhost-user device, I tried
to connect another QEMU VM to it through vhost-user -- I'll want to have
this working first as a reference before I start porting Cloud
Hypervisor's vhost-user implementation to crosvm. But the "frontend"
(vhost-user) QEMU process hung waiting for a reply on the vhost-user
socket from the backend one. Not really knowing what to do about this,
I decided that maybe I'd been a bit too ambitious in going straight for
vhost-user <-> virtio-vhost-user when I'd never actually used vhost-user
before, so maybe I should try a more conventional vhost-user setup
first.
As far as I can tell, vhost-user is usually used for connecting a VM to
a userspace networking stack. And usually, this networking stack is
DPDK, the "Data Plane Development Kit"[4]. DPDK was also used in the
virtio-vhost-user examples, so I figured my next step would be to try
it there as well, and therefore it was worth the time in learning how to
do a very basic setup with it.
Quick start -style documentation for this was pretty lacking, but I did
eventually manage to make this work. Here's what I did, for my own
future reference as much as anything else:
(1) Make some hugepages available. 1GiB for DPDK and 1GiB for QEMU:
echo 1024 > /sys/devices/system/node/node0/hugepages/hugepages-2048kB/nr_hugepages
(2) Take my ethernet interface offline so it could be used with DPDK:
nmcli d disconnect enp0s25
(3) Load the vfio-pci module, which allows PCI devices to be exported to
userspace rather than managed by the kernel:
mobprobe vfio-pci
(4) Export the ethernet interface:
usertools/dpdk-devbind.py -b vfio-pci enp0s25
(5) Run testpmd, a program that comes with DPDK mostly used for
debugging and tracing it seems, but that with no special arguments
acts as a simple packet forwarder. Here I create a vhost-user
socket, and forward traffic between vhost-user and my ethernet
interface:
build/app/dpdk-testpmd -l 0,1 -w 00:19.0 \
--vdev net_vhost0,iface=/run/vhost-user0.sock
The -w value is the PCI address of the ethernet interface. Note how
"00:19" corresponds to "p0s25". (19 in hex is 25 is decimal.)
(6) Start a VM. The relevant QEMU flags appear to be:
-chardev socket,id=char0,path=/run/vhost-user0.sock \
-netdev type=vhost-user,id=net0,chardev=char0,vhostforce \
-device virtio-net-pci,netdev=net0 \
-object memory-backend-file,id=mem0,size=1024M,mem-path=/dev/hugepages,share=on \
-numa node,memdev=mem0 \
-mem-prealloc
I figured this all out mostly from a guide for a DPDK benchmark[5]. I
have not yet experimented with variations on the QEMU flags yet. I'm
not sure if all the memory flags are required -- -mem-prealloc might
just be there because it was important for a benchmark, for example.
So this is the point I'm at with this exploration. Next up, I'll be
trying with DPDK inside a VM with a virtio-vhost-user device. I think
that maybe, despite the virtio-vhost-user device showing up as an
ethernet device inside the VM, it needs some special support which is
available for DPDK as a patchset, but that has not been written for the
kernel yet. I was a bit worried about this, because unlike the kernel,
DPDK isn't going to have things that Wi-Fi drivers for all sorts of
different hardware, and so using DPDK instead of the kernel network
stack would be a problem. But then I learned that DPDK has a component
called the Kernel Native Interface (KNI) which allows it to use network
interfaces from the kernel, so a hybrid approach would be possible, and
is what I think we'll end up using for now. Then, once
virtio-vhost-user is a bit more mature, a kernel driver will probably
show up, and we can use that instead and drop DPDK.
[4]: https://dpdk.org/
[5]: https://doc.dpdk.org/guides/howto/pvp_reference_benchmark.html?highlight=pvp
Website
-------
I was having a conversation about Spectrum yesterday, and I found myself
sending over a bunch of links to articles and papers that I often find
myself referring to when talking to somebody about Spectrum. This made
me think that maybe there should be some place where we keep all these
relevant articles. So I mined the IRC logs, the TWiS archive, and my
blog, and added whatever I could pull from my brain, and wrote a
Spectrum bibliography, containing 27 links to interesting articles and
papers that are particularly relevant to Spectrum.
This isn't on the website quite yet, but I did sent this as a patch[6]
to the mailing list, if you want an early look.
I also posted a patch to fix a minor issue where I'd mistakenly used
".." instead of "." as href values, to no user-visible effect[7].
[6]: https://spectrum-os.org/lists/archives/spectrum-devel/20200726045701.32259-…
[7]: https://spectrum-os.org/lists/archives/spectrum-devel/20200726055410.20641-…
Documentation
-------------
On Monday, I had a call with the Free Software Foundation Europe.
They're a part of NGI Zero (where my funding comes from), and they are
promoting their new "REUSE" specification[8] for license information in
free software projects to NGI Zero projects. It basically covers
standardised per-file license and copyright annotations, and a standard
way of including license texts.
I think this is really cool! It's something I've been unsure of how to
handle because it's all vague conventions that are different in
different circles, and it's nice to see something formalised about it.
They also have an automated tool[9] for checking compliance and
semi-automatically adding license information, which is great!
So I'm enthusiastically adopting the REUSE specification. I decided
that our smaller, first-party repositories (the documentation, the
website, etc.) would be a good place to get started, and so I posted a
patch[10] that makes the documentation repository REUSE-compliant.
[8]: https://reuse.software/
[9]: https://git.fsfe.org/reuse/tool
[10]: https://spectrum-os.org/lists/archives/spectrum-devel/20200726105527.27432-…
mktuntap
--------
I posted a patch[11] to make mktuntap REUSE-compliant.
[11]: https://spectrum-os.org/lists/archives/spectrum-devel/20200726110123.30159-…
The thing that's most on my mind this week is the extent to which I'm
learning about and working on software like QEMU and DPDK that I don't
see having a place in Spectrum in the long run. It's counterintuitive,
but this is definitely worth it. There's no point writing a kernel
driver for virtio-vhost-user (should such a thing be required) right
now, because if I use DPDK for now instead, at some point either
virtio-vhost-user will end up not being the thing that gets adopted by
the ecosystem and we'll have to move to something else, or (more likely)
it gets widely adopted and somebody else writes a kernel driver.
Similarly, using QEMU for network VMs is the smart choice even though
I don't want it to end up in the TCB, because even though I'm probably
going to end up implementing virtio-vhost-user in crosvm later, swapping
out QEMU is going to be so easy later that it would be a very bad idea
to implement that now in case virtio-vhost-user doesn't take off. But
it still /feels/ weird to be using QEMU for this stuff, you know?
1
0
This has been a week of thinking I wanted to do one thing, not being
sure how to do it, and finding out that there was a better way. I'll
write it up in the order it happened.
crosvm
------
Last week, I described that I wanted to implement a virtio proxy to be
able to allow a kernel in an application VM to use a virtual device in
another VM. I was wondering how to manage virtio buffers, and thought
that I probably wanted an allocator to be able to manage throwing
buffers of different sizes around.
This turned out to be a case of the XY problem[1]. I couldn't find a
good solution, but it turned out that an allocator wasn't what I wanted
anyway. edef pointed out that I could just make the shared memory I
allocated as big as necessary to hold buffers of the maximum size I
wanted to support. The kernel will only actually allocate pages as they
are written to, and I could use fallocate[2] with FALLOC_FL_PUNCH_HOLE
to tell the kernel it can drop pages when I'm done with them. This
would mean that an unusually large buffer would only take up lots of
memory while it was in use, and as soon as it was done with, the kernel
would be able to take back the memory. So exactly what I wanted from an
allocator, but with no need for an allocator at all!
This made the implementation much simpler, and by Friday I was able to
get the proxy into a state where it could pass unit tests that
transported messages in both directions through it.
And then it was suggested to me that maybe a virtio proxy is not what I
want after all.
The main disadvantage to a virtio proxy is that it requires context
switching to the host to send data between VMs. This is a trade-off I
was aware of, but a virtio proxy is pretty straightforward to write as
inter-VM communication systems go, and I was not aware of anything else
that would be up to the job. As it turns out, there is something.
vhost-user is a mechanism for connecting, say, a virtio device to a
userspace network stack in a performant way. I was aware of this, but
what I was not aware of was virtio-vhost-user[3]. virtio-vhost-user is
a proposed mechanism to allow a VMM to forward a vhost-user backend to a
VM. This means that two VMs could directly share virtqueues, with no
host copy step. This would mean there would be no opportunity for the
host to mediate communication between two guests, but that wasn't really
on the cards anyway -- if it's ever required, a virtio proxy would
probably be the way to go. For all the other cases, virtio-vhost-user
would be a faster, cleaner way of sharing network devices between VMs.
The main problem with virtio-vhost-user is that it's still in its
infancy. There's a patchset[4] implementing it for QEMU that's a couple
of years old, but that has not been accepted upstream. The main blocker
for this seems to be first standardising it in the Virtio spec[5][6]. The
good news here is that the standardisation process seems to be
progressing actively at the moment. It's being discussed on the
virtio-dev mailing list basically right now, with the most recent emails
dated Friday (unfortunately, I don't know of a good web archive with
virtio-dev, but you can find the thread on Gmane if you're interested
but not subscribed to the list).
The good news is that virtio-vhost-user mostly works by composing things
that already exist. There's no kernel work required, because devices
are just exposed by the VMM as regular virtio devices. The frontend VM
(i.e. the one that uses the virtual device, as opposed to the one that
provides it) doesn't need any special virtio-vhost-user support, because
it just needs to speak normal vhost-user. Only the backend VM needs
support for virtio-vhost-user, because its VMM needs to expose the
vhost-user backend from the host to that VM.
This means that provisionally using virtio-vhost-user in Spectrum
actually looks very feasible, with a couple of compromises. For
evaluation purposes, it's not worth writing a virtio-vhost-user device
for crosvm. But, the VMs that need that device are the ones that are
very specialised -- VMs that manage networking or block devices or
similar. So for these VMs, for now, we could use QEMU, with the
virtio-vhost-user patch. I investigated what it would take to port it
to the most recent QEMU version, and the answer appears to be "not much
at all". Obviously having two VMMs in the Trusted Computing Base (TCB)
isn't something we'd want in the long term, but it would be fine for,
say, reaching the next funding milestone. If we decide that
virtio-vhost-user is the way to go after all, support in crosvm can be
added then -- in general, adding a new virtio device to crosvm isn't a
huge undertaking.
Earlier, I said that the application side of the communication doesn't
need anything special, because to that it's just regular vhost-user.
This is true, but I glossed over there that crosvm doesn't actually
implement vhost-user. Implementing vhost-user in crosvm would probably
be a big deal at this stage, and not something I feel would be a good
use of my time. BUT! Remember, crosvm has two children: Amazon's
Firecracker[7], and at so-called "serverless" computing; and Intel's
Cloud Hypervisor[8], which aims at traditional, full system server
virtualisation. And both of these children inherited the crosvm device
model from their parents, and Cloud Hypervisor implements vhost-user[9].
So I _think_ it should be possible to pretty much lift the vhost-user
implementation from Cloud Hypervisor, and use it in crosvm. Pretty
neat!
So, the setup I'd like to evaluate is QEMU with the virtio-vhost-user
patch on one side, and crosvm with Cloud Hypervisor's vhost-user
implementation on the other.
It might well be that there are complications here. If there are, I'll
probably just finish the proxy and move on for now, because I want to
keep up the pace. I do think that virtio-vhost-user is probably the
way to do interguest networking in the long-term, though.
Another thing that I've realised is that I don't need to worry about
pulling bits out of crosvm to run in other VMs. I focused a lot on that
towards the beginning of the year, mostly motivated by Wayland, because
the virtio wayland implementation in crosvm is the only one there is.
Now that that works in a different way, though, there's no need to
continue down this path, because things like networking can be done in
more normal ways through virtio and the device VM kernel.
[1]: https://en.wikipedia.org/wiki/XY_problem
[2]: https://man7.org/linux/man-pages/man2/fallocate.2.html
[3]: https://wiki.qemu.org/Features/VirtioVhostUser
[4]: https://github.com/stefanha/qemu/compare/master...virtio-vhost-user
[5]: https://lists.nongnu.org/archive/html/qemu-devel/2019-04/msg03082.html
[6]: https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.…
[7]: https://firecracker-microvm.github.io/
[8]: https://github.com/cloud-hypervisor/cloud-hypervisor
[9]: https://github.com/cloud-hypervisor/cloud-hypervisor/blob/b4d04bdff6a7e2c3d…
Overall, it's been frustrating for me to try things, and discover
they're not going to work, or not going to work as well as some other
thing, and make a call on whether to keep going on something I know is
the worse option or switch to the better thing. I have to keep
reminding myself that Spectrum is a research project, and there are
always going to be false starts like this. Lots of what we're doing is
either very unusual (virtio-vhost-user) or brand new (interguest
Wayland), after all.
1
0
After I got an isolated Wayland compositor working last week, I wasn't
really sure what to do next -- this was a big piece of work that I'd
been very focused on for a while. The funding milestone I'm closest to
is to do with implementing hardware isolation, which the Wayland work
was a part of, so I decided to keep going with that, and explore other
types of isolation. More on that in a bit.
Wayland
-------
Posted my patch for virtio_wl display socket support in
libwayland-server[1]. This is what allows it to run in a VM, and
receive connections from clients in other VMs. The patch description is
very extensive, so I recommend reading it for more detail if you're
interested.
It introduces a libvirtio_wl, which should also be useful for porting
other programs that we might want to communicate with across a VM
boundary, if they are written with normal Unix sockets in mind
(including transferring file descriptors). This is the evolution of
code I previously had put in wlroots, moved to Wayland for convenience.
If it ever acquires another user (or maybe even if it doesn't) it might
make sense to make it its own package, since virtio_wl is useful even if
Wayland isn't involved.
[1]: https://spectrum-os.org/lists/archives/spectrum-devel/SJ0PR03MB5581479F3388…
crosvm
------
I pushed all my crosvm changes to get the isolated compositor working to
the work-in-progress "interguest" branch[2]. Remember, I only got it
working last week right before I needed to start writing the TWiS email,
so I hadn't even done that yet! I also posted some patches[3] to the list
to fix a bug in my previous crosvm deadlock fix, and to improve some
related documentation. As usual, these were kindly reviewed by Cole.
Next, I turned my attention to other forms of hardware isolation.
Wayland was a bit special, because despite crosvm including a virtual
"Wayland device", it's not really hardware, and so it required an
approach to isolation that will be quite different to other crosvm
virtual devices. My hope is that other virtual devices should all be
substantially similar to each other.
The basic idea for actual hardware isolation is that rather than having
drivers in the host kernel for USB, network devices, etc. those will be
exposed to dedicated VMs as virtual PCI devices. This should
substantially reduce host kernel attack surface. crosvm virtual devices
will be run in these device VMs, and communicate over virtio with
application VMs as normal. This will require implementing in crosvm a
virtio proxy device, than allows for the crosvm running an application
VM to forward virtio communication to the virtual device running in
userspace in the driver VM.
(The reason devices aren't attached to application VMs directly but run
in seperate device VMs is that hardware is probably not going to be very
happy if multiple kernels are trying to talk to it at the same time.
Additionally, this indirection means that application VMs only have to
use the one virtio driver for that device category, rather than any of
the hundreds of drivers for different hardware in that category. If one
of those drivers had a vulnerability, this should help to contain it to
the device VM.)
So I started writing this virtio proxy. The basic idea is to copy
virtio buffers from application VM guest memory into memory that can be
shared with the userspace virtual device in the device VM. I can't find
any prior art on this (which is not unusual -- not many systems isolate
drivers in this way), so this has required a lot of looking back at the
virtio paper[4] and spec[5] to make sure I understand what to do here.
As I write this, the next problem to solve is integrating some sort of
memory allocator that can manage buffer allocations in the shared memory
that the virtual device looks at. This is a new area for me that I'd
appreciate advice on if anybody can give it -- think of it like, I have a
memfd, mmaped into my process, and I would like to dynamically allocate
and release memory buffers of dynamic sizes in that region. I'm sure
there's a library I'll be able to plug in for this.
[2]: https://spectrum-os.org/git/crosvm/?h=interguest
[3]: https://spectrum-os.org/lists/archives/spectrum-devel/SJ0PR03MB55819DE7E13B…
[4]: https://www.ozlabs.org/~rusty/virtio-spec/virtio-paper.pdf
[5]: https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.…
As usual, big thank you to Cole for reviewing patches, and for finding
room for improvement even in languages/areas he isn't familiar with.
It feels nice to have done some thinking about the project at a slightly
higher level than I have been recently, and to know where I am on the
way to the next milestone. Having taken a lot of time away from the
milestone list this year to work on fundamentals, it's good to feel like
I'm getting back on track.
1
0
This is the problem you might have seen me complaining about on IRC a
couple of days ago. A detailed explanation of the issue and the fix
can be found in the second patch message. The fix consists of two
patches. The first just adds an API that's needed by the refactored,
fixed code. The actual bug fix is in the second patch.
Alyssa Ross (2):
msg_socket: introduce UnixSeqpacketExt
crosvm: fix deadlock on early VmRequest
devices/src/virtio/block.rs | 5 +
msg_socket/src/lib.rs | 52 ++++++----
src/linux.rs | 201 ++++++++++++++++++++++++++++++------
vm_control/src/lib.rs | 57 +++++++---
4 files changed, 245 insertions(+), 70 deletions(-)
--
2.26.2
3
10
08 Jul '20
It's very surprising that a method called "recv" would end up calling
read(). When UnixSeqpacket was introduced in 1d44223f9, the method
was called "read", so this behaviour made more sense. It was renamed
to "recv" in b7196e2a1, but the implementation was not changed to call
libc::recv.
In most cases, according to recv(2), the difference between read() and
recv() doesn't matter:
> The only difference between recv() and read(2) is the presence of
> flags. With a zero flags argument, recv() is generally equivalent
> to read(2) (but see NOTES).
But the NOTES section explains that
> If a zero-length datagram is pending, read(2) and recv() with a
> flags argument of zero provide different behavior. In this
> circumstance, read(2) has no effect (the datagram remains pending),
> while recv() consumes the pending datagram.
This means that, while we might expect UnixSeqpacket::recv(&mut []) to
mirror the behaviour of recv() and discard a message despite the empty
buffer, it would actually leave it there. This became relevant when I
implemented the startup message queue in 5bd2e7f60, because I used an
empty recv to drop the ready message from the socket, since it was
only used for polling and I didn't care about the content. But, it
turns out that my UnixSeqpacket::recv call did nothing, and left the
message on the socket. This wasn't immediately noticeable, but it
meant that when a message came through on the control socket later,
it would actually get the ready message from UnixSeqpacket::recv,
rather than the expected message on the control socket.
For example, trying to use crosvm disk resize would print a message
like the following:
> [ERROR:vm_control/src/lib.rs:649] unexpected disk socket result: Ready
> [ERROR:devices/src/virtio/block.rs:334] Attempted to resize read-only block device
We can see that it does try to process the received message, but first
it tries to do something with the Ready message, which should have
been discarded by now.
With this fix applied, we can see that it now receives and tries to
process the correct message (but then fails because the VM I tested
didn't support disk resizing!):
> [ERROR:devices/src/virtio/block.rs:334] Attempted to resize read-only block device
Fixes: b7196e2a1c1eb7123e7eace5418b7eb4a3e24dbe
Fixes: 5bd2e7f606b726b598fdc26fe26a9b4029fab6d3
Cc: Cole Helbling <cole.e.helbling(a)outlook.com>
---
This is the fix I described in This Week in Spectrum[1].
[1]: https://spectrum-os.org/lists/archives/spectrum-discuss/8736654ij2.fsf@alys…
sys_util/src/net.rs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/sys_util/src/net.rs b/sys_util/src/net.rs
index 71ab3eeb5..82b4f5bb0 100644
--- a/sys_util/src/net.rs
+++ b/sys_util/src/net.rs
@@ -197,7 +197,7 @@ impl UnixSeqpacket {
}
}
- /// Read data from the socket fd to a given buffer
+ /// Receive data from the socket fd to a given buffer.
///
/// # Arguments
/// * `buf` - A mut reference to the data buffer.
@@ -206,11 +206,11 @@ impl UnixSeqpacket {
/// * `usize` - The size of bytes read to the buffer.
///
/// # Errors
- /// Returns error when `libc::read` failed.
+ /// Returns error when `libc::recv` failed.
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
// Safe since we make sure the input `count` == `buf.len()` and handle the returned error.
unsafe {
- let ret = libc::read(self.fd, buf.as_mut_ptr() as *mut _, buf.len());
+ let ret = libc::recv(self.fd, buf.as_mut_ptr() as *mut _, buf.len(), 0);
if ret < 0 {
Err(io::Error::last_os_error())
} else {
--
2.26.2
2
6
I really didn't want this to be another week where I posted about how I
was still trying to patch Wayland to do virtio_wl, and I am delighted to
have just discovered it's not going to be!
crosvm
------
I realised that emulating accept(2) for the Wayland compositor socket in
the way I'd planned would require some crosvm rework. I want to have a
host proxy program that accepts the connection, then connects the
connection socket to crosvm. I had made it possible to dynamically add
sockets to the crosvm Wl device through the control socket, but this
turned out not be enough, because crosvm would store virtio_wl sockets
in a BTreeMap<String, PathBuf>, and then use connect(2) to connect to
the socket when asked to by the guest kernel. This works fine for
e.g. connecting to a host Wayland compositor, which is what crosvm was
designed for, but it wouldn't work for opening a connection socket from
accept(2), because you can only connect to a listening socket.
So instead, I modified the `crosvm wl add' command to take a file
descriptor pointing to the connection socket. I made crosvm store
sockets as an enum that looks like this:
enum WaylandSocket {
Listening(PathBuf),
NonListening(UnixStream),
}
This way, when it gets asked by the VM to connect to a socket, it can
either connect to a listening socket at its path using connect(2), or
just use the existing file descriptor if it's a non-listening socket. A
NonListening socket will be consumed by a connection, so when the VM
close(2)s it, it'll go away, and on the host side the connection will
finish as expected. Listening sockets can be connected to repeatedly,
as before.
I also added support to `crosvm add wl' for dynamic socket names. So
it's possible to do `crosvm add wl wl-conn-%d', and connections will be
added with names like `wl-conn-0', `wl-conn-1', etc. So it's easy to
get unique names for connection sockets. The chosen name is printed by
the command, so the caller knows what name to tell the VM to connect to.
I also found and fixed a bug with the previous crosvm deadlock fix[1].
I had assumed that device_sock.recv(&mut []) would drop a message from
the (SOCK_SEQPACKET) socket, without having to read any of it. But
UnixSeqpacket::recv calls libc::read, and read(2) tells us that:
> In the absence of any errors, or if read() does not check for errors,
> a read() with a count of 0 returns zero and has no other effects.
So this was in fact doing nothing at all. I don't know why crosvm's
UnixSeqpacket::recv calls read() instead of recv(), but it's always been
like that and I'm guessing this sort of thing (from recv(2)) might have
something to do with it:
> The only difference between recv() and read(2) is the presence of
> flags. With a zero flags argument, recv() is generally equivalent to
> read(2) (but see NOTES).
So probably read() just looked like a nicer way to recv() when no flags
were needed.
But, unfortunately, zero-byte reads are when the aforementioned NOTES
section becomes relevant:
> If a zero-length datagram is pending, read(2) and recv() with a flags
> argument of zero provide different behavior. In this circumstance,
> read(2) has no effect (the datagram remains pending), while recv()
> consumes the pending datagram.
So, my assumption that UnixSeqpacket::recv(&mut []) would consume a
message turned out to be quite reasonable -- the surprising thing was
that a method called `recv' would call read() rather than recv(). I
think the best fix here will be to just make it call recv() instead,
rather than modifying my code to do UnixSeqpacket::recv(&mut [0]) or
something, to prevent further nasty surprises with this in future.
[1]: https://spectrum-os.org/lists/archives/spectrum-devel/20200614114344.22642-…
Wayland
-------
I created API-compatible implementations of the libc sendmsg(2) and
recvmsg(2) functions for virtio_wl sockets. This was quite an
achievement, because the API (which allows you to send and receive data
and file descriptors, as well as other things I don't intend to support)
is rather arcane (see the example in cmsg(3) if you're not familiar with
them). I wrote unit tests for them, and it took a long time before they
worked reliably. Once I had these, though, I could find the places
where Wayland called sendmsg() and recvmsg() and fall back to the
virtio_wl-based implementations if the standard functions failed with
ENOTSOCK. I stubbed out some stuff that isn't going to work over
virtio_wl, like looking up the pid of the Wayland client through
getsockopt(2).
I also had to resort to a few hacks, like faking support for
MSG_DONTWAIT by using fcntl(2) to set O_NONBLOCK on the socket,
recv()ing from it, and then removing O_NONBLOCK again, or faking
mremap(2) by munmap()-ing and mmap()-ing. We will want to clean these
up later by implementing the required missing functionality in the
virtio_wl kernel module. In the first case, at least, this should be
pretty straightforward, because it supports non-blocking operations if
the socket is O_NONBLOCK -- it just needs to accept a MSG_DONTWAIT
option as well. The VIRTWL_IOCTL_{SEND,RECV} syscalls don't currently
have a flags argument, so that'll need to be added.
I implemented this bit by bit, at every step trying to run Alacritty on
my host system, connected to the virtio_wl Wayland server socket through
the accept() proxy, and using strace and some printf()-debugging to see
where the Wayland compositor in the VM would get stuck, and about an
hour ago, it finally worked! For the first time, a Wayland compositor
running in a VM can display an application running outside of it.
(Obviously we'll want the application to be running in another VM rather
than on the host, but that's similar enough that it probably works
already -- I just haven't tested it yet.) This feels like a huge
achievement. I've been working towards it for so long.
Next week, I'll be cleaning up this code and posting patches for all of
it. Then I'll probably move on to other sorts of device virtualization,
like running a virtual network device in a VM. I'm feeling so much more
positive about the direction of the project than I was before. It's
been difficult to make myself keep going making little progress for the
last couple of weeks, and it's great that I've managed to pick things up
again so much. I hope that the level of detail in this email is enough
to make up for the brevity of last week's! I'm sending late again, too,
but only by a couple of minutes -- I didn't expect this email to take
over an hour to write, but there we go.
Thanks for reading! I hope you're looking forward to seeing where
things go from here as much as I am.
1
0
This is going to be a very short one, and it's coming a day late,
because I have been busy and didn't want to stop yesterday. 2020-W22's
TWiS was sent a day early, so the delay is balanced out. :)
Wayland
-------
I'm finally making progress with the Wayland stuff I've been staring at
for the past two weeks! Now that I'm unblocked, things feel like
they're moving very quickly.
I've modified a Wayland compositor and libwayland-server to use a
virtio_wl socket. I've also implemented the compositor side of the
accept(2) fallback. Just now, I finished writing a proxy program that
implements the host side of that. Next up is testing it.
After that, I think we might be there? But it's also possible I have my
head so deep in it there's another bit that I've forgotten.
1
0
Another week that ended up being a detour from the Wayland work I wanted
to do, unfortunately. I think this will be the end of it, though.
crosvm
------
Fixed[1] an error in my crosvm deadlock fix[2] from last week. Due to
an oversight (obvious as soon as I tried to test the affected devices,
and from looking at the code in retrospect), this actually broke the
devices that were *not* affected by the deadlock, which I had neglected
to properly test. Fortunately, it was a very small and easy fix.
I also had an interesting (and ongoing) conversation[3] with Cole about
the deadlock fix in the mailing list thread. It's one of those
situations where getting to talk about the code with another person and
having to explain things is going to result in much better code. So
we'll end up with a much tidier fix than what I came up with on my own.
Thanks to Cole for his feedback and this opportunity. It's great to
know that somebody else is passing an eye over my code.
[1]: https://spectrum-os.org/git/crosvm/commit/?id=ca5bdd2ac3e473e9b082c44c2870f…
[2]: https://spectrum-os.org/lists/archives/spectrum-devel/20200614114344.22642-…
[3]: https://spectrum-os.org/lists/archives/spectrum-devel/CH2PR14MB357902F38908…
Wayland
-------
My current area of attention is modifying Wayland to use a virtio_wl
socket as the display socket. Preparing to start implementing that this
week, it occurred to me that working on it using my current setup was
going to be extremely time consuming. Up until now, I've been doing Nix
VM rebuilds when I want to test a change to, for example, wlroots. This
means rebuilding, in that case, wlroots and all dependencies
(i.e. Wayfire). In the case of wlroots, it's not so bad because
rebuilding two packages doesn't take all that long, but it wasn't going
to work for Wayland.
So I decided that the best way forward would be to create a VM running a
traditional Linux distribution, in which I could hack on Wayland without
having to restart the VM or rebuild all of its dependents. To that end,
I spent much of this week setting up an Alpine VM that can be run under
both crosvm and QEMU. (Configuring networking with crosvm is hard for a
one-off like this, but QEMU has a built-in userspace networking stack[4]
that makes it easy, so when I want to, e.g. install a package, I can
boot with QEMU to do that, and then go back to crosvm for testing
virtio_wl things.) This resulted in a Nixpkgs pull request[5] to
package Alpine's apk package manager, which was used in building the VM
image.
As of this afternoon, I have this VM all set up and ready to go. I have
a script that runs it under crosvm with a virtio_wl socket available to
try to connect Wayland to, and another virtio_wl socket connected to the
host Wayland compositor for testing. It also sets up some limited
networking that allows me to SSH into the VM, but does not allow the VM
to access the internet. A separate script runs the VM under QEMU with
full networking. The VM has Wayland, wlroots, and Sway (the choice of
wlroots-based compositor is irrelevant to this work, and Sway is easier
to build) installed from source, and because I can SSH to it I can edit
files in the VM easily from my host Emacs using TRAMP[6].
All of this will allow me to iterate on the Wayland work without having
to wait upwards of an hour to test each change. This was actually the
first time I'd ever booted a VM image in crosvm that I hadn't generated
specifically for that purpose myself with Nix. I improved my
understanding of crosvm in a few ways in the process (although not
anything concrete enough I can think of to write down here).
[4]: https://wiki.qemu.org/Documentation/Networking#User_Networking_.28SLIRP.29
[5]: https://github.com/NixOS/nixpkgs/pull/90676
[6]: https://www.gnu.org/software/tramp/
Next week, I'll continue with the Wayland work. It shouldn't be all
that much work in the end, but getting set up to actually be able to do
it has been annoyingly complicated.
1
0