aea4d90463
A container process that daemonizes/double-forks and setsid()'s away can escape bwrap's own pid tree and outlive the session, whether it ends via a normal exit, a forwarded SIGINT/SIGTERM, or -D/--daemonize -- --kill's own cgroup-based sweep already reaches such a straggler for a still-running session, but nothing ran that sweep automatically once the session itself ended. Export kill_via_cgroup() (previously kill_session.cpp-local) and call it from run_bwrap(), right after run_process_foreground() returns and before the session cgroup is removed, whenever anything is still left in it -- runs unconditionally regardless of why bwrap exited, and covers -D for free since it re-enters this same run_bwrap() call from within the daemonized child.
47 lines
2.3 KiB
C
47 lines
2.3 KiB
C
// Copyright (C) 2026 Viorel Munteanu
|
|
//
|
|
// This program is free software; you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License along
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#pragma once
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include "session_cgroup.h"
|
|
|
|
// Strategy 1 of kill_session()'s own three (see below): SIGTERM every pid
|
|
// currently in `cgroup`, wait up to `grace_period_seconds`, then force via
|
|
// `cgroup.kill` (or a fresh per-pid SIGKILL sweep if that knob isn't
|
|
// available). Returns true once the cgroup is confirmed empty.
|
|
//
|
|
// Exported here (not just kill_session.cpp-local) so run_bwrap() (bwrap.cpp)
|
|
// can reuse it directly for its own automatic post-exit straggler sweep --
|
|
// it already holds a SessionCgroup handle from create_session_cgroup(), so
|
|
// it calls this rather than the full kill_session(pid) wrapper below (see
|
|
// kill_session.cpp's own doc comment on this function for why that wrapper
|
|
// doesn't fit there).
|
|
bool kill_via_cgroup(const SessionCgroup& cgroup, int grace_period_seconds);
|
|
|
|
// Stops a tracked, running -r/--run session identified by `pid` (the same
|
|
// tracked pid --list-processes/-x/--exec use) -- see CLAUDE.md's
|
|
// kill_session.{h,cpp} entry for the three strategies this picks between,
|
|
// depending on what's actually available for that session (a dedicated
|
|
// cgroup, an isolated pid namespace, or neither). Sends SIGTERM first, waits
|
|
// up to `grace_period_seconds` for the session to actually stop, then forces
|
|
// it with SIGKILL if it's still there. Returns 0 once confirmed stopped, or 1
|
|
// if `pid` isn't a tracked/running session (logged with spdlog::error) or it
|
|
// still hadn't stopped a few seconds after the forced kill (also logged with
|
|
// spdlog::error).
|
|
int kill_session(pid_t pid, int grace_period_seconds = 10);
|