bye 0.1.6

A library for graceful shutdown with no downtime
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
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(missing_docs, clippy::pedantic)]
//! bye: graceful shutdown and USR1 zero-downtime upgrade helpers.
//!
//! - `Bye` manages a `CancellationToken` broadcast + a `TaskTracker`.
//! - `Bye::new_with_signals()` listens for TERM/INT/QUIT -> drain, USR1 -> fork+exec self and wait for child `ready()`.
//! - `ready()` tells the parent (or your process manager) that the service is ready.
//! - `systemd_tcp_listener(port)` uses socket activation if available.

use std::{
    env::VarError,
    ffi::{CString, OsStr},
    fmt::Display,
    net::{IpAddr, Ipv4Addr, SocketAddr},
    os::{
        fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd},
        unix::ffi::OsStrExt,
    },
    path::{Path, PathBuf},
    sync::{atomic::AtomicU32, LazyLock},
    time::Duration,
};

use bye::Error;
use nix::{
    errno::Errno,
    fcntl::{FcntlArg, FdFlag, OFlag, fcntl},
    sys::{socket::getsockname, wait::waitpid},
    unistd::{ForkResult, execve, fork, pipe2, read},
};
use tokio::{io::unix::AsyncFd, net::TcpListener};

pub use bye::Bye;
pub use tokio_util::sync::CancellationToken;

#[cfg(feature = "tracing")]
use tracing::{error, info, warn};

mod bye;

const READY_ENV: &str = "UPGRADE_FD";
static READY_LAST_PID: AtomicU32 = AtomicU32::new(0);

#[derive(Debug)]
struct UpgradeUsr1 {
    exe_path: CString,
    args: Vec<CString>,
    env: Vec<CString>,
}

impl UpgradeUsr1 {
    pub fn new(exe_path: &Path) -> bye::Result<Self> {
        let exe_path = CString::new(exe_path.as_os_str().as_bytes())?;
        let args = std::env::args_os()
            .map(|arg| CString::new(arg.as_bytes()))
            .collect::<Result<Vec<_>, _>>()?;

        let env = std::env::vars_os()
            .map(|(key, value)| {
                let mut kv = Vec::new();
                kv.extend(key.as_bytes());
                kv.push(b'=');
                kv.extend(value.as_bytes());
                CString::new(kv)
            })
            .collect::<Result<Vec<_>, _>>()?;

        Ok(Self {
            exe_path,
            args,
            env,
        })
    }

    pub async fn upgrade(&self, timeout: Option<Duration>) -> bye::Result<bool> {
        fork_and_exec(&self.exe_path, &self.args, &self.env, timeout).await
    }
}

fn cstr_kv_eq_ignore_ascii(e: &CString, key: &[u8]) -> bool {
    let bytes = e.as_bytes();
    if let Some(eq) = memchr::memchr(b'=', bytes) {
        bytes[..eq].eq_ignore_ascii_case(key)
    } else {
        false
    }
}

fn replace_or_push_env<D: Display>(env: &mut Vec<CString>, key: &str, val: D) -> bye::Result<()> {
    let kbytes = key.as_bytes();
    if let Some(idx) = env.iter().position(|e| cstr_kv_eq_ignore_ascii(e, kbytes)) {
        env[idx] = CString::new(format!("{key}={val}"))?;
    } else {
        env.push(CString::new(format!("{key}={val}"))?);
    }
    Ok(())
}

fn get_env_u32(env: &[CString], key: &str) -> Option<u32> {
    let kbytes = key.as_bytes();
    for e in env {
        let bytes = e.as_bytes();
        if let Some(eq) = memchr::memchr(b'=', bytes) {
            if bytes[..eq].eq_ignore_ascii_case(kbytes) {
                if let Ok(s) = std::str::from_utf8(&bytes[eq + 1..]) {
                    if let Ok(n) = s.parse::<u32>() {
                        return Some(n);
                    }
                }
            }
        }
    }
    None
}

async fn fork_and_exec(
    path: &CString,
    args: &[CString],
    env: &[CString],
    timeout: Option<Duration>,
) -> bye::Result<bool> {
    let (read_fd, write_fd) = pipe2(OFlag::O_CLOEXEC).map_err(Error::Pipe2)?;

    clear_cloexec(&write_fd)?;
    set_nonblocking(&read_fd)?;

    let mut env_with_fd = Vec::with_capacity(env.len() + 1);
    env_with_fd.extend(
        env.iter()
            .filter(|e| !cstr_kv_eq_ignore_ascii(e, READY_ENV.as_bytes()))
            .cloned(),
    );
    env_with_fd.push(CString::new(format!(
        "{}={}",
        READY_ENV,
        write_fd.as_raw_fd()
    ))?);

    // cstr for execve with old argv
    let argv_ref: Vec<&_> = args.iter().map(std::ffi::CString::as_c_str).collect();

    match unsafe { fork().map_err(Error::Fork)? } {
        ForkResult::Parent { child } => {
            drop(env_with_fd);

            // make sure the write fd is closed in the parent
            drop(write_fd);

            let af = AsyncFd::new(read_fd)?;
            // could have a timeout here if desired

            let fut = async move {
                let mut buf = [0; 1];
                loop {
                    let mut guard = af.readable().await?;
                    match read(&af, &mut buf) {
                        Ok(0) => return Ok(false),
                        Ok(_) => return Ok(true),
                        Err(Errno::EAGAIN) => {
                            // continue waiting
                            guard.clear_ready();
                        }
                        Err(Errno::EINTR) => {
                            // interrupted, continue waiting
                            guard.clear_ready();
                        }
                        Err(e) => {
                            return Err(Error::Nix(e));
                        }
                    }
                }
            };

            let result = if let Some(dur) = timeout {
                tokio::time::timeout(dur, fut).await
            } else {
                Ok(fut.await)
            };

            match result {
                Ok(Ok(true)) => Ok(true),
                Ok(Ok(false)) => Ok(false),
                Ok(Err(e)) => Err(e),
                Err(_) => {
                    use nix::sys::signal::{Signal, kill};
                    kill(child, Signal::SIGKILL).map_err(|e| Error::KillChild {
                        pid: child.into(),
                        source: e,
                    })?;
                    waitpid(child, None).map_err(Error::WaitPid)?;
                    Err(Error::ChildTimeout)
                }
            }
        }
        ForkResult::Child => {
            drop(read_fd);

            if let Some(nfds) = get_env_u32(&env_with_fd, "LISTEN_FDS") {
                if nfds > 0 {
                    let child_pid = std::process::id();
                    replace_or_push_env(&mut env_with_fd, "LISTEN_PID", child_pid).ok();

                    for i in 0..nfds {
                        #[allow(clippy::cast_possible_wrap)]
                        let fd = (3 + i) as i32;
                        let borrowed = unsafe { BorrowedFd::borrow_raw(fd) };
                        if let Err(e) = clear_cloexec(&borrowed) {
                            #[cfg(feature = "tracing")]
                            error!("failed to clear cloexec on fd {}: {}", fd, e);
                            std::process::exit(127);
                        }
                    }
                }
            }

            let envp: Vec<&_> = env_with_fd.iter().map(std::ffi::CString::as_c_str).collect();

            execve(path, &argv_ref, &envp).map_err(Error::Execve)?;
            std::process::exit(127);
        }
    }
}

fn parse_fd_from_env(v: &OsStr) -> bye::Result<i32> {
    let s = std::str::from_utf8(v.as_bytes()).map_err(|e| Error::EnvUtf8 {
        key: "UPGRADE_FD",
        source: e,
    })?;
    let fd = s.parse::<i32>().map_err(|e| Error::EnvParse {
        key: "UPGRADE_FD",
        source: e,
    })?;
    if fd < 0 {
        return Err(Error::InvalidUpgradeFd);
    }
    Ok(fd)
}

/// Tries to get the PID file path from the `PIDFILE` environment variable.
/// # Errors
/// - `VarError::NotPresent` if the `PIDFILE` environment variable is not set.
/// - `VarError::NotUnicode` if the `PIDFILE` environment variable is not valid Unicode.
/// - `VarError::Empty` if the `PIDFILE` environment variable is set but empty.
pub fn try_pid_file() -> Result<PathBuf, VarError> {
    Ok(std::env::var("PIDFILE")?.into())
}

static SYSTEMD_PORTS: LazyLock<Vec<u16>> = std::sync::LazyLock::new(|| {
    compute_systemd_ports().unwrap_or_else(|e| {
        #[cfg(feature = "tracing")]
        error!("Failed to compute systemd ports: {}", e);
        vec![]
    })
});

/// Returns the list of ports inherited from systemd socket activation.
/// This is computed once and cached for the lifetime of the program.
#[must_use]
pub fn systemd_ports() -> &'static [u16] {
    &SYSTEMD_PORTS
}

fn compute_systemd_ports() -> bye::Result<Vec<u16>> {
    let listen_fds = std::env::var("LISTEN_FDS")
        .unwrap_or("0".to_string())
        .parse::<u32>()
        .map_err(|e| Error::EnvParse {
            key: "LISTEN_FDS",
            source: e,
        })?;

    let listen_pid = std::env::var("LISTEN_PID")
        .unwrap_or("0".to_string())
        .parse::<u32>()
        .map_err(|e| Error::EnvParse {
            key: "LISTEN_PID",
            source: e,
        })?;

    if listen_fds == 0 || listen_pid != std::process::id() {
        return Err(Error::SystemdActivation);
    }

    let mut ports = Vec::with_capacity(listen_fds as usize);
    for i in 0..listen_fds {
        #[allow(clippy::cast_possible_wrap)]
        let fd = (3 + i) as i32;

        let port = match getsockname::<nix::sys::socket::SockaddrStorage>(fd) {
            Ok(v) => v
                .as_sockaddr_in()
                .map(nix::sys::socket::SockaddrIn::port)
                .or_else(|| v.as_sockaddr_in6().map(nix::sys::socket::SockaddrIn6::port))
                .unwrap_or(0),
            Err(e) => {
                return Err(Error::Sockname(e));
            }
        };
        if port != 0 {
            ports.push(port);
        }
    }

    Ok(ports)
}

/// Creates a TCP listener that uses systemd socket activation if available.
/// If no systemd socket is found for the given port, it falls back to binding a new socket.
/// # Errors
/// - If the systemd socket activation is enabled but no socket is found for the given port.
/// - If binding a new socket fails.
pub async fn systemd_tcp_listener(port: u16) -> bye::Result<TcpListener> {
    let systemd_ports = SYSTEMD_PORTS.iter().position(|&p| p == port);
    let listener = if let Some(systemd_port) = systemd_ports {
        #[allow(clippy::cast_possible_truncation)]
        #[allow(clippy::cast_possible_wrap)]
        let fd = (3 + systemd_port) as i32;
        #[cfg(feature = "tracing")]
        info!("using systemd socket fd: {}", fd);
        let raw_listener = unsafe { std::net::TcpListener::from_raw_fd(fd) };
        raw_listener.set_nonblocking(true)?;
        TcpListener::from_std(raw_listener)?
    } else {
        #[cfg(feature = "tracing")]
        warn!("no systemd socket found for port {}", port);
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), port);
        TcpListener::bind(addr).await?
    };

    Ok(listener)
}

/// Notifies the system that the service is ready.
/// This function should be called once the service is fully initialized and ready to accept requests.
/// # Errors
/// - If the `UPGRADE_FD` environment variable is set but invalid.
/// - If writing to the `UPGRADE_FD` fails.
/// - If writing to the PID file (if `PIDFILE` is set) fails.
pub fn ready() -> bye::Result<()> {
    let pid = std::process::id();
    let prev = READY_LAST_PID.swap(pid, std::sync::atomic::Ordering::AcqRel);

    if prev == pid {
        return Ok(());
    }

    if let Some(val) = std::env::var_os(READY_ENV) {
        let fd = parse_fd_from_env(&val)?;
        let fd = unsafe { OwnedFd::from_raw_fd(fd) };
        fcntl(&fd, FcntlArg::F_GETFD).map_err(|e| Error::Fcntl {
            op: "F_GETFD",
            source: e,
        })?;

        loop {
            let n = nix::unistd::write(&fd, &[1u8]);
            match n {
                Ok(_) => break,
                Err(Errno::EINTR | Errno::EAGAIN) => {}
                Err(Errno::EPIPE) => {
                    #[cfg(feature = "tracing")]
                    warn!("UPGRADE_FD is closed, ignoring write");
                    break;
                }
                Err(e) => {
                    return Err(Error::NotifyWrite(e));
                }
            }
        }
    }

    if let Ok(pid_file) = try_pid_file() {
        let pid = std::process::id();
        std::fs::write(pid_file, pid.to_string())?;
    }

    Ok(())
}

fn clear_cloexec<F: AsFd>(fd: &F) -> bye::Result<()> {
    let getfd = fcntl(fd, FcntlArg::F_GETFD).map_err(|e| Error::Fcntl {
        op: "F_GETFD",
        source: e,
    })?;
    let flags = FdFlag::from_bits_truncate(getfd);
    let new_flags = flags.difference(FdFlag::FD_CLOEXEC);
    fcntl(fd, FcntlArg::F_SETFD(new_flags)).map_err(|e| Error::Fcntl {
        op: "F_SETFD",
        source: e,
    })?;
    Ok(())
}

fn set_nonblocking(fd: &OwnedFd) -> bye::Result<()> {
    let getfl = fcntl(fd, FcntlArg::F_GETFL).map_err(|e| Error::Fcntl {
        op: "F_GETFL",
        source: e,
    })?;
    let flags = OFlag::from_bits_truncate(getfl);
    let new_flags = flags | OFlag::O_NONBLOCK;
    fcntl(fd, FcntlArg::F_SETFL(new_flags)).map_err(|e| Error::Fcntl {
        op: "F_SETFL",
        source: e,
    })?;
    Ok(())
}