From 85846d3ec971e4e7276fabc0a3e83bff67cb3972 Mon Sep 17 00:00:00 2001 From: mlarkin Date: Tue, 9 Jul 2024 15:51:11 +0000 Subject: [PATCH] vmctl(8): set exit code for vmctl stat -r set exit code to 1 if no running VMs are detected with vmctl stat -r. ok dv --- usr.sbin/vmctl/vmctl.c | 27 +++++++++++++++++++++------ usr.sbin/vmctl/vmctl.h | 4 ++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/usr.sbin/vmctl/vmctl.c b/usr.sbin/vmctl/vmctl.c index e08f032c04e..50135adc4e8 100644 --- a/usr.sbin/vmctl/vmctl.c +++ b/usr.sbin/vmctl/vmctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vmctl.c,v 1.90 2024/05/02 15:46:10 mlarkin Exp $ */ +/* $OpenBSD: vmctl.c,v 1.91 2024/07/09 15:51:11 mlarkin Exp $ */ /* * Copyright (c) 2014 Mike Larkin @@ -691,6 +691,7 @@ check_info_id(const char *name, uint32_t id) * 0 : Message successfully processed * EINVAL: Invalid or unexpected response from vmd * ENOMEM: memory allocation failure + * ENOENT: no entries */ int add_info(struct imsg *imsg, int *ret) @@ -698,6 +699,8 @@ add_info(struct imsg *imsg, int *ret) static size_t ct = 0; static struct vmop_info_result *vir = NULL; + *ret = 0; + if (imsg->hdr.type == IMSG_VMDOP_GET_INFO_VM_DATA) { vir = reallocarray(vir, ct + 1, sizeof(struct vmop_info_result)); @@ -707,7 +710,6 @@ add_info(struct imsg *imsg, int *ret) } memcpy(&vir[ct], imsg->data, sizeof(struct vmop_info_result)); ct++; - *ret = 0; return (0); } else if (imsg->hdr.type == IMSG_VMDOP_GET_INFO_VM_END_DATA) { switch (info_action) { @@ -718,11 +720,10 @@ add_info(struct imsg *imsg, int *ret) terminate_all(vir, ct, info_flags); break; default: - print_vm_info(vir, ct); + *ret = print_vm_info(vir, ct); break; } free(vir); - *ret = 0; return (1); } else { *ret = EINVAL; @@ -766,8 +767,12 @@ vm_state(unsigned int mask) * Parameters * list: the vm information (consolidated) returned from vmd via imsg * ct : the size (number of elements in 'list') of the result + * + * Return values: + * 0: no error + * ENOENT: no entries printed */ -void +int print_vm_info(struct vmop_info_result *list, size_t ct) { struct vm_info_result *vir; @@ -778,9 +783,11 @@ print_vm_info(struct vmop_info_result *list, size_t ct) char maxmem[FMT_SCALED_STRSIZE]; char user[16], group[16]; const char *name; - int running; + int running, found_running; extern int stat_rflag; + found_running = 0; + printf("%5s %5s %5s %7s %7s %7s %12s %8s %s\n", "ID", "PID", "VCPUS", "MAXMEM", "CURMEM", "TTY", "OWNER", "STATE", "NAME"); @@ -790,6 +797,9 @@ print_vm_info(struct vmop_info_result *list, size_t ct) running = (vir->vir_creator_pid != 0 && vir->vir_id != 0); if (!running && stat_rflag) continue; + + found_running++; + if (check_info_id(vir->vir_name, vir->vir_id)) { /* get user name */ name = user_from_uid(vmi->vir_uid, 1); @@ -841,6 +851,11 @@ print_vm_info(struct vmop_info_result *list, size_t ct) } } } + + if (found_running) + return (0); + else + return (ENOENT); } /* diff --git a/usr.sbin/vmctl/vmctl.h b/usr.sbin/vmctl/vmctl.h index 94a05168ee8..897b1d2b9a5 100644 --- a/usr.sbin/vmctl/vmctl.h +++ b/usr.sbin/vmctl/vmctl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: vmctl.h,v 1.38 2024/05/18 06:45:00 jsg Exp $ */ +/* $OpenBSD: vmctl.h,v 1.39 2024/07/09 15:51:11 mlarkin Exp $ */ /* * Copyright (c) 2015 Reyk Floeter @@ -108,7 +108,7 @@ void get_info_vm(uint32_t, const char *, enum actions, unsigned int); int add_info(struct imsg *, int *); const char *vm_state(unsigned int); -void print_vm_info(struct vmop_info_result *, size_t); +int print_vm_info(struct vmop_info_result *, size_t); void terminate_all(struct vmop_info_result *, size_t, unsigned int); __dead void vm_console(struct vmop_info_result *, size_t); -- 2.20.1