├── .gitignore ├── .travis.yml ├── COPYING ├── Makefile ├── README.md ├── bin └── huptime ├── packagers ├── deb │ └── DEBIAN │ │ └── control └── rpm │ └── huptime.spec ├── py.test ├── src ├── fdinfo.c ├── fdinfo.h ├── fdtable.c ├── fdtable.h ├── funcs.h ├── impl.c ├── impl.h ├── stubs.cc ├── stubs.h ├── stubs.map ├── utils.c └── utils.h └── test ├── __init__.py ├── client.py ├── harness.py ├── modes.py ├── proxy.py ├── servers.py ├── test_bindings.py └── test_meta.py /.gitignore: -------------------------------------------------------------------------------- 1 | lib/* 2 | src/*.o 3 | test/*.pyc 4 | test/__pycache__ 5 | rpmbuild/ 6 | debbuild/ 7 | *.deb 8 | *.rpm 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | compiler: 4 | - clang 5 | - gcc 6 | 7 | install: make build && sudo make install 8 | 9 | script: make test 10 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | DESCRIPTION := $(shell git describe --tags --match 'v*' | cut -d'v' -f2-) 4 | VERSION ?= $(shell echo $(DESCRIPTION) | cut -d'-' -f1) 5 | RELEASE ?= $(shell echo $(DESCRIPTION) | cut -d'-' -f2- -s | tr '-' '.') 6 | 7 | ifeq ($(VERSION),) 8 | $(error No VERSION available, please set manually.) 9 | endif 10 | ifeq ($(RELEASE),) 11 | RELEASE := 1 12 | endif 13 | 14 | SOFILE := lib/huptime/huptime.so 15 | INCLUDES := $(wildcard src/*.h) 16 | C_SOURCES := $(wildcard src/*.c) 17 | CXX_SOURCES := $(wildcard src/*.cc) 18 | OBJECTS := $(patsubst %.c,%.o,$(C_SOURCES)) $(patsubst %.cc,%.o,$(CXX_SOURCES)) 19 | DESTDIR ?= /usr/local 20 | ARCH_TARGET ?= $(shell uname -m) 21 | 22 | RPMBUILD := rpmbuild 23 | DEBBUILD := debbuild 24 | 25 | INSTALL_DIR := install -m 0755 -d 26 | INSTALL_BIN := install -m 0755 27 | 28 | ifeq ($(ARCH_TARGET),i386) 29 | ARCH_TARGET = x86_32 30 | endif 31 | ifeq ($(ARCH_TARGET),i486) 32 | ARCH_TARGET = x86_32 33 | endif 34 | ifeq ($(ARCH_TARGET),i586) 35 | ARCH_TARGET = x86_32 36 | endif 37 | ifeq ($(ARCH_TARGET),i686) 38 | ARCH_TARGET = x86_32 39 | endif 40 | ifeq ($(ARCH_TARGET),amd64) 41 | ARCH_TARGET = x86_64 42 | endif 43 | ifeq ($(ARCH_TARGET),x86_32) 44 | RPM_ARCH_OPT ?= --target=i386 45 | DEB_ARCH_OPT ?= i386 46 | else 47 | ifeq ($(ARCH_TARGET),x86_64) 48 | RPM_ARCH_OPT ?= --target=x86_64 49 | DEB_ARCH_OPT ?= amd64 50 | else 51 | $(error Unknown architecture $(ARCH_TARGET)?) 52 | endif 53 | endif 54 | 55 | CC := gcc 56 | CXX := g++ 57 | OFFSET_FLAGS ?= -D_LARGEFILE64_SOURCE=1 -D_FILE_OFFSET_BITS=64 58 | ifeq ($(ARCH_TARGET),x86_64) 59 | ARCH_FLAGS ?= -m64 -DARCH64BIT 60 | else 61 | ARCH_FLAGS ?= -m32 -DARCH32BIT 62 | endif 63 | CFLAGS ?= -Wall -fPIC -std=gnu99 -D_GNU_SOURCE $(OFFSET_FLAGS) $(ARCH_FLAGS) 64 | CXXFLAGS ?= -Wall -fPIC -fno-exceptions -fno-rtti -D_GNU_SOURCE -Wno-unused-function $(OFFSET_FLAGS) $(ARCH_FLAGS) 65 | LDFLAGS ?= -nostdlib -lc -ldl -lpthread 66 | 67 | default: test 68 | .PHONY: default 69 | 70 | test: build 71 | @./py.test -vv 72 | .PHONY: test 73 | 74 | debug: build 75 | @./py.test --capture=no -vv 76 | .PHONY: debug 77 | 78 | build: $(SOFILE) 79 | .PHONY: build 80 | 81 | $(SOFILE): $(OBJECTS) src/stubs.map 82 | @mkdir -p $(shell dirname $(SOFILE)) 83 | @$(CC) $(CFLAGS) -o $@ $(filter %.o,$^) $(LDFLAGS) \ 84 | -shared -Wl,--version-script,src/stubs.map \ 85 | -fvisibility=hidden 86 | 87 | %.o: %.c $(INCLUDES) 88 | @$(CC) -o $@ $(CFLAGS) -c $< 89 | 90 | %.o: %.cc $(INCLUDES) 91 | @$(CXX) -o $@ $(CXXFLAGS) -c $< 92 | 93 | install: build 94 | @mkdir -p $(DESTDIR)/bin 95 | @mkdir -p $(DESTDIR)/lib/huptime 96 | @$(INSTALL_BIN) bin/huptime $(DESTDIR)/bin/huptime 97 | @$(INSTALL_BIN) $(SOFILE) $(DESTDIR)/lib/huptime/$(shell basename $(SOFILE)) 98 | 99 | $(DEBBUILD): 100 | @rm -rf $(DEBBUILD) 101 | @$(INSTALL_DIR) $(DEBBUILD) 102 | .PHONY: $(DEBBUILD) 103 | 104 | $(RPMBUILD): 105 | @rm -rf $(RPMBUILD) 106 | @$(INSTALL_DIR) $(RPMBUILD) 107 | @$(INSTALL_DIR) $(RPMBUILD)/SRPMS 108 | @$(INSTALL_DIR) $(RPMBUILD)/BUILD 109 | @$(INSTALL_DIR) $(RPMBUILD)/BUILDROOT 110 | @$(INSTALL_DIR) $(RPMBUILD)/SPECS 111 | @$(INSTALL_DIR) $(RPMBUILD)/RPMS/$(ARCH_TARGET) 112 | @$(INSTALL_DIR) $(RPMBUILD)/SOURCES 113 | .PHONY: $(RPMBUILD) 114 | 115 | deb: $(DEBBUILD) 116 | @$(MAKE) install DESTDIR=$(DEBBUILD)/usr 117 | @sed -i -e 's/@(VERSION)/$(VERSION)-$(RELEASE)/' \ 118 | $(DEBBUILD)/usr/bin/huptime 119 | @rsync -rav packagers/deb/DEBIAN $(DEBBUILD) 120 | @sed -i -e 's/@(VERSION)/$(VERSION)/' $(DEBBUILD)/DEBIAN/control 121 | @sed -i -e 's/@(RELEASE)/$(RELEASE)/' $(DEBBUILD)/DEBIAN/control 122 | @sed -i -e 's/@(ARCH)/$(DEB_ARCH_OPT)/' $(DEBBUILD)/DEBIAN/control 123 | @fakeroot dpkg -b $(DEBBUILD) . 124 | .PHONY: deb 125 | 126 | rpm: $(RPMBUILD) 127 | @$(MAKE) install DESTDIR=$(RPMBUILD)/BUILDROOT/usr 128 | @sed -i -e 's/@(VERSION)/$(VERSION)-$(RELEASE)/' \ 129 | $(RPMBUILD)/BUILDROOT/usr/bin/huptime 130 | @rpmbuild -bb $(RPM_ARCH_OPT) \ 131 | --buildroot $(CURDIR)/$(RPMBUILD)/BUILDROOT \ 132 | --define="%_topdir $(CURDIR)/$(RPMBUILD)" \ 133 | --define="%version $(VERSION)" \ 134 | --define="%release $(RELEASE)" \ 135 | packagers/rpm/huptime.spec 136 | @mv $(RPMBUILD)/RPMS/$(ARCH_TARGET)/*.rpm . 137 | .PHONY: rpm 138 | 139 | packages: deb rpm 140 | .PHONY: packages 141 | 142 | clean: 143 | @rm -rf $(DEBBUILD) $(RPMBUILD) 144 | @rm -rf *.deb *.rpm 145 | @rm -f $(SOFILE) $(OBJECTS) 146 | @find . -name \*.pyc -exec rm -rf {} \; 147 | @rm -rf test/__pycache__ 148 | .PHONY: clean 149 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | High uptime 2 | =========== 3 | 4 | [![Build Status](https://travis-ci.org/amscanne/huptime.png)](http://travis-ci.org/amscanne/huptime) 5 | 6 | 7 | Huptime is a tool for achieving zero downtime restarts without the need to 8 | modify your program in any way. 9 | 10 | Although many applications support reloading configurations while running, a 11 | zero downtime restart allows for upgrading the application code without 12 | rejecting any clients. 13 | 14 | Basic Example 15 | ------------- 16 | 17 | In a terminal, run: 18 | 19 | huptime --exec python -m SimpleHTTPServer & 20 | PID=$! 21 | 22 | Then, in a second terminal: 23 | 24 | while true; do curl http://localhost:8000 2>/dev/null || echo "fail"; done 25 | 26 | Finally, in a third terminal (or back in the first): 27 | 28 | kill -HUP $PID 29 | 30 | You should see no "fail" output on the second terminal. 31 | 32 | With this reload, the complete code for SimpleHTTPServer is reloaded 33 | (potentially with changes), but at no time are connections denied or dropped. 34 | When the new version is up and running again (i.e. it binds the socket and 35 | calls accept), then pending connections will be processed. 36 | 37 | Why? 38 | ---- 39 | 40 | With continuous deployment, software can be updated dozens, hundreds or even 41 | thousands of times per day. It is critical that service is not interrupted during 42 | upgrades. 43 | 44 | In an ideal world, all applications would support a mechanism for doing zero 45 | downtime restarts. The reality is that many standard frameworks make this 46 | difficult to do from the top down. It's not practical to plumb this 47 | functionality through every layer, particularly for applications over which you 48 | have no control. 49 | 50 | Compound this with the fact that many applications consist of many different 51 | small components (written using different languages and frameworks), and you've 52 | got yourself a headache. 53 | 54 | Because of this complexity, one of the first things people have to do is implement 55 | a custom load balancing tier and a complex upgrade process. Although this is 56 | important at a certain scale, it shouldn't be that hard for simple services. 57 | It's crazy to add a whole new tier when the problem can be solved in a much 58 | simpler way! 59 | 60 | Huptime attempts to make it very simple to achieve these restarts for simple, 61 | unmodified applications. 62 | 63 | How do I install it? 64 | -------------------- 65 | 66 | Building this package gnerally requires: 67 | * make 68 | * gcc and g++ 69 | * python 70 | * rpmbuild (optional) and dpkg (optional) 71 | 72 | Clone the repo: 73 | 74 | git clone http://github.com/amscanne/huptime 75 | 76 | Install it the old-fashioned way: 77 | 78 | cd huptime && sudo make install 79 | 80 | Want Ubuntu & Debian packages? 81 | 82 | cd huptime && make deb && dpkg -i huptime*.deb 83 | 84 | How about CentOS or RedHat? 85 | 86 | cd huptime && make rpm && rpm -i huptime*.rpm 87 | 88 | How do I use it? 89 | ---------------- 90 | 91 | You simply need to run services via huptime. 92 | 93 | For example: 94 | 95 | # Start the service. 96 | huptime /usr/bin/myservice & 97 | 98 | # Zero downtime restart. 99 | killall -HUP myservice 100 | 101 | # Or, if you prefer... 102 | huptime --restart /usr/bin/myservice 103 | 104 | If there is a pidfile, it can be reset on restart: 105 | 106 | # Start the service. 107 | huptime --unlink /var/run/myservice.pid /usr/bin/myservice & 108 | 109 | # Zero downtime restarts. 110 | killall -HUP myservice 111 | 112 | # Again, if you prefer... 113 | huptime --restart /usr/bin/myservice 114 | 115 | Or, if you need exec (for example, to run under upstart): 116 | 117 | # Start the service and get the PID. 118 | huptime --exec /usr/bin/myservice & 119 | PID=$! 120 | 121 | # Zero downtime restart (same PID). 122 | kill -HUP $PID 123 | 124 | # Again, as always... 125 | huptime --restart /usr/bin/myservice 126 | 127 | What does it support? 128 | --------------------- 129 | 130 | Huptime should [+] handle the following normal things: 131 | 132 | * Daemonization & pid files 133 | * Process pools 134 | * Multiple server sockets 135 | * Event-based and thread-based servers 136 | * Integration with supervisors (just use exec!) 137 | 138 | In terms of languages and frameworks, huptime should support nearly all 139 | programs that are *dynamically linked* against a *modern libc*. 140 | 141 | Most modern dynamic languages (python, ruby, node, etc.) fall into this 142 | category. Most C/C++ programs also fall into this category. A unique exception 143 | is *go*, which invokes system calls directly and uses only static linking. 144 | (For the record, I am a big fan of this approach. However, both have their 145 | merits). 146 | 147 | [+] Should. YMMV. 148 | 149 | What else does it do? 150 | --------------------- 151 | 152 | * Transparent multi-binding (running the same service multiple times) 153 | 154 | If you are running Linux 3.9+, then you can also easily enable pools of 155 | processes by starting your services with the *--multi* option. Again, this does 156 | not require any modification on your application. 157 | 158 | For example: 159 | 160 | # Start the service (4 workers). 161 | huptime --multi=4 /usr/bin/myservice & 162 | 163 | # Zero downtime restart of all. 164 | killall -HUP myservice 165 | 166 | # Or, if you prefer... 167 | huptime --restart /usr/bin/myservice 168 | 169 | Want to manage the number of running scripts yourself? 170 | 171 | pids=""; 172 | 173 | reload() { 174 | for pid in $pids; do 175 | kill -HUP $pid; 176 | done 177 | } 178 | 179 | stop() { 180 | for pid in $pids; do 181 | kill -TERM $pid; 182 | done 183 | } 184 | 185 | trap reload SIGHUP; 186 | trap stop SIGTERM; 187 | trap stop SIGINT; 188 | 189 | count="0"; 190 | while [ "$count" -lt "$N" ]; do 191 | huptime --multi=1 /usr/bin/myservice & 192 | pids="$pids $!"; 193 | count=$(($count + 1)); 194 | done 195 | 196 | for pid in $pids; do 197 | wait $pid; 198 | done 199 | 200 | * Transparent restart on exit 201 | 202 | If you've got a stubborn program and you want to restart it automatically when 203 | it fails, you should really fix your program. Barring that, you should use a 204 | supervisor like upstart. Barring *that* (you don't care about the sensible 205 | features that a supervisor provides and want zero downtime restarts), you can 206 | use huptime. 207 | 208 | To enable this option, simply specify *--revive* on the huptime command line. 209 | 210 | For example: 211 | 212 | # Start a zero downtime netcat. 213 | huptime --revive nc -l 9000 < message.txt & 214 | 215 | # Clients will always find a server... 216 | nc localhost 9000 217 | 218 | How does it work? 219 | ----------------- 220 | 221 | Huptime installs a signal handler for `SIGHUP`. 222 | 223 | It tracks open file descriptors by intercepting calls to `bind` and `accept` 224 | (among other things). When the program receives a `SIGHUP`, it will 225 | intelligently `exec` a new copy of the program *without* closing any bound 226 | sockets and without requiring any changes to the program. 227 | 228 | Note that this is not simply a reload but rather a new version of the 229 | application with config changes and code changes (as both now appear on disk). 230 | 231 | When the new copy of the program tries to bind the same socket, huptime will 232 | silently replace it with the still-open socket from the previous version. 233 | 234 | There are two fundamental modes of operation: 235 | 236 | * fork (default) 237 | 238 | If you use fork, then when the process receives a `SIGHUP`, then it will `fork` 239 | and `exec` a new copy of the application. This results in less downtime, as new 240 | requests can start being served immediately, while old requests are still being 241 | finished by the original program. 242 | 243 | This may not integrate cleanly with supervisor processes like upstart however, 244 | which depend on the PID of the application staying constant. 245 | 246 | This may also present issues for some applications that check pidfiles or 247 | contain internal mechanisms for preventing two copies of themselves from 248 | running. Huptime goes to some effort to prevent conflict (allowing for unlink 249 | prior to executing the child), but it may still arise. 250 | 251 | * exec 252 | 253 | If you use exec, then when a process receives a `SIGHUP`, then it will begin 254 | queueing requests to the bound socket (in the kernel) and wait until all 255 | outstanding requests are finished. Only when existing requests are finished 256 | will the program restart. 257 | 258 | This may not work properly if requests are not bounded in how long they will 259 | take. This may also lead to high response times for some clients during the 260 | restart. However, this approach will play well with supervisors. 261 | 262 | For example, if you are using upstart, you can do the restart as: 263 | 264 | upstart reload service 265 | 266 | Limitations 267 | ----------- 268 | 269 | Although the majority of programs will work, I'm sure that *all* will not. 270 | 271 | The exit is not done through the normal application path on restart. Although 272 | all file descriptors are closed, there may be application-level resources (or 273 | some system resources) that are not cleaned up as expected and may cause 274 | problems. 275 | 276 | The command line and environment cannot be changed between restarts. You can 277 | easily work around this issue by putting all configuration inside a file that 278 | is read on start-up (i.e. `myservice --config-file=/etc/myservice.cfg`). 279 | 280 | What's up with the name? 281 | ------------------------ 282 | 283 | It's clever! Services are often reloaded using `SIGHUP`. The point of this tool 284 | is to maximize uptime by enabling zero downtime restarts via `SIGHUP`. It's 285 | your high availabilibuddy! 286 | -------------------------------------------------------------------------------- /bin/huptime: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # Copyright 2013 Adin Scannell , all rights reserved. 4 | # 5 | # This file is part of Huptime. 6 | # 7 | # Huptime is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # Huptime is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with Huptime. If not, see . 19 | # 20 | 21 | import os 22 | import sys 23 | import signal 24 | import re 25 | import copy 26 | import time 27 | import traceback 28 | import ctypes 29 | 30 | REALPATH = os.path.realpath(sys.argv[0]) 31 | BINDIR = os.path.dirname(REALPATH) 32 | BASEDIR = os.path.dirname(BINDIR) 33 | LIBDIR = os.path.join(BASEDIR, "lib", "huptime") 34 | SOFILE = os.path.join(LIBDIR, "huptime.so") 35 | 36 | # The version (injected by the build). 37 | VERSION = "@(VERSION)" 38 | 39 | # Defaults. 40 | STATUS = False 41 | RESTART = False 42 | STOP = False 43 | 44 | HUPTIME_MODE = "fork" 45 | HUPTIME_MULTI = False 46 | HUPTIME_REVIVE = False 47 | HUPTIME_WAIT = False 48 | HUPTIME_UNLINK = "" 49 | HUPTIME_DEBUG = False 50 | 51 | MULTI_COUNT = 1 52 | MULTI_PIDS = [] 53 | 54 | STOP_TIMEOUT = 10.0 55 | 56 | def usage(): 57 | print "usage: huptime [options] [--] " 58 | print " or huptime [options] [--] --status " 59 | print " or huptime [options] [--] --restart " 60 | print " or huptime [options] [--] --stop " 61 | print " or huptime --help" 62 | print 63 | print "where options are:" 64 | print 65 | print " --version Print the version and exit." 66 | print " --fork Run using fork mode (exclusive of --exec)." 67 | print " --exec Run using exec mode (exclusive of --fork)." 68 | print " --revive Restart the process on exit." 69 | print " --wait Wait for child processes to finish." 70 | print " --multi= Run N processes (and wait for exit)." 71 | print " This will enable SO_REUSEPORT (needs Linux 3.9+)." 72 | print " --unlink= Unlink the given file on restart." 73 | print " This is useful for pid files." 74 | print " --debug Print debug output to stderr." 75 | print " --timeout= Timeout between TERM and KILL for --stop." 76 | print " The default is %2.2f seconds." % STOP_TIMEOUT 77 | print 78 | print "Huptime is distributed in the hope that it will be useful," 79 | print "but WITHOUT ANY WARRANTY; without even the implied warranty of" 80 | print "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" 81 | print "GNU General Public License for more details." 82 | 83 | def debug(msg): 84 | if HUPTIME_DEBUG: 85 | print "huptime %d: %s" % (os.getpid(), msg) 86 | 87 | # Parse all options. 88 | ARGS = sys.argv[1:] 89 | 90 | while len(ARGS) > 0: 91 | 92 | arg = ARGS[0] 93 | 94 | if arg.startswith("--"): 95 | if "=" in arg: 96 | arg, value = arg[2:].split("=", 1) 97 | else: 98 | arg, value = arg[2:], None 99 | 100 | if not arg: 101 | break 102 | elif arg == "exec" and not value: 103 | HUPTIME_MODE = "exec" 104 | elif arg == "fork" and not value: 105 | HUPTIME_MODE = "fork" 106 | elif arg == "multi" and value: 107 | HUPTIME_MULTI = True 108 | MULTI_COUNT = value 109 | elif arg == "timeout" and value: 110 | STOP_TIMEOUT = value 111 | elif arg == "revive" and not value: 112 | HUPTIME_REVIVE = True 113 | elif arg == "wait" and not value: 114 | HUPTIME_WAIT = True 115 | elif arg == "debug" and not value: 116 | HUPTIME_DEBUG = True 117 | elif arg == "unlink" and value: 118 | HUPTIME_UNLINK = value 119 | elif arg == "help" and not value: 120 | usage() 121 | sys.exit(0) 122 | elif arg == "status" and not value: 123 | STATUS = True 124 | elif arg == "restart" and not value: 125 | RESTART = True 126 | elif arg == "stop" and not value: 127 | STOP = True 128 | elif arg == "version" and not value: 129 | print VERSION 130 | sys.exit(0) 131 | else: 132 | usage() 133 | sys.exit(1) 134 | else: 135 | # Non-option. 136 | break 137 | 138 | # Move to the next option. 139 | ARGS.pop(0) 140 | 141 | if len(ARGS) == 0: 142 | usage() 143 | sys.exit(0) 144 | 145 | try: 146 | MULTI_COUNT = int(MULTI_COUNT) 147 | if MULTI_COUNT <= 0: 148 | raise ValueError() 149 | except ValueError: 150 | print "Invalid value for --multi (should be positive integer)." 151 | sys.exit(1) 152 | 153 | try: 154 | STOP_TIMEOUT = float(STOP_TIMEOUT) 155 | if STOP_TIMEOUT < 0.0: 156 | raise ValueError() 157 | except ValueError: 158 | print "Invalid value for --timeout (should be non-negative)." 159 | sys.exit(1) 160 | 161 | if STATUS or RESTART or STOP: 162 | 163 | # Check that the user hasn't passed any 164 | # options which we could consider invalid. 165 | if len([x for x in (STATUS, RESTART, STOP) if x]) > 1: 166 | print "Invalid options: can't specify multi of --status, --restart and --stop." 167 | sys.exit(1) 168 | 169 | # Go through /proc/*/cmdline and find matches. 170 | # NOTE: Some interpretors may fudge the command 171 | # line, so we may it against argv[0:] or argv[1:]. 172 | exact_matches = [] 173 | inter_matches = [] 174 | 175 | for pid in os.listdir("/proc"): 176 | try: 177 | pid = int(pid) 178 | if pid == os.getpid(): 179 | continue 180 | 181 | cmd = open("/proc/%d/cmdline" % pid, 'r').read().split("\0") 182 | 183 | # An exact match. 184 | if len(cmd) >= len(ARGS) and cmd[:len(ARGS)] == ARGS: 185 | exact_matches.append(pid) 186 | 187 | # Interpreter match. 188 | elif (len(cmd) >= 1+len(ARGS) and cmd[1:1+len(ARGS)] == ARGS) or \ 189 | (len(cmd) >= 2+len(ARGS) and cmd[2:2+len(ARGS)] == ARGS): 190 | inter_matches.append(pid) 191 | except KeyboardInterrupt: 192 | sys.exit(1) 193 | except: 194 | continue 195 | 196 | if exact_matches: 197 | debug("Found exact processes: %s" % exact_matches) 198 | if inter_matches: 199 | debug("Found interpreter processes: %s" % inter_matches) 200 | 201 | # Kill the preferred process group in order 202 | # to do the restart. We grab the pids to block 203 | # until the restart is complete below. 204 | active_pids = [] 205 | if exact_matches: 206 | active_pids = exact_matches 207 | elif inter_matches: 208 | active_pids = inter_matches 209 | else: 210 | print "No process found?" 211 | sys.exit(1) 212 | 213 | for pid in active_pids: 214 | try: 215 | if STATUS: 216 | print pid 217 | elif RESTART: 218 | debug("Restarting PID %d..." % pid) 219 | os.kill(pid, signal.SIGHUP) 220 | elif STOP: 221 | debug("Killing PID %d (TERM)..." % pid) 222 | os.kill(pid, signal.SIGTERM) 223 | except OSError: 224 | continue 225 | 226 | # Nothing more to do. 227 | if STATUS: 228 | sys.exit(0) 229 | 230 | # Block until the SIGHUP signal has been 231 | # unmasked from this PID, or it no longer 232 | # exists. When the SIGHUP signal is unmasked, 233 | # know that the restart has been processed. 234 | for pid in active_pids: 235 | start_time = time.time() 236 | while True: 237 | try: 238 | data = open("/proc/%d/status" % pid, 'r').read().split("\n") 239 | if RESTART: 240 | is_restarted = False 241 | for line in data: 242 | m = re.match("SigBlk:\s*([0-9a-f]+)", line) 243 | if m: 244 | # SIGHUP happens to be 1. So if 245 | # this is not blocked, the number 246 | # will be even. Once the signal is 247 | # not blocked, we know it's done. 248 | if int(m.group(1), 16) % 2 == 0: 249 | is_restarted = True 250 | break 251 | if is_restarted: 252 | break 253 | elif STOP: 254 | now = time.time() 255 | if now - start_time > STOP_TIMEOUT: 256 | debug("Killing PID %d (KILL)..." % pid) 257 | os.kill(pid, signal.SIGKILL) 258 | start_time = now 259 | except KeyboardInterrupt: 260 | sys.exit(1) 261 | except: 262 | break 263 | debug("Restart complete for PID %d." % pid) 264 | 265 | else: 266 | debug("Mode is %s." % HUPTIME_MODE) 267 | debug("Unlink is %s." % HUPTIME_UNLINK) 268 | debug("Multi is %s." % HUPTIME_MULTI) 269 | debug("Revive is %s." % HUPTIME_REVIVE) 270 | debug("Wait is %s." % HUPTIME_WAIT) 271 | 272 | ENV = copy.copy(os.environ) 273 | ENV["LD_PRELOAD"] = SOFILE 274 | ENV["HUPTIME_DEBUG"] = str(HUPTIME_DEBUG).lower() 275 | ENV["HUPTIME_MODE"] = HUPTIME_MODE 276 | ENV["HUPTIME_UNLINK"] = HUPTIME_UNLINK 277 | ENV["HUPTIME_MULTI"] = str(HUPTIME_MULTI).lower() 278 | ENV["HUPTIME_REVIVE"] = str(HUPTIME_REVIVE).lower() 279 | ENV["HUPTIME_WAIT"] = str(HUPTIME_WAIT).lower() 280 | 281 | def do_exec(): 282 | try: 283 | os.execvpe(ARGS[0], ARGS, ENV) 284 | except Exception as e: 285 | sys.stderr.write("huptime: %s\n" % str(e)) 286 | if HUPTIME_DEBUG: 287 | traceback.print_exc() 288 | sys.exit(1) 289 | 290 | if MULTI_COUNT == 1: 291 | # Execute our new process. 292 | do_exec() 293 | 294 | else: 295 | # Execute many processes. 296 | # NOTE: In this case, to ensure that 297 | # this can cleanly be used from supervisors 298 | # and init scripts, we wait for the children 299 | # to complete. 300 | child_pids = [] 301 | for _ in range(MULTI_COUNT): 302 | parent_pid = os.getpid() 303 | pid = os.fork() 304 | if pid == 0: 305 | # We setup a safe procedure here to ensure that the 306 | # child will receive a SIGTERM when the parent exits. 307 | libc = ctypes.CDLL("libc.so.6") 308 | if libc: 309 | # Setup the signal for the parent dying. 310 | libc.prctl(1, signal.SIGTERM) 311 | 312 | # Check for a race condition. It's possible 313 | # that the parent died between the fork() and 314 | # the prtctl() above; we need to handle that. 315 | if os.getppid() != parent_pid: 316 | sys.exit(1) 317 | 318 | do_exec() 319 | else: 320 | child_pids.append(pid) 321 | 322 | for pid in child_pids: 323 | os.waitpid(pid, 0) 324 | -------------------------------------------------------------------------------- /packagers/deb/DEBIAN/control: -------------------------------------------------------------------------------- 1 | Package: huptime 2 | Section: extra 3 | Version: @(VERSION)-@(RELEASE) 4 | Architecture: @(ARCH) 5 | Maintainer: Adin Scannell 6 | Description: Utility for zero downtime restart of unmodified applications. 7 | -------------------------------------------------------------------------------- /packagers/rpm/huptime.spec: -------------------------------------------------------------------------------- 1 | Name: huptime 2 | Summary: Utility for zero downtime restart 3 | Version: %{version} 4 | Release: %{release} 5 | Group: System 6 | License: Copyright 2013 Adin Scannell 7 | URL: http://github.com/amscanne/huptime 8 | Packager: Adin Scannell 9 | BuildRoot: %{_tmppath}/%{name}.%{version}-buildroot 10 | AutoReq: no 11 | AutoProv: no 12 | 13 | %global _binary_filedigest_algorithm 1 14 | %define __os_install_post %{nil} 15 | 16 | %description 17 | Utility for zero downtime restart of unmodified applications. 18 | 19 | %install 20 | true 21 | 22 | %files 23 | /usr/bin/huptime 24 | /usr/lib/huptime/huptime.so 25 | 26 | %changelog 27 | * Sat Oct 26 2013 Adin Scannell 28 | - Initial creation of package. 29 | -------------------------------------------------------------------------------- /src/fdinfo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * fdinfo.c 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | #include "fdinfo.h" 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | /* Total active bound FDs. */ 30 | int total_bound = 0; 31 | 32 | /* Total active tracked FDs. */ 33 | int total_tracked = 0; 34 | 35 | /* Total saved FDs. */ 36 | int total_saved = 0; 37 | 38 | /* Total initial FDs. */ 39 | int total_initial = 0; 40 | 41 | /* Total dummy FDs. */ 42 | int total_dummy = 0; 43 | 44 | /* Total epoll FDs. */ 45 | int total_epoll = 0; 46 | 47 | #define exactly(fn, fd, buf, bytes) \ 48 | do { \ 49 | for( int _n = 0; _n != bytes; ) \ 50 | { \ 51 | int _t = fn(fd, buf, bytes-_n); \ 52 | if( _t < 0 && \ 53 | (errno == EINTR || \ 54 | errno == EAGAIN) ) \ 55 | { \ 56 | continue; \ 57 | } \ 58 | if( _t <= 0 ) \ 59 | { \ 60 | return -1; \ 61 | } \ 62 | _n += _t; \ 63 | } \ 64 | } while(0) 65 | 66 | int 67 | info_decode(int pipe, int *fd, fdinfo_t **info) 68 | { 69 | fdtype_t type; 70 | 71 | /* Decode the FD. */ 72 | exactly(read, pipe, fd, sizeof(int)); 73 | 74 | /* Decode the type. */ 75 | exactly(read, pipe, &type, sizeof(fdtype_t)); 76 | 77 | /* Allocate. */ 78 | *info = alloc_info(type); 79 | 80 | int listened = 0; 81 | 82 | switch( type ) 83 | { 84 | case BOUND: 85 | /* Read whether it was listened or not. */ 86 | exactly(read, pipe, &listened, sizeof(int)); 87 | (*info)->bound.real_listened = listened; 88 | (*info)->bound.stub_listened = 0; 89 | (*info)->bound.is_ghost = 1; 90 | 91 | /* Read the bound address. */ 92 | exactly(read, pipe, &(*info)->bound.addrlen, sizeof(socklen_t)); 93 | if( (*info)->bound.addrlen > 0 ) 94 | { 95 | (*info)->bound.addr = malloc((*info)->bound.addrlen); 96 | exactly(read, pipe, (*info)->bound.addr, (*info)->bound.addrlen); 97 | } 98 | break; 99 | 100 | case SAVED: 101 | /* Read the original FD. */ 102 | exactly(read, pipe, 103 | &(*info)->saved.fd, 104 | sizeof((*info)->saved.fd)); 105 | 106 | /* Read the original offset. */ 107 | exactly(read, pipe, 108 | &(*info)->saved.offset, 109 | sizeof((*info)->saved.offset)); 110 | break; 111 | 112 | case TRACKED: 113 | case DUMMY: 114 | case EPOLL: 115 | /* Should never happen. */ 116 | break; 117 | } 118 | 119 | return 0; 120 | } 121 | 122 | int 123 | info_encode(int pipe, int fd, fdinfo_t* info) 124 | { 125 | /* Encode the FD. */ 126 | exactly(write, pipe, &fd, sizeof(int)); 127 | 128 | /* Encode the type. */ 129 | exactly(write, pipe, &info->type, sizeof(fdtype_t)); 130 | 131 | int listened = 0; 132 | 133 | switch( info->type ) 134 | { 135 | case BOUND: 136 | listened = info->bound.real_listened; 137 | 138 | /* Write whether it was listened or not. */ 139 | exactly(write, pipe, &listened, sizeof(int)); 140 | 141 | /* Write the bound address. */ 142 | exactly(write, pipe, &info->bound.addrlen, sizeof(socklen_t)); 143 | if( info->bound.addrlen > 0 ) 144 | { 145 | exactly(write, pipe, info->bound.addr, info->bound.addrlen); 146 | } 147 | break; 148 | 149 | case SAVED: 150 | /* Write the original FD. */ 151 | exactly(write, pipe, 152 | &info->saved.fd, 153 | sizeof(info->saved.fd)); 154 | 155 | /* Write the original offset. */ 156 | exactly(write, pipe, 157 | &info->saved.offset, 158 | sizeof(info->saved.offset)); 159 | break; 160 | 161 | case TRACKED: 162 | case DUMMY: 163 | case EPOLL: 164 | /* Should never happen. */ 165 | break; 166 | } 167 | 168 | return 0; 169 | } 170 | -------------------------------------------------------------------------------- /src/fdinfo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * fdinfo.h 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | #ifndef HUPTIME_FDINFO_H 23 | #define HUPTIME_FDINFO_H 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | typedef enum 31 | { 32 | /* BOUND FDs are the sockets that have been 33 | * bound. These are the thing of most interest, 34 | * since we will ensure these are not closed and 35 | * are transparently passed between copies of the 36 | * application. */ 37 | BOUND = 1, 38 | 39 | /* TRACKED FDs are descriptors that have been 40 | * returned from BOUND FDs. Essentially we must 41 | * wait until all TRACKED FDs have been closed in 42 | * the application before we can cleanly exit. */ 43 | TRACKED = 2, 44 | 45 | /* SAVED FDs are descriptors that we have saved 46 | * from startup. Because the program may go through 47 | * and close some of the file descriptors it had open 48 | * at start-up, we stuff them somewhere so that we can 49 | * recreate the environment as accurately as possible. 50 | * This may lead to some problems with open terminal 51 | * FDs, etc. but we'll see what happens. */ 52 | SAVED = 3, 53 | 54 | /* DUMMY FDs hold the dummy sockets used for simulating 55 | * accept(), select() etc. during graceful shutdown. */ 56 | DUMMY = 4, 57 | 58 | /* EPOLL FDs contain information about registered FDs. 59 | * If we ever register a server socket on an epoll FD, 60 | * then we need to swap out the dummy socket. */ 61 | EPOLL = 5, 62 | 63 | } fdtype_t; 64 | 65 | struct fdinfo; 66 | typedef struct fdinfo fdinfo_t; 67 | 68 | typedef 69 | struct boundinfo 70 | { 71 | int stub_listened :1; 72 | int real_listened :1; 73 | int is_ghost :1; 74 | 75 | /* We see some higher-level tools passing 76 | * more complex address data down. The default 77 | * struct sockaddr is only 16 bytes, but java 78 | * (for example) will pass a 28 byte structure. 79 | * And of course, it's handled just *fine*. 80 | * So instead of storing things as a sockaddr, 81 | * we just store a copy of the data as passed. */ 82 | struct sockaddr* addr; 83 | socklen_t addrlen; 84 | 85 | } __attribute__((packed)) boundinfo_t; 86 | 87 | typedef 88 | struct trackedinfo 89 | { 90 | fdinfo_t *bound; 91 | } trackedinfo_t; 92 | 93 | typedef 94 | struct savedinfo 95 | { 96 | int fd; 97 | off_t offset; 98 | } savedinfo_t; 99 | 100 | typedef 101 | struct initialinfo 102 | { 103 | } initialinfo_t; 104 | 105 | typedef 106 | struct dummyinfo 107 | { 108 | int client; 109 | } dummyinfo_t; 110 | 111 | typedef 112 | struct epollinfo 113 | { 114 | } epollinfo_t; 115 | 116 | struct fdinfo 117 | { 118 | fdtype_t type; 119 | int refs; 120 | union 121 | { 122 | boundinfo_t bound; 123 | trackedinfo_t tracked; 124 | savedinfo_t saved; 125 | initialinfo_t initial; 126 | dummyinfo_t dummy; 127 | epollinfo_t epoll; 128 | }; 129 | }; 130 | 131 | /* Statistics. */ 132 | extern int total_bound; 133 | extern int total_tracked; 134 | extern int total_saved; 135 | extern int total_initial; 136 | extern int total_dummy; 137 | extern int total_epoll; 138 | 139 | static inline fdinfo_t* 140 | alloc_info(fdtype_t type) 141 | { 142 | fdinfo_t *info = (fdinfo_t*)calloc(1, sizeof(fdinfo_t)); 143 | memset(info, 0, sizeof(fdinfo_t)); 144 | info->type = type; 145 | info->refs = 1; 146 | switch( type ) 147 | { 148 | case BOUND: 149 | __sync_fetch_and_add(&total_bound, 1); 150 | break; 151 | case TRACKED: 152 | __sync_fetch_and_add(&total_tracked, 1); 153 | break; 154 | case SAVED: 155 | __sync_fetch_and_add(&total_saved, 1); 156 | break; 157 | case DUMMY: 158 | __sync_fetch_and_add(&total_dummy, 1); 159 | break; 160 | case EPOLL: 161 | __sync_fetch_and_add(&total_epoll, 1); 162 | break; 163 | } 164 | return info; 165 | } 166 | 167 | static void dec_ref(fdinfo_t* info); 168 | static inline void 169 | free_info(fdinfo_t* info) 170 | { 171 | switch( info->type ) 172 | { 173 | case BOUND: 174 | if( info->bound.addr != NULL ) 175 | { 176 | free(info->bound.addr); 177 | } 178 | __sync_fetch_and_add(&total_bound, -1); 179 | break; 180 | case TRACKED: 181 | if( info->tracked.bound != NULL ) 182 | { 183 | dec_ref(info->tracked.bound); 184 | } 185 | __sync_fetch_and_add(&total_tracked, -1); 186 | break; 187 | case SAVED: 188 | __sync_fetch_and_add(&total_saved, -1); 189 | break; 190 | case DUMMY: 191 | __sync_fetch_and_add(&total_dummy, -1); 192 | break; 193 | case EPOLL: 194 | __sync_fetch_and_add(&total_epoll, -1); 195 | break; 196 | } 197 | free(info); 198 | } 199 | 200 | static inline void 201 | inc_ref(fdinfo_t* info) 202 | { 203 | __sync_fetch_and_add(&info->refs, 1); 204 | } 205 | 206 | static inline void 207 | dec_ref(fdinfo_t* info) 208 | { 209 | if( __sync_fetch_and_add(&info->refs, -1) == 1 ) 210 | { 211 | free_info(info); 212 | } 213 | } 214 | 215 | int info_decode(int pipe, int *fd, fdinfo_t **info); 216 | int info_encode(int pipe, int fd, fdinfo_t *info); 217 | 218 | #endif 219 | -------------------------------------------------------------------------------- /src/fdtable.c: -------------------------------------------------------------------------------- 1 | /* 2 | * fdtable.c 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | #include "fdtable.h" 23 | #include "fdinfo.h" 24 | 25 | #include 26 | #include 27 | 28 | static fdinfo_t **fd_table = NULL; 29 | static int fd_size = 0; 30 | 31 | int 32 | fd_limit(void) 33 | { 34 | return fd_size; 35 | } 36 | 37 | int 38 | fd_max(void) 39 | { 40 | struct rlimit rlim; 41 | getrlimit(RLIMIT_NOFILE, &rlim); 42 | return rlim.rlim_max; 43 | } 44 | 45 | static inline void 46 | table_ensure(int index) 47 | { 48 | int orig_size = fd_size; 49 | if( index < fd_size ) 50 | { 51 | return; 52 | } 53 | 54 | if( fd_size == 0 ) 55 | { 56 | fd_size = 1; 57 | } 58 | while( index >= fd_size ) 59 | { 60 | fd_size *= 2; 61 | } 62 | 63 | /* Reallocate the table. */ 64 | fd_table = realloc(fd_table, sizeof(fdinfo_t*) * fd_size); 65 | 66 | /* Clear the new entries. */ 67 | memset(&fd_table[orig_size], 0, sizeof(fdinfo_t*) * (fd_size-orig_size)); 68 | } 69 | 70 | fdinfo_t* 71 | fd_lookup(int fd) 72 | { 73 | if( fd >= fd_size ) 74 | { 75 | return NULL; 76 | } 77 | 78 | return fd_table[fd]; 79 | } 80 | 81 | void 82 | fd_save(int fd, fdinfo_t *info) 83 | { 84 | table_ensure(fd); 85 | fd_table[fd] = info; 86 | } 87 | 88 | void 89 | fd_delete(int fd) 90 | { 91 | if( fd >= fd_size ) 92 | { 93 | return; 94 | } 95 | 96 | fd_table[fd] = NULL; 97 | } 98 | -------------------------------------------------------------------------------- /src/fdtable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * fdtable.h 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | #ifndef HUPTIME_FDTABLE_H 23 | #define HUPTIME_FDTABLE_H 24 | 25 | #include "fdinfo.h" 26 | 27 | /* Lookup the given FD. */ 28 | fdinfo_t* fd_lookup(int fd); 29 | 30 | /* Save the given entry. */ 31 | void fd_save(int fd, fdinfo_t* info); 32 | 33 | /* Delete the given entry. */ 34 | void fd_delete(int fd); 35 | 36 | /* Get the maximum possible FD. */ 37 | int fd_max(void); 38 | 39 | /* Get the maximum tracked FD. */ 40 | int fd_limit(void); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /src/funcs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * funcs.h 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | #ifndef HUPTIME_FUNCS_H 23 | #define HUPTIME_FUNCS_H 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | /* Typedefs for libc functions that we override. */ 32 | typedef int (*bind_t)(int sockfd, const struct sockaddr *addr, socklen_t addrlen); 33 | typedef int (*accept_t)(int sockfd, struct sockaddr *addr, socklen_t *addrlen); 34 | typedef int (*accept4_t)(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); 35 | typedef int (*listen_t)(int sockfd, int backlog); 36 | typedef int (*close_t)(int fd); 37 | typedef pid_t (*fork_t)(void); 38 | typedef int (*dup_t)(int fd); 39 | typedef int (*dup2_t)(int fd, int fd2); 40 | typedef int (*dup3_t)(int fd, int fd2, int flags); 41 | typedef void (*exit_t)(int status); 42 | typedef pid_t (*wait_t)(void *status); 43 | typedef pid_t (*waitpid_t)(pid_t pid, int *status, int options); 44 | typedef long (*syscall_t)(long number, ...); 45 | typedef int (*epoll_create_t)(int size); 46 | typedef int (*epoll_create1_t)(int flags); 47 | 48 | /* A structure containing all functions. */ 49 | typedef struct 50 | { 51 | bind_t bind; 52 | listen_t listen; 53 | accept_t accept; 54 | accept4_t accept4; 55 | close_t close; 56 | fork_t fork; 57 | dup_t dup; 58 | dup2_t dup2; 59 | dup3_t dup3; 60 | exit_t exit; 61 | wait_t wait; 62 | waitpid_t waitpid; 63 | syscall_t syscall; 64 | epoll_create_t epoll_create; 65 | epoll_create1_t epoll_create1; 66 | } funcs_t; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /src/impl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * impl.c 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | #include "impl.h" 23 | #include "stubs.h" 24 | #include "fdinfo.h" 25 | #include "fdtable.h" 26 | #include "utils.h" 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | #define unlikely(x) __builtin_expect(!!(x), 0) 43 | 44 | #ifndef SYS_accept4 45 | #ifdef ARCH64BIT 46 | #define SYS_accept4 (288) 47 | #elif ARCH32BIT 48 | #define SYS_accept4 (0x40000000 + 288) 49 | #else 50 | #error "Unknown architecture?" 51 | #endif 52 | #endif 53 | 54 | typedef enum 55 | { 56 | FORK = 1, 57 | EXEC = 2, 58 | } exit_strategy_t; 59 | 60 | typedef enum 61 | { 62 | FALSE = 0, 63 | TRUE = 1, 64 | } bool_t; 65 | 66 | /* Copy of execution environment. */ 67 | static char **environ_copy = NULL; 68 | static char **args_copy = NULL; 69 | static char *exe_copy = NULL; 70 | static char *cwd_copy = NULL; 71 | 72 | /* Whether or not we are currently exiting. */ 73 | static bool_t is_exiting = FALSE; 74 | 75 | /* Our exit strategy (set on startup). */ 76 | static exit_strategy_t exit_strategy = FORK; 77 | 78 | /* Files to unlink? */ 79 | static char *to_unlink = NULL; 80 | 81 | /* Multi mode? */ 82 | static bool_t multi_mode = FALSE; 83 | 84 | /* Revive mode? */ 85 | static bool_t revive_mode = FALSE; 86 | 87 | /* Wait mode? */ 88 | static bool_t wait_mode = FALSE; 89 | 90 | /* Whether or not our HUP handler will exit or restart. */ 91 | static pid_t master_pid = (pid_t)-1; 92 | 93 | /* Debug hook. */ 94 | static bool_t debug_enabled = FALSE; 95 | 96 | #define DEBUG(fmt, args...) \ 97 | do { \ 98 | if( debug_enabled == TRUE ) \ 99 | { \ 100 | pid_t pid = getpid(); \ 101 | fprintf(stderr, "huptime %d: " fmt "\n", pid, ## args); \ 102 | fflush(stderr); \ 103 | } \ 104 | } while(0) 105 | 106 | /* Lock (for thread-safe fd tracking). */ 107 | static pthread_mutex_t mutex; 108 | 109 | #define L() \ 110 | do { \ 111 | DEBUG("-wait- %d", __LINE__); \ 112 | pthread_mutex_lock(&mutex); \ 113 | DEBUG("-acquired- %d", __LINE__); \ 114 | } while(0) 115 | 116 | #define U() \ 117 | do { \ 118 | DEBUG("-release- %d", __LINE__); \ 119 | pthread_mutex_unlock(&mutex); \ 120 | } while(0) 121 | 122 | /* Our restart signal pipe. */ 123 | static int restart_pipe[2] = { -1, -1 }; 124 | 125 | /* Our core signal handlers. */ 126 | static void* impl_restart_thread(void*); 127 | void 128 | sighandler(int signo) 129 | { 130 | /* Notify the restart thread. 131 | * We have to do this in a separate thread, because 132 | * we have no guarantees about which thread has been 133 | * interrupted in order to execute this signal handler. 134 | * Because this could have happened during a critical 135 | * section (i.e. locks held) we have no choice but to 136 | * fire the restart asycnhronously so that it too can 137 | * grab locks appropriately. */ 138 | 139 | if( restart_pipe[1] == -1 ) 140 | { 141 | /* We've already run. */ 142 | return; 143 | } 144 | 145 | while( 1 ) 146 | { 147 | char go = 'R'; 148 | int rc = write(restart_pipe[1], &go, 1); 149 | if( rc == 0 ) 150 | { 151 | /* Wat? Try again. */ 152 | continue; 153 | } 154 | else if( rc == 1 ) 155 | { 156 | /* Done. */ 157 | libc.close(restart_pipe[1]); 158 | restart_pipe[1] = -1; 159 | break; 160 | } 161 | else if( rc < 0 && (errno == EAGAIN || errno == EINTR) ) 162 | { 163 | /* Go again. */ 164 | continue; 165 | } 166 | else 167 | { 168 | /* Shit. */ 169 | DEBUG("Restart pipe fubared!? Sorry."); 170 | break; 171 | } 172 | } 173 | } 174 | 175 | static int 176 | do_dup(int fd) 177 | { 178 | int rval = -1; 179 | fdinfo_t *info = NULL; 180 | 181 | if( fd < 0 ) 182 | { 183 | errno = EINVAL; 184 | return -1; 185 | } 186 | 187 | DEBUG("do_dup(%d, ...) ...", fd); 188 | L(); 189 | info = fd_lookup(fd); 190 | if( info == NULL ) 191 | { 192 | U(); 193 | rval = libc.dup(fd); 194 | DEBUG("do_dup(%d) => %d (no info)", fd, rval); 195 | return rval; 196 | } 197 | 198 | rval = libc.dup(fd); 199 | if( rval >= 0 ) 200 | { 201 | inc_ref(info); 202 | fd_save(rval, info); 203 | } 204 | 205 | U(); 206 | DEBUG("do_dup(%d) => %d (with info)", fd, rval); 207 | return rval; 208 | } 209 | 210 | void 211 | impl_exec(void) 212 | { 213 | DEBUG("Preparing for exec..."); 214 | 215 | /* Reset our signal masks. 216 | * We intentionally mask SIGHUP here so that 217 | * it can't be called prior to us installing 218 | * our signal handlers. */ 219 | sigset_t set; 220 | sigemptyset(&set); 221 | sigaddset(&set, SIGHUP); 222 | sigprocmask(SIG_BLOCK, &set, NULL); 223 | 224 | /* Encode extra information. 225 | * 226 | * This includes information about sockets which 227 | * are in the BOUND or SAVED state. Note that we 228 | * can't really do anything with these *now* as 229 | * there are real threads running rampant -- so 230 | * we encode things for the exec() and take care 231 | * of it post-exec(), where we know we're solo. 232 | * 233 | * This information is encoded into a pipe which 234 | * is passed as an extra environment variable into 235 | * the next child. Although there is a limit on the 236 | * amount of data that can be stuffed into a pipe, 237 | * past Linux 2.6.11 (IIRC) this is 65K. */ 238 | int pipes[2]; 239 | if( pipe(pipes) < 0 ) 240 | { 241 | DEBUG("Unable to create pipes?"); 242 | libc.exit(1); 243 | } 244 | 245 | /* Stuff information into the pipe. */ 246 | for( int fd = 0; fd < fd_limit(); fd += 1 ) 247 | { 248 | fdinfo_t *info = fd_lookup(fd); 249 | 250 | int to_be_saved = (info != NULL && 251 | (info->type == BOUND || info->type == SAVED)); 252 | 253 | if( fd == 2 || to_be_saved ) 254 | { 255 | /* I can't believe this is necessary. 256 | * When node.js starts up, it seems to run over 257 | * an arbitrary number of file descriptors and 258 | * mark them all CLO_EXEC. That is so messed up. 259 | * That's some seriously broken behaviour. */ 260 | fcntl(fd, F_SETFD, 0); 261 | } 262 | if( to_be_saved ) 263 | { 264 | if( info_encode(pipes[1], fd, info) < 0 ) 265 | { 266 | DEBUG("Error encoding fd %d: %s", 267 | fd, strerror(errno)); 268 | } 269 | else 270 | { 271 | DEBUG("Encoded fd %d (type %d).", fd, info->type); 272 | } 273 | } 274 | } 275 | libc.close(pipes[1]); 276 | DEBUG("Finished encoding."); 277 | 278 | /* Prepare our environment variable. */ 279 | char pipe_env[32]; 280 | snprintf(pipe_env, 32, "HUPTIME_PIPE=%d", pipes[0]); 281 | 282 | /* Mask the existing environment variable. */ 283 | char **environ = environ_copy; 284 | int environ_len = 0; 285 | 286 | for( environ_len = 0; 287 | environ[environ_len] != NULL; 288 | environ_len += 1 ) 289 | { 290 | if( !strncmp("HUPTIME_PIPE=", 291 | environ[environ_len], 292 | strlen("HUPTIME_PIPE=")) ) 293 | { 294 | environ[environ_len] = pipe_env; 295 | break; 296 | } 297 | } 298 | 299 | /* Do we need to extend the environment? */ 300 | if( environ[environ_len] == NULL ) 301 | { 302 | char** new_environ = malloc(sizeof(char*) * (environ_len + 2)); 303 | memcpy(new_environ, environ, sizeof(char*) * (environ_len)); 304 | new_environ[environ_len] = pipe_env; 305 | new_environ[environ_len + 1] = NULL; 306 | environ = new_environ; 307 | } 308 | 309 | /* Execute in the same environment, etc. */ 310 | chdir(cwd_copy); 311 | DEBUG("Doing exec()... bye!"); 312 | execve(exe_copy, args_copy, environ); 313 | 314 | /* Bail. Should never reach here. */ 315 | DEBUG("Things went horribly wrong!"); 316 | libc.exit(1); 317 | } 318 | 319 | void 320 | impl_exit_check(void) 321 | { 322 | if( is_exiting == TRUE && total_tracked == 0 ) 323 | { 324 | if( wait_mode == TRUE ) 325 | { 326 | /* Check for any active child processes. 327 | * NOTE: Because we are using waitid() here, and 328 | * that allows us to specify WNOWAIT, the child 329 | * will stay in a waitable state for to be reaped 330 | * whenever the actual program wants to. */ 331 | do { 332 | siginfo_t info; 333 | int rval = waitid(P_ALL, 0, &info, WNOHANG|WNOWAIT); 334 | if( rval < 0 && errno == EINTR ) 335 | { 336 | continue; 337 | } 338 | if( rval >= 0 || (rval < 0 && errno != ECHILD) ) 339 | { 340 | /* There are still active child processes. */ 341 | return; 342 | } 343 | break; 344 | } while( 1 ); 345 | } 346 | 347 | DEBUG("No active connections, finishing exit."); 348 | 349 | switch( exit_strategy ) 350 | { 351 | case FORK: 352 | /* We're done. 353 | * No more connections are active, and there's 354 | * presumably already a child process handling 355 | * new incoming connections. */ 356 | DEBUG("Goodbye!"); 357 | libc.exit(0); 358 | break; 359 | 360 | case EXEC: 361 | /* Let's do the exec. 362 | * We're wrapped up existing connections, we can 363 | * re-execute the application to start handling new 364 | * incoming connections. */ 365 | DEBUG("See you soon..."); 366 | impl_exec(); 367 | break; 368 | } 369 | } 370 | } 371 | 372 | static int 373 | info_close(int fd, fdinfo_t* info) 374 | { 375 | int rval = -1; 376 | 377 | switch( info->type ) 378 | { 379 | case BOUND: 380 | case TRACKED: 381 | case EPOLL: 382 | if( info->type == BOUND && revive_mode == TRUE ) 383 | { 384 | /* We don't close bound sockets in revive mode. 385 | * This allows the program to exit "cleanly" and 386 | * we will preserve the socket for the next run. */ 387 | rval = 0; 388 | break; 389 | } 390 | dec_ref(info); 391 | fd_delete(fd); 392 | rval = libc.close(fd); 393 | break; 394 | 395 | case SAVED: 396 | case DUMMY: 397 | /* Woah, their program is most likely either messed up, 398 | * or it's going through and closing all descriptors 399 | * prior to an exec. We're just going to ignore this. */ 400 | break; 401 | } 402 | 403 | return rval; 404 | } 405 | 406 | static int 407 | do_dup3(int fd, int fd2, int flags) 408 | { 409 | int rval = -1; 410 | fdinfo_t *info = NULL; 411 | fdinfo_t *info2 = NULL; 412 | 413 | if( fd < 0 || fd2 < 0 ) 414 | { 415 | errno = EINVAL; 416 | return -1; 417 | } 418 | 419 | DEBUG("do_dup3(%d, %d, ...) ...", fd, fd2); 420 | L(); 421 | if( fd == fd2 ) 422 | { 423 | U(); 424 | DEBUG("do_dup3(%d, %d, ...) => 0", fd, fd2); 425 | return fd2; 426 | } 427 | 428 | info = fd_lookup(fd); 429 | info2 = fd_lookup(fd2); 430 | if( info2 != NULL ) 431 | { 432 | rval = info_close(fd2, info2); 433 | if( rval < 0 ) 434 | { 435 | U(); 436 | DEBUG("do_dup3(%d, %d, ...) => %d (close failed)", fd, fd2, rval); 437 | return rval; 438 | } 439 | } 440 | 441 | rval = libc.dup3(fd, fd2, flags); 442 | if( rval < 0 ) 443 | { 444 | U(); 445 | DEBUG("do_dup3(%d, %d, ...) => %d (dup3 failed)", fd, fd2, rval); 446 | return rval; 447 | } 448 | 449 | if( info != NULL ) 450 | { 451 | inc_ref(info); 452 | fd_save(fd2, info); 453 | } 454 | 455 | U(); 456 | DEBUG("do_dup3(%d, %d, ...) => %d", fd, fd2, rval); 457 | return rval; 458 | } 459 | 460 | static int 461 | do_dup2(int fd, int fd2) 462 | { 463 | return do_dup3(fd, fd2, 0); 464 | } 465 | 466 | static int 467 | do_close(int fd) 468 | { 469 | int rval = -1; 470 | fdinfo_t *info = NULL; 471 | 472 | if( fd < 0 ) 473 | { 474 | errno = EINVAL; 475 | return -1; 476 | } 477 | 478 | DEBUG("do_close(%d, ...) ...", fd); 479 | L(); 480 | info = fd_lookup(fd); 481 | if( info == NULL ) 482 | { 483 | U(); 484 | rval = libc.close(fd); 485 | DEBUG("do_close(%d) => %d (no info)", fd, rval); 486 | return rval; 487 | } 488 | 489 | rval = info_close(fd, info); 490 | impl_exit_check(); 491 | U(); 492 | 493 | DEBUG("do_close(%d) => %d (%d tracked)", 494 | fd, rval, total_tracked); 495 | return rval; 496 | } 497 | 498 | static void 499 | impl_init_lock(void) 500 | { 501 | /* Initialize our lock. 502 | * This is a recursive lock simply for convenience. 503 | * There are a few calls (i.e. bind) which leverage 504 | * other unlock internal calls (do_dup2), so we make 505 | * the lock recursive. This could easily be eliminated 506 | * with a little bit of refactoring. */ 507 | pthread_mutexattr_t mutex_attr; 508 | pthread_mutexattr_init(&mutex_attr); 509 | pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); 510 | pthread_mutex_init(&mutex, &mutex_attr); 511 | } 512 | 513 | static void 514 | impl_init_thread(void) 515 | { 516 | if( restart_pipe[0] != -1 ) 517 | { 518 | libc.close(restart_pipe[0]); 519 | } 520 | if( restart_pipe[1] != -1 ) 521 | { 522 | libc.close(restart_pipe[1]); 523 | } 524 | 525 | /* Create our restart thread. 526 | * 527 | * See the note in sighandler() for an explanation 528 | * of why the restart must be done in a separate thread. 529 | * 530 | * We do the thread creation here instead of in the 531 | * handler because pthread_create() is not a signal-safe 532 | * function to call from the handler. */ 533 | if( pipe(restart_pipe) < 0 ) 534 | { 535 | DEBUG("Error creating restart pipes: %s", strerror(errno)); 536 | libc.exit(1); 537 | } 538 | 539 | /* Ensure that we have cloexec. */ 540 | if( fcntl(restart_pipe[0], F_SETFD, FD_CLOEXEC) < 0 || 541 | fcntl(restart_pipe[1], F_SETFD, FD_CLOEXEC) < 0 ) 542 | { 543 | DEBUG("Can't set restart pipe to cloexec?"); 544 | libc.exit(1); 545 | } 546 | 547 | pthread_t thread; 548 | pthread_attr_t thread_attr; 549 | pthread_attr_init(&thread_attr); 550 | pthread_attr_setdetachstate(&thread_attr, 1); 551 | if( pthread_create(&thread, &thread_attr, impl_restart_thread, NULL) < 0 ) 552 | { 553 | DEBUG("Error creating restart thread: %s", strerror(errno)); 554 | libc.exit(1); 555 | } 556 | } 557 | 558 | static void 559 | impl_install_sighandlers(void) 560 | { 561 | struct sigaction action; 562 | struct sigaction old_action; 563 | action.sa_handler = sighandler; 564 | action.sa_flags = SA_RESTART; 565 | sigaction(SIGHUP, &action, &old_action); 566 | 567 | if( old_action.sa_handler != sighandler ) 568 | { 569 | DEBUG("Signal handler installed."); 570 | } 571 | } 572 | 573 | void 574 | impl_init(void) 575 | { 576 | const char* mode_env = getenv("HUPTIME_MODE"); 577 | const char* multi_env = getenv("HUPTIME_MULTI"); 578 | const char* revive_env = getenv("HUPTIME_REVIVE"); 579 | const char* debug_env = getenv("HUPTIME_DEBUG"); 580 | const char* pipe_env = getenv("HUPTIME_PIPE"); 581 | const char* wait_env = getenv("HUPTIME_WAIT"); 582 | 583 | if( debug_env != NULL && strlen(debug_env) > 0 ) 584 | { 585 | debug_enabled = !strcasecmp(debug_env, "true") ? TRUE: FALSE; 586 | } 587 | 588 | DEBUG("Initializing..."); 589 | 590 | /* Initialize our lock. */ 591 | impl_init_lock(); 592 | 593 | /* Save this pid as our master pid. 594 | * This is done to handle processes that use 595 | * process pools. We remember the master pid and 596 | * will do the full fork()/exec() only when we are 597 | * the master. Otherwise, we will simply shutdown 598 | * gracefully, and all the master to restart. */ 599 | master_pid = getpid(); 600 | 601 | /* Grab our exit strategy. */ 602 | if( mode_env != NULL && strlen(mode_env) > 0 ) 603 | { 604 | if( !strcasecmp(mode_env, "fork") ) 605 | { 606 | exit_strategy = FORK; 607 | DEBUG("Exit strategy is fork."); 608 | } 609 | else if( !strcasecmp(mode_env, "exec") ) 610 | { 611 | exit_strategy = EXEC; 612 | DEBUG("Exit strategy is exec."); 613 | } 614 | else 615 | { 616 | fprintf(stderr, "Unknown exit strategy."); 617 | libc.exit(1); 618 | } 619 | } 620 | 621 | /* Check if we have something to unlink. */ 622 | to_unlink = getenv("HUPTIME_UNLINK"); 623 | if( to_unlink != NULL && strlen(to_unlink) > 0 ) 624 | { 625 | DEBUG("Unlink is '%s'.", to_unlink); 626 | } 627 | 628 | /* Clear up any outstanding child processes. 629 | * Because we may have exited before the process 630 | * could do appropriate waitpid()'s, we try to 631 | * clean up children here. Note that we may have 632 | * some zombies that hang around during the life 633 | * of the program, but at every restart they will 634 | * be cleaned up (so at least they won't grow 635 | * without bound). */ 636 | int status = 0; 637 | while( waitpid((pid_t)-1, &status, WNOHANG) > 0 ); 638 | 639 | /* Check if we're in multi mode. */ 640 | if( multi_env != NULL && strlen(multi_env) > 0 ) 641 | { 642 | multi_mode = !strcasecmp(multi_env, "true") ? TRUE: FALSE; 643 | } 644 | #ifndef SO_REUSEPORT 645 | if( multi_mode == TRUE ) 646 | { 647 | fprintf(stderr, "WARNING: Multi mode not supported.\n"); 648 | fprintf(stderr, "(Requires at least Linux 3.9 and recent headers).\n"); 649 | } 650 | #endif 651 | 652 | /* Check if we're in revive mode. */ 653 | if( revive_env != NULL && strlen(revive_env) > 0 ) 654 | { 655 | revive_mode = !strcasecmp(revive_env, "true") ? TRUE : FALSE; 656 | } 657 | 658 | /* Check if we are in wait mode. */ 659 | if( wait_env != NULL && strlen(wait_env) > 0 ) 660 | { 661 | wait_mode = !strcasecmp(wait_env, "true") ? TRUE : FALSE; 662 | } 663 | 664 | /* Check if we're a respawn. */ 665 | if( pipe_env != NULL && strlen(pipe_env) > 0 ) 666 | { 667 | int fd = -1; 668 | fdinfo_t *info = NULL; 669 | int pipefd = strtol(pipe_env, NULL, 10); 670 | 671 | DEBUG("Loading all file descriptors."); 672 | 673 | /* Decode all passed information. */ 674 | while( !info_decode(pipefd, &fd, &info) ) 675 | { 676 | fd_save(fd, info); 677 | DEBUG("Decoded fd %d (type %d).", fd, info->type); 678 | info = NULL; 679 | } 680 | if( info != NULL ) 681 | { 682 | dec_ref(info); 683 | } 684 | 685 | /* Finished with the pipe. */ 686 | libc.close(pipefd); 687 | unsetenv("HUPTIME_PIPE"); 688 | DEBUG("Finished decoding."); 689 | 690 | /* Close all non-encoded descriptors. */ 691 | for( fd = 0; fd < fd_max(); fd += 1 ) 692 | { 693 | info = fd_lookup(fd); 694 | if( info == NULL ) 695 | { 696 | DEBUG("Closing fd %d.", fd); 697 | libc.close(fd); 698 | } 699 | } 700 | 701 | /* Restore all given file descriptors. */ 702 | for( fd = 0; fd < fd_limit(); fd += 1 ) 703 | { 704 | info = fd_lookup(fd); 705 | if( info != NULL && info->type == SAVED ) 706 | { 707 | fdinfo_t *orig_info = fd_lookup(info->saved.fd); 708 | if( orig_info != NULL ) 709 | { 710 | /* Uh-oh, conflict. Move the original (best effort). */ 711 | do_dup(info->saved.fd); 712 | do_close(info->saved.fd); 713 | } 714 | 715 | /* Return the offset (ignore failure). */ 716 | if( info->saved.offset != (off_t)-1 ) 717 | { 718 | lseek(fd, info->saved.offset, SEEK_SET); 719 | } 720 | 721 | /* Move the SAVED fd back. */ 722 | libc.dup2(fd, info->saved.fd); 723 | DEBUG("Restored fd %d.", info->saved.fd); 724 | } 725 | } 726 | } 727 | else 728 | { 729 | DEBUG("Saving all initial file descriptors."); 730 | 731 | /* Save all of our initial files. These are used 732 | * for re-execing the process. These are persisted 733 | * effectively forever, and on restarts we close 734 | * everything that is not a BOUND socket or a SAVED 735 | * file descriptor. */ 736 | for( int fd = 0; fd < fd_max(); fd += 1 ) 737 | { 738 | fdinfo_t *info = fd_lookup(fd); 739 | if( info != NULL ) 740 | { 741 | /* Encoded earlier. */ 742 | continue; 743 | } 744 | 745 | /* Make a new SAVED FD. */ 746 | int newfd = libc.dup(fd); 747 | if( newfd >= 0 ) 748 | { 749 | fdinfo_t *saved_info = alloc_info(SAVED); 750 | 751 | if( saved_info != NULL ) 752 | { 753 | saved_info->saved.fd = fd; 754 | saved_info->saved.offset = lseek(fd, 0, SEEK_CUR); 755 | fd_save(newfd, saved_info); 756 | DEBUG("Saved fd %d (offset %lld).", 757 | fd, (long long int)saved_info->saved.offset); 758 | } 759 | } 760 | } 761 | } 762 | 763 | /* Save the environment. 764 | * 765 | * NOTE: We reserve extra space in the environment 766 | * for our special start-up parameters, which will be added 767 | * in impl_exec() below. (The encoded BOUND/SAVED sockets). 768 | * 769 | * We also filter out the special variables above that were 770 | * used to pass in information about sockets that were bound. */ 771 | free(environ_copy); 772 | environ_copy = (char**)read_nul_sep("/proc/self/environ"); 773 | DEBUG("Saved environment."); 774 | 775 | /* Save the arguments. */ 776 | free(args_copy); 777 | args_copy = (char**)read_nul_sep("/proc/self/cmdline"); 778 | DEBUG("Saved args."); 779 | for( int i = 0; args_copy[i] != NULL; i += 1 ) 780 | { 781 | DEBUG(" arg%d=%s", i, args_copy[i]); 782 | } 783 | 784 | /* Save the cwd & exe. */ 785 | free(cwd_copy); 786 | cwd_copy = (char*)read_link("/proc/self/cwd"); 787 | DEBUG("Saved cwd."); 788 | free(exe_copy); 789 | exe_copy = (char*)read_link("/proc/self/exe"); 790 | DEBUG("Saved exe."); 791 | 792 | /* Install our signal handlers. */ 793 | impl_install_sighandlers(); 794 | 795 | /* Initialize our thread. */ 796 | impl_init_thread(); 797 | 798 | /* Unblock our signals. 799 | * Note that we have specifically masked the 800 | * signals prior to the exec() below, to cover 801 | * the race between program start and having 802 | * installed the appropriate handlers. */ 803 | sigset_t set; 804 | sigemptyset(&set); 805 | sigaddset(&set, SIGHUP); 806 | sigprocmask(SIG_UNBLOCK, &set, NULL); 807 | 808 | /* Done. */ 809 | DEBUG("Initialization complete."); 810 | } 811 | 812 | static int 813 | impl_dummy_server(void) 814 | { 815 | int dummy_server = -1; 816 | 817 | /* Create our dummy sock. */ 818 | struct sockaddr_un dummy_addr; 819 | char *socket_path = tempnam("/tmp", ".huptime"); 820 | 821 | memset(&dummy_addr, 0, sizeof(struct sockaddr_un)); 822 | dummy_addr.sun_family = AF_UNIX; 823 | strncpy(dummy_addr.sun_path, socket_path, sizeof(dummy_addr.sun_path)-1); 824 | 825 | /* Create a dummy server. */ 826 | dummy_server = socket(AF_UNIX, SOCK_STREAM, 0); 827 | if( dummy_server < 0 ) 828 | { 829 | fprintf(stderr, "Unable to create unix socket?"); 830 | return -1; 831 | } 832 | if( fcntl(dummy_server, F_SETFD, FD_CLOEXEC) < 0 ) 833 | { 834 | close(dummy_server); 835 | fprintf(stderr, "Unable to set cloexec?"); 836 | return -1; 837 | } 838 | if( libc.bind( 839 | dummy_server, 840 | (struct sockaddr*)&dummy_addr, 841 | sizeof(struct sockaddr_un)) < 0 ) 842 | { 843 | close(dummy_server); 844 | fprintf(stderr, "Unable to bind unix socket?"); 845 | return -1; 846 | } 847 | if( libc.listen(dummy_server, 1) < 0 ) 848 | { 849 | close(dummy_server); 850 | fprintf(stderr, "Unable to listen on unix socket?"); 851 | return -1; 852 | } 853 | 854 | /* Connect a dummy client. */ 855 | int dummy_client = socket(AF_UNIX, SOCK_STREAM, 0); 856 | if( dummy_client < 0 ) 857 | { 858 | close(dummy_server); 859 | fprintf(stderr, "Unable to create unix socket?"); 860 | return -1; 861 | } 862 | if( fcntl(dummy_client, F_SETFD, FD_CLOEXEC) < 0 ) 863 | { 864 | close(dummy_server); 865 | close(dummy_client); 866 | fprintf(stderr, "Unable to set cloexec?"); 867 | return -1; 868 | } 869 | if( connect( 870 | dummy_client, 871 | (struct sockaddr*)&dummy_addr, 872 | sizeof(struct sockaddr_un)) < 0 ) 873 | { 874 | close(dummy_server); 875 | close(dummy_client); 876 | fprintf(stderr, "Unable to connect dummy client?"); 877 | return -1; 878 | } 879 | 880 | /* Put the client into an error state. */ 881 | int dummy_fd = libc.accept(dummy_server, NULL, 0); 882 | if( dummy_fd < 0 ) 883 | { 884 | fprintf(stderr, "Unable to accept internal client?"); 885 | close(dummy_server); 886 | close(dummy_client); 887 | return -1; 888 | } 889 | close(dummy_fd); 890 | 891 | /* Save the dummy info. */ 892 | fdinfo_t* dummy_info = alloc_info(DUMMY); 893 | if( dummy_info == NULL ) 894 | { 895 | fprintf(stderr, "Unable to allocate dummy info?"); 896 | return -1; 897 | } 898 | dummy_info->dummy.client = dummy_client; 899 | fd_save(dummy_server, dummy_info); 900 | inc_ref(dummy_info); 901 | fd_save(dummy_client, dummy_info); 902 | 903 | /* Ensure that it's unlinked. */ 904 | unlink(socket_path); 905 | free(socket_path); 906 | 907 | return dummy_server; 908 | } 909 | 910 | void 911 | impl_exit_start(void) 912 | { 913 | if( is_exiting == TRUE ) 914 | { 915 | return; 916 | } 917 | 918 | /* We are now exiting. 919 | * After this point, all calls to various sockets, 920 | * (i.e. accept(), listen(), etc. will result in stalls. 921 | * We are just waiting until existing connections have 922 | * finished and then we will be either exec()'ing a new 923 | * version or exiting this process. */ 924 | is_exiting = TRUE; 925 | 926 | /* Get ready to restart. 927 | * We only proceed with actual restart actions 928 | * if we are the master process, otherwise we will 929 | * simply prepare to shutdown cleanly once all the 930 | * current active connections have finished. */ 931 | if( master_pid == getpid() ) 932 | { 933 | pid_t child; 934 | DEBUG("Exit started -- this is the master."); 935 | 936 | /* Unlink files (e.g. pidfile). */ 937 | if( to_unlink != NULL && strlen(to_unlink) > 0 ) 938 | { 939 | DEBUG("Unlinking '%s'...", to_unlink); 940 | unlink(to_unlink); 941 | } 942 | 943 | /* Neuter this process. */ 944 | for( int fd = 0; fd < fd_limit(); fd += 1 ) 945 | { 946 | fdinfo_t* info = fd_lookup(fd); 947 | if( exit_strategy == FORK && 948 | info != NULL && info->type == SAVED ) 949 | { 950 | /* Close initial files. Since these 951 | * are now passed on to the child, we 952 | * ensure that the parent won't mess 953 | * with them anymore. Note that we still 954 | * have a copy as all SAVED descriptors. */ 955 | if( info->saved.fd == 2 ) 956 | { 957 | /* We treat stderr special. 958 | * Assuming logging will go here, we 959 | * allow the parent process to continue 960 | * writing to this file (and hope that 961 | * it's open in APPEND mode, etc.). */ 962 | continue; 963 | } 964 | int nullfd = open("/dev/null", O_RDWR); 965 | do_dup2(nullfd, info->saved.fd); 966 | libc.close(nullfd); 967 | } 968 | if( info != NULL && 969 | info->type == BOUND && !info->bound.is_ghost ) 970 | { 971 | /* Change BOUND sockets to dummy sockets. 972 | * This will allow select() and poll() to 973 | * operate as you expect, and never give 974 | * back new clients. */ 975 | int newfd = do_dup(fd); 976 | if( newfd >= 0 ) 977 | { 978 | int dummy_server = impl_dummy_server(); 979 | if( dummy_server >= 0 ) 980 | { 981 | /* Remove the descriptor in any epoll FDs. */ 982 | for( int efd = 0; efd < fd_limit(); efd += 1 ) 983 | { 984 | fdinfo_t* einfo = fd_lookup(efd); 985 | if( einfo != NULL && einfo->type == EPOLL ) 986 | { 987 | struct epoll_event no_event; 988 | epoll_ctl(efd, EPOLL_CTL_DEL, fd, &no_event); 989 | } 990 | } 991 | 992 | info->bound.is_ghost = 1; 993 | do_dup2(dummy_server, fd); 994 | DEBUG("Replaced FD %d with dummy.", fd); 995 | } 996 | else 997 | { 998 | do_close(newfd); 999 | } 1000 | } 1001 | } 1002 | } 1003 | 1004 | switch( exit_strategy ) 1005 | { 1006 | case FORK: 1007 | /* Start the child process. 1008 | * We will exit gracefully when the tracked 1009 | * connection count reaches zero. */ 1010 | DEBUG("Exit strategy is fork."); 1011 | child = libc.fork(); 1012 | if( child == 0 ) 1013 | { 1014 | DEBUG("I'm the child."); 1015 | impl_exec(); 1016 | } 1017 | else 1018 | { 1019 | DEBUG("I'm the parent."); 1020 | } 1021 | break; 1022 | 1023 | case EXEC: 1024 | /* Nothing necessary beyond the above. */ 1025 | DEBUG("Exit strategy is exec."); 1026 | break; 1027 | } 1028 | } 1029 | else 1030 | { 1031 | /* Force our strategy to fork, though we haven't forked. 1032 | * This will basically just have this process exit cleanly 1033 | * once all the current active connections have finished. */ 1034 | DEBUG("Exit started -- this is the child."); 1035 | exit_strategy = FORK; 1036 | } 1037 | } 1038 | 1039 | void 1040 | impl_restart(void) 1041 | { 1042 | /* Indicate that we are now exiting. */ 1043 | L(); 1044 | impl_exit_start(); 1045 | impl_exit_check(); 1046 | U(); 1047 | } 1048 | 1049 | void* 1050 | impl_restart_thread(void* arg) 1051 | { 1052 | /* Wait for our signal. */ 1053 | while( 1 ) 1054 | { 1055 | char go = 0; 1056 | int rc = read(restart_pipe[0], &go, 1); 1057 | if( rc == 1 ) 1058 | { 1059 | /* Go. */ 1060 | break; 1061 | } 1062 | else if( rc == 0 ) 1063 | { 1064 | /* Wat? Restart. */ 1065 | DEBUG("Restart pipe closed?!"); 1066 | break; 1067 | } 1068 | else if( rc < 0 && (errno == EAGAIN || errno == EINTR) ) 1069 | { 1070 | /* Keep trying. */ 1071 | continue; 1072 | } 1073 | else 1074 | { 1075 | /* Real error. Let's restart. */ 1076 | DEBUG("Restart pipe fubared?!"); 1077 | break; 1078 | } 1079 | } 1080 | 1081 | libc.close(restart_pipe[0]); 1082 | restart_pipe[0] = -1; 1083 | 1084 | /* See note above in sighandler(). */ 1085 | impl_restart(); 1086 | return arg; 1087 | } 1088 | 1089 | static pid_t 1090 | do_fork(void) 1091 | { 1092 | pid_t res = (pid_t)-1; 1093 | 1094 | /* We block SIGHUP during fork(). 1095 | * This is because we communicate our restart 1096 | * intention via a pipe, and it's conceivable 1097 | * that between the fork() and impl_init_thread() 1098 | * the signal handler will be triggered and we'll 1099 | * end up writing to the restart pipe that is 1100 | * still connected to the master process. */ 1101 | sigset_t set; 1102 | sigemptyset(&set); 1103 | sigaddset(&set, SIGHUP); 1104 | sigprocmask(SIG_BLOCK, &set, NULL); 1105 | 1106 | DEBUG("do_fork() ..."); 1107 | 1108 | L(); 1109 | res = libc.fork(); 1110 | if( res == 0 ) 1111 | { 1112 | if( total_bound == 0 ) 1113 | { 1114 | /* We haven't yet bound any sockets. This is 1115 | * a common pattern where the process may be 1116 | * daemonizing. We reset the master_pid so that 1117 | * the initalization routine will actually reset 1118 | * and treat this new process as a master. 1119 | * The reason we don't do this if sockets are 1120 | * already bound, is that if master_pid != getpid(), 1121 | * i.e. for process pools, then we neither fork() 1122 | * nor exec(), but simply go into a normal exit. */ 1123 | master_pid = getpid(); 1124 | } 1125 | 1126 | impl_init_lock(); 1127 | impl_init_thread(); 1128 | } 1129 | else 1130 | { 1131 | U(); 1132 | } 1133 | 1134 | sigprocmask(SIG_UNBLOCK, &set, NULL); 1135 | DEBUG("do_fork() => %d", res); 1136 | return res; 1137 | } 1138 | 1139 | static int 1140 | do_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) 1141 | { 1142 | fdinfo_t *info = NULL; 1143 | int rval = -1; 1144 | 1145 | if( sockfd < 0 ) 1146 | { 1147 | errno = EINVAL; 1148 | return -1; 1149 | } 1150 | 1151 | /* At this point, we can reasonably assume 1152 | * the program has started up and has installed 1153 | * whatever signal handlers it wants. We check 1154 | * that our own signal handler is installed. 1155 | * If the user doesn't want us to override the 1156 | * built-in signal handlers, they shouldn't use 1157 | * huptime. */ 1158 | impl_install_sighandlers(); 1159 | 1160 | DEBUG("do_bind(%d, ...) ...", sockfd); 1161 | L(); 1162 | 1163 | /* See if this socket already exists. */ 1164 | for( int fd = 0; fd < fd_limit(); fd += 1 ) 1165 | { 1166 | fdinfo_t *info = fd_lookup(fd); 1167 | if( info != NULL && 1168 | info->type == BOUND && 1169 | info->bound.addrlen == addrlen && 1170 | !memcmp(addr, (void*)info->bound.addr, addrlen) ) 1171 | { 1172 | DEBUG("Found ghost %d, cloning...", fd); 1173 | 1174 | /* Give back a duplicate of this one. */ 1175 | int rval = do_dup2(fd, sockfd); 1176 | if( rval < 0 ) 1177 | { 1178 | /* Dup2 failed? */ 1179 | DEBUG("Failed."); 1180 | continue; 1181 | } 1182 | if( info->bound.is_ghost ) 1183 | { 1184 | /* Close the original (not needed). */ 1185 | info->bound.is_ghost = 0; 1186 | do_close(fd); 1187 | } 1188 | 1189 | /* Success. */ 1190 | U(); 1191 | DEBUG("do_bind(%d, ...) => 0 (ghosted)", sockfd); 1192 | return 0; 1193 | } 1194 | } 1195 | 1196 | #ifdef SO_REUSEPORT 1197 | /* Multi mode? Set socket options. */ 1198 | if( multi_mode == TRUE ) 1199 | { 1200 | int optval = 1; 1201 | if( setsockopt(sockfd, 1202 | SOL_SOCKET, 1203 | SO_REUSEPORT, 1204 | &optval, 1205 | sizeof(optval)) < 0 ) 1206 | { 1207 | U(); 1208 | DEBUG("do_bind(%d, ...) => -1 (no multi?)", sockfd); 1209 | return -1; 1210 | } 1211 | 1212 | DEBUG("Multi mode enabled."); 1213 | } 1214 | #endif 1215 | 1216 | /* Try a real bind. */ 1217 | info = alloc_info(BOUND); 1218 | if( info == NULL ) 1219 | { 1220 | U(); 1221 | DEBUG("do_bind(%d, ...) => -1 (alloc error?)", sockfd); 1222 | return -1; 1223 | } 1224 | rval = libc.bind(sockfd, addr, addrlen); 1225 | if( rval < 0 ) 1226 | { 1227 | dec_ref(info); 1228 | U(); 1229 | DEBUG("do_bind(%d, ...) => %d (error)", sockfd, rval); 1230 | return rval; 1231 | } 1232 | 1233 | /* Ensure that this socket is non-blocking, 1234 | * this is because we override the behavior 1235 | * for accept() and we require non-blocking 1236 | * behavior. We deal with the consequences. */ 1237 | rval = fcntl(sockfd, F_SETFL, O_NONBLOCK); 1238 | if( rval < 0 ) 1239 | { 1240 | dec_ref(info); 1241 | U(); 1242 | DEBUG("do_bind(%d, ...) => %d (fcntl error)", sockfd, rval); 1243 | return -1; 1244 | } 1245 | 1246 | /* Save a refresh bound socket info. */ 1247 | info->bound.stub_listened = 0; 1248 | info->bound.real_listened = 0; 1249 | info->bound.addr = (struct sockaddr*)malloc(addrlen); 1250 | info->bound.addrlen = addrlen; 1251 | memcpy((void*)info->bound.addr, (void*)addr, addrlen); 1252 | fd_save(sockfd, info); 1253 | 1254 | /* Success. */ 1255 | U(); 1256 | DEBUG("do_bind(%d, ...) => %d", sockfd, rval); 1257 | return rval; 1258 | } 1259 | 1260 | static int 1261 | do_listen(int sockfd, int backlog) 1262 | { 1263 | int rval = -1; 1264 | fdinfo_t *info = NULL; 1265 | 1266 | if( sockfd < 0 ) 1267 | { 1268 | errno = EINVAL; 1269 | return -1; 1270 | } 1271 | 1272 | DEBUG("do_listen(%d, ...) ...", sockfd); 1273 | L(); 1274 | info = fd_lookup(sockfd); 1275 | if( info == NULL || info->type != BOUND ) 1276 | { 1277 | U(); 1278 | DEBUG("do_listen(%d, %d) => -1 (not BOUND)", sockfd, backlog); 1279 | errno = EINVAL; 1280 | return -1; 1281 | } 1282 | 1283 | /* Check if we can short-circuit this. */ 1284 | if( info->bound.real_listened ) 1285 | { 1286 | info->bound.stub_listened = 1; 1287 | U(); 1288 | DEBUG("do_listen(%d, %d) => 0 (stub)", sockfd, backlog); 1289 | return 0; 1290 | } 1291 | 1292 | /* Can we really call listen() ? */ 1293 | if( is_exiting == TRUE ) 1294 | { 1295 | info->bound.stub_listened = 1; 1296 | U(); 1297 | DEBUG("do_listen(%d, %d) => 0 (is_exiting)", sockfd, backlog); 1298 | return 0; 1299 | } 1300 | 1301 | /* We largely ignore the backlog parameter. People 1302 | * don't really use sensible values here for the most 1303 | * part. Hopefully (as is default on some systems), 1304 | * tcp syn cookies are enabled, and there's no real 1305 | * limit for this queue and this parameter is silently 1306 | * ignored. If not, then we use the largest value we 1307 | * can sensibly use. */ 1308 | (void)backlog; 1309 | rval = libc.listen(sockfd, SOMAXCONN); 1310 | if( rval < 0 ) 1311 | { 1312 | U(); 1313 | DEBUG("do_listen(%d, %d) => %d", sockfd, backlog, rval); 1314 | return rval; 1315 | } 1316 | 1317 | /* We're done. */ 1318 | info->bound.real_listened = 1; 1319 | info->bound.stub_listened = 1; 1320 | U(); 1321 | DEBUG("do_listen(%d, %d) => %d", sockfd, backlog, rval); 1322 | return rval; 1323 | } 1324 | 1325 | static int 1326 | do_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) 1327 | { 1328 | int rval = -1; 1329 | fdinfo_t *info = NULL; 1330 | 1331 | if( sockfd < 0 ) 1332 | { 1333 | errno = EINVAL; 1334 | return -1; 1335 | } 1336 | 1337 | DEBUG("do_accept4(%d, ...) ...", sockfd); 1338 | L(); 1339 | info = fd_lookup(sockfd); 1340 | if( info == NULL || (info->type != BOUND && info->type != DUMMY) ) 1341 | { 1342 | U(); 1343 | /* Should return an error. */ 1344 | rval = libc.accept4(sockfd, addr, addrlen, flags); 1345 | DEBUG("do_accept4(%d, ...) => %d (no info)", sockfd, rval); 1346 | return rval; 1347 | } 1348 | 1349 | /* Check that they've called listen. */ 1350 | if( info->type == BOUND && !info->bound.stub_listened ) 1351 | { 1352 | U(); 1353 | DEBUG("do_accept4(%d, ...) => -1 (not listened)", sockfd); 1354 | errno = EINVAL; 1355 | return -1; 1356 | } 1357 | 1358 | /* Check if this is a dummy. 1359 | * There's no way that they should be calling accept(). 1360 | * The dummy FD will never trigger a poll, select, epoll, 1361 | * etc. So we just act as a socket with no clients does -- 1362 | * either return immediately or block forever. NOTE: We 1363 | * still return in case of EINTR or other suitable errors. */ 1364 | if( info->type == DUMMY && info->dummy.client >= 0 ) 1365 | { 1366 | rval = info->dummy.client; 1367 | info->dummy.client = -1; 1368 | U(); 1369 | DEBUG("do_accept4(%d, ...) => %d (dummy client)", sockfd, rval); 1370 | return rval; 1371 | } 1372 | 1373 | U(); 1374 | 1375 | if( !(flags & SOCK_NONBLOCK) ) 1376 | { 1377 | /* Wait for activity on the socket. */ 1378 | struct pollfd poll_info; 1379 | poll_info.fd = sockfd; 1380 | poll_info.events = POLLIN; 1381 | poll_info.revents = 0; 1382 | if( poll(&poll_info, 1, -1) < 0 ) 1383 | { 1384 | return -1; 1385 | } 1386 | } 1387 | 1388 | L(); 1389 | 1390 | /* Check our status. */ 1391 | if( is_exiting == TRUE ) 1392 | { 1393 | /* We've transitioned from not exiting 1394 | * to exiting in this period. This will 1395 | * circle around a return a dummy descriptor. */ 1396 | U(); 1397 | DEBUG("do_accept4(%d, ...) => -1 (interrupted)", sockfd); 1398 | errno = flags & SOCK_NONBLOCK ? EAGAIN : EINTR; 1399 | return -1; 1400 | } 1401 | 1402 | /* Do the accept for real. */ 1403 | fdinfo_t *new_info = alloc_info(TRACKED); 1404 | if( new_info == NULL ) 1405 | { 1406 | U(); 1407 | DEBUG("do_accept4(%d, ...) => -1 (alloc error?)", sockfd); 1408 | return -1; 1409 | } 1410 | inc_ref(info); 1411 | new_info->tracked.bound = info; 1412 | rval = libc.accept4(sockfd, addr, addrlen, flags); 1413 | 1414 | if( rval >= 0 ) 1415 | { 1416 | /* Save the reference to the socket. */ 1417 | fd_save(rval, new_info); 1418 | } 1419 | else 1420 | { 1421 | /* An error occured, nothing to track. */ 1422 | dec_ref(new_info); 1423 | } 1424 | 1425 | U(); 1426 | DEBUG("do_accept4(%d, ...) => %d (tracked %d) %s", 1427 | sockfd, rval, total_tracked, 1428 | rval == -1 ? strerror(errno) : ""); 1429 | return rval; 1430 | } 1431 | 1432 | static int 1433 | do_accept4_retry(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) 1434 | { 1435 | while (1) 1436 | { 1437 | int rval = do_accept4(sockfd, addr, addrlen, flags); 1438 | if( rval < 0 && (errno == EAGAIN || errno == EINTR) ) 1439 | { 1440 | /* Signal interrupted the system call. 1441 | * Many programs cannot handle this cleanly, 1442 | * (hence why they are using huptime). So we 1443 | * simply absorb this error and continue. */ 1444 | if( (flags & SOCK_NONBLOCK) && errno == EAGAIN ) 1445 | { 1446 | return rval; 1447 | } 1448 | continue; 1449 | } 1450 | 1451 | /* Otherwise, give the error back. */ 1452 | return rval; 1453 | } 1454 | } 1455 | 1456 | static int 1457 | do_accept_retry(int sockfd, struct sockaddr *addr, socklen_t *addrlen) 1458 | { 1459 | return do_accept4_retry(sockfd, addr, addrlen, 0); 1460 | } 1461 | 1462 | static void 1463 | do_exit(int status) 1464 | { 1465 | if( revive_mode == TRUE ) 1466 | { 1467 | DEBUG("Reviving..."); 1468 | impl_exec(); 1469 | } 1470 | 1471 | libc.exit(status); 1472 | } 1473 | 1474 | static pid_t 1475 | do_wait(void *status) 1476 | { 1477 | pid_t rval = libc.wait(status); 1478 | L(); 1479 | impl_exit_check(); 1480 | U(); 1481 | return rval; 1482 | } 1483 | 1484 | static pid_t 1485 | do_waitpid(pid_t pid, int *status, int options) 1486 | { 1487 | pid_t rval = libc.waitpid(pid, status, options); 1488 | L(); 1489 | impl_exit_check(); 1490 | U(); 1491 | return rval; 1492 | } 1493 | 1494 | static long 1495 | do_syscall(long number, long a1, long a2, long a3, long a4, long a5, long a6) 1496 | { 1497 | /* This is very annoying. 1498 | * Unfortunately, it looks like uv in nodejs uses 1499 | * the syscall() function to directly call accept4. 1500 | * Why? I don't know. 1501 | * Anyways, this function is used internally within 1502 | * libc, but we won't intercept any of those calls. 1503 | * So performance isn't a critical concern here, but 1504 | * we need to intercept syscall() for node.js. */ 1505 | 1506 | if( unlikely(number == SYS_accept4) ) 1507 | { 1508 | return do_accept4((int)a1, (struct sockaddr*)a2, (socklen_t*)a3, (int)a4); 1509 | } 1510 | 1511 | return libc.syscall(number, a1, a2, a3, a4, a5, a6); 1512 | } 1513 | 1514 | static int 1515 | do_epoll_create1(int flags) 1516 | { 1517 | int rval = libc.epoll_create1(flags); 1518 | if( rval >= 0 ) 1519 | { 1520 | fdinfo_t* info = alloc_info(EPOLL); 1521 | fd_save(rval, info); 1522 | } 1523 | return rval; 1524 | } 1525 | 1526 | static int 1527 | do_epoll_create(int size) 1528 | { 1529 | return do_epoll_create1(0); 1530 | } 1531 | 1532 | funcs_t impl = 1533 | { 1534 | .bind = do_bind, 1535 | .listen = do_listen, 1536 | .accept = do_accept_retry, 1537 | .accept4 = do_accept4_retry, 1538 | .close = do_close, 1539 | .fork = do_fork, 1540 | .dup = do_dup, 1541 | .dup2 = do_dup2, 1542 | .dup3 = do_dup3, 1543 | .exit = do_exit, 1544 | .wait = do_wait, 1545 | .waitpid = do_waitpid, 1546 | .syscall = (syscall_t)do_syscall, 1547 | .epoll_create = do_epoll_create, 1548 | .epoll_create1 = do_epoll_create1, 1549 | }; 1550 | funcs_t libc; 1551 | -------------------------------------------------------------------------------- /src/impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * impl.h 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | #ifndef HUPTIME_IMPL_H 23 | #define HUPTIME_IMPL_H 24 | 25 | #include "funcs.h" 26 | 27 | /* Our initialization routine. */ 28 | extern void impl_init(); 29 | 30 | /* The internal impementations. */ 31 | extern funcs_t impl; 32 | extern funcs_t libc; 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/stubs.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * stubs.cc 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | extern "C" { 23 | #include "stubs.h" 24 | #include "impl.h" 25 | 26 | #include 27 | #include 28 | #include 29 | } 30 | 31 | template 32 | static FUNC_T 33 | get_libc_function(const char* name, FUNC_T def) 34 | { 35 | char *error; 36 | FUNC_T result; 37 | 38 | /* Clear last error (if any). */ 39 | dlerror(); 40 | 41 | /* Try to get the symbol. */ 42 | result = (FUNC_T)dlsym(RTLD_NEXT, name); 43 | error = dlerror(); 44 | if( result == NULL || error != NULL ) 45 | { 46 | fprintf(stderr, "dlsym(RTLD_NEXT, \"%s\") failed: %s", name, error); 47 | result = def; 48 | } 49 | 50 | return result; 51 | } 52 | 53 | static int initialized = 0; 54 | 55 | static void __attribute__((constructor)) 56 | setup(void) 57 | { 58 | #define likely(x) __builtin_expect (!!(x), 1) 59 | if( likely(initialized) ) 60 | return; 61 | 62 | initialized = 1; 63 | 64 | #define GET_LIBC_FUNCTION(_name) \ 65 | libc._name = get_libc_function<_name ## _t>(# _name, &_name) 66 | 67 | GET_LIBC_FUNCTION(bind); 68 | GET_LIBC_FUNCTION(listen); 69 | GET_LIBC_FUNCTION(accept); 70 | GET_LIBC_FUNCTION(accept4); 71 | GET_LIBC_FUNCTION(close); 72 | GET_LIBC_FUNCTION(fork); 73 | GET_LIBC_FUNCTION(dup); 74 | GET_LIBC_FUNCTION(dup2); 75 | GET_LIBC_FUNCTION(dup3); 76 | GET_LIBC_FUNCTION(exit); 77 | GET_LIBC_FUNCTION(wait); 78 | GET_LIBC_FUNCTION(waitpid); 79 | GET_LIBC_FUNCTION(syscall); 80 | GET_LIBC_FUNCTION(epoll_create); 81 | GET_LIBC_FUNCTION(epoll_create1); 82 | #undef GET_LIBC_FUNCTION 83 | 84 | impl_init(); 85 | } 86 | 87 | extern "C" 88 | { 89 | 90 | static int 91 | stub_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) 92 | { 93 | return impl.bind(sockfd, addr, addrlen); 94 | } 95 | 96 | static int 97 | stub_listen(int sockfd, int backlog) 98 | { 99 | return impl.listen(sockfd, backlog); 100 | } 101 | 102 | static int 103 | stub_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) 104 | { 105 | return impl.accept(sockfd, addr, addrlen); 106 | } 107 | 108 | static int 109 | stub_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) 110 | { 111 | return impl.accept4(sockfd, addr, addrlen, flags); 112 | } 113 | 114 | static int 115 | stub_close(int fd) 116 | { 117 | return impl.close(fd); 118 | } 119 | 120 | static pid_t 121 | stub_fork() 122 | { 123 | return impl.fork(); 124 | } 125 | 126 | static int 127 | stub_dup(int fd) 128 | { 129 | return impl.dup(fd); 130 | } 131 | 132 | static int 133 | stub_dup2(int fd, int fd2) 134 | { 135 | return impl.dup2(fd, fd2); 136 | } 137 | 138 | static int 139 | stub_dup3(int fd, int fd2, int flags) 140 | { 141 | return impl.dup3(fd, fd2, flags); 142 | } 143 | 144 | static void 145 | stub_exit(int status) 146 | { 147 | impl.exit(status); 148 | } 149 | 150 | static pid_t 151 | stub_wait(void *status) 152 | { 153 | return impl.wait(status); 154 | } 155 | 156 | static pid_t 157 | stub_waitpid(pid_t pid, int *status, int options) 158 | { 159 | return impl.waitpid(pid, status, options); 160 | } 161 | 162 | static int 163 | stub_syscall(int number, long a1, long a2, long a3, long a4, long a5, long a6) 164 | { 165 | return impl.syscall(number, a1, a2, a3, a4, a5, a6); 166 | } 167 | 168 | static int 169 | stub_epoll_create(int size) 170 | { 171 | return impl.epoll_create(size); 172 | } 173 | 174 | static int 175 | stub_epoll_create1(int flags) 176 | { 177 | return impl.epoll_create1(flags); 178 | } 179 | 180 | /* Exports name as aliasname in .dynsym. */ 181 | #define PUBLIC_ALIAS(name, aliasname) \ 182 | typeof(name) aliasname __attribute__ ((alias (#name))) \ 183 | __attribute__ ((visibility ("default"))); 184 | 185 | /* Exports stub_ ##name as name@version. */ 186 | #define SYMBOL_VERSION(name, version, version_ident) \ 187 | PUBLIC_ALIAS(stub_ ## name, stub_ ## name ## _ ## version_ident); \ 188 | asm(".symver stub_" #name "_" #version_ident ", " #name "@" version); 189 | 190 | /* Exports stub_ ##name as name@@ (i.e., the unversioned symbol for name). */ 191 | #define GLIBC_DEFAULT(name) \ 192 | SYMBOL_VERSION(name, "@", default_) 193 | 194 | /* Exports stub_ ##name as name@@GLIBC_MAJOR.MINOR.PATCH. */ 195 | #define GLIBC_VERSION(name, major, minor) \ 196 | SYMBOL_VERSION(name, "GLIBC_" # major "." # minor, \ 197 | glibc_ ## major ## minor) 198 | #define GLIBC_VERSION2(name, major, minor, patch) \ 199 | SYMBOL_VERSION(name, "GLIBC_" # major "." # minor "." # patch, \ 200 | glibc_ ## major ## minor ## patch) 201 | 202 | GLIBC_DEFAULT(bind) 203 | GLIBC_VERSION2(bind, 2, 2, 5) 204 | GLIBC_DEFAULT(listen) 205 | GLIBC_VERSION2(listen, 2, 2, 5) 206 | GLIBC_DEFAULT(accept) 207 | GLIBC_VERSION2(accept, 2, 2, 5) 208 | GLIBC_DEFAULT(accept4) 209 | GLIBC_VERSION2(accept4, 2, 2, 5) 210 | GLIBC_DEFAULT(close) 211 | GLIBC_VERSION2(close, 2, 2, 5) 212 | GLIBC_DEFAULT(fork) 213 | GLIBC_VERSION2(fork, 2, 2, 5) 214 | GLIBC_DEFAULT(dup) 215 | GLIBC_VERSION2(dup, 2, 2, 5) 216 | GLIBC_DEFAULT(dup2) 217 | GLIBC_VERSION2(dup2, 2, 2, 5) 218 | GLIBC_DEFAULT(dup3) 219 | GLIBC_VERSION2(dup3, 2, 2, 5) 220 | GLIBC_DEFAULT(exit) 221 | GLIBC_VERSION(exit, 2, 0) 222 | GLIBC_DEFAULT(wait) 223 | GLIBC_VERSION2(wait, 2, 2, 5) 224 | GLIBC_DEFAULT(waitpid) 225 | GLIBC_VERSION2(waitpid, 2, 2, 5) 226 | GLIBC_DEFAULT(syscall) 227 | GLIBC_VERSION2(syscall, 2, 2, 5) 228 | GLIBC_DEFAULT(epoll_create) 229 | GLIBC_VERSION2(epoll_create, 2, 3, 2) 230 | GLIBC_DEFAULT(epoll_create1) 231 | GLIBC_VERSION(epoll_create1, 2, 9) 232 | 233 | } 234 | -------------------------------------------------------------------------------- /src/stubs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * stubs.h 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | #ifndef HUPTIME_STUBS_H 23 | #define HUPTIME_STUBS_H 24 | 25 | #include "funcs.h" 26 | 27 | /* The libc implementations. */ 28 | extern funcs_t libc; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/stubs.map: -------------------------------------------------------------------------------- 1 | /* 2 | * stubs.map 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | GLIBC_2.2.5 { 23 | global: 24 | bind; 25 | listen; 26 | accept; 27 | accept4; 28 | close; 29 | fork; 30 | dup; 31 | dup2; 32 | dup3; 33 | syscall; 34 | local: *; 35 | }; 36 | 37 | GLIBC_2.3.2 { 38 | global: 39 | epoll_create; 40 | local: *; 41 | }; 42 | 43 | GLIBC_2.0 { 44 | global: 45 | exit; 46 | local: *; 47 | }; 48 | 49 | GLIBC_2.9 { 50 | global: 51 | epoll_create1; 52 | local: *; 53 | }; 54 | -------------------------------------------------------------------------------- /src/utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * utils.c 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | #include "utils.h" 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #define INITIAL_BUF_SIZE 4096 35 | 36 | const char** 37 | read_nul_sep(const char* filename) 38 | { 39 | char *buf = NULL; 40 | char *newbuf = NULL; 41 | int buflen = 0; 42 | int count = 0; 43 | int size = 0; 44 | int i = 0; 45 | char **results; 46 | int fd = open(filename, O_RDONLY); 47 | 48 | buf = (char*)malloc(INITIAL_BUF_SIZE); 49 | size = INITIAL_BUF_SIZE; 50 | if( buf == NULL ) 51 | { 52 | close(fd); 53 | return NULL; 54 | } 55 | 56 | /* Read the full file. */ 57 | while( 1 ) 58 | { 59 | int r = 0; 60 | 61 | if( size-buflen == 0 ) 62 | { 63 | buf = (char*)realloc(buf, size*2); 64 | size = size*2; 65 | if( buf == NULL ) 66 | { 67 | close(fd); 68 | return NULL; 69 | } 70 | } 71 | 72 | r = read(fd, &buf[buflen], size-buflen); 73 | if( r < 0 ) 74 | { 75 | close(fd); 76 | free(buf); 77 | return NULL; 78 | } 79 | 80 | if( r == 0 ) 81 | { 82 | close(fd); 83 | break; 84 | } 85 | 86 | buflen += r; 87 | } 88 | 89 | /* Count nuls. */ 90 | for( i = 0; i < buflen; i += 1 ) 91 | { 92 | if( buf[i] == '\0' ) 93 | { 94 | count += 1; 95 | } 96 | } 97 | if( buf[i-1] != '\0' ) 98 | { 99 | count += 1; 100 | } 101 | 102 | /* Allocate our strings. */ 103 | results = (char**)malloc(sizeof(char*)*(count+1) + (buflen+1)); 104 | for( i = count; i < count + 1; i += 1 ) 105 | { 106 | results[i] = NULL; 107 | } 108 | 109 | /* Copy buffer in and reset the pointer. */ 110 | newbuf = ((char*)results) + sizeof(char*)*(count+1); 111 | memcpy(newbuf, buf, buflen); 112 | newbuf[buflen] = '\0'; 113 | free(buf); 114 | 115 | /* Point the strings into the new buffer. */ 116 | count = 0; 117 | for( i = 0; i < buflen; ) 118 | { 119 | results[count++] = &newbuf[i]; 120 | for( ; i < buflen && newbuf[i] != '\0'; i += 1 ); 121 | i += 1; 122 | } 123 | 124 | return (const char**)results; 125 | } 126 | 127 | const char* 128 | read_link(const char* filename) 129 | { 130 | char buf[PATH_MAX+1]; 131 | size_t r = readlink(filename, buf, PATH_MAX+1); 132 | if( r == (size_t)-1 ) 133 | { 134 | return NULL; 135 | } 136 | buf[r] = '\0'; 137 | return (const char*)strdup(buf); 138 | } 139 | 140 | pid_t* 141 | get_tasks(void) 142 | { 143 | int count = 0; 144 | int size = 3; 145 | pid_t *buffer = malloc(sizeof(pid_t) * size); 146 | DIR *dp = NULL; 147 | struct dirent *ep = NULL; 148 | buffer[0] = (pid_t)-1; 149 | 150 | dp = opendir("/proc/self/task"); 151 | if( dp == NULL ) 152 | { 153 | fprintf(stderr, "Failed to fetch tasks?\n"); 154 | return buffer; 155 | } 156 | 157 | while( (ep = readdir(dp)) != NULL ) 158 | { 159 | if( ep->d_name[0] == '.' || ep->d_name[0] == '\0' ) 160 | { 161 | continue; 162 | } 163 | long task_id = strtol(ep->d_name, NULL, 10); 164 | if( count+1 >= size ) 165 | { 166 | size = size * 2; 167 | buffer = realloc(buffer, sizeof(long) * size); 168 | } 169 | buffer[count] = (pid_t)task_id; 170 | count += 1; 171 | buffer[count] = (pid_t)-1; 172 | } 173 | 174 | return buffer; 175 | } 176 | -------------------------------------------------------------------------------- /src/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * utils.h 3 | * 4 | * Copyright 2013 Adin Scannell , all rights reserved. 5 | * 6 | * This file is part of Huptime. 7 | * 8 | * Huptime is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Huptime is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Huptime. If not, see . 20 | */ 21 | 22 | #ifndef HUPTIME_UTILS_H 23 | #define HUPTIME_UTILS_H 24 | 25 | #include 26 | #include 27 | 28 | const char** read_nul_sep(const char* filename); 29 | const char* read_link(const char* filename); 30 | 31 | pid_t* get_tasks(void); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /test/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Adin Scannell , all rights reserved. 3 | # 4 | # This file is part of Huptime. 5 | # 6 | # Huptime is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # Huptime is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with Huptime. If not, see . 18 | # 19 | -------------------------------------------------------------------------------- /test/client.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Adin Scannell , all rights reserved. 3 | # 4 | # This file is part of Huptime. 5 | # 6 | # Huptime is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # Huptime is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with Huptime. If not, see . 18 | # 19 | """ 20 | The client. 21 | 22 | We implement a simple client that talks to the 23 | simple server protocol implemented by servers.py. 24 | """ 25 | 26 | import sys 27 | import socket 28 | import unittest 29 | import threading 30 | import traceback 31 | 32 | import servers 33 | 34 | DEFAULT_CLIENTS = 10 35 | 36 | class Client(object): 37 | 38 | def __init__(self, host=None, port=None): 39 | super(Client, self).__init__() 40 | if host is None: 41 | host = "localhost" 42 | if port is None: 43 | port = servers.DEFAULT_PORT 44 | self._sock = socket.socket() 45 | self._sock.connect((host, port)) 46 | 47 | def cookie(self): 48 | self._sock.send("cookie") 49 | server_cookie = self._sock.recv(1024) 50 | return server_cookie 51 | 52 | def ping(self): 53 | self._sock.send("ping") 54 | assert self._sock.recv(1024) == "pong" 55 | 56 | def drop(self): 57 | self._sock.send("drop") 58 | assert self._sock.recv(1024) == "okay" 59 | self._sock.close() 60 | 61 | class ClientThread(threading.Thread): 62 | 63 | def __init__(self, **kwargs): 64 | super(ClientThread, self).__init__() 65 | self._client = Client(**kwargs) 66 | self._cookie = None 67 | self._exception = None 68 | self.daemon = True 69 | self.start() 70 | 71 | def run(self): 72 | try: 73 | self._client.ping() 74 | self._cookie = self._client.cookie() 75 | self._client.drop() 76 | except Exception as e: 77 | self._exception = e 78 | 79 | def verify(self, valid_cookies): 80 | self.join() 81 | if self._exception: 82 | raise self._exception 83 | assert self._cookie in valid_cookies 84 | 85 | class Clients(object): 86 | 87 | def __init__(self, N=None, host=None, port=None): 88 | super(Clients, self).__init__() 89 | if N is None: 90 | N = DEFAULT_CLIENTS 91 | self._clients = map(lambda x: ClientThread(host=host, port=port), range(N)) 92 | 93 | def verify(self, valid_cookies): 94 | for c in self._clients: 95 | c.verify(valid_cookies) 96 | -------------------------------------------------------------------------------- /test/harness.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Adin Scannell , all rights reserved. 3 | # 4 | # This file is part of Huptime. 5 | # 6 | # Huptime is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # Huptime is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with Huptime. If not, see . 18 | # 19 | """ 20 | Harness. 21 | 22 | A generic test harness. 23 | """ 24 | 25 | import sys 26 | import unittest 27 | import signal 28 | import os 29 | import time 30 | import uuid 31 | import tempfile 32 | import threading 33 | import traceback 34 | import re 35 | 36 | import proxy 37 | import client 38 | 39 | def proxy_starter(proxy, host=None, port=None, backlog=None): 40 | def fn(): 41 | proxy._wait() 42 | proxy.bind(host=host, port=port) 43 | proxy.listen(backlog=backlog) 44 | proxy._call("run") 45 | return fn 46 | 47 | class Harness(object): 48 | 49 | def __init__(self, mode_class, server_class, cookie=None, **kwargs): 50 | 51 | super(Harness, self).__init__() 52 | self._cookie_file = tempfile.NamedTemporaryFile() 53 | self._set_cookie(cookie) 54 | self._mode = mode_class() 55 | self._kwargs = kwargs 56 | self._proxy = proxy.ProxyClient( 57 | self._mode, 58 | server_class, 59 | self._cookie_file.name) 60 | 61 | # Run the server normally. 62 | proxy_starter(self._proxy, **self._kwargs)() 63 | 64 | def __getattr__(self, attr): 65 | return getattr(self._proxy, attr) 66 | 67 | def _set_cookie(self, cookie=None): 68 | if cookie is None: 69 | cookie = str(uuid.uuid4()) 70 | self._cookie = cookie 71 | self._cookie_file.truncate(0) 72 | self._cookie_file.seek(0, 0) 73 | self._cookie_file.write(self._cookie) 74 | self._cookie_file.flush() 75 | 76 | def clients(self, **kwargs): 77 | return client.Clients( 78 | host=self._kwargs.get("host"), 79 | port=self._kwargs.get("port"), 80 | **kwargs) 81 | 82 | def restart(self, cookie=None): 83 | # Connect clients. 84 | old_clients = self.clients() 85 | 86 | # Reset the cookie. 87 | old_cookie = self._cookie 88 | self._set_cookie(cookie) 89 | 90 | # Hook to fetch current pid. 91 | def getpid(): 92 | return self._proxy.getpid() 93 | orig_pid = getpid() 94 | 95 | # Grab the current pid, and hit 96 | # the server with a restart signal. 97 | sys.stderr.write("harness: restart\n") 98 | self._proxy.restart() 99 | 100 | # Whenever it's ready, restart the server. 101 | start_thread = threading.Thread( 102 | target=proxy_starter(self._proxy, **self._kwargs)) 103 | start_thread.daemon = True 104 | start_thread.start() 105 | 106 | # Call into the mode to validate. 107 | self._mode.check_restart(orig_pid, getpid, start_thread) 108 | 109 | # Connect new clients. 110 | new_clients = self.clients() 111 | 112 | # Check behavior according to the mode. 113 | # NOTE: The expected behaviour here is that 114 | # the mode will drop the all clients in order 115 | # to assert that things are fully working. 116 | self._mode.check_clients( 117 | orig_pid, getpid, start_thread, 118 | old_clients, new_clients, 119 | old_cookie, self._cookie) 120 | -------------------------------------------------------------------------------- /test/modes.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Adin Scannell , all rights reserved. 3 | # 4 | # This file is part of Huptime. 5 | # 6 | # Huptime is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # Huptime is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with Huptime. If not, see . 18 | # 19 | """ 20 | Modes. 21 | 22 | These are various modes of operation. 23 | """ 24 | 25 | import sys 26 | import os 27 | import time 28 | import subprocess 29 | import threading 30 | 31 | class Mode(object): 32 | 33 | # Before each RPC call, we check 34 | # for something in the mode. 35 | # This is expected to do something, 36 | # and the validation checks should 37 | # ensure that the system is still 38 | # in the expected state. 39 | def pre(self, name, server): 40 | if hasattr(self, 'pre_%s' % name): 41 | getattr(self, 'pre_%s' % name)(server) 42 | 43 | def post(self, name, server): 44 | if hasattr(self, 'post_%s' % name): 45 | getattr(self, 'post_%s' % name)(server) 46 | 47 | def _run(self, cmdline, reap=True, **kwargs): 48 | args = self._args() 49 | cmd = [ 50 | os.path.abspath( 51 | os.path.join( 52 | os.path.dirname(__file__), 53 | "..", 54 | "bin", 55 | "huptime")), 56 | "--debug" 57 | ] 58 | cmd.extend(args) 59 | cmd.extend(cmdline) 60 | sys.stderr.write("exec: %s\n" % " ".join(cmd)) 61 | return subprocess.Popen(cmd, **kwargs) 62 | 63 | def _args(self): 64 | raise NotImplementedError() 65 | 66 | def start(self, cmdline, **kwargs): 67 | proc = self._run(cmdline, **kwargs) 68 | t = threading.Thread(target=lambda: proc.wait()) 69 | t.daemon = True 70 | t.start() 71 | 72 | def stop(self, cmdline): 73 | proc = self._run(["--stop"] + cmdline) 74 | proc.wait() 75 | 76 | def restart(self, cmdline): 77 | proc = self._run(["--restart"] + cmdline) 78 | proc.wait() 79 | 80 | def status(self, cmdline): 81 | proc = self._run( 82 | ["--restart"] + cmdline, 83 | stdout=subprocess.PIPE) 84 | proc.wait() 85 | if proc.returncode != 0: 86 | return [] 87 | else: 88 | return map( 89 | lambda x: x.strip(), 90 | proc.stdout.readlines()) 91 | 92 | def check_clients(self, 93 | start_thread, 94 | old_clients, new_clients, 95 | old_cookie, new_cookie): 96 | raise NotImplementedError() 97 | 98 | def check_restart(self, start_thread): 99 | raise NotImplementedError() 100 | 101 | def __str__(self): 102 | return self.__class__.__name__ 103 | 104 | class Fork(Mode): 105 | 106 | def _args(self): 107 | return ["--fork"] 108 | 109 | def check_restart(self, pid, getpid, start_thread): 110 | # Should come up immediately. 111 | sys.stderr.write("%s: waiting for startup...\n" % self) 112 | start_thread.join() 113 | 114 | # Ensure that it's a new pid. 115 | assert pid != getpid() 116 | 117 | def check_clients(self, 118 | pid, getpid, start_thread, 119 | old_clients, new_clients, 120 | old_cookie, new_cookie): 121 | # All the new clients should be responsive. 122 | sys.stderr.write("%s: checking new clients...\n" % self) 123 | new_clients.verify([new_cookie]) 124 | 125 | # Drop all the old clients. 126 | old_clients.verify([old_cookie, new_cookie]) 127 | 128 | class Exec(Mode): 129 | 130 | def _args(self): 131 | return ["--exec"] 132 | 133 | def check_restart(self, pid, getpid, start_thread): 134 | # Ensure it's not started yet. 135 | assert start_thread.isAlive() 136 | 137 | def check_clients(self, 138 | pid, getpid, start_thread, 139 | old_clients, new_clients, 140 | old_cookie, new_cookie): 141 | 142 | # All old clients should keep working. 143 | sys.stderr.write("%s: checking old clients...\n" % self) 144 | old_clients.verify([old_cookie, new_cookie]) 145 | 146 | # Wait for startup (blocking). 147 | sys.stderr.write("%s: waiting for startup...\n" % self) 148 | start_thread.join() 149 | 150 | # Ensure it's still the same pid. 151 | assert pid == getpid() 152 | 153 | # All the new clients should now be responsive. 154 | sys.stderr.write("%s: checking new clients...\n" % self) 155 | new_clients.verify([new_cookie]) 156 | 157 | MODES = [ 158 | Fork, 159 | Exec, 160 | ] 161 | -------------------------------------------------------------------------------- /test/proxy.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Adin Scannell , all rights reserved. 3 | # 4 | # This file is part of Huptime. 5 | # 6 | # Huptime is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # Huptime is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with Huptime. If not, see . 18 | # 19 | """ 20 | Proxy. 21 | 22 | This proxy is used to drive the server classes 23 | through the huptime binary, but still have them 24 | accessible from the test harness. 25 | """ 26 | 27 | import os 28 | import sys 29 | import uuid 30 | import subprocess 31 | import threading 32 | import traceback 33 | import pickle 34 | 35 | import modes 36 | import servers 37 | 38 | class ProxyServer(object): 39 | 40 | def __init__(self, mode_name, server_name, cookie_file): 41 | cookie = open(cookie_file, 'r').read() 42 | self._mode = getattr(modes, mode_name)() 43 | self._server = getattr(servers, server_name)(cookie) 44 | self._cond = threading.Condition() 45 | 46 | def run(self): 47 | # Open our pipes. 48 | in_pipe = os.fdopen(os.dup(0), 'r') 49 | out_pipe = os.fdopen(os.dup(1), 'w') 50 | devnull = open("/dev/null", 'r') 51 | os.dup2(devnull.fileno(), 0) 52 | devnull.close() 53 | os.dup2(2, 1) 54 | 55 | # Dump our startup message. 56 | robj = { 57 | "id": None, 58 | "result": None 59 | } 60 | out_pipe.write(pickle.dumps(robj)) 61 | out_pipe.flush() 62 | sys.stderr.write("proxy %d: started.\n" % os.getpid()) 63 | 64 | # Get the call from the other side. 65 | while True: 66 | try: 67 | obj = pickle.load(in_pipe) 68 | sys.stderr.write("proxy %d: <- %s\n" % (os.getpid(), obj)) 69 | except: 70 | # We're done! 71 | break 72 | 73 | def closure(obj, out_pipe): 74 | def fn(): 75 | self._process(obj, out_pipe) 76 | return fn 77 | 78 | t = threading.Thread(target=closure(obj, out_pipe)) 79 | t.start() 80 | 81 | def _process(self, obj, out): 82 | uniq = obj.get("id") 83 | try: 84 | if not "method_name" in obj: 85 | raise ValueError("no method_name?") 86 | method_name = obj["method_name"] 87 | args = obj.get("args") 88 | kwargs = obj.get("kwargs") 89 | if method_name: 90 | method = getattr(self._server, method_name) 91 | self._mode.pre(method_name, self._server) 92 | result = method(*args, **kwargs) 93 | self._mode.post(method_name, self._server) 94 | else: 95 | result = None 96 | robj = { 97 | "id": uniq, 98 | "result": result 99 | } 100 | except Exception as e: 101 | traceback.print_exc() 102 | robj = { 103 | "id": uniq, 104 | "exception": e 105 | } 106 | 107 | self._cond.acquire() 108 | try: 109 | sys.stderr.write("proxy %d: -> %s\n" % (os.getpid(), robj)) 110 | out.write(pickle.dumps(robj)) 111 | out.flush() 112 | finally: 113 | self._cond.release() 114 | 115 | class ProxyClient(object): 116 | 117 | def __init__(self, mode, server_class, cookie_file): 118 | super(ProxyClient, self).__init__() 119 | self._mode = mode 120 | self._server_class = server_class 121 | self._cond = threading.Condition() 122 | self._results = {} 123 | self._cookie_file = cookie_file 124 | self._cmdline = [ 125 | "python", 126 | __file__, 127 | mode.__class__.__name__, 128 | server_class.__name__, 129 | self._cookie_file, 130 | ] 131 | 132 | r, w = os.pipe() 133 | self._out = os.fdopen(w, 'w') 134 | proc_in = os.fdopen(r, 'r') 135 | 136 | r, w = os.pipe() 137 | self._in = os.fdopen(r, 'r') 138 | proc_out = os.fdopen(w, 'w') 139 | 140 | self._mode.start( 141 | self._cmdline, 142 | stdin=proc_in, 143 | stdout=proc_out, 144 | close_fds=True) 145 | 146 | proc_in.close() 147 | proc_out.close() 148 | 149 | # Start the processing thread. 150 | t = threading.Thread(target=self._run) 151 | t.daemon = True 152 | t.start() 153 | 154 | def _call(self, method_name=None, args=None, kwargs=None): 155 | if args is None: 156 | args = [] 157 | if kwargs is None: 158 | kwargs = {} 159 | 160 | # Send the call to the other side. 161 | uniq = str(uuid.uuid4()) 162 | obj = { 163 | "id": uniq, 164 | "method_name": method_name, 165 | "args": args, 166 | "kwargs": kwargs 167 | } 168 | sys.stderr.write("proxy client: -> %s\n" % obj) 169 | self._out.write(pickle.dumps(obj)) 170 | self._out.flush() 171 | 172 | return uniq 173 | 174 | def _wait(self, uniq=None, method_name=None): 175 | # Wait for a result to appear. 176 | self._cond.acquire() 177 | try: 178 | while True: 179 | if uniq in self._results: 180 | res = self._results[uniq] 181 | del self._results[uniq] 182 | if "exception" in res: 183 | raise res["exception"] 184 | elif "result" in res: 185 | return res["result"] 186 | else: 187 | raise ValueError("no result?") 188 | sys.stderr.write("proxy client: waiting for %s (%s)...\n" % 189 | (uniq, method_name)) 190 | self._cond.wait() 191 | finally: 192 | self._cond.release() 193 | 194 | def _run(self): 195 | # Get the return from the other side. 196 | while True: 197 | try: 198 | obj = pickle.load(self._in) 199 | sys.stderr.write("proxy client: <- %s\n" % obj) 200 | except: 201 | # We're done! 202 | break 203 | self._cond.acquire() 204 | try: 205 | uniq = obj.get("id") 206 | self._results[uniq] = obj 207 | self._cond.notifyAll() 208 | finally: 209 | self._cond.release() 210 | 211 | def stop(self): 212 | self._mode.stop(self._cmdline) 213 | 214 | def restart(self): 215 | self._mode.restart(self._cmdline) 216 | 217 | def __getattr__(self, method_name): 218 | def _fn(*args, **kwargs): 219 | uniq = self._call(method_name, args, kwargs) 220 | return self._wait(uniq, method_name=method_name) 221 | return _fn 222 | 223 | if __name__ == "__main__": 224 | proxy = ProxyServer(*sys.argv[1:]) 225 | proxy.run() 226 | -------------------------------------------------------------------------------- /test/servers.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Adin Scannell , all rights reserved. 3 | # 4 | # This file is part of Huptime. 5 | # 6 | # Huptime is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # Huptime is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with Huptime. If not, see . 18 | # 19 | """ 20 | Servers. 21 | 22 | This file contains various server implementations, 23 | which can be created by different tests. 24 | """ 25 | 26 | import os 27 | import sys 28 | import socket 29 | import thread 30 | import threading 31 | import traceback 32 | import select 33 | import errno 34 | 35 | DEFAULT_HOST = "" 36 | DEFAULT_PORT = 7869 37 | DEFAULT_BACKLOG = 1 38 | DEFAULT_N = 8 39 | 40 | class Server(object): 41 | 42 | """ 43 | A generic server model. 44 | 45 | This server exposes a simple run() method, which 46 | will bind() and listen() to a socket, then accept() 47 | and and serve clients individually. 48 | """ 49 | 50 | def __init__(self, cookie): 51 | super(Server, self).__init__() 52 | self._sock = socket.socket() 53 | self._cookie = cookie 54 | self._cond = threading.Condition() 55 | self._clients = 0 56 | assert self._cookie 57 | 58 | def bind(self, host=None, port=None): 59 | if host is None: 60 | host = DEFAULT_HOST 61 | if port is None: 62 | port = DEFAULT_PORT 63 | sys.stderr.write("%s: bind()\n" % self) 64 | self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 65 | self._sock.bind((host, port)) 66 | 67 | def close(self): 68 | sys.stderr.write("%s: close()\n" % self) 69 | self._sock.close() 70 | 71 | def listen(self, backlog=None): 72 | if backlog is None: 73 | backlog = DEFAULT_BACKLOG 74 | sys.stderr.write("%s: listen()\n" % self) 75 | self._sock.listen(backlog) 76 | 77 | def accept(self): 78 | sys.stderr.write("%s: accept()\n" % self) 79 | client, _ = self._sock.accept() 80 | return client 81 | 82 | def getpid(self): 83 | sys.stderr.write("%s: getpid()\n" % self) 84 | return os.getpid() 85 | 86 | def handle(self, client): 87 | # This implements a very simple protocol that 88 | # allows us to test for a code "version" (by the 89 | # cookie at startup) and liveness (via ping). 90 | # The corresponding client code is available in 91 | # client.py. 92 | rval = False 93 | command = client.recv(1024) 94 | sys.stderr.write("%s: recv(%d) => %s\n" % 95 | (self, client.fileno(), command)) 96 | if not command: 97 | # Bad client? 98 | # Occassionally huptime will send back 99 | # fake clients during a restart to get 100 | # around race races. We handle this as 101 | # a serve would handle a bad client. 102 | client.close() 103 | return False 104 | 105 | # Ensure it's a valid command. 106 | assert command in ["cookie", "ping", "drop"] 107 | 108 | if command == "cookie": 109 | client.send(self._cookie) 110 | rval = True 111 | elif command == "ping": 112 | client.send("pong") 113 | rval = True 114 | elif command == "drop": 115 | client.send("okay") 116 | client.close() 117 | 118 | return rval 119 | 120 | def run(self): 121 | raise NotImplementedError() 122 | 123 | def __str__(self): 124 | return "%s %d.%d" % ( 125 | self.__class__.__name__, 126 | os.getpid(), 127 | thread.get_ident()) 128 | 129 | class SimpleServer(Server): 130 | 131 | def run(self): 132 | sys.stderr.write("%s: run()\n" % self) 133 | while True: 134 | client = self.accept() 135 | while self.handle(client): 136 | # Continue until finished. 137 | pass 138 | 139 | class EventServer(Server): 140 | 141 | def run(self): 142 | sys.stderr.write("%s: run()\n" % self) 143 | self._fdmap = {self._sock.fileno(): self._sock} 144 | while True: 145 | rfds, wfds, efds = select.select(self._fdmap.keys(), [], []) 146 | for fd in rfds: 147 | sock = self._fdmap.get(fd) 148 | if sock == self._sock: 149 | # Accept the client. 150 | client = self.accept() 151 | self._fdmap[client.fileno()] = client 152 | else: 153 | # Process the request. 154 | if not self.handle(sock): 155 | del self._fdmap[fd] 156 | 157 | class ThreadServer(Server): 158 | 159 | def run(self): 160 | sys.stderr.write("%s: run()\n" % self) 161 | while True: 162 | client = self.accept() 163 | # Fire a thread to handle it. 164 | def closure(c): 165 | def fn(): 166 | sys.stderr.write("%s: thread_start()\n" % self) 167 | try: 168 | while self.handle(c): 169 | pass 170 | except: 171 | traceback.print_exc() 172 | sys.stderr.write("%s: thread_exit()\n" % self) 173 | return fn 174 | t = threading.Thread(target=closure(client)) 175 | t.daemon = True 176 | t.start() 177 | 178 | class ProcessServer(Server): 179 | 180 | def __init__(self, *args, **kwargs): 181 | super(ProcessServer, self).__init__(*args, **kwargs) 182 | 183 | def run(self): 184 | sys.stderr.write("%s: run()\n" % self) 185 | while True: 186 | client = self.accept() 187 | pid = os.fork() 188 | if pid == 0: 189 | while self.handle(client): 190 | # Continue until finished. 191 | pass 192 | os._exit(0) 193 | else: 194 | client.close() 195 | t = threading.Thread(target=lambda: os.waitpid(pid, 0)) 196 | t.daemon = True 197 | t.start() 198 | 199 | class PoolServer(SimpleServer): 200 | 201 | def _create(self, target): 202 | raise NotImplementedError() 203 | 204 | def run(self, N=None): 205 | sys.stderr.write("%s: run()\n" % self) 206 | if N is None: 207 | N = DEFAULT_N 208 | for _ in range(N): 209 | self._create(target=super(PoolServer, self).run) 210 | super(PoolServer, self).run() 211 | 212 | class ThreadPoolServer(PoolServer): 213 | 214 | def _create(self, target): 215 | t = threading.Thread(target=target) 216 | t.daemon = True 217 | t.start() 218 | 219 | class ProcessPoolServer(PoolServer): 220 | 221 | def _create(self, target): 222 | pid = os.fork() 223 | if pid == 0: 224 | target() 225 | os._exit(0) 226 | else: 227 | t = threading.Thread(target=lambda: os.waitpid(pid, 0)) 228 | t.daemon = True 229 | t.start() 230 | 231 | SERVERS = [ 232 | SimpleServer, 233 | EventServer, 234 | ThreadServer, 235 | ProcessServer, 236 | ThreadPoolServer, 237 | ProcessPoolServer, 238 | ] 239 | -------------------------------------------------------------------------------- /test/test_bindings.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Adin Scannell , all rights reserved. 3 | # 4 | # This file is part of Huptime. 5 | # 6 | # Huptime is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # Huptime is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with Huptime. If not, see . 18 | # 19 | """ 20 | Test the libc bindings. 21 | 22 | These are microtests for ensure the libc 23 | functions are exactly as they should be. 24 | 25 | (To be implemented.) 26 | """ 27 | -------------------------------------------------------------------------------- /test/test_meta.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Adin Scannell , all rights reserved. 3 | # 4 | # This file is part of Huptime. 5 | # 6 | # Huptime is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # Huptime is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with Huptime. If not, see . 18 | # 19 | """ 20 | A basic test driver. 21 | 22 | The harness will run through the basic workflow 23 | and assert that all is well at each step, based 24 | on the behavior asserted by all the different pieces. 25 | 26 | This should cover nearly all the code, with the 27 | exception of code edge cases. Separate tests will 28 | exist for this edge cases. 29 | """ 30 | 31 | import sys 32 | import threading 33 | import pytest 34 | 35 | import harness 36 | import servers 37 | import modes 38 | 39 | @pytest.fixture(params=map(lambda x: x.__name__, servers.SERVERS)) 40 | def server(request): 41 | """ A server object. """ 42 | return getattr(servers, request.param) 43 | 44 | @pytest.fixture(params=map(lambda x: x.__name__, modes.MODES)) 45 | def mode(request): 46 | """ A mode object. """ 47 | return getattr(modes, request.param) 48 | 49 | def test_thrice(mode, server): 50 | h = harness.Harness(mode, server) 51 | try: 52 | h.restart() 53 | h.restart() 54 | h.restart() 55 | finally: 56 | h.stop() 57 | --------------------------------------------------------------------------------