listenfd-1.0.0/.cargo_vcs_info.json0000644000000001360000000000100126420ustar { "git": { "sha1": "d96553b7dde28c69e5521abb9c4bcc524112799a" }, "path_in_vcs": "" }listenfd-1.0.0/.github/FUNDING.yml000064400000000000000000000000240072674642500146330ustar 00000000000000github: [mitsuhiko] listenfd-1.0.0/.github/workflows/clippy.yml000064400000000000000000000005140072674642500171020ustar 00000000000000name: Clippy on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: stable profile: minimal components: clippy, rustfmt override: true - name: Run clippy run: make lint listenfd-1.0.0/.github/workflows/rustfmt.yml000064400000000000000000000005260072674642500173110ustar 00000000000000name: Rustfmt on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: stable profile: minimal components: clippy, rustfmt override: true - name: Run rustfmt run: make format-check listenfd-1.0.0/.github/workflows/tests.yml000064400000000000000000000016270072674642500167520ustar 00000000000000name: Tests on: [push] jobs: test-latest: name: Test on Latest runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: stable profile: minimal override: true - name: Test run: make test test-windows-latest: name: Build on Latest (Windows) runs-on: windows-2019 steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: stable profile: minimal override: true - name: Build run: cargo check test-stable: name: Build on 1.42.0 runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: 1.42.0 profile: minimal override: true - name: Build run: cargo check listenfd-1.0.0/.gitignore000064400000000000000000000000370072674642500134520ustar 00000000000000 /target **/*.rs.bk Cargo.lock listenfd-1.0.0/CHANGELOG.md000064400000000000000000000002240072674642500132710ustar 00000000000000# Changelog ## 1.0.0 * Added support for UNIX Datagram sockets. * Update to UUID 1.0. ## 0.5.0 * File descriptors on UNIX now set `FD_CLOEXEC`. listenfd-1.0.0/Cargo.lock0000644000000625530000000000100106300ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "base64" version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "block-buffer" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ "generic-array", ] [[package]] name = "buf_redux" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b953a6887648bb07a535631f2bc00fbdb2a2216f135552cb3f534ed136b9c07f" dependencies = [ "memchr", "safemem", ] [[package]] name = "byteorder" version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "bytes" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cpufeatures" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469" dependencies = [ "libc", ] [[package]] name = "digest" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" dependencies = [ "generic-array", ] [[package]] name = "fastrand" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "779d043b6a0b90cc4c0ed7ee380a6504394cee7efd7db050e3774eee387324b2" dependencies = [ "instant", ] [[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "form_urlencoded" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" dependencies = [ "matches", "percent-encoding", ] [[package]] name = "futures-channel" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba3dda0b6588335f360afc675d0564c17a77a2bda81ca178a4b6081bd86c7f0b" dependencies = [ "futures-core", "futures-sink", ] [[package]] name = "futures-core" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0c8ff0461b82559810cdccfde3215c3f373807f5e5232b71479bff7bb2583d7" [[package]] name = "futures-sink" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3055baccb68d74ff6480350f8d6eb8fcfa3aa11bdc1a1ae3afdd0514617d508" [[package]] name = "futures-task" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ee7c6485c30167ce4dfb83ac568a849fe53274c831081476ee13e0dce1aad72" [[package]] name = "futures-util" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b5cf40b47a271f77a8b1bec03ca09044d99d2372c0de244e66430761127164" dependencies = [ "futures-core", "futures-sink", "futures-task", "pin-project-lite", "pin-utils", "slab", ] [[package]] name = "generic-array" version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803" dependencies = [ "typenum", "version_check", ] [[package]] name = "getrandom" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" dependencies = [ "cfg-if", "libc", "wasi", ] [[package]] name = "h2" version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c9de88456263e249e241fcd211d3954e2c9b0ef7ccfc235a444eb367cae3689" dependencies = [ "bytes", "fnv", "futures-core", "futures-sink", "futures-util", "http", "indexmap", "slab", "tokio", "tokio-util", "tracing", ] [[package]] name = "hashbrown" version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" [[package]] name = "headers" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4c4eb0471fcb85846d8b0690695ef354f9afb11cb03cac2e1d7c9253351afb0" dependencies = [ "base64", "bitflags", "bytes", "headers-core", "http", "httpdate", "mime", "sha-1", ] [[package]] name = "headers-core" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" dependencies = [ "http", ] [[package]] name = "hermit-abi" version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] [[package]] name = "http" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "31f4c6746584866f0feabcc69893c5b51beef3831656a968ed7ae254cdc4fd03" dependencies = [ "bytes", "fnv", "itoa 1.0.1", ] [[package]] name = "http-body" version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ff4f84919677303da5f147645dbea6b1881f368d03ac84e1dc09031ebd7b2c6" dependencies = [ "bytes", "http", "pin-project-lite", ] [[package]] name = "httparse" version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503" [[package]] name = "httpdate" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" [[package]] name = "hyper" version = "0.14.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7ec3e62bdc98a2f0393a5048e4c30ef659440ea6e0e572965103e72bd836f55" dependencies = [ "bytes", "futures-channel", "futures-core", "futures-util", "h2", "http", "http-body", "httparse", "httpdate", "itoa 0.4.8", "pin-project-lite", "socket2", "tokio", "tower-service", "tracing", "want", ] [[package]] name = "idna" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8" dependencies = [ "matches", "unicode-bidi", "unicode-normalization", ] [[package]] name = "indexmap" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" dependencies = [ "autocfg", "hashbrown", ] [[package]] name = "instant" version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ "cfg-if", ] [[package]] name = "itoa" version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] name = "itoa" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" [[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" version = "0.2.112" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" [[package]] name = "listenfd" version = "1.0.0" dependencies = [ "hyper", "libc", "tokio", "uuid", "warp", "winapi", ] [[package]] name = "lock_api" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712a4d093c9976e24e7dbca41db895dabcbac38eb5f4045393d17a95bdfb1109" dependencies = [ "scopeguard", ] [[package]] name = "log" version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ "cfg-if", ] [[package]] name = "matches" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" [[package]] name = "memchr" version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "mime" version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" [[package]] name = "mime_guess" version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212" dependencies = [ "mime", "unicase", ] [[package]] name = "mio" version = "0.7.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8067b404fe97c70829f082dec8bcf4f71225d7eaea1d8645349cb76fa06205cc" dependencies = [ "libc", "log", "miow", "ntapi", "winapi", ] [[package]] name = "miow" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" dependencies = [ "winapi", ] [[package]] name = "multipart" version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00dec633863867f29cb39df64a397cdf4a6354708ddd7759f70c7fb51c5f9182" dependencies = [ "buf_redux", "httparse", "log", "mime", "mime_guess", "quick-error", "rand", "safemem", "tempfile", "twoway", ] [[package]] name = "ntapi" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" dependencies = [ "winapi", ] [[package]] name = "num_cpus" version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" dependencies = [ "hermit-abi", "libc", ] [[package]] name = "once_cell" version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" [[package]] name = "opaque-debug" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "parking_lot" version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" dependencies = [ "instant", "lock_api", "parking_lot_core", ] [[package]] name = "parking_lot_core" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216" dependencies = [ "cfg-if", "instant", "libc", "redox_syscall", "smallvec", "winapi", ] [[package]] name = "percent-encoding" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] name = "pin-project" version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "58ad3879ad3baf4e44784bc6a718a8698867bb991f8ce24d1bcbe2cfb4c3a75e" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "744b6f092ba29c3650faf274db506afd39944f48420f6c86b17cfe0ee1cb36bb" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "pin-project-lite" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c" [[package]] name = "pin-utils" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "ppv-lite86" version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" [[package]] name = "proc-macro2" version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" dependencies = [ "unicode-xid", ] [[package]] name = "quick-error" version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" [[package]] name = "quote" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d" dependencies = [ "proc-macro2", ] [[package]] name = "rand" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" dependencies = [ "libc", "rand_chacha", "rand_core", "rand_hc", ] [[package]] name = "rand_chacha" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", "rand_core", ] [[package]] name = "rand_core" version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" dependencies = [ "getrandom", ] [[package]] name = "rand_hc" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" dependencies = [ "rand_core", ] [[package]] name = "redox_syscall" version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" dependencies = [ "bitflags", ] [[package]] name = "remove_dir_all" version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" dependencies = [ "winapi", ] [[package]] name = "ryu" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" [[package]] name = "safemem" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" [[package]] name = "scoped-tls" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" [[package]] name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" version = "1.0.133" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a" [[package]] name = "serde_json" version = "1.0.74" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee2bb9cd061c5865d345bb02ca49fcef1391741b672b54a0bf7b679badec3142" dependencies = [ "itoa 1.0.1", "ryu", "serde", ] [[package]] name = "serde_urlencoded" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9" dependencies = [ "form_urlencoded", "itoa 0.4.8", "ryu", "serde", ] [[package]] name = "sha-1" version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" dependencies = [ "block-buffer", "cfg-if", "cpufeatures", "digest", "opaque-debug", ] [[package]] name = "signal-hook-registry" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" dependencies = [ "libc", ] [[package]] name = "slab" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5" [[package]] name = "smallvec" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" [[package]] name = "socket2" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5dc90fe6c7be1a323296982db1836d1ea9e47b6839496dde9a541bc496df3516" dependencies = [ "libc", "winapi", ] [[package]] name = "syn" version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a684ac3dcd8913827e18cd09a68384ee66c1de24157e3c556c9ab16d85695fb7" dependencies = [ "proc-macro2", "quote", "unicode-xid", ] [[package]] name = "tempfile" version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" dependencies = [ "cfg-if", "fastrand", "libc", "redox_syscall", "remove_dir_all", "winapi", ] [[package]] name = "thiserror" version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "tinyvec" version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c1c1d5a42b6245520c249549ec267180beaffcc0615401ac8e31853d4b6d8d2" dependencies = [ "tinyvec_macros", ] [[package]] name = "tinyvec_macros" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbbf1c778ec206785635ce8ad57fe52b3009ae9e0c9f574a728f3049d3e55838" dependencies = [ "bytes", "libc", "memchr", "mio", "num_cpus", "once_cell", "parking_lot", "pin-project-lite", "signal-hook-registry", "tokio-macros", "winapi", ] [[package]] name = "tokio-macros" version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "tokio-stream" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50145484efff8818b5ccd256697f36863f587da82cf8b409c53adf1e840798e3" dependencies = [ "futures-core", "pin-project-lite", "tokio", ] [[package]] name = "tokio-tungstenite" version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "511de3f85caf1c98983545490c3d09685fa8eb634e57eec22bb4db271f46cbd8" dependencies = [ "futures-util", "log", "pin-project", "tokio", "tungstenite", ] [[package]] name = "tokio-util" version = "0.6.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e99e1983e5d376cd8eb4b66604d2e99e79f5bd988c3055891dcd8c9e2604cc0" dependencies = [ "bytes", "futures-core", "futures-sink", "log", "pin-project-lite", "tokio", ] [[package]] name = "tower-service" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" [[package]] name = "tracing" version = "0.1.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105" dependencies = [ "cfg-if", "log", "pin-project-lite", "tracing-core", ] [[package]] name = "tracing-core" version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4" dependencies = [ "lazy_static", ] [[package]] name = "try-lock" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" [[package]] name = "tungstenite" version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0b2d8558abd2e276b0a8df5c05a2ec762609344191e5fd23e292c910e9165b5" dependencies = [ "base64", "byteorder", "bytes", "http", "httparse", "log", "rand", "sha-1", "thiserror", "url", "utf-8", ] [[package]] name = "twoway" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59b11b2b5241ba34be09c3cc85a36e56e48f9888862e19cedf23336d35316ed1" dependencies = [ "memchr", ] [[package]] name = "typenum" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" [[package]] name = "unicase" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" dependencies = [ "version_check", ] [[package]] name = "unicode-bidi" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a01404663e3db436ed2746d9fefef640d868edae3cceb81c3b8d5732fda678f" [[package]] name = "unicode-normalization" version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9" dependencies = [ "tinyvec", ] [[package]] name = "unicode-xid" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" [[package]] name = "url" version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c" dependencies = [ "form_urlencoded", "idna", "matches", "percent-encoding", ] [[package]] name = "utf-8" version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" [[package]] name = "uuid" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93bbc61e655a4833cf400d0d15bf3649313422fa7572886ad6dab16d79886365" [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "want" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" dependencies = [ "log", "try-lock", ] [[package]] name = "warp" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3cef4e1e9114a4b7f1ac799f16ce71c14de5778500c5450ec6b7b920c55b587e" dependencies = [ "bytes", "futures-channel", "futures-util", "headers", "http", "hyper", "log", "mime", "mime_guess", "multipart", "percent-encoding", "pin-project", "scoped-tls", "serde", "serde_json", "serde_urlencoded", "tokio", "tokio-stream", "tokio-tungstenite", "tokio-util", "tower-service", "tracing", ] [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" listenfd-1.0.0/Cargo.toml0000644000000024510000000000100106420ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "listenfd" version = "1.0.0" authors = ["Armin Ronacher "] description = "A simple library to work with listenfds passed from the outside (systemd/catflap socket activation)" homepage = "https://github.com/mitsuhiko/rust-listenfd" readme = "README.md" keywords = [ "socket", "listenfd", "systemd", "socketactivation", ] categories = ["os::unix-apis"] license = "Apache-2.0" [dev-dependencies.hyper] version = "0.14.16" [dev-dependencies.tokio] version = "1.15.0" features = ["full"] [dev-dependencies.warp] version = "0.3.2" [target."cfg(not(windows))".dependencies.libc] version = "0.2.40" [target."cfg(windows)".dependencies.uuid] version = "1.0.0" [target."cfg(windows)".dependencies.winapi] version = "0.3.4" features = [ "winsock2", "processthreadsapi", ] listenfd-1.0.0/Cargo.toml.orig000064400000000000000000000013320072674642500143500ustar 00000000000000[package] name = "listenfd" version = "1.0.0" authors = ["Armin Ronacher "] description = "A simple library to work with listenfds passed from the outside (systemd/catflap socket activation)" keywords = ["socket", "listenfd", "systemd", "socketactivation"] categories = [ "os::unix-apis" ] homepage = "https://github.com/mitsuhiko/rust-listenfd" readme = "README.md" license = "Apache-2.0" edition = "2018" [target."cfg(not(windows))".dependencies] libc = "0.2.40" [target."cfg(windows)".dependencies] uuid = "1.0.0" winapi = { version = "0.3.4", features = ["winsock2", "processthreadsapi"] } [dev-dependencies] hyper = "0.14.16" tokio = { version = "1.15.0", features = ["full"] } warp = "0.3.2" listenfd-1.0.0/LICENSE000064400000000000000000000251370072674642500124770ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. listenfd-1.0.0/Makefile000064400000000000000000000005630072674642500131260ustar 00000000000000all: test build: @cargo build --all-features doc: @cargo doc --all-features test: @cargo test format: @rustup component add rustfmt 2> /dev/null @cargo fmt --all format-check: @rustup component add rustfmt 2> /dev/null @cargo fmt --all -- --check lint: @rustup component add clippy 2> /dev/null @cargo clippy .PHONY: all doc test format format-check lint listenfd-1.0.0/README.md000064400000000000000000000040130072674642500127370ustar 00000000000000# listenfd [![Build Status](https://github.com/mitsuhiko/listenfd/workflows/Tests/badge.svg?branch=master)](https://github.com/mitsuhiko/listenfd/actions?query=workflow%3ATests) [![Crates.io](https://img.shields.io/crates/d/listenfd.svg)](https://crates.io/crates/listenfd) [![License](https://img.shields.io/github/license/mitsuhiko/listenfd)](https://github.com/mitsuhiko/listenfd/blob/master/LICENSE) [![rustc 1.42.0](https://img.shields.io/badge/rust-1.42%2B-orange.svg)](https://img.shields.io/badge/rust-1.42%2B-orange.svg) [![Documentation](https://docs.rs/listenfd/badge.svg)](https://docs.rs/listenfd) listenfd is a crate that provides support for working with externally managed and passed file descriptors. This lets you work with systems that support socket activation or similar. Currently this supports systemd on Unix and [systemfd](https://github.com/mitsuhiko/systemfd) on Unix and Windows. systemfd is very convenient in combination with cargo-watch for development purposes whereas systemd is useful for production deployments on linux. ## Example ```rust use listenfd::ListenFd; let mut listenfd = ListenFd::from_env(); let mut server = make_a_server(); // if we are given a tcp listener on listen fd 0, we use that one server = if let Some(listener) = listenfd.take_tcp_listener(0)? { server.listen(listener) // otherwise fall back to local listening } else { server.bind("127.0.0.1:3000")? }; ``` You can then use this with cargo watch and systemfd: ``` $ cargo install systemfd cargo-watch systemfd --no-pid -s http::3000 -- cargo watch -x run ``` Now systemfd will open the socket and keep it open. cargo watch will recompile the code on demand and the server will pick up the socket that systemfd opened. No more connection resets. ## License and Links - [Documentation](https://docs.rs/listenfd/) - [Issue Tracker](https://github.com/mitsuhiko/listenfd/issues) - [Examples](https://github.com/mitsuhiko/listenfd/tree/main/examples) - License: [Apache-2.0](https://github.com/mitsuhiko/listenfd/blob/main/LICENSE) listenfd-1.0.0/examples/demo.rs000064400000000000000000000025750072674642500146030ustar 00000000000000#![deny(warnings)] use hyper::server::Server; use listenfd::ListenFd; use std::convert::Infallible; use warp::Filter; /// You'll need to install `systemfd` and `cargo-watch`: /// ``` /// cargo install systemfd cargo-watch /// ``` /// And run with: /// ``` /// systemfd --no-pid -s http::3030 -- cargo watch -x 'run --example autoreload' /// ``` #[tokio::main] async fn main() { // Match any request and return hello world! let routes = warp::any().map(|| "Hello, World!"); // hyper let's us build a server from a TcpListener (which will be // useful shortly). Thus, we'll need to convert our `warp::Filter` into // a `hyper::service::MakeService` for use with a `hyper::server::Server`. let svc = warp::service(routes); let make_svc = hyper::service::make_service_fn(|_: _| { // the clone is there because not all warp filters impl Copy let svc = svc.clone(); async move { Ok::<_, Infallible>(svc) } }); let mut listenfd = ListenFd::from_env(); // if listenfd doesn't take a TcpListener (i.e. we're not running via // the command above), we fall back to explicitly binding to a given // host:port. let server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { Server::from_tcp(l).unwrap() } else { Server::bind(&([127, 0, 0, 1], 3030).into()) }; server.serve(make_svc).await.unwrap(); } listenfd-1.0.0/src/lib.rs000064400000000000000000000042120072674642500133640ustar 00000000000000//! listenfd is a crate that provides support for working with externally //! managed and passed file descriptors. This lets you work with systems //! that support socket activation or similar. //! //! Currently this supports a slightly modified systemd protocl on unix and //! a custom protocol on Windows. If you want to use this for development //! you can use the [systemfd](https://github.com/mitsuhiko/systemfd) //! utility which implements both those protocols. //! //! The systemd extension is that if the `LISTEN_PID` variable is not set or //! empty, the check for the pid is removed. This is useful when binaries //! are proxied in between like cargo-watch. For the windows protocol //! have a look at the systemfd documentation. //! //! ## Example //! //! This example shows how to use this crate with an `actix-web` server: //! //! ```rust //! # use std::io; //! # struct Server; //! # impl Server { //! # fn listen(self, _: X) -> Self { self } //! # fn bind(self, _: X) -> io::Result { Ok(self) } //! # } //! # fn make_a_server() -> Server { Server }; //! # fn test() -> io::Result<()> { //! use listenfd::ListenFd; //! //! let mut listenfd = ListenFd::from_env(); //! let mut server = make_a_server(); //! //! // if we are given a tcp listener on listen fd 0, we use that one //! server = if let Some(listener) = listenfd.take_tcp_listener(0)? { //! server.listen(listener) //! // otherwise fall back to local listening //! } else { //! server.bind("127.0.0.1:3000")? //! }; //! # Ok(()) } //! ``` //! //! You can then use this with cargo watch and systemfd: //! //! ```plain //! $ cargo install systemfd cargo-watch //! systemfd --no-pid -s 3000 -- cargo watch -x run //! ``` //! //! Now systemfd will open the socket and keep it open. cargo watch will //! recompile the code on demand and the server will pick up the socket //! that systemfd opened. No more connection resets. //! //! The `--no-pid` flag is necessary to ensure that the `LISTEN_PID` environment //! variable is not set or the socket passing will be prevented by the pid check. #[cfg(not(windows))] mod unix; #[cfg(windows)] mod windows; mod manager; pub use manager::*; listenfd-1.0.0/src/manager.rs000064400000000000000000000117570072674642500142440ustar 00000000000000use std::io; use std::net::{TcpListener, UdpSocket}; #[cfg(not(windows))] use std::os::unix::net::{UnixDatagram, UnixListener}; #[cfg(not(windows))] use crate::unix as imp; #[cfg(windows)] use crate::windows as imp; /// A helper object that gives access to raw file descriptors. pub struct ListenFd { fds: Vec>, } impl ListenFd { /// Creates the listenfd manager object from the environment. pub fn from_env() -> ListenFd { match imp::get_fds() { Some(fds) => ListenFd { fds: fds.into_iter().map(Some).collect(), }, None => ListenFd::empty(), } } /// Creates an empty listenfd object. /// /// This is helpful when the ability to work with external file /// descriptors should be disabled in certain code paths. This /// way the functions on the object will just never return /// sockets. pub fn empty() -> ListenFd { ListenFd { fds: vec![] } } /// Returns the number of fds in the manager object. /// /// Note that even if fds are taken out of the manager this count /// does not change. #[allow(clippy::len_without_is_empty)] pub fn len(&self) -> usize { self.fds.len() } fn with_fd io::Result>( &mut self, idx: usize, f: F, ) -> io::Result> { let bucket = match self.fds.get_mut(idx) { Some(None) | None => return Ok(None), Some(bucket) => bucket, }; f(*bucket.as_ref().unwrap()).map(|rv| { bucket.take(); Some(rv) }) } /// Takes the TCP listener at an index. /// /// If the given index has been used before `Ok(None)` is returned, /// otherwise the fd at that index is returned as `TcpListener`. If /// the fd at that position is not a TCP socket then an error is /// returned and the fd is left at its place. pub fn take_tcp_listener(&mut self, idx: usize) -> io::Result> { self.with_fd(idx, imp::make_tcp_listener) } /// Takes the UNIX stream listener at an index. /// /// If the given index has been used before `Ok(None)` is returned, /// otherwise the fd at that index is returned as `UnixListener`. If /// the fd at that position is not a UNIX stream socket then an error is /// returned and the fd is left at its place. /// /// This function is only available on unix platforms. #[cfg(not(windows))] pub fn take_unix_listener(&mut self, idx: usize) -> io::Result> { self.with_fd(idx, imp::make_unix_listener) } /// Takes the UDP socket at an index. /// /// If the given index has been used before `Ok(None)` is returned, /// otherwise the fd at that index is returned as `UdpSocket`. If /// the fd at that position is not a UDP socket then an error is /// returned and the fd is left at its place. pub fn take_udp_socket(&mut self, idx: usize) -> io::Result> { let _idx = idx; self.with_fd(idx, imp::make_udp_socket) } /// Takes the UNIX datagram socket at an index. /// /// If the given index has been used before `Ok(None)` is returned, /// otherwise the fd at that index is returned as `UnixDatagram`. If /// the fd at that position is not a UNIX datagram socket then an error is /// returned and the fd is left at its place. /// /// This function is only available on unix platforms. #[cfg(not(windows))] pub fn take_unix_datagram(&mut self, idx: usize) -> io::Result> { self.with_fd(idx, imp::make_unix_datagram) } /// Takes a custom socket on unix platforms. /// /// You have to provide a socket family, socket type, and a hint /// for the validation error, e.g. /// `libc::AF_UNIX, libc::SOCK_SEQPACKET, "unix seqpacket socket"`. /// /// The file descriptor will be validated to actually be a socket /// with the appropriate options, set as CLOEXEC, and converted to /// the given Rust type using `FromRawFd`. /// /// This function is only available on unix platforms. #[cfg(not(windows))] pub fn take_custom( &mut self, idx: usize, sock_fam: libc::c_int, sock_type: libc::c_int, hint: &str, ) -> io::Result> { self.with_fd(idx, |fd| imp::make_custom(fd, sock_fam, sock_type, hint)) } /// Takes the `RawFd` on unix platforms. #[cfg(not(windows))] pub fn take_raw_fd(&mut self, idx: usize) -> io::Result> { let _idx = idx; self.with_fd(idx, Ok) } /// Takes the `RawSocket` on windows platforms. /// /// This will error if the fd at this position is not a socket (but a handle). #[cfg(windows)] pub fn take_raw_socket(&mut self, idx: usize) -> io::Result> { let _idx = idx; Ok(None) } } listenfd-1.0.0/src/unix.rs000064400000000000000000000075150072674642500136120ustar 00000000000000use std::env; use std::io; use std::mem; use std::net::{TcpListener, UdpSocket}; use std::os::unix::io::{FromRawFd, RawFd}; use std::os::unix::net::{UnixDatagram, UnixListener}; pub type FdType = RawFd; fn is_sock(fd: FdType) -> bool { unsafe { let mut stat: libc::stat = mem::zeroed(); libc::fstat(fd as libc::c_int, &mut stat); (stat.st_mode & libc::S_IFMT) == libc::S_IFSOCK } } fn validate_socket( fd: FdType, sock_fam: libc::c_int, sock_type: libc::c_int, hint: &str, ) -> io::Result { if !is_sock(fd) { return Err(io::Error::new( io::ErrorKind::InvalidInput, format!("fd {} is not a socket", fd), )); } let is_valid = unsafe { let mut ty: libc::c_int = mem::zeroed(); let mut ty_len = mem::size_of_val(&ty) as libc::c_uint; let mut sockaddr: libc::sockaddr = mem::zeroed(); let mut sockaddr_len = mem::size_of_val(&sockaddr) as libc::c_uint; libc::getsockname(fd, &mut sockaddr, &mut sockaddr_len) == 0 && libc::getsockopt( fd, libc::SOL_SOCKET, libc::SO_TYPE, mem::transmute(&mut ty), &mut ty_len, ) == 0 && ty == sock_type && (sockaddr.sa_family as libc::c_int == sock_fam || (sockaddr.sa_family as libc::c_int == libc::AF_INET6 && sock_fam == libc::AF_INET)) }; if !is_valid { return Err(io::Error::new( io::ErrorKind::InvalidInput, format!("fd {} is not a valid {}", fd, hint), )); } Ok(fd) } fn mark_cloexec(fd: i32) -> io::Result { let rv = unsafe { libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC) }; if rv < 0 { return Err(io::Error::new( io::ErrorKind::Other, format!( "fd {} cannot be marked as FD_CLOEXEC ({})", fd, io::Error::last_os_error() ), )); } Ok(fd) } pub fn make_tcp_listener(fd: FdType) -> io::Result { validate_socket(fd, libc::AF_INET, libc::SOCK_STREAM, "tcp socket") .and_then(mark_cloexec) .map(|fd| unsafe { FromRawFd::from_raw_fd(fd) }) } pub fn make_unix_listener(fd: FdType) -> io::Result { validate_socket(fd, libc::AF_UNIX, libc::SOCK_STREAM, "unix stream socket") .and_then(mark_cloexec) .map(|fd| unsafe { FromRawFd::from_raw_fd(fd) }) } pub fn make_udp_socket(fd: FdType) -> io::Result { validate_socket(fd, libc::AF_INET, libc::SOCK_DGRAM, "udp socket") .and_then(mark_cloexec) .map(|fd| unsafe { FromRawFd::from_raw_fd(fd) }) } pub fn make_unix_datagram(fd: FdType) -> io::Result { validate_socket(fd, libc::AF_UNIX, libc::SOCK_DGRAM, "unix datagram socket") .and_then(mark_cloexec) .map(|fd| unsafe { FromRawFd::from_raw_fd(fd) }) } pub fn make_custom( fd: FdType, sock_fam: libc::c_int, sock_type: libc::c_int, hint: &str, ) -> io::Result { validate_socket(fd, sock_fam, sock_type, hint) .and_then(mark_cloexec) .map(|fd| unsafe { FromRawFd::from_raw_fd(fd) }) } pub fn get_fds() -> Option> { // modified systemd protocol if let Some(count) = env::var("LISTEN_FDS").ok().and_then(|x| x.parse().ok()) { let ok = match env::var("LISTEN_PID").as_ref().map(|x| x.as_str()) { Err(env::VarError::NotPresent) | Ok("") => true, Ok(val) if val.parse().ok() == Some(unsafe { libc::getpid() }) => true, _ => false, }; env::remove_var("LISTEN_PID"); env::remove_var("LISTEN_FDS"); if ok { return Some((0..count).map(|offset| 3 + offset as FdType).collect()); } } None } listenfd-1.0.0/src/windows.rs000064400000000000000000000063630072674642500143210ustar 00000000000000use std::env; use std::io::{self, Read, Write}; use std::mem; use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream, UdpSocket}; use uuid::Uuid; pub use std::os::windows::io::{FromRawSocket, RawSocket}; use winapi::ctypes::{c_uint, c_ulong, c_void}; use winapi::shared::ntdef::{HANDLE, NTSTATUS}; use winapi::um::processthreadsapi::GetCurrentProcessId; use winapi::um::winsock2::{ WSASocketW, FROM_PROTOCOL_INFO, WSAPROTOCOL_INFOW, WSA_FLAG_OVERLAPPED, }; pub use self::RawSocket as FdType; pub fn make_tcp_listener(fd: FdType) -> io::Result { Ok(unsafe { FromRawSocket::from_raw_socket(fd) }) } pub fn make_udp_socket(fd: FdType) -> io::Result { Ok(unsafe { FromRawSocket::from_raw_socket(fd) }) } #[repr(C)] pub struct IO_STATUS_BLOCK { status: *const c_void, information: *const c_ulong, } #[repr(C)] pub struct FILE_INFORMATION { port: HANDLE, key: *const c_void, } #[link(name = "ntdll")] extern "system" { #[link_name = "NtSetInformationFile"] fn NtSetInformationFile( file_handle: HANDLE, block: *mut IO_STATUS_BLOCK, file_info: *mut FILE_INFORMATION, len: c_ulong, cls: c_uint, ) -> NTSTATUS; } /// This detaches a socket from IOCP. /// /// This only works on windows 8.1 and later. In ealier windows /// versions it's not possible to detach the socket until shutdown /// which means that stuff that uses IOCP (like tokio) cannot be /// used for reloading type scenarios. unsafe fn detach_from_iocp(sock: FdType) { let mut status_block: IO_STATUS_BLOCK = mem::zeroed(); let mut file_info: FILE_INFORMATION = mem::zeroed(); // FileReplaceCompletionInformation NtSetInformationFile( sock as HANDLE, &mut status_block, &mut file_info, mem::size_of::() as c_ulong, 61, ); } pub fn get_fds() -> Option> { let addr: SocketAddr = env::var("SYSTEMFD_SOCKET_SERVER") .ok() .and_then(|x| x.parse().ok())?; let secret: Uuid = env::var("SYSTEMFD_SOCKET_SECRET") .ok() .and_then(|x| x.parse().ok())?; env::remove_var("SYSTEMFD_SOCKET_SERVER"); env::remove_var("SYSTEMFD_SOCKET_SECRET"); let mut data = Vec::new(); let proto_len = mem::size_of::(); let mut listener = TcpStream::connect(addr).ok()?; let pid = unsafe { GetCurrentProcessId() }; listener .write_all(format!("{}|{}", secret, pid).as_bytes()) .ok()?; listener.shutdown(Shutdown::Write).ok()?; listener.read_to_end(&mut data).ok()?; let items = data.len() / proto_len; let mut rv = Vec::new(); for idx in 0..items { let offset = idx * proto_len; let proto_info: &mut WSAPROTOCOL_INFOW = unsafe { mem::transmute(data[offset..offset + proto_len].as_ptr()) }; unsafe { let sock = WSASocketW( FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, proto_info, 0, WSA_FLAG_OVERLAPPED, ) as FdType; if sock == 0 { return None; } detach_from_iocp(sock); rv.push(sock); } } Some(rv) }