From 58daab21cd043e1dc37024a7f99b396788372918 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 9 Mar 2024 14:19:48 +0100 Subject: Merging upstream version 1.44.3. Signed-off-by: Daniel Baumann --- .../h2o/libh2o/misc/p5-Server-Starter/.gitignore | 32 ++ .../h2o/libh2o/misc/p5-Server-Starter/.travis.yml | 8 + .../h2o/libh2o/misc/p5-Server-Starter/Build.PL | 65 +++ .../h2o/libh2o/misc/p5-Server-Starter/Changes | 104 ++++ .../h2o/libh2o/misc/p5-Server-Starter/LICENSE | 379 +++++++++++++ .../h2o/libh2o/misc/p5-Server-Starter/META.json | 89 +++ .../h2o/libh2o/misc/p5-Server-Starter/README.md | 58 ++ .../misc/p5-Server-Starter/builder/MyBuilder.pm | 13 + .../h2o/libh2o/misc/p5-Server-Starter/cpanfile | 9 + .../misc/p5-Server-Starter/lib/Server/Starter.pm | 603 +++++++++++++++++++++ .../p5-Server-Starter/lib/Server/Starter/Guard.pm | 21 + .../h2o/libh2o/misc/p5-Server-Starter/minil.toml | 2 + .../misc/p5-Server-Starter/script/start_server | 179 ++++++ .../h2o/libh2o/misc/p5-Server-Starter/t/00-base.t | 8 + .../misc/p5-Server-Starter/t/01-starter-echod.pl | 35 ++ .../libh2o/misc/p5-Server-Starter/t/01-starter.t | 82 +++ .../p5-Server-Starter/t/02-startfail-server.pl | 32 ++ .../libh2o/misc/p5-Server-Starter/t/02-startfail.t | 61 +++ .../p5-Server-Starter/t/03-starter-unix-echod.pl | 22 + .../misc/p5-Server-Starter/t/03-starter-unix.t | 40 ++ .../misc/p5-Server-Starter/t/04-starter-dir.t | 54 ++ .../p5-Server-Starter/t/05-killolddelay-echod.pl | 35 ++ .../misc/p5-Server-Starter/t/05-killolddelay.t | 87 +++ .../p5-Server-Starter/t/06-autorestart-echod.pl | 34 ++ .../misc/p5-Server-Starter/t/06-autorestart.t | 84 +++ .../misc/p5-Server-Starter/t/07-envdir-print.pl | 29 + .../libh2o/misc/p5-Server-Starter/t/07-envdir.t | 77 +++ .../h2o/libh2o/misc/p5-Server-Starter/t/08-wait3.t | 39 ++ .../h2o/libh2o/misc/p5-Server-Starter/t/09-guard.t | 31 ++ .../misc/p5-Server-Starter/t/10-bindaddr-server.pl | 26 + .../libh2o/misc/p5-Server-Starter/t/10-bindaddr.t | 55 ++ .../p5-Server-Starter/t/11-specified-fd-server.pl | 25 + .../misc/p5-Server-Starter/t/11-specified-fd.t | 40 ++ .../misc/p5-Server-Starter/t/12-stop-server.pl | 24 + .../h2o/libh2o/misc/p5-Server-Starter/t/12-stop.t | 50 ++ .../misc/p5-Server-Starter/t/13-unix-daemonize.t | 68 +++ 36 files changed, 2600 insertions(+) create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/.gitignore create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/.travis.yml create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/Build.PL create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/Changes create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/LICENSE create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/META.json create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/README.md create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/builder/MyBuilder.pm create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/cpanfile create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/lib/Server/Starter.pm create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/lib/Server/Starter/Guard.pm create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/minil.toml create mode 100755 web/server/h2o/libh2o/misc/p5-Server-Starter/script/start_server create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/00-base.t create mode 100755 web/server/h2o/libh2o/misc/p5-Server-Starter/t/01-starter-echod.pl create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/01-starter.t create mode 100755 web/server/h2o/libh2o/misc/p5-Server-Starter/t/02-startfail-server.pl create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/02-startfail.t create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/03-starter-unix-echod.pl create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/03-starter-unix.t create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/04-starter-dir.t create mode 100755 web/server/h2o/libh2o/misc/p5-Server-Starter/t/05-killolddelay-echod.pl create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/05-killolddelay.t create mode 100755 web/server/h2o/libh2o/misc/p5-Server-Starter/t/06-autorestart-echod.pl create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/06-autorestart.t create mode 100755 web/server/h2o/libh2o/misc/p5-Server-Starter/t/07-envdir-print.pl create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/07-envdir.t create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/08-wait3.t create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/09-guard.t create mode 100755 web/server/h2o/libh2o/misc/p5-Server-Starter/t/10-bindaddr-server.pl create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/10-bindaddr.t create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/11-specified-fd-server.pl create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/11-specified-fd.t create mode 100755 web/server/h2o/libh2o/misc/p5-Server-Starter/t/12-stop-server.pl create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/12-stop.t create mode 100644 web/server/h2o/libh2o/misc/p5-Server-Starter/t/13-unix-daemonize.t (limited to 'web/server/h2o/libh2o/misc/p5-Server-Starter') diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/.gitignore b/web/server/h2o/libh2o/misc/p5-Server-Starter/.gitignore new file mode 100644 index 000000000..b76c8adfc --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/.gitignore @@ -0,0 +1,32 @@ +Makefile +inc/ +.c +ppport.h +.DS_Store +.*.sw[pon] +*.bak +*.old +Build +_build/ +xshelper.h +META.yml +MYMETA.yml +MYMETA.json +MANIFEST +MANIFEST.bak +.online +blib/ +pm_to_blib +nytprof* +core +perltidy.ERR +tmp/ +Server-Starter-*.tar.gz +/Server-Starter-* +/.build +/_build_params +/Build +/Build.bat +!Build/ +!META.json +!LICENSE diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/.travis.yml b/web/server/h2o/libh2o/misc/p5-Server-Starter/.travis.yml new file mode 100644 index 000000000..f3168e947 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/.travis.yml @@ -0,0 +1,8 @@ +language: perl +sudo: false +perl: + - "5.12" + - "5.14" + - "5.16" + - "5.18" + - "5.20" diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/Build.PL b/web/server/h2o/libh2o/misc/p5-Server-Starter/Build.PL new file mode 100644 index 000000000..ffc64f5af --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/Build.PL @@ -0,0 +1,65 @@ +# ========================================================================= +# THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA. +# DO NOT EDIT DIRECTLY. +# ========================================================================= + +use 5.008_001; + +use strict; +use warnings; +use utf8; + +use builder::MyBuilder; +use File::Basename; +use File::Spec; + +my %args = ( + license => 'perl', + dynamic_config => 0, + + configure_requires => { + 'Module::Build' => 0.38, + }, + + name => 'Server-Starter', + module_name => 'Server::Starter', + allow_pureperl => 0, + + script_files => [glob('script/*'), glob('bin/*')], + c_source => [qw()], + PL_files => {}, + + test_files => ((-d '.git' || $ENV{RELEASE_TESTING}) && -d 'xt') ? 't/ xt/' : 't/', + recursive_test_files => 1, + + +); +if (-d 'share') { + $args{share_dir} = 'share'; +} + +my $builder = builder::MyBuilder->subclass( + class => 'MyBuilder', + code => q{ + sub ACTION_distmeta { + die "Do not run distmeta. Install Minilla and `minil install` instead.\n"; + } + sub ACTION_installdeps { + die "Do not run installdeps. Run `cpanm --installdeps .` instead.\n"; + } + } +)->new(%args); +$builder->create_build_script(); + +use File::Copy; + +print "cp META.json MYMETA.json\n"; +copy("META.json","MYMETA.json") or die "Copy failed(META.json): $!"; + +if (-f 'META.yml') { + print "cp META.yml MYMETA.yml\n"; + copy("META.yml","MYMETA.yml") or die "Copy failed(META.yml): $!"; +} else { + print "There is no META.yml... You may install this module from the repository...\n"; +} + diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/Changes b/web/server/h2o/libh2o/misc/p5-Server-Starter/Changes new file mode 100644 index 000000000..1232b8649 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/Changes @@ -0,0 +1,104 @@ +Revision history for Perl extension Server::Starter. + +{{$NEXT}} + +0.31 2015-07-20T02:38:57Z + - do not remove the socket file when becoming a daemon (thanks to andyjones) #34 #36 + - emit name of the directory to which it failed to chdir(2) (thanks to tokuhirom) #33 + +0.30 2015-06-05T05:28:43Z + - unlink the status file only when created by itself (thanks to tokuhirom) #32 + - redo #26 (thanks to tokuhirom) #31 + +0.29 2015-06-04T06:45:26Z + - build should fail on Windows (thanks to chorny) #26 + - add `--stop` option (thanks to tokuhirom) #28 + - do not close STDIN in case the listening port is mapped to fd zero (thanks to tokuhirom) #29 #24 + - reopen STDIN to suppress unnecessary warnings (thanks to touhirom) #30 + +0.28 2015-05-28T22:08:37Z + - add `--port=[host:]port=fd` option for specifying the file descriptor number (thanks to tokuhirom) #24 + +0.27 2015-04-28T01:02:28Z + - revert 0.26 so that the install script can update the shebang (thanks to miyagawa) #22 + - modernize the build tool (thanks to miyagawa) #23 + +0.26 + - `start_server` command uses perl found in $PATH instead of /usr/bin/perl #21 + +0.25 + - fix `already in use` error if the program is restarted (regression in 0.21) #18 + - tests now pass on environments wo. IPv6 support #19 + +0.24 + - introduce --daemonize option (#18 #6) + - fix bug that causes a infinite loop in shutdown (amends #14) + +0.23 + - set IPV6_V6ONLY for socket bound to an IPv6 address (#16) + +0.22 + - support for IPv6 (#16) + - include repository URL in META.yml (#15; thanks to ether) + +0.21 + - remove dependency against non-standard modules (#14) + +0.19 + - reimplement changes in 0.15, 0.16 for stability (#13) + - update inc/Module/Install + +0.17 + - add option: --backlog to change the backlog size (default: SOMAXCONN) (thanks to Yuryu) + +0.16 + - [bugfix] unset the environment variable when a file is removed from the directory specified by --envdir + +0.15 + - added option: --envdir for reloading configuration (thanks to limitusus) + - added options: --enable-auto-restart and interval for periodical automatic restarting (thanks to limitusus) + - added option: --kill-old-delay for delaying SIGTERM (thanks to limitusus) + +0.14 + - fix regression in 0.13; start_server wo. "--dir" was causing errors + +0.13 + - add option: --dir (thanks to kazeburo) + +0.12 + - bugfix: support for programs with whitespaces (thanks to clkao) + - add option: --signal-on-term (thanks to miyagawa) + +0.11 + - remove unix socket file on shutdown + +0.10 + - support for unix sockets with --path option + +0.09 + - added options: --signal-on-hup, --status-file, --restart + +0.08 + - added --pid-file option + +0.07 Sat May 08 14:00:00 2010 + - --port option is now omittable (so daemons _not_ binding to TCP ports (like FCGI servers binding to unix domain sockets) can be hot-deployied using Server::Starter) + +0.06 Sat Jan 02 10:26:00 2010 + - bugfix: start_server did not shutdown upon receiving SIGINT while the worker is failing to start up + +0.05 Tue Oct 13 20:30:00 2009 + - use $^X in tests (or tests will fail on hosts using a different installation of perl from one installed to /usr/bin/perl) + +0.04 Fri Oct 09 19:25:00 2009 + - signals received just after spawning worker process were dismissed (thanks to kazeburo for reporting and helping fix the problem) + +0.03 Thu Sep 24 20:00:00 2009 + - require 5.008 + - require Test::TCP 0.11 to satisfy the dependencies + +0.02 Thu Sep 09 17:06:00 2009 + - add README, Changes + +0.01 Thu Sep 09 17:00:00 2009 + - initial version diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/LICENSE b/web/server/h2o/libh2o/misc/p5-Server-Starter/LICENSE new file mode 100644 index 000000000..a4d95a593 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/LICENSE @@ -0,0 +1,379 @@ +This software is copyright (c) 2015 by Kazuho Oku. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +Terms of the Perl programming language system itself + +a) the GNU General Public License as published by the Free + Software Foundation; either version 1, or (at your option) any + later version, or +b) the "Artistic License" + +--- The GNU General Public License, Version 1, February 1989 --- + +This software is Copyright (c) 2015 by Kazuho Oku. + +This is free software, licensed under: + + The GNU General Public License, Version 1, February 1989 + + GNU GENERAL PUBLIC LICENSE + Version 1, February 1989 + + Copyright (C) 1989 Free Software Foundation, Inc. + 51 Franklin St, 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 license agreements of most software companies try to keep users +at the mercy of those companies. By contrast, our 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. The +General Public License applies to the Free Software Foundation's +software and to any other program whose authors commit to using it. +You can use it for your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Specifically, the General Public License is designed to make +sure that you have the freedom to give away or sell copies of free +software, 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 a 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 tell them 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. + + 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 Agreement 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 work containing the +Program or a portion of it, either verbatim or with modifications. Each +licensee is addressed as "you". + + 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 +General Public License and to the absence of any warranty; and give any +other recipients of the Program a copy of this General Public License +along with the Program. You may charge a fee for the physical act of +transferring a copy. + + 2. You may modify your copy or copies of the Program or any portion of +it, and copy and distribute such modifications under the terms of Paragraph +1 above, provided that you also do the following: + + a) cause the modified files to carry prominent notices stating that + you changed the files and the date of any change; and + + b) cause the whole of any work that you distribute or publish, that + in whole or in part contains the Program or any part thereof, either + with or without modifications, to be licensed at no charge to all + third parties under the terms of this General Public License (except + that you may choose to grant warranty protection to some or all + third parties, at your option). + + c) If the modified program normally reads commands interactively when + run, you must cause it, when started running for such interactive use + in the simplest and most usual 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 General + Public License. + + d) 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. + +Mere aggregation of another independent work with the Program (or its +derivative) on a volume of a storage or distribution medium does not bring +the other work under the scope of these terms. + + 3. You may copy and distribute the Program (or a portion or derivative of +it, under Paragraph 2) in object code or executable form under the terms of +Paragraphs 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 + Paragraphs 1 and 2 above; or, + + b) accompany it with a written offer, valid for at least three + years, to give any third party free (except for a nominal charge + for the cost of distribution) a complete machine-readable copy of the + corresponding source code, to be distributed under the terms of + Paragraphs 1 and 2 above; or, + + c) accompany it with the information you received as to where the + corresponding source code may be obtained. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form alone.) + +Source code for a work means the preferred form of the work for making +modifications to it. For an executable file, complete source code means +all the source code for all modules it contains; but, as a special +exception, it need not include source code for modules which are standard +libraries that accompany the operating system on which the executable +file runs, or for standard header files or definitions files that +accompany that operating system. + + 4. You may not copy, modify, sublicense, distribute or transfer the +Program except as expressly provided under this General Public License. +Any attempt otherwise to copy, modify, sublicense, distribute or transfer +the Program is void, and will automatically terminate your rights to use +the Program under this License. However, parties who have received +copies, or rights to use copies, from you under this General Public +License will not have their licenses terminated so long as such parties +remain in full compliance. + + 5. By copying, distributing or modifying 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. + + 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. + + 7. 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 the 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 +the license, you may choose any version ever published by the Free Software +Foundation. + + 8. 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 + + 9. 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. + + 10. 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 + + Appendix: 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 humanity, 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. + + + Copyright (C) 19yy + + 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 1, 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) 19xx 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 a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + program `Gnomovision' (a program to direct compilers to make passes + at assemblers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +That's all there is to it! + + +--- The Artistic License 1.0 --- + +This software is Copyright (c) 2015 by Kazuho Oku. + +This is free software, licensed under: + + The Artistic License 1.0 + +The Artistic License + +Preamble + +The intent of this document is to state the conditions under which a Package +may be copied, such that the Copyright Holder maintains some semblance of +artistic control over the development of the package, while giving the users of +the package the right to use and distribute the Package in a more-or-less +customary fashion, plus the right to make reasonable modifications. + +Definitions: + + - "Package" refers to the collection of files distributed by the Copyright + Holder, and derivatives of that collection of files created through + textual modification. + - "Standard Version" refers to such a Package if it has not been modified, + or has been modified in accordance with the wishes of the Copyright + Holder. + - "Copyright Holder" is whoever is named in the copyright or copyrights for + the package. + - "You" is you, if you're thinking about copying or distributing this Package. + - "Reasonable copying fee" is whatever you can justify on the basis of media + cost, duplication charges, time of people involved, and so on. (You will + not be required to justify it to the Copyright Holder, but only to the + computing community at large as a market that must bear the fee.) + - "Freely Available" means that no fee is charged for the item itself, though + there may be fees involved in handling the item. It also means that + recipients of the item may redistribute it under the same conditions they + received it. + +1. You may make and give away verbatim copies of the source form of the +Standard Version of this Package without restriction, provided that you +duplicate all of the original copyright notices and associated disclaimers. + +2. You may apply bug fixes, portability fixes and other modifications derived +from the Public Domain or from the Copyright Holder. A Package modified in such +a way shall still be considered the Standard Version. + +3. You may otherwise modify your copy of this Package in any way, provided that +you insert a prominent notice in each changed file stating how and when you +changed that file, and provided that you do at least ONE of the following: + + a) place your modifications in the Public Domain or otherwise make them + Freely Available, such as by posting said modifications to Usenet or an + equivalent medium, or placing the modifications on a major archive site + such as ftp.uu.net, or by allowing the Copyright Holder to include your + modifications in the Standard Version of the Package. + + b) use the modified Package only within your corporation or organization. + + c) rename any non-standard executables so the names do not conflict with + standard executables, which must also be provided, and provide a separate + manual page for each non-standard executable that clearly documents how it + differs from the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +4. You may distribute the programs of this Package in object code or executable +form, provided that you do at least ONE of the following: + + a) distribute a Standard Version of the executables and library files, + together with instructions (in the manual page or equivalent) on where to + get the Standard Version. + + b) accompany the distribution with the machine-readable source of the Package + with your modifications. + + c) accompany any non-standard executables with their corresponding Standard + Version executables, giving the non-standard executables non-standard + names, and clearly documenting the differences in manual pages (or + equivalent), together with instructions on where to get the Standard + Version. + + d) make other distribution arrangements with the Copyright Holder. + +5. You may charge a reasonable copying fee for any distribution of this +Package. You may charge any fee you choose for support of this Package. You +may not charge a fee for this Package itself. However, you may distribute this +Package in aggregate with other (possibly commercial) programs as part of a +larger (possibly commercial) software distribution provided that you do not +advertise this Package as a product of your own. + +6. The scripts and library files supplied as input to or produced as output +from the programs of this Package do not automatically fall under the copyright +of this Package, but belong to whomever generated them, and may be sold +commercially, and may be aggregated with this Package. + +7. C or perl subroutines supplied by you and linked into this Package shall not +be considered part of this Package. + +8. The name of the Copyright Holder may not be used to endorse or promote +products derived from this software without specific prior written permission. + +9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + +The End + diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/META.json b/web/server/h2o/libh2o/misc/p5-Server-Starter/META.json new file mode 100644 index 000000000..64f85645b --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/META.json @@ -0,0 +1,89 @@ +{ + "abstract" : "a superdaemon for hot-deploying server programs", + "author" : [ + "Kazuho Oku" + ], + "dynamic_config" : 0, + "generated_by" : "Minilla/v2.3.0, CPAN::Meta::Converter version 2.150001", + "license" : [ + "perl_5" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : "2" + }, + "name" : "Server-Starter", + "no_index" : { + "directory" : [ + "t", + "xt", + "inc", + "share", + "eg", + "examples", + "author", + "builder" + ] + }, + "prereqs" : { + "configure" : { + "requires" : { + "Module::Build" : "0.38" + } + }, + "develop" : { + "requires" : { + "Test::CPAN::Meta" : "0", + "Test::MinimumVersion::Fast" : "0.04", + "Test::PAUSE::Permissions" : "0.04", + "Test::Pod" : "1.41", + "Test::Spellunker" : "v0.2.7" + } + }, + "runtime" : { + "requires" : { + "perl" : "5.008" + } + }, + "test" : { + "requires" : { + "IO::Socket::IP" : "0", + "Net::EmptyPort" : "0", + "Test::Requires" : "0", + "Test::SharedFork" : "0", + "Test::TCP" : "2.08" + } + } + }, + "release_status" : "unstable", + "resources" : { + "bugtracker" : { + "web" : "https://github.com/kazuho/p5-Server-Starter/issues" + }, + "homepage" : "https://github.com/kazuho/p5-Server-Starter", + "repository" : { + "type" : "git", + "url" : "git://github.com/kazuho/p5-Server-Starter.git", + "web" : "https://github.com/kazuho/p5-Server-Starter" + } + }, + "version" : "0.31", + "x_contributors" : [ + "kazuho ", + "yappo ", + "Robert Buels ", + "chromatic ", + "Chia-liang Kao ", + "Masahiro Nagano ", + "Tomoya Kabe ", + "Haruka Iwao ", + "Kazuho Oku ", + "Karen Etheridge ", + "Bugdebugger ", + "Tatsuhiko Miyagawa ", + "Alexandr Ciornii ", + "Tokuhiro Matsuno ", + "Andy Jones ", + "Kazuho Oku " + ] +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/README.md b/web/server/h2o/libh2o/misc/p5-Server-Starter/README.md new file mode 100644 index 000000000..c4edcc0c3 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/README.md @@ -0,0 +1,58 @@ +# NAME + +Server::Starter - a superdaemon for hot-deploying server programs + +# SYNOPSIS + + # from command line + % start_server --port=80 my_httpd + + # in my_httpd + use Server::Starter qw(server_ports); + + my $listen_sock = IO::Socket::INET->new( + Proto => 'tcp', + ); + $listen_sock->fdopen((values %{server_ports()})[0], 'w') + or die "failed to bind to listening socket:$!"; + + while (1) { + if (my $conn = $listen_sock->accept) { + .... + } + } + +# DESCRIPTION + +It is often a pain to write a server program that supports graceful restarts, with no resource leaks. [Server::Starter](https://metacpan.org/pod/Server::Starter) solves the problem by splitting the task into two. One is [start\_server](https://metacpan.org/pod/start_server), a script provided as a part of the module, which works as a superdaemon that binds to zero or more TCP ports or unix sockets, and repeatedly spawns the server program that actually handles the necessary tasks (for example, responding to incoming commenctions). The spawned server programs under [Server::Starter](https://metacpan.org/pod/Server::Starter) call accept(2) and handle the requests. + +To gracefully restart the server program, send SIGHUP to the superdaemon. The superdaemon spawns a new server program, and if (and only if) it starts up successfully, sends SIGTERM to the old server program. + +By using [Server::Starter](https://metacpan.org/pod/Server::Starter) it is much easier to write a hot-deployable server. Following are the only requirements a server program to be run under [Server::Starter](https://metacpan.org/pod/Server::Starter) should conform to: + +\- receive file descriptors to listen to through an environment variable +\- perform a graceful shutdown when receiving SIGTERM + +A Net::Server personality that can be run under [Server::Starter](https://metacpan.org/pod/Server::Starter) exists under the name [Net::Server::SS::PreFork](https://metacpan.org/pod/Net::Server::SS::PreFork). + +# METHODS + +- server\_ports + + Returns zero or more file descriptors on which the server program should call accept(2) in a hashref. Each element of the hashref is: (host:port|port|path\_of\_unix\_socket) => file\_descriptor. + +- start\_server + + Starts the superdaemon. Used by the `start_server` script. + +# AUTHOR + +Kazuho Oku + +# SEE ALSO + +[Net::Server::SS::PreFork](https://metacpan.org/pod/Net::Server::SS::PreFork) + +# LICENSE + +This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/builder/MyBuilder.pm b/web/server/h2o/libh2o/misc/p5-Server-Starter/builder/MyBuilder.pm new file mode 100644 index 000000000..8c8982d45 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/builder/MyBuilder.pm @@ -0,0 +1,13 @@ +package builder::MyBuilder; +use strict; +use warnings; +use base qw(Module::Build); + + +# https://github.com/kazuho/p5-Server-Starter/issues/25 +if (lc $^O eq "mswin32") { + die "OS unsupported\n"; #will result in NA by cpantesters +} + +1; + diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/cpanfile b/web/server/h2o/libh2o/misc/p5-Server-Starter/cpanfile new file mode 100644 index 000000000..de18fb959 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/cpanfile @@ -0,0 +1,9 @@ +requires 'perl', '5.008'; + +on test => sub { + requires 'IO::Socket::IP'; + requires 'Net::EmptyPort'; + requires 'Test::Requires'; + requires 'Test::SharedFork'; + requires 'Test::TCP', '2.08'; +}; diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/lib/Server/Starter.pm b/web/server/h2o/libh2o/misc/p5-Server-Starter/lib/Server/Starter.pm new file mode 100644 index 000000000..f33d232d9 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/lib/Server/Starter.pm @@ -0,0 +1,603 @@ +package Server::Starter; + +use 5.008; +use strict; +use warnings; +use Carp; +use Fcntl; +use IO::Handle; +use IO::Socket::UNIX; +use POSIX qw(:sys_wait_h); +use Socket (); +use Server::Starter::Guard; +use Fcntl qw(:flock); + +use Exporter qw(import); + +our $VERSION = '0.31'; +our @EXPORT_OK = qw(start_server restart_server stop_server server_ports); + +my @signals_received; + +sub start_server { + my $opts = { + (@_ == 1 ? @$_[0] : @_), + }; + $opts->{interval} = 1 + if not defined $opts->{interval}; + $opts->{signal_on_hup} ||= 'TERM'; + $opts->{signal_on_term} ||= 'TERM'; + $opts->{backlog} ||= Socket::SOMAXCONN(); + for ($opts->{signal_on_hup}, $opts->{signal_on_term}) { + # normalize to the one that can be passed to kill + tr/a-z/A-Z/; + s/^SIG//i; + } + + # prepare args + my $ports = $opts->{port}; + my $paths = $opts->{path}; + croak "either of ``port'' or ``path'' option is mandatory\n" + unless $ports || $paths; + $ports = [ $ports ] + if ! ref $ports && defined $ports; + $paths = [ $paths ] + if ! ref $paths && defined $paths; + croak "mandatory option ``exec'' is missing or is not an arrayref\n" + unless $opts->{exec} && ref $opts->{exec} eq 'ARRAY'; + + # set envs + $ENV{ENVDIR} = $opts->{envdir} + if defined $opts->{envdir}; + $ENV{ENABLE_AUTO_RESTART} = $opts->{enable_auto_restart} + if defined $opts->{enable_auto_restart}; + $ENV{KILL_OLD_DELAY} = $opts->{kill_old_delay} + if defined $opts->{kill_old_delay}; + $ENV{AUTO_RESTART_INTERVAL} = $opts->{auto_restart_interval} + if defined $opts->{auto_restart_interval}; + + # open log file + my $logfh; + if ($opts->{log_file}) { + if ($opts->{log_file} =~ /^\s*\|\s*/s) { + my $cmd = $'; + open $logfh, '|-', $cmd + or die "failed to open pipe:$opts->{log_file}: $!"; + } else { + open $logfh, '>>', $opts->{log_file} + or die "failed to open log file:$opts->{log_file}: $!"; + } + $logfh->autoflush(1); + } + + # create guard that removes the status file + my $status_file_created; + my $status_file_guard = $opts->{status_file} && Server::Starter::Guard->new( + sub { + if ($status_file_created) { + unlink $opts->{status_file}; + } + }, + ); + + print STDERR "start_server (pid:$$) starting now...\n"; + + # start listening, setup envvar + my @sock; + my @sockenv; + for my $hostport (@$ports) { + my ($domain, $sa); + my $fd; + my $sockopts = sub {}; + if ($hostport =~ /^\s*(\d+)(?:\s*=(\d+))?\s*$/) { + # by default, only bind to IPv4 (for compatibility) + $hostport = $1; + $fd = $2; + $domain = Socket::PF_INET; + $sa = pack_sockaddr_in $1, Socket::inet_aton("0.0.0.0"); + } elsif ($hostport =~ /^\s*(?:\[\s*|)([^\]]*)\s*(?:\]\s*|):\s*(\d+)(?:\s*=(\d+))?\s*$/) { + my ($host, $port) = ($1, $2); + $fd = $3; + if ($host =~ /:/) { + # IPv6 + local $@; + eval { + $hostport = "[$host]:$port"; + my $addr = Socket::inet_pton(Socket::AF_INET6(), $host) + or die "failed to resolve host:$host:$!"; + $sa = Socket::pack_sockaddr_in6($port, $addr); + $domain = Socket::PF_INET6(); + }; + if ($@) { + die "No support for IPv6. Please update Perl (or Perl modules)"; + } + $sockopts = sub { + my $sock = shift; + local $@; + eval { + setsockopt $sock, Socket::IPPROTO_IPV6(), Socket::IPV6_V6ONLY(), 1; + }; + }; + } else { + # IPv4 + $domain = Socket::PF_INET; + $hostport = "$host:$port"; + my $addr = gethostbyname $host + or die "failed to resolve host:$host:$!"; + $sa = Socket::pack_sockaddr_in($port, $addr); + } + } else { + croak "invalid ``port'' value:$hostport\n" + } + socket my $sock, $domain, Socket::SOCK_STREAM(), 0 + or die "failed to create socket:$!"; + setsockopt $sock, Socket::SOL_SOCKET, Socket::SO_REUSEADDR(), pack("l", 1); + $sockopts->($sock); + bind $sock, $sa + or die "failed to bind to $hostport:$!"; + listen $sock, $opts->{backlog} + or die "listen(2) failed:$!"; + fcntl($sock, F_SETFD, my $flags = '') + or die "fcntl(F_SETFD, 0) failed:$!"; + if (defined $fd) { + POSIX::dup2($sock->fileno, $fd) + or die "dup2(2) failed(${fd}): $!"; + print STDERR "socket is duplicated to file descriptor ${fd}\n"; + close $sock; + push @sockenv, "$hostport=$fd"; + } else { + push @sockenv, "$hostport=" . $sock->fileno; + } + push @sock, $sock; + } + my $path_remove_guard = Server::Starter::Guard->new( + sub { + -S $_ and unlink $_ + for @$paths; + }, + ); + for my $path (@$paths) { + if (-S $path) { + warn "removing existing socket file:$path"; + unlink $path + or die "failed to remove existing socket file:$path:$!"; + } + unlink $path; + my $saved_umask = umask(0); + my $sock = IO::Socket::UNIX->new( + Listen => $opts->{backlog}, + Local => $path, + ) or die "failed to listen to file $path:$!"; + umask($saved_umask); + fcntl($sock, F_SETFD, my $flags = '') + or die "fcntl(F_SETFD, 0) failed:$!"; + push @sockenv, "$path=" . $sock->fileno; + push @sock, $sock; + } + $ENV{SERVER_STARTER_PORT} = join ";", @sockenv; + $ENV{SERVER_STARTER_GENERATION} = 0; + + # setup signal handlers + _set_sighandler($_, sub { + push @signals_received, $_[0]; + }) for (qw/INT TERM HUP ALRM/); + $SIG{PIPE} = 'IGNORE'; + + # setup status monitor + my ($current_worker, %old_workers, $last_restart_time); + my $update_status = $opts->{status_file} + ? sub { + my $tmpfn = "$opts->{status_file}.$$"; + open my $tmpfh, '>', $tmpfn + or die "failed to create temporary file:$tmpfn:$!"; + $status_file_created = 1; + my %gen_pid = ( + ($current_worker + ? ($ENV{SERVER_STARTER_GENERATION} => $current_worker) + : ()), + map { $old_workers{$_} => $_ } keys %old_workers, + ); + print $tmpfh "$_:$gen_pid{$_}\n" + for sort keys %gen_pid; + close $tmpfh; + rename $tmpfn, $opts->{status_file} + or die "failed to rename $tmpfn to $opts->{status_file}:$!"; + } : sub { + }; + + # now that setup is complete, redirect outputs to the log file (if specified) + if ($logfh) { + STDOUT->flush; + STDERR->flush; + open STDOUT, '>&=', $logfh + or die "failed to dup STDOUT to file: $!"; + open STDERR, '>&=', $logfh + or die "failed to dup STDERR to file: $!"; + close $logfh; + undef $logfh; + } + + # daemonize + if ($opts->{daemonize}) { + my $pid = fork; + die "fork failed:$!" + unless defined $pid; + if ($pid != 0) { + $path_remove_guard->dismiss; + exit 0; + } + # in child process + POSIX::setsid(); + $pid = fork; + die "fork failed:$!" + unless defined $pid; + if ($pid != 0) { + $path_remove_guard->dismiss; + exit 0; + } + # do not close STDIN if `--port=n=0`. + unless (grep /=0$/, @sockenv) { + close STDIN; + open STDIN, '<', '/dev/null' + or die "reopen failed: $!"; + } + } + + # open pid file + my $pid_file_guard = sub { + return unless $opts->{pid_file}; + open my $fh, '>', $opts->{pid_file} + or die "failed to open file:$opts->{pid_file}: $!"; + flock($fh, LOCK_EX) + or die "flock failed($opts->{pid_file}): $!"; + print $fh "$$\n"; + $fh->flush(); + return Server::Starter::Guard->new( + sub { + unlink $opts->{pid_file} + or warn "failed to unlink file:$opts->{pid_file}:$!"; + close $fh; + }, + ); + }->(); + + # setup the start_worker function + my $start_worker = sub { + my $pid; + while (1) { + $ENV{SERVER_STARTER_GENERATION}++; + $pid = fork; + die "fork(2) failed:$!" + unless defined $pid; + if ($pid == 0) { + my @args = @{$opts->{exec}}; + # child process + if (defined $opts->{dir}) { + chdir $opts->{dir} or die "failed to chdir:$opts->{dir}:$!"; + } + { exec { $args[0] } @args }; + print STDERR "failed to exec $args[0]$!"; + exit(255); + } + print STDERR "starting new worker $pid\n"; + sleep $opts->{interval}; + if ((grep { $_ ne 'HUP' } @signals_received) + || waitpid($pid, WNOHANG) <= 0) { + last; + } + print STDERR "new worker $pid seems to have failed to start, exit status:$?\n"; + } + # ready, update the environment + $current_worker = $pid; + $last_restart_time = time; + $update_status->(); + }; + + # setup the wait function + my $wait = sub { + my $block = @signals_received == 0; + my @r; + if ($block && $ENV{ENABLE_AUTO_RESTART}) { + alarm(1); + @r = _wait3($block); + alarm(0); + } else { + @r = _wait3($block); + } + return @r; + }; + + # setup the cleanup function + my $cleanup = sub { + my $sig = shift; + my $term_signal = $sig eq 'TERM' ? $opts->{signal_on_term} : 'TERM'; + $old_workers{$current_worker} = $ENV{SERVER_STARTER_GENERATION}; + undef $current_worker; + print STDERR "received $sig, sending $term_signal to all workers:", + join(',', sort keys %old_workers), "\n"; + kill $term_signal, $_ + for sort keys %old_workers; + while (%old_workers) { + if (my @r = _wait3(1)) { + my ($died_worker, $status) = @r; + print STDERR "worker $died_worker died, status:$status\n"; + delete $old_workers{$died_worker}; + $update_status->(); + } + } + print STDERR "exiting\n"; + }; + + # the main loop + $start_worker->(); + while (1) { + # wait for next signal (or when auto-restart becomes necessary) + my @r = $wait->(); + # reload env if necessary + my %loaded_env = _reload_env(); + my @loaded_env_keys = keys %loaded_env; + local @ENV{@loaded_env_keys} = map { $loaded_env{$_} } (@loaded_env_keys); + $ENV{AUTO_RESTART_INTERVAL} ||= 360 + if $ENV{ENABLE_AUTO_RESTART}; + # restart if worker died + if (@r) { + my ($died_worker, $status) = @r; + if ($died_worker == $current_worker) { + print STDERR "worker $died_worker died unexpectedly with status:$status, restarting\n"; + $start_worker->(); + } else { + print STDERR "old worker $died_worker died, status:$status\n"; + delete $old_workers{$died_worker}; + $update_status->(); + } + } + # handle signals + my $restart; + while (@signals_received) { + my $sig = shift @signals_received; + if ($sig eq 'HUP') { + print STDERR "received HUP, spawning a new worker\n"; + $restart = 1; + last; + } elsif ($sig eq 'ALRM') { + # skip + } else { + return $cleanup->($sig); + } + } + if (! $restart && $ENV{ENABLE_AUTO_RESTART}) { + my $auto_restart_interval = $ENV{AUTO_RESTART_INTERVAL}; + my $elapsed_since_restart = time - $last_restart_time; + if ($elapsed_since_restart >= $auto_restart_interval && ! %old_workers) { + print STDERR "autorestart triggered (interval=$auto_restart_interval)\n"; + $restart = 1; + } elsif ($elapsed_since_restart >= $auto_restart_interval * 2) { + print STDERR "autorestart triggered (forced, interval=$auto_restart_interval)\n"; + $restart = 1; + } + } + # restart if requested + if ($restart) { + $old_workers{$current_worker} = $ENV{SERVER_STARTER_GENERATION}; + $start_worker->(); + print STDERR "new worker is now running, sending $opts->{signal_on_hup} to old workers:"; + if (%old_workers) { + print STDERR join(',', sort keys %old_workers), "\n"; + } else { + print STDERR "none\n"; + } + my $kill_old_delay = defined $ENV{KILL_OLD_DELAY} ? $ENV{KILL_OLD_DELAY} : $ENV{ENABLE_AUTO_RESTART} ? 5 : 0; + if ($kill_old_delay != 0) { + print STDERR "sleeping $kill_old_delay secs before killing old workers\n"; + sleep $kill_old_delay; + } + print STDERR "killing old workers\n"; + kill $opts->{signal_on_hup}, $_ + for sort keys %old_workers; + } + } + + die "unreachable"; +} + +sub restart_server { + my $opts = { + (@_ == 1 ? @$_[0] : @_), + }; + die "--restart option requires --pid-file and --status-file to be set as well\n" + unless $opts->{pid_file} && $opts->{status_file}; + + # get pid + my $pid = do { + open my $fh, '<', $opts->{pid_file} + or die "failed to open file:$opts->{pid_file}:$!"; + my $line = <$fh>; + chomp $line; + $line; + }; + + # function that returns a list of active generations in sorted order + my $get_generations = sub { + open my $fh, '<', $opts->{status_file} + or die "failed to open file:$opts->{status_file}:$!"; + my %gen; + while (my $line = <$fh>) { + if ($line =~ /^(\d+):/) { + $gen{$1} = 1; + } + } + sort { $a <=> $b } keys %gen; + }; + + # wait for this generation + my $wait_for = do { + my @gens = $get_generations->() + or die "no active process found in the status file"; + pop(@gens) + 1; + }; + + # send HUP + kill 'HUP', $pid + or die "failed to send SIGHUP to the server process:$!"; + + # wait for the generation + while (1) { + my @gens = $get_generations->(); + last if scalar(@gens) == 1 && $gens[0] == $wait_for; + sleep 1; + } +} + +sub stop_server { + my $opts = { + (@_ == 1 ? @$_[0] : @_), + }; + die "--stop option requires --pid-file to be set as well\n" + unless $opts->{pid_file}; + + # get pid + open my $fh, '<', $opts->{pid_file} + or die "failed to open file:$opts->{pid_file}:$!"; + my $pid = do { + my $line = <$fh>; + chomp $line; + $line; + }; + + print STDERR "stop_server (pid:$$) stopping now (pid:$pid)...\n"; + + # send TERM + kill 'TERM', $pid + or die "failed to send SIGTERM to the server process:$!"; + + # wait process + flock($fh, LOCK_EX) + or die "flock failed($opts->{pid_file}): $!"; + close $fh; +} + +sub server_ports { + die "no environment variable SERVER_STARTER_PORT. Did you start the process using server_starter?", + unless $ENV{SERVER_STARTER_PORT}; + my %ports = map { + +(split /=/, $_, 2) + } split /;/, $ENV{SERVER_STARTER_PORT}; + \%ports; +} + +sub _reload_env { + my $dn = $ENV{ENVDIR}; + return if !defined $dn or !-d $dn; + my $d; + opendir($d, $dn) or return; + my %env; + while (my $n = readdir($d)) { + next if $n =~ /^\./; + open my $fh, '<', "$dn/$n" or next; + chomp(my $v = <$fh>); + $env{$n} = $v if defined $v; + } + return %env; +} + +our $sighandler_should_die; +my $sighandler_got_sig; + +sub _set_sighandler { + my ($sig, $proc) = @_; + $SIG{$sig} = sub { + $proc->(@_); + $sighandler_got_sig = 1; + die "got signal" + if $sighandler_should_die; + }; +} + +sub _wait3 { + my $block = shift; + my $pid = -1; + if ($block) { + local $@; + eval { + $sighandler_got_sig = 0; + local $sighandler_should_die = 1; + die "exit from eval" + if $sighandler_got_sig; + $pid = wait(); + }; + if ($pid == -1 && $@) { + $! = Errno::EINTR; + } + } else { + $pid = waitpid(-1, WNOHANG); + } + return $pid > 0 ? ($pid, $?) : (); +} + +1; +__END__ + +=head1 NAME + +Server::Starter - a superdaemon for hot-deploying server programs + +=head1 SYNOPSIS + + # from command line + % start_server --port=80 my_httpd + + # in my_httpd + use Server::Starter qw(server_ports); + + my $listen_sock = IO::Socket::INET->new( + Proto => 'tcp', + ); + $listen_sock->fdopen((values %{server_ports()})[0], 'w') + or die "failed to bind to listening socket:$!"; + + while (1) { + if (my $conn = $listen_sock->accept) { + .... + } + } + +=head1 DESCRIPTION + +It is often a pain to write a server program that supports graceful restarts, with no resource leaks. L solves the problem by splitting the task into two. One is L, a script provided as a part of the module, which works as a superdaemon that binds to zero or more TCP ports or unix sockets, and repeatedly spawns the server program that actually handles the necessary tasks (for example, responding to incoming commenctions). The spawned server programs under L call accept(2) and handle the requests. + +To gracefully restart the server program, send SIGHUP to the superdaemon. The superdaemon spawns a new server program, and if (and only if) it starts up successfully, sends SIGTERM to the old server program. + +By using L it is much easier to write a hot-deployable server. Following are the only requirements a server program to be run under L should conform to: + +- receive file descriptors to listen to through an environment variable +- perform a graceful shutdown when receiving SIGTERM + +A Net::Server personality that can be run under L exists under the name L. + +=head1 METHODS + +=over 4 + +=item server_ports + +Returns zero or more file descriptors on which the server program should call accept(2) in a hashref. Each element of the hashref is: (host:port|port|path_of_unix_socket) => file_descriptor. + +=item start_server + +Starts the superdaemon. Used by the C script. + +=back + +=head1 AUTHOR + +Kazuho Oku + +=head1 SEE ALSO + +L + +=head1 LICENSE + +This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. + +=cut diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/lib/Server/Starter/Guard.pm b/web/server/h2o/libh2o/misc/p5-Server-Starter/lib/Server/Starter/Guard.pm new file mode 100644 index 000000000..f25b49c17 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/lib/Server/Starter/Guard.pm @@ -0,0 +1,21 @@ +package Server::Starter::Guard; + +use strict; +use warnings; + +sub new { + my ($klass, $handler) = @_; + return bless { + handler => $handler, + active => 1, + }, $klass; +} + +sub dismiss { shift->{active} = 0 } + +sub DESTROY { + my $self = shift; + $self->{active} && $self->{handler}->(); +} + +1; diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/minil.toml b/web/server/h2o/libh2o/misc/p5-Server-Starter/minil.toml new file mode 100644 index 000000000..4dac44941 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/minil.toml @@ -0,0 +1,2 @@ +[build] +build_class = "builder::MyBuilder" diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/script/start_server b/web/server/h2o/libh2o/misc/p5-Server-Starter/script/start_server new file mode 100755 index 000000000..20dd9fab7 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/script/start_server @@ -0,0 +1,179 @@ +#!perl + +use strict; +use warnings; + +use Getopt::Long; +use Pod::Usage; +use Server::Starter qw(start_server restart_server stop_server); + +my %opts = ( + port => [], + path => [], +); + +GetOptions( + map { + $_ => do { + my $name = (split '=', $_, 2)[0]; + $name =~ s/-/_/g; + $opts{$name} ||= undef; + ref($opts{$name}) ? $opts{$name} : \$opts{$name}; + }, + } qw(port=s path=s interval=i log-file=s pid-file=s dir=s signal-on-hup=s signal-on-term=s + backlog=i envdir=s enable-auto-restart=i daemonize auto-restart-interval=i kill-old-delay=i + status-file=s restart stop help version), +) or exit 1; +pod2usage( + -exitval => 0, + -verbose => 1, +) if $opts{help}; +if ($opts{version}) { + print "$Server::Starter::VERSION\n"; + exit 0; +} + +if ($opts{restart}) { + restart_server(%opts); + exit 0; +} + +if ($opts{stop}) { + stop_server(%opts); + exit 0; +} + +if ($opts{daemonize}) { + die "Usage: --log-file option must be specified together with --deamonize\n" + unless defined $opts{log_file}; +} + +# validate options +die "server program not specified\n" + unless @ARGV; + +start_server( + %opts, + exec => \@ARGV, +); + +__END__ + +=head1 NAME + +start_server - a superdaemon for hot-deploying server programs + +=head1 SYNOPSIS + + start_server [options] -- server-prog server-arg1 server-arg2 ... + + # start Plack using Starlet listening at TCP port 8000 + start_server --port=8000 -- plackup -s Starlet --max-workers=100 index.psgi + +=head1 DESCRIPTION + +This script is a frontend of L. For more information please refer to the documentation of the module. + +=head1 OPTIONS + +=head2 --port=(port|host:port|port=fd|host:port=fd) + +TCP port to listen to (if omitted, will not bind to any ports) + +If host is not specified, then the program will bind to the default address of IPv4 ("0.0.0.0"). +Square brackets should be used to specify an IPv6 address (e.g. --port=[::1]:8080) + +If fd is specified, then start_server allocates the socket at the given number. + +=head2 --path=path + +path at where to listen using unix socket (optional) + +=head2 --dir=path + +working directory, start_server do chdir to before exec (optional) + +=head2 --interval=seconds + +minimum interval to respawn the server program (default: 1) + +=head2 --signal-on-hup=SIGNAL + +name of the signal to be sent to the server process when start_server receives a SIGHUP (default: SIGTERM). If you use this option, be sure to also use C<--signal-on-term> below. + +=head2 --signal-on-term=SIGNAL + +name of the signal to be sent to the server process when start_server receives a SIGTERM (default: SIGTERM) + +=head2 --pid-file=filename + +if set, writes the process id of the start_server process to the file + +=head2 --status-file=filename + +if set, writes the status of the server process(es) to the file + +=head2 --envdir=ENVDIR + +directory that contains environment variables to the server processes. +It is intended for use with C in C. +This can be overwritten by environment variable C. + +=head2 --log-file=file + +=head2 --log-file="| cmd args..." + +if set, redirects STDOUT and STDERR to given file or command + +=head2 --daemonize + +deamonizes the server (by doing fork,setsid,fork). Must be used together with C<--log-file>. + +=head2 --enable-auto-restart + +enables automatic restart by time. +This can be overwritten by environment variable C. + +=head2 --auto-restart-interval=seconds + +automatic restart interval (default 360). It is used with C<--enable-auto-restart> option. +This can be overwritten by environment variable C. + +=head2 --kill-old-delay=seconds + +time to suspend to send a signal to the old worker. The default value is 5 when C<--enable-auto-restart> is set, 0 otherwise. +This can be overwritten by environment variable C. + +=head2 --backlog=size + +specifies a listen backlog parameter, whose default is SOMAXCONN (usually 128 on Linux). While SOMAXCONN is enough for most loads, large backlog is required for heavy loads. + +=head2 --restart + +this is a wrapper command that reads the pid of the start_server process from --pid-file, sends SIGHUP to the process and waits until the server(s) of the older generation(s) die by monitoring the contents of the --status-file + +=head2 --stop + +this is a wrapper command that reads the pid of the start_server process from --pid-file, sends SIGTERM to the process. + +=head2 --help + +prints this help + +=head2 --version + +prints the version number + +=head1 AUTHOR + +Kazuho Oku + +=head1 SEE ALSO + +L + +=head1 LICENSE + +This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. + +=cut diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/00-base.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/00-base.t new file mode 100644 index 000000000..4e4dbb0a9 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/00-base.t @@ -0,0 +1,8 @@ +use strict; +use warnings; + +use Test::More tests => 1; + +BEGIN { + use_ok('Server::Starter'); +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/01-starter-echod.pl b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/01-starter-echod.pl new file mode 100755 index 000000000..f35d200d9 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/01-starter-echod.pl @@ -0,0 +1,35 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use lib qw(blib/lib lib); + +use IO::Socket::INET; +use Server::Starter qw(server_ports); + +my $sigfn = shift @ARGV; +open my $sigfh, '>', $sigfn + or die "could not open file:$sigfn:$!"; + +$SIG{TERM} = $SIG{USR1} = sub { + my $signame = shift; + print $sigfh $signame; + sleep 2; + exit 0; +}; + +my $listener = IO::Socket::INET->new( + Proto => 'tcp', +); +$listener->fdopen((values %{server_ports()})[0], 'w') + or die "failed to bind listening socket:$!"; + +while (1) { + if (my $conn = $listener->accept) { + my $buf; + while ($conn->sysread($buf, 1048576) > 0) { + $conn->syswrite("$$:$buf"); + } + } +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/01-starter.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/01-starter.t new file mode 100644 index 000000000..f4b0d4162 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/01-starter.t @@ -0,0 +1,82 @@ +use strict; +use warnings; + +use File::Temp (); +use Test::TCP; +use Test::More tests => 28; + +use Server::Starter qw(start_server); + +$SIG{PIPE} = sub {}; + +my $tempdir = File::Temp::tempdir(CLEANUP => 1); + +for my $signal_on_hup ('TERM', 'USR1') { + + test_tcp( + server => sub { + my $port = shift; + start_server( + port => $port, + exec => [ + $^X, qw(t/01-starter-echod.pl), "$tempdir/signame", + ], + status_file => "$tempdir/status", + ($signal_on_hup ne 'TERM' + ? (signal_on_hup => $signal_on_hup) : ()), + ); + }, + client => sub { + my ($port, $server_pid) = @_; + my $buf; + #sleep 1; + my $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ); + ok($sock, 'connect'); + # check response and get pid + is($sock->syswrite("hello"), 5, 'write'); + ok($sock->sysread($buf, 1048576), 'read'); + undef $sock; + like($buf, qr/^\d+:hello$/, 'read'); + $buf =~ /^(\d+):/; + my $worker_pid = $1; + # switch to next gen + sleep 3; + my $status = get_status(); + like(get_status(), qr/^1:\d+\n$/s, 'status before restart'); + kill 'HUP', $server_pid; + sleep 2; + like(get_status(), qr/^1:\d+\n2:\d+$/s, 'status during restart'); + sleep 2; + like(get_status(), qr/^2:\d+\n$/s, 'status after restart'); + is( + do { + open my $fh, '<', "$tempdir/signame" + or die $!; + <$fh>; + }, + $signal_on_hup, + 'signal sent on hup', + ); + $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ); + ok($sock, 'reconnect'); + is($sock->syswrite("hello"), 5, 'write after switching'); + ok($sock->sysread($buf, 1048576), 'read after switching'); + like($buf, qr/^\d+:hello$/, 'read after swiching (format)'); + isnt($buf, "$worker_pid:hello", 'pid should have changed'); + }, + ); + + ok ! -e "$tempdir/status", 'no more status file'; +} + +sub get_status { + open my $fh, '<', "$tempdir/status" + or die "failed to open file:$tempdir/status:$!"; + do { undef $/; <$fh> }; +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/02-startfail-server.pl b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/02-startfail-server.pl new file mode 100755 index 000000000..6913bc8eb --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/02-startfail-server.pl @@ -0,0 +1,32 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use lib qw(blib/lib lib); + +use IO::Socket::INET; +use Server::Starter qw(server_ports); + +$SIG{TERM} = sub { + exit 0; +}; + +my $gen = $ENV{SERVER_STARTER_GENERATION}; + +if ($gen == 1 || 3 <= $gen && $gen < 5) { + # emulate startup failure + exit 1; +} + +my $listener = IO::Socket::INET->new( + Proto => 'tcp', +); +$listener->fdopen((values %{server_ports()})[0], 'w') + or die "failed to bind listening socket:$!"; + +while (1) { + if (my $conn = $listener->accept) { + $conn->syswrite($gen); + } +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/02-startfail.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/02-startfail.t new file mode 100644 index 000000000..0985534b2 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/02-startfail.t @@ -0,0 +1,61 @@ +use strict; +use warnings; + +use Test::TCP; +use Test::More tests => 9; + +use Server::Starter qw(start_server); + +test_tcp( + server => sub { + my $port = shift; + start_server( + port => $port, + exec => [ $^X, qw(t/02-startfail-server.pl) ], + ); + }, + client => sub { + my ($port, $server_pid) = @_; + my $buf; + sleep 3; + { + my $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ); + ok($sock, 'connect'); + # check generation + ok($sock->sysread($buf, 1048576), 'read'); + is($buf, 2, 'check generation'); + } + # request restart, that will fail + kill 'HUP', $server_pid; + sleep 1; + { + my $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ); + ok($sock, 'connect'); + ok( + $sock->sysread($buf, 1048576), + 'read while worker is failing to reboot', + ); + is($buf, 2, 'check generation'); + } + # wait until server succeds in reboot + sleep 5; + { + my $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ); + ok($sock, 'connect'); + ok( + $sock->sysread($buf, 1048576), + 'read after worker succeeds to reboot', + ); + is($buf, 5, 'check generation'); + } + }, +); diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/03-starter-unix-echod.pl b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/03-starter-unix-echod.pl new file mode 100644 index 000000000..a54677475 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/03-starter-unix-echod.pl @@ -0,0 +1,22 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use lib qw(blib/lib lib); + +use IO::Socket::UNIX; +use Server::Starter qw(server_ports); + +my $listener = IO::Socket::UNIX->new() + or die "failed to create unix socket:$!"; +$listener->fdopen((values %{server_ports()})[0], 'w') + or die "failde to bind to listening socket:$!"; + +while (1) { + if (my $conn = $listener->accept) { + while ($conn->sysread(my $buf, 1048576) > 0) { + $conn->syswrite($buf); + } + } +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/03-starter-unix.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/03-starter-unix.t new file mode 100644 index 000000000..965de6b68 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/03-starter-unix.t @@ -0,0 +1,40 @@ +use strict; +use warnings; + +use File::Temp (); +use IO::Socket::UNIX; +use Test::More tests => 4; +use Test::SharedFork; + +use Server::Starter qw(start_server); + +$SIG{PIPE} = sub {}; + +my $sockfile = File::Temp::tmpnam(); + +my $pid = fork; +die "fork failed: $!" + unless defined $pid; +if ($pid == 0) { + # child + start_server( + path => $sockfile, + exec => [ $^X, qw(t/03-starter-unix-echod.pl) ], + ); + exit 0; +} else { + # parent + sleep 1 + until -e $sockfile; + my $sock = IO::Socket::UNIX->new( + Peer => $sockfile, + ) or die "failed to connect to unix socket:$!"; + is $sock->syswrite('hello', 5), 5, 'write'; + is $sock->sysread(my $buf, 5), 5, 'read length'; + is $buf, 'hello', 'read data'; + kill 'TERM', $pid; + while (wait != $pid) {} + ok ! -e $sockfile, 'socket file removed after shutdown'; +} + +unlink $sockfile; diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/04-starter-dir.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/04-starter-dir.t new file mode 100644 index 000000000..5f1b3968a --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/04-starter-dir.t @@ -0,0 +1,54 @@ +use strict; +use warnings; + +use File::Temp (); +use Test::More tests => 1; +use Net::EmptyPort qw/empty_port/; +use IO::Select; +use Server::Starter qw(start_server); + +$SIG{PIPE} = sub {}; + +pipe my $logrh, my $logwh + or die "Died: failed to create pipe:$!"; +my $port = empty_port + or die "could not get any port"; +my $tempdir = File::Temp::tempdir(CLEANUP => 0); +open(my $fh, '>', "$tempdir/dir_status") or die "$!"; +close($fh); + +my $pid = fork; + +if ( ! defined $pid ) { + die "Died: fork failed: $!"; +} +elsif ( $pid == 0 ) { + close $logrh; + open STDOUT, '>&', $logwh + or die "Died: failed to redirect STDOUT"; + close $logwh; + start_server( + port => $port, #not use + exec => [ + $^X, '-e', 'printf "%s\n", -f "dir_status" ? "OK" : "NG"; sleep(1)' + ], + dir => $tempdir + ); + exit(255); +} + +close $logwh; +my $result; +my $s = IO::Select->new($logrh); +my @ready = $s->can_read(10); +die "could not read logs from pipe" unless @ready; +sysread($logrh, my $buf, 65536); +like($buf, qr/OK\W/); + +kill 'TERM', $pid; +while (wait != $pid) {} + +unlink "$tempdir/status"; +rmdir $tempdir; + + diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/05-killolddelay-echod.pl b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/05-killolddelay-echod.pl new file mode 100755 index 000000000..5b48ca559 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/05-killolddelay-echod.pl @@ -0,0 +1,35 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use lib qw(blib/lib lib); + +use IO::Socket::INET; +use Server::Starter qw(server_ports); + +my $sigfn = shift @ARGV; +open my $sigfh, '>', $sigfn + or die "could not open file:$sigfn:$!"; + +$SIG{TERM} = $SIG{USR1} = sub { + my $signame = shift; + print $sigfh $signame; + sleep 1; + exit 0; +}; + +my $listener = IO::Socket::INET->new( + Proto => 'tcp', +); +$listener->fdopen((values %{server_ports()})[0], 'w') + or die "failed to bind listening socket:$!"; + +while (1) { + if (my $conn = $listener->accept) { + my $buf; + while ($conn->sysread($buf, 1048576) > 0) { + $conn->syswrite("$$:$buf"); + } + } +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/05-killolddelay.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/05-killolddelay.t new file mode 100644 index 000000000..19856af52 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/05-killolddelay.t @@ -0,0 +1,87 @@ +use strict; +use warnings; + +use File::Temp (); +use Test::TCP; +use Test::More tests => 28; + +use Server::Starter qw(start_server); + +$SIG{PIPE} = sub {}; + +my $tempdir = File::Temp::tempdir(CLEANUP => 1); + +for my $signal_on_hup ('TERM', 'USR1') { + + test_tcp( + server => sub { + my $port = shift; + start_server( + port => $port, + exec => [ + $^X, qw(t/05-killolddelay-echod.pl), "$tempdir/signame", + ], + status_file => "$tempdir/status", + enable_auto_restart => 0, + kill_old_delay => 3, + ($signal_on_hup ne 'TERM' + ? (signal_on_hup => $signal_on_hup) : ()), + ); + }, + client => sub { + my ($port, $server_pid) = @_; + my $buf; + #sleep 1; + my $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ); + ok($sock, 'connect'); + # check response and get pid + is($sock->syswrite("hello"), 5, 'write'); + ok($sock->sysread($buf, 1048576), 'read'); + undef $sock; + like($buf, qr/^\d+:hello$/, 'read'); + $buf =~ /^(\d+):/; + my $worker_pid = $1; + # switch to next gen + sleep 2; + my $status = get_status(); + like(get_status(), qr/^1:\d+\n$/s, 'status before restart'); + kill 'HUP', $server_pid; + sleep 4; + like(get_status(), qr/^1:\d+\n2:\d+$/s, 'status during restart'); + # Child process has finished in 2 seconds but the parent + # checks and calls waitpid every second, so wait for an + # additional 1 second. + sleep 3; + like(get_status(), qr/^2:\d+\n$/s, 'status after restart'); + is( + do { + open my $fh, '<', "$tempdir/signame" + or die $!; + <$fh>; + }, + $signal_on_hup, + 'signal sent on hup', + ); + $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ); + ok($sock, 'reconnect'); + is($sock->syswrite("hello"), 5, 'write after switching'); + ok($sock->sysread($buf, 1048576), 'read after switching'); + like($buf, qr/^\d+:hello$/, 'read after swiching (format)'); + isnt($buf, "$worker_pid:hello", 'pid should have changed'); + }, + ); + + ok ! -e "$tempdir/status", 'no more status file'; +} + +sub get_status { + open my $fh, '<', "$tempdir/status" + or die "failed to open file:$tempdir/status:$!"; + do { undef $/; <$fh> }; +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/06-autorestart-echod.pl b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/06-autorestart-echod.pl new file mode 100755 index 000000000..2d71a654f --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/06-autorestart-echod.pl @@ -0,0 +1,34 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use lib qw(blib/lib lib); + +use IO::Socket::INET; +use Server::Starter qw(server_ports); + +my $sigfn = shift @ARGV; +open my $sigfh, '>', $sigfn + or die "could not open file:$sigfn:$!"; + +$SIG{TERM} = $SIG{USR1} = sub { + my $signame = shift; + print $sigfh $signame; + exit 0; +}; + +my $listener = IO::Socket::INET->new( + Proto => 'tcp', +); +$listener->fdopen((values %{server_ports()})[0], 'w') + or die "failed to bind listening socket:$!"; + +while (1) { + if (my $conn = $listener->accept) { + my $buf; + while ($conn->sysread($buf, 1048576) > 0) { + $conn->syswrite("$$:$buf"); + } + } +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/06-autorestart.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/06-autorestart.t new file mode 100644 index 000000000..1e1f86582 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/06-autorestart.t @@ -0,0 +1,84 @@ +use strict; +use warnings; + +use File::Temp (); +use Test::TCP; +use Test::More tests => 28; + +use Server::Starter qw(start_server); + +$SIG{PIPE} = sub {}; + +my $tempdir = File::Temp::tempdir(CLEANUP => 1); + +for my $signal_on_hup ('TERM', 'USR1') { + + test_tcp( + server => sub { + my $port = shift; + start_server( + port => $port, + exec => [ + $^X, qw(t/05-killolddelay-echod.pl), "$tempdir/signame", + ], + status_file => "$tempdir/status", + enable_auto_restart => 1, + auto_restart_interval => 6, + kill_old_delay => 2, + ($signal_on_hup ne 'TERM' + ? (signal_on_hup => $signal_on_hup) : ()), + ); + }, + client => sub { + my ($port, $server_pid) = @_; + my $buf; + #sleep 1; + my $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ); + ok($sock, 'connect'); + # check response and get pid + is($sock->syswrite("hello"), 5, 'write'); + ok($sock->sysread($buf, 1048576), 'read'); + undef $sock; + like($buf, qr/^\d+:hello$/, 'read'); + $buf =~ /^(\d+):/; + my $worker_pid = $1; + # switch to next gen + sleep 2; + my $status = get_status(); + like(get_status(), qr/^1:\d+\n$/s, 'status before auto-restart'); + sleep 7; # new worker spawn at 7 (since start, interval(1) + auto_restart_interval(6)), status updated at 8 (7 + interval(1)), old dies at 11 (8 + kill_old_delay(2) + sleep(1) in the child source code) + like(get_status(), qr/^1:\d+\n2:\d+$/s, 'status during transient state'); + sleep 3; + like(get_status(), qr/^2:\d+\n$/s, 'status after auto-restart'); + is( + do { + open my $fh, '<', "$tempdir/signame" + or die $!; + <$fh>; + }, + $signal_on_hup, + 'signal sent on hup', + ); + $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ); + ok($sock, 'reconnect'); + is($sock->syswrite("hello"), 5, 'write after switching'); + ok($sock->sysread($buf, 1048576), 'read after switching'); + like($buf, qr/^\d+:hello$/, 'read after swiching (format)'); + isnt($buf, "$worker_pid:hello", 'pid should have changed'); + }, + ); + + ok ! -e "$tempdir/status", 'no more status file'; +} + +sub get_status { + open my $fh, '<', "$tempdir/status" + or die "failed to open file:$tempdir/status:$!"; + do { undef $/; <$fh> }; +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/07-envdir-print.pl b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/07-envdir-print.pl new file mode 100755 index 000000000..22cb01af0 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/07-envdir-print.pl @@ -0,0 +1,29 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use lib qw(blib/lib lib); + +use IO::Socket::INET; +use Server::Starter qw(server_ports); + +$SIG{TERM} = $SIG{USR1} = sub { + exit 0; +}; + +my $listener = IO::Socket::INET->new( + Proto => 'tcp', +); +$listener->fdopen((values %{server_ports()})[0], 'w') + or die "failed to bind listening socket:$!"; + +while (1) { + if (my $conn = $listener->accept) { + my $s = ""; + for my $envkey (keys %ENV) { + $s .= $envkey . "=" . $ENV{$envkey} . "\n"; + } + $conn->syswrite($s); + } +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/07-envdir.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/07-envdir.t new file mode 100644 index 000000000..f10fbade4 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/07-envdir.t @@ -0,0 +1,77 @@ +use strict; +use warnings; + +use File::Temp (); +use Test::TCP; +use Test::More tests => 4; + +use Server::Starter qw(start_server); + +$SIG{PIPE} = sub {}; + +my $tempdir = File::Temp::tempdir(CLEANUP => 1); + +# Prepare envdir +mkdir "$tempdir/env" or die $!; +open my $envfh, ">", "$tempdir/env/FOO" or die $!; +print $envfh "foo-value1"; +close $envfh; +undef $envfh; +sleep 1; + +test_tcp( + server => sub { + my $port = shift; + start_server( + port => $port, + exec => [ + $^X, qw(t/07-envdir-print.pl), + ], + status_file => "$tempdir/status", + envdir => "$tempdir/env", + ); + }, + client => sub { + my ($port, $server_pid) = @_; + #sleep 1; + my $fetch_env = sub { + my $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ) or die $!; + my $buf; + $sock->sysread($buf, 1048576) + or die $!; + undef $sock; + $buf; + }; + my $restart = sub { + sleep 1; + kill "HUP", $server_pid; + sleep 2; + }; + # Initial worker does not read envdir + my $buf = $fetch_env->(); + ok($buf !~ qr/^FOO=foo-value1$/m, 'changed env'); + # rewrite envdir + open my $envfh, ">", "$tempdir/env/FOO" or die $!; + print $envfh "foo-value2"; + close $envfh; + undef $envfh; + # switch to next gen + $restart->(); + # new worker reads the rewritten envdir + $buf = $fetch_env->(); + ok($buf =~ /^FOO=foo-value2$/m, 'changed env'); + # remove the env file and check that the removal gets reflected + unlink "$tempdir/env/FOO" + or die $!; + # switch to next gen + $restart->(); + # new worker reads the rewritten envdir + $buf = $fetch_env->(); + ok($buf !~ /^FOO=foo-value2$/m, 'removed env'); + }, +); + +ok ! -e "$tempdir/status", 'no more status file'; diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/08-wait3.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/08-wait3.t new file mode 100644 index 000000000..e13b6e8b3 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/08-wait3.t @@ -0,0 +1,39 @@ +use strict; +use warnings; +use Test::More; +use Server::Starter; + +my $gotsig = 0; +Server::Starter::_set_sighandler('USR1', sub { + warn "got SIGUSR1"; + ++$gotsig; +}); + +my $pid = fork; +die "fork failed:$!" + unless defined $pid; +if ($pid == 0) { + # child process, send signal twice + sleep 1; + kill 'USR1', getppid; + sleep 1; + kill 'USR1', getppid; + sleep 10; + die "child process not killed"; +} + +my @r = Server::Starter::_wait3(0); +ok ! @r, "nonblocking wait returns without pid"; + +for (my $i = 1; $i <= 2; ++$i) { + @r = Server::Starter::_wait3(1); + is $gotsig, $i, "woke up after signal (count: $i)"; + ok ! @r, "child is alive"; +} + +kill 'KILL', $pid; +@r = Server::Starter::_wait3(1); +is $gotsig, 2, "did not receive signal"; +is $r[0], $pid, "child died"; + +done_testing; diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/09-guard.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/09-guard.t new file mode 100644 index 000000000..a51903a4e --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/09-guard.t @@ -0,0 +1,31 @@ +use strict; +use warnings; +use Test::More; + +use_ok("Server::Starter::Guard"); + +subtest "guard is called when it goes out of scope" => sub { + my $cnt = 0; + + my $guard = Server::Starter::Guard->new(sub { + $cnt++; + }); + + is $cnt, 0; + undef $guard; + is $cnt, 1; +}; + +subtest "guard can be dismissed" => sub { + my $cnt = 0; + + my $guard = Server::Starter::Guard->new(sub { + $cnt++; + }); + + $guard->dismiss; + undef $guard; + is $cnt, 0; +}; + +done_testing; diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/10-bindaddr-server.pl b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/10-bindaddr-server.pl new file mode 100755 index 000000000..279c59673 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/10-bindaddr-server.pl @@ -0,0 +1,26 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use lib qw(blib/lib lib); + +use IO::Socket::INET; +use Server::Starter qw(server_ports); + +$SIG{TERM} = $SIG{USR1} = sub { + exit 0; +}; +$SIG{HUP} = sub {}; + +my $listener = IO::Socket::INET->new( + Proto => 'tcp', +); +$listener->fdopen((values %{server_ports()})[0], 'w') + or die "failed to bind listening socket:$!"; + +while (1) { + if (my $conn = $listener->accept) { + close $conn; + } +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/10-bindaddr.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/10-bindaddr.t new file mode 100644 index 000000000..16a76768c --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/10-bindaddr.t @@ -0,0 +1,55 @@ +use strict; +use warnings; +use Test::More; +use Test::Requires qw(IO::Socket::IP); +use Net::EmptyPort qw(can_bind empty_port); +use Server::Starter qw(start_server); + +plan skip_all => 'IPv6 not available' + unless can_bind('::1'); + +my $port = empty_port; + +sub doit { + my ($bind_addr, $other_addr) = @_; + my $pid = fork; + die "fork failed:$!" + unless defined $pid; + if ($pid == 0) { + # server + start_server( + port => "[$bind_addr]:$port", + exec => [ + $^X, qw(t/10-bindaddr-server.pl), + ], + ); + exit 0; + } + # client + sleep 1; + my $sock = IO::Socket::IP->new( + PeerHost => $bind_addr, + PeerPort => $port, + Proto => 'tcp', + ); + ok($sock, "connected to bindaddr"); + $sock->sysread(my $buf, 1024); # wait for disconnect + undef $sock; + $sock = IO::Socket::IP->new( + PeerHost => $other_addr, + PeerPort => $port, + Proto => 'tcp', + ); + ok ! defined $sock, "cannot connect to other addr"; + kill 'TERM', $pid; + wait(); +} + +subtest "v4" => sub { + doit("127.0.0.1", "::1"); +}; +subtest "v6" => sub { + doit("::1", "127.0.0.1"); +}; + +done_testing; diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/11-specified-fd-server.pl b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/11-specified-fd-server.pl new file mode 100644 index 000000000..f05282ee1 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/11-specified-fd-server.pl @@ -0,0 +1,25 @@ +#! /usr/bin/perl +use strict; +use warnings; + +use lib qw(blib/lib lib); + +use IO::Socket::INET; +use Server::Starter qw(server_ports); + +die "fd must be zero" unless ((values %{server_ports()})[0]) eq 0; + +my $listener = IO::Socket::INET->new( + Proto => 'tcp', +); +$listener->fdopen(0, 'w') + or die "failed to bind listening socket:$!"; + +while (1) { + if (my $conn = $listener->accept) { + my $buf; + while ($conn->sysread($buf, 1048576) > 0) { + $conn->syswrite("$$:$buf"); + } + } +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/11-specified-fd.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/11-specified-fd.t new file mode 100644 index 000000000..7e3b820be --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/11-specified-fd.t @@ -0,0 +1,40 @@ +use strict; +use warnings; + +use File::Temp (); +use Test::TCP; +use Test::More tests => 4; + +use Server::Starter qw(start_server); + +$SIG{PIPE} = sub {}; + +test_tcp( + server => sub { + my $port = shift; + start_server( + port => "$port=0", + exec => [ + $^X, qw(t/11-specified-fd-server.pl) + ], + ); + exit 0; + }, + client => sub { + my ($port, $server_pid) = @_; + my $buf; + #sleep 1; + my $sock = IO::Socket::INET->new( + PeerAddr => "127.0.0.1:$port", + Proto => 'tcp', + ); + ok($sock, 'connect'); + # check response and get pid + is($sock->syswrite("hello"), 5, 'write'); + ok($sock->sysread($buf, 1048576), 'read'); + undef $sock; + like($buf, qr/^\d+:hello$/, 'read'); + kill $server_pid; + }, +); + diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/12-stop-server.pl b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/12-stop-server.pl new file mode 100755 index 000000000..ace7e5db4 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/12-stop-server.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use lib qw(blib/lib lib); + +use IO::Socket::INET; +use Server::Starter qw(server_ports); + +my $listener = IO::Socket::INET->new( + Proto => 'tcp', +); +$listener->fdopen((values %{server_ports()})[0], 'w') + or die "failed to bind listening socket:$!"; + +while (1) { + if (my $conn = $listener->accept) { + my $buf; + while ($conn->sysread($buf, 1048576) > 0) { + $conn->syswrite("$$:$buf"); + } + } +} diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/12-stop.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/12-stop.t new file mode 100644 index 000000000..6d40c25f5 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/12-stop.t @@ -0,0 +1,50 @@ +use strict; +use warnings; +use utf8; +use Test::More; +use Server::Starter qw(start_server restart_server stop_server); +use File::Temp qw(tempdir); +use Test::TCP; + +plan tests => 2; + +my $dir = tempdir( CLEANUP => 1 ); +my $pidfile = "$dir/pid"; + +test_tcp( + server => sub { + my $port = shift; + + start_server( + pid_file => $pidfile, + daemonize => 1, + port => $port, + exec => [ $^X, 't/12-stop-server.pl' ], + ); + exit 0; + }, + client => sub { + my $port = shift; + + while (!-s $pidfile) { + note 'pid file is not available'; + sleep 1; # wait pid file + } + + my $pid = do { + open my $fh, '<', $pidfile + or die "Cannot open $pidfile: $!"; + local $/; + <$fh>; + }; + note "PID=$pid"; + is(kill(0, $pid), 1, 'there is a process'); + + stop_server( + pid_file => $pidfile, + port => $port, + ); + ok((!-e $pidfile), 'pid file was unlinked'); + }, +); + diff --git a/web/server/h2o/libh2o/misc/p5-Server-Starter/t/13-unix-daemonize.t b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/13-unix-daemonize.t new file mode 100644 index 000000000..ab401dc30 --- /dev/null +++ b/web/server/h2o/libh2o/misc/p5-Server-Starter/t/13-unix-daemonize.t @@ -0,0 +1,68 @@ +use strict; +use warnings; +use utf8; +use Test::More; +use Server::Starter qw(start_server stop_server); +use Server::Starter::Guard; +use File::Temp qw(tempdir); + +plan tests => 1; + +my $dir = tempdir( CLEANUP => 1 ); +my $pidfile = "$dir/pid"; +my $sockfile = "$dir/server.sock"; + +fork_ok( + child => sub { + start_server( + pid_file => $pidfile, + daemonize => 1, + path => $sockfile, + exec => [ $^X, qw(t/03-starter-unix-echod.pl) ], + ); + }, + + parent => sub { + my $guard = Server::Starter::Guard->new(sub { + stop_server( pid_file => $pidfile ); + }); + + wait_for(sub { -e $pidfile }) + or BAIL_OUT("Pidfile '$pidfile' was not created in a timely fashion"); + + wait_for(sub { -e $sockfile }) + or BAIL_OUT("Socket '$sockfile' was not created in a timely fashion"); + + ok(-e $sockfile, 'there is a socket'); + }, +) or die "fork failed: $!"; + +sub fork_ok { + my (%args) = @_; + + my $pid = fork; + return unless defined $pid; + if ($pid == 0) { + $args{child}->(); + } + else { + $args{parent}->($pid); + } + + return 1; +} + +sub wait_for { + my ($code, %opts) = @_; + + my $times = $opts{times} || 10; + my $every = $opts{every} || 1; + + while ( $times > 0 ) { + return 1 if $code->(); + $times--; + sleep $every; + } + + return 0; +} -- cgit v1.2.3