From nobody Sat Sep 19 16:19:52 2026 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4hnF7r6psJz6syVG for ; Sat, 19 Sep 2026 16:19:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR2" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4hnF7r63Rrz3Btn for ; Sat, 19 Sep 2026 16:19:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1789834792; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/NMnsIW8VfjKfUnOd1emHkPzWi1QlGiHn67p7/3tolU=; b=p6b80vzwIJqq4sopqD+Qiulk9bi9dG9Gn0yn6XZku7j12r5KlmteJeqeepPNt0DJC1kjRH nOP1rTbAiEm3tKkY4VRrhqGZrcXZEdCkpUz6wX4TyBjoqrneq9yX+z/eeIzINPQWYsK623 P0m7jgowUMlm+/B6OOScWcmq39ecFsdEEuW5NYIYonWAnCssIpfNHr6t7wwgCSqWHkh/bb 9yFW6yViBig8ANc4tPuzrLyAfxTSr2B/NV8DqXWrQwglkRJjdtmbFW3796YjNc6sT8HzSi pKnvHAGRBABQ1Oc9ce3WTLTJO5X6q+O6wAFe/3sccMqWI4w5SdaF/z2BvrhhlA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1789834792; b=kqRALSlKSzJhPcU0qYtOSpGzYbMWm8u6nR/WquHXH/JFOABiRqGrxbTvPiex1YC5//Z5Ma QEXOjo3UD52K6h/5nFeJog0Awbsw6fBXtlrXTD/Pf3WRM/GKDRac14W38QZd2dXJQFuMzK MeiMWciqPU80rDN5bCYaC1/scybY8W4U2vhdKmIrqq9xh6kQfOkChZo3zgn/LQBbaHjhPl nad/krdJbIp/iPU7t3S7i01mzZhxOkLttsBypwob1T2guExvzotR6YzEDVr16tdhpSZbDS W5Zg18vWb9uJ0laHnlxspfps22irxkHQTAixT4THcXm6SvfojKv9Oqu3SiUnzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1789834792; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/NMnsIW8VfjKfUnOd1emHkPzWi1QlGiHn67p7/3tolU=; b=pBNmgIbt/ahH5J4DzJo+8MIXZuFlKM2+hzseq0dDPjw7q5Qmq+Y+0OcsIDB5FkwN2gm94c HF9jWJJqHEoKmtAc7npQEEDq1tBuoCq3eOC8YA9Sy7sL2dfKWfmSTa7bivx1xucr7VdGMo H6yghJ8I5GdNxUzHBfwWXBblBXtGD4l6rn0Ss9TcmV/3l3pLeMkgwTQhJ6vZMdzUq+fVKa orEKV3CdcyhLJONntE3H2s3j6A1XREZFDhwDfVqWc2g/nb10q3FCVwMpSNhNOjfkmFMHtT jyxfaCcOVeYzxrtdFGpBBT0qv79T8gPh/e6+cppsNPx9p2DQrGHEm80h+vVqbw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hnF7r56khz19LK for ; Sat, 19 Sep 2026 16:19:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 22881 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sat, 19 Sep 2026 16:19:52 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Roman Bogorodskiy Subject: git: ed60e5710e74 - main - bhyvectl: support querying VM pid List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: novel X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: ed60e5710e7489651c2829f220d1e87b3ed7429a Auto-Submitted: auto-generated Date: Sat, 19 Sep 2026 16:19:52 +0000 Message-Id: <6aaeb628.22881.5bfedb45@gitrepo.freebsd.org> The branch main has been updated by novel: URL: https://cgit.FreeBSD.org/src/commit/?id=ed60e5710e7489651c2829f220d1e87b3ed7429a commit ed60e5710e7489651c2829f220d1e87b3ed7429a Author: Roman Bogorodskiy AuthorDate: 2026-08-22 08:50:00 +0000 Commit: Roman Bogorodskiy CommitDate: 2026-09-19 16:19:35 +0000 bhyvectl: support querying VM pid Extend bhyvectl(8) to support querying VM pid using the `--get-vm-pid` flag. This is useful in monitor mode when the VM pid differs from the main bhyve(8) process run by the user. Knowing the VM pid is necessary, for example, to query process resource usage or trigger ACPI shutdown with SIGTERM. Of course, it could be obtained by matching the monitor process children by the process title, but it's a little more complex and fragile than it could be. Implement that by adding the "get_vm_pid" IPC command, and using it to implement `bhyvectl --get-vm-pid`, which prints the VM PID. When the VM PID is not known, ESRCH is returned. Reviewed by: bnovkov (previous iteration without man changes) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D59108 --- usr.sbin/bhyve/bhyve_config.5 | 2 +- usr.sbin/bhyve/ipc.c | 11 ++++++++ usr.sbin/bhyvectl/Makefile | 3 +-- usr.sbin/bhyvectl/bhyvectl.8 | 8 ++++-- usr.sbin/bhyvectl/bhyvectl.c | 60 ++++++++++++++++++++++++++++++++++++------- 5 files changed, 70 insertions(+), 14 deletions(-) diff --git a/usr.sbin/bhyve/bhyve_config.5 b/usr.sbin/bhyve/bhyve_config.5 index 37a740aec3ec..55742b6fd5a8 100644 --- a/usr.sbin/bhyve/bhyve_config.5 +++ b/usr.sbin/bhyve/bhyve_config.5 @@ -232,7 +232,7 @@ Configuration can specify up to 255 strings. .It Va rundir Ta String Ta Pa /var/run/bhyve/ Ta Path to the runtime directory for .Xr bhyve 8 -to store its service files, such as the checkpoint socket. +to store its service files, such as the IPC socket. Overriding this path is useful when running .Xr bhyve 8 as an unprivileged user. diff --git a/usr.sbin/bhyve/ipc.c b/usr.sbin/bhyve/ipc.c index cdf07e18ed3f..ba938e99aeb5 100644 --- a/usr.sbin/bhyve/ipc.c +++ b/usr.sbin/bhyve/ipc.c @@ -56,6 +56,17 @@ static struct ipc_thread_ctx { int sockfd; } thr_ctx; +static nvlist_t * +ipc_get_vm_pid(struct vmctx *ctx __unused, const nvlist_t *nvl __unused) +{ + nvlist_t *reply; + + reply = nvlist_create(0); + nvlist_add_number(reply, "pid", getpid()); + return (reply); +} +IPC_COMMAND(get_vm_pid, ipc_get_vm_pid); + static nvlist_t * handle_message(struct vmctx *ctx, nvlist_t *nvl) { diff --git a/usr.sbin/bhyvectl/Makefile b/usr.sbin/bhyvectl/Makefile index 8cfb1169e585..829e5a5734e7 100644 --- a/usr.sbin/bhyvectl/Makefile +++ b/usr.sbin/bhyvectl/Makefile @@ -10,12 +10,11 @@ PACKAGE= bhyve MAN= bhyvectl.8 -LIBADD= vmmapi util +LIBADD= vmmapi util nv CFLAGS+= -I${.CURDIR} -I${SRCTOP}/sys/amd64/vmm .if ${MK_BHYVE_SNAPSHOT} != "no" -LIBADD+= nv CFLAGS+= -DBHYVE_SNAPSHOT # usr.sbin/bhyve/snapshot.h needs ucl header diff --git a/usr.sbin/bhyvectl/bhyvectl.8 b/usr.sbin/bhyvectl/bhyvectl.8 index b999230cfd73..366fc2e8b3ef 100644 --- a/usr.sbin/bhyvectl/bhyvectl.8 +++ b/usr.sbin/bhyvectl/bhyvectl.8 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd July 8, 2026 +.Dd September 11, 2026 .Dt BHYVECTL 8 .Os .Sh NAME @@ -37,6 +37,7 @@ .Op Fl -create .Op Fl -destroy .Op Fl -get-stats +.Op Fl -get-vm-pid .Op Fl -inject-nmi .Op Fl -force-reset .Op Fl -force-poweroff @@ -61,6 +62,9 @@ Create the specified VM. Destroy the specified VM. .It Fl -get-stats Retrieve statistics for the specified VM. +.It Fl -get-vm-pid +Print the process ID of the bhyve process running the specified VM. +In monitor mode, this is the child process executing the VM. .It Fl -inject-nmi Inject a non-maskable interrupt (NMI) into the VM. .It Fl -force-reset @@ -79,7 +83,7 @@ Save a snapshot of a virtual machine similar to The virtual machine will terminate after the snapshot has been saved. .It Fl -rundir= Ns Ar -Specify path to the directory containing the checkpoint socket. +Specify path to the directory containing the bhyve IPC socket. If not specified, .Pa /var/run/bhyve is used. diff --git a/usr.sbin/bhyvectl/bhyvectl.c b/usr.sbin/bhyvectl/bhyvectl.c index ba2518267b14..3f67daa7be21 100644 --- a/usr.sbin/bhyvectl/bhyvectl.c +++ b/usr.sbin/bhyvectl/bhyvectl.c @@ -72,6 +72,7 @@ static int get_active_cpus, get_debug_cpus, get_suspended_cpus; static uint64_t memsize; static int run; static int get_cpu_topology; +static int get_vm_pid; #ifdef BHYVE_SNAPSHOT static int vm_suspend_opt; #endif @@ -87,8 +88,8 @@ enum { #ifdef BHYVE_SNAPSHOT SET_CHECKPOINT_FILE, SET_SUSPEND_FILE, - SET_RUNDIR, #endif + SET_RUNDIR, OPT_LAST, }; @@ -137,11 +138,12 @@ setup_options(void) { "get-debug-cpus", NO_ARG, &get_debug_cpus, 1 }, { "get-suspended-cpus", NO_ARG, &get_suspended_cpus, 1 }, { "get-cpu-topology", NO_ARG, &get_cpu_topology, 1 }, + { "get-vm-pid", NO_ARG, &get_vm_pid, 1 }, #ifdef BHYVE_SNAPSHOT { "checkpoint", REQ_ARG, 0, SET_CHECKPOINT_FILE}, { "suspend", REQ_ARG, 0, SET_SUSPEND_FILE}, - { "rundir", REQ_ARG, 0, SET_RUNDIR}, #endif + { "rundir", REQ_ARG, 0, SET_RUNDIR}, }; return (bhyvectl_opts(common_opts, nitems(common_opts))); @@ -158,8 +160,8 @@ usage(const struct option *opts) #ifdef BHYVE_SNAPSHOT [SET_CHECKPOINT_FILE] = "filename", [SET_SUSPEND_FILE] = "filename", - [SET_RUNDIR] = "path", #endif + [SET_RUNDIR] = "path", }; (void)fprintf(stderr, "Usage: %s --vm=\n", progname); for (const struct option *o = opts; o->name; o++) { @@ -255,12 +257,13 @@ show_memseg(struct vmctx *ctx) } static int __unused -ipc_send_message(const char *vmname, nvlist_t *request, const char *rundir) +ipc_send_message(const char *vmname, nvlist_t *request, const char *rundir, + nvlist_t **replyp) { int err = 0, socket_fd, ret; struct sockaddr_un addr; const char* errmsg; - nvlist_t *reply; + nvlist_t *reply = NULL; socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); if (socket_fd < 0) { @@ -292,22 +295,54 @@ ipc_send_message(const char *vmname, nvlist_t *request, const char *rundir) request = NULL; if (reply == NULL) { perror("nvlist_xfer() failed"); + err = errno; goto done; } if (nvlist_exists_string(reply, "error")) { errmsg = nvlist_get_string(reply, "error"); - fprintf(stderr, "%s: IPC command failed: %s\n", __func__, errmsg); + fprintf(stderr, "%s: IPC command failed: %s\n", + __func__, errmsg); err = -1; + } else if (replyp != NULL) { + *replyp = reply; + reply = NULL; } done: if (request != NULL) nvlist_destroy(request); + if (reply != NULL) + nvlist_destroy(reply); if (socket_fd >= 0) close(socket_fd); return (err); } +static int +vm_pid_request(const char *vmname, const char *rundir, pid_t *pid) +{ + nvlist_t *nvl, *reply; + int error; + + nvl = nvlist_create(0); + nvlist_add_string(nvl, "cmd", "get_vm_pid"); + error = ipc_send_message(vmname, nvl, rundir, &reply); + if (error == -1) + error = EOPNOTSUPP; + else if (error == ENOENT || error == ECONNREFUSED) + error = ESRCH; + if (error == 0) { + if (nvlist_exists_number(reply, "pid")) + *pid = (pid_t)nvlist_get_number(reply, "pid"); + else + error = EPROTO; + nvlist_destroy(reply); + } + if (error != 0) + errno = error; + return (error); +} + #ifdef BHYVE_SNAPSHOT static int open_directory(const char *file) @@ -340,7 +375,7 @@ snapshot_request(const char *vmname, char *file, bool suspend, const char *rundi nvlist_add_bool(nvl, "suspend", suspend); nvlist_move_descriptor(nvl, "fddir", fd); - return (ipc_send_message(vmname, nvl, rundir)); + return (ipc_send_message(vmname, nvl, rundir, NULL)); } #endif @@ -402,11 +437,10 @@ main(int argc, char *argv[]) vm_suspend_opt = (ch == SET_SUSPEND_FILE); break; +#endif case SET_RUNDIR: rundir = optarg; break; - -#endif default: usage(opts); } @@ -536,6 +570,14 @@ main(int argc, char *argv[]) "maxcpus=%hu\n", sockets, cores, threads, maxcpus); } + if (!error && get_vm_pid) { + pid_t pid; + + error = vm_pid_request(vmname, rundir, &pid); + if (!error) + printf("vm pid:\t%d\n", pid); + } + if (!error && run) { struct vm_exit vmexit; cpuset_t cpuset;