From 84a9a1fbdf3efb6652145b389149a07fae687058 Mon Sep 17 00:00:00 2001 From: Leif Walsh Date: Tue, 2 Mar 2021 11:01:08 -0500 Subject: [PATCH] Don't use systemd socket activation becuase startup ordering is weird. Fixes #9. Co-authored-by: Geoffrey Thomas --- Cargo.lock | 82 ---------------------------------------- Cargo.toml | 1 - README.md | 13 ++++--- debian/Cargo.toml.append | 1 - debian/README.source | 3 -- debian/changelog | 7 ++++ debian/control | 2 +- debian/install | 1 - debian/rules | 4 -- nsncd.service | 3 ++ nsncd.socket | 19 ---------- src/main.rs | 24 ++++++------ 12 files changed, 30 insertions(+), 130 deletions(-) delete mode 100644 nsncd.socket diff --git a/Cargo.lock b/Cargo.lock index 4ce292f..2569f98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -79,12 +79,6 @@ dependencies = [ "serde", ] -[[package]] -name = "build-env" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e068f31938f954b695423ecaf756179597627d0828c0d3e48c0a722a8b23cf9e" - [[package]] name = "bumpalo" version = "3.4.0" @@ -231,16 +225,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "cstr-argument" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20bd4e8067c20c7c3a4dea759ef91d4b18418ddb5bd8837ef6e2f2f93ca7ccbb" -dependencies = [ - "cfg-if", - "memchr", -] - [[package]] name = "csv" version = "1.1.3" @@ -290,33 +274,6 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" -[[package]] -name = "foreign-types" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" -dependencies = [ - "foreign-types-macros", - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-macros" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63f713f8b2aa9e24fec85b0e290c56caee12e3b6ae0aeeda238a75b28251afd6" -dependencies = [ - "proc-macro2 1.0.23", - "quote 1.0.7", - "syn 1.0.42", -] - -[[package]] -name = "foreign-types-shared" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7684cf33bb7f28497939e8c7cf17e3e4e3b8d9a0080ffa4f8ae2f515442ee855" - [[package]] name = "getrandom" version = "0.1.15" @@ -379,17 +336,6 @@ version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235" -[[package]] -name = "libsystemd-sys" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33a586fb140c86d24965adfa47dc36388b5c19e79bc47eeba2faa2cd62eb0701" -dependencies = [ - "build-env", - "libc", - "pkg-config", -] - [[package]] name = "log" version = "0.4.11" @@ -445,7 +391,6 @@ dependencies = [ "slog", "slog-async", "slog-term", - "systemd", ] [[package]] @@ -494,12 +439,6 @@ version = "11.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a170cebd8021a008ea92e4db85a72f80b35df514ec664b296fdcbb654eac0b2c" -[[package]] -name = "pkg-config" -version = "0.3.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" - [[package]] name = "plotters" version = "0.2.15" @@ -762,21 +701,6 @@ dependencies = [ "unicode-xid 0.2.1", ] -[[package]] -name = "systemd" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b273269ab8abffb87d5a3f2711f99ce51c0b9148143dee80940d85ab5e323935" -dependencies = [ - "cstr-argument", - "foreign-types", - "libc", - "libsystemd-sys", - "log", - "memchr", - "utf8-cstr", -] - [[package]] name = "take_mut" version = "0.2.2" @@ -850,12 +774,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" -[[package]] -name = "utf8-cstr" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55bcbb425141152b10d5693095950b51c3745d019363fc2929ffd8f61449b628" - [[package]] name = "walkdir" version = "2.3.1" diff --git a/Cargo.toml b/Cargo.toml index 92bcab6..e7ba67c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,6 @@ slog-term = "^2.6" nix = "^0.18" num-derive = "^0.2" num-traits = "^0.2" -systemd = "^0.6" [dev-dependencies] criterion = "^0.3" diff --git a/README.md b/README.md index 54fae74..9aaa21d 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,13 @@ and mailing lists. ## Installing -You can use the provided debian package to install `nsncd` to run under -`systemd`. It currently only works when managed by `systemd`, if you're -interested in managing it in other ways, we'd need to modify how process startup -works. +Just run the `nsncd` binary and it will listen at `/var/run/nscd/socket`. +There's a simple `systemd` unit file, too. + +If you're on a Debian-based system, you can use the provided Debian package to +install `nsncd` to run under `systemd`. See `debian/README.source` for how to +build it - we use a few Rust crates that aren't packaged for stable Debian +releases. ## Bug Reports and Contributions @@ -28,4 +31,4 @@ Please create GitHub issues and/or pull requests. ## License -`nsncd` is licensed under the [Apache License 2.0](./LICENSE). \ No newline at end of file +`nsncd` is licensed under the [Apache License 2.0](./LICENSE). diff --git a/debian/Cargo.toml.append b/debian/Cargo.toml.append index 99835a1..65076f5 100644 --- a/debian/Cargo.toml.append +++ b/debian/Cargo.toml.append @@ -1,5 +1,4 @@ [patch.crates-io] -systemd = { path = "vendor-patched/systemd" } syn = { path = "vendor/syn" } diff --git a/debian/README.source b/debian/README.source index d60cefc..9908505 100644 --- a/debian/README.source +++ b/debian/README.source @@ -16,9 +16,6 @@ internet. The "debian/rules vendor" target does the following: - Runs "cargo vendor" and sets up Cargo configuration - - Works around an issue where the current version of rustc in - Stretch/Buster fails to compile the latest version of the "systemd" - crate - Works around an issue where the "syn" crate includes a .gitignore file, which is automatically ignored by dpkg-source when building the source package diff --git a/debian/changelog b/debian/changelog index f8c5f19..9e93309 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +nsncd (1.1) unstable; urgency=medium + + * Remove systemd socket activation (closes twosigma/nsncd#9, internal + issue SPDE-8114). + + -- Geoffrey Thomas Mon, 08 Mar 2021 14:06:57 -0500 + nsncd (1.0) unstable; urgency=medium * Initial release. diff --git a/debian/control b/debian/control index 23471f0..18db544 100644 --- a/debian/control +++ b/debian/control @@ -2,7 +2,7 @@ Source: nsncd Section: misc Priority: optional Maintainer: Geoffrey Thomas -Build-Depends: debhelper (>= 12), cargo, libsystemd-dev, pkg-config +Build-Depends: debhelper (>= 12), cargo, pkg-config Standards-Version: 3.9.8 Homepage: https://github.com/twosigma/nsncd VCS-Git: https://github.com/twosigma/nsncd diff --git a/debian/install b/debian/install index 06b640f..7f30550 100644 --- a/debian/install +++ b/debian/install @@ -1,3 +1,2 @@ target/release/nsncd usr/lib *.service lib/systemd/system -*.socket lib/systemd/system diff --git a/debian/rules b/debian/rules index 46c1339..2a2f5cb 100755 --- a/debian/rules +++ b/debian/rules @@ -10,9 +10,5 @@ override_dh_auto_build: vendor: mkdir -p .cargo cargo vendor > .cargo/config - mkdir vendor-patched - mv vendor/systemd vendor-patched - find vendor-patched/systemd -name .cargo-checksum.json -delete - find vendor-patched/systemd -name \*.rs -exec sed -i 's/use \(\(..*\)::.*\) as \2/use ::\1 as \2/' {} + cat debian/Cargo.toml.append >> Cargo.toml echo 'nsncd: source-is-missing vendor/*' > debian/source/lintian-overrides diff --git a/nsncd.service b/nsncd.service index 89d5af5..7f6c3d0 100644 --- a/nsncd.service +++ b/nsncd.service @@ -18,3 +18,6 @@ Description=name-service non-caching daemon [Service] ExecStart=/usr/lib/nsncd Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/nsncd.socket b/nsncd.socket deleted file mode 100644 index efe1275..0000000 --- a/nsncd.socket +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright 2020 Two Sigma Open Source, LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -[Socket] -ListenStream=/var/run/nscd/socket - -[Install] -WantedBy=sockets.target diff --git a/src/main.rs b/src/main.rs index e64780d..78949b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,17 +45,15 @@ // - daemon/pidfile stuff use std::io::prelude::*; -use std::os::unix::{ - io::FromRawFd, - net::{UnixListener, UnixStream}, -}; +use std::os::unix::fs::PermissionsExt; +use std::os::unix::net::{UnixListener, UnixStream}; +use std::path::Path; use std::thread; -use anyhow::{ensure, Context, Result}; +use anyhow::{Context, Result}; use slog::{debug, error, Drain}; use slog_async; use slog_term; -use systemd::daemon::{listen_fds, LISTEN_FDS_START}; mod ffi; mod handlers; @@ -74,6 +72,8 @@ fn handle_stream(log: &slog::Logger, mut stream: UnixStream) -> Result<()> { Ok(()) } +const SOCKET_PATH: &str = "/var/run/nscd/socket"; + fn main() -> Result<()> { ffi::disable_internal_nscd(); @@ -83,13 +83,11 @@ fn main() -> Result<()> { let logger = slog::Logger::root(drain, slog::o!()); - let listen_fds_found = listen_fds(true)?; - ensure!( - listen_fds_found == 1, - "expected one listen fd, got {}", - listen_fds_found - ); - let listener = unsafe { UnixListener::from_raw_fd(LISTEN_FDS_START) }; + let path = Path::new(SOCKET_PATH); + std::fs::create_dir_all(path.parent().expect("socket path has no parent"))?; + std::fs::remove_file(path).ok(); + let listener = UnixListener::bind(path).context("could not bind to socket")?; + std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o777))?; for stream in listener.incoming() { match stream {