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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! What the tests of the restarts of services need: [`Manager`], a fake service manager, and
//! [`store`], the store of a service that a test runs in its own process.
//!
//! This is behind the `testing` cargo feature.

use std::{
    collections::VecDeque,
    env,
    ffi::OsStr,
    fmt,
    fs::remove_file,
    io::{self, ErrorKind, IoSliceMut},
    mem::MaybeUninit,
    os::{
        fd::{AsFd, AsRawFd, OwnedFd, RawFd},
        unix::{net::UnixDatagram, process::CommandExt},
    },
    path::{Path, PathBuf},
    process::{self, Command},
    sync::{
        Arc, Condvar, Mutex, MutexGuard, PoisonError,
        atomic::{AtomicUsize, Ordering},
    },
    thread::{self, JoinHandle},
    time::{Duration, Instant},
};

use rustix::{
    cmsg_space,
    io::fcntl_dupfd_cloexec,
    net::{RecvAncillaryBuffer, RecvAncillaryMessage, RecvFlags, recvmsg},
};

use crate::{
    Error, Passed, Result, Store,
    notifier::{MAX_FDS, Notifier},
    passed::START,
    queue::{self, Queue},
};

/// A service manager for tests, which runs a service as systemd does, with the sockets of socket
/// activation and a file descriptor store, without systemd.
///
/// It binds a notification socket of its own, in the temporary directory, and serves it on a
/// thread of its own, as systemd serves `$NOTIFY_SOCKET`. It keeps what a service stores, as
/// systemd does: one file descriptor of each open file description, under the name that the first
/// came with, and as many as its store takes ([`set_store_max`](Self::set_store_max)), closing the
/// others. It removes from the store what a service removes, and closes the pipe of a barrier,
/// which lets the service go on. It keeps the text of every notification, for the test to wait for
/// ([`wait_for`](Self::wait_for)).
///
/// [`command`](Self::command) starts a service as systemd starts one, with the sockets of socket
/// activation that the test gives the manager ([`add_socket`](Self::add_socket)) and what is in
/// the store, so that a test restarts a service by stopping it and starting it again. For a
/// service that runs in the test process, [`store`] gives the store that [`Store::from_env`] would,
/// out of [`notify_socket`](Self::notify_socket) and [`store_max`](Self::store_max), and
/// [`passed`](Self::passed) gives what [`Passed::from_env`] would.
pub struct Manager {
    /// The path of the notification socket.
    path: PathBuf,
    /// What the thread that serves the socket shares with the manager.
    shared: Arc<Shared>,
    serving: Option<JoinHandle<()>>,
    /// The sockets of socket activation, with their names.
    activation: Vec<(String, OwnedFd)>,
}

impl Manager {
    /// A manager whose store takes 16 file descriptors, and that has no socket of socket
    /// activation yet.
    pub fn new() -> Result<Self> {
        let path = env::temp_dir().join(unique_name());
        let socket = UnixDatagram::bind(&path)?;
        let shared = Arc::new(Shared {
            state: Mutex::new(State {
                store_max: DEFAULT_STORE_MAX,
                ..State::default()
            }),
            changed: Condvar::new(),
        });
        let serving = {
            let shared = shared.clone();
            thread::Builder::new()
                .name("zoter manager".to_owned())
                .spawn(move || serve(&socket, &shared))
        };
        let serving = match serving {
            Ok(serving) => serving,
            Err(e) => {
                let _ = remove_file(&path);

                return Err(e.into());
            }
        };

        Ok(Self {
            path,
            shared,
            serving: Some(serving),
            activation: Vec::new(),
        })
    }

    /// The path of the notification socket, as `$NOTIFY_SOCKET` names it.
    pub fn notify_socket(&self) -> &Path {
        &self.path
    }

    /// Have the store take `max` file descriptors, as `FileDescriptorStoreMax=` has systemd do.
    ///
    /// The services that the manager starts from then on get it as `$FDSTORE`, or no `$FDSTORE` if
    /// it is 0, as systemd keeps no store then. What the store keeps already stays.
    pub fn set_store_max(&mut self, max: usize) {
        self.shared.state().store_max = max;
    }

    /// How many file descriptors the store takes.
    pub fn store_max(&self) -> usize {
        self.shared.state().store_max
    }

    /// Pass `socket` to the services that the manager starts, under `name`, as a socket of socket
    /// activation, which the manager keeps open while they come and go.
    pub fn add_socket(&mut self, name: &str, socket: OwnedFd) {
        self.activation.push((name.to_owned(), socket));
    }

    /// Take the sockets of socket activation back, as when their units go.
    pub fn take_sockets(&mut self) -> Vec<(String, OwnedFd)> {
        std::mem::take(&mut self.activation)
    }

    /// What systemd would pass a service that it starts now: duplicates of the sockets of socket
    /// activation, then of what is in the store, with their names, as [`Passed::from_env`] gives
    /// them.
    pub fn passed(&self) -> Result<Passed> {
        let state = self.shared.state();
        let fds = self
            .activation
            .iter()
            .chain(&state.store)
            .map(|(name, fd)| Ok((name.clone(), fd.try_clone()?)))
            .collect::<io::Result<_>>()?;

        Ok(Passed::new(fds))
    }

    /// A command that runs `program` as systemd runs a service.
    ///
    /// Arguments added to the command go to `program`. It runs with what [`passed`](Self::passed)
    /// gives as its file descriptors from 3 on, with `$LISTEN_FDS`, `$LISTEN_FDNAMES` and
    /// `$LISTEN_PID` telling of them, and with `$NOTIFY_SOCKET` naming the socket of the manager,
    /// and `$FDSTORE` telling how many file descriptors the store takes, unless it takes none. A
    /// shell runs `program` as itself, so that `$LISTEN_PID` is the PID of `program`, which can't
    /// be known before it starts. The command keeps duplicates of the file descriptors to pass
    /// until it is dropped.
    pub fn command(&self, program: impl AsRef<OsStr>) -> Result<Command> {
        let passed = self.passed()?.into_vec();
        let count = passed.len();
        // The file descriptors may be among those they are to be passed as, so that passing one
        // would close another before it's passed. Copies out of that way, which close on exec, are
        // what is passed instead.
        let first_free = RawFd::try_from(count)
            .ok()
            .and_then(|count| START.checked_add(count))
            .ok_or_else(|| io::Error::from(ErrorKind::InvalidInput))?;
        let mut names = Vec::with_capacity(count);
        let mut copies = Vec::with_capacity(count);
        for (name, fd) in passed {
            names.push(name);
            copies.push(fcntl_dupfd_cloexec(&fd, first_free).map_err(Error::os)?);
        }
        let sources: Vec<RawFd> = copies.iter().map(AsRawFd::as_raw_fd).collect();

        let mut command = Command::new("sh");
        command
            .args(["-c", r#"LISTEN_PID=$$ exec "$0" "$@""#])
            .arg(program);
        // What the tests run with must not tell the service of other file descriptors, or of
        // another manager.
        for var in [
            "LISTEN_PID",
            "LISTEN_PIDFDID",
            "LISTEN_FDS",
            "LISTEN_FDNAMES",
        ] {
            command.env_remove(var);
        }
        command.env("NOTIFY_SOCKET", &self.path);
        // systemd tells of a store that takes file descriptors, and leaves the variable unset for
        // a service without one.
        match self.store_max() {
            0 => command.env_remove("FDSTORE"),
            max => command.env("FDSTORE", max.to_string()),
        };
        if count > 0 {
            command
                .env("LISTEN_FDS", count.to_string())
                .env("LISTEN_FDNAMES", names.join(":"));
        }
        // SAFETY: The closure runs in the child, between `fork` and `exec`, where only
        // async-signal-safe functions may be called: it calls `dup2`, and `last_os_error` if that
        // fails, which reads `errno` and neither allocates nor takes a lock. It owns the copies,
        // which are open in the child, as the command holds them, and so the closure, for as long
        // as it may spawn. They are all `first_free` or above, so none is among the targets, 3 up
        // to that, and no `dup2` closes a source that another has yet to copy. A target that is
        // open is closed, but only in the child, whose copy nothing uses but for `std`'s pipe for
        // reporting a failed `exec`. `dup2` clears the close-on-exec flag of the target, which
        // keeps it open across `exec`. The copies stay close-on-exec, so they close then.
        unsafe {
            command.pre_exec(move || {
                let _copies = &copies;
                for (target, source) in (START..).zip(&sources) {
                    if libc::dup2(*source, target) < 0 {
                        return Err(io::Error::last_os_error());
                    }
                }

                Ok(())
            });
        }

        Ok(command)
    }

    /// Wait, `timeout` at most, for a notification that has `state` as one of its lines, such as
    /// `READY=1`, and let go of those that came before it.
    ///
    /// Each notification satisfies only one wait: a second wait for `READY=1` waits for another
    /// one. It fails with [`Error::Io`] of [`ErrorKind::TimedOut`] if none comes in time. It does
    /// not use [`Error::TimedOut`], because that error means that the manager is late, and here the
    /// service is late.
    pub fn wait_for(&self, state: &str, timeout: Duration) -> Result<()> {
        let deadline = Instant::now() + timeout;
        let mut guard = self.shared.state();
        loop {
            while let Some(notification) = guard.notifications.pop_front() {
                if notification.lines().any(|line| line == state) {
                    return Ok(());
                }
            }
            let left = deadline.saturating_duration_since(Instant::now());
            if left.is_zero() {
                return Err(io::Error::new(
                    ErrorKind::TimedOut,
                    format!("no `{state}` came in time"),
                )
                .into());
            }
            guard = self
                .shared
                .changed
                .wait_timeout(guard, left)
                .unwrap_or_else(PoisonError::into_inner)
                .0;
        }
    }

    /// Wait, `timeout` at most, for the manager to have dealt with what the services sent it so
    /// far.
    ///
    /// The manager deals with what it gets on a thread of its own, in the order that it came. So
    /// what a service stored, removed or told it just before may not show yet, as in
    /// [`stored_names`](Self::stored_names). This sends the manager a barrier of the test's own,
    /// which shows among the notifications as `BARRIER=1`, and waits for the manager to get to
    /// it. It fails with [`Error::TimedOut`] if the manager takes longer than `timeout`.
    pub fn barrier(&self, timeout: Duration) -> Result<()> {
        Notifier::new(&self.path)?.barrier(timeout)
    }

    /// The text of each notification that came and that [`wait_for`](Self::wait_for) didn't let
    /// go of, oldest first, to wait for none of them again.
    ///
    /// The manager deals with the notifications on a thread of its own, so a notification sent
    /// just before this may not be among them: [`barrier`](Self::barrier) waits for those.
    pub fn take_notifications(&self) -> Vec<String> {
        self.shared.state().notifications.drain(..).collect()
    }

    /// The names of what is in the store, in the order that it was stored.
    pub fn stored_names(&self) -> Vec<String> {
        let state = self.shared.state();

        state.store.iter().map(|(name, _)| name.clone()).collect()
    }

    /// Duplicates of what is in the store, with their names, in the order that it was stored.
    pub fn stored(&self) -> Result<Vec<(String, OwnedFd)>> {
        let state = self.shared.state();

        state
            .store
            .iter()
            .map(|(name, fd)| Ok((name.clone(), fd.try_clone()?)))
            .collect()
    }

    /// How many file descriptors the hand-over under `key` holds in its queue sockets, those of its
    /// state included, in its items, as a service stored them with
    /// [`Store::hand_over`](crate::Store::hand_over).
    ///
    /// The queue sockets give their file descriptors up as they are read, so this takes them out
    /// and puts them back in queue sockets of its own, which take their places in the store. It
    /// fails as reading a queue does, as with a socket that no hand-over stored.
    pub fn stored_fds(&self, key: &str) -> Result<usize> {
        let name = format!("{key}/fds");
        let mut state = self.shared.state();
        let mut count = 0;
        for (_, socket) in state.store.iter_mut().filter(|(stored, _)| *stored == name) {
            let mut taken = Vec::new();
            queue::take(socket.as_fd(), |index, fds, _| taken.push((index, fds)))?;
            let refilled = Queue::new()?;
            for (index, fds) in &taken {
                let fds: Vec<_> = fds.iter().map(AsFd::as_fd).collect();
                if !refilled.push(*index, &fds)? {
                    return Err(io::Error::other("a queue of the manager is full").into());
                }
                count += fds.len();
            }
            *socket = refilled.into_socket();
        }

        Ok(count)
    }

    /// Put `fds` in the store in place of what it holds, as for a test to hand a service over what
    /// no service stored.
    pub fn replace_store(&self, fds: Vec<(String, OwnedFd)>) {
        self.shared.state().store = fds;
    }

    /// The names that services removed from the store since this was last called, in the order
    /// that they were removed.
    pub fn take_removed(&self) -> Vec<String> {
        std::mem::take(&mut self.shared.state().removed)
    }

    /// Close what is in the store, as systemd does once a service stops for good.
    pub fn clear_store(&self) {
        self.shared.state().store.clear();
    }
}

impl fmt::Debug for Manager {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Manager")
            .field("path", &self.path)
            .field("activation", &self.activation)
            .field("stored", &self.stored_names())
            .finish_non_exhaustive()
    }
}

impl Drop for Manager {
    fn drop(&mut self) {
        self.shared.state().stopping = true;
        // Wake the thread up, as it may be waiting for a notification.
        if let Ok(waking) = UnixDatagram::unbound() {
            let _ = waking.send_to(&[], &self.path);
        }
        if let Some(serving) = self.serving.take() {
            let _ = serving.join();
        }
        let _ = remove_file(&self.path);
    }
}

/// The store of a service whose manager takes notifications at `notify_socket`, under `key`,
/// which takes `max` file descriptors: the one that [`Store::from_env`] gives a service whose
/// `$NOTIFY_SOCKET` is `notify_socket` and whose `$FDSTORE` is `max`.
///
/// It is for a service that a test runs in its own process, where the environment tells nothing
/// of the manager. The test gives it the [`notify_socket`](Manager::notify_socket) and the
/// [`store_max`](Manager::store_max) of a [`Manager`], or those of a manager that it plays itself.
///
/// It fails as [`Store::from_env`] does for such a `$NOTIFY_SOCKET`, `$FDSTORE` and key.
pub fn store(notify_socket: impl AsRef<OsStr>, key: &str, max: usize) -> Result<Store> {
    Store::with_notifier(Notifier::new(notify_socket)?, key, max)
}

/// What the thread that serves the notification socket shares with the manager.
struct Shared {
    state: Mutex<State>,
    /// Notified when a notification comes.
    changed: Condvar,
}

impl Shared {
    fn state(&self) -> MutexGuard<'_, State> {
        self.state.lock().unwrap_or_else(PoisonError::into_inner)
    }
}

#[derive(Default)]
struct State {
    /// The file descriptor store, with the names that each file descriptor was stored under.
    store: Vec<(String, OwnedFd)>,
    /// How many file descriptors the store takes.
    store_max: usize,
    /// The names removed from the store, that the test didn't take yet.
    removed: Vec<String>,
    /// The text of each notification that the test didn't wait for yet, oldest first.
    notifications: VecDeque<String>,
    /// Whether the manager is going, and the thread is to stop.
    stopping: bool,
}

/// Serve `socket`, the notification socket, until the manager goes.
fn serve(socket: &UnixDatagram, shared: &Shared) {
    loop {
        let received = receive(socket);
        let mut state = shared.state();
        if state.stopping {
            return;
        }
        let Ok((text, fds)) = received else {
            continue;
        };
        process(&mut state, &text, fds);
        state.notifications.push_back(text);
        drop(state);
        shared.changed.notify_all();
    }
}

/// Deal with the notification `text`, and the file descriptors that came with it, as systemd does.
///
/// It keeps those that it stores, and closes the others: the pipe of a barrier among them, after
/// the notifications that came before it, which lets the service go on.
fn process(state: &mut State, text: &str, fds: Vec<OwnedFd>) {
    let assignments: Vec<_> = text
        .lines()
        .filter_map(|line| line.split_once('='))
        .collect();
    let value = |key: &str| {
        assignments
            .iter()
            .find(|(assigned, _)| *assigned == key)
            .map(|(_, value)| *value)
    };
    let name = value("FDNAME");
    if value("FDSTORE") == Some("1") {
        let name = name.unwrap_or("stored");
        for fd in fds {
            let kept = state.store.iter().any(|(_, stored)| same_file(stored, &fd));
            if !kept && state.store.len() < state.store_max {
                state.store.push((name.to_owned(), fd));
            }
        }
    } else if value("FDSTOREREMOVE") == Some("1")
        && let Some(name) = name
    {
        state.store.retain(|(stored, _)| stored != name);
        state.removed.push(name.to_owned());
    }
}

/// The next notification that `socket` gets, with the file descriptors that come with it.
fn receive(socket: &UnixDatagram) -> io::Result<(String, Vec<OwnedFd>)> {
    let mut text = [0; MAX_NOTIFICATION];
    let mut space = [MaybeUninit::uninit(); cmsg_space!(ScmRights(MAX_FDS))];
    let mut control = RecvAncillaryBuffer::new(&mut space);
    let received = recvmsg(
        socket,
        &mut [IoSliceMut::new(&mut text)],
        &mut control,
        RecvFlags::CMSG_CLOEXEC,
    )?;
    // Every file descriptor is taken, so that none stays open in the kernel.
    let fds = control
        .drain()
        .filter_map(|message| match message {
            RecvAncillaryMessage::ScmRights(fds) => Some(fds),
            _ => None,
        })
        .flatten()
        .collect();
    let text = String::from_utf8_lossy(&text[..received.bytes]).into_owned();

    Ok((text, fds))
}

/// Whether `a` and `b` are of the same open file description, as systemd tells through `kcmp(2)`,
/// if the kernel lets the process call that.
fn same_file(a: &OwnedFd, b: &OwnedFd) -> bool {
    // SAFETY: `getpid` has no preconditions.
    let pid = libc::c_long::from(unsafe { libc::getpid() });
    // SAFETY: The call reads and writes no memory, and takes each of its arguments as a long.
    let order = unsafe {
        libc::syscall(
            libc::SYS_kcmp,
            pid,
            pid,
            KCMP_FILE,
            libc::c_long::from(a.as_fd().as_raw_fd()),
            libc::c_long::from(b.as_fd().as_raw_fd()),
        )
    };

    order == 0
}

/// A name that no other manager uses, of this process or another.
fn unique_name() -> String {
    // Managers can be made on several threads at once.
    static NEXT: AtomicUsize = AtomicUsize::new(0);

    format!(
        "zoter-manager-{}-{}",
        process::id(),
        NEXT.fetch_add(1, Ordering::Relaxed)
    )
}

/// How many file descriptors the store takes unless set otherwise.
const DEFAULT_STORE_MAX: usize = 16;
/// The longest notification that systemd takes.
const MAX_NOTIFICATION: usize = 4096;
/// What `kcmp(2)` compares of two file descriptors: their open file descriptions.
const KCMP_FILE: libc::c_long = 0;

#[cfg(test)]
mod tests {
    use std::{os::fd::AsFd, process::Stdio};

    use rustix::io::write;

    use super::*;
    use crate::{
        Error,
        test_utils::{pipe, read_byte},
    };

    #[test]
    fn store_and_remove() {
        let manager = Manager::new().unwrap();
        let notifier = Notifier::new(manager.notify_socket()).unwrap();
        let (read, write_end) = pipe();

        notifier.store("a", &[write_end.as_fd()]).unwrap();
        // Once of each open file description, under the name that it first came with.
        notifier.store("b", &[write_end.as_fd()]).unwrap();
        notifier.store("b", &[pipe().1.as_fd()]).unwrap();
        notifier.barrier(WAIT).unwrap();
        assert_eq!(manager.stored_names(), ["a", "b"]);
        let (_, stored) = manager.stored().unwrap().remove(0);
        write(&stored, b"x").unwrap();
        assert_eq!(read_byte(&read), b'x');

        notifier.remove("b").unwrap();
        notifier.barrier(WAIT).unwrap();
        assert_eq!(manager.stored_names(), ["a"]);
        assert_eq!(manager.take_removed(), ["b"]);
        assert!(manager.take_removed().is_empty());

        manager.clear_store();
        assert!(manager.stored_names().is_empty());
    }

    #[test]
    fn store_takes_its_max() {
        let mut manager = Manager::new().unwrap();
        manager.set_store_max(2);
        let notifier = Notifier::new(manager.notify_socket()).unwrap();
        let fds: Vec<_> = (0..3).map(|_| pipe().1).collect();
        let borrowed: Vec<_> = fds.iter().map(AsFd::as_fd).collect();

        notifier.store("a", &borrowed).unwrap();
        notifier.barrier(WAIT).unwrap();
        assert_eq!(manager.stored_names(), ["a", "a"]);
    }

    #[test]
    fn wait_for_notifications() {
        let manager = Manager::new().unwrap();
        let notifier = Notifier::new(manager.notify_socket()).unwrap();

        notifier.notify("STATUS=Starting").unwrap();
        notifier.notify("READY=1\nSTATUS=Up").unwrap();
        manager.wait_for("READY=1", WAIT).unwrap();
        // Each notification is waited for once.
        let error = manager
            .wait_for("READY=1", Duration::from_millis(100))
            .unwrap_err();
        assert!(
            matches!(&error, Error::Io(e) if e.kind() == ErrorKind::TimedOut),
            "{error:?}"
        );

        notifier.notify("STATUS=Down").unwrap();
        manager.barrier(WAIT).unwrap();
        assert_eq!(manager.take_notifications(), ["STATUS=Down", "BARRIER=1"]);
        assert!(manager.take_notifications().is_empty());
    }

    #[test]
    fn passed_as_systemd_passes_them() {
        let mut manager = Manager::new().unwrap();
        let notifier = Notifier::new(manager.notify_socket()).unwrap();
        manager.add_socket("example.socket", pipe().1);
        notifier.store("stored", &[pipe().1.as_fd()]).unwrap();
        notifier.barrier(WAIT).unwrap();

        let passed = manager.passed().unwrap();
        let names: Vec<_> = passed.iter().map(|(name, _)| name).collect();
        assert_eq!(names, ["example.socket", "stored"]);
        assert_eq!(manager.take_sockets().len(), 1);
        assert_eq!(manager.passed().unwrap().len(), 1);
    }

    /// The file descriptors of a hand-over are counted, and stay in the store.
    #[test]
    fn stored_fds() {
        let manager = Manager::new().unwrap();
        let store = super::store(manager.notify_socket(), "test", 2).unwrap();
        let items = [(1, Fds(vec![pipe().1])), (2, Fds(vec![pipe().1, pipe().1]))];
        store.hand_over(&[][..], items).unwrap();

        assert_eq!(manager.stored_fds("test").unwrap(), 3);
        assert_eq!(manager.stored_fds("test").unwrap(), 3);
        assert_eq!(manager.stored_fds("other").unwrap(), 0);
        let mut passed = manager.passed().unwrap();
        let mut store = super::store(manager.notify_socket(), "test", 2).unwrap();
        let mut restored = store.take_over(&mut passed).unwrap().unwrap();
        let fds: Vec<_> = restored.take_items().map(|item| item.fds.len()).collect();
        assert_eq!(fds, [1, 2]);
    }

    /// File descriptors with an empty record.
    struct Fds(Vec<OwnedFd>);

    impl crate::Save for Fds {
        fn saved_len(&self) -> Option<usize> {
            Some(0)
        }

        fn save(&self, _dest: &mut [u8]) -> Result<()> {
            Ok(())
        }

        fn fds(&self) -> Vec<std::os::fd::BorrowedFd<'_>> {
            self.0.iter().map(AsFd::as_fd).collect()
        }
    }

    /// The service runs with its file descriptors from 3 on, and the variables that tell of them
    /// and of the manager. `$FDSTORE` is unset for a store that takes no file descriptors.
    #[test]
    fn command() {
        let mut manager = Manager::new().unwrap();
        manager.add_socket("first", pipe().1);
        manager.add_socket("second", pipe().1);
        let script = r#"
            test "$LISTEN_PID" = "$$" && test -e /proc/$$/fd/3 && test -e /proc/$$/fd/4 &&
            echo "$LISTEN_FDS $LISTEN_FDNAMES ${FDSTORE-unset} $NOTIFY_SOCKET $1"
        "#;

        for (max, told) in [(4, "4"), (0, "unset")] {
            manager.set_store_max(max);
            let output = manager
                .command("sh")
                .unwrap()
                .args(["-c", script, "sh", "arg"])
                .stdin(Stdio::null())
                .output()
                .unwrap();
            assert!(output.status.success(), "{output:?}");
            let expected = format!(
                "2 first:second {told} {} arg\n",
                manager.notify_socket().display()
            );
            assert_eq!(String::from_utf8_lossy(&output.stdout), expected);
        }
    }

    const WAIT: Duration = Duration::from_secs(10);
}