├── CMakeLists.txt ├── LICENSE ├── Makefile ├── README.md ├── deb2snap ├── src ├── CMakeLists.txt └── preload.c ├── tests ├── CMakeLists.txt ├── exec.sh └── overlay.sh └── tools ├── copy-deb ├── dbus-daemon ├── dep-tree ├── fixes ├── wrapper.in ├── xmir-run └── xmir2-run /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # -*- Mode: CMake; indent-tabs-mode: nil; tab-width: 4 -*- 2 | # 3 | # Copyright (C) 2015 Canonical, Ltd. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; version 3. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | cmake_minimum_required(VERSION 2.8.9) 18 | 19 | project(snappy-preload C) 20 | 21 | if(${PROJECT_BINARY_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) 22 | message(FATAL_ERROR "In-tree build attempt detected, aborting. Set your build dir outside your source dir, delete CMakeCache.txt from source root and try again.") 23 | endif() 24 | 25 | # Standard install paths 26 | include(GNUInstallDirs) 27 | 28 | # Build flags 29 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -Wall -Wextra") 30 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs") 31 | 32 | if ("${CMAKE_BUILD_TYPE}" STREQUAL "release" OR "${CMAKE_BUILD_TYPE}" STREQUAL "relwithdebinfo") 33 | option(Werror "Treat warnings as errors" ON) 34 | else() 35 | option(Werror "Treat warnings as errors" OFF) 36 | endif() 37 | 38 | if (Werror) 39 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") 40 | endif() 41 | 42 | # Tests 43 | include(CTest) 44 | enable_testing() 45 | 46 | add_subdirectory(src) 47 | add_subdirectory(tests) 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 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 | {project} Copyright (C) {year} {fullname} 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 | 676 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # -*- Mode: Makefile; indent-tabs-mode: t; tab-width: 2 -*- 2 | # 3 | # Copyright (C) 2015 Canonical, Ltd. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; version 3. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | all: builddir 18 | cd builddir && make 19 | 20 | builddir: 21 | mkdir -p builddir 22 | cd builddir && cmake .. 23 | 24 | clean: 25 | rm -r builddir 26 | 27 | check: all 28 | @make -C builddir test 29 | 30 | %: builddir 31 | @[ "$@" = "Makefile" ] || make -C builddir $@ 32 | 33 | .PHONY: builddir check clean 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # deb2snap 2 | 3 | `deb2snap` is a script that lets you quickly and easily make snaps out of existing binaries that were not written with snaps in mind. 4 | 5 | Especially packages in the Ubuntu archive. 6 | 7 | ## Features 8 | 9 | ### Path redirection 10 | 11 | Many programs that are built in the Ubuntu archive have hardcoded paths to data (e.g. png, xml, or plugin files) and other executables (e.g. /usr/bin/bzr instead of calling bzr). 12 | 13 | But snaps install all program files into their own subdirectory on the system (like `/apps/foo/0/`). So a program from the Ubuntu archive would fail to find its own files when inside a snap! 14 | 15 | We'll fix this by **intercepting system calls** that take a path and redirecting the call to our copy of the file inside the snap. 16 | 17 | ### Dependency bundling 18 | 19 | Snaps are monolithic bundles of software, while Debian packages use a web of dependencies. All libraries and data that a program needs will have to be included in your snaps. It'd be a pain to manually hunt down and bundle all those dependencies. 20 | 21 | We'll fix this by **automatically including all dependencies** in the snap. 22 | 23 | ### 32-bit support 24 | 25 | Snappy doesn't offer an i386 version. And the amd64 version doesn't even ship with enough support to recognize or run a 32-bit executable. 26 | 27 | We'll fix this by **intercepting attempts to run 32-bit executables** and running it with a bundled copy of libc6:i386 as necessary. Just use the `--32` flag to enable support for this in your snap. 28 | 29 | ## Examples 30 | 31 | ### Setup 32 | 33 | - First, [install an up-to-date snappy system](https://developer.ubuntu.com/snappy/start). You can easily get the latest snappy image with the following command: `sudo ubuntu-device-flash core --enable-ssh -o snappy.img` 34 | - And you'll probably want to connect to your snappy machine with `ssh`, especially if you're going to be running any Mir apps that will take over the screen. 35 | - Remember to run `export LC_ALL=C.UTF-8` in your `ssh` terminal. Ubuntu Snappy doesn't have all locales yet and ssh may have ported over your local locale setting. 36 | 37 | And finally build deb2snap: 38 | 39 | sudo add-apt-repository ppa:snappy-dev/tools # if on 15.04 or earlier 40 | sudo apt-get update 41 | sudo apt-get install cmake gcc-multilib ubuntu-snappy-cli 42 | bzr branch lp:deb2snap 43 | cd deb2snap 44 | make 45 | 46 | ### Commandline app 47 | 48 | Let's start off with a simple commandline app. How about fan-favorite `fortune`? 49 | 50 | sudo apt-get install fortune-mod 51 | ./deb2snap fortune 52 | 53 | This will generate a file something like `fortune-mod_1-1.99.1-7_amd64.snap` in your current directory. It took the snap name from the package that `fortune` belongs to as well as the version for that package. You could override either with `-n` and `-v` respectively. 54 | 55 | Note how we installed fortune-mod first. `deb2snap` pulls files from your installed system. So you'll need to first install any package you want to bundle into a snap. 56 | 57 | Let's install that snap file and run `fortune`: 58 | 59 | $ sudo snappy install --allow-unauthenticated fortune-mod_1-1.99.1-7_amd64.snap 60 | $ fortune.fortune-mod 61 | Small things make base men proud. 62 | -- William Shakespeare, "Henry VI" 63 | 64 | Thanks, `fortune`. 65 | 66 | Let's be proud and base and **make the snap smaller**. We don't *need* to include *every* dependency of `fortune`. Some come included with Ubuntu Core for free. Let's exclude anything that comes with Ubuntu Core 15.04: 67 | 68 | ./deb2snap -d 15.04 fortune 69 | 70 | But what if we want **more fortunes**? We can include more packages in the snap with the `-p` flag. 71 | 72 | ./deb2snap -d 15.04 -p fortunes-spam -p fortunes-ubuntu-server fortune 73 | 74 | ### Non-archive app 75 | 76 | Let's say you've got some random executable or script on your machine. You'd like to package it up as a snap even though it didn't come from the Ubuntu archive. 77 | 78 | That's fine! Instead of giving the name of a program on your machine, just point `deb2snap` at the executable: 79 | 80 | ./deb2snap -d 15.04 ~/Desktop/my-custom-app 81 | 82 | `deb2snap` will automatically scan the executable and include the libraries you'll need. But if you have any other programs or data that your app will need from the archive, you can always include them with `-p`. 83 | 84 | Pointing at a script is especially useful if you need to do some minor setup before calling the real program. Just remember to include the real program with `-p` in that case, since `deb2snap` won't be able to detect that automatically like usual. 85 | 86 | ### Mir app 87 | 88 | #### Mir server snap 89 | 90 | Mir apps need a Mir server that has access to the input and video hardware. For these examples, you'll need an actual physical machine (not a VM) and a Mir framework snap. 91 | 92 | The latter is easy enough: 93 | 94 | bzr branch lp:~mir-team/mir/snappy-packaging 95 | cd snappy-packaging 96 | make 97 | 98 | And you'll have a `mir` snap sitting in your current directory. Install this on your machine and Mir will immediately start (and take over your screen!). You should see a cursor on a black background. 99 | 100 | You can stop and start the system compositor service like so (assuming the version of the snap is 0): 101 | 102 | sudo systemctl stop mir_system-compositor_0 103 | sudo systemctl start mir_system-compositor_0 104 | 105 | Your app can connect to the system compositor by wrapping itself with a call to `/apps/mir/current/bin/mir-run`. But you don't need to worry about that, `deb2snap` will do it for you. 106 | 107 | Presumably one day a similar Mir framework will be available in the store. But for now, you'll have to make your own. 108 | 109 | #### Mir client snap 110 | 111 | Once you have the Mir framework installed, let's build a simple Mir app: 112 | 113 | ./deb2snap -d 15.04 --mir mir_demo_client_fingerpaint 114 | 115 | Note the use of `--mir`. This tells `deb2snap` that your app needs to be wrapped with a call to `/apps/mir/current/bin/mir-run` and needs to ask snappy for permission to connect to Mir. 116 | 117 | After installing the above snap, you can run it as simply as: 118 | 119 | mir-demo-client-fingerpaint.mir-demos 120 | 121 | ### X app 122 | 123 | Many apps in the Ubuntu archive still use the X protocol directly (rather than a toolkit that has been ported to Mir). For these, we'll need to bundle Xmir into our snap. 124 | 125 | Let's build a neat snappy demo: xfreerdp. This will let us transform any snappy install into a thin client! 126 | 127 | There are two versions of Xmir: the one offered in Ubuntu 15.04 and earlier, which works as an extension to Xorg, and the one offered in 15.10 and later, which works as a standalone X server called `Xmir`. `deb2snap` has support for both, and both need a working Mir framework, as above. 128 | 129 | When you pass `--xmir` to `deb2snap`, you will get whichever version of Xmir is available on your system. 130 | 131 | The older version has some notable bugs: you'll have graphical glitches around your cursor, you'll see a second cursor on the screen, and you'll need to run your app as root. Build it into your snap like so: 132 | 133 | ./deb2snap -d 15.04 --xmir xfreerdp 134 | # copy and install snap into snappy machine 135 | sudo /apps/bin/xfreerdp.freerdp-x11 /f /v:SERVER /u:USER /p:PASSWORD 136 | 137 | The newer version doesn't have those bugs. You can simply build it into your snap like so: 138 | 139 | ./deb2snap -d 15.04 --xmir xfreerdp 140 | # copy and install snap into snappy machine 141 | xfreerdp.freerdp-x11 /f /v:SERVER /u:USER /p:PASSWORD 142 | 143 | #### Building Xmir Yourself 144 | 145 | If you are on an older system but still want the new Xmir, you can build it yourself. Try these steps: 146 | 147 | git clone git://people.freedesktop.org/~mlankhorst/xserver 148 | cd xserver 149 | sudo apt-get build-dep xorg-server 150 | debian/rules build 151 | cp ./build-main/hw/xmir/Xmir ~/ 152 | 153 | Then run `deb2snap` with the `--xmir-binary` argument: 154 | 155 | ./deb2snap -d 15.04 --xmir-binary ~/Xmir xfreerdp 156 | 157 | ## How it works 158 | 159 | ### libsnappypreload.so 160 | 161 | This is the library shim that does the actual interception. We'll wrap your program in a tiny shell that sets LD_PRELOAD to point at this. 162 | 163 | There are some clever things this library does, including intercepting execve calls to ensure that subprocesses also LD_PRELOAD libsnappypreload.so, no matter what happened to the environment in the mean time. 164 | 165 | ### Pulling in system packages 166 | 167 | The whole point of this script is to let you run already-compiled code. It can pull in files from installed debs and copy them into the snap. It will find all Depends and Recommends and include them too. 168 | 169 | This pulls from the debs installed **on your system**! So you have to have all the packages you want to include in the snap installed. 170 | 171 | ## Common options 172 | 173 | * -d 15.04 174 | 175 | The version of Ubuntu Core you want to target. It will skip including any package already provided by that version of Ubuntu Core. You can add a suffix like '/beta-2', if there isn't an official release yet. 176 | 177 | * -p PACKAGE 178 | 179 | An additional package to include in the snap. All Depends and Recommends will be included too. This can be used multiple times. 180 | 181 | * -n NAME 182 | 183 | Names the snap. 184 | 185 | * -v VERSION 186 | 187 | Versions the snap. 188 | 189 | * --overlay DIRECTORY 190 | 191 | A directory to copy over the snap directory right before building the snap. This lets you specify complicated meta/ files or include custom files in specific locations. Any instances of @PACKAGE@, @VERSION@, or @ARCH@ will be replaced with the correct value before going into the snap. 192 | 193 | * --mir 194 | 195 | Uses a mir wrapper script so that your executable will connect to a running Mir server. 196 | 197 | * --xmir 198 | 199 | Uses an xmir wrapper script so that your executable will connect to a running Mir server through xmir and will package Xorg and xmir into your snap for you. This will use an unconfined AppArmor template and require running the app as root. Even then, there will be some bugs and oddities. 200 | 201 | * --xmir-binary BINARY 202 | 203 | Like `--xmir` but specifies an Xmir server executable that you've built and will use that instead of Xorg. Can be used fully confined. 204 | 205 | * --aa-template TEMPLATE 206 | 207 | The AppArmor template to use. For example, 'unconfined'. 208 | 209 | * --vendor "NAME <EMAIL>" 210 | 211 | Your name and email. 212 | 213 | * --desc "DESCRIPTION" 214 | 215 | What the package does. Defaults to a short description from the Ubuntu archive. 216 | 217 | * --32 218 | 219 | Include a copy of libc6:i386 and a 32-bit compatible version of libsnappypreload.so so that 32-bit executables will run. 220 | 221 | * --arch ARCH 222 | 223 | The architecture for the snap you want to create. Defaults to your system architecture. You only need to specify this if you are including only multiarch packages. 224 | 225 | ## Caveats 226 | 227 | It would be fair to describe `deb2snap` as a pile of hacks. The tricks it uses for path redirection and 32-bit support are not ideal. The long term fix for both would be using something like overlayfs. But that's not ready *today* for Ubuntu Snappy so here we are. 228 | 229 | Additionally, there are some known bugs: 230 | 231 | * `postinst` scripts are not run. Any archive package that needs to do some post-installation setup (like compile gsettings schemas or generate caches) may not work as expected. 232 | 233 | * It's very likely that not every single syscall that takes a path is intercepted. If your app needs one of the calls that we don't intercept, your files may not be found. Please report any instance of this! 234 | -------------------------------------------------------------------------------- /deb2snap: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # -*- Mode: sh; indent-tabs-mode: nil; tab-width: 4 -*- 3 | # 4 | # Copyright (C) 2015 Canonical, Ltd. 5 | # 6 | # This program 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; version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | set -e 19 | 20 | ARCH= 21 | DESCRIPTION="A custom command" 22 | LOCAL_EXEC=false 23 | DBUS=false 24 | MIR=false 25 | SHIP32=false 26 | VERBOSE=false 27 | XMIR=false 28 | XMIREXEC= 29 | 30 | while [[ $# > 0 ]] 31 | do 32 | key="$1" 33 | 34 | case $key in 35 | --32) 36 | SHIP32=true 37 | PACKAGES="$PACKAGES libc6:i386" 38 | ;; 39 | --aa-template) 40 | AA_TEMPLATE="$2" 41 | shift 42 | ;; 43 | --arch) 44 | ARCH="$2" 45 | shift 46 | ;; 47 | --dbus) 48 | DBUS=true 49 | ;; 50 | --desc) 51 | DESCRIPTION="$2" 52 | shift 53 | ;; 54 | -d|--distro) 55 | DISTRO="$2" 56 | shift 57 | ;; 58 | --local-exec) 59 | LOCAL_EXEC=true 60 | ;; 61 | --mir) 62 | MIR=true 63 | ;; 64 | -n|--name) 65 | PACKAGE_NAME="$2" 66 | shift 67 | ;; 68 | --overlay) 69 | OVERLAY_DIR="$2" 70 | shift 71 | ;; 72 | -p|--package) 73 | PACKAGES="$PACKAGES $2" 74 | shift 75 | ;; 76 | --vendor) 77 | VENDOR="vendor: \"$2\"" 78 | shift 79 | ;; 80 | --verbose) 81 | VERBOSE=true 82 | ;; 83 | -v|--version) 84 | VERSION="$2" 85 | shift 86 | ;; 87 | --xmir-binary) 88 | XMIR=true 89 | XMIREXEC="$2" 90 | shift 91 | ;; 92 | --xmir) 93 | XMIR=true 94 | ;; 95 | --dkms) 96 | DKMS="$2" 97 | shift 98 | ;; 99 | *) 100 | if [ -e $1 ]; then 101 | COMMAND=$(readlink -e $1) 102 | elif which $1 >/dev/null; then 103 | COMMAND=$(which $1) 104 | else 105 | echo "Unknown command $1" 106 | exit 1 107 | fi 108 | COMMANDS="$COMMANDS $COMMAND" 109 | ;; 110 | esac 111 | shift 112 | done 113 | 114 | function get_pkg_for_file() 115 | { 116 | dpkg -S $@ 2>/dev/null | sed 's/: .*//' 117 | } 118 | 119 | function get_pkgs_from_ldd() 120 | { 121 | FILES=$(ldd $1 | grep '=> /' | cut -d' ' -f3) 122 | get_pkg_for_file $FILES 123 | } 124 | 125 | if ! [ -e ./builddir ]; then 126 | echo "Looks like you haven't run make yet, doing it for you" 127 | make 128 | fi 129 | 130 | if [ -z "$COMMANDS" ]; then 131 | echo "You must provide a command to include in the snap" 132 | exit 1 133 | fi 134 | 135 | CMD_PACKAGE= 136 | for COMMAND in $COMMANDS; do 137 | ONE_CMD_PACKAGE=$(get_pkg_for_file $COMMAND) 138 | if [ -n "$ONE_CMD_PACKAGE" ]; then 139 | PACKAGES="$PACKAGES $ONE_CMD_PACKAGE" 140 | else 141 | # just for tracking that we can't assume package name/version 142 | ONE_CMD_PACKAGE="UNKNOWN" 143 | fi 144 | if [ -z "$CMD_PACKAGE" ]; then 145 | CMD_PACKAGE="$ONE_CMD_PACKAGE" 146 | elif [ "$CMD_PACKAGE" != "$ONE_CMD_PACKAGE" ]; then 147 | CMD_PACKAGE="$CMD_PACKAGE $ONE_CMD_PACKAGE" 148 | fi 149 | done 150 | if $LOCAL_EXEC; then 151 | CMD_PACKAGE= # act like we didn't find anything 152 | fi 153 | 154 | COMMANDS_LEN=$(echo $CMD_PACKAGE | wc -w) 155 | if [ $COMMANDS_LEN -gt 1 -o "$CMD_PACKAGE" = "UNKNOWN" ]; then 156 | CMD_PACKAGE= 157 | fi 158 | 159 | if [ -z "$ARCH" ]; then 160 | ARCH=$(dpkg-architecture -qDEB_HOST_ARCH) 161 | fi 162 | 163 | if [ -z "$PACKAGE_NAME" ]; then 164 | if [ -z "$CMD_PACKAGE" ]; then 165 | echo "No package name specified" 166 | exit 1 167 | fi 168 | PACKAGE_NAME=$(echo $CMD_PACKAGE | tr '~+:' '-') # ':' is not allowed 169 | fi 170 | 171 | if [ -z "$VERSION" ]; then 172 | if [ -z "$CMD_PACKAGE" ]; then 173 | echo "No version specified" 174 | exit 1 175 | fi 176 | VERSION=$(apt-cache policy $CMD_PACKAGE | grep '\*\*\*' | cut -d' ' -f3) 177 | fi 178 | VERSION=$(echo $VERSION | tr '~+:' '-') # ':' in paths will confuse LD_PRELOAD 179 | 180 | if [ -z "$VENDOR" ]; then 181 | VENDOR="vendor: \"Unknown\"" 182 | fi 183 | 184 | ROOT=$(mktemp -dt snap.XXXXXXXXXX) 185 | mkdir -p $ROOT/bin $ROOT/meta 186 | 187 | if $DBUS; then 188 | PACKAGES="dbus $PACKAGES" 189 | cp -p ./tools/dbus-daemon $ROOT/bin/_dbus-daemon 190 | fi 191 | 192 | if $XMIR; then 193 | if [ -n "$XMIREXEC" ]; then 194 | # Xmir 2.0! Whole different way of calling it. 195 | # Just for ease, include more than we *need* by grabbing all of xorg 196 | PACKAGES="xorg $(get_pkgs_from_ldd $XMIREXEC) $PACKAGES" 197 | cp -p ./tools/xmir2-run $ROOT/bin/_xmir-run 198 | cp -p "$XMIREXEC" $ROOT/bin/_Xmir 199 | elif which Xmir >/dev/null; then 200 | # Xmir 2.0 again, and again include xorg for all its data packages 201 | PACKAGES="xorg xmir $PACKAGES" 202 | cp -p ./tools/xmir2-run $ROOT/bin/_xmir-run 203 | cp -p "$(which Xmir)" $ROOT/bin/_Xmir 204 | else 205 | # Old Xmir 1.0, needs an unconfined AA template 206 | PACKAGES="xorg xserver-xorg-xmir $PACKAGES" 207 | cp -p ./tools/xmir-run $ROOT/bin/_xmir-run 208 | if [ -n "AA_TEMPLATE" ]; then 209 | AA_TEMPLATE="unconfined" 210 | echo "Using an unconfined AppArmor template, because Xmir requires it" 211 | fi 212 | fi 213 | fi 214 | 215 | if [ -n "$DKMS" ]; then 216 | dkms_conf="`dpkg -L $DKMS | grep dkms.conf`" 217 | if [ $? == 1 ]; then 218 | echo "DKMS $DKMS cannot be found, please check the spelling." 219 | exit 1 220 | fi 221 | dkms_package_name=`grep PACKAGE_NAME $dkms_conf | awk -F'"' '{ print $2 }'` 222 | dkms_package_ver=`grep PACKAGE_VERSION $dkms_conf | awk -F'"' '{ print $2 }'` 223 | dkms_module_names=`grep BUILT_MODULE_NAME $dkms_conf | awk -F'"' '{ print $2 }'` 224 | 225 | # check if dkms has been installed 226 | dkms status $dkms_package_name -v $dkms_package_ver -k `uname -r` | grep installed > /dev/null 227 | if [ $? = 1 ]; then 228 | echo "DKMS $DKMS has not been installed, plasea use 'dkms install' to install it first." 229 | exit 1 230 | fi 231 | 232 | dkms_modules="" 233 | for m in $dkms_module_names; do 234 | kv=`uname -r` 235 | ko=`find /lib/modules/$kv/updates/dkms/ -name $m.ko` 236 | dkms_modules="$ko $modules" 237 | done 238 | fi 239 | 240 | # This grabs all the current system's ld.so.conf entries as reported by 241 | # ldconfig and prepends our new root prefix to each of them. 242 | # Unfortunately, we lose the ordering when we sort. We also grab directories 243 | # we end up not caring about (from other packages installed on the system). 244 | # Is there a cleaner way to do this? 245 | LDPATHS=$(ldconfig --print-cache | grep ' => /' | sed 's|^.* => \(/[^ ]*\)/[^/ ]*.*$|\1|g' | sort -r | uniq | tr '\n' : | sed "s=^=\$SNAP_APP_PATH/debs=;s=:/=:\$SNAP_APP_PATH/debs/=g") 246 | 247 | mkdir -p $ROOT/lib 248 | cp -p ./builddir/src/libsnappypreload.so $ROOT/lib/libsnappypreload.so 249 | LDPRELOAD=\$SNAP_APP_PATH/lib/libsnappypreload.so 250 | 251 | if $SHIP32; then 252 | mkdir -p $ROOT/lib32 253 | cp -p ./builddir/src/libsnappypreload32.so $ROOT/lib32/libsnappypreload.so 254 | LDPRELOAD=$LDPRELOAD:\$SNAP_APP_PATH/lib32/libsnappypreload.so 255 | fi 256 | 257 | for COMMAND in $COMMANDS; do 258 | SHORT_COMMAND=$(basename $COMMAND | tr '_' '-') # snappy doesn't allow underscores 259 | cp -p $COMMAND $ROOT/bin/$SHORT_COMMAND.real 260 | chmod a+x $ROOT/bin/$SHORT_COMMAND.real 261 | 262 | if [ "$(file -b --mime-encoding $COMMAND)" = "binary" ]; then 263 | PACKAGES="$PACKAGES $(get_pkgs_from_ldd $COMMAND)" 264 | RPATHS=$(objdump -p $COMMAND | grep RPATH | awk '{ print $2 }') 265 | if [ -n $RPATHS ]; then 266 | ORIGINAL_PATH=$(dirname $COMMAND) 267 | for RPATH in $(echo $RPATHS | tr : ' '); do 268 | RPATH=$(echo $RPATH | sed "s|\$ORIGIN|$ORIGINAL_PATH|") 269 | LDPATHS=$LDPATHS:\$SNAP_APP_PATH/debs$RPATH 270 | done 271 | fi 272 | fi 273 | 274 | CAPS= 275 | WRAPPER_COMMAND="\$SNAP_APP_PATH/bin/$SHORT_COMMAND.real" 276 | 277 | if $DBUS; then 278 | WRAPPER_COMMAND="dbus-run-session --dbus-daemon=\$SNAP_APP_PATH/bin/_dbus-daemon -- $WRAPPER_COMMAND" 279 | fi 280 | 281 | if $MIR; then 282 | WRAPPER_COMMAND="env QT_QPA_PLATFORM=ubuntumirclient /apps/mir/current/bin/mir-run $WRAPPER_COMMAND" 283 | FRAMEWORKS="${FRAMEWORKS} 284 | - mir" 285 | CAPS="${CAPS} 286 | - mir_client" 287 | fi 288 | 289 | if $XMIR; then 290 | WRAPPER_COMMAND="\$SNAP_APP_PATH/bin/_xmir-run $WRAPPER_COMMAND" 291 | FRAMEWORKS="${FRAMEWORKS} 292 | - mir" 293 | CAPS="${CAPS} 294 | - mir_client" 295 | fi 296 | 297 | sed "s|@COMMAND@|$WRAPPER_COMMAND|g;s|@LDPATHS@|$LDPATHS|g;s|@LDPRELOAD@|$LDPRELOAD|g" ./tools/wrapper.in > $ROOT/bin/$SHORT_COMMAND 298 | chmod a+x $ROOT/bin/$SHORT_COMMAND 299 | 300 | YAML_BINARIES="$YAML_BINARIES 301 | - name: bin/$SHORT_COMMAND" 302 | 303 | if [ -n "$CAPS" ]; then 304 | # networking is added by default if no caps are specified, so don't regress that 305 | YAML_BINARIES="$YAML_BINARIES 306 | caps: 307 | - networking$CAPS" 308 | fi 309 | 310 | if [ -n "$AA_TEMPLATE" ]; then 311 | YAML_BINARIES="$YAML_BINARIES 312 | security-template: $AA_TEMPLATE" 313 | fi 314 | done 315 | 316 | if [ -n "$FRAMEWORKS" ]; then 317 | YAML_FRAMEWORKS="frameworks:$FRAMEWORKS" 318 | fi 319 | 320 | cat > $ROOT/meta/package.yaml < $ROOT/meta/readme.md 331 | else 332 | echo "$DESCRIPTION" > $ROOT/meta/readme.md 333 | fi 334 | 335 | if [ -n "$PACKAGES" ]; then 336 | if [ -n "$DISTRO" ]; then 337 | DOWNLOAD_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/snappy-preload/manifests" 338 | DISTRO_FILEPART=${DISTRO//-} 339 | DISTRO_FILEPART=${DISTRO_FILEPART//\//-} 340 | MANIFEST_FILE="ubuntu-core-$DISTRO_FILEPART-core-$ARCH.manifest" 341 | if [ ! -r "$DOWNLOAD_DIR/$MANIFEST_FILE" ]; then 342 | mkdir -p "$DOWNLOAD_DIR" 343 | DISTRO_URLPART=$DISTRO 344 | if [ "${DISTRO##*/}" = "$DISTRO" ]; then # that means no release was specified 345 | DISTRO_URLPART=$DISTRO/release 346 | fi 347 | wget --quiet -N http://cdimage.ubuntu.com/ubuntu-core/releases/$DISTRO_URLPART/$MANIFEST_FILE -P "$DOWNLOAD_DIR" 348 | fi 349 | MANIFEST_ARG="--manifest $DOWNLOAD_DIR/$MANIFEST_FILE" 350 | fi 351 | if [ -z "$DKMS" ]; then 352 | ALL_PACKAGES="$(./tools/dep-tree $MANIFEST_ARG $PACKAGES)" 353 | else 354 | ALL_PACKAGES="$(./tools/dep-tree --ignore $DKMS $MANIFEST_ARG $PACKAGES)" 355 | fi 356 | 357 | if $MIR && echo $ALL_PACKAGES | grep -q libqt5gui; then 358 | PACKAGES="$PACKAGES qtubuntu-desktop" 359 | ALL_PACKAGES="$(./tools/dep-tree $MANIFEST_ARG $PACKAGES)" 360 | fi 361 | 362 | echo $ALL_PACKAGES | xargs ./tools/copy-deb $ROOT/debs 363 | 364 | if [ -n "$DKMS" ]; then 365 | for ko in $dkms_modules; do 366 | dest=$ROOT/`dirname $ko` 367 | mkdir -p $dest 368 | cp -p $ko $dest 369 | done 370 | fi 371 | fi 372 | 373 | ./tools/fixes "$ROOT" $PACKAGE_NAME $VERSION 374 | 375 | if [ -n "$OVERLAY_DIR" ]; then 376 | OVERLAY_TMP=$(mktemp -dt snap.XXXXXXXXXX) 377 | cp -dR --preserve=mode "$OVERLAY_DIR"/* "$OVERLAY_TMP" 378 | find "$OVERLAY_TMP" -type f -exec sh -c "file {} | grep text >/dev/null && sed -i 's/@PACKAGE@/$PACKAGE_NAME/g;s/@VERSION@/$VERSION/g;s/@ARCH@/$ARCH/g' {}" \; 379 | cp -rp "$OVERLAY_TMP"/* "$ROOT" 380 | rm -rf "$OVERLAY_TMP" 381 | fi 382 | 383 | if $VERBOSE; then 384 | snappy build $ROOT 385 | else 386 | snappy build $ROOT | grep Generated 387 | fi 388 | 389 | rm -rf "$ROOT" 390 | exit 0 391 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # -*- Mode: CMake; indent-tabs-mode: nil; tab-width: 4 -*- 2 | # 3 | # Copyright (C) 2015 Canonical, Ltd. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; version 3. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | add_library(snappypreload SHARED preload.c) 18 | set_target_properties(snappypreload PROPERTIES 19 | COMPILE_FLAGS "-DSNAPPY_LIBNAME=\\\"libsnappypreload.so\\\"") 20 | target_link_libraries(snappypreload -ldl) 21 | 22 | execute_process(COMMAND uname -m COMMAND tr -d '\n' OUTPUT_VARIABLE ARCHITECTURE) 23 | if(${ARCHITECTURE} STREQUAL "x86_64") 24 | add_library(snappypreload32 SHARED preload.c) 25 | set_target_properties(snappypreload32 PROPERTIES 26 | COMPILE_FLAGS "-DSNAPPY_LIBNAME=\\\"libsnappypreload32.so\\\" -m32") 27 | target_link_libraries(snappypreload32 -ldl -m32) 28 | endif() 29 | -------------------------------------------------------------------------------- /src/preload.c: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; indent-tabs-mode: nil; tab-width: 4 -*- 2 | * 3 | * Copyright (C) 2015 Canonical, Ltd. 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; version 3. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #define _GNU_SOURCE 19 | #define __USE_GNU 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #define LD_PRELOAD "LD_PRELOAD" 37 | #define SNAPPY_PRELOAD "SNAPPY_PRELOAD" 38 | static char **saved_ld_preloads = NULL; 39 | static size_t num_saved_ld_preloads = 0; 40 | static char *saved_snappy_preload = NULL; 41 | static char *saved_tmpdir = NULL; 42 | static char *saved_varlib = NULL; 43 | 44 | static void constructor() __attribute__((constructor)); 45 | 46 | static char * 47 | getenvdup (const char *varname) 48 | { 49 | char *envvar = secure_getenv (varname); 50 | if (envvar == NULL || envvar[0] == 0) // identical for our purposes 51 | return NULL; 52 | else 53 | return strdup (envvar); 54 | } 55 | 56 | void constructor() 57 | { 58 | char *ld_preload_copy, *p, *savedptr = NULL; 59 | size_t libnamelen; 60 | 61 | // We need to save LD_PRELOAD and SNAPPY_PRELOAD in case we need to 62 | // propagate the values to an exec'd program. 63 | ld_preload_copy = getenvdup (LD_PRELOAD); 64 | if (ld_preload_copy == NULL) { 65 | return; 66 | } 67 | 68 | saved_snappy_preload = getenvdup (SNAPPY_PRELOAD); 69 | if (saved_snappy_preload == NULL) { 70 | free (ld_preload_copy); 71 | return; 72 | } 73 | 74 | saved_tmpdir = getenvdup ("SNAP_APP_TMPDIR"); 75 | if (!saved_tmpdir) { 76 | saved_tmpdir = getenvdup ("TMPDIR"); 77 | } 78 | 79 | saved_varlib = getenvdup ("SNAP_APP_DATA_PATH"); 80 | if (!saved_varlib) { 81 | saved_varlib = getenvdup ("SNAPP_APP_DATA_PATH"); 82 | } 83 | 84 | // Pull out each absolute-pathed libsnappypreload.so we find. Better to 85 | // accidentally include some other libsnappypreload than not propagate 86 | // ourselves. 87 | libnamelen = strlen (SNAPPY_LIBNAME); 88 | for (p = strtok_r (ld_preload_copy, " :", &savedptr); 89 | p; 90 | p = strtok_r (NULL, " :", &savedptr)) { 91 | size_t plen = strlen (p); 92 | if (plen > libnamelen && p[0] == '/' && strcmp (p + strlen (p) - strlen (SNAPPY_LIBNAME) - 1, "/" SNAPPY_LIBNAME) == 0) { 93 | num_saved_ld_preloads++; 94 | saved_ld_preloads = realloc (saved_ld_preloads, (num_saved_ld_preloads + 1) * sizeof (char *)); 95 | saved_ld_preloads[num_saved_ld_preloads - 1] = strdup (p); 96 | saved_ld_preloads[num_saved_ld_preloads] = NULL; 97 | } 98 | } 99 | free (ld_preload_copy); 100 | } 101 | 102 | static char * 103 | redirect_writable_path (const char *pathname, const char *basepath) 104 | { 105 | char *redirected_pathname; 106 | int chop = 0; 107 | 108 | if (pathname[0] == 0) { 109 | return strdup (basepath); 110 | } 111 | 112 | redirected_pathname = malloc (PATH_MAX); 113 | 114 | if (basepath[strlen (basepath) - 1] == '/') { 115 | chop = 1; 116 | } 117 | strncpy (redirected_pathname, basepath, PATH_MAX - 1 - chop); 118 | 119 | strncat (redirected_pathname, pathname, PATH_MAX - 1 - strlen (redirected_pathname)); 120 | 121 | // No need to see if it already exists -- app can only be in TMPDIR, not /tmp 122 | return redirected_pathname; 123 | } 124 | 125 | static char * 126 | redirect_path_full (const char *pathname, int check_parent, int only_if_absolute) 127 | { 128 | int (*_access) (const char *pathname, int mode); 129 | char *redirected_pathname; 130 | char *preload_dir; 131 | int ret; 132 | int chop = 0; 133 | char *slash = 0; 134 | 135 | if (pathname == NULL) { 136 | return NULL; 137 | } 138 | 139 | preload_dir = saved_snappy_preload; 140 | if (preload_dir == NULL) { 141 | return strdup (pathname); 142 | } 143 | 144 | // Do not redirect when accessing /dev 145 | if (strcmp (pathname, "/dev") == 0 || strncmp (pathname, "/dev/", 5) == 0) { 146 | return strdup (pathname); 147 | } 148 | 149 | if (only_if_absolute && pathname[0] != '/') { 150 | return strdup (pathname); 151 | } 152 | 153 | // Sometimes programs will hardcode /tmp (like Xorg does for its lock file). 154 | // In that case, let's redirect to TMPDIR. 155 | if (strcmp (pathname, "/tmp") == 0 || strncmp (pathname, "/tmp/", 5) == 0) { 156 | if (saved_tmpdir && strncmp (pathname, saved_tmpdir, strlen (saved_tmpdir)) != 0) { 157 | return redirect_writable_path (pathname + 4, saved_tmpdir); 158 | } else { 159 | return strdup (pathname); 160 | } 161 | } 162 | 163 | _access = (int (*)(const char *pathname, int mode)) dlsym (RTLD_NEXT, "access"); 164 | 165 | // And each app should have its own /var/lib writable tree. Here, we want 166 | // to support reading the base system's files if they exist, else let the app 167 | // play in /var/lib themselves. So we reverse the normal check: first see if 168 | // it exists in root, else do our redirection. 169 | if (strcmp (pathname, "/var/lib") == 0 || strncmp (pathname, "/var/lib/", 9) == 0) { 170 | if (saved_varlib && strncmp (pathname, saved_varlib, strlen (saved_varlib)) != 0 && _access (pathname, F_OK) != 0) { 171 | return redirect_writable_path (pathname + 8, saved_varlib); 172 | } else { 173 | return strdup (pathname); 174 | } 175 | } 176 | 177 | redirected_pathname = malloc (PATH_MAX); 178 | 179 | if (preload_dir[strlen (preload_dir) - 1] == '/') { 180 | chop = 1; 181 | } 182 | strncpy (redirected_pathname, preload_dir, PATH_MAX - 1 - chop); 183 | 184 | if (pathname[0] != '/') { 185 | size_t cursize = strlen (redirected_pathname); 186 | if (getcwd (redirected_pathname + cursize, PATH_MAX - cursize) == NULL) { 187 | free (redirected_pathname); 188 | return strdup (pathname); 189 | } 190 | strncat (redirected_pathname, "/", PATH_MAX - 1 - strlen (redirected_pathname)); 191 | } 192 | 193 | strncat (redirected_pathname, pathname, PATH_MAX - 1 - strlen (redirected_pathname)); 194 | 195 | if (check_parent) { 196 | slash = strrchr (redirected_pathname, '/'); 197 | if (slash) { // should always be true 198 | *slash = 0; 199 | } 200 | } 201 | 202 | ret = _access (redirected_pathname, F_OK); 203 | 204 | if (check_parent && slash) { 205 | *slash = '/'; 206 | } 207 | 208 | if (ret == 0 || errno == ENOTDIR) { // ENOTDIR is OK because it exists at least 209 | return redirected_pathname; 210 | } else { 211 | free (redirected_pathname); 212 | return strdup (pathname); 213 | } 214 | } 215 | 216 | static char * 217 | redirect_path (const char *pathname) 218 | { 219 | return redirect_path_full (pathname, 0, 0); 220 | } 221 | 222 | static char * 223 | redirect_path_target (const char *pathname) 224 | { 225 | return redirect_path_full (pathname, 1, 0); 226 | } 227 | 228 | static char * 229 | redirect_path_if_absolute (const char *pathname) 230 | { 231 | return redirect_path_full (pathname, 0, 1); 232 | } 233 | 234 | #define REDIRECT_1_1(RET, NAME) \ 235 | RET \ 236 | NAME (const char *path) \ 237 | { \ 238 | RET (*_NAME) (const char *path); \ 239 | char *new_path = NULL; \ 240 | RET result; \ 241 | _NAME = (RET (*)(const char *path)) dlsym (RTLD_NEXT, #NAME); \ 242 | new_path = redirect_path (path); \ 243 | result = _NAME (new_path); \ 244 | free (new_path); \ 245 | return result; \ 246 | } 247 | 248 | #define REDIRECT_1_2(RET, NAME, T2) \ 249 | RET \ 250 | NAME (const char *path, T2 A2) \ 251 | { \ 252 | RET (*_NAME) (const char *path, T2 A2); \ 253 | char *new_path = NULL; \ 254 | RET result; \ 255 | _NAME = (RET (*)(const char *path, T2 A2)) dlsym (RTLD_NEXT, #NAME); \ 256 | new_path = redirect_path (path); \ 257 | result = _NAME (new_path, A2); \ 258 | free (new_path); \ 259 | return result; \ 260 | } 261 | 262 | #define REDIRECT_1_3(RET, NAME, T2, T3) \ 263 | RET \ 264 | NAME (const char *path, T2 A2, T3 A3) \ 265 | { \ 266 | RET (*_NAME) (const char *path, T2 A2, T3 A3); \ 267 | char *new_path = NULL; \ 268 | RET result; \ 269 | _NAME = (RET (*)(const char *path, T2 A2, T3 A3)) dlsym (RTLD_NEXT, #NAME); \ 270 | new_path = redirect_path (path); \ 271 | result = _NAME (new_path, A2, A3); \ 272 | free (new_path); \ 273 | return result; \ 274 | } 275 | 276 | #define REDIRECT_2_2(RET, NAME, T1) \ 277 | RET \ 278 | NAME (T1 A1, const char *path) \ 279 | { \ 280 | RET (*_NAME) (T1 A1, const char *path); \ 281 | char *new_path = NULL; \ 282 | RET result; \ 283 | _NAME = (RET (*)(T1 A1, const char *path)) dlsym (RTLD_NEXT, #NAME); \ 284 | new_path = redirect_path (path); \ 285 | result = _NAME (A1, new_path); \ 286 | free (new_path); \ 287 | return result; \ 288 | } 289 | 290 | #define REDIRECT_2_3(RET, NAME, T1, T3) \ 291 | RET \ 292 | NAME (T1 A1, const char *path, T3 A3) \ 293 | { \ 294 | RET (*_NAME) (T1 A1, const char *path, T3 A3); \ 295 | char *new_path = NULL; \ 296 | RET result; \ 297 | _NAME = (RET (*)(T1 A1, const char *path, T3 A3)) dlsym (RTLD_NEXT, #NAME); \ 298 | new_path = redirect_path (path); \ 299 | result = _NAME (A1, new_path, A3); \ 300 | free (new_path); \ 301 | return result; \ 302 | } 303 | 304 | #define REDIRECT_2_3_AT(RET, NAME, T1, T3) \ 305 | RET \ 306 | NAME (T1 A1, const char *path, T3 A3) \ 307 | { \ 308 | RET (*_NAME) (T1 A1, const char *path, T3 A3); \ 309 | char *new_path = NULL; \ 310 | RET result; \ 311 | _NAME = (RET (*)(T1 A1, const char *path, T3 A3)) dlsym (RTLD_NEXT, #NAME); \ 312 | new_path = redirect_path_if_absolute (path); \ 313 | result = _NAME (A1, new_path, A3); \ 314 | free (new_path); \ 315 | return result; \ 316 | } 317 | 318 | #define REDIRECT_2_4_AT(RET, NAME, T1, T3, T4) \ 319 | RET \ 320 | NAME (T1 A1, const char *path, T3 A3, T4 A4) \ 321 | { \ 322 | RET (*_NAME) (T1 A1, const char *path, T3 A3, T4 A4); \ 323 | char *new_path = NULL; \ 324 | RET result; \ 325 | _NAME = (RET (*)(T1 A1, const char *path, T3 A3, T4 A4)) dlsym (RTLD_NEXT, #NAME); \ 326 | new_path = redirect_path_if_absolute (path); \ 327 | result = _NAME (A1, new_path, A3, A4); \ 328 | free (new_path); \ 329 | return result; \ 330 | } 331 | 332 | #define REDIRECT_3_5(RET, NAME, T1, T2, T4, T5) \ 333 | RET \ 334 | NAME (T1 A1, T2 A2, const char *path, T4 A4, T5 A5) \ 335 | { \ 336 | RET (*_NAME) (T1 A1, T2 A2, const char *path, T4 A4, T5 A5); \ 337 | char *new_path = NULL; \ 338 | RET result; \ 339 | _NAME = (RET (*)(T1 A1, T2 A2, const char *path, T4 A4, T5 A5)) dlsym (RTLD_NEXT, #NAME); \ 340 | new_path = redirect_path (path); \ 341 | result = _NAME (A1, A2, new_path, A4, A5); \ 342 | free (new_path); \ 343 | return result; \ 344 | } 345 | 346 | #define REDIRECT_TARGET(RET, NAME) \ 347 | RET \ 348 | NAME (const char *path, const char *target) \ 349 | { \ 350 | RET (*_NAME) (const char *path, const char *target); \ 351 | char *new_path = NULL; \ 352 | char *new_target = NULL; \ 353 | RET result; \ 354 | _NAME = (RET (*)(const char *path, const char *target)) dlsym (RTLD_NEXT, #NAME); \ 355 | new_path = redirect_path (path); \ 356 | new_target = redirect_path_target (target); \ 357 | result = _NAME (new_path, new_target); \ 358 | free (new_path); \ 359 | free (new_target); \ 360 | return result; \ 361 | } 362 | 363 | #define REDIRECT_OPEN(NAME) \ 364 | int \ 365 | NAME (const char *path, int flags, ...) \ 366 | { \ 367 | int mode = 0; \ 368 | int (*_NAME) (const char *path, int flags, mode_t mode); \ 369 | char *new_path = NULL; \ 370 | int result; \ 371 | if (flags & (O_CREAT|O_TMPFILE)) \ 372 | { \ 373 | va_list ap; \ 374 | va_start (ap, flags); \ 375 | mode = va_arg (ap, mode_t); \ 376 | va_end (ap); \ 377 | } \ 378 | _NAME = (int (*)(const char *path, int flags, mode_t mode)) dlsym (RTLD_NEXT, #NAME); \ 379 | new_path = redirect_path (path); \ 380 | result = _NAME (new_path, flags, mode); \ 381 | free (new_path); \ 382 | return result; \ 383 | } 384 | 385 | #define REDIRECT_OPEN_AT(NAME) \ 386 | int \ 387 | NAME (int dirfp, const char *path, int flags, ...) \ 388 | { \ 389 | int mode = 0; \ 390 | int (*_NAME) (int dirfp, const char *path, int flags, mode_t mode); \ 391 | char *new_path = NULL; \ 392 | int result; \ 393 | if (flags & (O_CREAT|O_TMPFILE)) \ 394 | { \ 395 | va_list ap; \ 396 | va_start (ap, flags); \ 397 | mode = va_arg (ap, mode_t); \ 398 | va_end (ap); \ 399 | } \ 400 | _NAME = (int (*)(int dirfp, const char *path, int flags, mode_t mode)) dlsym (RTLD_NEXT, #NAME); \ 401 | new_path = redirect_path_if_absolute (path); \ 402 | result = _NAME (dirfp, new_path, flags, mode); \ 403 | free (new_path); \ 404 | return result; \ 405 | } 406 | 407 | REDIRECT_1_2(FILE *, fopen, const char *) 408 | REDIRECT_1_2(FILE *, fopen64, const char *) 409 | REDIRECT_1_1(int, unlink) 410 | REDIRECT_2_3_AT(int, unlinkat, int, int) 411 | REDIRECT_1_2(int, access, int) 412 | REDIRECT_1_2(int, eaccess, int) 413 | REDIRECT_1_2(int, euidaccess, int) 414 | REDIRECT_2_4_AT(int, faccessat, int, int, int) 415 | REDIRECT_1_2(int, stat, struct stat *) 416 | REDIRECT_1_2(int, stat64, struct stat64 *) 417 | REDIRECT_1_2(int, lstat, struct stat *) 418 | REDIRECT_1_2(int, lstat64, struct stat64 *) 419 | REDIRECT_1_2(int, creat, mode_t) 420 | REDIRECT_1_2(int, creat64, mode_t) 421 | REDIRECT_1_2(int, truncate, off_t) 422 | REDIRECT_2_2(char *, bindtextdomain, const char *) 423 | REDIRECT_2_3(int, __xstat, int, struct stat *) 424 | REDIRECT_2_3(int, __xstat64, int, struct stat64 *) 425 | REDIRECT_2_3(int, __lxstat, int, struct stat *) 426 | REDIRECT_2_3(int, __lxstat64, int, struct stat64 *) 427 | REDIRECT_3_5(int, __fxstatat, int, int, struct stat *, int) 428 | REDIRECT_3_5(int, __fxstatat64, int, int, struct stat64 *, int) 429 | REDIRECT_1_2(int, statfs, struct statfs *) 430 | REDIRECT_1_2(int, statfs64, struct statfs64 *) 431 | REDIRECT_1_2(int, statvfs, struct statvfs *) 432 | REDIRECT_1_2(int, statvfs64, struct statvfs64 *) 433 | REDIRECT_1_2(long, pathconf, int) 434 | REDIRECT_1_1(DIR *, opendir) 435 | REDIRECT_1_2(int, mkdir, mode_t) 436 | REDIRECT_1_1(int, rmdir) 437 | REDIRECT_1_3(int, chown, uid_t, gid_t) 438 | REDIRECT_1_3(int, lchown, uid_t, gid_t) 439 | REDIRECT_1_2(int, chmod, mode_t) 440 | REDIRECT_1_2(int, lchmod, mode_t) 441 | REDIRECT_1_1(int, chdir) 442 | REDIRECT_1_3(ssize_t, readlink, char *, size_t) 443 | REDIRECT_1_2(char *, realpath, char *) 444 | REDIRECT_TARGET(int, link) 445 | REDIRECT_TARGET(int, rename) 446 | REDIRECT_OPEN(open) 447 | REDIRECT_OPEN(open64) 448 | REDIRECT_OPEN_AT(openat) 449 | REDIRECT_OPEN_AT(openat64) 450 | REDIRECT_2_3(int, inotify_add_watch, int, uint32_t) 451 | 452 | int 453 | scandir (const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) 454 | { 455 | int (*_scandir) (const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)); 456 | char *new_path = NULL; 457 | int ret; 458 | 459 | _scandir = (int (*)(const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **))) dlsym (RTLD_NEXT, "scandir"); 460 | 461 | new_path = redirect_path (dirp); 462 | ret = _scandir (new_path, namelist, filter, compar); 463 | free (new_path); 464 | 465 | return ret; 466 | } 467 | 468 | int 469 | scandir64 (const char *dirp, struct dirent64 ***namelist, 470 | int (*filter)(const struct dirent64 *), 471 | int (*compar)(const struct dirent64 **, const struct dirent64 **)) 472 | { 473 | int (*_scandir64) (const char *dirp, struct dirent64 ***namelist, 474 | int (*filter)(const struct dirent64 *), 475 | int (*compar)(const struct dirent64 **, const struct dirent64 **)); 476 | char *new_path = NULL; 477 | int ret; 478 | 479 | _scandir64 = (int (*)(const char *dirp, struct dirent64 ***namelist, 480 | int (*filter)(const struct dirent64 *), 481 | int (*compar)(const struct dirent64 **, const struct dirent64 **))) 482 | dlsym (RTLD_NEXT, "scandir64"); 483 | 484 | new_path = redirect_path (dirp); 485 | ret = _scandir64 (new_path, namelist, filter, compar); 486 | free (new_path); 487 | 488 | return ret; 489 | } 490 | 491 | 492 | int 493 | scandirat (int dirfd, const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) 494 | { 495 | int (*_scandirat) (int dirfd, const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)); 496 | char *new_path = NULL; 497 | int ret; 498 | 499 | _scandirat = (int (*)(int dirfd, const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **))) dlsym (RTLD_NEXT, "scandirat"); 500 | 501 | new_path = redirect_path_if_absolute (dirp); 502 | ret = _scandirat (dirfd, new_path, namelist, filter, compar); 503 | free (new_path); 504 | 505 | return ret; 506 | } 507 | 508 | int 509 | scandirat64 (int dirfd, const char *dirp, struct dirent64 ***namelist, 510 | int (*filter)(const struct dirent64 *), 511 | int (*compar)(const struct dirent64 **, const struct dirent64 **)) 512 | { 513 | int (*_scandirat64) (int dirfd, const char *dirp, struct dirent64 ***namelist, 514 | int (*filter)(const struct dirent64 *), 515 | int (*compar)(const struct dirent64 **, const struct dirent64 **)); 516 | char *new_path = NULL; 517 | int ret; 518 | 519 | _scandirat64 = (int (*)(int dirfd, const char *dirp, struct dirent64 ***namelist, 520 | int (*filter)(const struct dirent64 *), 521 | int (*compar)(const struct dirent64 **, const struct dirent64 **))) 522 | dlsym (RTLD_NEXT, "scandirat64"); 523 | 524 | new_path = redirect_path_if_absolute (dirp); 525 | ret = _scandirat64 (dirfd, new_path, namelist, filter, compar); 526 | free (new_path); 527 | 528 | return ret; 529 | } 530 | 531 | int 532 | bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen) 533 | { 534 | int (*_bind) (int sockfd, const struct sockaddr *addr, socklen_t addrlen); 535 | int result; 536 | 537 | _bind = (int (*)(int sockfd, const struct sockaddr *addr, socklen_t addrlen)) dlsym (RTLD_NEXT, "bind"); 538 | 539 | if (addr->sa_family == AF_UNIX && ((const struct sockaddr_un *)addr)->sun_path[0] != 0) { // could be abstract socket 540 | char *new_path = NULL; 541 | struct sockaddr_un new_addr; 542 | 543 | new_path = redirect_path (((const struct sockaddr_un *)addr)->sun_path); 544 | 545 | new_addr.sun_family = AF_UNIX; 546 | strcpy (new_addr.sun_path, new_path); 547 | free (new_path); 548 | 549 | result = _bind (sockfd, (const struct sockaddr *)&new_addr, sizeof(new_addr)); 550 | } else { 551 | result = _bind (sockfd, addr, addrlen); 552 | } 553 | 554 | return result; 555 | } 556 | 557 | int 558 | connect (int sockfd, const struct sockaddr *addr, socklen_t addrlen) 559 | { 560 | int (*_connect) (int sockfd, const struct sockaddr *addr, socklen_t addrlen); 561 | 562 | _connect = (int (*)(int sockfd, const struct sockaddr *addr, socklen_t addrlen)) dlsym (RTLD_NEXT, "connect"); 563 | 564 | /* addrlen == sizeof(sa_family_t) is the case of unnamed sockets, 565 | * and first byte of sun_path is 0 for abstract sockets. 566 | */ 567 | if (addr->sa_family == AF_UNIX 568 | && addrlen > sizeof(sa_family_t) 569 | && ((const struct sockaddr_un *) addr)->sun_path[0] != '\0') { 570 | 571 | const struct sockaddr_un *un_addr = (const struct sockaddr_un *) addr; 572 | char *new_path = NULL; 573 | struct sockaddr_un new_addr; 574 | 575 | new_path = redirect_path (un_addr->sun_path); 576 | 577 | new_addr.sun_family = AF_UNIX; 578 | strcpy (new_addr.sun_path, new_path); 579 | free (new_path); 580 | 581 | return _connect (sockfd, (const struct sockaddr *)&new_addr, sizeof(new_addr)); 582 | } 583 | 584 | return _connect (sockfd, addr, addrlen); 585 | } 586 | 587 | void * 588 | dlopen (const char *path, int mode) 589 | { 590 | void *(*_dlopen) (const char *path, int mode); 591 | char *new_path = NULL; 592 | void *result; 593 | 594 | _dlopen = (void *(*)(const char *path, int mode)) dlsym (RTLD_NEXT, "dlopen"); 595 | 596 | if (path && path[0] == '/') { 597 | new_path = redirect_path (path); 598 | result = _dlopen (new_path, mode); 599 | free (new_path); 600 | } else { 601 | // non-absolute library paths aren't simply relative paths, they need 602 | // a whole lookup algorithm 603 | result = _dlopen (path, mode); 604 | } 605 | 606 | return result; 607 | } 608 | 609 | static char * 610 | ensure_in_ld_preload (char *ld_preload, const char *to_be_added) 611 | { 612 | if (ld_preload && ld_preload[0] != 0) { 613 | char *ld_preload_copy; 614 | char *p, *savedptr = NULL; 615 | int found = 0; 616 | 617 | // Check if we are already in LD_PRELOAD and thus can bail 618 | ld_preload_copy = strdup (ld_preload); 619 | for (p = strtok_r (ld_preload_copy + strlen (LD_PRELOAD) + 1, " :", &savedptr); 620 | p; 621 | p = strtok_r (NULL, " :", &savedptr)) { 622 | if (strcmp (p, to_be_added) == 0) { 623 | found = 1; 624 | break; 625 | } 626 | } 627 | free (ld_preload_copy); 628 | 629 | if (!found) { 630 | ld_preload = realloc (ld_preload, strlen (to_be_added) + strlen (ld_preload) + 2); 631 | strcat (ld_preload, ":"); 632 | strcat (ld_preload, to_be_added); 633 | } 634 | } else { 635 | ld_preload = realloc (ld_preload, strlen (to_be_added) + strlen (LD_PRELOAD) + 2); 636 | strcpy (ld_preload, LD_PRELOAD "="); 637 | strcat (ld_preload, to_be_added); 638 | } 639 | 640 | return ld_preload; 641 | } 642 | 643 | static char ** 644 | execve_copy_envp (char *const envp[]) 645 | { 646 | int i, num_elements; 647 | char **new_envp = NULL; 648 | char *ld_preload = NULL; 649 | char *snappy_preload = NULL; 650 | 651 | for (num_elements = 0; envp && envp[num_elements]; num_elements++) { 652 | // this space intentionally left blank 653 | } 654 | 655 | new_envp = malloc (sizeof (char *) * (num_elements + 3)); 656 | 657 | for (i = 0; i < num_elements; i++) { 658 | new_envp[i] = strdup (envp[i]); 659 | if (strncmp (envp[i], LD_PRELOAD "=", strlen (LD_PRELOAD) + 1) == 0) { 660 | ld_preload = new_envp[i]; // point at last defined LD_PRELOAD 661 | } 662 | } 663 | 664 | if (saved_ld_preloads) { 665 | size_t j; 666 | char *ld_preload_copy; 667 | ld_preload_copy = ld_preload ? strdup (ld_preload) : NULL; 668 | for (j = 0; j < num_saved_ld_preloads; j++) { 669 | ld_preload_copy = ensure_in_ld_preload(ld_preload_copy, saved_ld_preloads[j]); 670 | } 671 | new_envp[i++] = ld_preload_copy; 672 | } 673 | 674 | if (saved_snappy_preload) { 675 | snappy_preload = malloc (strlen (saved_snappy_preload) + strlen (SNAPPY_PRELOAD) + 2); 676 | strcpy (snappy_preload, SNAPPY_PRELOAD "="); 677 | strcat (snappy_preload, saved_snappy_preload); 678 | new_envp[i++] = snappy_preload; 679 | } 680 | 681 | new_envp[i++] = NULL; 682 | return new_envp; 683 | } 684 | 685 | static int 686 | execve32_wrapper (int (*_execve) (const char *path, char *const argv[], char *const envp[]), char *path, char *const argv[], char *const envp[]) 687 | { 688 | char *custom_loader = NULL; 689 | char **new_argv; 690 | int i, num_elements, result; 691 | 692 | custom_loader = redirect_path ("/lib/ld-linux.so.2"); 693 | if (strcmp (custom_loader, "/lib/ld-linux.so.2") == 0) { 694 | free (custom_loader); 695 | return 0; 696 | } 697 | 698 | // envp is already adjusted for our needs. But we need to shift argv 699 | for (num_elements = 0; argv && argv[num_elements]; num_elements++) { 700 | // this space intentionally left blank 701 | } 702 | new_argv = malloc (sizeof (char *) * (num_elements + 2)); 703 | new_argv[0] = path; 704 | for (i = 0; i < num_elements; i++) { 705 | new_argv[i + 1] = argv[i]; 706 | } 707 | new_argv[num_elements + 1] = 0; 708 | 709 | // Now actually run execve with our loader and adjusted argv 710 | result = _execve (custom_loader, new_argv, envp); 711 | 712 | // Cleanup on error 713 | free (new_argv); 714 | free (custom_loader); 715 | return result; 716 | } 717 | 718 | static int 719 | execve_wrapper (const char *func, const char *path, char *const argv[], char *const envp[]) 720 | { 721 | int (*_execve) (const char *path, char *const argv[], char *const envp[]); 722 | char *new_path = NULL; 723 | char **new_envp = NULL; 724 | int i, result; 725 | 726 | _execve = (int (*)(const char *path, char *const argv[], char *const envp[])) dlsym (RTLD_NEXT, func); 727 | 728 | new_path = redirect_path (path); 729 | 730 | // Make sure we inject our original preload values, can't trust this 731 | // program to pass them along in envp for us. 732 | new_envp = execve_copy_envp (envp); 733 | 734 | result = _execve (new_path, argv, new_envp); 735 | 736 | if (result == -1 && errno == ENOENT) { 737 | // OK, get prepared for gross hacks here. In order to run 32-bit ELF 738 | // executables -- which will hardcode /lib/ld-linux.so.2 as their ld.so 739 | // loader, we must redirect that check to our own version of ld-linux.so.2. 740 | // But that lookup is done behind the scenes by execve, so we can't 741 | // intercept it like normal. Instead, we'll prefix the command by the 742 | // ld.so loader which will only work if the architecture matches. So if 743 | // we failed to run it normally above because the loader couldn't find 744 | // something, try with our own 32-bit loader. 745 | int (*_access) (const char *pathname, int mode); 746 | _access = (int (*)(const char *pathname, int mode)) dlsym (RTLD_NEXT, "access"); 747 | if (_access (new_path, F_OK) == 0) { 748 | // Only actually try this if the path actually did exist. That 749 | // means the ENOENT must have been a missing linked library or the 750 | // wrong ld.so loader. Lets assume the latter and try to run as 751 | // a 32-bit executable. 752 | result = execve32_wrapper (_execve, new_path, argv, new_envp); 753 | } 754 | } 755 | 756 | free (new_path); 757 | for (i = 0; new_envp[i]; i++) { 758 | free (new_envp[i]); 759 | } 760 | free (new_envp); 761 | 762 | return result; 763 | } 764 | 765 | int 766 | execv (const char *path, char *const argv[]) 767 | { 768 | return execve (path, argv, environ); 769 | } 770 | 771 | int 772 | execve (const char *path, char *const argv[], char *const envp[]) 773 | { 774 | return execve_wrapper ("execve", path, argv, envp); 775 | } 776 | 777 | int 778 | __execve (const char *path, char *const argv[], char *const envp[]) 779 | { 780 | return execve_wrapper ("__execve", path, argv, envp); 781 | } 782 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # -*- Mode: CMake; indent-tabs-mode: nil; tab-width: 4 -*- 2 | # 3 | # Copyright (C) 2015 Canonical, Ltd. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; version 3. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | set(PRELOAD "PRELOAD=${CMAKE_BINARY_DIR}/src/libsnappypreload.so") 18 | 19 | macro(add_preload_test test_target test_command) 20 | set(full_command env ${PRELOAD} BUILDDIR=${CMAKE_BINARY_DIR} ${test_command} ${ARGN}) 21 | add_custom_target(${test_target} 22 | COMMAND ${full_command} 23 | DEPENDS snappypreload) 24 | add_test(NAME ${test_target} 25 | COMMAND ${full_command}) 26 | endmacro() 27 | 28 | add_preload_test(test-exec ${CMAKE_CURRENT_SOURCE_DIR}/exec.sh) 29 | add_preload_test(test-overlay ${CMAKE_CURRENT_SOURCE_DIR}/overlay.sh) 30 | -------------------------------------------------------------------------------- /tests/exec.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # -*- Mode: sh; indent-tabs-mode: nil; tab-width: 4 -*- 3 | # 4 | # Copyright (C) 2015 Canonical, Ltd. 5 | # 6 | # This program 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; version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | set -ex 19 | 20 | ROOT=$(mktemp -d "$BUILDDIR/snappy-preload.test.XXXXXXXXXX") 21 | UNDERLAY="$ROOT/underlay" 22 | OVERLAYROOT="$ROOT/overlay" 23 | OVERLAY="$OVERLAYROOT$UNDERLAY" 24 | 25 | export "SNAPPY_PRELOAD=$OVERLAYROOT" 26 | 27 | mkdir -p "$UNDERLAY" "$OVERLAY" 28 | 29 | echo "#!/bin/sh\nfalse" > "$UNDERLAY/in-both" 30 | echo "#!/bin/sh\ntrue" > "$OVERLAY/in-both" 31 | chmod a+x "$UNDERLAY/in-both" 32 | chmod a+x "$OVERLAY/in-both" 33 | LD_PRELOAD=$PRELOAD sh -c "$UNDERLAY/in-both" 34 | 35 | echo "#!/bin/sh\ntrue" > "$UNDERLAY/only-in-under" 36 | chmod a+x "$UNDERLAY/only-in-under" 37 | LD_PRELOAD=$PRELOAD sh -c "$UNDERLAY/only-in-under" 38 | 39 | echo "#!/bin/sh\ntrue" > "$OVERLAY/only-in-over" 40 | chmod a+x "$OVERLAY/only-in-over" 41 | LD_PRELOAD=$PRELOAD sh -c "$UNDERLAY/only-in-over" 42 | 43 | cat < "$OVERLAY/recurse1" 44 | set -e 45 | echo \$SNAPPY_PRELOAD 46 | echo \$LD_PRELOAD 47 | export LD_PRELOAD=xxx:\$LD_PRELOAD:xxx 48 | count=\$1 49 | count=\$((count-1)) 50 | if [ \$count -ge 0 ]; then 51 | \$0 \$count 52 | fi 53 | EOF 54 | chmod a+x "$OVERLAY/recurse1" 55 | RECURSE_OUTPUT=$(LD_PRELOAD=$PRELOAD sh -c "$UNDERLAY/recurse1 1" | tr '\n' ' ') 56 | test "$RECURSE_OUTPUT" = "$SNAPPY_PRELOAD $PRELOAD $SNAPPY_PRELOAD xxx:$PRELOAD:xxx " 57 | 58 | cat < "$OVERLAY/recurse2" 59 | set -e 60 | echo \$SNAPPY_PRELOAD 61 | echo \$LD_PRELOAD 62 | unset SNAPPY_PRELOAD 63 | unset LD_PRELOAD 64 | count=\$1 65 | count=\$((count-1)) 66 | if [ \$count -ge 0 ]; then 67 | \$0 \$count 68 | fi 69 | EOF 70 | chmod a+x "$OVERLAY/recurse2" 71 | RECURSE_OUTPUT=$(LD_PRELOAD=$PRELOAD sh -c "$UNDERLAY/recurse2 1" | tr '\n' ' ') 72 | test "$RECURSE_OUTPUT" = "$SNAPPY_PRELOAD $PRELOAD $SNAPPY_PRELOAD $PRELOAD " 73 | 74 | cat < "$OVERLAY/recurse3" 75 | set -e 76 | echo \$SNAPPY_PRELOAD 77 | echo \$LD_PRELOAD 78 | export LD_PRELOAD=xxx 79 | count=\$1 80 | count=\$((count-1)) 81 | if [ \$count -ge 0 ]; then 82 | \$0 \$count 83 | fi 84 | EOF 85 | chmod a+x "$OVERLAY/recurse3" 86 | RECURSE_OUTPUT=$(LD_PRELOAD=$PRELOAD sh -c "$UNDERLAY/recurse3 1" | tr '\n' ' ') 87 | test "$RECURSE_OUTPUT" = "$SNAPPY_PRELOAD $PRELOAD $SNAPPY_PRELOAD xxx:$PRELOAD " 88 | 89 | cat < "$OVERLAY/recurse4" 90 | set -e 91 | echo \$SNAPPY_PRELOAD 92 | echo \$LD_PRELOAD 93 | unset SNAPPY_PRELOAD 94 | export LD_PRELOAD=xxx 95 | count=\$1 96 | count=\$((count-1)) 97 | if [ \$count -ge 0 ]; then 98 | \$0 \$count 99 | fi 100 | EOF 101 | chmod a+x "$OVERLAY/recurse4" 102 | RECURSE_OUTPUT=$(LD_PRELOAD=$PRELOAD:xxx:/xxx$PRELOAD sh -c "$UNDERLAY/recurse4 1" | tr '\n' ' ') 103 | test "$RECURSE_OUTPUT" = "$SNAPPY_PRELOAD $PRELOAD:xxx:/xxx$PRELOAD $SNAPPY_PRELOAD xxx:$PRELOAD:/xxx$PRELOAD " 104 | -------------------------------------------------------------------------------- /tests/overlay.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # -*- Mode: sh; indent-tabs-mode: nil; tab-width: 4 -*- 3 | # 4 | # Copyright (C) 2015 Canonical, Ltd. 5 | # 6 | # This program 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; version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | set -ex 19 | 20 | ROOT=$(mktemp -d "$BUILDDIR/snappy-preload.test.XXXXXXXXXX") 21 | UNDERLAY="$ROOT/underlay" 22 | OVERLAYROOT="$ROOT/overlay" 23 | OVERLAY="$OVERLAYROOT$UNDERLAY" 24 | 25 | export "SNAPPY_PRELOAD=$OVERLAYROOT" 26 | 27 | mkdir -p "$UNDERLAY" "$OVERLAY" 28 | 29 | echo "a" > "$UNDERLAY/in-both" 30 | echo "not-in-over" > "$UNDERLAY/not-in-over" 31 | 32 | echo "b" > "$OVERLAY/in-both" 33 | echo "not-in-under" > "$OVERLAY/not-in-under" 34 | 35 | # open 36 | test "$(LD_PRELOAD=$PRELOAD cat $UNDERLAY/in-both)" = "b" 37 | test "$(LD_PRELOAD=$PRELOAD cat $UNDERLAY/not-in-over)" = "not-in-over" 38 | test "$(LD_PRELOAD=$PRELOAD cat $UNDERLAY/not-in-under)" = "not-in-under" 39 | 40 | # opendir 41 | test "$(LD_PRELOAD=$PRELOAD ls -1 $UNDERLAY | tr '\n' ' ')" = "in-both not-in-under " 42 | 43 | # unlink 44 | LD_PRELOAD=$PRELOAD rm $UNDERLAY/in-both 45 | test "$(LD_PRELOAD=$PRELOAD cat $UNDERLAY/in-both)" = "a" 46 | echo "b" > "$OVERLAY/in-both" 47 | 48 | # chmod 49 | chmod 400 "$OVERLAY/in-both" 50 | chmod 600 "$UNDERLAY/in-both" # just for comparison 51 | WRITE_OUTPUT=$(LD_PRELOAD=$PRELOAD sh -c "echo c > $UNDERLAY/in-both" || echo failed) 52 | test "$WRITE_OUTPUT" = "failed" 53 | 54 | # stat 55 | test "$(stat --printf=%a $UNDERLAY/in-both)" = "600" 56 | test "$(LD_PRELOAD=$PRELOAD stat --printf=%a $UNDERLAY/in-both)" = "400" 57 | -------------------------------------------------------------------------------- /tools/copy-deb: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # -*- Mode: sh; indent-tabs-mode: nil; tab-width: 4 -*- 3 | # 4 | # Copyright (C) 2015 Canonical, Ltd. 5 | # 6 | # This program 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; version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | DIR=$1 19 | if [[ $DIR != /* ]]; then 20 | echo "Please specify a target directory as the first argument" 21 | exit 1 22 | fi 23 | shift 24 | 25 | set -o pipefail 26 | set +e 27 | dpkg -L $* | grep '^/' | while read file; do 28 | mkdir -p "$DIR$(dirname $file)" 29 | if [ -h "$file" ] && [ "$(readlink $file)" != "." ]; then 30 | # Disallow a self-redirection like /usr/bin/X11 because then the snap 31 | # size explodes as we do recursive copies. Let's just accept that 32 | # /usr/bin/X11 won't work. 33 | cp -prL "$file" "$DIR$file" 34 | elif [ -d "$file" ]; then 35 | mkdir -p "$DIR$file" 36 | else 37 | cp -p "$file" "$DIR$file" 38 | fi 39 | done 40 | -------------------------------------------------------------------------------- /tools/dbus-daemon: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | exec dbus-daemon --address=unix:path=$(mktemp -u --tmpdir=$SNAP_APP_TMPDIR) $@ 3 | -------------------------------------------------------------------------------- /tools/dep-tree: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4 -*- 3 | # 4 | # Copyright (C) 2015 Canonical, Ltd. 5 | # 6 | # This program 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; version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | import apt 19 | import apt_pkg 20 | import argparse 21 | import sys 22 | 23 | parser = argparse.ArgumentParser() 24 | parser.add_argument('packages', metavar='package', nargs='+') 25 | parser.add_argument('--manifest', metavar='file', default='') 26 | parser.add_argument('--verbose', action="store_true") 27 | parser.add_argument('--ignore', nargs='+') 28 | args = parser.parse_args() 29 | 30 | cache = apt.Cache() 31 | alldeps = set() 32 | manifestdeps = set() 33 | skipped = set() 34 | 35 | if args.manifest: 36 | try: 37 | with open(args.manifest) as f: 38 | for line in f: 39 | words = line.split() 40 | if not words: 41 | continue 42 | pkg = words[0].split(':')[0] 43 | if pkg in cache: 44 | manifestdeps.add(pkg) 45 | except: 46 | print("Could not open manifest file %s" % args.manifest, file=sys.stderr) 47 | sys.exit(1) 48 | 49 | def addDeps(pkgs): 50 | for p in pkgs: 51 | if p in alldeps: 52 | continue 53 | if p in manifestdeps and p not in args.packages: 54 | skipped.add(p) 55 | continue 56 | if args.ignore and p in args.ignore: 57 | continue 58 | try: 59 | cacheName = p.split(':')[0] if ':' in p and apt_pkg.get_architectures()[0] == p.split(':')[1] else p 60 | installedPkg = cache[cacheName].installed 61 | deps = installedPkg.dependencies + installedPkg.recommends 62 | alldeps.add(p) 63 | addDeps([x[0].name for x in deps]) 64 | except Exception: 65 | pass 66 | 67 | addDeps(args.packages) 68 | 69 | exit = False 70 | for p in args.packages: 71 | if p not in alldeps: 72 | exit = True 73 | print("Package %s not installed" % p, file=sys.stderr) 74 | if exit: 75 | sys.exit(1) 76 | 77 | if skipped and args.verbose: 78 | print("Skipped the following manifest packages:", file=sys.stderr) 79 | for d in sorted(skipped): 80 | print(" " + d, file=sys.stderr) 81 | 82 | for d in sorted(alldeps): 83 | print(d) 84 | -------------------------------------------------------------------------------- /tools/fixes: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # -*- Mode: sh; indent-tabs-mode: nil; tab-width: 4 -*- 3 | # 4 | # Copyright (C) 2015 Canonical, Ltd. 5 | # 6 | # This program 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; version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | set -e 19 | 20 | ROOT=$1 21 | PACKAGE=$2 22 | VERSION=$3 23 | 24 | if [ -d $ROOT/debs/etc/X11 ]; then 25 | # X will only start for root user if we don't do this 26 | echo "allowed_users=anybody" >> $ROOT/debs/etc/X11/Xwrapper.config 27 | fi 28 | 29 | if [ -d $ROOT/debs/var/lib/xkb ]; then 30 | # Copy compiled xkb data for Xorg, because bug 972324 prevents Xorg from 31 | # starting if it has to compile them itself when /tmp is on a separate 32 | # filesystem. 33 | cp -p /var/lib/xkb/*.xkm $ROOT/debs/var/lib/xkb/ 34 | fi 35 | 36 | if [ -e $ROOT/debs/usr/share/ca-certificates/mozilla/AC_Raíz_Certicámara_S.A..crt ]; then 37 | # Work around bug 1446820 and its overzealous check for '..' 38 | mv $ROOT/debs/usr/share/ca-certificates/mozilla/AC_Raíz_Certicámara_S.A..crt \ 39 | $ROOT/debs/usr/share/ca-certificates/mozilla/AC_Raíz_Certicámara_S.A.crt 40 | fi 41 | 42 | if [ -e $ROOT/debs/usr/lib/ssl/certs/AC_Raíz_Certicámara_S.A..pem ]; then 43 | # Work around bug 1446820 and its overzealous check for '..' 44 | mv $ROOT/debs/usr/lib/ssl/certs/AC_Raíz_Certicámara_S.A..pem \ 45 | $ROOT/debs/usr/lib/ssl/certs/AC_Raíz_Certicámara_S.A.pem 46 | fi 47 | 48 | exit 0 49 | -------------------------------------------------------------------------------- /tools/wrapper.in: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # -*- Mode: sh; indent-tabs-mode: nil; tab-width: 4 -*- 3 | # 4 | # Copyright (C) 2015 Canonical, Ltd. 5 | # 6 | # This program 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; version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | export SNAPPY_PRELOAD="$SNAP_APP_PATH/debs" 19 | 20 | if [ -d "$SNAPPY_PRELOAD/var/lib" ] && [ -w "$SNAP_APP_DATA_PATH" ]; then 21 | # Pre-seed app-version-specific /var/lib directory with data from snap 22 | cp -rp $SNAPPY_PRELOAD/var/lib/* "$SNAP_APP_DATA_PATH" 23 | fi 24 | 25 | export LD_LIBRARY_PATH=@LDPATHS@ 26 | export LD_PRELOAD=@LDPRELOAD@ 27 | export PATH=$(echo $PATH | sed "s=^/=$SNAP_APP_PATH/debs/=;s=:/=:$SNAP_APP_PATH/debs/=g"):$PATH 28 | 29 | # Bug 1457183 30 | export TMPDIR="$SNAP_APP_TMPDIR" 31 | 32 | exec @COMMAND@ $@ 33 | -------------------------------------------------------------------------------- /tools/xmir-run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # -*- Mode: sh; indent-tabs-mode: nil; tab-width: 4 -*- 3 | # 4 | # Copyright (C) 2015 Canonical, Ltd. 5 | # 6 | # This program 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; version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | /apps/mir/current/bin/mir-run Xorg -mir xorg :1 & 19 | while ! [ -S "$SNAP_APP_TMPDIR/.X11-unix/X1" ]; do 20 | sleep 0.1 21 | done 22 | export DISPLAY=:1 23 | 24 | exec $* 25 | -------------------------------------------------------------------------------- /tools/xmir2-run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # -*- Mode: sh; indent-tabs-mode: nil; tab-width: 4 -*- 3 | # 4 | # Copyright (C) 2015 Canonical, Ltd. 5 | # 6 | # This program 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; version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | /apps/mir/current/bin/mir-run "$SNAP_APP_PATH/bin/_Xmir" -mir xorg -rootless :1 & 19 | while ! [ -S "$SNAP_APP_TMPDIR/.X11-unix/X1" ]; do 20 | sleep 0.1 21 | done 22 | export DISPLAY=:1 23 | 24 | exec $* 25 | --------------------------------------------------------------------------------