├── .gitignore ├── LICENSE ├── NOTICE ├── README.md ├── pom.xml └── src ├── main └── java │ └── org │ └── skife │ └── gressil │ ├── ArgvFinder.java │ ├── Daemon.java │ ├── DaemonCommand.java │ ├── DaemonStatus.java │ ├── JvmBasedArgvFinder.java │ ├── LinuxArgvFinder.java │ ├── MacARGVFinder.java │ ├── Main.java │ ├── MicroC.java │ └── Status.java └── test └── java └── org └── skife └── gressil ├── DaemonTest.java ├── MacARGVFinderTest.java └── examples └── ChattyDaemon.java /.gitignore: -------------------------------------------------------------------------------- 1 | dependency-reduced-pom.xml 2 | gressil.pid 3 | src/test/java/org/skife/jdbi/v2/docs/TestArrayBinding.java 4 | target 5 | report 6 | *.ipr 7 | *.iws 8 | *.iml 9 | .clover 10 | .idea 11 | build 12 | out 13 | .classpath 14 | .project 15 | .settings 16 | *~ 17 | test-output 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Much thanks to the JNR folks, this library uses jnr-posix, jnr-ffi, and their dependencies. The JNR libraries in use, jnr-ffi, jnr-posix, and jnr-x86asm are tri-licensed under CPL/GPL/LGPL. The text of these licenses is at the end of this file. 2 | -- 3 | Much thanks to the ASM folks (INRIA and France Telecom specifically). The jnr libraries use ASM and this library uses JNR. ASm is licensed under the 3-clause BSD license, a copy of which can be found later in this file. 4 | -- 5 | Much thanks to Kohsuke Kawaguchi for the initial pointer on how to get ARGV for a process out of the Darwin kernel via the Akuma project. 6 | -- 7 | Much thanks to the kind folks at Google for Guava and findbugs -- these libraries are used in the tests for this library. They are licensed under the Apache License 2.0. You can find a copy of that license in the LICENSE file in this same directory. 8 | -- 9 | Much thanks to Object Mentor for JUnit, which is used to test this library, JUnit is distributed under the CPL 1.0, a copy of which appears later in this file. 10 | 11 | 12 | 13 | -- 14 | ASM license: 15 | Copyright (c) 2000-2011 INRIA, France Telecom 16 | All rights reserved. 17 | 18 | Redistribution and use in source and binary forms, with or without 19 | modification, are permitted provided that the following conditions 20 | are met: 21 | 22 | 1. Redistributions of source code must retain the above copyright 23 | notice, this list of conditions and the following disclaimer. 24 | 25 | 2. Redistributions in binary form must reproduce the above copyright 26 | notice, this list of conditions and the following disclaimer in the 27 | documentation and/or other materials provided with the distribution. 28 | 29 | 3. Neither the name of the copyright holders nor the names of its 30 | contributors may be used to endorse or promote products derived from 31 | this software without specific prior written permission. 32 | 33 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 34 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 35 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 36 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 37 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 38 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 39 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 40 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 41 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 42 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 43 | THE POSSIBILITY OF SUCH DAMAGE. 44 | -- 45 | === Common Public License - v 1.0 46 | 47 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS COMMON PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 48 | 49 | 1. DEFINITIONS 50 | 51 | "Contribution" means: 52 | 53 | a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and 54 | b) in the case of each subsequent Contributor: 55 | 56 | i) changes to the Program, and 57 | 58 | ii) additions to the Program; 59 | 60 | where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program. 61 | 62 | "Contributor" means any person or entity that distributes the Program. 63 | 64 | "Licensed Patents " mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program. 65 | 66 | "Program" means the Contributions distributed in accordance with this Agreement. 67 | 68 | "Recipient" means anyone who receives the Program under this Agreement, including all Contributors. 69 | 70 | 2. GRANT OF RIGHTS 71 | 72 | a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form. 73 | 74 | b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder. 75 | 76 | c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program. 77 | 78 | d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement. 79 | 80 | 3. REQUIREMENTS 81 | 82 | A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that: 83 | 84 | a) it complies with the terms and conditions of this Agreement; and 85 | 86 | b) its license agreement: 87 | 88 | i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose; 89 | 90 | ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits; 91 | 92 | iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange. 95 | 96 | When the Program is made available in source code form: 97 | 98 | a) it must be made available under this Agreement; and 99 | 100 | b) a copy of this Agreement must be included with each copy of the Program. 101 | 102 | Contributors may not remove or alter any copyright notices contained within the Program. 103 | 104 | Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution. 105 | 106 | 4. COMMERCIAL DISTRIBUTION 107 | 108 | Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense. 109 | 110 | For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages. 111 | 112 | 5. NO WARRANTY 113 | 114 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations. 115 | 116 | 6. DISCLAIMER OF LIABILITY 117 | 118 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 119 | 120 | 7. GENERAL 121 | 122 | If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. 123 | 124 | If Recipient institutes patent litigation against a Contributor with respect to a patent applicable to software (including a cross-claim or counterclaim in a lawsuit), then any patent licenses granted by that Contributor to such Recipient under this Agreement shall terminate as of the date such litigation is filed. In addition, if Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed. 125 | 126 | All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive. 127 | 128 | Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. IBM is the initial Agreement Steward. IBM may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved. 129 | 130 | This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation. 131 | 132 | === GPL 2.0 133 | 134 | GNU GENERAL PUBLIC LICENSE 135 | Version 2, June 1991 136 | 137 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 138 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 139 | Everyone is permitted to copy and distribute verbatim copies 140 | of this license document, but changing it is not allowed. 141 | 142 | Preamble 143 | 144 | The licenses for most software are designed to take away your 145 | freedom to share and change it. By contrast, the GNU General Public 146 | License is intended to guarantee your freedom to share and change free 147 | software--to make sure the software is free for all its users. This 148 | General Public License applies to most of the Free Software 149 | Foundation's software and to any other program whose authors commit to 150 | using it. (Some other Free Software Foundation software is covered by 151 | the GNU Library General Public License instead.) You can apply it to 152 | your programs, too. 153 | 154 | When we speak of free software, we are referring to freedom, not 155 | price. Our General Public Licenses are designed to make sure that you 156 | have the freedom to distribute copies of free software (and charge for 157 | this service if you wish), that you receive source code or can get it 158 | if you want it, that you can change the software or use pieces of it 159 | in new free programs; and that you know you can do these things. 160 | 161 | To protect your rights, we need to make restrictions that forbid 162 | anyone to deny you these rights or to ask you to surrender the rights. 163 | These restrictions translate to certain responsibilities for you if you 164 | distribute copies of the software, or if you modify it. 165 | 166 | For example, if you distribute copies of such a program, whether 167 | gratis or for a fee, you must give the recipients all the rights that 168 | you have. You must make sure that they, too, receive or can get the 169 | source code. And you must show them these terms so they know their 170 | rights. 171 | 172 | We protect your rights with two steps: (1) copyright the software, and 173 | (2) offer you this license which gives you legal permission to copy, 174 | distribute and/or modify the software. 175 | 176 | Also, for each author's protection and ours, we want to make certain 177 | that everyone understands that there is no warranty for this free 178 | software. If the software is modified by someone else and passed on, we 179 | want its recipients to know that what they have is not the original, so 180 | that any problems introduced by others will not reflect on the original 181 | authors' reputations. 182 | 183 | Finally, any free program is threatened constantly by software 184 | patents. We wish to avoid the danger that redistributors of a free 185 | program will individually obtain patent licenses, in effect making the 186 | program proprietary. To prevent this, we have made it clear that any 187 | patent must be licensed for everyone's free use or not licensed at all. 188 | 189 | The precise terms and conditions for copying, distribution and 190 | modification follow. 191 | 192 | GNU GENERAL PUBLIC LICENSE 193 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 194 | 195 | 0. This License applies to any program or other work which contains 196 | a notice placed by the copyright holder saying it may be distributed 197 | under the terms of this General Public License. The "Program", below, 198 | refers to any such program or work, and a "work based on the Program" 199 | means either the Program or any derivative work under copyright law: 200 | that is to say, a work containing the Program or a portion of it, 201 | either verbatim or with modifications and/or translated into another 202 | language. (Hereinafter, translation is included without limitation in 203 | the term "modification".) Each licensee is addressed as "you". 204 | 205 | Activities other than copying, distribution and modification are not 206 | covered by this License; they are outside its scope. The act of 207 | running the Program is not restricted, and the output from the Program 208 | is covered only if its contents constitute a work based on the 209 | Program (independent of having been made by running the Program). 210 | Whether that is true depends on what the Program does. 211 | 212 | 1. You may copy and distribute verbatim copies of the Program's 213 | source code as you receive it, in any medium, provided that you 214 | conspicuously and appropriately publish on each copy an appropriate 215 | copyright notice and disclaimer of warranty; keep intact all the 216 | notices that refer to this License and to the absence of any warranty; 217 | and give any other recipients of the Program a copy of this License 218 | along with the Program. 219 | 220 | You may charge a fee for the physical act of transferring a copy, and 221 | you may at your option offer warranty protection in exchange for a fee. 222 | 223 | 2. You may modify your copy or copies of the Program or any portion 224 | of it, thus forming a work based on the Program, and copy and 225 | distribute such modifications or work under the terms of Section 1 226 | above, provided that you also meet all of these conditions: 227 | 228 | a) You must cause the modified files to carry prominent notices 229 | stating that you changed the files and the date of any change. 230 | 231 | b) You must cause any work that you distribute or publish, that in 232 | whole or in part contains or is derived from the Program or any 233 | part thereof, to be licensed as a whole at no charge to all third 234 | parties under the terms of this License. 235 | 236 | c) If the modified program normally reads commands interactively 237 | when run, you must cause it, when started running for such 238 | interactive use in the most ordinary way, to print or display an 239 | announcement including an appropriate copyright notice and a 240 | notice that there is no warranty (or else, saying that you provide 241 | a warranty) and that users may redistribute the program under 242 | these conditions, and telling the user how to view a copy of this 243 | License. (Exception: if the Program itself is interactive but 244 | does not normally print such an announcement, your work based on 245 | the Program is not required to print an announcement.) 246 | 247 | These requirements apply to the modified work as a whole. If 248 | identifiable sections of that work are not derived from the Program, 249 | and can be reasonably considered independent and separate works in 250 | themselves, then this License, and its terms, do not apply to those 251 | sections when you distribute them as separate works. But when you 252 | distribute the same sections as part of a whole which is a work based 253 | on the Program, the distribution of the whole must be on the terms of 254 | this License, whose permissions for other licensees extend to the 255 | entire whole, and thus to each and every part regardless of who wrote it. 256 | 257 | Thus, it is not the intent of this section to claim rights or contest 258 | your rights to work written entirely by you; rather, the intent is to 259 | exercise the right to control the distribution of derivative or 260 | collective works based on the Program. 261 | 262 | In addition, mere aggregation of another work not based on the Program 263 | with the Program (or with a work based on the Program) on a volume of 264 | a storage or distribution medium does not bring the other work under 265 | the scope of this License. 266 | 267 | 3. You may copy and distribute the Program (or a work based on it, 268 | under Section 2) in object code or executable form under the terms of 269 | Sections 1 and 2 above provided that you also do one of the following: 270 | 271 | a) Accompany it with the complete corresponding machine-readable 272 | source code, which must be distributed under the terms of Sections 273 | 1 and 2 above on a medium customarily used for software interchange; or, 274 | 275 | b) Accompany it with a written offer, valid for at least three 276 | years, to give any third party, for a charge no more than your 277 | cost of physically performing source distribution, a complete 278 | machine-readable copy of the corresponding source code, to be 279 | distributed under the terms of Sections 1 and 2 above on a medium 280 | customarily used for software interchange; or, 281 | 282 | c) Accompany it with the information you received as to the offer 283 | to distribute corresponding source code. (This alternative is 284 | allowed only for noncommercial distribution and only if you 285 | received the program in object code or executable form with such 286 | an offer, in accord with Subsection b above.) 287 | 288 | The source code for a work means the preferred form of the work for 289 | making modifications to it. For an executable work, complete source 290 | code means all the source code for all modules it contains, plus any 291 | associated interface definition files, plus the scripts used to 292 | control compilation and installation of the executable. However, as a 293 | special exception, the source code distributed need not include 294 | anything that is normally distributed (in either source or binary 295 | form) with the major components (compiler, kernel, and so on) of the 296 | operating system on which the executable runs, unless that component 297 | itself accompanies the executable. 298 | 299 | If distribution of executable or object code is made by offering 300 | access to copy from a designated place, then offering equivalent 301 | access to copy the source code from the same place counts as 302 | distribution of the source code, even though third parties are not 303 | compelled to copy the source along with the object code. 304 | 305 | 4. You may not copy, modify, sublicense, or distribute the Program 306 | except as expressly provided under this License. Any attempt 307 | otherwise to copy, modify, sublicense or distribute the Program is 308 | void, and will automatically terminate your rights under this License. 309 | However, parties who have received copies, or rights, from you under 310 | this License will not have their licenses terminated so long as such 311 | parties remain in full compliance. 312 | 313 | 5. You are not required to accept this License, since you have not 314 | signed it. However, nothing else grants you permission to modify or 315 | distribute the Program or its derivative works. These actions are 316 | prohibited by law if you do not accept this License. Therefore, by 317 | modifying or distributing the Program (or any work based on the 318 | Program), you indicate your acceptance of this License to do so, and 319 | all its terms and conditions for copying, distributing or modifying 320 | the Program or works based on it. 321 | 322 | 6. Each time you redistribute the Program (or any work based on the 323 | Program), the recipient automatically receives a license from the 324 | original licensor to copy, distribute or modify the Program subject to 325 | these terms and conditions. You may not impose any further 326 | restrictions on the recipients' exercise of the rights granted herein. 327 | You are not responsible for enforcing compliance by third parties to 328 | this License. 329 | 330 | 7. If, as a consequence of a court judgment or allegation of patent 331 | infringement or for any other reason (not limited to patent issues), 332 | conditions are imposed on you (whether by court order, agreement or 333 | otherwise) that contradict the conditions of this License, they do not 334 | excuse you from the conditions of this License. If you cannot 335 | distribute so as to satisfy simultaneously your obligations under this 336 | License and any other pertinent obligations, then as a consequence you 337 | may not distribute the Program at all. For example, if a patent 338 | license would not permit royalty-free redistribution of the Program by 339 | all those who receive copies directly or indirectly through you, then 340 | the only way you could satisfy both it and this License would be to 341 | refrain entirely from distribution of the Program. 342 | 343 | If any portion of this section is held invalid or unenforceable under 344 | any particular circumstance, the balance of the section is intended to 345 | apply and the section as a whole is intended to apply in other 346 | circumstances. 347 | 348 | It is not the purpose of this section to induce you to infringe any 349 | patents or other property right claims or to contest validity of any 350 | such claims; this section has the sole purpose of protecting the 351 | integrity of the free software distribution system, which is 352 | implemented by public license practices. Many people have made 353 | generous contributions to the wide range of software distributed 354 | through that system in reliance on consistent application of that 355 | system; it is up to the author/donor to decide if he or she is willing 356 | to distribute software through any other system and a licensee cannot 357 | impose that choice. 358 | 359 | This section is intended to make thoroughly clear what is believed to 360 | be a consequence of the rest of this License. 361 | 362 | 8. If the distribution and/or use of the Program is restricted in 363 | certain countries either by patents or by copyrighted interfaces, the 364 | original copyright holder who places the Program under this License 365 | may add an explicit geographical distribution limitation excluding 366 | those countries, so that distribution is permitted only in or among 367 | countries not thus excluded. In such case, this License incorporates 368 | the limitation as if written in the body of this License. 369 | 370 | 9. The Free Software Foundation may publish revised and/or new versions 371 | of the General Public License from time to time. Such new versions will 372 | be similar in spirit to the present version, but may differ in detail to 373 | address new problems or concerns. 374 | 375 | Each version is given a distinguishing version number. If the Program 376 | specifies a version number of this License which applies to it and "any 377 | later version", you have the option of following the terms and conditions 378 | either of that version or of any later version published by the Free 379 | Software Foundation. If the Program does not specify a version number of 380 | this License, you may choose any version ever published by the Free Software 381 | Foundation. 382 | 383 | 10. If you wish to incorporate parts of the Program into other free 384 | programs whose distribution conditions are different, write to the author 385 | to ask for permission. For software which is copyrighted by the Free 386 | Software Foundation, write to the Free Software Foundation; we sometimes 387 | make exceptions for this. Our decision will be guided by the two goals 388 | of preserving the free status of all derivatives of our free software and 389 | of promoting the sharing and reuse of software generally. 390 | 391 | NO WARRANTY 392 | 393 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 394 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 395 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 396 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 397 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 398 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 399 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 400 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 401 | REPAIR OR CORRECTION. 402 | 403 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 404 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 405 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 406 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 407 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 408 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 409 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 410 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 411 | POSSIBILITY OF SUCH DAMAGES. 412 | 413 | END OF TERMS AND CONDITIONS 414 | 415 | == GNU LGPL 2.1 416 | 417 | GNU LESSER GENERAL PUBLIC LICENSE 418 | Version 2.1, February 1999 419 | 420 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 421 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 422 | Everyone is permitted to copy and distribute verbatim copies 423 | of this license document, but changing it is not allowed. 424 | 425 | [This is the first released version of the Lesser GPL. It also counts 426 | as the successor of the GNU Library Public License, version 2, hence 427 | the version number 2.1.] 428 | 429 | Preamble 430 | 431 | The licenses for most software are designed to take away your 432 | freedom to share and change it. By contrast, the GNU General Public 433 | Licenses are intended to guarantee your freedom to share and change 434 | free software--to make sure the software is free for all its users. 435 | 436 | This license, the Lesser General Public License, applies to some 437 | specially designated software packages--typically libraries--of the 438 | Free Software Foundation and other authors who decide to use it. You 439 | can use it too, but we suggest you first think carefully about whether 440 | this license or the ordinary General Public License is the better 441 | strategy to use in any particular case, based on the explanations below. 442 | 443 | When we speak of free software, we are referring to freedom of use, 444 | not price. Our General Public Licenses are designed to make sure that 445 | you have the freedom to distribute copies of free software (and charge 446 | for this service if you wish); that you receive source code or can get 447 | it if you want it; that you can change the software and use pieces of 448 | it in new free programs; and that you are informed that you can do 449 | these things. 450 | 451 | To protect your rights, we need to make restrictions that forbid 452 | distributors to deny you these rights or to ask you to surrender these 453 | rights. These restrictions translate to certain responsibilities for 454 | you if you distribute copies of the library or if you modify it. 455 | 456 | For example, if you distribute copies of the library, whether gratis 457 | or for a fee, you must give the recipients all the rights that we gave 458 | you. You must make sure that they, too, receive or can get the source 459 | code. If you link other code with the library, you must provide 460 | complete object files to the recipients, so that they can relink them 461 | with the library after making changes to the library and recompiling 462 | it. And you must show them these terms so they know their rights. 463 | 464 | We protect your rights with a two-step method: (1) we copyright the 465 | library, and (2) we offer you this license, which gives you legal 466 | permission to copy, distribute and/or modify the library. 467 | 468 | To protect each distributor, we want to make it very clear that 469 | there is no warranty for the free library. Also, if the library is 470 | modified by someone else and passed on, the recipients should know 471 | that what they have is not the original version, so that the original 472 | author's reputation will not be affected by problems that might be 473 | introduced by others. 474 | 475 | Finally, software patents pose a constant threat to the existence of 476 | any free program. We wish to make sure that a company cannot 477 | effectively restrict the users of a free program by obtaining a 478 | restrictive license from a patent holder. Therefore, we insist that 479 | any patent license obtained for a version of the library must be 480 | consistent with the full freedom of use specified in this license. 481 | 482 | Most GNU software, including some libraries, is covered by the 483 | ordinary GNU General Public License. This license, the GNU Lesser 484 | General Public License, applies to certain designated libraries, and 485 | is quite different from the ordinary General Public License. We use 486 | this license for certain libraries in order to permit linking those 487 | libraries into non-free programs. 488 | 489 | When a program is linked with a library, whether statically or using 490 | a shared library, the combination of the two is legally speaking a 491 | combined work, a derivative of the original library. The ordinary 492 | General Public License therefore permits such linking only if the 493 | entire combination fits its criteria of freedom. The Lesser General 494 | Public License permits more lax criteria for linking other code with 495 | the library. 496 | 497 | We call this license the "Lesser" General Public License because it 498 | does Less to protect the user's freedom than the ordinary General 499 | Public License. It also provides other free software developers Less 500 | of an advantage over competing non-free programs. These disadvantages 501 | are the reason we use the ordinary General Public License for many 502 | libraries. However, the Lesser license provides advantages in certain 503 | special circumstances. 504 | 505 | For example, on rare occasions, there may be a special need to 506 | encourage the widest possible use of a certain library, so that it becomes 507 | a de-facto standard. To achieve this, non-free programs must be 508 | allowed to use the library. A more frequent case is that a free 509 | library does the same job as widely used non-free libraries. In this 510 | case, there is little to gain by limiting the free library to free 511 | software only, so we use the Lesser General Public License. 512 | 513 | In other cases, permission to use a particular library in non-free 514 | programs enables a greater number of people to use a large body of 515 | free software. For example, permission to use the GNU C Library in 516 | non-free programs enables many more people to use the whole GNU 517 | operating system, as well as its variant, the GNU/Linux operating 518 | system. 519 | 520 | Although the Lesser General Public License is Less protective of the 521 | users' freedom, it does ensure that the user of a program that is 522 | linked with the Library has the freedom and the wherewithal to run 523 | that program using a modified version of the Library. 524 | 525 | The precise terms and conditions for copying, distribution and 526 | modification follow. Pay close attention to the difference between a 527 | "work based on the library" and a "work that uses the library". The 528 | former contains code derived from the library, whereas the latter must 529 | be combined with the library in order to run. 530 | 531 | GNU LESSER GENERAL PUBLIC LICENSE 532 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 533 | 534 | 0. This License Agreement applies to any software library or other 535 | program which contains a notice placed by the copyright holder or 536 | other authorized party saying it may be distributed under the terms of 537 | this Lesser General Public License (also called "this License"). 538 | Each licensee is addressed as "you". 539 | 540 | A "library" means a collection of software functions and/or data 541 | prepared so as to be conveniently linked with application programs 542 | (which use some of those functions and data) to form executables. 543 | 544 | The "Library", below, refers to any such software library or work 545 | which has been distributed under these terms. A "work based on the 546 | Library" means either the Library or any derivative work under 547 | copyright law: that is to say, a work containing the Library or a 548 | portion of it, either verbatim or with modifications and/or translated 549 | straightforwardly into another language. (Hereinafter, translation is 550 | included without limitation in the term "modification".) 551 | 552 | "Source code" for a work means the preferred form of the work for 553 | making modifications to it. For a library, complete source code means 554 | all the source code for all modules it contains, plus any associated 555 | interface definition files, plus the scripts used to control compilation 556 | and installation of the library. 557 | 558 | Activities other than copying, distribution and modification are not 559 | covered by this License; they are outside its scope. The act of 560 | running a program using the Library is not restricted, and output from 561 | such a program is covered only if its contents constitute a work based 562 | on the Library (independent of the use of the Library in a tool for 563 | writing it). Whether that is true depends on what the Library does 564 | and what the program that uses the Library does. 565 | 566 | 1. You may copy and distribute verbatim copies of the Library's 567 | complete source code as you receive it, in any medium, provided that 568 | you conspicuously and appropriately publish on each copy an 569 | appropriate copyright notice and disclaimer of warranty; keep intact 570 | all the notices that refer to this License and to the absence of any 571 | warranty; and distribute a copy of this License along with the 572 | Library. 573 | 574 | You may charge a fee for the physical act of transferring a copy, 575 | and you may at your option offer warranty protection in exchange for a 576 | fee. 577 | 578 | 2. You may modify your copy or copies of the Library or any portion 579 | of it, thus forming a work based on the Library, and copy and 580 | distribute such modifications or work under the terms of Section 1 581 | above, provided that you also meet all of these conditions: 582 | 583 | a) The modified work must itself be a software library. 584 | 585 | b) You must cause the files modified to carry prominent notices 586 | stating that you changed the files and the date of any change. 587 | 588 | c) You must cause the whole of the work to be licensed at no 589 | charge to all third parties under the terms of this License. 590 | 591 | d) If a facility in the modified Library refers to a function or a 592 | table of data to be supplied by an application program that uses 593 | the facility, other than as an argument passed when the facility 594 | is invoked, then you must make a good faith effort to ensure that, 595 | in the event an application does not supply such function or 596 | table, the facility still operates, and performs whatever part of 597 | its purpose remains meaningful. 598 | 599 | (For example, a function in a library to compute square roots has 600 | a purpose that is entirely well-defined independent of the 601 | application. Therefore, Subsection 2d requires that any 602 | application-supplied function or table used by this function must 603 | be optional: if the application does not supply it, the square 604 | root function must still compute square roots.) 605 | 606 | These requirements apply to the modified work as a whole. If 607 | identifiable sections of that work are not derived from the Library, 608 | and can be reasonably considered independent and separate works in 609 | themselves, then this License, and its terms, do not apply to those 610 | sections when you distribute them as separate works. But when you 611 | distribute the same sections as part of a whole which is a work based 612 | on the Library, the distribution of the whole must be on the terms of 613 | this License, whose permissions for other licensees extend to the 614 | entire whole, and thus to each and every part regardless of who wrote 615 | it. 616 | 617 | Thus, it is not the intent of this section to claim rights or contest 618 | your rights to work written entirely by you; rather, the intent is to 619 | exercise the right to control the distribution of derivative or 620 | collective works based on the Library. 621 | 622 | In addition, mere aggregation of another work not based on the Library 623 | with the Library (or with a work based on the Library) on a volume of 624 | a storage or distribution medium does not bring the other work under 625 | the scope of this License. 626 | 627 | 3. You may opt to apply the terms of the ordinary GNU General Public 628 | License instead of this License to a given copy of the Library. To do 629 | this, you must alter all the notices that refer to this License, so 630 | that they refer to the ordinary GNU General Public License, version 2, 631 | instead of to this License. (If a newer version than version 2 of the 632 | ordinary GNU General Public License has appeared, then you can specify 633 | that version instead if you wish.) Do not make any other change in 634 | these notices. 635 | 636 | Once this change is made in a given copy, it is irreversible for 637 | that copy, so the ordinary GNU General Public License applies to all 638 | subsequent copies and derivative works made from that copy. 639 | 640 | This option is useful when you wish to copy part of the code of 641 | the Library into a program that is not a library. 642 | 643 | 4. You may copy and distribute the Library (or a portion or 644 | derivative of it, under Section 2) in object code or executable form 645 | under the terms of Sections 1 and 2 above provided that you accompany 646 | it with the complete corresponding machine-readable source code, which 647 | must be distributed under the terms of Sections 1 and 2 above on a 648 | medium customarily used for software interchange. 649 | 650 | If distribution of object code is made by offering access to copy 651 | from a designated place, then offering equivalent access to copy the 652 | source code from the same place satisfies the requirement to 653 | distribute the source code, even though third parties are not 654 | compelled to copy the source along with the object code. 655 | 656 | 5. A program that contains no derivative of any portion of the 657 | Library, but is designed to work with the Library by being compiled or 658 | linked with it, is called a "work that uses the Library". Such a 659 | work, in isolation, is not a derivative work of the Library, and 660 | therefore falls outside the scope of this License. 661 | 662 | However, linking a "work that uses the Library" with the Library 663 | creates an executable that is a derivative of the Library (because it 664 | contains portions of the Library), rather than a "work that uses the 665 | library". The executable is therefore covered by this License. 666 | Section 6 states terms for distribution of such executables. 667 | 668 | When a "work that uses the Library" uses material from a header file 669 | that is part of the Library, the object code for the work may be a 670 | derivative work of the Library even though the source code is not. 671 | Whether this is true is especially significant if the work can be 672 | linked without the Library, or if the work is itself a library. The 673 | threshold for this to be true is not precisely defined by law. 674 | 675 | If such an object file uses only numerical parameters, data 676 | structure layouts and accessors, and small macros and small inline 677 | functions (ten lines or less in length), then the use of the object 678 | file is unrestricted, regardless of whether it is legally a derivative 679 | work. (Executables containing this object code plus portions of the 680 | Library will still fall under Section 6.) 681 | 682 | Otherwise, if the work is a derivative of the Library, you may 683 | distribute the object code for the work under the terms of Section 6. 684 | Any executables containing that work also fall under Section 6, 685 | whether or not they are linked directly with the Library itself. 686 | 687 | 6. As an exception to the Sections above, you may also combine or 688 | link a "work that uses the Library" with the Library to produce a 689 | work containing portions of the Library, and distribute that work 690 | under terms of your choice, provided that the terms permit 691 | modification of the work for the customer's own use and reverse 692 | engineering for debugging such modifications. 693 | 694 | You must give prominent notice with each copy of the work that the 695 | Library is used in it and that the Library and its use are covered by 696 | this License. You must supply a copy of this License. If the work 697 | during execution displays copyright notices, you must include the 698 | copyright notice for the Library among them, as well as a reference 699 | directing the user to the copy of this License. Also, you must do one 700 | of these things: 701 | 702 | a) Accompany the work with the complete corresponding 703 | machine-readable source code for the Library including whatever 704 | changes were used in the work (which must be distributed under 705 | Sections 1 and 2 above); and, if the work is an executable linked 706 | with the Library, with the complete machine-readable "work that 707 | uses the Library", as object code and/or source code, so that the 708 | user can modify the Library and then relink to produce a modified 709 | executable containing the modified Library. (It is understood 710 | that the user who changes the contents of definitions files in the 711 | Library will not necessarily be able to recompile the application 712 | to use the modified definitions.) 713 | 714 | b) Use a suitable shared library mechanism for linking with the 715 | Library. A suitable mechanism is one that (1) uses at run time a 716 | copy of the library already present on the user's computer system, 717 | rather than copying library functions into the executable, and (2) 718 | will operate properly with a modified version of the library, if 719 | the user installs one, as long as the modified version is 720 | interface-compatible with the version that the work was made with. 721 | 722 | c) Accompany the work with a written offer, valid for at 723 | least three years, to give the same user the materials 724 | specified in Subsection 6a, above, for a charge no more 725 | than the cost of performing this distribution. 726 | 727 | d) If distribution of the work is made by offering access to copy 728 | from a designated place, offer equivalent access to copy the above 729 | specified materials from the same place. 730 | 731 | e) Verify that the user has already received a copy of these 732 | materials or that you have already sent this user a copy. 733 | 734 | For an executable, the required form of the "work that uses the 735 | Library" must include any data and utility programs needed for 736 | reproducing the executable from it. However, as a special exception, 737 | the materials to be distributed need not include anything that is 738 | normally distributed (in either source or binary form) with the major 739 | components (compiler, kernel, and so on) of the operating system on 740 | which the executable runs, unless that component itself accompanies 741 | the executable. 742 | 743 | It may happen that this requirement contradicts the license 744 | restrictions of other proprietary libraries that do not normally 745 | accompany the operating system. Such a contradiction means you cannot 746 | use both them and the Library together in an executable that you 747 | distribute. 748 | 749 | 7. You may place library facilities that are a work based on the 750 | Library side-by-side in a single library together with other library 751 | facilities not covered by this License, and distribute such a combined 752 | library, provided that the separate distribution of the work based on 753 | the Library and of the other library facilities is otherwise 754 | permitted, and provided that you do these two things: 755 | 756 | a) Accompany the combined library with a copy of the same work 757 | based on the Library, uncombined with any other library 758 | facilities. This must be distributed under the terms of the 759 | Sections above. 760 | 761 | b) Give prominent notice with the combined library of the fact 762 | that part of it is a work based on the Library, and explaining 763 | where to find the accompanying uncombined form of the same work. 764 | 765 | 8. You may not copy, modify, sublicense, link with, or distribute 766 | the Library except as expressly provided under this License. Any 767 | attempt otherwise to copy, modify, sublicense, link with, or 768 | distribute the Library is void, and will automatically terminate your 769 | rights under this License. However, parties who have received copies, 770 | or rights, from you under this License will not have their licenses 771 | terminated so long as such parties remain in full compliance. 772 | 773 | 9. You are not required to accept this License, since you have not 774 | signed it. However, nothing else grants you permission to modify or 775 | distribute the Library or its derivative works. These actions are 776 | prohibited by law if you do not accept this License. Therefore, by 777 | modifying or distributing the Library (or any work based on the 778 | Library), you indicate your acceptance of this License to do so, and 779 | all its terms and conditions for copying, distributing or modifying 780 | the Library or works based on it. 781 | 782 | 10. Each time you redistribute the Library (or any work based on the 783 | Library), the recipient automatically receives a license from the 784 | original licensor to copy, distribute, link with or modify the Library 785 | subject to these terms and conditions. You may not impose any further 786 | restrictions on the recipients' exercise of the rights granted herein. 787 | You are not responsible for enforcing compliance by third parties with 788 | this License. 789 | 790 | 11. If, as a consequence of a court judgment or allegation of patent 791 | infringement or for any other reason (not limited to patent issues), 792 | conditions are imposed on you (whether by court order, agreement or 793 | otherwise) that contradict the conditions of this License, they do not 794 | excuse you from the conditions of this License. If you cannot 795 | distribute so as to satisfy simultaneously your obligations under this 796 | License and any other pertinent obligations, then as a consequence you 797 | may not distribute the Library at all. For example, if a patent 798 | license would not permit royalty-free redistribution of the Library by 799 | all those who receive copies directly or indirectly through you, then 800 | the only way you could satisfy both it and this License would be to 801 | refrain entirely from distribution of the Library. 802 | 803 | If any portion of this section is held invalid or unenforceable under any 804 | particular circumstance, the balance of the section is intended to apply, 805 | and the section as a whole is intended to apply in other circumstances. 806 | 807 | It is not the purpose of this section to induce you to infringe any 808 | patents or other property right claims or to contest validity of any 809 | such claims; this section has the sole purpose of protecting the 810 | integrity of the free software distribution system which is 811 | implemented by public license practices. Many people have made 812 | generous contributions to the wide range of software distributed 813 | through that system in reliance on consistent application of that 814 | system; it is up to the author/donor to decide if he or she is willing 815 | to distribute software through any other system and a licensee cannot 816 | impose that choice. 817 | 818 | This section is intended to make thoroughly clear what is believed to 819 | be a consequence of the rest of this License. 820 | 821 | 12. If the distribution and/or use of the Library is restricted in 822 | certain countries either by patents or by copyrighted interfaces, the 823 | original copyright holder who places the Library under this License may add 824 | an explicit geographical distribution limitation excluding those countries, 825 | so that distribution is permitted only in or among countries not thus 826 | excluded. In such case, this License incorporates the limitation as if 827 | written in the body of this License. 828 | 829 | 13. The Free Software Foundation may publish revised and/or new 830 | versions of the Lesser General Public License from time to time. 831 | Such new versions will be similar in spirit to the present version, 832 | but may differ in detail to address new problems or concerns. 833 | 834 | Each version is given a distinguishing version number. If the Library 835 | specifies a version number of this License which applies to it and 836 | "any later version", you have the option of following the terms and 837 | conditions either of that version or of any later version published by 838 | the Free Software Foundation. If the Library does not specify a 839 | license version number, you may choose any version ever published by 840 | the Free Software Foundation. 841 | 842 | 14. If you wish to incorporate parts of the Library into other free 843 | programs whose distribution conditions are incompatible with these, 844 | write to the author to ask for permission. For software which is 845 | copyrighted by the Free Software Foundation, write to the Free 846 | Software Foundation; we sometimes make exceptions for this. Our 847 | decision will be guided by the two goals of preserving the free status 848 | of all derivatives of our free software and of promoting the sharing 849 | and reuse of software generally. 850 | 851 | NO WARRANTY 852 | 853 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 854 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 855 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 856 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 857 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 858 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 859 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 860 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 861 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 862 | 863 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 864 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 865 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 866 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 867 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 868 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 869 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 870 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 871 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 872 | DAMAGES. 873 | 874 | END OF TERMS AND CONDITIONS 875 | 876 | How to Apply These Terms to Your New Libraries 877 | 878 | If you develop a new library, and you want it to be of the greatest 879 | possible use to the public, we recommend making it free software that 880 | everyone can redistribute and change. You can do so by permitting 881 | redistribution under these terms (or, alternatively, under the terms of the 882 | ordinary General Public License). 883 | 884 | To apply these terms, attach the following notices to the library. It is 885 | safest to attach them to the start of each source file to most effectively 886 | convey the exclusion of warranty; and each file should have at least the 887 | "copyright" line and a pointer to where the full notice is found. 888 | 889 | 890 | Copyright (C) 891 | 892 | This library is free software; you can redistribute it and/or 893 | modify it under the terms of the GNU Lesser General Public 894 | License as published by the Free Software Foundation; either 895 | version 2.1 of the License, or (at your option) any later version. 896 | 897 | This library is distributed in the hope that it will be useful, 898 | but WITHOUT ANY WARRANTY; without even the implied warranty of 899 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 900 | Lesser General Public License for more details. 901 | 902 | You should have received a copy of the GNU Lesser General Public 903 | License along with this library; if not, write to the Free Software 904 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 905 | 906 | Also add information on how to contact you by electronic and paper mail. 907 | 908 | You should also get your employer (if you work as a programmer) or your 909 | school, if any, to sign a "copyright disclaimer" for the library, if 910 | necessary. Here is a sample; alter the names: 911 | 912 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 913 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 914 | 915 | , 1 April 1990 916 | Ty Coon, President of Vice 917 | 918 | That's all there is to it! 919 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Gressil uses [jnr-ffi](https://github.com/jnr/jnr-ffi) to provide 2 | daemonization and "forking" for Java processes. It uses 3 | posix_spawn to achieve this, rather than 4 | fork and exec. Spawn is used, rather than 5 | the standard C world idiom of fork followed by 6 | exec as fork is very unsafe on the JVM -- 7 | there is no such thing as a critical section which cannot get splatted 8 | by GC reshuffling pointers. Here, the child process is started by spawning 9 | a new process complete with command line, *not* by forking the state of the 10 | parent process. 11 | 12 | Usage for daemonization looks like: 13 | 14 | ```java 15 | package org.skife.gressil.examples; 16 | 17 | import org.skife.gressil.Daemon; 18 | 19 | import java.io.File; 20 | import java.io.IOException; 21 | import java.util.Arrays; 22 | import java.util.Date; 23 | 24 | import static org.skife.gressil.Daemon.remoteDebugOnPort; 25 | 26 | public class ChattyDaemon 27 | { 28 | public static void main(String[] args) throws IOException 29 | { 30 | new Daemon().withMainArgs(args) 31 | .withPidFile(new File("/tmp/chatty.pid")) 32 | .withStdout(new File("/tmp/chatty.out")) 33 | .withExtraMainArgs("hello", "world,", "how are you?") 34 | .withExtraJvmArgs(remoteDebugOnPort(5005)) 35 | .daemonize(); 36 | 37 | while (!Thread.currentThread().isInterrupted()) { 38 | System.out.println(new Date() + " " + Arrays.toString(args)); 39 | try { 40 | Thread.sleep(1000); 41 | } 42 | catch (InterruptedException e) { 43 | Thread.currentThread().interrupt(); 44 | } 45 | } 46 | } 47 | } 48 | ``` 49 | In the parent process the call to Daemon#daemonize() will 50 | call System.exit(), in the child process it will return 51 | normally. 52 | 53 | The child process, in this case, will also wait for a Java debugger to 54 | attach on port 5005. It will attach stdout to /tmp/chatty.out, 55 | and stdin and stderr will default to /dev/null (which stdout would also attach to 56 | by default if it were not specified). 57 | 58 | The easiest way to get started is via maven: 59 | 60 | ```xml 61 | 62 | org.skife.gressil 63 | gressil 64 | 0.0.4 65 | 66 | ``` 67 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | org.skife.gressil 4 | gressil 5 | jar 6 | 0.0.5-SNAPSHOT 7 | gressil 8 | http://github.com/brianm/gressil 9 | 10 | Java Process Daemonization via posix_spawn 11 | 12 | 13 | 14 | org.basepom 15 | basepom-standard-oss 16 | 4 17 | 18 | 19 | 20 | 21 | Apache License 2.0 22 | http://www.apache.org/licenses/LICENSE-2.0.html 23 | repo 24 | 25 | 26 | 27 | 28 | scm:git:git://github.com/brianm/gressil.git 29 | scm:git:git@github.com:brianm/gressil.git 30 | http://github.com/brianm/gressil/tree/master 31 | gressil-0.0.5 32 | 33 | 34 | 35 | ${env.JAVA7_HOME} 36 | 1.7 37 | UTF-8 38 | true 39 | true 40 | true 41 | false 42 | true 43 | 44 | 45 | 46 | 47 | 48 | com.github.jnr 49 | jnr-ffi 50 | 0.6.0 51 | 52 | 53 | 54 | com.google.guava 55 | guava 56 | 11.0.1 57 | test 58 | 59 | 60 | 61 | junit 62 | junit 63 | 4.8.2 64 | test 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | org.apache.maven.plugins 73 | maven-release-plugin 74 | 2.4.2 75 | 76 | 77 | org.apache.maven.scm 78 | maven-scm-provider-gitexe 79 | 1.9 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | brianm 89 | Brian McCallister 90 | brianm@skife.org 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /src/main/java/org/skife/gressil/ArgvFinder.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | import java.util.List; 4 | 5 | interface ArgvFinder 6 | { 7 | public List getArgv(); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/org/skife/gressil/Daemon.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | import jnr.ffi.Library; 4 | import jnr.ffi.Pointer; 5 | import jnr.ffi.byref.IntByReference; 6 | 7 | import java.io.File; 8 | import java.io.FileOutputStream; 9 | import java.io.IOException; 10 | import java.io.OutputStream; 11 | import java.io.PrintStream; 12 | import java.nio.charset.StandardCharsets; 13 | import java.nio.file.Files; 14 | import java.util.ArrayList; 15 | import java.util.Collections; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | import static java.lang.String.format; 20 | import static java.util.Arrays.asList; 21 | 22 | public class Daemon 23 | { 24 | private final List programArgs; 25 | private final File pidfile; 26 | private final File out; 27 | private final File err; 28 | private final List extraVmArgs; 29 | private final List extraProgramArgs; 30 | 31 | private static final MicroC posix = Library.loadLibrary("c", MicroC.class); 32 | 33 | public Daemon() 34 | { 35 | this(null, 36 | null, 37 | new File("/dev/null"), 38 | new File("/dev/null"), 39 | Collections.emptyList(), 40 | Collections.emptyList()); 41 | } 42 | 43 | private Daemon(List argv, File pidfile, File out, File err, List extraVmArgs, List extraProgramArgs) 44 | { 45 | this.programArgs = argv; 46 | this.pidfile = pidfile; 47 | this.out = out; 48 | this.err = err; 49 | this.extraVmArgs = extraVmArgs; 50 | this.extraProgramArgs = extraProgramArgs; 51 | } 52 | 53 | /** 54 | * tl;dr pass the String[] received in your public static void main(String[] args) call here. 55 | *

56 | * This is the preferred way to get the program arguments. The argument here should be the same 57 | * array of strings as was passed to main(String[] args). 58 | *

59 | * If the args are *not* passed here, we will attempt to figure out what they are by poking 60 | * around the JVM, but the "poke around the JVM" method gets confused by whitespace in argument 61 | * names (ie, quoted arguments) which in the real ARGV will be one element, but via the poke around 62 | * the JVM method of figuring out program args the whitespace will lead to it being two arguments. 63 | * This usually leads to undesired behavior. 64 | */ 65 | public Daemon withMainArgs(String... args) 66 | { 67 | return withArgv(asList(args)); 68 | } 69 | 70 | public Daemon withArgv(List args) 71 | { 72 | return new Daemon(args, pidfile, out, err, extraVmArgs, extraProgramArgs); 73 | } 74 | 75 | public Daemon withExtraJvmArgs(List extraVmArgs) 76 | { 77 | return new Daemon(programArgs, pidfile, out, err, extraVmArgs, extraProgramArgs); 78 | } 79 | 80 | public Daemon withExtraJvmArgs(String... extraVmArgs) 81 | { 82 | return new Daemon(programArgs, pidfile, out, err, asList(extraVmArgs), extraProgramArgs); 83 | } 84 | 85 | public Daemon withExtraMainArgs(List extraProgramArgs) 86 | { 87 | return new Daemon(programArgs, pidfile, out, err, extraVmArgs, extraProgramArgs); 88 | } 89 | 90 | public Daemon withExtraMainArgs(String... extraProgramArgs) 91 | { 92 | return new Daemon(programArgs, pidfile, out, err, extraVmArgs, asList(extraProgramArgs)); 93 | } 94 | 95 | public Daemon withPidFile(File pidfile) 96 | { 97 | return new Daemon(programArgs, pidfile, out, err, extraVmArgs, extraProgramArgs); 98 | } 99 | 100 | public Daemon withStdout(File out) 101 | { 102 | return new Daemon(programArgs, pidfile, out, err, extraVmArgs, extraProgramArgs); 103 | } 104 | 105 | public Daemon withStderr(File err) 106 | { 107 | return new Daemon(programArgs, pidfile, out, err, extraVmArgs, extraProgramArgs); 108 | } 109 | 110 | Status forkish() throws IOException 111 | { 112 | if (isDaemon()) { 113 | posix.setsid(); 114 | 115 | OutputStream old_out = System.out; 116 | OutputStream old_err = System.err; 117 | 118 | System.setOut(new PrintStream(new FileOutputStream(out, true))); 119 | System.setErr(new PrintStream(new FileOutputStream(err, true))); 120 | old_err.close(); 121 | old_out.close(); 122 | 123 | if (pidfile != null) { 124 | FileOutputStream p_out = new FileOutputStream(pidfile); 125 | p_out.write(String.valueOf(posix.getpid()).getBytes()); 126 | p_out.close(); 127 | pidfile.deleteOnExit(); 128 | } 129 | 130 | return Status.child(posix.getpid()); 131 | } 132 | else 133 | { 134 | String[] envp = getEnv(Daemon.class.getName() + "=daemon"); 135 | List argv = buildARGV(posix); 136 | 137 | jnr.ffi.Runtime runtime = jnr.ffi.Runtime.getSystemRuntime(); 138 | Pointer NULL = Pointer.wrap(runtime, 0L); 139 | IntByReference child_pid = new IntByReference(); 140 | 141 | int rs = posix.posix_spawnp(child_pid, argv.get(0), NULL, NULL, 142 | argv.toArray(new String[argv.size()]), envp); 143 | if (rs != 0) { 144 | throw new RuntimeException(posix.strerror(rs)); 145 | } 146 | return Status.parent(child_pid.getValue()); 147 | } 148 | } 149 | 150 | public void daemonize() throws IOException 151 | { 152 | if (forkish().isParent()) { 153 | System.exit(0); 154 | } 155 | } 156 | 157 | public static boolean isDaemon() 158 | { 159 | return "daemon".equals(System.getenv(Daemon.class.getName())); 160 | } 161 | 162 | public List buildARGV(MicroC posix) 163 | { 164 | List argv; 165 | String os = System.getProperty("os.name"); 166 | if (this.programArgs != null) { 167 | // if we had args passed to us, don't mess around, just use them 168 | argv = new JvmBasedArgvFinder(this.programArgs).getArgv(); 169 | } 170 | else if ("Linux".equals(os)) { 171 | argv = new LinuxArgvFinder(posix.getpid()).getArgv(); 172 | } 173 | // else if ("Mac OS X".equals(os)) { 174 | // argv = new MacARGVFinder().getArgv(); 175 | // if (!argv.get(0).endsWith("java")) { 176 | // this works sometimes, not others, needs debugging. For now this heuristic seems to work 177 | // argv = new JvmBasedArgvFinder(this.programArgs).getArgv(); 178 | // } 179 | // } 180 | else 181 | { 182 | argv = new JvmBasedArgvFinder(this.programArgs).getArgv(); 183 | } 184 | 185 | if (this.extraVmArgs.size() > 0) { 186 | List new_argv = new ArrayList(argv.size() + extraVmArgs.size()); 187 | new_argv.add(argv.get(0)); 188 | new_argv.addAll(extraVmArgs); 189 | new_argv.addAll(argv.subList(1, argv.size())); 190 | argv = new_argv; 191 | } 192 | 193 | if (this.extraProgramArgs.size() > 0) { 194 | argv.addAll(extraProgramArgs); 195 | } 196 | 197 | return argv; 198 | } 199 | 200 | /** 201 | * Creates vm arguments for jdwp remote debugging, suspending the VM on startup 202 | * 203 | * @param port port to listen for remote debugger on 204 | */ 205 | public static List waitForRemoteDebugOnPort(int port) 206 | { 207 | return asList("-Xdebug", format("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=%d", port)); 208 | } 209 | 210 | /** 211 | * Creates vm arguments for jdwp remote debugging, without suspending the VM on startup 212 | * 213 | * @param port port to listen for remote debugger on 214 | */ 215 | public static List remoteDebugOnPort(int port) 216 | { 217 | return asList("-Xdebug", format("-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=%d", port)); 218 | } 219 | 220 | public static String[] getEnv(String... additions) 221 | { 222 | String[] envp = new String[System.getenv().size() + additions.length]; 223 | int i = 0; 224 | for (Map.Entry pair : System.getenv().entrySet()) { 225 | envp[i++] = new StringBuilder(pair.getKey()).append("=").append(pair.getValue()).toString(); 226 | } 227 | System.arraycopy(additions, 0, envp, envp.length - 1, additions.length); 228 | return envp; 229 | } 230 | 231 | /** 232 | * 0 program is running or service is OK 233 | * 1 program is dead and /var/run pid file exists 234 | * 2 program is dead and /var/lock lock file exists 235 | * 3 program is not running 236 | * 4 program or service status is unknown 237 | * 5-99 reserved for future LSB use 238 | * 100-149 reserved for distribution use 239 | * 150-199 reserved for application use 240 | * 200-254 reserved 241 | */ 242 | public DaemonStatus checkStatus() 243 | { 244 | if (this.pidfile == null) { 245 | throw new IllegalStateException("No pidfile specified, cannot check status!"); 246 | } 247 | if (!pidfile.exists()) { 248 | return DaemonStatus.STATUS_NOT_RUNNING; 249 | } 250 | 251 | final int pid; 252 | try 253 | { 254 | byte[] content = Files.readAllBytes(pidfile.toPath()); 255 | String s = new String(content, StandardCharsets.UTF_8).trim(); 256 | pid = Integer.parseInt(s); 257 | 258 | } 259 | catch (Exception e) 260 | { 261 | System.err.println(e.getMessage()); 262 | return DaemonStatus.STATUS_UNKNOWN; 263 | } 264 | 265 | int rs = posix.kill(pid, 0); 266 | if (rs == 0) { 267 | return DaemonStatus.STATUS_RUNNING; 268 | } 269 | else 270 | { 271 | return DaemonStatus.STATUS_DEAD; 272 | } 273 | } 274 | 275 | public DaemonStatus stop() 276 | { 277 | /* 278 | 1 generic or unspecified error (current practice) 279 | 2 invalid or excess argument(s) 280 | 3 unimplemented feature (for example, "reload") 281 | 4 user had insufficient privilege 282 | 5 program is not installed 283 | 6 program is not configured 284 | 7 program is not running 285 | 8-99 reserved for future LSB use 286 | 100-149 reserved for distribution use 287 | 150-199 reserved for application use 288 | 200-254 reserved 289 | */ 290 | if (this.pidfile == null) { 291 | throw new IllegalStateException("No pidfile specified, cannot stop!"); 292 | } 293 | if (!pidfile.exists()) { 294 | return DaemonStatus.STOP_NOT_RUNNING; 295 | } 296 | 297 | final int pid; 298 | try 299 | { 300 | byte[] content = Files.readAllBytes(pidfile.toPath()); 301 | String s = new String(content, StandardCharsets.UTF_8).trim(); 302 | pid = Integer.parseInt(s); 303 | 304 | } 305 | catch (Exception e) 306 | { 307 | System.err.println(e.getMessage()); 308 | return DaemonStatus.STOP_GENERAL_ERROR; 309 | } 310 | 311 | int rs = posix.kill(pid, 2); 312 | if (rs == 0) { 313 | return DaemonStatus.STOP_SUCCESS; 314 | } 315 | else 316 | { 317 | return DaemonStatus.STOP_GENERAL_ERROR; 318 | } 319 | } 320 | 321 | public void execute(DaemonCommand cmd) throws IOException 322 | { 323 | final DaemonStatus status; 324 | switch (cmd) { 325 | case start: 326 | daemonize(); 327 | break; 328 | case status: 329 | status = checkStatus(); 330 | System.exit(status.getExitCode()); 331 | break; 332 | case stop: 333 | status = stop(); 334 | System.exit(status.getExitCode()); 335 | break; 336 | } 337 | } 338 | } 339 | -------------------------------------------------------------------------------- /src/main/java/org/skife/gressil/DaemonCommand.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | public enum DaemonCommand 4 | { 5 | start, stop, status 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/org/skife/gressil/DaemonStatus.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | public enum DaemonStatus 4 | { 5 | // for status 6 | STATUS_RUNNING(0), STATUS_DEAD(1), STATUS_NOT_RUNNING(3), STATUS_UNKNOWN(4), 7 | 8 | // for stop 9 | STOP_NOT_RUNNING(7), STOP_GENERAL_ERROR(1), STOP_SUCCESS(0); 10 | 11 | private final int exitCode; 12 | 13 | public int getExitCode() { 14 | return exitCode; 15 | } 16 | 17 | DaemonStatus(final int code) {exitCode = code;} 18 | 19 | /* 20 | * 0 program is running or service is OK 21 | * 1 program is dead and /var/run pid file exists 22 | * 2 program is dead and /var/lock lock file exists 23 | * 3 program is not running 24 | * 4 program or service status is unknown 25 | * 5-99 reserved for future LSB use 26 | * 100-149 reserved for distribution use 27 | * 150-199 reserved for application use 28 | * 200-254 reserved 29 | */ 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/skife/gressil/JvmBasedArgvFinder.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | import java.io.File; 4 | import java.lang.management.ManagementFactory; 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | class JvmBasedArgvFinder implements ArgvFinder 10 | { 11 | private final List programArgs; 12 | 13 | public JvmBasedArgvFinder(List programArgs) 14 | { 15 | this.programArgs = programArgs; 16 | } 17 | 18 | public List getArgv() 19 | { 20 | // much cribbed from java.dzone.com/articles/programmatically-restart-java 21 | 22 | List ARGV = new ArrayList(); 23 | String java = System.getProperty("java.home") + "/bin/java"; 24 | ARGV.add(java); 25 | 26 | /** 27 | * This bit retrieves the jvm arguments (ie, -Dwaffles=good -Xmx1G0) 28 | * unfortunately, it seems to split it on whitespace, not ARGV style, so 29 | * we need to reconstruct the arguments. Luckily, all jvm arguments begin 30 | * with a - so we can basically start with a - and grab everything up to 31 | * the next - and know it is one argument. 32 | */ 33 | List raw_jvm_args = ManagementFactory.getRuntimeMXBean().getInputArguments(); 34 | List fixed_jvm_args = new ArrayList(); 35 | StringBuilder current = new StringBuilder(); 36 | for (String raw_arg : raw_jvm_args) { 37 | if (raw_arg.startsWith("-")) { 38 | if (current.length() > 0) { 39 | fixed_jvm_args.add(current.toString()); 40 | current = new StringBuilder(); 41 | } 42 | current.append(raw_arg); 43 | } 44 | else { 45 | // escape whitespace in an argument, dir with space in name for example 46 | current.append("\\ ").append(raw_arg); 47 | } 48 | } 49 | ARGV.addAll(fixed_jvm_args); 50 | 51 | /** 52 | * sun.java.command contains the main class and program arguments. Instead of the 53 | * main class it may have "-jar jarname" 54 | */ 55 | String whole_command_line = System.getProperty("sun.java.command"); 56 | 57 | // we need a better way to split this, one that respects spaces in an argument 58 | String[] java_sun_command = whole_command_line.split("\\s+"); 59 | 60 | 61 | 62 | String cmd = java_sun_command[0]; 63 | if (new File(cmd).exists()) { 64 | // this is a frighteningly weak test :-( 65 | // java -jar ./waffles.jar 66 | ARGV.add("-jar"); 67 | ARGV.add(new File(java_sun_command[0]).getPath()); 68 | } 69 | else { 70 | // java -cp waffles.jar hello.Main 71 | ARGV.add("-cp"); 72 | 73 | // escape whitespace in the classpath, ie dirs with spaces in names 74 | String raw_cp = System.getProperty("java.class.path").replaceAll(" ", "\\ "); 75 | ARGV.add(raw_cp); 76 | ARGV.add(cmd); 77 | } 78 | 79 | if (this.programArgs == null) { 80 | // we were not given program args, so we have to hope there were no spaces 81 | // in arguments (escaped spaces and quotes are lost from java.class.path) 82 | 83 | // append all but the first, which was the jar name or main class 84 | ARGV.addAll(Arrays.asList(java_sun_command).subList(1, java_sun_command.length)); 85 | } 86 | else { 87 | // we have the program args given, no need to infer them. Yea! 88 | ARGV.addAll(this.programArgs); 89 | } 90 | 91 | return ARGV; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/org/skife/gressil/LinuxArgvFinder.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.IOException; 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | class LinuxArgvFinder implements ArgvFinder 12 | { 13 | private final int pid; 14 | 15 | LinuxArgvFinder(int pid) 16 | { 17 | this.pid = pid; 18 | } 19 | 20 | public List getArgv() 21 | { 22 | final String cmdline; 23 | File procfs_file = new File("/proc/" + pid + "/cmdline"); 24 | try { 25 | cmdline = readFile(procfs_file); 26 | } 27 | catch (IOException e) { 28 | throw new IllegalStateException("Unable to access " + procfs_file.getAbsolutePath()); 29 | } 30 | return new ArrayList(Arrays.asList(cmdline.split("\0"))); 31 | } 32 | 33 | private static String readFile(File f) throws IOException 34 | { 35 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 36 | FileInputStream fin = new FileInputStream(f); 37 | try { 38 | int sz; 39 | byte[] buf = new byte[1024]; 40 | 41 | while ((sz = fin.read(buf)) >= 0) { 42 | baos.write(buf, 0, sz); 43 | } 44 | 45 | return baos.toString(); 46 | } 47 | finally { 48 | fin.close(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/skife/gressil/MacARGVFinder.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | import jnr.ffi.*; 4 | import jnr.ffi.Runtime; 5 | import jnr.ffi.byref.IntByReference; 6 | 7 | import java.io.ByteArrayOutputStream; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | class MacARGVFinder implements ArgvFinder 12 | { 13 | public List getArgv() 14 | { 15 | final int CTL_KERN = 1; 16 | final int KERN_PROCARGS2 = 49; 17 | int rs; 18 | 19 | jnr.ffi.Runtime runtime = Runtime.getSystemRuntime(); 20 | Pointer NULL = Pointer.wrap(runtime, 0L); 21 | 22 | SmallC sc = Library.loadLibrary("c", SmallC.class); 23 | 24 | final IntByReference oldlenp = new IntByReference(4); 25 | rs = sc.sysctl(new int[]{CTL_KERN, KERN_PROCARGS2, sc.getpid()}, 3, 26 | NULL, oldlenp, 27 | NULL, 0); 28 | if (rs != 0) { 29 | throw new IllegalStateException(sc.strerror(rs)); 30 | } 31 | 32 | final Pointer oldp = Memory.allocateDirect(runtime, oldlenp.getValue()); 33 | rs = sc.sysctl(new int[]{CTL_KERN, KERN_PROCARGS2, sc.getpid()}, 3, 34 | oldp, oldlenp, 35 | NULL, 0); 36 | if (rs != 0) { 37 | throw new IllegalStateException(sc.strerror(rs)); 38 | } 39 | 40 | class ProcessStuffReader 41 | { 42 | private int offset = 0; 43 | 44 | private final int argc; 45 | private final int size; 46 | private final List argv; 47 | 48 | ProcessStuffReader() 49 | { 50 | this.size = oldlenp.getValue(); 51 | argc = readInt(); 52 | readString(); // exec path? 53 | skipNulls(); 54 | List argv = new ArrayList(argc); 55 | for (int i = 0; i < argc; i++) { 56 | argv.add(readString()); 57 | } 58 | this.argv = argv; 59 | } 60 | 61 | private int readInt() 62 | { 63 | int i = oldp.getInt(offset); 64 | offset += 4; 65 | return i; 66 | } 67 | 68 | private void skipNulls() 69 | { 70 | while (offset < size && oldp.getByte(offset) == '\0') { 71 | offset++; 72 | } 73 | } 74 | 75 | private String readString() 76 | { 77 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); 78 | byte c; 79 | while ((c = oldp.getByte(offset++)) != '\0') { 80 | bout.write(c); 81 | } 82 | return new String(bout.toByteArray()); 83 | } 84 | 85 | public List getArgv() 86 | { 87 | return argv; 88 | } 89 | } 90 | 91 | return new ProcessStuffReader().getArgv(); 92 | } 93 | 94 | public static interface SmallC 95 | { 96 | // int sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen); 97 | int sysctl(int[] name, int namelen, 98 | Pointer oldp, IntByReference oldlenp, 99 | Pointer newp, int newlen); 100 | 101 | int getpid(); 102 | 103 | String strerror(int errno); 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/org/skife/gressil/Main.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | /** 7 | * Class for testing, will be main class for jar 8 | */ 9 | public class Main 10 | { 11 | public static void main(String[] args) throws IOException 12 | { 13 | if (args.length != 1) { 14 | System.err.println("java -jar gressil.jar start|stop|status"); 15 | System.exit(1); 16 | } 17 | new Daemon().withPidFile(new File("gressil.pid")) 18 | .execute(DaemonCommand.valueOf(args[0])); 19 | 20 | try 21 | { 22 | Thread.currentThread().join(); 23 | } 24 | catch (InterruptedException e) 25 | { 26 | System.exit(0); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/skife/gressil/MicroC.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | import jnr.ffi.Pointer; 4 | import jnr.ffi.annotations.In; 5 | import jnr.ffi.annotations.Out; 6 | import jnr.ffi.byref.IntByReference; 7 | 8 | public interface MicroC 9 | { 10 | int getpid(); 11 | int setsid(); 12 | String strerror(int errno); 13 | int kill(int pid, int signal); 14 | 15 | int posix_spawnp(@Out IntByReference pid, @In CharSequence path, 16 | @In Pointer fileActions, @In Pointer attr, 17 | @In CharSequence[] argv, @In CharSequence[] envp); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/skife/gressil/Status.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | public final class Status 4 | { 5 | private final boolean child; 6 | 7 | private final int childPid; 8 | 9 | private Status(boolean child, int pid) 10 | { 11 | this.child = child; 12 | childPid = pid; 13 | } 14 | 15 | public int getChildPid() 16 | { 17 | return childPid; 18 | } 19 | 20 | public boolean isChild() 21 | { 22 | return child; 23 | } 24 | 25 | public boolean isParent() 26 | { 27 | return !child; 28 | } 29 | 30 | 31 | @Override 32 | public boolean equals(Object o) 33 | { 34 | if (this == o) return true; 35 | if (o == null || getClass() != o.getClass()) return false; 36 | 37 | Status status = (Status) o; 38 | 39 | return child == status.child && childPid == status.childPid; 40 | 41 | } 42 | 43 | @Override 44 | public int hashCode() 45 | { 46 | int result = (child ? 1 : 0); 47 | result = 31 * result + childPid; 48 | return result; 49 | } 50 | 51 | static Status child(int pid) 52 | { 53 | return new Status(true, pid); 54 | } 55 | 56 | static Status parent(int pid) 57 | { 58 | return new Status(false, pid); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/org/skife/gressil/DaemonTest.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | import com.google.common.base.Charsets; 4 | import com.google.common.io.Files; 5 | import jnr.ffi.Library; 6 | 7 | import java.io.File; 8 | 9 | import static org.hamcrest.CoreMatchers.equalTo; 10 | import static org.hamcrest.CoreMatchers.is; 11 | import static org.junit.Assert.assertThat; 12 | 13 | public class DaemonTest 14 | { 15 | public static void main(String[] args) throws Exception 16 | { 17 | File out = new File("/tmp/gressil.out"); 18 | File err = new File("/tmp/gressil.err"); 19 | File pid = new File("/tmp/gressil.pid"); 20 | File extra = new File("/tmp/gressil.touchme"); 21 | File done = new File("/tmp/gressil.done"); 22 | out.delete(); 23 | err.delete(); 24 | pid.delete(); 25 | extra.delete(); 26 | done.delete(); 27 | 28 | if (args.length > 0) { 29 | for (String arg : args) { 30 | Files.touch(new File(arg)); 31 | } 32 | } 33 | 34 | Status status = new Daemon().withPidFile(pid) 35 | .withStdout(out) 36 | .withStderr(err) 37 | .withExtraMainArgs(extra.getAbsolutePath()) 38 | .forkish(); 39 | 40 | if (status.isParent()) { 41 | // parent 42 | while (!done.exists()) { 43 | Thread.sleep(10); 44 | } 45 | 46 | String out_msg = Files.readFirstLine(out, Charsets.US_ASCII); 47 | assertThat(out_msg, equalTo("out out")); 48 | 49 | String err_msg = Files.readFirstLine(err, Charsets.US_ASCII); 50 | assertThat(err_msg, equalTo("err err")); 51 | 52 | int child_pid = Integer.parseInt(Files.readFirstLine(pid, Charsets.US_ASCII)); 53 | assertThat(child_pid, equalTo(status.getChildPid())); 54 | 55 | assertThat(extra.exists(), is(true)); 56 | 57 | System.out.printf("child pid is %d\n", status.getChildPid()); 58 | Library.loadLibrary("c", MicroC.class).kill(status.getChildPid(), 15); 59 | 60 | out.delete(); 61 | err.delete(); 62 | pid.delete(); 63 | extra.delete(); 64 | 65 | System.out.println("PASSED"); 66 | } 67 | else { 68 | // child 69 | System.out.println("out out"); 70 | System.err.println("err err"); 71 | Files.touch(done); 72 | Thread.currentThread().join(); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/test/java/org/skife/gressil/MacARGVFinderTest.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil; 2 | 3 | import org.junit.Ignore; 4 | import org.junit.Test; 5 | 6 | public class MacARGVFinderTest 7 | { 8 | @Test 9 | @Ignore 10 | public void testFoo() throws Exception 11 | { 12 | JvmBasedArgvFinder jvm = new JvmBasedArgvFinder(null); 13 | MacARGVFinder mf = new MacARGVFinder(); 14 | 15 | System.out.println(mf.getArgv()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/org/skife/gressil/examples/ChattyDaemon.java: -------------------------------------------------------------------------------- 1 | package org.skife.gressil.examples; 2 | 3 | import org.skife.gressil.Daemon; 4 | 5 | import java.io.File; 6 | import java.io.IOException; 7 | import java.util.Arrays; 8 | import java.util.Date; 9 | 10 | import static org.skife.gressil.Daemon.remoteDebugOnPort; 11 | 12 | public class ChattyDaemon 13 | { 14 | public static void main(String[] args) throws IOException 15 | { 16 | new Daemon().withMainArgs(args) 17 | .withPidFile(new File("/tmp/chatty.pid")) 18 | .withStdout(new File("/tmp/chatty.out")) 19 | .withExtraMainArgs("hello", "world,", "how are you?") 20 | .withExtraJvmArgs(remoteDebugOnPort(5005)) 21 | .daemonize(); 22 | 23 | while (!Thread.currentThread().isInterrupted()) { 24 | System.out.println(new Date() + " " + Arrays.toString(args)); 25 | try { 26 | Thread.sleep(1000); 27 | } 28 | catch (InterruptedException e) { 29 | Thread.currentThread().interrupt(); 30 | } 31 | } 32 | } 33 | } 34 | --------------------------------------------------------------------------------