]> git.proxmox.com Git - qemu-server.git/blobdiff - qmeventd/qmeventd.c
qmeventd: further improve getting VMID from PID
[qemu-server.git] / qmeventd / qmeventd.c
index a14c3315c76e625cd278598f157f7c3ed5a16677..0130103de0575b795f7fb800ac8024f4f93c688d 100644 (file)
@@ -1,41 +1,21 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
 /*
-
-    Copyright (C) 2018 Proxmox Server Solutions GmbH
-
-    Copyright: qmeventd is under GNU GPL, the GNU General Public License.
-
-    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; version 2 dated June, 1991.
-
-    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., 59 Temple Place - Suite 330, Boston, MA
-    02111-1307, USA.
+    Copyright (C) 2018 - 2021 Proxmox Server Solutions GmbH
 
     Author: Dominik Csapak <d.csapak@proxmox.com>
-
-    qmeventd listens on a given socket, and waits for qemu processes
-    to connect
-
-    it then waits for shutdown events followed by the closing of the socket,
-    it then calls /usr/sbin/qm cleanup with following arguments
-
-    /usr/sbin/qm cleanup VMID <graceful> <guest>
-
-    parameter explanation:
-
-    graceful:
-    1|0 depending if it saw a shutdown event before the socket closed
-
-    guest:
-    1|0 depending if the shutdown was requested from the guest
-
+    Author: Stefan Reiter <s.reiter@proxmox.com>
+
+    Description:
+
+    qmeventd listens on a given socket, and waits for qemu processes to
+    connect. After accepting a connection qmeventd waits for shutdown events
+    followed by the closing of the socket. Once that happens `qm cleanup` will
+    be executed with following three arguments:
+    VMID <graceful> <guest>
+    Where `graceful` can be `1` or `0` depending if shutdown event was observed
+    before the socket got closed. The second parameter `guest` is also boolean
+    `1` or `0` depending if the shutdown was requested from the guest OS
+    (i.e., the "inside").
 */
 
 #ifndef _GNU_SOURCE
 
 #include <errno.h>
 #include <fcntl.h>
+#include <gmodule.h>
 #include <json.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <sys/wait.h>
 #include <unistd.h>
-#include <gmodule.h>
+#include <time.h>
 
 #include "qmeventd.h"
 
+#define DEFAULT_KILL_TIMEOUT 60
+
 static int verbose = 0;
+static int kill_timeout = DEFAULT_KILL_TIMEOUT;
 static int epoll_fd = 0;
 static const char *progname;
 GHashTable *vm_clients; // key=vmid (freed on remove), value=*Client (free manually)
 GSList *forced_cleanups;
-volatile sig_atomic_t alarm_triggered = 0;
+static int needs_cleanup = 0;
 
 /*
  * Helper functions
@@ -76,6 +61,7 @@ usage()
     fprintf(stderr, "Usage: %s [-f] [-v] PATH\n", progname);
     fprintf(stderr, "  -f       run in foreground (default: false)\n");
     fprintf(stderr, "  -v       verbose (default: false)\n");
+    fprintf(stderr, "  -t <s>   kill timeout (default: %ds)\n", DEFAULT_KILL_TIMEOUT);
     fprintf(stderr, "  PATH     use PATH for socket\n");
 }
 
@@ -89,14 +75,13 @@ get_pid_from_fd(int fd)
 }
 
 /*
- * reads the vmid from /proc/<pid>/cmdline
- * after the '-id' argument
+ * parses the vmid from the qemu.slice entry of /proc/<pid>/cgroup
  */
 static unsigned long
 get_vmid_from_pid(pid_t pid)
 {
     char filename[32] = { 0 };
-    int len = snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
+    int len = snprintf(filename, sizeof(filename), "/proc/%d/cgroup", pid);
     if (len < 0) {
        fprintf(stderr, "error during snprintf for %d: %s\n", pid,
                strerror(errno));
@@ -113,41 +98,54 @@ get_vmid_from_pid(pid_t pid)
     }
 
     unsigned long vmid = 0;
-    ssize_t rc = 0;
     char *buf = NULL;
     size_t buflen = 0;
-    while ((rc = getdelim(&buf, &buflen, '\0', fp)) >= 0) {
-       if (!strcmp(buf, "-id")) {
-           break;
+
+    while (getline(&buf, &buflen, fp) >= 0) {
+       char *cgroup_path = strrchr(buf, ':');
+       if (!cgroup_path) {
+           fprintf(stderr, "unexpected cgroup entry %s\n", buf);
+           goto ret;
        }
-    }
+       cgroup_path++;
 
-    if (rc < 0) {
-       goto err;
-    }
+       if (strncmp(cgroup_path, "/qemu.slice/", 12)) {
+           continue;
+       }
+
+       char *vmid_start = strrchr(buf, '/');
+       if (!vmid_start) {
+           fprintf(stderr, "unexpected cgroup entry %s\n", buf);
+           goto ret;
+       }
+       vmid_start++;
 
-    if (getdelim(&buf, &buflen, '\0', fp) >= 0) {
-       if (buf[0] == '-' || buf[0] == '\0') {
-           fprintf(stderr, "invalid vmid %s\n", buf);
+       if (vmid_start[0] == '-' || vmid_start[0] == '\0') {
+           fprintf(stderr, "invalid vmid in cgroup entry %s\n", buf);
            goto ret;
        }
 
        errno = 0;
        char *endptr = NULL;
-       vmid = strtoul(buf, &endptr, 10);
-       if (errno != 0) {
+       vmid = strtoul(vmid_start, &endptr, 10);
+       if (!endptr || strncmp(endptr, ".scope", 6)) {
+           fprintf(stderr, "unexpected cgroup entry %s\n", buf);
            vmid = 0;
-           goto err;
-       } else if (*endptr != '\0') {
-           fprintf(stderr, "invalid vmid %s\n", buf);
+           continue;
+       }
+       if (errno != 0) {
+           fprintf(stderr, "error parsing vmid for %d: %s\n", pid, strerror(errno));
            vmid = 0;
        }
 
        goto ret;
     }
 
-err:
-    fprintf(stderr, "error parsing vmid for %d: %s\n", pid, strerror(errno));
+    if (errno) {
+       fprintf(stderr, "error parsing vmid for %d: %s\n", pid, strerror(errno));
+    } else {
+       fprintf(stderr, "error parsing vmid for %d: no matching qemu.slice cgroup entry\n", pid);
+    }
 
 ret:
     free(buf);
@@ -212,13 +210,11 @@ handle_qmp_event(struct Client *client, struct json_object *obj)
     if (!json_object_object_get_ex(obj, "event", &event)) {
        return;
     }
-    VERBOSE_PRINT("%s: got QMP event: %s\n", client->qemu.vmid,
-                 json_object_get_string(event));
+    VERBOSE_PRINT("%s: got QMP event: %s\n", client->qemu.vmid, json_object_get_string(event));
 
     if (client->state == STATE_TERMINATING) {
        // QEMU sometimes sends a second SHUTDOWN after SIGTERM, ignore
-       VERBOSE_PRINT("%s: event was after termination, ignoring\n",
-                     client->qemu.vmid);
+       VERBOSE_PRINT("%s: event was after termination, ignoring\n", client->qemu.vmid);
        return;
     }
 
@@ -303,8 +299,10 @@ handle_qmp_return(struct Client *client, struct json_object *data, bool error)
            VERBOSE_PRINT("%s: QMP handshake complete\n", client->qemu.vmid);
            break;
 
-       case STATE_IDLE:
+       // we expect an empty return object after sending quit
        case STATE_TERMINATING:
+           break;
+       case STATE_IDLE:
            VERBOSE_PRINT("%s: spurious return value received\n",
                          client->qemu.vmid);
            break;
@@ -329,39 +327,33 @@ handle_vzdump_handshake(struct Client *client, struct json_object *data)
     json_bool has_vmid = data && json_object_object_get_ex(data, "vmid", &vmid_obj);
 
     if (!has_vmid) {
-       VERBOSE_PRINT("pid%d: invalid vzdump handshake: no vmid\n",
-                     client->pid);
+       VERBOSE_PRINT("pid%d: invalid vzdump handshake: no vmid\n", client->pid);
        return;
     }
 
     const char *vmid_str = json_object_get_string(vmid_obj);
 
     if (!vmid_str) {
-       VERBOSE_PRINT("pid%d: invalid vzdump handshake: vmid is not a string\n",
-                     client->pid);
+       VERBOSE_PRINT("pid%d: invalid vzdump handshake: vmid is not a string\n", client->pid);
        return;
     }
 
     int res = snprintf(client->vzdump.vmid, sizeof(client->vzdump.vmid), "%s", vmid_str);
     if (res < 0 || res >= (int)sizeof(client->vzdump.vmid)) {
-       VERBOSE_PRINT("pid%d: invalid vzdump handshake: vmid too long or invalid\n",
-                     client->pid);
+       VERBOSE_PRINT("pid%d: invalid vzdump handshake: vmid too long or invalid\n", client->pid);
        return;
     }
 
-    struct Client *vmc =
-       (struct Client*) g_hash_table_lookup(vm_clients, client->vzdump.vmid);
+    struct Client *vmc = (struct Client*) g_hash_table_lookup(vm_clients, client->vzdump.vmid);
     if (vmc) {
        vmc->qemu.backup = true;
 
        // only mark as VZDUMP once we have set everything up, otherwise 'cleanup'
        // might try to access an invalid value
        client->type = CLIENT_VZDUMP;
-       VERBOSE_PRINT("%s: vzdump backup started\n",
-                     client->vzdump.vmid);
+       VERBOSE_PRINT("%s: vzdump backup started\n", client->vzdump.vmid);
     } else {
-       VERBOSE_PRINT("%s: vzdump requested backup start for unregistered VM\n",
-                     client->vzdump.vmid);
+       VERBOSE_PRINT("%s: vzdump requested backup start for unregistered VM\n", client->vzdump.vmid);
     }
 }
 
@@ -373,6 +365,11 @@ void
 add_new_client(int client_fd)
 {
     struct Client *client = calloc(sizeof(struct Client), 1);
+    if (client == NULL) {
+       fprintf(stderr, "could not add new client - allocation failed!\n");
+       fflush(stderr);
+       return;
+    }
     client->state = STATE_HANDSHAKE;
     client->type = CLIENT_NONE;
     client->fd = client_fd;
@@ -459,14 +456,18 @@ cleanup_client(struct Client *client)
            break;
     }
 
+    if (client->pidfd > 0) {
+       (void)close(client->pidfd);
+    }
+    VERBOSE_PRINT("removing %s from forced cleanups\n", client->qemu.vmid);
+    forced_cleanups = g_slist_remove(forced_cleanups, client);
     free(client);
 }
 
 void
 terminate_client(struct Client *client)
 {
-    VERBOSE_PRINT("%s: terminating client (pid %d)\n",
-                 client->qemu.vmid, client->pid);
+    VERBOSE_PRINT("%s: terminating client (pid %d)\n", client->qemu.vmid, client->pid);
 
     client->state = STATE_TERMINATING;
 
@@ -490,19 +491,22 @@ terminate_client(struct Client *client)
        }
     }
 
-    int err = kill(client->pid, SIGTERM);
-    log_neg(err, "kill");
+    // try to send a 'quit' command first, fallback to SIGTERM of the pid
+    static const char qmp_quit_command[] = "{\"execute\":\"quit\"}\n";
+    VERBOSE_PRINT("%s: sending 'quit' via QMP\n", client->qemu.vmid);
+    if (!must_write(client->fd, qmp_quit_command, sizeof(qmp_quit_command) - 1)) {
+       VERBOSE_PRINT("%s: sending 'SIGTERM' to pid %d\n", client->qemu.vmid, client->pid);
+       int err = kill(client->pid, SIGTERM);
+       log_neg(err, "kill");
+    }
+
+    time_t timeout = time(NULL) + kill_timeout;
 
-    struct CleanupData *data_ptr = malloc(sizeof(struct CleanupData));
-    struct CleanupData data = {
-       .pid = client->pid,
-       .pidfd = pidfd
-    };
-    *data_ptr = data;
-    forced_cleanups = g_slist_prepend(forced_cleanups, (void *)data_ptr);
+    client->pidfd = pidfd;
+    client->timeout = timeout;
 
-    // resets any other alarms, but will fire eventually and cleanup all
-    alarm(5);
+    forced_cleanups = g_slist_prepend(forced_cleanups, (void *)client);
+    needs_cleanup = 1;
 }
 
 void
@@ -559,15 +563,13 @@ handle_client(struct Client *client)
                break;
            case json_tokener_continue:
                if (client->buflen >= sizeof(client->buf)) {
-                   VERBOSE_PRINT("pid%d: msg too large, discarding buffer\n",
-                                 client->pid);
+                   VERBOSE_PRINT("pid%d: msg too large, discarding buffer\n", client->pid);
                    memset(client->buf, 0, sizeof(client->buf));
                    client->buflen = 0;
                } // else we have enough space try again after next read
                break;
            default:
-               VERBOSE_PRINT("pid%d: parse error: %d, discarding buffer\n",
-                             client->pid, jerr);
+               VERBOSE_PRINT("pid%d: parse error: %d, discarding buffer\n", client->pid, jerr);
                memset(client->buf, 0, client->buflen);
                client->buflen = 0;
                break;
@@ -577,58 +579,51 @@ handle_client(struct Client *client)
     json_tokener_free(tok);
 }
 
-
-/*
- * SIGALRM and cleanup handling
- *
- * terminate_client will set an alarm for 5 seconds and add its client's PID to
- * the forced_cleanups list - when the timer expires, we iterate the list and
- * attempt to issue SIGKILL to all processes which haven't yet stopped.
- */
-
 static void
-alarm_handler(__attribute__((unused)) int signum)
+sigkill(void *ptr, void *time_ptr)
 {
-    alarm_triggered = 1;
-}
-
-static void
-sigkill(void *ptr, __attribute__((unused)) void *unused)
-{
-    struct CleanupData data = *((struct CleanupData *)ptr);
+    struct Client *data = ptr;
     int err;
 
-    if (data.pidfd > 0) {
-       err = pidfd_send_signal(data.pidfd, SIGKILL, NULL, 0);
-       (void)close(data.pidfd);
+    if (data->timeout != 0 && data->timeout > *(time_t *)time_ptr) {
+       return;
+    }
+
+    if (data->pidfd > 0) {
+       err = pidfd_send_signal(data->pidfd, SIGKILL, NULL, 0);
+       (void)close(data->pidfd);
+       data->pidfd = -1;
     } else {
-       err = kill(data.pid, SIGKILL);
+       err = kill(data->pid, SIGKILL);
     }
 
     if (err < 0) {
        if (errno != ESRCH) {
            fprintf(stderr, "SIGKILL cleanup of pid '%d' failed - %s\n",
-                   data.pid, strerror(errno));
+                   data->pid, strerror(errno));
        }
     } else {
        fprintf(stderr, "cleanup failed, terminating pid '%d' with SIGKILL\n",
-               data.pid);
+               data->pid);
     }
+
+    data->timeout = 0;
+
+    // remove ourselves from the list
+    forced_cleanups = g_slist_remove(forced_cleanups, ptr);
 }
 
 static void
 handle_forced_cleanup()
 {
-    if (alarm_triggered) {
+    if (g_slist_length(forced_cleanups) > 0) {
        VERBOSE_PRINT("clearing forced cleanup backlog\n");
-       alarm_triggered = 0;
-       g_slist_foreach(forced_cleanups, sigkill, NULL);
-       g_slist_free_full(forced_cleanups, free);
-       forced_cleanups = NULL;
+       time_t cur_time = time(NULL);
+       g_slist_foreach(forced_cleanups, sigkill, &cur_time);
     }
+    needs_cleanup = g_slist_length(forced_cleanups) > 0;
 }
 
-
 int
 main(int argc, char *argv[])
 {
@@ -637,7 +632,7 @@ main(int argc, char *argv[])
     char *socket_path = NULL;
     progname = argv[0];
 
-    while ((opt = getopt(argc, argv, "hfv")) != -1) {
+    while ((opt = getopt(argc, argv, "hfvt:")) != -1) {
        switch (opt) {
            case 'f':
                daemonize = 0;
@@ -645,6 +640,15 @@ main(int argc, char *argv[])
            case 'v':
                verbose = 1;
                break;
+           case 't':
+               errno = 0;
+               char *endptr = NULL;
+               kill_timeout = strtoul(optarg, &endptr, 10);
+               if (errno != 0 || *endptr != '\0' || kill_timeout == 0) {
+                   usage();
+                   exit(EXIT_FAILURE);
+               }
+               break;
            case 'h':
                usage();
                exit(EXIT_SUCCESS);
@@ -661,7 +665,6 @@ main(int argc, char *argv[])
     }
 
     signal(SIGCHLD, SIG_IGN);
-    signal(SIGALRM, alarm_handler);
 
     socket_path = argv[optind];
 
@@ -695,9 +698,8 @@ main(int argc, char *argv[])
     int nevents;
 
     for(;;) {
-       nevents = epoll_wait(epoll_fd, events, 1, -1);
+       nevents = epoll_wait(epoll_fd, events, 1, needs_cleanup ? 10*1000 : -1);
        if (nevents < 0 && errno == EINTR) {
-           handle_forced_cleanup();
            continue;
        }
        bail_neg(nevents, "epoll_wait");
@@ -705,8 +707,7 @@ main(int argc, char *argv[])
        for (int n = 0; n < nevents; n++) {
            if (events[n].data.fd == sock) {
 
-               int conn_sock = accept4(sock, NULL, NULL,
-                                       SOCK_NONBLOCK | SOCK_CLOEXEC);
+               int conn_sock = accept4(sock, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
                log_neg(conn_sock, "accept");
                if (conn_sock > -1) {
                    add_new_client(conn_sock);
@@ -715,7 +716,6 @@ main(int argc, char *argv[])
                handle_client((struct Client *)events[n].data.ptr);
            }
        }
-
        handle_forced_cleanup();
     }
 }