From ce8380f3137519a54ea1b45ed8daf6f7966f0644 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Sun, 12 Jan 2020 03:09:40 +0100 Subject: [PATCH] WIP Initial Tokio port --- Cargo.lock | 333 ++++++++++++++++++++++++++++++++ greetd/Cargo.toml | 3 + greetd/src/main.rs | 169 +++++++++++----- greetd/src/pollable/client.rs | 147 -------------- greetd/src/pollable/listener.rs | 47 ----- greetd/src/pollable/mod.rs | 30 --- greetd/src/pollable/signals.rs | 63 ------ greetd/src/session.rs | 14 +- 8 files changed, 465 insertions(+), 341 deletions(-) delete mode 100644 greetd/src/pollable/client.rs delete mode 100644 greetd/src/pollable/listener.rs delete mode 100644 greetd/src/pollable/mod.rs delete mode 100644 greetd/src/pollable/signals.rs diff --git a/Cargo.lock b/Cargo.lock index 9f2a48d..155c2f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,6 +19,11 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "arc-swap" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "atty" version = "0.2.13" @@ -38,6 +43,11 @@ name = "byteorder" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "bytes" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cc" version = "1.0.48" @@ -62,6 +72,107 @@ dependencies = [ "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "fnv" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fuchsia-zircon" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-channel 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-executor 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-io 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-sink 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-task 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-channel" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-sink 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-executor" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-task 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-io" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-macro" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-sink" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-task" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-util" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-channel 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-io 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-macro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-sink 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-task 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-nested 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "greet_proto" version = "0.2.0" @@ -85,25 +196,102 @@ version = "0.1.1" dependencies = [ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "greet_proto 0.2.0", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)", "pam-sys 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-util 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "users 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "iovec" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itoa" version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "libc" version = "0.2.66" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "log" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memchr" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "mio" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "mio-uds" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "miow" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "multimap" version = "0.8.0" @@ -112,6 +300,16 @@ dependencies = [ "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "net2" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "nix" version = "0.16.1" @@ -132,6 +330,31 @@ dependencies = [ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "pin-project-lite" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "pin-utils" +version = "0.1.0-alpha.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "proc-macro-hack" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro-nested" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro2" version = "1.0.7" @@ -198,6 +421,20 @@ dependencies = [ "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "signal-hook-registry" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arc-swap 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "slab" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "strsim" version = "0.8.0" @@ -221,6 +458,48 @@ dependencies = [ "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tokio" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", + "pin-project-lite 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "signal-hook-registry 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-macros 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tokio-macros" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tokio-util" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-sink 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "pin-project-lite 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "toml" version = "0.5.5" @@ -257,6 +536,11 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.3.8" @@ -266,6 +550,11 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" @@ -276,19 +565,55 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "ws2_32-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [metadata] "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +"checksum arc-swap 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d7b8a9123b8027467bce0099fe556c628a53c8d83df0507084c31e9ba2e39aff" "checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" +"checksum bytes 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "10004c15deb332055f7a4a208190aed362cf9a7c2f6ab70a305fba50e1105f38" "checksum cc 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)" = "f52a465a666ca3d838ebbf08b241383421412fe7ebb463527bba275526d89f76" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" +"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" +"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +"checksum futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b6f16056ecbb57525ff698bb955162d0cd03bee84e6241c27ff75c08d8ca5987" +"checksum futures-channel 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fcae98ca17d102fd8a3603727b9259fcf7fa4239b603d2142926189bc8999b86" +"checksum futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "79564c427afefab1dfb3298535b21eda083ef7935b4f0ecbfcb121f0aec10866" +"checksum futures-executor 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1e274736563f686a837a0568b478bdabfeaec2dca794b5649b04e2fe1627c231" +"checksum futures-io 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e676577d229e70952ab25f3945795ba5b16d63ca794ca9d2c860e5595d20b5ff" +"checksum futures-macro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "52e7c56c15537adb4f76d0b7a76ad131cb4d2f4f32d3b0bcabcbe1c7c5e87764" +"checksum futures-sink 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "171be33efae63c2d59e6dbba34186fe0d6394fb378069a76dfd80fdcffd43c16" +"checksum futures-task 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0bae52d6b29cf440e298856fec3965ee6fa71b06aa7495178615953fd669e5f9" +"checksum futures-util 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c0d66274fb76985d3c62c886d1da7ac4c0903a8c9f754e8fe0f35a6a6cc39e76" +"checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" +"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" +"checksum memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3197e20c7edb283f87c071ddfc7a2cca8f8e0b888c242959846a6fce03c72223" +"checksum mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)" = "302dec22bcf6bae6dfb69c647187f4b4d0fb6f535521f7bc022430ce8e12008f" +"checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" +"checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" "checksum multimap 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a97fbd5d00e0e37bfb10f433af8f5aaf631e739368dc9fc28286ca81ca4948dc" +"checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum nix 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dd0eaf8df8bab402257e0a5c17a254e4cc1f72a93588a1ddfb5d356c801aa7cb" "checksum pam-sys 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "cd4858311a097f01a0006ef7d0cd50bca81ec430c949d7bf95cbefd202282434" +"checksum pin-project-lite 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e8822eb8bb72452f038ebf6048efa02c3fe22bf83f76519c9583e47fc194a422" +"checksum pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587" +"checksum proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd45702f76d6d3c75a80564378ae228a85f0b59d2f3ed43c91b4a69eb2ebfc5" +"checksum proc-macro-nested 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "369a6ed065f249a159e06c45752c780bda2fb53c995718f9e484d08daa9eb42e" "checksum proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "0319972dcae462681daf4da1adeeaa066e3ebd29c69be96c6abb1259d2ee2bcc" "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" "checksum rpassword 4.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d59f0e97173c514b9036cd450c195a6483ba81055c6fa0f1bff3ab563f47d44a" @@ -297,15 +622,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449" "checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64" "checksum serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)" = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7" +"checksum signal-hook-registry 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94f478ede9f64724c5d173d7bb56099ec3e2d9fc2774aac65d34b8b890405f41" +"checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1e4ff033220a41d1a57d8125eab57bf5263783dfdcc18688b1dacc6ce9651ef8" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +"checksum tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "ffa2fdcfa937b20cb3c822a635ceecd5fc1a27a6a474527e5516aa24b8c8820a" +"checksum tokio-macros 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "50a61f268a3db2acee8dcab514efc813dc6dbe8a00e86076f935f94304b59a7a" +"checksum tokio-util 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "571da51182ec208780505a32528fc5512a8fe1443ab960b3f2f3ef093cd16930" "checksum toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "01d1404644c8b12b16bfcffa4322403a91a451584daaaa7c28d3152e6cbc98cf" "checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum users 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c72f4267aea0c3ec6d07eaabea6ead7c5ddacfafc5e22bcf8d186706851fb4cf" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +"checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" diff --git a/greetd/Cargo.toml b/greetd/Cargo.toml index 60acf5a..10a79bc 100644 --- a/greetd/Cargo.toml +++ b/greetd/Cargo.toml @@ -14,3 +14,6 @@ serde = { version = "1.0", features = ["derive"] } greet_proto = { path = "../greet_proto" } libc = "0.2" byteorder = "1.3" +futures = "0.3" +tokio = { version = "0.2", features = ["net", "stream", "sync", "macros", "signal", "rt-util", "io-util"] } +tokio-util = { version = "0.2", features = ["codec"] } diff --git a/greetd/src/main.rs b/greetd/src/main.rs index be02afc..74b0b86 100644 --- a/greetd/src/main.rs +++ b/greetd/src/main.rs @@ -1,21 +1,31 @@ mod config; mod context; mod pam; -mod pollable; mod scrambler; mod session; mod terminal; +use std::cell::RefCell; use std::error::Error; +use std::io; +use std::rc::Rc; -use nix::poll::{poll, PollFd}; use nix::unistd::{chown, Gid, Uid}; +use crate::scrambler::Scrambler; use config::VtSelection; use context::Context; -use pollable::{Listener, PollRunResult, Pollable, Signals}; +use greet_proto::{Failure, Header, Request, Response}; use terminal::Terminal; +use tokio::{ + net::{UnixListener, UnixStream}, + prelude::*, + signal::unix::{signal, SignalKind}, + stream::StreamExt, + task, +}; + fn reset_vt(vt: usize) -> Result<(), Box> { let term = Terminal::open(vt)?; term.kd_setmode(terminal::KdMode::Text)?; @@ -23,7 +33,53 @@ fn reset_vt(vt: usize) -> Result<(), Box> { Ok(()) } -fn main() { +async fn client(ctx: Rc>>, mut s: UnixStream) -> Result<(), Box> { + loop { + let mut header_bytes = [0; Header::len()]; + + s.read_exact(&mut header_bytes[..]).await?; + let header = Header::from_slice(&header_bytes)?; + if header.version != 1 { + return Err(io::Error::new(io::ErrorKind::Other, "invalid message version").into()); + } + + let mut body_bytes = vec![0; header.len as usize]; + s.read_exact(&mut body_bytes[..]).await?; + + let req = Request::from_slice(&body_bytes)?; + body_bytes.scramble(); + + let resp = match req { + Request::Login { + username, + password, + cmd, + env, + } => match ctx.borrow_mut().login(username, password, cmd, env) { + Ok(_) => Response::Success, + Err(e) => Response::Failure(Failure::LoginError { + description: format!("{}", e), + }), + }, + Request::Shutdown { action } => match ctx.borrow_mut().shutdown(action) { + Ok(_) => Response::Success, + Err(e) => Response::Failure(Failure::ShutdownError { + action, + description: format!("{}", e), + }), + }, + }; + + let resp_bytes = resp.to_bytes().expect("unable to serialize response"); + let header = Header::new(resp_bytes.len() as u32); + let header_bytes = header.to_bytes().expect("unable to serialize header"); + + s.write_all(&header_bytes).await?; + } +} + +#[tokio::main] +async fn main() { let config = config::read_config(); eprintln!("starting greetd"); @@ -31,7 +87,7 @@ fn main() { std::env::set_var("GREETD_SOCK", &config.socket_path); let _ = std::fs::remove_file(config.socket_path.clone()); - let listener = Listener::new(&config.socket_path).expect("unable to create listener"); + let mut listener = UnixListener::bind(&config.socket_path).expect("unable to create listener"); let u = users::get_user_by_name(&config.greeter_user).expect("unable to get user struct"); let uid = Uid::from_raw(u.uid()); @@ -39,8 +95,6 @@ fn main() { chown(config.socket_path.as_str(), Some(uid), Some(gid)) .expect("unable to chown greetd socket"); - let signals = Signals::new().expect("unable to create signalfd"); - let term = Terminal::open(0).expect("unable to open controlling terminal"); let vt = match config.vt() { VtSelection::Current => term.vt_get_current().expect("unable to get current VT"), @@ -49,61 +103,72 @@ fn main() { }; drop(term); - let mut ctx = Context::new(config.greeter, config.greeter_user, vt); - if let Err(e) = ctx.greet() { + let ctx = Rc::new(RefCell::new(Context::new( + config.greeter, + config.greeter_user, + vt, + ))); + if let Err(e) = ctx.borrow_mut().greet() { eprintln!("unable to start greeter: {}", e); reset_vt(vt).expect("unable to reset vt"); std::process::exit(1); } - let mut pollables: Vec> = vec![Box::new(listener), Box::new(signals)]; + let mut incoming = listener.incoming(); - let mut fds: Vec = Vec::new(); - let mut fds_changed = true; + let local = task::LocalSet::new(); + local + .run_until(async move { + let alarm_ctx = ctx.clone(); + task::spawn_local(async move { + let mut alarm = signal(SignalKind::alarm()).expect("unable to listen for SIGALRM"); + loop { + alarm.recv().await; + alarm_ctx + .borrow_mut() + .alarm() + .expect("unable to read alarm"); + } + }); - loop { - if fds_changed { - fds_changed = false; - fds = pollables - .iter() - .map(|x| PollFd::new(x.fd(), x.poll_flags())) - .collect(); - } + let child_ctx = ctx.clone(); + task::spawn_local(async move { + let mut child = signal(SignalKind::child()).expect("unable to listen for SIGCHLD"); + loop { + child.recv().await; + child_ctx + .borrow_mut() + .check_children() + .expect("unable to check children"); + } + }); - if let Err(e) = poll(&mut fds, -1) { - eprintln!("poll failed: {}", e); - reset_vt(vt).expect("unable to reset vt"); - std::process::exit(1); - } + let term_ctx = ctx.clone(); + task::spawn_local(async move { + let mut term = + signal(SignalKind::terminate()).expect("unable to listen for SIGTERM"); + loop { + term.recv().await; + term_ctx + .borrow_mut() + .terminate() + .expect("unable to terminate"); + } + }); - let mut idx_compensation: isize = 0; - for (idx, fd) in fds.iter().enumerate() { - if let Some(revents) = fd.revents() { - let pollable = &mut pollables[(idx as isize + idx_compensation) as usize]; - if revents.intersects(pollable.poll_flags()) { - match pollable.run(&mut ctx) { - Ok(PollRunResult::Uneventful) => (), - Ok(PollRunResult::NewPollable(p)) => { - // New pollables are pushed at the end, so they do - // not require index compensation. - pollables.push(p); - fds_changed = true; - } - Ok(PollRunResult::Dead) => { - // We remove a pollable at the current index, so - // compensate the index of future accesses. - idx_compensation -= 1; - pollables.remove(idx); - fds_changed = true; - } - Err(e) => { - eprintln!("task failed: {}", e); - reset_vt(vt).expect("unable to reset vt"); - std::process::exit(1); - } + while let Some(stream) = incoming.next().await { + match stream { + Ok(stream) => { + let ctx1 = ctx.clone(); + task::spawn_local(async move { + if let Err(e) = client(ctx1, stream).await { + eprintln!("client loop failed: {}", e); + } + }); } + Err(e) => eprintln!("accept failed: {}", e), } } - } - } + }) + .await; } diff --git a/greetd/src/pollable/client.rs b/greetd/src/pollable/client.rs deleted file mode 100644 index b7e122b..0000000 --- a/greetd/src/pollable/client.rs +++ /dev/null @@ -1,147 +0,0 @@ -use std::error::Error; -use std::io; -use std::io::{Read, Take, Write}; -use std::os::unix::io::{AsRawFd, RawFd}; -use std::os::unix::net::UnixStream; - -use nix::fcntl::{fcntl, FcntlArg, FdFlag}; -use nix::poll::PollFlags; - -use super::{PollRunResult, Pollable}; -use crate::context::Context; -use crate::scrambler::Scrambler; -use greet_proto::{Failure, Header, Request, Response}; - -enum ClientState { - AwaitingHeader, - AwaitingPayload { len: u32 }, -} - -pub struct Client { - stream: Take, - buf: Vec, - state: ClientState, -} - -impl Client { - pub fn new(stream: UnixStream) -> Result> { - stream.set_nonblocking(true)?; - let fd = stream.as_raw_fd(); - let flags = fcntl(fd, FcntlArg::F_GETFD)?; - fcntl( - fd, - FcntlArg::F_SETFD(FdFlag::from_bits(flags).unwrap() | FdFlag::FD_CLOEXEC), - )?; - Ok(Client { - stream: stream.take(Header::len() as u64), - buf: Vec::new(), - state: ClientState::AwaitingHeader, - }) - } -} - -impl Pollable for Client { - fn fd(&self) -> RawFd { - self.stream.get_ref().as_raw_fd() - } - - fn poll_flags(&self) -> PollFlags { - PollFlags::POLLIN - } - - fn run(&mut self, ctx: &mut Context) -> Result> { - loop { - match self.state { - ClientState::AwaitingHeader => { - match self.stream.read_to_end(&mut self.buf) { - Ok(_) => { - if self.buf.len() < Header::len() { - // Got EOF before we got enough data. - self.buf.scramble(); - break Ok(PollRunResult::Dead); - } - let header = Header::from_slice(self.buf.as_slice())?; - - if header.version != 1 { - return Err(io::Error::new( - io::ErrorKind::Other, - "invalid message version", - ) - .into()); - } - - self.state = ClientState::AwaitingPayload { len: header.len }; - self.stream.set_limit(header.len as u64); - self.buf.truncate(0); - } - Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { - break Ok(PollRunResult::Uneventful) - } - Err(_) => { - self.buf.scramble(); - break Ok(PollRunResult::Dead); - } - } - } - ClientState::AwaitingPayload { len } => { - match self.stream.read_to_end(&mut self.buf) { - Ok(_) => { - if self.buf.len() < len as usize { - // Got EOF before we got enough data. - self.buf.scramble(); - break Ok(PollRunResult::Dead); - } - self.state = ClientState::AwaitingHeader; - self.stream.set_limit(Header::len() as u64); - - let req = Request::from_slice(&self.buf)?; - self.buf.scramble(); - - let resp = match req { - Request::Login { - username, - password, - cmd, - env, - } => match ctx.login(username, password, cmd, env) { - Ok(_) => Response::Success, - Err(e) => Response::Failure(Failure::LoginError { - description: format!("{}", e), - }), - }, - Request::Shutdown { action } => match ctx.shutdown(action) { - Ok(_) => Response::Success, - Err(e) => Response::Failure(Failure::ShutdownError { - action, - description: format!("{}", e), - }), - }, - }; - - let resp_bytes = resp.to_bytes().expect("unable to serialize response"); - let header = Header::new(resp_bytes.len() as u32); - let header_bytes = - header.to_bytes().expect("unable to serialize header"); - - if self.stream.get_mut().write_all(&header_bytes).is_err() { - eprintln!("unable to write response header"); - break Ok(PollRunResult::Dead); - } - if self.stream.get_mut().write_all(&resp_bytes).is_err() { - eprintln!("unable to write response"); - break Ok(PollRunResult::Dead); - } - } - Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { - break Ok(PollRunResult::Uneventful) - } - Err(_) => { - self.buf.scramble(); - break Ok(PollRunResult::Dead); - } - } - } - } - } - } -} diff --git a/greetd/src/pollable/listener.rs b/greetd/src/pollable/listener.rs deleted file mode 100644 index 47f7dad..0000000 --- a/greetd/src/pollable/listener.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::error::Error; -use std::os::unix::io::{AsRawFd, RawFd}; -use std::os::unix::net::UnixListener; - -use nix::fcntl::{fcntl, FcntlArg, FdFlag}; -use nix::poll::PollFlags; - -use super::client::Client; -use super::{PollRunResult, Pollable}; -use crate::context::Context; - -pub struct Listener { - listener: UnixListener, -} - -impl Listener { - pub fn new(p: &str) -> Result> { - let listener = UnixListener::bind(p)?; - listener.set_nonblocking(true)?; - let fd = listener.as_raw_fd(); - let flags = fcntl(fd, FcntlArg::F_GETFD)?; - fcntl( - fd, - FcntlArg::F_SETFD(FdFlag::from_bits(flags).unwrap() | FdFlag::FD_CLOEXEC), - )?; - Ok(Listener { listener }) - } -} - -impl Pollable for Listener { - fn fd(&self) -> RawFd { - self.listener.as_raw_fd() - } - - fn poll_flags(&self) -> PollFlags { - PollFlags::POLLIN - } - - fn run(&mut self, _: &mut Context) -> Result> { - let stream = match self.listener.accept() { - Ok((stream, _)) => stream, - Err(_) => return Ok(PollRunResult::Uneventful), - }; - - Ok(PollRunResult::NewPollable(Box::new(Client::new(stream)?))) - } -} diff --git a/greetd/src/pollable/mod.rs b/greetd/src/pollable/mod.rs deleted file mode 100644 index 18f3f3b..0000000 --- a/greetd/src/pollable/mod.rs +++ /dev/null @@ -1,30 +0,0 @@ -pub mod client; -pub mod listener; -pub mod signals; - -pub use client::Client; -pub use listener::Listener; -pub use signals::Signals; - -use std::error::Error; -use std::os::unix::io::RawFd; - -use nix::poll::PollFlags; - -use crate::context::Context; - -pub enum PollRunResult { - Uneventful, - Dead, - NewPollable(Box), -} - -/// A Pollable is an event loop entry that will be scheduled when its fd has -/// activity matching what was subscribed for. It can optionally remove itself -/// or add another pollable to the event loop (such as in the case of pollables -/// that call accept()). -pub trait Pollable { - fn fd(&self) -> RawFd; - fn poll_flags(&self) -> PollFlags; - fn run(&mut self, ctx: &mut Context) -> Result>; -} diff --git a/greetd/src/pollable/signals.rs b/greetd/src/pollable/signals.rs deleted file mode 100644 index 5b00344..0000000 --- a/greetd/src/pollable/signals.rs +++ /dev/null @@ -1,63 +0,0 @@ -use std::convert::TryFrom; -use std::error::Error; -use std::os::unix::io::{AsRawFd, RawFd}; - -use nix::poll::PollFlags; -use nix::sys::signal::{SigSet, Signal}; -use nix::sys::signalfd::{SfdFlags, SignalFd}; - -use super::{PollRunResult, Pollable}; -use crate::context::Context; - -/// Returns a set containing the signals we want to block in the main process. -/// This is also used to unblock the same signals again before starting child -/// processes. -pub fn blocked_sigset() -> SigSet { - let mut mask = SigSet::empty(); - mask.add(Signal::SIGALRM); - mask.add(Signal::SIGTERM); - mask.add(Signal::SIGCHLD); - mask -} - -/// A pollable that listens for SIGALRM, SIGTERM and SIGCHLD and reports it to -/// the execution context. -pub struct Signals { - listener: SignalFd, -} - -impl Signals { - pub fn new() -> Result> { - let mask = blocked_sigset(); - mask.thread_block()?; - - let listener = SignalFd::with_flags(&mask, SfdFlags::SFD_NONBLOCK | SfdFlags::SFD_CLOEXEC)?; - - Ok(Signals { listener }) - } -} - -impl Pollable for Signals { - fn fd(&self) -> RawFd { - self.listener.as_raw_fd() - } - - fn poll_flags(&self) -> PollFlags { - PollFlags::POLLIN - } - - fn run(&mut self, ctx: &mut Context) -> Result> { - loop { - match self.listener.read_signal() { - Ok(Some(sig)) => match Signal::try_from(sig.ssi_signo as i32) { - Ok(Signal::SIGALRM) => ctx.alarm()?, - Ok(Signal::SIGCHLD) => ctx.check_children()?, - Ok(Signal::SIGTERM) => ctx.terminate()?, - _ => (), - }, - Ok(None) => break Ok(PollRunResult::Uneventful), - Err(err) => break Err(err.into()), - } - } - } -} diff --git a/greetd/src/session.rs b/greetd/src/session.rs index 721a8b8..e7ebb84 100644 --- a/greetd/src/session.rs +++ b/greetd/src/session.rs @@ -11,7 +11,7 @@ use std::time::{Duration, Instant}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use nix::fcntl::{fcntl, FcntlArg}; -use nix::sys::signal::Signal; +use nix::sys::signal::{SigSet, Signal}; use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus}; use nix::unistd::{ close, execve, fork, initgroups, pipe, setgid, setsid, setuid, ForkResult, Gid, Pid, Uid, @@ -22,9 +22,19 @@ use users::User; use crate::pam::converse::PasswordConv; use crate::pam::session::PamSession; -use crate::pollable::signals::blocked_sigset; use crate::terminal; +/// Returns a set containing the signals we want to block in the main process. +/// This is also used to unblock the same signals again before starting child +/// processes. +pub fn blocked_sigset() -> SigSet { + let mut mask = SigSet::empty(); + mask.add(Signal::SIGALRM); + mask.add(Signal::SIGTERM); + mask.add(Signal::SIGCHLD); + mask +} + fn dup_fd_cloexec(fd: RawFd) -> Result> { match fcntl(fd, FcntlArg::F_DUPFD_CLOEXEC(0)) { Ok(fd) => Ok(fd),