From 147c3df7c15a222133c0c1ea538397ac6f11bd1b Mon Sep 17 00:00:00 2001 From: RandyLambert Date: Thu, 7 Jul 2022 14:50:15 +0800 Subject: [PATCH 01/10] [Feat] Change to unix domain socket * Remove jsonrpc and jsonrpc_test Signed-off-by: shouxunsun <1randylambert@gmail.com> * Update Cargo.toml Signed-off-by: shouxunsun <1randylambert@gmail.com> * rpc Use unix domain socket Signed-off-by: shouxunsun <1randylambert@gmail.com> * upgrade tokio Signed-off-by: shouxunsun <1randylambert@gmail.com> Co-authored-by: Ti Chi Robot Co-authored-by: Yang Keao Signed-off-by: RandyLambert --- Cargo.lock | 796 +++++++++++++++++-------------- Cargo.toml | 13 +- src/cmd/interactive/handler.rs | 149 ++++++ src/cmd/interactive/mod.rs | 1 + src/cmd/mod.rs | 1 + src/hookfs/runtime.rs | 3 +- src/injector/latency_injector.rs | 4 +- src/lib.rs | 2 +- src/main.rs | 21 +- src/{jsonrpc.rs => todarpc.rs} | 53 +- tests/jsonrpc_test.rs | 57 --- 11 files changed, 618 insertions(+), 482 deletions(-) create mode 100644 src/cmd/interactive/handler.rs create mode 100644 src/cmd/interactive/mod.rs create mode 100644 src/cmd/mod.rs rename src/{jsonrpc.rs => todarpc.rs} (56%) delete mode 100644 tests/jsonrpc_test.rs diff --git a/Cargo.lock b/Cargo.lock index fe39508..490ee89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,7 +23,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" dependencies = [ - "winapi 0.3.9", + "winapi", ] [[package]] @@ -32,7 +32,7 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" dependencies = [ - "winapi 0.3.9", + "winapi", ] [[package]] @@ -60,14 +60,20 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", "libc", - "winapi 0.3.9", + "winapi", ] [[package]] name = "autocfg" -version = "1.0.1" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "base64" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" [[package]] name = "bitflags" @@ -76,16 +82,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] -name = "byteorder" -version = "1.4.2" +name = "bumpalo" +version = "3.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" +checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3" [[package]] -name = "bytes" -version = "0.5.6" +name = "byteorder" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" +checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" [[package]] name = "bytes" @@ -121,7 +127,7 @@ dependencies = [ "num-integer", "num-traits", "time", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -139,6 +145,22 @@ dependencies = [ "vec_map", ] +[[package]] +name = "core-foundation" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" + [[package]] name = "crc32fast" version = "1.2.1" @@ -210,22 +232,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "fuchsia-zircon" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" -dependencies = [ - "bitflags", - "fuchsia-zircon-sys", -] - -[[package]] -name = "fuchsia-zircon-sys" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" - [[package]] name = "fuser" version = "0.6.0" @@ -238,12 +244,6 @@ dependencies = [ "users", ] -[[package]] -name = "futures" -version = "0.1.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678" - [[package]] name = "futures" version = "0.3.12" @@ -284,7 +284,6 @@ dependencies = [ "futures-core", "futures-task", "futures-util", - "num_cpus", ] [[package]] @@ -326,7 +325,6 @@ version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "632a8cd0f2a4b3fdea1657f08bde063848c3bd00f9bbf6e256b8be78802e624b" dependencies = [ - "futures 0.1.31", "futures-channel", "futures-core", "futures-io", @@ -334,7 +332,7 @@ dependencies = [ "futures-sink", "futures-task", "memchr", - "pin-project-lite 0.2.4", + "pin-project-lite", "pin-utils", "proc-macro-hack", "proc-macro-nested", @@ -358,6 +356,31 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "h2" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37a82c6d637fc9515a4694bbf1cb2457b79d81ce52b3108bdeea58b07dd34a57" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db0d4cf898abf0081f964436dc980e96670a0f36863e4b83aaacdb65c9d7ccc3" + [[package]] name = "heck" version = "0.3.2" @@ -383,152 +406,132 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" [[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - -[[package]] -name = "humantime-serde" -version = "1.0.1" +name = "http" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac34a56cfd4acddb469cc7fff187ed5ac36f498ba085caf8bbc725e3ff474058" +checksum = "75f43d41e26995c17e71ee126451dd3941010b0514a81a9d11f3b341debc2399" dependencies = [ - "humantime", - "serde", + "bytes", + "fnv", + "itoa 1.0.2", ] [[package]] -name = "idna" -version = "0.1.5" +name = "http-body" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" +checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" dependencies = [ - "matches", - "unicode-bidi", - "unicode-normalization", + "bytes", + "http", + "pin-project-lite", ] [[package]] -name = "instant" -version = "0.1.9" +name = "httparse" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" -dependencies = [ - "cfg-if 1.0.0", -] +checksum = "496ce29bb5a52785b44e0f7ca2847ae0bb839c9bd28f69acac9b99d461c0c04c" [[package]] -name = "iovec" -version = "0.1.4" +name = "httpdate" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" -dependencies = [ - "libc", -] +checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" [[package]] -name = "itertools" -version = "0.9.0" +name = "humantime" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" -dependencies = [ - "either", -] +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] -name = "itoa" -version = "0.4.7" +name = "humantime-serde" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +checksum = "ac34a56cfd4acddb469cc7fff187ed5ac36f498ba085caf8bbc725e3ff474058" +dependencies = [ + "humantime", + "serde", +] [[package]] -name = "jsonrpc-client-transports" -version = "17.0.0" +name = "hyper" +version = "0.14.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15b6c6ad01c7354d60de493148c30ac8a82b759e22ae678c8705e9b8e0c566a4" +checksum = "42dc3c131584288d375f2d07f822b0cb012d8c6fb899a5b9fdb3cb7eb9b6004f" dependencies = [ - "derive_more", - "futures 0.3.12", - "jsonrpc-core", - "jsonrpc-pubsub", - "log", - "serde", - "serde_json", - "url", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa 1.0.2", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", ] [[package]] -name = "jsonrpc-core" -version = "17.0.0" +name = "hyper-rustls" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07569945133257ff557eb37b015497104cea61a2c9edaf126c1cbd6e8332397f" +checksum = "d87c48c02e0dc5e3b849a2041db3029fd066650f8f717c07bf8ed78ccb895cac" dependencies = [ - "futures 0.3.12", + "http", + "hyper", "log", - "serde", - "serde_derive", - "serde_json", + "rustls", + "rustls-native-certs", + "tokio", + "tokio-rustls", ] [[package]] -name = "jsonrpc-core-client" -version = "17.0.0" +name = "indexmap" +version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ac9d56dc729912796637c30f475bbf834594607b27740dfea6e5fa7ba40d1f1" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" dependencies = [ - "futures 0.3.12", - "jsonrpc-client-transports", + "autocfg", + "hashbrown", ] [[package]] -name = "jsonrpc-derive" -version = "17.0.0" +name = "itertools" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b68ba7e76e5c7796cfa4d2a30e83986550c34404c6d40551c902ca6f7bd4a137" +checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" dependencies = [ - "proc-macro-crate", - "proc-macro2", - "quote", - "syn", + "either", ] [[package]] -name = "jsonrpc-pubsub" -version = "17.0.0" +name = "itoa" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c48dbebce7a9c88ab272a4db7d6478aa4c6d9596e6c086366e89efc4e9ed89e" -dependencies = [ - "futures 0.3.12", - "jsonrpc-core", - "lazy_static", - "log", - "parking_lot", - "rand", - "serde", -] +checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] -name = "jsonrpc-stdio-server" -version = "17.0.0" +name = "itoa" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "369ec805537bd431d6d9a2df7297c73e214da68efc90c1b8c325d63e55aec5a7" -dependencies = [ - "futures 0.3.12", - "jsonrpc-core", - "log", - "tokio 0.2.24", - "tokio-util 0.3.1", -] +checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" [[package]] -name = "kernel32-sys" -version = "0.2.2" +name = "js-sys" +version = "0.3.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +checksum = "c3fac17f7123a73ca62df411b1bf727ccc805daa070338fda671c86dac1bdc27" dependencies = [ - "winapi 0.2.8", - "winapi-build", + "wasm-bindgen", ] [[package]] @@ -539,9 +542,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.82" +version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" [[package]] name = "libflate" @@ -563,10 +566,11 @@ checksum = "3286f09f7d4926fc486334f28d8d2e6ebe4f7f9994494b6dab27ddfad2c9b11b" [[package]] name = "lock_api" -version = "0.4.2" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd96ffd135b2fd7b973ac026d28085defbe8983df057ced3eb4f2130b0831312" +checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53" dependencies = [ + "autocfg", "scopeguard", ] @@ -588,12 +592,6 @@ dependencies = [ "regex-automata", ] -[[package]] -name = "matches" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" - [[package]] name = "memchr" version = "2.3.4" @@ -611,77 +609,14 @@ dependencies = [ [[package]] name = "mio" -version = "0.6.23" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4afd66f5b91bf2a3bc13fad0e21caedac168ca4c707504e75585648ae80e4cc4" +checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" dependencies = [ - "cfg-if 0.1.10", - "fuchsia-zircon", - "fuchsia-zircon-sys", - "iovec", - "kernel32-sys", "libc", "log", - "miow 0.2.2", - "net2", - "slab", - "winapi 0.2.8", -] - -[[package]] -name = "mio-named-pipes" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656" -dependencies = [ - "log", - "mio", - "miow 0.3.6", - "winapi 0.3.9", -] - -[[package]] -name = "mio-uds" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0" -dependencies = [ - "iovec", - "libc", - "mio", -] - -[[package]] -name = "miow" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebd808424166322d4a38da87083bfddd3ac4c131334ed55856112eb06d46944d" -dependencies = [ - "kernel32-sys", - "net2", - "winapi 0.2.8", - "ws2_32-sys", -] - -[[package]] -name = "miow" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897" -dependencies = [ - "socket2", - "winapi 0.3.9", -] - -[[package]] -name = "net2" -version = "0.2.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "391630d12b68002ae1e25e8f974306474966550ad82dac6886fb8910c19568ae" -dependencies = [ - "cfg-if 0.1.10", - "libc", - "winapi 0.3.9", + "wasi 0.11.0+wasi-snapshot-preview1", + "windows-sys", ] [[package]] @@ -731,37 +666,35 @@ version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + [[package]] name = "parking_lot" -version = "0.11.1" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" dependencies = [ - "instant", "lock_api", "parking_lot_core", ] [[package]] name = "parking_lot_core" -version = "0.8.3" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" +checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" dependencies = [ "cfg-if 1.0.0", - "instant", "libc", "redox_syscall", "smallvec", - "winapi 0.3.9", + "windows-sys", ] -[[package]] -name = "percent-encoding" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" - [[package]] name = "pin-project" version = "0.4.27" @@ -782,12 +715,6 @@ dependencies = [ "syn", ] -[[package]] -name = "pin-project-lite" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b" - [[package]] name = "pin-project-lite" version = "0.2.4" @@ -812,15 +739,6 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" -[[package]] -name = "proc-macro-crate" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" -dependencies = [ - "toml", -] - [[package]] name = "proc-macro-error" version = "1.0.4" @@ -933,9 +851,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.2.5" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94341e4e44e24f6b591b59e47a8a027df12e008d73fd5672dbea9cc22f4507d9" +checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42" dependencies = [ "bitflags", ] @@ -977,24 +895,115 @@ dependencies = [ "rand", ] +[[package]] +name = "ring" +version = "0.16.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin", + "untrusted", + "web-sys", + "winapi", +] + [[package]] name = "rle-decode-fast" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cabe4fa914dec5870285fa7f71f602645da47c486e68486d2b4ceb4a343e90ac" +[[package]] +name = "rustls" +version = "0.20.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aab8ee6c7097ed6057f43c187a62418d0c05a4bd5f18b3571db50ee0f9ce033" +dependencies = [ + "log", + "ring", + "sct", + "webpki", +] + +[[package]] +name = "rustls-native-certs" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0167bac7a9f490495f3c33013e7722b53cb087ecbe082fb0c6387c96f634ea50" +dependencies = [ + "openssl-probe", + "rustls-pemfile", + "schannel", + "security-framework", +] + +[[package]] +name = "rustls-pemfile" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7522c9de787ff061458fe9a829dc790a3f5b22dc571694fc5883f448b94d9a9" +dependencies = [ + "base64", +] + [[package]] name = "ryu" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +[[package]] +name = "schannel" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2" +dependencies = [ + "lazy_static", + "windows-sys", +] + [[package]] name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "sct" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "security-framework" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23a2ac85147a3a11d77ecf1bc7166ec0b92febfa4461c37944e180f319ece467" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "serde" version = "1.0.120" @@ -1021,7 +1030,7 @@ version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fceb2595057b6891a4ee808f70054bd2d12f0e97f1cbb78689b59f676df325a" dependencies = [ - "itoa", + "itoa 0.4.7", "ryu", "serde", ] @@ -1058,15 +1067,20 @@ checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" [[package]] name = "socket2" -version = "0.3.19" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" +checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0" dependencies = [ - "cfg-if 1.0.0", "libc", - "winapi 0.3.9", + "winapi", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "strsim" version = "0.8.0" @@ -1099,9 +1113,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.58" +version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" +checksum = "6498a9efc342871f91cc2d0d694c674368b4ceb40f62b65a7a08c3792935e702" dependencies = [ "proc-macro2", "quote", @@ -1163,24 +1177,9 @@ checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ "libc", "wasi 0.10.0+wasi-snapshot-preview1", - "winapi 0.3.9", + "winapi", ] -[[package]] -name = "tinyvec" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "317cca572a0e89c3ce0ca1f1bdc9369547fe318a683418e42ac8f59d14701023" -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 = "toda" version = "0.2.3" @@ -1192,14 +1191,13 @@ dependencies = [ "dynasmrt", "env_logger", "fuser", - "futures 0.3.12", + "futures", "glob", + "http", "humantime-serde", + "hyper", + "hyper-rustls", "itertools", - "jsonrpc-core", - "jsonrpc-core-client", - "jsonrpc-derive", - "jsonrpc-stdio-server", "libc", "nix", "once_cell", @@ -1212,8 +1210,8 @@ dependencies = [ "structopt", "thiserror", "time", - "tokio 0.2.24", - "tokio-util 0.6.9", + "tokio", + "tokio-util", "tracing", "tracing-futures", "tracing-subscriber", @@ -1221,42 +1219,29 @@ dependencies = [ [[package]] name = "tokio" -version = "0.2.24" +version = "1.19.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "099837d3464c16a808060bb3f02263b412f6fafcb5d01c533d309985fbeebe48" +checksum = "c51a52ed6686dd62c320f9b89299e9dfb46f730c7a48e635c19f21d116cb1439" dependencies = [ - "bytes 0.5.6", - "fnv", - "futures-core", - "iovec", - "lazy_static", + "bytes", "libc", "memchr", "mio", - "mio-named-pipes", - "mio-uds", "num_cpus", - "pin-project-lite 0.1.11", + "once_cell", + "parking_lot", + "pin-project-lite", "signal-hook-registry", - "slab", + "socket2", "tokio-macros", - "winapi 0.3.9", -] - -[[package]] -name = "tokio" -version = "1.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbbf1c778ec206785635ce8ad57fe52b3009ae9e0c9f574a728f3049d3e55838" -dependencies = [ - "pin-project-lite 0.2.4", + "winapi", ] [[package]] name = "tokio-macros" -version = "0.2.6" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e44da00bfc73a25f814cd8d7e57a68a5c31b74b3152a0a1d1f590c97ed06265a" +checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" dependencies = [ "proc-macro2", "quote", @@ -1264,59 +1249,53 @@ dependencies = [ ] [[package]] -name = "tokio-util" -version = "0.3.1" +name = "tokio-rustls" +version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" +checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59" dependencies = [ - "bytes 0.5.6", - "futures-core", - "futures-sink", - "log", - "pin-project-lite 0.1.11", - "tokio 0.2.24", + "rustls", + "tokio", + "webpki", ] [[package]] name = "tokio-util" -version = "0.6.9" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e99e1983e5d376cd8eb4b66604d2e99e79f5bd988c3055891dcd8c9e2604cc0" +checksum = "cc463cd8deddc3770d20f9852143d50bf6094e640b485cb2e189a2099085ff45" dependencies = [ - "bytes 1.1.0", + "bytes", "futures-core", "futures-sink", - "log", - "pin-project-lite 0.2.4", - "tokio 1.15.0", + "pin-project-lite", + "tokio", + "tracing", ] [[package]] -name = "toml" -version = "0.5.8" +name = "tower-service" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" -dependencies = [ - "serde", -] +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" [[package]] name = "tracing" -version = "0.1.22" +version = "0.1.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3" +checksum = "5d0ecdcb44a79f0fe9844f0c4f33a342cbcbb5117de8001e6ba0dc2351327d09" dependencies = [ "cfg-if 1.0.0", - "pin-project-lite 0.2.4", + "pin-project-lite", "tracing-attributes", "tracing-core", ] [[package]] name = "tracing-attributes" -version = "0.1.11" +version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada" +checksum = "cc6b8ad3567499f98a1db7a752b07a7c8c7c7c34c332ec00effb2b0027974b7c" dependencies = [ "proc-macro2", "quote", @@ -1325,11 +1304,12 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.17" +version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" +checksum = "f54c8ca710e81886d498c2fd3331b56c93aa248d49de2222ad2742247c60072f" dependencies = [ "lazy_static", + "valuable", ] [[package]] @@ -1386,22 +1366,10 @@ dependencies = [ ] [[package]] -name = "unicode-bidi" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" -dependencies = [ - "matches", -] - -[[package]] -name = "unicode-normalization" -version = "0.1.17" +name = "try-lock" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07fbfce1c8a97d547e8b5334978438d9d6ec8c20e38f56d4a4374d181493eaef" -dependencies = [ - "tinyvec", -] +checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" [[package]] name = "unicode-segmentation" @@ -1422,15 +1390,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" [[package]] -name = "url" -version = "1.7.2" +name = "untrusted" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" -dependencies = [ - "idna", - "matches", - "percent-encoding", -] +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" [[package]] name = "users" @@ -1442,6 +1405,12 @@ dependencies = [ "log", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vec_map" version = "0.8.2" @@ -1454,6 +1423,16 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" +[[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 = "wasi" version = "0.9.0+wasi-snapshot-preview1" @@ -1467,10 +1446,84 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" [[package]] -name = "winapi" -version = "0.2.8" +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +checksum = "7c53b543413a17a202f4be280a7e5c62a1c69345f5de525ee64f8cfdbc954994" +dependencies = [ + "cfg-if 1.0.0", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5491a68ab4500fa6b4d726bd67408630c3dbe9c4fe7bda16d5c82a1fd8c7340a" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c441e177922bc58f1e12c022624b6216378e5febc2f0533e41ba443d505b80aa" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d94ac45fcf608c1f45ef53e748d35660f168490c10b23704c7779ab8f5c3048" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a89911bd99e5f3659ec4acf9c4d93b0a90fe4a2a11f15328472058edc5261be" + +[[package]] +name = "web-sys" +version = "0.3.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fed94beee57daf8dd7d51f2b15dc2bcde92d7a72304cdf662a4371008b71b90" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" +dependencies = [ + "ring", + "untrusted", +] [[package]] name = "winapi" @@ -1482,12 +1535,6 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu", ] -[[package]] -name = "winapi-build" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" - [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" @@ -1500,7 +1547,7 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ - "winapi 0.3.9", + "winapi", ] [[package]] @@ -1510,11 +1557,44 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] -name = "ws2_32-sys" -version = "0.2.1" +name = "windows-sys" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" +checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" dependencies = [ - "winapi 0.2.8", - "winapi-build", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", ] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" + +[[package]] +name = "windows_i686_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" + +[[package]] +name = "windows_i686_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" diff --git a/Cargo.toml b/Cargo.toml index 642e255..f4ef6fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ fuser = {version = "0.6", features = ["abi-7-19"]} time = "0.1" libc = "0.2" async-trait = "0.1" -tokio = {version = "0.2", features = ["rt-core", "rt-threaded", "sync", "fs", "time", "blocking", "macros", "full"]} -tokio-util = "0.6" +tokio = {version = "1.19", features = ["sync", "fs", "time", "macros", "full"]} +tokio-util = "0.7" thiserror = "1.0" futures = "0.3" derive_more = "0.99.9" @@ -35,10 +35,11 @@ retry = "1.2.0" tracing = "0.1" tracing-futures = "0.2" tracing-subscriber = "0.2" -jsonrpc-stdio-server = "17.0.0" -jsonrpc-derive = "17.0.0" -jsonrpc-core = "17.0.0" -jsonrpc-core-client = "17.0.0" +hyper = {version = "0.14", features = ["runtime", "client", "server", "http1", "http2", "stream"]} +# hyper = {git = "https://github.com/Andrewmatilde/hyper.git", features = ["runtime", "client", "server", "http1", "http2", "stream", "error_return"]} +# hyper-rustls = { git = "https://github.com/Andrewmatilde/hyper-rustls.git", features = ["http2"] } +hyper-rustls = { version = "0.23", features = ["http2"] } +http = "0.2.7" [profile.release] debug = true diff --git a/src/cmd/interactive/handler.rs b/src/cmd/interactive/handler.rs new file mode 100644 index 0000000..002fd4b --- /dev/null +++ b/src/cmd/interactive/handler.rs @@ -0,0 +1,149 @@ +use std::future::Future; +use std::pin::Pin; +use std::sync::Arc; +use std::task::{Context, Poll}; + +use anyhow::Error; +use futures::TryStreamExt; +use http::{Method, Request, Response, StatusCode}; +use hyper::server::conn::{Connection, Http}; +use hyper::service::Service; +use hyper::Body; +use tokio::sync::Mutex; +use tokio::task::JoinHandle; +use tracing::instrument; +use tokio::net::{UnixListener}; +#[cfg(unix)] +use std::os::unix::io::{FromRawFd}; +use crate::todarpc::TodaRpc; +use crate::injector::{InjectorConfig}; + +#[derive(Debug)] +pub struct TodaServer { + toda_rpc: Arc>, + task: Option>>, +} + +impl TodaServer { + pub fn new(toda_rpc: TodaRpc) -> Self { + Self { + toda_rpc: Arc::new(Mutex::new(toda_rpc)), + task: None, + } + } + + pub fn serve_interactive(&mut self) { + let mut service = TodaService(self.toda_rpc.clone()); + + self.task = Some(tokio::spawn(async move { + let unix_listener = UnixListener::from_std(unsafe {std::os::unix::net::UnixListener::from_raw_fd(3)}).unwrap(); + + loop { + match (unix_listener).accept().await { + Ok((stream, addr)) => { + + let http = Http::new(); + let conn = http.serve_connection(stream, &mut service); + if let Err(e) = conn.await { + tracing::error!( + "error : http.serve_connection to {:?} failed, error: {:?}", + addr, + e + ); + return Err(anyhow::anyhow!("{}",e)); + } + } + Err(e) => { + tracing::error!("error : accept connection failed"); + return Err(anyhow::anyhow!("{}", e)); + } + } + } + })); + } +} + +pub struct TodaService(Arc>); + +impl TodaService { + + async fn read_config(request: Request) -> anyhow::Result> { + let request_data: Vec = request + .into_body() + .try_fold(vec![], |mut data, seg| { + data.extend(seg); + futures::future::ok(data) + }) + .await?; + let raw_config: Vec = serde_json::from_slice(&request_data)?; + + Ok(raw_config) + } + + #[instrument] + async fn handle(toda_rpc: &mut TodaRpc, request: Request) -> anyhow::Result> { + + let mut response = Response::new(Body::empty()); + if request.method() != Method::PUT { + *response.status_mut() = StatusCode::METHOD_NOT_ALLOWED; + return Ok(response); + } + *response.status_mut() = StatusCode::OK; + + match request.uri().path() { + "/get_status" => { + match toda_rpc.get_status() { + Err(err) => { + *response.body_mut() = err.to_string().into(); + } + Ok(res) => { + *response.body_mut() = res.into(); + } + } + }, + "/update" => { + let config = match Self::read_config(request).await { + Err(e) => { + *response.body_mut() = e.to_string().into(); + *response.status_mut() = StatusCode::BAD_REQUEST; + return Ok(response); + } + Ok(c) => c, + }; + match toda_rpc.update(config) { + Ok(res) => { + *response.body_mut() = res.into(); + } + Err(err) => { + *response.body_mut() = err.to_string().into(); + } + } + }, + _ => { + *response.status_mut() = StatusCode::NOT_FOUND; + }, + }; + + return Ok(response) + + } +} + +impl Service> for TodaService { + type Response = Response; + type Error = anyhow::Error; + #[allow(clippy::type_complexity)] + type Future = + Pin>>>; + + fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + #[inline] + fn call(&mut self, request: Request) -> Self::Future { + let handler = self.0.clone(); + Box::pin(async move { Self::handle(&mut *handler.lock().await, request).await }) + } +} + diff --git a/src/cmd/interactive/mod.rs b/src/cmd/interactive/mod.rs new file mode 100644 index 0000000..ef7d850 --- /dev/null +++ b/src/cmd/interactive/mod.rs @@ -0,0 +1 @@ +pub mod handler; \ No newline at end of file diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs new file mode 100644 index 0000000..b91716e --- /dev/null +++ b/src/cmd/mod.rs @@ -0,0 +1 @@ +pub mod interactive; diff --git a/src/hookfs/runtime.rs b/src/hookfs/runtime.rs index f5530c0..97d64b9 100644 --- a/src/hookfs/runtime.rs +++ b/src/hookfs/runtime.rs @@ -10,8 +10,7 @@ pub static RUNTIME: Lazy>> = Lazy::new(|| { trace!("build tokio runtime"); RwLock::new(Some( - tokio::runtime::Builder::new() - .threaded_scheduler() + tokio::runtime::Builder::new_multi_thread() .thread_name("toda") .enable_all() .build() diff --git a/src/injector/latency_injector.rs b/src/injector/latency_injector.rs index 1ec8321..933ebb3 100644 --- a/src/injector/latency_injector.rs +++ b/src/injector/latency_injector.rs @@ -2,7 +2,7 @@ use std::path::Path; use std::time::Duration; use async_trait::async_trait; -use tokio::time::delay_for; +use tokio::time::sleep; use tokio::select; use tokio_util::sync::CancellationToken; use tracing::{debug, trace}; @@ -28,7 +28,7 @@ impl Injector for LatencyInjector { debug!("inject io delay {:?}", latency); select! { - _ = delay_for(latency) => {} + _ = sleep(latency) => {} _ = token.cancelled() => { debug!("cancelled"); } diff --git a/src/lib.rs b/src/lib.rs index ae8b02a..c87a99f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ pub mod fuse_device; pub mod hookfs; pub mod injector; -pub mod jsonrpc; +pub mod todarpc; pub mod mount; pub mod mount_injector; pub mod ptrace; diff --git a/src/main.rs b/src/main.rs index d860e90..d1fc512 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,8 @@ extern crate derive_more; mod fuse_device; mod hookfs; mod injector; -mod jsonrpc; +mod cmd; +mod todarpc; mod mount; mod mount_injector; mod ptrace; @@ -36,20 +37,20 @@ use std::convert::TryFrom; use std::os::unix::io::RawFd; use std::path::PathBuf; use std::sync::{mpsc, Mutex}; -use std::{io, thread}; +use std::{io}; use anyhow::Result; use injector::InjectorConfig; -use jsonrpc::start_server; use mount_injector::{MountInjectionGuard, MountInjector}; use nix::sys::signal::{signal, SigHandler, Signal}; use nix::unistd::{pipe, read, write}; use replacer::{Replacer, UnionReplacer}; use structopt::StructOpt; -use tokio::runtime::Runtime; use tracing::{info, instrument}; use tracing_subscriber::EnvFilter; use utils::encode_path; +use crate::cmd::interactive::handler::TodaServer; +use crate::todarpc::TodaRpc; #[derive(StructOpt, Debug, Clone)] #[structopt(name = "basic")] @@ -185,15 +186,9 @@ fn main() -> Result<()> { Ok(e) => Some(e.hookfs.clone()), Err(_) => None, }; - thread::spawn(|| { - Runtime::new() - .expect("Failed to create Tokio runtime") - .block_on(start_server(jsonrpc::RpcImpl::new( - Mutex::new(status), - Mutex::new(tx), - hookfs, - ))); - }); + let mut toda_server = TodaServer::new(TodaRpc::new(Mutex::new(status),Mutex::new(tx),hookfs)); + toda_server.serve_interactive(); + } info!("waiting for signal to exit"); wait_for_signal(reader)?; diff --git a/src/jsonrpc.rs b/src/todarpc.rs similarity index 56% rename from src/jsonrpc.rs rename to src/todarpc.rs index 6ce4ee6..7e2d6dd 100644 --- a/src/jsonrpc.rs +++ b/src/todarpc.rs @@ -1,8 +1,5 @@ use std::sync::{mpsc, Arc, Mutex}; -use jsonrpc_derive::rpc; -use jsonrpc_stdio_server::jsonrpc_core::*; -use jsonrpc_stdio_server::ServerBuilder; use tracing::{info, trace}; use crate::hookfs::HookFs; @@ -13,41 +10,14 @@ pub enum Comm { Shutdown = 0, } -pub async fn start_server(config: RpcImpl) { - info!("Starting jsonrpc server"); - let server = new_server(config); - let server = server.build(); - server.await; -} - -pub fn new_server(config: RpcImpl) -> ServerBuilder { - info!("Creating jsonrpc server"); - let io = new_handler(config); - ServerBuilder::new(io) -} - -pub fn new_handler(config: RpcImpl) -> IoHandler { - info!("Creating jsonrpc handler"); - let mut io = IoHandler::new(); - io.extend_with(config.to_delegate()); - io -} - -#[rpc] -pub trait Rpc { - #[rpc(name = "get_status")] - fn get_status(&self, inst: String) -> Result; - #[rpc(name = "update")] - fn update(&self, config: Vec) -> Result; -} - -pub struct RpcImpl { +#[derive(Debug)] +pub struct TodaRpc { status: Mutex>, tx: Mutex>, hookfs: Option>, } -impl RpcImpl { +impl TodaRpc { pub fn new( status: Mutex>, tx: Mutex>, @@ -55,16 +25,8 @@ impl RpcImpl { ) -> Self { Self { status, tx, hookfs } } -} -impl Drop for RpcImpl { - fn drop(&mut self) { - trace!("Dropping jrpc handler"); - } -} - -impl Rpc for RpcImpl { - fn get_status(&self, _inst: String) -> Result { + pub fn get_status(&self) -> anyhow::Result { info!("rpc get_status called"); match &*self.status.lock().unwrap() { Ok(_) => Ok("ok".to_string()), @@ -72,17 +34,20 @@ impl Rpc for RpcImpl { let tx = &self.tx.lock().unwrap(); tx.send(Comm::Shutdown) .expect("Send through channel failed"); + tracing::error!("get_status error: {:?}", e); Ok(e.to_string()) } } } - fn update(&self, config: Vec) -> Result { + pub fn update(&self, config: Vec) -> anyhow::Result { info!("rpc update called"); if let Err(e) = &*self.status.lock().unwrap() { + tracing::error!("update error: {:?}", e); return Ok(e.to_string()); } let injectors = MultiInjector::build(config); if let Err(e) = &injectors { + tracing::error!("update MultiInjector::build error: {:?}", e); return Ok(e.to_string()); } futures::executor::block_on(async { @@ -93,3 +58,5 @@ impl Rpc for RpcImpl { Ok("ok".to_string()) } } + + diff --git a/tests/jsonrpc_test.rs b/tests/jsonrpc_test.rs deleted file mode 100644 index 770483c..0000000 --- a/tests/jsonrpc_test.rs +++ /dev/null @@ -1,57 +0,0 @@ -use std::sync::mpsc::channel; -use std::sync::Mutex; - -use anyhow::anyhow; -use toda::jsonrpc::{self, new_handler, Comm}; -#[test] -fn test_status_good() { - let (tx, _rx) = channel(); - let io = new_handler(jsonrpc::RpcImpl::new( - Mutex::new(Ok(())), - Mutex::new(tx), - None, - )); - let request = r#"{"jsonrpc": "2.0","method":"get_status","params":[""],"id":1}"#; - let response = r#"{"jsonrpc":"2.0","result":"ok","id":1}"#; - assert_eq!(io.handle_request_sync(request), Some(response.to_string())); -} - -#[test] -fn test_status_bad() { - let (tx, rx) = channel(); - let io = new_handler(jsonrpc::RpcImpl::new( - Mutex::new(Err(anyhow!("Not good"))), - Mutex::new(tx), - None, - )); - let request = r#"{"jsonrpc": "2.0","method":"get_status","params":[""],"id":1}"#; - let response = r#"{"jsonrpc":"2.0","result":"Not good","id":1}"#; - assert_eq!(io.handle_request_sync(request), Some(response.to_string())); - assert_eq!(rx.recv().unwrap(), Comm::Shutdown); -} - -#[test] -fn test_should_not_update_config_if_status_is_failed() { - let (tx, _rx) = channel(); - let request = r#"{"jsonrpc": "2.0","method":"update","params":[[]],"id":1}"#; - let response = r#"{"jsonrpc":"2.0","result":"Not good","id":1}"#; - let io = new_handler(jsonrpc::RpcImpl::new( - Mutex::new(Err(anyhow!("Not good"))), - Mutex::new(tx), - None, - )); - assert_eq!(io.handle_request_sync(request), Some(response.to_string())); -} - -#[test] -fn test_should_fail_if_config_is_bad() { - let (tx, _rx) = channel(); - let request = r#"{"jsonrpc": "2.0","method":"update","params":[["blah"]],"id":1}"#; - let response = r#"{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params: invalid type: string \"blah\", expected internally tagged enum."},"id":1}"#; - let io = new_handler(jsonrpc::RpcImpl::new( - Mutex::new(Ok(())), - Mutex::new(tx), - None, - )); - assert_eq!(io.handle_request_sync(request), Some(response.to_string())); -} From 08a9f9dfa24d9608b18dfa4b255b4ac5b322e3a5 Mon Sep 17 00:00:00 2001 From: RandyLambert Date: Fri, 8 Jul 2022 17:56:19 +0800 Subject: [PATCH 02/10] [Fix] Fix there is no reactor running error * Use std::thread::spawn instead of tokio::spawn to solve there is no reactor running error Signed-off-by: shouxunsun <1randylambert@gmail.com> Co-authored-by: Yang Keao Co-authored-by: xixi Signed-off-by: RandyLambert --- Cargo.lock | 265 +++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/cmd/interactive/handler.rs | 50 ++++--- src/todarpc.rs | 2 +- 4 files changed, 295 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 490ee89..e600f47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,6 +41,119 @@ version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1" +[[package]] +name = "async-attributes" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "async-channel" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2114d64672151c0c5eaa5e131ec84a74f06e1e559830dabba01ca30605d66319" +dependencies = [ + "concurrent-queue", + "event-listener", + "futures-core", +] + +[[package]] +name = "async-executor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "871f9bb5e0a22eeb7e8cf16641feb87c9dc67032ccf8ff49e772eb9941d3a965" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "once_cell", + "slab", +] + +[[package]] +name = "async-global-executor" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5262ed948da60dd8956c6c5aca4d4163593dddb7b32d73267c93dab7b2e98940" +dependencies = [ + "async-channel", + "async-executor", + "async-io", + "async-lock", + "blocking", + "futures-lite", + "num_cpus", + "once_cell", + "tokio", +] + +[[package]] +name = "async-io" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e18f61464ae81cde0a23e713ae8fd299580c54d697a35820cfd0625b8b0e07" +dependencies = [ + "concurrent-queue", + "futures-lite", + "libc", + "log", + "once_cell", + "parking", + "polling", + "slab", + "socket2", + "waker-fn", + "winapi", +] + +[[package]] +name = "async-lock" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e97a171d191782fba31bb902b14ad94e24a68145032b7eedf871ab0bc0d077b6" +dependencies = [ + "event-listener", +] + +[[package]] +name = "async-std" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d" +dependencies = [ + "async-attributes", + "async-channel", + "async-global-executor", + "async-io", + "async-lock", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30696a84d817107fc028e049980e09d5e140e8da8f1caeb17e8e950658a3cea9" + [[package]] name = "async-trait" version = "0.1.42" @@ -52,6 +165,12 @@ dependencies = [ "syn", ] +[[package]] +name = "atomic-waker" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" + [[package]] name = "atty" version = "0.2.14" @@ -81,6 +200,20 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blocking" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6ccb65d468978a086b69884437ded69a90faab3bbe6e67f242173ea728acccc" +dependencies = [ + "async-channel", + "async-task", + "atomic-waker", + "fastrand", + "futures-lite", + "once_cell", +] + [[package]] name = "bumpalo" version = "3.10.0" @@ -99,6 +232,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" +[[package]] +name = "cache-padded" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1db59621ec70f09c5e9b597b220c7a2b43611f4710dc03ceb8748637775692c" + [[package]] name = "cc" version = "1.0.66" @@ -145,6 +284,15 @@ dependencies = [ "vec_map", ] +[[package]] +name = "concurrent-queue" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3" +dependencies = [ + "cache-padded", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -170,6 +318,16 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "crossbeam-utils" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d82ee10ce34d7bc12c2122495e7593a9c41347ecdd64185af4ecf72cb1a7f83" +dependencies = [ + "cfg-if 1.0.0", + "once_cell", +] + [[package]] name = "derive_more" version = "0.99.11" @@ -226,6 +384,21 @@ dependencies = [ "termcolor", ] +[[package]] +name = "event-listener" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71" + +[[package]] +name = "fastrand" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3fcf0cee53519c866c09b5de1f6c56ff9d647101f81c1964fa632e148896cdf" +dependencies = [ + "instant", +] + [[package]] name = "fnv" version = "1.0.7" @@ -292,6 +465,21 @@ version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28be053525281ad8259d47e4de5de657b25e7bac113458555bb4b70bc6870500" +[[package]] +name = "futures-lite" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7694489acd39452c77daa48516b894c153f192c3578d5a839b62c58099fcbf48" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "memchr", + "parking", + "pin-project-lite", + "waker-fn", +] + [[package]] name = "futures-macro" version = "0.3.12" @@ -356,6 +544,18 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "gloo-timers" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fb7d06c1c8cc2a29bee7ec961009a0b2caa0793ee4900c2ffb348734ba1c8f9" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "h2" version = "0.3.13" @@ -504,6 +704,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "itertools" version = "0.9.0" @@ -534,6 +743,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -672,6 +890,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "parking" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" + [[package]] name = "parking_lot" version = "0.12.1" @@ -733,6 +957,19 @@ version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +[[package]] +name = "polling" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "685404d509889fade3e86fe3a5803bca2ec09b0c0778d5ada6ec8bf7a8de5259" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "log", + "wepoll-ffi", + "winapi", +] + [[package]] name = "ppv-lite86" version = "0.2.10" @@ -1185,6 +1422,7 @@ name = "toda" version = "0.2.3" dependencies = [ "anyhow", + "async-std", "async-trait", "bitflags", "derive_more", @@ -1423,6 +1661,12 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" +[[package]] +name = "waker-fn" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" + [[package]] name = "want" version = "0.3.0" @@ -1476,6 +1720,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de9a9cec1733468a8c657e57fa2413d2ae2c0129b95e87c5b72b8ace4d13f31f" +dependencies = [ + "cfg-if 1.0.0", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.81" @@ -1525,6 +1781,15 @@ dependencies = [ "untrusted", ] +[[package]] +name = "wepoll-ffi" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d743fdedc5c64377b5fc2bc036b01c7fd642205a0d96356034ae3404d49eb7fb" +dependencies = [ + "cc", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index f4ef6fd..0c8494d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ fuser = {version = "0.6", features = ["abi-7-19"]} time = "0.1" libc = "0.2" async-trait = "0.1" +async-std = {version = "1.12 ", features = ["attributes", "tokio1"]} tokio = {version = "1.19", features = ["sync", "fs", "time", "macros", "full"]} tokio-util = "0.7" thiserror = "1.0" @@ -36,8 +37,6 @@ tracing = "0.1" tracing-futures = "0.2" tracing-subscriber = "0.2" hyper = {version = "0.14", features = ["runtime", "client", "server", "http1", "http2", "stream"]} -# hyper = {git = "https://github.com/Andrewmatilde/hyper.git", features = ["runtime", "client", "server", "http1", "http2", "stream", "error_return"]} -# hyper-rustls = { git = "https://github.com/Andrewmatilde/hyper-rustls.git", features = ["http2"] } hyper-rustls = { version = "0.23", features = ["http2"] } http = "0.2.7" diff --git a/src/cmd/interactive/handler.rs b/src/cmd/interactive/handler.rs index 002fd4b..d842cd8 100644 --- a/src/cmd/interactive/handler.rs +++ b/src/cmd/interactive/handler.rs @@ -6,11 +6,12 @@ use std::task::{Context, Poll}; use anyhow::Error; use futures::TryStreamExt; use http::{Method, Request, Response, StatusCode}; -use hyper::server::conn::{Connection, Http}; +use hyper::server::conn::{Http}; use hyper::service::Service; use hyper::Body; use tokio::sync::Mutex; -use tokio::task::JoinHandle; +use std::thread::JoinHandle; +use tokio::runtime::Runtime; use tracing::instrument; use tokio::net::{UnixListener}; #[cfg(unix)] @@ -35,30 +36,35 @@ impl TodaServer { pub fn serve_interactive(&mut self) { let mut service = TodaService(self.toda_rpc.clone()); - self.task = Some(tokio::spawn(async move { - let unix_listener = UnixListener::from_std(unsafe {std::os::unix::net::UnixListener::from_raw_fd(3)}).unwrap(); - - loop { - match (unix_listener).accept().await { - Ok((stream, addr)) => { - - let http = Http::new(); - let conn = http.serve_connection(stream, &mut service); - if let Err(e) = conn.await { - tracing::error!( - "error : http.serve_connection to {:?} failed, error: {:?}", - addr, - e - ); - return Err(anyhow::anyhow!("{}",e)); + self.task = Some(std::thread::spawn( move || { + Runtime::new() + .expect("Failed to create Tokio runtime") + .block_on(async { + let unix_listener = UnixListener::from_std(unsafe {std::os::unix::net::UnixListener::from_raw_fd(3)}).unwrap(); + + loop { + match (unix_listener).accept().await { + Ok((stream, addr)) => { + + let http = Http::new(); + let conn = http.serve_connection(stream, &mut service); + if let Err(e) = conn.await { + tracing::error!( + "error : http.serve_connection to {:?} failed, error: {:?}", + addr, + e + ); + return Err(anyhow::anyhow!("{}",e)); + } + } + Err(e) => { + tracing::error!("error : accept connection failed"); + return Err(anyhow::anyhow!("{}", e)); } - } - Err(e) => { - tracing::error!("error : accept connection failed"); - return Err(anyhow::anyhow!("{}", e)); } } } + ) })); } } diff --git a/src/todarpc.rs b/src/todarpc.rs index 7e2d6dd..b8f87fb 100644 --- a/src/todarpc.rs +++ b/src/todarpc.rs @@ -1,6 +1,6 @@ use std::sync::{mpsc, Arc, Mutex}; -use tracing::{info, trace}; +use tracing::{info}; use crate::hookfs::HookFs; use crate::injector::{InjectorConfig, MultiInjector}; From f48a8b165ff21cca61f5807d525eeb70ea66aaa7 Mon Sep 17 00:00:00 2001 From: RandyLambert Date: Mon, 1 Aug 2022 02:17:10 +0800 Subject: [PATCH 03/10] [Feat] Use unix domain socket with path * use unix domain socket with path Signed-off-by: RandyLambert --- src/cmd/interactive/handler.rs | 6 +++--- src/main.rs | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cmd/interactive/handler.rs b/src/cmd/interactive/handler.rs index d842cd8..706695e 100644 --- a/src/cmd/interactive/handler.rs +++ b/src/cmd/interactive/handler.rs @@ -3,6 +3,7 @@ use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; +use std::path::PathBuf; use anyhow::Error; use futures::TryStreamExt; use http::{Method, Request, Response, StatusCode}; @@ -15,7 +16,6 @@ use tokio::runtime::Runtime; use tracing::instrument; use tokio::net::{UnixListener}; #[cfg(unix)] -use std::os::unix::io::{FromRawFd}; use crate::todarpc::TodaRpc; use crate::injector::{InjectorConfig}; @@ -33,14 +33,14 @@ impl TodaServer { } } - pub fn serve_interactive(&mut self) { + pub fn serve_interactive(&mut self, unix_socket_path: PathBuf) { let mut service = TodaService(self.toda_rpc.clone()); self.task = Some(std::thread::spawn( move || { Runtime::new() .expect("Failed to create Tokio runtime") .block_on(async { - let unix_listener = UnixListener::from_std(unsafe {std::os::unix::net::UnixListener::from_raw_fd(3)}).unwrap(); + let unix_listener = UnixListener::bind(unix_socket_path).unwrap(); loop { match (unix_listener).accept().await { diff --git a/src/main.rs b/src/main.rs index d1fc512..29295f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,6 +63,9 @@ struct Options { #[structopt(short = "v", long = "verbose", default_value = "trace")] verbose: String, + + #[structopt(short = "u", long = "unix-socket-path", default_value = "/toda.sock")] + unix_socket_path: PathBuf, } #[instrument(skip(option))] @@ -187,7 +190,7 @@ fn main() -> Result<()> { Err(_) => None, }; let mut toda_server = TodaServer::new(TodaRpc::new(Mutex::new(status),Mutex::new(tx),hookfs)); - toda_server.serve_interactive(); + toda_server.serve_interactive(option.unix_socket_path.clone()); } info!("waiting for signal to exit"); From f810eecfd2864b2876c4c03c63a5ac130c28223a Mon Sep 17 00:00:00 2001 From: RandyLambert Date: Sun, 7 Aug 2022 22:05:53 +0800 Subject: [PATCH 04/10] [Feat] Use unix domain socket with path * use unix domain socket with path Signed-off-by: RandyLambert Co-authored-by: Ti Chi Robot --- src/cmd/interactive/handler.rs | 5 +++-- src/main.rs | 25 +++++++++++++------------ src/signal.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 src/signal.rs diff --git a/src/cmd/interactive/handler.rs b/src/cmd/interactive/handler.rs index 706695e..49bccd3 100644 --- a/src/cmd/interactive/handler.rs +++ b/src/cmd/interactive/handler.rs @@ -33,14 +33,15 @@ impl TodaServer { } } - pub fn serve_interactive(&mut self, unix_socket_path: PathBuf) { + pub fn serve_interactive(&mut self, interactive_path: PathBuf) { let mut service = TodaService(self.toda_rpc.clone()); self.task = Some(std::thread::spawn( move || { Runtime::new() .expect("Failed to create Tokio runtime") .block_on(async { - let unix_listener = UnixListener::bind(unix_socket_path).unwrap(); + tracing::info!("TodaServer listener try binding {:?}", interactive_path); + let unix_listener = UnixListener::bind(interactive_path).unwrap(); loop { match (unix_listener).accept().await { diff --git a/src/main.rs b/src/main.rs index 29295f2..1cda844 100644 --- a/src/main.rs +++ b/src/main.rs @@ -145,17 +145,17 @@ static mut SIGNAL_PIPE_WRITER: RawFd = 0; const SIGNAL_MSG: [u8; 6] = *b"SIGNAL"; -extern "C" fn signal_handler(_: libc::c_int) { - unsafe { - write(SIGNAL_PIPE_WRITER, &SIGNAL_MSG).unwrap(); - } -} - -fn wait_for_signal(chan: RawFd) -> Result<()> { - let mut buf = vec![0u8; 6]; - read(chan, buf.as_mut_slice())?; - Ok(()) -} +// extern "C" fn signal_handler(_: libc::c_int) { +// unsafe { +// write(SIGNAL_PIPE_WRITER, &SIGNAL_MSG).unwrap(); +// } +// } + +// fn wait_for_signal(chan: RawFd) -> Result<()> { +// let mut buf = vec![0u8; 6]; +// read(chan, buf.as_mut_slice())?; +// Ok(()) +// } fn main() -> Result<()> { let (reader, writer) = pipe()?; @@ -194,7 +194,8 @@ fn main() -> Result<()> { } info!("waiting for signal to exit"); - wait_for_signal(reader)?; + let mut signals = Signals::from_kinds(&[SignalKind::interrupt(), SignalKind::terminate()], opt.interactive_path.clone().unwrap())?; + signals.wait().await?; info!("start to recover and exit"); if let Ok(v) = mount_injector { resume(option, v)?; diff --git a/src/signal.rs b/src/signal.rs new file mode 100644 index 0000000..6ae9d0e --- /dev/null +++ b/src/signal.rs @@ -0,0 +1,30 @@ +use futures::future::select_all; +use tokio::signal::unix::{signal, Signal, SignalKind}; +use std::path::PathBuf; + +pub struct Signals +{ + pub signals :Vec, + interactive_path: PathBuf, +} + +impl Signals { + pub fn from_kinds<'a>( + kinds: impl 'a + IntoIterator, + interactive_path: PathBuf, + ) -> anyhow::Result { + let signals = kinds + .into_iter() + .map(|kind| signal(*kind)) + .collect::, _>>()?; + Ok(Signals{signals, interactive_path}) + } + + pub async fn wait(&mut self) -> anyhow::Result<()> { + select_all(self.signals.iter_mut().map(|sig| Box::pin(sig.recv()))).await; + if self.interactive_path != PathBuf::new() { + std::fs::remove_file(self.interactive_path.clone()).unwrap(); + } + Ok(()) + } +} From 2dd8d3cf4d2710e62209ae850061fbdd74f62bbf Mon Sep 17 00:00:00 2001 From: RandyLambert Date: Mon, 15 Aug 2022 01:04:19 +0800 Subject: [PATCH 05/10] [Feat] Use unix domain socket with path * use unix domain socket with path Signed-off-by: RandyLambert Co-authored-by: Ti Chi Robot --- src/lib.rs | 1 + src/main.rs | 37 ++++++++++++++++++++----------------- src/signal.rs | 2 +- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c87a99f..4b4f45e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,3 +29,4 @@ pub mod ptrace; pub mod replacer; pub mod stop; pub mod utils; +pub mod signal; diff --git a/src/main.rs b/src/main.rs index 1cda844..1f8dc67 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ mod stop; mod utils; use std::convert::TryFrom; -use std::os::unix::io::RawFd; +// use std::os::unix::io::RawFd; use std::path::PathBuf; use std::sync::{mpsc, Mutex}; use std::{io}; @@ -42,8 +42,9 @@ use std::{io}; use anyhow::Result; use injector::InjectorConfig; use mount_injector::{MountInjectionGuard, MountInjector}; -use nix::sys::signal::{signal, SigHandler, Signal}; -use nix::unistd::{pipe, read, write}; +use tokio::signal::unix::SignalKind; +// use nix::sys::signal::{signal, SigHandler, Signal}; +// use nix::unistd::{pipe, read, write}; use replacer::{Replacer, UnionReplacer}; use structopt::StructOpt; use tracing::{info, instrument}; @@ -51,6 +52,7 @@ use tracing_subscriber::EnvFilter; use utils::encode_path; use crate::cmd::interactive::handler::TodaServer; use crate::todarpc::TodaRpc; +use toda::signal::Signals; #[derive(StructOpt, Debug, Clone)] #[structopt(name = "basic")] @@ -64,8 +66,8 @@ struct Options { #[structopt(short = "v", long = "verbose", default_value = "trace")] verbose: String, - #[structopt(short = "u", long = "unix-socket-path", default_value = "/toda.sock")] - unix_socket_path: PathBuf, + #[structopt(long = "interactive-path")] + interactive_path: Option, } #[instrument(skip(option))] @@ -141,9 +143,9 @@ fn resume(option: Options, mount_guard: MountInjectionGuard) -> Result<()> { Ok(()) } -static mut SIGNAL_PIPE_WRITER: RawFd = 0; +// static mut SIGNAL_PIPE_WRITER: RawFd = 0; -const SIGNAL_MSG: [u8; 6] = *b"SIGNAL"; +// const SIGNAL_MSG: [u8; 6] = *b"SIGNAL"; // extern "C" fn signal_handler(_: libc::c_int) { // unsafe { @@ -156,15 +158,15 @@ const SIGNAL_MSG: [u8; 6] = *b"SIGNAL"; // read(chan, buf.as_mut_slice())?; // Ok(()) // } +#[tokio::main] +async fn main() -> anyhow::Result<()>{ + // let (reader, writer) = pipe()?; + // unsafe { + // SIGNAL_PIPE_WRITER = writer; + // } -fn main() -> Result<()> { - let (reader, writer) = pipe()?; - unsafe { - SIGNAL_PIPE_WRITER = writer; - } - - unsafe { signal(Signal::SIGINT, SigHandler::Handler(signal_handler))? }; - unsafe { signal(Signal::SIGTERM, SigHandler::Handler(signal_handler))? }; + // unsafe { signal(Signal::SIGINT, SigHandler::Handler(signal_handler))? }; + // unsafe { signal(Signal::SIGTERM, SigHandler::Handler(signal_handler))? }; let option = Options::from_args(); let env_filter = EnvFilter::try_from_default_env() @@ -190,12 +192,13 @@ fn main() -> Result<()> { Err(_) => None, }; let mut toda_server = TodaServer::new(TodaRpc::new(Mutex::new(status),Mutex::new(tx),hookfs)); - toda_server.serve_interactive(option.unix_socket_path.clone()); + toda_server.serve_interactive(option.interactive_path.clone().unwrap()); } info!("waiting for signal to exit"); - let mut signals = Signals::from_kinds(&[SignalKind::interrupt(), SignalKind::terminate()], opt.interactive_path.clone().unwrap())?; + let mut signals = Signals::from_kinds(&[SignalKind::interrupt(), SignalKind::terminate()], option.interactive_path.clone().unwrap())?; signals.wait().await?; + info!("start to recover and exit"); if let Ok(v) = mount_injector { resume(option, v)?; diff --git a/src/signal.rs b/src/signal.rs index 6ae9d0e..29872f4 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -23,7 +23,7 @@ impl Signals { pub async fn wait(&mut self) -> anyhow::Result<()> { select_all(self.signals.iter_mut().map(|sig| Box::pin(sig.recv()))).await; if self.interactive_path != PathBuf::new() { - std::fs::remove_file(self.interactive_path.clone()).unwrap(); + std::fs::remove_file(self.interactive_path.clone())?; } Ok(()) } From 00c07a2cec3b7fa58f09735e86cd827831bfc22d Mon Sep 17 00:00:00 2001 From: RandyLambert Date: Wed, 31 Aug 2022 02:09:28 +0800 Subject: [PATCH 06/10] [Feat] Use unix domain socket with path * use unix domain socket with path Signed-off-by: RandyLambert --- .gitignore | 3 ++- src/cmd/interactive/handler.rs | 32 ++++++++++++++++---------------- src/main.rs | 25 +++++++++++++++---------- src/signal.rs | 15 +++------------ src/todarpc.rs | 4 +--- 5 files changed, 37 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index 46b76ae..817b61b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /example/toda -/toda \ No newline at end of file +/toda +/.vscode \ No newline at end of file diff --git a/src/cmd/interactive/handler.rs b/src/cmd/interactive/handler.rs index 49bccd3..78aff40 100644 --- a/src/cmd/interactive/handler.rs +++ b/src/cmd/interactive/handler.rs @@ -34,8 +34,8 @@ impl TodaServer { } pub fn serve_interactive(&mut self, interactive_path: PathBuf) { - let mut service = TodaService(self.toda_rpc.clone()); + let toda_rpc = self.toda_rpc.clone(); self.task = Some(std::thread::spawn( move || { Runtime::new() .expect("Failed to create Tokio runtime") @@ -44,25 +44,26 @@ impl TodaServer { let unix_listener = UnixListener::bind(interactive_path).unwrap(); loop { + let mut service = TodaService(toda_rpc.clone()); match (unix_listener).accept().await { Ok((stream, addr)) => { - - let http = Http::new(); - let conn = http.serve_connection(stream, &mut service); - if let Err(e) = conn.await { - tracing::error!( - "error : http.serve_connection to {:?} failed, error: {:?}", - addr, - e - ); - return Err(anyhow::anyhow!("{}",e)); - } + tokio::task::spawn(async move { + let http = Http::new(); + let conn = http.serve_connection(stream, &mut service); + if let Err(e) = conn.await { + tracing::error!( + "error : http.serve_connection to {:?} failed, error: {:?}", + addr, + e + ); + } + }); } Err(e) => { - tracing::error!("error : accept connection failed"); + tracing::error!("error: accept connection failed"); return Err(anyhow::anyhow!("{}", e)); } - } + } } } ) @@ -117,7 +118,7 @@ impl TodaService { } Ok(c) => c, }; - match toda_rpc.update(config) { + match toda_rpc.update(config).await { Ok(res) => { *response.body_mut() = res.into(); } @@ -153,4 +154,3 @@ impl Service> for TodaService { Box::pin(async move { Self::handle(&mut *handler.lock().await, request).await }) } } - diff --git a/src/main.rs b/src/main.rs index 1f8dc67..2c72ca2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,7 @@ use std::convert::TryFrom; use std::path::PathBuf; use std::sync::{mpsc, Mutex}; use std::{io}; +use std::process::exit; use anyhow::Result; use injector::InjectorConfig; @@ -186,22 +187,26 @@ async fn main() -> anyhow::Result<()>{ }; let (tx, _) = mpsc::channel(); - { + if option.interactive_path.is_some() { let hookfs = match &mount_injector { Ok(e) => Some(e.hookfs.clone()), Err(_) => None, }; let mut toda_server = TodaServer::new(TodaRpc::new(Mutex::new(status),Mutex::new(tx),hookfs)); toda_server.serve_interactive(option.interactive_path.clone().unwrap()); - - } - info!("waiting for signal to exit"); - let mut signals = Signals::from_kinds(&[SignalKind::interrupt(), SignalKind::terminate()], option.interactive_path.clone().unwrap())?; - signals.wait().await?; - - info!("start to recover and exit"); - if let Ok(v) = mount_injector { - resume(option, v)?; + + info!("waiting for signal to exit"); + let mut signals = Signals::from_kinds(&[SignalKind::interrupt(), SignalKind::terminate()])?; + signals.wait().await?; + + // delete the unix socket file + std::fs::remove_file(option.interactive_path.clone().unwrap())?; + + info!("start to recover and exit"); + if let Ok(v) = mount_injector { + resume(option, v)?; + } + exit(0); } Ok(()) } diff --git a/src/signal.rs b/src/signal.rs index 29872f4..86d0dc0 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -1,30 +1,21 @@ use futures::future::select_all; use tokio::signal::unix::{signal, Signal, SignalKind}; -use std::path::PathBuf; -pub struct Signals -{ - pub signals :Vec, - interactive_path: PathBuf, -} +pub struct Signals(Vec); impl Signals { pub fn from_kinds<'a>( kinds: impl 'a + IntoIterator, - interactive_path: PathBuf, ) -> anyhow::Result { let signals = kinds .into_iter() .map(|kind| signal(*kind)) .collect::, _>>()?; - Ok(Signals{signals, interactive_path}) + Ok(Signals(signals)) } pub async fn wait(&mut self) -> anyhow::Result<()> { - select_all(self.signals.iter_mut().map(|sig| Box::pin(sig.recv()))).await; - if self.interactive_path != PathBuf::new() { - std::fs::remove_file(self.interactive_path.clone())?; - } + select_all(self.0.iter_mut().map(|sig| Box::pin(sig.recv()))).await; Ok(()) } } diff --git a/src/todarpc.rs b/src/todarpc.rs index b8f87fb..f26e30e 100644 --- a/src/todarpc.rs +++ b/src/todarpc.rs @@ -39,7 +39,7 @@ impl TodaRpc { } } } - pub fn update(&self, config: Vec) -> anyhow::Result { + pub async fn update(&self, config: Vec) -> anyhow::Result { info!("rpc update called"); if let Err(e) = &*self.status.lock().unwrap() { tracing::error!("update error: {:?}", e); @@ -58,5 +58,3 @@ impl TodaRpc { Ok("ok".to_string()) } } - - From 2e3f0233149f76a111638e40505411c9500673dd Mon Sep 17 00:00:00 2001 From: RandyLambert Date: Sun, 11 Sep 2022 21:48:14 +0800 Subject: [PATCH 07/10] [Feat] Use unix domain socket with path * use unix domain socket with path Signed-off-by: RandyLambert --- src/cmd/interactive/handler.rs | 111 +++++++++++++++---------------- src/cmd/interactive/mod.rs | 2 +- src/hookfs/mod.rs | 2 +- src/injector/filter.rs | 15 ++--- src/injector/latency_injector.rs | 2 +- src/lib.rs | 4 +- src/main.rs | 52 ++++----------- src/mount_injector.rs | 7 +- src/signal.rs | 3 +- src/todarpc.rs | 2 +- 10 files changed, 86 insertions(+), 114 deletions(-) diff --git a/src/cmd/interactive/handler.rs b/src/cmd/interactive/handler.rs index 78aff40..dce57b6 100644 --- a/src/cmd/interactive/handler.rs +++ b/src/cmd/interactive/handler.rs @@ -1,112 +1,106 @@ use std::future::Future; +use std::path::PathBuf; use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; +use std::thread::JoinHandle; -use std::path::PathBuf; use anyhow::Error; use futures::TryStreamExt; use http::{Method, Request, Response, StatusCode}; -use hyper::server::conn::{Http}; +use hyper::server::conn::Http; use hyper::service::Service; use hyper::Body; -use tokio::sync::Mutex; -use std::thread::JoinHandle; +use tokio::net::UnixListener; use tokio::runtime::Runtime; use tracing::instrument; -use tokio::net::{UnixListener}; + +use crate::injector::InjectorConfig; #[cfg(unix)] use crate::todarpc::TodaRpc; -use crate::injector::{InjectorConfig}; #[derive(Debug)] pub struct TodaServer { - toda_rpc: Arc>, + toda_rpc: Arc, task: Option>>, } impl TodaServer { pub fn new(toda_rpc: TodaRpc) -> Self { Self { - toda_rpc: Arc::new(Mutex::new(toda_rpc)), + toda_rpc: Arc::new(toda_rpc), task: None, } } pub fn serve_interactive(&mut self, interactive_path: PathBuf) { - let toda_rpc = self.toda_rpc.clone(); - self.task = Some(std::thread::spawn( move || { + self.task = Some(std::thread::spawn(move || { Runtime::new() - .expect("Failed to create Tokio runtime") - .block_on(async { - tracing::info!("TodaServer listener try binding {:?}", interactive_path); - let unix_listener = UnixListener::bind(interactive_path).unwrap(); - - loop { - let mut service = TodaService(toda_rpc.clone()); - match (unix_listener).accept().await { - Ok((stream, addr)) => { - tokio::task::spawn(async move { - let http = Http::new(); - let conn = http.serve_connection(stream, &mut service); - if let Err(e) = conn.await { - tracing::error!( + .expect("Failed to create Tokio runtime") + .block_on(async { + tracing::info!("TodaServer listener try binding {:?}", interactive_path); + let unix_listener = UnixListener::bind(interactive_path).unwrap(); + + loop { + let mut service = TodaService(toda_rpc.clone()); + match (unix_listener).accept().await { + Ok((stream, addr)) => { + tokio::task::spawn(async move { + let http = Http::new(); + let conn = http.serve_connection(stream, &mut service); + if let Err(e) = conn.await { + tracing::error!( "error : http.serve_connection to {:?} failed, error: {:?}", addr, e ); - } - }); + } + }); + } + Err(e) => { + tracing::error!("error: accept connection failed"); + return Err(anyhow::anyhow!("{}", e)); + } } - Err(e) => { - tracing::error!("error: accept connection failed"); - return Err(anyhow::anyhow!("{}", e)); - } - } - } - } - ) + } + }) })); } } -pub struct TodaService(Arc>); +pub struct TodaService(Arc); impl TodaService { - async fn read_config(request: Request) -> anyhow::Result> { let request_data: Vec = request - .into_body() - .try_fold(vec![], |mut data, seg| { - data.extend(seg); - futures::future::ok(data) - }) - .await?; + .into_body() + .try_fold(vec![], |mut data, seg| { + data.extend(seg); + futures::future::ok(data) + }) + .await?; let raw_config: Vec = serde_json::from_slice(&request_data)?; Ok(raw_config) } #[instrument] - async fn handle(toda_rpc: &mut TodaRpc, request: Request) -> anyhow::Result> { - + async fn handle(toda_rpc: &TodaRpc, request: Request) -> anyhow::Result> { let mut response = Response::new(Body::empty()); if request.method() != Method::PUT { *response.status_mut() = StatusCode::METHOD_NOT_ALLOWED; return Ok(response); } *response.status_mut() = StatusCode::OK; - + match request.uri().path() { - "/get_status" => { - match toda_rpc.get_status() { - Err(err) => { - *response.body_mut() = err.to_string().into(); - } - Ok(res) => { - *response.body_mut() = res.into(); - } + "/get_status" => match toda_rpc.get_status() { + Err(err) => { + *response.body_mut() = err.to_string().into(); + } + Ok(res) => { + *response.body_mut() = res.into(); } }, "/update" => { @@ -124,16 +118,15 @@ impl TodaService { } Err(err) => { *response.body_mut() = err.to_string().into(); - } + } } - }, + } _ => { *response.status_mut() = StatusCode::NOT_FOUND; - }, + } }; - return Ok(response) - + Ok(response) } } @@ -151,6 +144,6 @@ impl Service> for TodaService { #[inline] fn call(&mut self, request: Request) -> Self::Future { let handler = self.0.clone(); - Box::pin(async move { Self::handle(&mut *handler.lock().await, request).await }) + Box::pin(async move { Self::handle(&handler, request).await }) } } diff --git a/src/cmd/interactive/mod.rs b/src/cmd/interactive/mod.rs index ef7d850..062ae9d 100644 --- a/src/cmd/interactive/mod.rs +++ b/src/cmd/interactive/mod.rs @@ -1 +1 @@ -pub mod handler; \ No newline at end of file +pub mod handler; diff --git a/src/hookfs/mod.rs b/src/hookfs/mod.rs index d72fbae..73395ef 100644 --- a/src/hookfs/mod.rs +++ b/src/hookfs/mod.rs @@ -324,7 +324,7 @@ impl HookFs { // TODO: create a standalone runtime only for interrupt is too ugly. // this RWLock is actually redundant, and the injector is rarely written. - let mut rt = tokio::runtime::Runtime::new().unwrap(); + let mut rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async { let injector = self.injector.read().await; injector.interrupt(); diff --git a/src/injector/filter.rs b/src/injector/filter.rs index d6bcaf9..b45345d 100644 --- a/src/injector/filter.rs +++ b/src/injector/filter.rs @@ -108,14 +108,13 @@ impl Filter { }) .unwrap_or(Method::all()); - let path_filter = conf - .path.and_then(|path| -> Option { - if !path.is_empty() { - Pattern::new(&path).ok() - } else { - None - } - }); + let path_filter = conf.path.and_then(|path| -> Option { + if !path.is_empty() { + Pattern::new(&path).ok() + } else { + None + } + }); Ok(Self { path_filter, methods, diff --git a/src/injector/latency_injector.rs b/src/injector/latency_injector.rs index 933ebb3..ecad95a 100644 --- a/src/injector/latency_injector.rs +++ b/src/injector/latency_injector.rs @@ -2,8 +2,8 @@ use std::path::Path; use std::time::Duration; use async_trait::async_trait; -use tokio::time::sleep; use tokio::select; +use tokio::time::sleep; use tokio_util::sync::CancellationToken; use tracing::{debug, trace}; diff --git a/src/lib.rs b/src/lib.rs index 4b4f45e..2dca258 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,11 +22,11 @@ pub mod fuse_device; pub mod hookfs; pub mod injector; -pub mod todarpc; pub mod mount; pub mod mount_injector; pub mod ptrace; pub mod replacer; +pub mod signal; pub mod stop; +pub mod todarpc; pub mod utils; -pub mod signal; diff --git a/src/main.rs b/src/main.rs index 2c72ca2..5c6494c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,39 +21,37 @@ extern crate derive_more; +mod cmd; mod fuse_device; mod hookfs; mod injector; -mod cmd; -mod todarpc; mod mount; mod mount_injector; mod ptrace; mod replacer; mod stop; +mod todarpc; mod utils; use std::convert::TryFrom; -// use std::os::unix::io::RawFd; +use std::io; use std::path::PathBuf; -use std::sync::{mpsc, Mutex}; -use std::{io}; use std::process::exit; +use std::sync::{mpsc, Mutex}; use anyhow::Result; use injector::InjectorConfig; use mount_injector::{MountInjectionGuard, MountInjector}; -use tokio::signal::unix::SignalKind; -// use nix::sys::signal::{signal, SigHandler, Signal}; -// use nix::unistd::{pipe, read, write}; use replacer::{Replacer, UnionReplacer}; use structopt::StructOpt; +use toda::signal::Signals; +use tokio::signal::unix::SignalKind; use tracing::{info, instrument}; use tracing_subscriber::EnvFilter; use utils::encode_path; + use crate::cmd::interactive::handler::TodaServer; use crate::todarpc::TodaRpc; -use toda::signal::Signals; #[derive(StructOpt, Debug, Clone)] #[structopt(name = "basic")] @@ -144,31 +142,8 @@ fn resume(option: Options, mount_guard: MountInjectionGuard) -> Result<()> { Ok(()) } -// static mut SIGNAL_PIPE_WRITER: RawFd = 0; - -// const SIGNAL_MSG: [u8; 6] = *b"SIGNAL"; - -// extern "C" fn signal_handler(_: libc::c_int) { -// unsafe { -// write(SIGNAL_PIPE_WRITER, &SIGNAL_MSG).unwrap(); -// } -// } - -// fn wait_for_signal(chan: RawFd) -> Result<()> { -// let mut buf = vec![0u8; 6]; -// read(chan, buf.as_mut_slice())?; -// Ok(()) -// } #[tokio::main] -async fn main() -> anyhow::Result<()>{ - // let (reader, writer) = pipe()?; - // unsafe { - // SIGNAL_PIPE_WRITER = writer; - // } - - // unsafe { signal(Signal::SIGINT, SigHandler::Handler(signal_handler))? }; - // unsafe { signal(Signal::SIGTERM, SigHandler::Handler(signal_handler))? }; - +async fn main() -> anyhow::Result<()> { let option = Options::from_args(); let env_filter = EnvFilter::try_from_default_env() .or_else(|_| EnvFilter::try_from(&option.verbose)) @@ -192,16 +167,17 @@ async fn main() -> anyhow::Result<()>{ Ok(e) => Some(e.hookfs.clone()), Err(_) => None, }; - let mut toda_server = TodaServer::new(TodaRpc::new(Mutex::new(status),Mutex::new(tx),hookfs)); + let mut toda_server = + TodaServer::new(TodaRpc::new(Mutex::new(status), Mutex::new(tx), hookfs)); toda_server.serve_interactive(option.interactive_path.clone().unwrap()); - + info!("waiting for signal to exit"); let mut signals = Signals::from_kinds(&[SignalKind::interrupt(), SignalKind::terminate()])?; - signals.wait().await?; - + signals.wait().await; + // delete the unix socket file std::fs::remove_file(option.interactive_path.clone().unwrap())?; - + info!("start to recover and exit"); if let Ok(v) = mount_injector { resume(option, v)?; diff --git a/src/mount_injector.rs b/src/mount_injector.rs index 2ac35f9..5a152de 100644 --- a/src/mount_injector.rs +++ b/src/mount_injector.rs @@ -130,7 +130,12 @@ impl MountInjector { std::fs::create_dir_all(new_path.as_path())?; - let args = ["allow_other", "fsname=toda", "default_permissions", "nonempty"]; + let args = [ + "allow_other", + "fsname=toda", + "default_permissions", + "nonempty", + ]; let flags: Vec<_> = args .iter() .flat_map(|item| vec![OsStr::new("-o"), OsStr::new(item)]) diff --git a/src/signal.rs b/src/signal.rs index 86d0dc0..f21a72b 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -14,8 +14,7 @@ impl Signals { Ok(Signals(signals)) } - pub async fn wait(&mut self) -> anyhow::Result<()> { + pub async fn wait(&mut self) { select_all(self.0.iter_mut().map(|sig| Box::pin(sig.recv()))).await; - Ok(()) } } diff --git a/src/todarpc.rs b/src/todarpc.rs index f26e30e..117c021 100644 --- a/src/todarpc.rs +++ b/src/todarpc.rs @@ -1,6 +1,6 @@ use std::sync::{mpsc, Arc, Mutex}; -use tracing::{info}; +use tracing::info; use crate::hookfs::HookFs; use crate::injector::{InjectorConfig, MultiInjector}; From ac16f59d33386e05be36fc8a0507fff49419cc6d Mon Sep 17 00:00:00 2001 From: RandyLambert Date: Sun, 11 Sep 2022 23:23:51 +0800 Subject: [PATCH 08/10] [Feat] Use unix domain socket with path * use unix domain socket with path Signed-off-by: RandyLambert --- src/cmd/interactive/handler.rs | 58 +++++++++++++++++----------------- src/todarpc.rs | 4 +-- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/cmd/interactive/handler.rs b/src/cmd/interactive/handler.rs index dce57b6..2a96dae 100644 --- a/src/cmd/interactive/handler.rs +++ b/src/cmd/interactive/handler.rs @@ -3,7 +3,6 @@ use std::path::PathBuf; use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; -use std::thread::JoinHandle; use anyhow::Error; use futures::TryStreamExt; @@ -12,7 +11,8 @@ use hyper::server::conn::Http; use hyper::service::Service; use hyper::Body; use tokio::net::UnixListener; -use tokio::runtime::Runtime; +// use tokio::runtime::Runtime; +use tokio::task::JoinHandle; use tracing::instrument; use crate::injector::InjectorConfig; @@ -35,36 +35,36 @@ impl TodaServer { pub fn serve_interactive(&mut self, interactive_path: PathBuf) { let toda_rpc = self.toda_rpc.clone(); - self.task = Some(std::thread::spawn(move || { - Runtime::new() - .expect("Failed to create Tokio runtime") - .block_on(async { - tracing::info!("TodaServer listener try binding {:?}", interactive_path); - let unix_listener = UnixListener::bind(interactive_path).unwrap(); + self.task = Some(tokio::task::spawn(async move { + // Runtime::new() + // .expect("Failed to create Tokio runtime") + // .block_on(async { + tracing::info!("TodaServer listener try binding {:?}", interactive_path); + let unix_listener = UnixListener::bind(interactive_path).unwrap(); - loop { - let mut service = TodaService(toda_rpc.clone()); - match (unix_listener).accept().await { - Ok((stream, addr)) => { - tokio::task::spawn(async move { - let http = Http::new(); - let conn = http.serve_connection(stream, &mut service); - if let Err(e) = conn.await { - tracing::error!( - "error : http.serve_connection to {:?} failed, error: {:?}", - addr, - e - ); - } - }); + loop { + let mut service = TodaService(toda_rpc.clone()); + match (unix_listener).accept().await { + Ok((stream, addr)) => { + tokio::task::spawn(async move { + let http = Http::new(); + let conn = http.serve_connection(stream, &mut service); + if let Err(e) = conn.await { + tracing::error!( + "error : http.serve_connection to {:?} failed, error: {:?}", + addr, + e + ); } - Err(e) => { - tracing::error!("error: accept connection failed"); - return Err(anyhow::anyhow!("{}", e)); - } - } + }); + } + Err(e) => { + tracing::error!("error: accept connection failed"); + return Err(anyhow::anyhow!("{}", e)); } - }) + } + } + // }) })); } } diff --git a/src/todarpc.rs b/src/todarpc.rs index 117c021..e4f7fec 100644 --- a/src/todarpc.rs +++ b/src/todarpc.rs @@ -50,11 +50,11 @@ impl TodaRpc { tracing::error!("update MultiInjector::build error: {:?}", e); return Ok(e.to_string()); } - futures::executor::block_on(async { + // futures::executor::block_on(async { let hookfs = self.hookfs.as_ref().unwrap(); let mut current_injectors = hookfs.injector.write().await; *current_injectors = injectors.unwrap(); - }); + // }); Ok("ok".to_string()) } } From 1fd65193e7cdd812b19e57723bdb3e16090beba5 Mon Sep 17 00:00:00 2001 From: RandyLambert Date: Sat, 17 Sep 2022 18:17:26 +0800 Subject: [PATCH 09/10] [Feat] Use unix domain socket with path * use unix domain socket with path Signed-off-by: RandyLambert --- src/cmd/interactive/handler.rs | 5 ----- src/hookfs/mod.rs | 5 +---- src/main.rs | 10 +++++----- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/cmd/interactive/handler.rs b/src/cmd/interactive/handler.rs index 2a96dae..7526050 100644 --- a/src/cmd/interactive/handler.rs +++ b/src/cmd/interactive/handler.rs @@ -11,7 +11,6 @@ use hyper::server::conn::Http; use hyper::service::Service; use hyper::Body; use tokio::net::UnixListener; -// use tokio::runtime::Runtime; use tokio::task::JoinHandle; use tracing::instrument; @@ -36,9 +35,6 @@ impl TodaServer { pub fn serve_interactive(&mut self, interactive_path: PathBuf) { let toda_rpc = self.toda_rpc.clone(); self.task = Some(tokio::task::spawn(async move { - // Runtime::new() - // .expect("Failed to create Tokio runtime") - // .block_on(async { tracing::info!("TodaServer listener try binding {:?}", interactive_path); let unix_listener = UnixListener::bind(interactive_path).unwrap(); @@ -64,7 +60,6 @@ impl TodaServer { } } } - // }) })); } } diff --git a/src/hookfs/mod.rs b/src/hookfs/mod.rs index 73395ef..af375a6 100644 --- a/src/hookfs/mod.rs +++ b/src/hookfs/mod.rs @@ -319,16 +319,13 @@ impl HookFs { self.enable_injection.store(true, Ordering::SeqCst); } - pub fn disable_injection(&self) { + pub async fn disable_injection(&self) { self.enable_injection.store(false, Ordering::SeqCst); // TODO: create a standalone runtime only for interrupt is too ugly. // this RWLock is actually redundant, and the injector is rarely written. - let mut rt = tokio::runtime::Runtime::new().unwrap(); - rt.block_on(async { let injector = self.injector.read().await; injector.interrupt(); - }); } pub fn rebuild_path>(&self, path: P) -> Result { diff --git a/src/main.rs b/src/main.rs index 5c6494c..3ee634d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -162,26 +162,26 @@ async fn main() -> anyhow::Result<()> { }; let (tx, _) = mpsc::channel(); - if option.interactive_path.is_some() { + if let Some(path) = option.interactive_path.clone() { let hookfs = match &mount_injector { Ok(e) => Some(e.hookfs.clone()), Err(_) => None, }; let mut toda_server = TodaServer::new(TodaRpc::new(Mutex::new(status), Mutex::new(tx), hookfs)); - toda_server.serve_interactive(option.interactive_path.clone().unwrap()); + toda_server.serve_interactive(path.clone()); info!("waiting for signal to exit"); let mut signals = Signals::from_kinds(&[SignalKind::interrupt(), SignalKind::terminate()])?; signals.wait().await; - // delete the unix socket file - std::fs::remove_file(option.interactive_path.clone().unwrap())?; - info!("start to recover and exit"); if let Ok(v) = mount_injector { resume(option, v)?; } + + // delete the unix socket file + std::fs::remove_file(path.clone())?; exit(0); } Ok(()) From 2596116e510ec175acb4611c125f8b0f3a2829f7 Mon Sep 17 00:00:00 2001 From: "sunshouxun.0406" Date: Mon, 19 Sep 2022 16:47:00 +0800 Subject: [PATCH 10/10] [Feat] Use unix domain socket with path * use unix domain socket with path Signed-off-by: RandyLambert --- src/hookfs/mod.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/hookfs/mod.rs b/src/hookfs/mod.rs index af375a6..b43981c 100644 --- a/src/hookfs/mod.rs +++ b/src/hookfs/mod.rs @@ -321,9 +321,6 @@ impl HookFs { pub async fn disable_injection(&self) { self.enable_injection.store(false, Ordering::SeqCst); - - // TODO: create a standalone runtime only for interrupt is too ugly. - // this RWLock is actually redundant, and the injector is rarely written. let injector = self.injector.read().await; injector.interrupt(); }