zoter 0.2.0

Restart systemd services without dropping their clients
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! The file descriptors that systemd passes to a service: [`Passed`].

use std::{
    env::{self, VarError},
    os::fd::{AsFd, BorrowedFd, FromRawFd, OwnedFd, RawFd},
    process,
    str::FromStr,
};

use rustix::io::{FdFlags, fcntl_setfd};

use crate::{Error, Result};

/// The file descriptors that the service manager passed to this process, with their names.
///
/// systemd passes a service the sockets of the socket units that activate it, and the file
/// descriptors that were stored in its store before it was restarted, which come last. The name of
/// a socket is the `FileDescriptorName=` of its unit, by default the name of the unit (`connection`
/// if the unit has `Accept=yes`). That of a stored file descriptor is the name it was stored under,
/// or `stored` if it was stored with none, and `unknown` stands for a name that systemd didn't
/// send. The names aren't unique, and the file descriptors come in no set order but for the stored
/// ones coming last, so tell them apart by their names rather than their places.
///
/// [`Store::take_over`](crate::Store::take_over) takes those of a hand-over out of it, and what is
/// left is for the service to take, by name.
#[derive(Debug, Default)]
pub struct Passed {
    fds: Vec<(String, OwnedFd)>,
    /// The names of the file descriptors that this was created with, whatever was taken out of it
    /// since.
    created_with: Vec<String>,
}

impl Passed {
    /// Take the file descriptors that systemd passed to this process, as `sd_listen_fds(3)` does.
    ///
    /// It takes none if systemd passed none to this process: either the environment lists none, or
    /// it lists those of another process. It fails with [`Error::Environment`] if the environment
    /// describes them in a way that makes no sense. It fails with [`Error::Io`] if the environment
    /// lists a file descriptor that isn't open. When it fails, it closes the file descriptors that
    /// it took up to then. The file descriptors that it takes are close-on-exec.
    ///
    /// Whether it succeeds or not, it removes `$LISTEN_PID`, `$LISTEN_FDS` and `$LISTEN_FDNAMES`
    /// from the environment, as `sd_listen_fds(3)` does when asked to, and `$LISTEN_PIDFDID` as
    /// well, which systemd 259 and later set, and which it doesn't check. So no call but the first
    /// takes the file descriptors, and the processes that this one starts don't inherit what tells
    /// of them.
    ///
    /// # Safety
    ///
    /// No other thread may read or write the environment meanwhile, as for
    /// [`std::env::remove_var`]: call this before the process starts any other thread.
    ///
    /// Nothing else in the process may own the file descriptors that the environment tells of, as
    /// nothing does when systemd passes them: this takes them as its own, and closes them as
    /// they're dropped. So nothing may have taken them before, such as an implementation of
    /// `sd_listen_fds(3)` that left the environment as it was, as `sd_listen_fds(0)` does. File
    /// descriptors that the process took some other way go to [`Passed::new`] instead.
    pub unsafe fn from_env() -> Result<Self> {
        // Read all the variables before any of them can end this early, as all of them go either
        // way.
        let pid = var("LISTEN_PID");
        let fds = var("LISTEN_FDS");
        let names = var("LISTEN_FDNAMES");
        for name in [
            "LISTEN_PID",
            "LISTEN_PIDFDID",
            "LISTEN_FDS",
            "LISTEN_FDNAMES",
        ] {
            // SAFETY: No other thread reads or writes the environment meanwhile, as the caller
            // sees to.
            unsafe { env::remove_var(name) };
        }

        let Some(count) = parse_count(pid?.as_deref(), fds?.as_deref(), process::id())? else {
            return Ok(Self::default());
        };
        // Once taken, the file descriptors close again if the names turn out to make no sense.
        let fds = take_fds(count)?;
        let names = parse_names(names?.as_deref(), count)?;

        Ok(Self::new(names.into_iter().zip(fds).collect()))
    }

    /// File descriptors passed to this process some other way, with their names.
    pub fn new(fds: Vec<(String, OwnedFd)>) -> Self {
        let created_with = fds.iter().map(|(name, _)| name.clone()).collect();

        Self { fds, created_with }
    }

    /// Take the first file descriptor named `name`, if any.
    pub fn take(&mut self, name: &str) -> Option<OwnedFd> {
        let index = self.fds.iter().position(|(passed, _)| passed == name)?;

        Some(self.fds.remove(index).1)
    }

    /// Take every file descriptor named `name`, in their order.
    pub fn take_all(&mut self, name: &str) -> Vec<OwnedFd> {
        self.fds
            .extract_if(.., |(passed, _)| passed == name)
            .map(|(_, fd)| fd)
            .collect()
    }

    /// The file descriptors that are left, with their names, in their order.
    pub fn iter(&self) -> impl ExactSizeIterator<Item = (&str, BorrowedFd<'_>)> {
        self.fds
            .iter()
            .map(|(name, fd)| (name.as_str(), fd.as_fd()))
    }

    /// How many file descriptors are left.
    pub fn len(&self) -> usize {
        self.fds.len()
    }

    /// Whether no file descriptor is left.
    pub fn is_empty(&self) -> bool {
        self.fds.is_empty()
    }

    /// The file descriptors that are left, with their names, in their order.
    pub fn into_vec(self) -> Vec<(String, OwnedFd)> {
        self.fds
    }

    /// Whether this was created with a file descriptor whose name doesn't start with `prefix`,
    /// whatever was taken out of it since.
    pub(crate) fn had_other_than(&self, prefix: &str) -> bool {
        self.created_with
            .iter()
            .any(|name| !name.starts_with(prefix))
    }

    /// Take every file descriptor whose name starts with `prefix`, with its name, in their order.
    pub(crate) fn take_prefixed(&mut self, prefix: &str) -> Vec<(String, OwnedFd)> {
        self.fds
            .extract_if(.., |(name, _)| name.starts_with(prefix))
            .collect()
    }
}

/// The value of the environment variable `name`, if it is set.
fn var(name: &str) -> Result<Option<String>> {
    match env::var(name) {
        Ok(value) => Ok(Some(value)),
        Err(VarError::NotPresent) => Ok(None),
        Err(VarError::NotUnicode(_)) => {
            Err(Error::Environment(format!("${name} isn't valid Unicode")))
        }
    }
}

/// How many file descriptors systemd passed to the process with PID `own_pid`, going by
/// `$LISTEN_PID` and `$LISTEN_FDS`: `None` if it passed none, because they are unset, or
/// `$LISTEN_PID` is another PID.
///
/// As `sd_listen_fds(3)` does, it takes `$LISTEN_FDS` for garbage if it isn't a number from 1 to
/// [`MAX_LISTEN_FDS`], but only looks at it if the PID is right.
fn parse_count(pid: Option<&str>, fds: Option<&str>, own_pid: u32) -> Result<Option<usize>> {
    let Some(pid) = pid else {
        return Ok(None);
    };
    if parse_number::<u32>("$LISTEN_PID", pid)? != own_pid {
        return Ok(None);
    }
    let Some(fds) = fds else {
        return Ok(None);
    };
    match parse_number("$LISTEN_FDS", fds)? {
        count @ 1..=MAX_LISTEN_FDS => Ok(Some(count)),
        count => Err(Error::Environment(format!(
            "$LISTEN_FDS is {count}, not a number from 1 to {MAX_LISTEN_FDS}"
        ))),
    }
}

/// Take the `count` file descriptors from [`START`] on, and make them close-on-exec.
///
/// Each is found to be open before it's taken: `$LISTEN_FDS` may tell of more than there are, and
/// an `OwnedFd` of one that isn't open aborts the process when it drops, in a debug build.
fn take_fds(count: usize) -> Result<Vec<OwnedFd>> {
    (START..)
        .take(count)
        .map(|fd| -> Result<OwnedFd> {
            // SAFETY: It's only borrowed for the call below, which fails with `EBADF` if it isn't
            // open, and does nothing else to it.
            let borrowed = unsafe { BorrowedFd::borrow_raw(fd) };
            fcntl_setfd(borrowed, FdFlags::CLOEXEC).map_err(Error::os)?;
            // SAFETY: It's open, as the call above shows, and systemd passed it to this process, as
            // `$LISTEN_PID` says. Nothing else owns it, as the caller of `Passed::from_env` sees
            // to, and nothing takes it after this does, as the variables that tell of it are gone.
            Ok(unsafe { OwnedFd::from_raw_fd(fd) })
        })
        .collect()
}

/// The names of `count` file descriptors, going by `$LISTEN_FDNAMES`.
///
/// There is one for each, or none at all, in which case they are all `unknown`.
fn parse_names(names: Option<&str>, count: usize) -> Result<Vec<String>> {
    let Some(names) = names else {
        return Ok(vec![UNKNOWN.to_owned(); count]);
    };
    let names: Vec<_> = names.split(':').map(str::to_owned).collect();
    if names.len() != count {
        return Err(Error::Environment(format!(
            "$LISTEN_FDNAMES has {} names for {count} file descriptors",
            names.len()
        )));
    }

    Ok(names)
}

/// The number that `value` is, for the environment variable `name`.
fn parse_number<T>(name: &str, value: &str) -> Result<T>
where
    T: FromStr,
    T::Err: std::error::Error,
{
    value
        .parse()
        .map_err(|e| Error::Environment(format!("{name} is {value:?}, not a number: {e}")))
}

/// The largest count that `$LISTEN_FDS` can have: `sd_listen_fds(3)` takes a larger one for garbage
/// too, as the number of the last file descriptor would overflow.
const MAX_LISTEN_FDS: usize = (RawFd::MAX - START) as usize;

/// The first file descriptor that systemd passes, `SD_LISTEN_FDS_START`.
pub(crate) const START: RawFd = 3;

/// The name of a file descriptor that systemd sent no names for.
const UNKNOWN: &str = "unknown";

#[cfg(test)]
mod tests {
    use std::os::fd::AsRawFd;

    use rustix::io::fcntl_getfd;

    use super::*;
    use crate::test_utils::{in_own_process, is_open, pipe};

    #[test]
    fn count() {
        assert_eq!(parse_count(Some("42"), Some("3"), 42).unwrap(), Some(3));
        assert_eq!(parse_count(Some("42"), Some("1"), 42).unwrap(), Some(1));
        assert_eq!(
            parse_count(Some("42"), Some(&MAX_LISTEN_FDS.to_string()), 42).unwrap(),
            Some(MAX_LISTEN_FDS)
        );
    }

    #[test]
    fn count_is_none_unless_for_this_process() {
        // Nothing is passed if either variable is unset, or the PID is another one. The count isn't
        // even looked at then, so it can't make anything fail.
        assert_eq!(parse_count(None, Some("3"), 42).unwrap(), None);
        assert_eq!(parse_count(Some("42"), None, 42).unwrap(), None);
        assert_eq!(parse_count(None, None, 42).unwrap(), None);
        assert_eq!(parse_count(Some("41"), Some("3"), 42).unwrap(), None);
        assert_eq!(parse_count(Some("43"), Some("3"), 42).unwrap(), None);
        assert_eq!(parse_count(Some("41"), Some("garbage"), 42).unwrap(), None);
    }

    #[test]
    fn count_garbage() {
        for pid in ["", "x", "-1", " 42", "42 ", "4.2", "4294967296"] {
            let error = parse_count(Some(pid), Some("3"), 42).unwrap_err();
            assert!(
                matches!(error, Error::Environment(_)),
                "$LISTEN_PID={pid:?}: {error:?}"
            );
        }

        let too_many = (MAX_LISTEN_FDS + 1).to_string();
        for fds in [
            "",
            "x",
            "-1",
            "0",
            "3 ",
            "1.5",
            "99999999999999999999",
            &too_many,
        ] {
            let error = parse_count(Some("42"), Some(fds), 42).unwrap_err();
            assert!(
                matches!(error, Error::Environment(_)),
                "$LISTEN_FDS={fds:?}: {error:?}"
            );
        }
    }

    #[test]
    fn names() {
        let names = parse_names(Some("web:ssh:stored"), 3).unwrap();
        assert_eq!(names, ["web", "ssh", "stored"]);

        // Names aren't unique, and can be anything but the separator, empty ones included.
        let names = parse_names(Some("a:a:unknown:"), 4).unwrap();
        assert_eq!(names, ["a", "a", "unknown", ""]);
        assert_eq!(parse_names(Some(""), 1).unwrap(), [""]);
        assert_eq!(parse_names(Some("one"), 1).unwrap(), ["one"]);
    }

    #[test]
    fn names_unknown() {
        assert_eq!(
            parse_names(None, 3).unwrap(),
            ["unknown", "unknown", "unknown"]
        );
        assert_eq!(parse_names(None, 1).unwrap(), ["unknown"]);
    }

    #[test]
    fn names_that_do_not_match_the_count() {
        for (names, count) in [("a:b", 3), ("a:b:c", 2), ("a", 2), ("", 2), ("a:b", 1)] {
            let error = parse_names(Some(names), count).unwrap_err();
            assert!(
                matches!(error, Error::Environment(_)),
                "{names:?} for {count}: {error:?}"
            );
        }
    }

    /// The file descriptors are taken by name, the first or all of them, in their order, and those
    /// with other names stay.
    #[test]
    fn take_by_name() {
        let fd = || pipe().1;
        let mut passed = Passed::new(vec![
            ("a".to_owned(), fd()),
            ("b".to_owned(), fd()),
            ("a".to_owned(), fd()),
            ("prefix/x".to_owned(), fd()),
            ("prefix/y".to_owned(), fd()),
        ]);
        let numbers =
            |passed: &Passed| -> Vec<_> { passed.iter().map(|(_, fd)| fd.as_raw_fd()).collect() };
        let all = numbers(&passed);

        assert!(passed.take("c").is_none());
        assert_eq!(passed.take("a").unwrap().as_raw_fd(), all[0]);
        let prefixed: Vec<_> = passed
            .take_prefixed("prefix/")
            .into_iter()
            .map(|(name, fd)| (name, fd.as_raw_fd()))
            .collect();
        assert_eq!(
            prefixed,
            [
                ("prefix/x".to_owned(), all[3]),
                ("prefix/y".to_owned(), all[4])
            ]
        );
        let names: Vec<_> = passed.iter().map(|(name, _)| name).collect();
        assert_eq!(names, ["b", "a"]);
        let taken: Vec<_> = passed
            .take_all("a")
            .iter()
            .map(AsRawFd::as_raw_fd)
            .collect();
        assert_eq!(taken, [all[2]]);
        assert_eq!(passed.len(), 1);
        assert!(passed.take_all("a").is_empty());
        let left = passed.into_vec();
        assert_eq!(left[0].0, "b");
        assert_eq!(left[0].1.as_raw_fd(), all[1]);
    }

    /// What this was created with is remembered by name, whatever is taken out of it later.
    #[test]
    fn had_other_than_remembers_what_it_was_created_with() {
        let fd = || pipe().1;
        let passed = |names: &[&str]| {
            Passed::new(names.iter().map(|name| (name.to_string(), fd())).collect())
        };

        assert!(!Passed::default().had_other_than("key/"));
        assert!(!passed(&[]).had_other_than("key/"));
        assert!(!passed(&["key/state", "key/fds", "key/fds"]).had_other_than("key/"));
        assert!(passed(&["key/state", "web"]).had_other_than("key/"));
        assert!(passed(&["other/state"]).had_other_than("key/"));
        assert!(passed(&["unknown"]).had_other_than("key/"));
        assert!(!passed(&["web"]).had_other_than(""));
        assert!(!passed(&["web"]).had_other_than("w"));

        let mut passed = passed(&["web", "key/state", "web", "key/fds", "ssh"]);
        assert!(passed.had_other_than("key/"));
        passed.take("web").unwrap();
        assert!(passed.had_other_than("key/"));
        assert_eq!(passed.take_all("web").len(), 1);
        assert!(passed.had_other_than("key/"));
        assert_eq!(passed.take_prefixed("key/").len(), 2);
        assert!(passed.had_other_than("key/"));
        passed.take("ssh").unwrap();
        assert!(passed.is_empty());
        assert!(passed.had_other_than("key/"));
        let left = passed.into_vec();
        assert!(left.is_empty());
    }

    #[test]
    fn had_other_than_ignores_what_was_taken_out_when_nothing_else_was_there() {
        let mut passed = Passed::new(vec![("key/state".to_owned(), pipe().1)]);
        passed.take_prefixed("key/");
        assert!(passed.is_empty());
        assert!(!passed.had_other_than("key/"));
    }

    #[test]
    fn from_the_environment() {
        let exports = "LISTEN_PID=$$ LISTEN_FDS=2 LISTEN_FDNAMES=first:second LISTEN_PIDFDID=1";
        if !in_own_process("passed::tests::from_the_environment", exports) {
            return;
        }

        // SAFETY: The test runs in a process of its own, which has no other thread that reads or
        // writes the environment, nor anything that owns the file descriptors that it tells of.
        let passed = unsafe { Passed::from_env() }.unwrap();
        let names: Vec<_> = passed.iter().map(|(name, _)| name).collect();
        assert_eq!(names, ["first", "second"]);
        let numbers: Vec<_> = passed.iter().map(|(_, fd)| fd.as_raw_fd()).collect();
        assert_eq!(numbers, [3, 4]);
        for (_, fd) in passed.iter() {
            assert!(fcntl_getfd(fd).unwrap().contains(FdFlags::CLOEXEC));
        }
        assert_listen_variables_removed();
        // There is nothing left to take.
        // SAFETY: As above.
        assert!(unsafe { Passed::from_env() }.unwrap().is_empty());
    }

    #[test]
    fn from_the_environment_of_another_process() {
        let exports = "LISTEN_PID=1 LISTEN_FDS=2 LISTEN_FDNAMES=a:b LISTEN_PIDFDID=1";
        if !in_own_process(
            "passed::tests::from_the_environment_of_another_process",
            exports,
        ) {
            return;
        }

        // SAFETY: The test runs in a process of its own, which has no other thread that reads or
        // writes the environment, nor anything that owns the file descriptors that it tells of.
        assert!(unsafe { Passed::from_env() }.unwrap().is_empty());
        assert_listen_variables_removed();
        // The file descriptors are not for this process, which leaves them as they are.
        assert!(is_open(3) && is_open(4));
    }

    #[test]
    fn from_the_environment_closes_all_on_error() {
        // There are two file descriptors, and one name.
        let exports = "LISTEN_PID=$$ LISTEN_FDS=2 LISTEN_FDNAMES=a";
        if !in_own_process(
            "passed::tests::from_the_environment_closes_all_on_error",
            exports,
        ) {
            return;
        }

        // SAFETY: The test runs in a process of its own, which has no other thread that reads or
        // writes the environment, nor anything that owns the file descriptors that it tells of.
        let error = unsafe { Passed::from_env() }.unwrap_err();
        assert!(matches!(error, Error::Environment(_)), "{error:?}");
        assert!(!is_open(3) && !is_open(4));
        assert_listen_variables_removed();
    }

    #[test]
    fn from_the_environment_with_fds_that_are_not_open() {
        // Only the file descriptors 3 and 4 are open.
        let exports = "LISTEN_PID=$$ LISTEN_FDS=3";
        let test = "passed::tests::from_the_environment_with_fds_that_are_not_open";
        if !in_own_process(test, exports) {
            return;
        }

        // SAFETY: The test runs in a process of its own, which has no other thread that reads or
        // writes the environment, nor anything that owns the file descriptors that it tells of.
        let error = unsafe { Passed::from_env() }.unwrap_err();
        let Error::Io(error) = error else {
            panic!("{error:?}");
        };
        assert_eq!(
            error.raw_os_error(),
            Some(rustix::io::Errno::BADF.raw_os_error())
        );
        assert!(!is_open(3) && !is_open(4));
        assert_listen_variables_removed();
    }

    fn assert_listen_variables_removed() {
        for var in [
            "LISTEN_PID",
            "LISTEN_PIDFDID",
            "LISTEN_FDS",
            "LISTEN_FDNAMES",
        ] {
            assert!(env::var_os(var).is_none(), "${var} is still set");
        }
    }
}