byssus 0.1.0

Live, read-only filesystem attachments between isolated workspaces
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
//! The `byssusd` daemon: startup, event loop, reload and shutdown.
//!
//! See `docs/DESIGN.md`, "Daemon lifecycle".

use std::collections::BTreeSet;
use std::os::fd::AsFd;
use std::time::{Duration, Instant};

use anyhow::{Context as _, bail};
use rustix::event::{Timespec, epoll};

use crate::app::{self, ConfigSource};
use crate::config::{Config, OwnershipPolicy};
use crate::notify::Notifier;
use crate::privileges::plan::Goal;
use crate::reconcile::{self, Trigger};
use crate::runtime::Runtime;
use crate::signals::SignalFd;
use crate::state::{State, StateStore};
use crate::watcher::Watcher;

/// Daemon command-line options.
#[derive(Debug, Clone, Default)]
pub struct Options {
    /// Configuration location.
    pub config: ConfigSource,
    /// Service user override.
    pub user: Option<String>,
    /// Permit running as root without a service user.
    pub allow_root: bool,
    /// Permit target roots on slave mounts.
    pub allow_slave_namespace: bool,
}

const TOKEN_INOTIFY: u64 = 1;
const TOKEN_SIGNAL: u64 = 2;

/// Membership changes are applied after this quiet period, so that a burst of
/// events (for example a directory being deleted file by file) is seen as a
/// whole — including any removal of the membership directory itself.
const DEBOUNCE: Duration = Duration::from_millis(150);
/// Upper bound on how long continuous changes can postpone a pass.
const MAX_DEBOUNCE: Duration = Duration::from_secs(1);

/// Most group names listed in the systemd status line.
const MAX_STATUS_NAMES: usize = 5;

struct Daemon {
    options: Options,
    config: Config,
    runtime: Runtime,
    watcher: Watcher,
    store: StateStore,
    state: State,
    degraded: reconcile::Degraded,
    unique_supported: bool,
    epoll: std::os::fd::OwnedFd,
    next_resync: Option<Instant>,
    /// A pending inotify-triggered pass: (first change, due time).
    pending: Option<(Instant, Instant)>,
    /// Observation notes already logged.
    notes: reconcile::NoteLog,
    /// systemd notifications.
    notifier: Notifier,
    /// Why the most recent reload was rejected, until one succeeds.
    last_reload_error: Option<String>,
    /// Groups and sets whose directories could not be read in the last pass.
    unreadable: BTreeSet<String>,
}

/// Runs the daemon until `SIGTERM` or `SIGINT`.
pub fn run(options: Options) -> anyhow::Result<()> {
    let notifier = Notifier::from_env().unwrap_or_else(|e| {
        tracing::warn!(msg = "ignoring invalid NOTIFY_SOCKET", error = %e);
        Notifier::disabled()
    });
    let loaded = app::load_config(&options.config, OwnershipPolicy::Enforce)?;
    let config = loaded.config;
    app::set_umask();
    let user = options.user.clone().or_else(|| config.daemon.user.clone());
    app::normalize_privileges(Goal::KeepSysAdmin, user.as_deref(), options.allow_root)?;
    let environment = app::check_environment()?;
    app::verify_config_paths(&config)?;
    let writer = app::open_state_for_write(&config)?;
    let mut runtime = Runtime::open(&config)?;
    app::check_propagation(&mut runtime, options.allow_slave_namespace)?;

    // Watches and signals are set up before the initial reconcile so that no
    // change during startup is missed.
    let signals = SignalFd::new(&[libc::SIGHUP, libc::SIGTERM, libc::SIGINT])
        .context("cannot set up signal handling")?;
    let watcher = Watcher::new(&runtime)?;
    let epoll = epoll::create(epoll::CreateFlags::CLOEXEC).context("epoll_create")?;
    epoll::add(
        &epoll,
        signals.as_fd(),
        epoll::EventData::new_u64(TOKEN_SIGNAL),
        epoll::EventFlags::IN,
    )?;
    epoll::add(
        &epoll,
        watcher.as_fd(),
        epoll::EventData::new_u64(TOKEN_INOTIFY),
        epoll::EventFlags::IN,
    )?;

    let crate::app::Writer { store, lock, state } = writer;
    let mut daemon = Daemon {
        options,
        config,
        runtime,
        watcher,
        store,
        state,
        degraded: reconcile::Degraded::default(),
        unique_supported: environment.features.unique_mount_ids(),
        epoll,
        next_resync: None,
        pending: None,
        notes: reconcile::NoteLog::default(),
        notifier,
        last_reload_error: None,
        unreadable: BTreeSet::new(),
    };
    tracing::info!(
        msg = "byssusd started",
        version = crate::VERSION,
        groups = daemon.config.groups.len(),
        records = daemon.state.len(),
    );
    daemon.pass(Trigger::Startup);
    // Ready only once the startup reconcile has populated the views, so units
    // ordered after byssusd (such as container runtimes) see every member.
    daemon.notifier.ready(&daemon.status_text());
    let result = daemon.event_loop(&signals);
    daemon.notifier.stopping();

    if let Err(e) = daemon.store.save(&daemon.state) {
        tracing::error!(msg = "cannot write state file on shutdown", error = %e);
    }
    drop(lock);
    drop(environment);
    tracing::info!(msg = "byssusd stopped; mounts are preserved");
    result
}

impl Daemon {
    fn pass(&mut self, trigger: Trigger) {
        let watcher = &mut self.watcher;
        let pass = reconcile::run_pass(
            &mut self.runtime,
            &mut self.state,
            &self.store,
            reconcile::PassOptions {
                degraded: &self.degraded,
                unique_supported: self.unique_supported,
                trigger,
            },
            &mut self.notes,
            &mut |runtime| watcher.sync(runtime),
        );
        self.unreadable.clear();
        for note in &pass.observed.notes {
            match note {
                reconcile::observe::Note::MembershipUnreadable { group, .. } => {
                    self.unreadable.insert(group.to_string());
                }
                reconcile::observe::Note::SetRootUnreadable { set, .. } => {
                    self.unreadable.insert(reconcile::set_label(set));
                }
                reconcile::observe::Note::MembershipDeleted { group } => {
                    self.degraded.groups.insert(group.clone());
                }
                reconcile::observe::Note::SetRootDeleted { set } => {
                    self.degraded.sets.insert(set.clone());
                }
                _ => {}
            }
        }
        let level_debug = pass.plan.steps.is_empty() && pass.plan.findings.is_empty();
        if level_debug {
            tracing::debug!(msg = "reconcile complete", trigger = %trigger, steps = 0);
        } else {
            tracing::info!(
                msg = "reconcile complete",
                trigger = %trigger,
                steps = pass.plan.steps.len(),
                completed = pass.execution.completed(),
                failed = pass.execution.failures(),
                findings = pass.plan.findings.len(),
            );
        }
        self.next_resync = self
            .config
            .daemon
            .resync_interval
            .map(|interval| Instant::now() + interval);
        self.notifier.status(&self.status_text());
    }

    fn status_text(&self) -> String {
        use std::fmt::Write as _;
        let mut text = format!(
            "{} group(s), {} mount(s)",
            self.runtime.groups.len(),
            self.state.len()
        );
        if !self.degraded.is_empty() {
            let names: Vec<String> = self
                .degraded
                .groups
                .iter()
                .map(ToString::to_string)
                .chain(self.degraded.sets.iter().map(reconcile::set_label))
                .collect();
            let _ = write!(text, "; degraded: {}", names.join(", "));
        }
        if !self.unreadable.is_empty() {
            let shown: Vec<&str> = self
                .unreadable
                .iter()
                .take(MAX_STATUS_NAMES)
                .map(String::as_str)
                .collect();
            let _ = write!(text, "; unreadable: {}", shown.join(", "));
            if self.unreadable.len() > MAX_STATUS_NAMES {
                let _ = write!(
                    text,
                    " and {} more",
                    self.unreadable.len() - MAX_STATUS_NAMES
                );
            }
            text.push_str(" (see byssus status)");
        }
        if let Some(error) = &self.last_reload_error {
            let _ = write!(
                text,
                "; last reload failed, running previous configuration: {error}"
            );
        }
        text
    }

    fn event_loop(&mut self, signals: &SignalFd) -> anyhow::Result<()> {
        let mut events = Vec::with_capacity(4);
        loop {
            let deadline = [self.next_resync, self.pending.map(|(_, due)| due)]
                .into_iter()
                .flatten()
                .min();
            let timeout = deadline.map(|deadline| {
                duration_to_timespec(deadline.saturating_duration_since(Instant::now()))
            });
            events.clear();
            match epoll::wait(
                &self.epoll,
                rustix::buffer::spare_capacity(&mut events),
                timeout.as_ref(),
            ) {
                Ok(_) => {}
                Err(rustix::io::Errno::INTR) => continue,
                Err(e) => return Err(e).context("epoll_wait"),
            }

            let mut run_pass = None;
            for event in &events {
                match event.data.u64() {
                    TOKEN_SIGNAL => {
                        while let Some(signal) = signals.read().context("reading signalfd")? {
                            match i32::try_from(signal).unwrap_or(-1) {
                                libc::SIGHUP => {
                                    self.notifier.reloading();
                                    if self.reload() {
                                        self.pending = None;
                                        self.pass(Trigger::Reload);
                                    }
                                    self.notifier.ready(&self.status_text());
                                }
                                libc::SIGTERM | libc::SIGINT => {
                                    tracing::info!(msg = "shutdown requested", signal = signal);
                                    return Ok(());
                                }
                                other => tracing::debug!(msg = "ignoring signal", signal = other),
                            }
                        }
                    }
                    TOKEN_INOTIFY => self.handle_inotify()?,
                    _ => {}
                }
            }

            let now = Instant::now();
            if run_pass.is_none() && self.pending.is_some_and(|(_, due)| now >= due) {
                run_pass = Some(Trigger::Inotify);
            }
            if run_pass.is_none() && self.next_resync.is_some_and(|due| now >= due) {
                run_pass = Some(Trigger::Resync);
            }
            if let Some(trigger) = run_pass {
                self.pending = None;
                self.pass(trigger);
            }
        }
    }

    fn handle_inotify(&mut self) -> anyhow::Result<()> {
        let changes = self.watcher.drain().context("reading inotify events")?;
        for group in &changes.lost {
            if self.degraded.groups.insert(group.clone()) {
                tracing::error!(
                    op = "degrade",
                    group = %group,
                    msg = "membership directory was removed, moved or unmounted; keeping existing mounts and making no changes to this group until configuration is reloaded (SIGHUP)",
                );
            }
        }
        for set in &changes.lost_sets {
            if self.degraded.sets.insert(set.clone()) {
                tracing::error!(
                    op = "degrade",
                    group = %reconcile::set_label(set),
                    msg = "group set membership root was removed, moved or unmounted; keeping existing mounts and making no changes to this set until configuration is reloaded (SIGHUP)",
                );
            }
        }
        if changes.overflow {
            tracing::warn!(msg = "inotify queue overflowed; running a full reconcile");
        }
        if changes.membership_changed || changes.overflow {
            let now = Instant::now();
            let first = self.pending.map_or(now, |(first, _)| first);
            self.pending = Some((first, (now + DEBOUNCE).min(first + MAX_DEBOUNCE)));
        }
        Ok(())
    }

    /// Transactional reload. Returns whether the new configuration was
    /// applied.
    fn reload(&mut self) -> bool {
        tracing::info!(msg = "reloading configuration");
        match self.prepare_reload() {
            Ok((config, runtime, watcher)) => {
                if let Err(e) = self.swap_watcher(&watcher) {
                    tracing::error!(msg = "reload failed; keeping previous configuration", error = %format!("{e:#}"));
                    self.last_reload_error = Some(first_line(&format!("{e:#}")));
                    return false;
                }
                self.config = config;
                self.runtime = runtime;
                self.watcher = watcher;
                self.degraded = reconcile::Degraded::default();
                self.last_reload_error = None;
                tracing::info!(
                    msg = "configuration reloaded",
                    groups = self.config.groups.len()
                );
                true
            }
            Err(e) => {
                tracing::error!(msg = "reload failed; keeping previous configuration", error = %format!("{e:#}"));
                self.last_reload_error = Some(first_line(&format!("{e:#}")));
                false
            }
        }
    }

    fn prepare_reload(&self) -> anyhow::Result<(Config, Runtime, Watcher)> {
        let loaded = app::load_config(&self.options.config, OwnershipPolicy::Enforce)?;
        let config = loaded.config;
        if config.daemon.state_dir != self.config.daemon.state_dir {
            bail!(
                "daemon.state_dir changed from {} to {}; restart byssusd to change it",
                self.config.daemon.state_dir,
                config.daemon.state_dir
            );
        }
        if config.daemon.user != self.config.daemon.user {
            tracing::warn!(msg = "daemon.user changed; it takes effect on restart");
        }
        app::verify_config_paths(&config)?;
        let mut runtime = Runtime::open(&config)?;
        app::check_propagation(&mut runtime, self.options.allow_slave_namespace)?;
        let watcher = Watcher::new(&runtime)?;
        Ok((config, runtime, watcher))
    }

    fn swap_watcher(&self, new: &Watcher) -> anyhow::Result<()> {
        epoll::add(
            &self.epoll,
            new.as_fd(),
            epoll::EventData::new_u64(TOKEN_INOTIFY),
            epoll::EventFlags::IN,
        )
        .context("registering new inotify instance")?;
        epoll::delete(&self.epoll, self.watcher.as_fd())
            .context("removing old inotify instance")?;
        Ok(())
    }
}

fn first_line(s: &str) -> String {
    s.lines().next().unwrap_or_default().to_owned()
}

fn duration_to_timespec(d: Duration) -> Timespec {
    Timespec {
        tv_sec: i64::try_from(d.as_secs()).unwrap_or(i64::MAX),
        tv_nsec: i64::from(d.subsec_nanos()),
    }
}