2013-07-18 09:54:21 +00:00
|
|
|
/*
|
|
|
|
* virsystemd.c: helpers for using systemd APIs
|
|
|
|
*
|
2014-04-14 08:50:42 +00:00
|
|
|
* Copyright (C) 2013, 2014 Red Hat, Inc.
|
2013-07-18 09:54:21 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2018-12-13 14:53:50 +00:00
|
|
|
#define LIBVIRT_VIRSYSTEMDPRIV_H_ALLOW
|
2016-06-24 15:41:09 +00:00
|
|
|
#include "virsystemdpriv.h"
|
|
|
|
|
2013-07-18 09:54:21 +00:00
|
|
|
#include "virsystemd.h"
|
2016-02-01 15:50:54 +00:00
|
|
|
#include "virbuffer.h"
|
2020-09-09 14:42:42 +00:00
|
|
|
#include "virgdbus.h"
|
2013-07-18 09:54:21 +00:00
|
|
|
#include "virstring.h"
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virutil.h"
|
2013-07-22 15:33:37 +00:00
|
|
|
#include "virlog.h"
|
2013-07-26 14:18:55 +00:00
|
|
|
#include "virerror.h"
|
2016-06-06 14:03:27 +00:00
|
|
|
#include "virfile.h"
|
2019-06-25 12:03:56 +00:00
|
|
|
#include "virhash.h"
|
|
|
|
#include "virsocketaddr.h"
|
2013-07-18 09:54:21 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_SYSTEMD
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.systemd");
|
2013-07-26 14:18:55 +00:00
|
|
|
|
2016-07-20 13:46:56 +00:00
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
# define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
2019-06-25 12:03:56 +00:00
|
|
|
struct _virSystemdActivation {
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *fds;
|
2019-06-25 12:03:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _virSystemdActivationEntry virSystemdActivationEntry;
|
|
|
|
struct _virSystemdActivationEntry {
|
|
|
|
int *fds;
|
|
|
|
size_t nfds;
|
|
|
|
};
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static void virSystemdEscapeName(virBuffer *buf,
|
2016-02-01 15:28:29 +00:00
|
|
|
const char *name)
|
2013-07-26 14:18:55 +00:00
|
|
|
{
|
|
|
|
static const char hextable[16] = "0123456789abcdef";
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define ESCAPE(c) \
|
|
|
|
do { \
|
|
|
|
virBufferAddChar(buf, '\\'); \
|
|
|
|
virBufferAddChar(buf, 'x'); \
|
|
|
|
virBufferAddChar(buf, hextable[(c >> 4) & 15]); \
|
|
|
|
virBufferAddChar(buf, hextable[c & 15]); \
|
2013-07-26 14:18:55 +00:00
|
|
|
} while (0)
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define VALID_CHARS \
|
|
|
|
"0123456789" \
|
|
|
|
"abcdefghijklmnopqrstuvwxyz" \
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
2013-07-26 14:18:55 +00:00
|
|
|
":-_.\\"
|
|
|
|
|
2016-02-01 15:28:29 +00:00
|
|
|
if (*name == '.') {
|
2013-07-26 14:18:55 +00:00
|
|
|
ESCAPE(*name);
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*name) {
|
|
|
|
if (*name == '/')
|
|
|
|
virBufferAddChar(buf, '-');
|
2016-02-01 15:28:29 +00:00
|
|
|
else if (*name == '-' ||
|
2013-07-26 14:18:55 +00:00
|
|
|
*name == '\\' ||
|
|
|
|
!strchr(VALID_CHARS, *name))
|
|
|
|
ESCAPE(*name);
|
|
|
|
else
|
|
|
|
virBufferAddChar(buf, *name);
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ESCAPE
|
|
|
|
#undef VALID_CHARS
|
|
|
|
}
|
|
|
|
|
|
|
|
char *virSystemdMakeScopeName(const char *name,
|
2016-02-01 15:50:54 +00:00
|
|
|
const char *drivername,
|
|
|
|
bool legacy_behaviour)
|
2013-07-26 14:18:55 +00:00
|
|
|
{
|
2020-07-03 02:30:20 +00:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2013-07-26 14:18:55 +00:00
|
|
|
|
2015-07-16 13:35:05 +00:00
|
|
|
virBufferAddLit(&buf, "machine-");
|
2016-02-01 15:50:54 +00:00
|
|
|
if (legacy_behaviour) {
|
|
|
|
virSystemdEscapeName(&buf, drivername);
|
|
|
|
virBufferAddLit(&buf, "\\x2d");
|
|
|
|
}
|
2016-02-01 15:28:29 +00:00
|
|
|
virSystemdEscapeName(&buf, name);
|
2013-07-26 14:18:55 +00:00
|
|
|
virBufferAddLit(&buf, ".scope");
|
|
|
|
|
|
|
|
return virBufferContentAndReset(&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *virSystemdMakeSliceName(const char *partition)
|
|
|
|
{
|
2020-07-03 02:30:20 +00:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2013-07-26 14:18:55 +00:00
|
|
|
|
|
|
|
if (*partition == '/')
|
|
|
|
partition++;
|
|
|
|
|
2016-02-01 15:28:29 +00:00
|
|
|
virSystemdEscapeName(&buf, partition);
|
2013-07-26 14:18:55 +00:00
|
|
|
virBufferAddLit(&buf, ".slice");
|
|
|
|
|
|
|
|
return virBufferContentAndReset(&buf);
|
|
|
|
}
|
|
|
|
|
2016-06-24 15:41:09 +00:00
|
|
|
static int virSystemdHasMachinedCachedValue = -1;
|
2019-08-13 14:34:30 +00:00
|
|
|
static int virSystemdHasLogindCachedValue = -1;
|
2016-06-24 15:41:09 +00:00
|
|
|
|
|
|
|
/* Reset the cache from tests for testing the underlying dbus calls
|
|
|
|
* as well */
|
|
|
|
void virSystemdHasMachinedResetCachedValue(void)
|
|
|
|
{
|
|
|
|
virSystemdHasMachinedCachedValue = -1;
|
|
|
|
}
|
|
|
|
|
2019-08-13 14:34:30 +00:00
|
|
|
void virSystemdHasLogindResetCachedValue(void)
|
|
|
|
{
|
|
|
|
virSystemdHasLogindCachedValue = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-24 15:38:33 +00:00
|
|
|
/* -2 = machine1 is not supported on this machine
|
|
|
|
* -1 = error
|
|
|
|
* 0 = machine1 is available
|
|
|
|
*/
|
2020-10-27 13:14:08 +00:00
|
|
|
int
|
2016-06-24 15:38:33 +00:00
|
|
|
virSystemdHasMachined(void)
|
|
|
|
{
|
|
|
|
int ret;
|
2016-06-24 15:41:09 +00:00
|
|
|
int val;
|
|
|
|
|
2020-01-31 16:07:00 +00:00
|
|
|
val = g_atomic_int_get(&virSystemdHasMachinedCachedValue);
|
2016-06-24 15:41:09 +00:00
|
|
|
if (val != -1)
|
|
|
|
return val;
|
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if ((ret = virGDBusIsServiceEnabled("org.freedesktop.machine1")) < 0) {
|
2016-06-24 15:41:09 +00:00
|
|
|
if (ret == -2)
|
2020-01-31 16:08:35 +00:00
|
|
|
g_atomic_int_set(&virSystemdHasMachinedCachedValue, -2);
|
2016-06-24 15:38:33 +00:00
|
|
|
return ret;
|
2016-06-24 15:41:09 +00:00
|
|
|
}
|
2016-06-24 15:38:33 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if ((ret = virGDBusIsServiceRegistered("org.freedesktop.systemd1")) == -1)
|
2016-06-24 15:41:09 +00:00
|
|
|
return ret;
|
2020-01-31 16:08:35 +00:00
|
|
|
g_atomic_int_set(&virSystemdHasMachinedCachedValue, ret);
|
2016-06-24 15:41:09 +00:00
|
|
|
return ret;
|
2016-06-24 15:38:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 14:43:27 +00:00
|
|
|
int
|
2019-08-13 14:23:39 +00:00
|
|
|
virSystemdHasLogind(void)
|
|
|
|
{
|
|
|
|
int ret;
|
2019-08-13 14:34:30 +00:00
|
|
|
int val;
|
|
|
|
|
2020-01-31 16:07:00 +00:00
|
|
|
val = g_atomic_int_get(&virSystemdHasLogindCachedValue);
|
2019-08-13 14:34:30 +00:00
|
|
|
if (val != -1)
|
|
|
|
return val;
|
2019-08-13 14:23:39 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
ret = virGDBusIsServiceEnabled("org.freedesktop.login1");
|
2019-08-13 14:34:30 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (ret == -2)
|
2020-01-31 16:08:35 +00:00
|
|
|
g_atomic_int_set(&virSystemdHasLogindCachedValue, -2);
|
2019-08-13 14:23:39 +00:00
|
|
|
return ret;
|
2019-08-13 14:34:30 +00:00
|
|
|
}
|
2019-08-13 14:23:39 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if ((ret = virGDBusIsServiceRegistered("org.freedesktop.login1")) == -1)
|
2019-08-13 14:23:39 +00:00
|
|
|
return ret;
|
|
|
|
|
2020-01-31 16:08:35 +00:00
|
|
|
g_atomic_int_set(&virSystemdHasLogindCachedValue, ret);
|
2019-08-13 14:23:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-02 18:45:55 +00:00
|
|
|
|
2020-10-30 11:06:56 +00:00
|
|
|
/**
|
|
|
|
* virSystemdGetMachineByPID:
|
|
|
|
* @conn: dbus connection
|
|
|
|
* @pid: pid of running VM
|
|
|
|
*
|
|
|
|
* Returns dbus object path to VM registered with machined.
|
|
|
|
* On error returns NULL.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
virSystemdGetMachineByPID(GDBusConnection *conn,
|
|
|
|
pid_t pid)
|
2016-02-02 18:45:55 +00:00
|
|
|
{
|
2020-09-09 14:42:42 +00:00
|
|
|
g_autoptr(GVariant) message = NULL;
|
|
|
|
g_autoptr(GVariant) reply = NULL;
|
2020-10-30 11:06:56 +00:00
|
|
|
char *object = NULL;
|
2016-02-02 18:45:55 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
message = g_variant_new("(u)", pid);
|
2016-02-02 18:45:55 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if (virGDBusCallMethod(conn,
|
|
|
|
&reply,
|
2020-09-21 13:42:00 +00:00
|
|
|
G_VARIANT_TYPE("(o)"),
|
2020-09-09 14:42:42 +00:00
|
|
|
NULL,
|
|
|
|
"org.freedesktop.machine1",
|
|
|
|
"/org/freedesktop/machine1",
|
|
|
|
"org.freedesktop.machine1.Manager",
|
|
|
|
"GetMachineByPID",
|
|
|
|
message) < 0)
|
|
|
|
return NULL;
|
2016-02-02 18:45:55 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
g_variant_get(reply, "(o)", &object);
|
|
|
|
|
2016-10-06 14:54:41 +00:00
|
|
|
VIR_DEBUG("Domain with pid %lld has object path '%s'",
|
|
|
|
(long long) pid, object);
|
2016-02-02 18:45:55 +00:00
|
|
|
|
2020-10-30 11:06:56 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
virSystemdGetMachineNameByPID(pid_t pid)
|
|
|
|
{
|
|
|
|
GDBusConnection *conn;
|
|
|
|
g_autoptr(GVariant) message = NULL;
|
|
|
|
g_autoptr(GVariant) reply = NULL;
|
|
|
|
g_autoptr(GVariant) gvar = NULL;
|
|
|
|
g_autofree char *object = NULL;
|
|
|
|
char *name = NULL;
|
|
|
|
|
|
|
|
if (virSystemdHasMachined() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(conn = virGDBusGetSystemBus()))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
object = virSystemdGetMachineByPID(conn, pid);
|
|
|
|
if (!object)
|
|
|
|
return NULL;
|
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
message = g_variant_new("(ss)",
|
|
|
|
"org.freedesktop.machine1.Machine", "Name");
|
|
|
|
|
|
|
|
if (virGDBusCallMethod(conn,
|
|
|
|
&reply,
|
2020-09-21 13:42:00 +00:00
|
|
|
G_VARIANT_TYPE("(v)"),
|
2020-09-09 14:42:42 +00:00
|
|
|
NULL,
|
|
|
|
"org.freedesktop.machine1",
|
|
|
|
object,
|
|
|
|
"org.freedesktop.DBus.Properties",
|
|
|
|
"Get",
|
|
|
|
message) < 0)
|
|
|
|
return NULL;
|
2016-02-02 18:45:55 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
g_variant_get(reply, "(v)", &gvar);
|
|
|
|
g_variant_get(gvar, "s", &name);
|
2016-02-02 18:45:55 +00:00
|
|
|
|
2016-10-06 14:54:41 +00:00
|
|
|
VIR_DEBUG("Domain with pid %lld has machine name '%s'",
|
|
|
|
(long long) pid, name);
|
2016-02-02 18:45:55 +00:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-27 13:15:03 +00:00
|
|
|
/**
|
|
|
|
* virSystemdGetMachineUnitByPID:
|
|
|
|
* @pid: pid of running VM
|
|
|
|
*
|
|
|
|
* Returns systemd Unit name of a running VM registered with machined.
|
|
|
|
* On error returns NULL.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virSystemdGetMachineUnitByPID(pid_t pid)
|
|
|
|
{
|
|
|
|
GDBusConnection *conn;
|
|
|
|
g_autoptr(GVariant) message = NULL;
|
|
|
|
g_autoptr(GVariant) reply = NULL;
|
|
|
|
g_autoptr(GVariant) gvar = NULL;
|
|
|
|
g_autofree char *object = NULL;
|
|
|
|
char *unit = NULL;
|
|
|
|
|
|
|
|
if (virSystemdHasMachined() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(conn = virGDBusGetSystemBus()))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
object = virSystemdGetMachineByPID(conn, pid);
|
|
|
|
if (!object)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
message = g_variant_new("(ss)",
|
|
|
|
"org.freedesktop.machine1.Machine", "Unit");
|
|
|
|
|
|
|
|
if (virGDBusCallMethod(conn,
|
|
|
|
&reply,
|
|
|
|
G_VARIANT_TYPE("(v)"),
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.machine1",
|
|
|
|
object,
|
|
|
|
"org.freedesktop.DBus.Properties",
|
|
|
|
"Get",
|
|
|
|
message) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
g_variant_get(reply, "(v)", &gvar);
|
|
|
|
g_variant_get(gvar, "s", &unit);
|
|
|
|
|
|
|
|
VIR_DEBUG("Domain with pid %lld has unit name '%s'",
|
|
|
|
(long long) pid, unit);
|
|
|
|
|
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 09:54:21 +00:00
|
|
|
/**
|
|
|
|
* virSystemdCreateMachine:
|
|
|
|
* @name: driver unique name of the machine
|
|
|
|
* @drivername: name of the virt driver
|
|
|
|
* @privileged: whether driver is running privileged or per user
|
|
|
|
* @uuid: globally unique UUID of the machine
|
|
|
|
* @rootdir: root directory of machine filesystem
|
|
|
|
* @pidleader: PID of the leader process
|
2014-11-11 17:38:43 +00:00
|
|
|
* @iscontainer: true if a container, false if a VM
|
|
|
|
* @nnicindexes: number of network interface indexes in list
|
|
|
|
* @nicindexes: list of network interface indexes
|
|
|
|
* @partition: name of the slice to place the machine in
|
2013-07-22 15:33:37 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on fatal error, or -2 if systemd-machine is not available
|
2013-07-18 09:54:21 +00:00
|
|
|
*/
|
|
|
|
int virSystemdCreateMachine(const char *name,
|
|
|
|
const char *drivername,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
const char *rootdir,
|
|
|
|
pid_t pidleader,
|
|
|
|
bool iscontainer,
|
2014-11-11 17:38:43 +00:00
|
|
|
size_t nnicindexes,
|
|
|
|
int *nicindexes,
|
2019-05-22 23:12:14 +00:00
|
|
|
const char *partition,
|
|
|
|
unsigned int maxthreads)
|
2013-07-18 09:54:21 +00:00
|
|
|
{
|
2020-09-09 14:42:42 +00:00
|
|
|
int rc;
|
|
|
|
GDBusConnection *conn;
|
|
|
|
GVariant *guuid;
|
|
|
|
GVariant *gnicindexes;
|
|
|
|
GVariant *gprops;
|
|
|
|
GVariant *message;
|
|
|
|
g_autofree char *creatorname = NULL;
|
|
|
|
g_autofree char *slicename = NULL;
|
|
|
|
g_autofree char *scopename = NULL;
|
2014-11-11 17:38:43 +00:00
|
|
|
static int hasCreateWithNetwork = 1;
|
2013-07-18 09:54:21 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if ((rc = virSystemdHasMachined()) < 0)
|
|
|
|
return rc;
|
2014-02-27 20:21:57 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if (!(conn = virGDBusGetSystemBus()))
|
2013-10-11 16:50:43 +00:00
|
|
|
return -1;
|
2013-07-18 09:54:21 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
creatorname = g_strdup_printf("libvirt-%s", drivername);
|
2013-07-18 09:54:21 +00:00
|
|
|
|
|
|
|
if (partition) {
|
2013-07-26 14:18:55 +00:00
|
|
|
if (!(slicename = virSystemdMakeSliceName(partition)))
|
2020-09-09 14:42:42 +00:00
|
|
|
return -1;
|
2013-07-18 09:54:21 +00:00
|
|
|
} else {
|
2019-10-20 11:49:46 +00:00
|
|
|
slicename = g_strdup("");
|
2013-07-18 09:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-11-11 17:38:43 +00:00
|
|
|
* The systemd DBus APIs we're invoking have the
|
|
|
|
* following signature(s)
|
|
|
|
*
|
|
|
|
* CreateMachineWithNetwork(in s name,
|
|
|
|
* in ay id,
|
|
|
|
* in s service,
|
|
|
|
* in s class,
|
|
|
|
* in u leader,
|
|
|
|
* in s root_directory,
|
|
|
|
* in ai nicindexes
|
|
|
|
* in a(sv) scope_properties,
|
|
|
|
* out o path);
|
2013-07-18 09:54:21 +00:00
|
|
|
*
|
|
|
|
* CreateMachine(in s name,
|
|
|
|
* in ay id,
|
|
|
|
* in s service,
|
|
|
|
* in s class,
|
|
|
|
* in u leader,
|
|
|
|
* in s root_directory,
|
|
|
|
* in a(sv) scope_properties,
|
|
|
|
* out o path);
|
|
|
|
*
|
|
|
|
* @name a host unique name for the machine. shows up
|
|
|
|
* in 'ps' listing & similar
|
|
|
|
*
|
|
|
|
* @id: a UUID of the machine, ideally matching /etc/machine-id
|
|
|
|
* for containers
|
|
|
|
*
|
|
|
|
* @service: identifier of the client ie "libvirt-lxc"
|
|
|
|
*
|
|
|
|
* @class: either the string "container" or "vm" depending
|
|
|
|
* on the type of machine
|
|
|
|
*
|
|
|
|
* @leader: main PID of the machine, either the host emulator
|
|
|
|
* process, or the 'init' PID of the container
|
|
|
|
*
|
|
|
|
* @root_directory: the root directory of the container, if
|
|
|
|
* this is known & visible in the host filesystem, or empty string
|
|
|
|
*
|
2014-11-11 17:38:43 +00:00
|
|
|
* @nicindexes: list of network interface indexes for the
|
|
|
|
* host end of the VETH device pairs.
|
|
|
|
*
|
2013-07-18 09:54:21 +00:00
|
|
|
* @scope_properties:an array (not a dict!) of properties that are
|
|
|
|
* passed on to PID 1 when creating a scope unit for your machine.
|
|
|
|
* Will allow initial settings for the cgroup & similar.
|
|
|
|
*
|
|
|
|
* @path: a bus path returned for the machine object created, to
|
|
|
|
* allow further API calls to be made against the object.
|
2014-11-11 17:38:43 +00:00
|
|
|
*
|
2013-07-18 09:54:21 +00:00
|
|
|
*/
|
|
|
|
|
2013-07-22 15:33:37 +00:00
|
|
|
VIR_DEBUG("Attempting to create machine via systemd");
|
2020-01-31 16:07:00 +00:00
|
|
|
if (g_atomic_int_get(&hasCreateWithNetwork)) {
|
2020-09-09 14:42:42 +00:00
|
|
|
g_autoptr(virError) error = NULL;
|
|
|
|
|
2020-10-05 17:09:00 +00:00
|
|
|
error = g_new0(virError, 1);
|
2020-09-09 14:42:42 +00:00
|
|
|
|
|
|
|
guuid = g_variant_new_fixed_array(G_VARIANT_TYPE("y"),
|
|
|
|
uuid, 16, sizeof(unsigned char));
|
|
|
|
gnicindexes = g_variant_new_fixed_array(G_VARIANT_TYPE("i"),
|
|
|
|
nicindexes, nnicindexes, sizeof(int));
|
|
|
|
gprops = g_variant_new_parsed("[('Slice', <%s>),"
|
|
|
|
" ('After', <['libvirtd.service']>),"
|
|
|
|
" ('Before', <['virt-guest-shutdown.target']>)]",
|
|
|
|
slicename);
|
|
|
|
message = g_variant_new("(s@ayssus@ai@a(sv))",
|
|
|
|
name,
|
|
|
|
guuid,
|
|
|
|
creatorname,
|
|
|
|
iscontainer ? "container" : "vm",
|
|
|
|
(unsigned int)pidleader,
|
|
|
|
NULLSTR_EMPTY(rootdir),
|
|
|
|
gnicindexes,
|
|
|
|
gprops);
|
|
|
|
|
|
|
|
rc = virGDBusCallMethod(conn,
|
2020-09-21 13:42:00 +00:00
|
|
|
NULL,
|
2020-09-09 14:42:42 +00:00
|
|
|
NULL,
|
|
|
|
error,
|
|
|
|
"org.freedesktop.machine1",
|
|
|
|
"/org/freedesktop/machine1",
|
|
|
|
"org.freedesktop.machine1.Manager",
|
|
|
|
"CreateMachineWithNetwork",
|
|
|
|
message);
|
|
|
|
|
|
|
|
g_variant_unref(message);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (error->level == VIR_ERR_ERROR) {
|
|
|
|
if (virGDBusErrorIsUnknownMethod(error)) {
|
2014-11-11 17:38:43 +00:00
|
|
|
VIR_INFO("CreateMachineWithNetwork isn't supported, switching "
|
|
|
|
"to legacy CreateMachine method for systemd-machined");
|
2020-09-09 14:42:42 +00:00
|
|
|
virResetError(error);
|
2020-01-31 16:08:35 +00:00
|
|
|
g_atomic_int_set(&hasCreateWithNetwork, 0);
|
2014-11-11 17:38:43 +00:00
|
|
|
/* Could re-structure without Using goto, but this
|
|
|
|
* avoids another atomic read which would trigger
|
|
|
|
* another memory barrier */
|
|
|
|
goto fallback;
|
|
|
|
}
|
2020-09-09 14:42:42 +00:00
|
|
|
virReportErrorObject(error);
|
|
|
|
virResetError(error);
|
|
|
|
return -1;
|
2014-11-11 17:38:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fallback:
|
2020-09-09 14:42:42 +00:00
|
|
|
guuid = g_variant_new_fixed_array(G_VARIANT_TYPE("y"),
|
|
|
|
uuid, 16, sizeof(unsigned char));
|
|
|
|
gprops = g_variant_new_parsed("[('Slice', <%s>),"
|
|
|
|
" ('After', <['libvirtd.service']>),"
|
|
|
|
" ('Before', <['virt-guest-shutdown.target']>)]",
|
|
|
|
slicename);
|
|
|
|
message = g_variant_new("(s@ayssus@a(sv))",
|
|
|
|
name,
|
|
|
|
guuid,
|
|
|
|
creatorname,
|
|
|
|
iscontainer ? "container" : "vm",
|
|
|
|
(unsigned int)pidleader,
|
|
|
|
NULLSTR_EMPTY(rootdir),
|
|
|
|
gprops);
|
|
|
|
|
|
|
|
rc = virGDBusCallMethod(conn,
|
2020-09-21 13:42:00 +00:00
|
|
|
NULL,
|
2020-09-09 14:42:42 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.machine1",
|
|
|
|
"/org/freedesktop/machine1",
|
|
|
|
"org.freedesktop.machine1.Manager",
|
|
|
|
"CreateMachine",
|
|
|
|
message);
|
|
|
|
|
|
|
|
g_variant_unref(message);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return -1;
|
2014-11-11 17:38:43 +00:00
|
|
|
}
|
2013-07-18 09:54:21 +00:00
|
|
|
|
2019-05-22 23:12:14 +00:00
|
|
|
if (maxthreads > 0) {
|
2021-03-10 13:33:06 +00:00
|
|
|
uint64_t max = maxthreads;
|
|
|
|
|
2019-05-22 23:12:14 +00:00
|
|
|
if (!(scopename = virSystemdMakeScopeName(name, drivername, false)))
|
2020-09-09 14:42:42 +00:00
|
|
|
return -1;
|
2019-05-22 23:12:14 +00:00
|
|
|
|
2021-03-10 13:33:06 +00:00
|
|
|
gprops = g_variant_new_parsed("[('TasksMax', <%t>)]", max);
|
2013-07-18 09:54:21 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
message = g_variant_new("(sb@a(sv))",
|
|
|
|
scopename,
|
|
|
|
true,
|
|
|
|
gprops);
|
|
|
|
|
|
|
|
rc = virGDBusCallMethod(conn,
|
2020-09-21 13:42:00 +00:00
|
|
|
NULL,
|
2020-09-09 14:42:42 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
|
|
|
"SetUnitProperties",
|
|
|
|
message);
|
|
|
|
|
|
|
|
g_variant_unref(message);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-07-18 09:54:21 +00:00
|
|
|
}
|
2013-09-30 14:46:29 +00:00
|
|
|
|
2016-02-01 15:50:54 +00:00
|
|
|
int virSystemdTerminateMachine(const char *name)
|
2013-09-30 14:46:29 +00:00
|
|
|
{
|
2020-09-09 14:42:42 +00:00
|
|
|
int rc;
|
|
|
|
GDBusConnection *conn;
|
|
|
|
g_autoptr(GVariant) message = NULL;
|
|
|
|
g_autoptr(virError) error = NULL;
|
2015-01-16 11:26:39 +00:00
|
|
|
|
2016-02-01 15:50:54 +00:00
|
|
|
if (!name)
|
|
|
|
return 0;
|
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if ((rc = virSystemdHasMachined()) < 0)
|
|
|
|
return rc;
|
2015-01-16 11:26:39 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if (!(conn = virGDBusGetSystemBus()))
|
|
|
|
return -1;
|
2014-02-27 20:21:57 +00:00
|
|
|
|
2020-10-05 17:09:00 +00:00
|
|
|
error = g_new0(virError, 1);
|
2013-09-30 14:46:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The systemd DBus API we're invoking has the
|
|
|
|
* following signature
|
|
|
|
*
|
|
|
|
* TerminateMachine(in s name);
|
|
|
|
*
|
|
|
|
* @name a host unique name for the machine. shows up
|
|
|
|
* in 'ps' listing & similar
|
|
|
|
*/
|
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
message = g_variant_new("(s)", name);
|
|
|
|
|
2013-09-30 14:46:29 +00:00
|
|
|
VIR_DEBUG("Attempting to terminate machine via systemd");
|
2020-09-09 14:42:42 +00:00
|
|
|
if (virGDBusCallMethod(conn,
|
2020-09-21 13:42:00 +00:00
|
|
|
NULL,
|
2020-09-09 14:42:42 +00:00
|
|
|
NULL,
|
|
|
|
error,
|
|
|
|
"org.freedesktop.machine1",
|
|
|
|
"/org/freedesktop/machine1",
|
|
|
|
"org.freedesktop.machine1.Manager",
|
|
|
|
"TerminateMachine",
|
|
|
|
message) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (error->level == VIR_ERR_ERROR &&
|
2015-10-20 16:15:12 +00:00
|
|
|
STRNEQ_NULLABLE("org.freedesktop.machine1.NoSuchMachine",
|
2020-09-09 14:42:42 +00:00
|
|
|
error->str1)) {
|
|
|
|
virReportErrorObject(error);
|
|
|
|
return -1;
|
2015-01-16 11:26:39 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
return 0;
|
2013-09-30 14:46:29 +00:00
|
|
|
}
|
2014-02-21 12:06:42 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
virSystemdNotifyStartup(void)
|
|
|
|
{
|
2020-01-14 17:38:59 +00:00
|
|
|
#ifndef WIN32
|
2016-06-06 14:03:27 +00:00
|
|
|
const char *path;
|
|
|
|
const char *msg = "READY=1";
|
|
|
|
int fd;
|
|
|
|
struct sockaddr_un un = {
|
|
|
|
.sun_family = AF_UNIX,
|
|
|
|
};
|
|
|
|
struct iovec iov = {
|
|
|
|
.iov_base = (char *)msg,
|
|
|
|
.iov_len = strlen(msg),
|
|
|
|
};
|
|
|
|
struct msghdr mh = {
|
|
|
|
.msg_name = &un,
|
|
|
|
.msg_iov = &iov,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
};
|
|
|
|
|
2019-08-01 12:35:56 +00:00
|
|
|
if (!(path = getenv("NOTIFY_SOCKET"))) {
|
2016-06-06 14:03:27 +00:00
|
|
|
VIR_DEBUG("Skipping systemd notify, not requested");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NB sun_path field is *not* NUL-terminated, hence >, not >= */
|
|
|
|
if (strlen(path) > sizeof(un.sun_path)) {
|
|
|
|
VIR_WARN("Systemd notify socket path '%s' too long", path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(un.sun_path, path, strlen(path));
|
|
|
|
if (un.sun_path[0] == '@')
|
|
|
|
un.sun_path[0] = '\0';
|
|
|
|
|
2016-07-11 23:26:48 +00:00
|
|
|
mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(path);
|
|
|
|
|
2016-06-06 14:03:27 +00:00
|
|
|
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
VIR_WARN("Unable to create socket FD");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0)
|
|
|
|
VIR_WARN("Failed to notify systemd");
|
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
2020-01-14 17:38:59 +00:00
|
|
|
#endif /* !WIN32 */
|
2014-02-21 12:06:42 +00:00
|
|
|
}
|
2014-04-11 07:20:48 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
virSystemdPMSupportTarget(const char *methodName, bool *result)
|
|
|
|
{
|
2020-09-09 14:42:42 +00:00
|
|
|
int rc;
|
|
|
|
GDBusConnection *conn;
|
|
|
|
g_autoptr(GVariant) reply = NULL;
|
2014-04-11 07:20:48 +00:00
|
|
|
char *response;
|
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if ((rc = virSystemdHasLogind()) < 0)
|
|
|
|
return rc;
|
2014-04-11 07:20:48 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if (!(conn = virGDBusGetSystemBus()))
|
2014-04-11 07:20:48 +00:00
|
|
|
return -1;
|
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
if (virGDBusCallMethod(conn,
|
|
|
|
&reply,
|
2020-09-21 13:42:00 +00:00
|
|
|
G_VARIANT_TYPE("(s)"),
|
2020-09-09 14:42:42 +00:00
|
|
|
NULL,
|
|
|
|
"org.freedesktop.login1",
|
|
|
|
"/org/freedesktop/login1",
|
|
|
|
"org.freedesktop.login1.Manager",
|
|
|
|
methodName,
|
|
|
|
NULL) < 0)
|
|
|
|
return -1;
|
2014-04-11 07:20:48 +00:00
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
g_variant_get(reply, "(&s)", &response);
|
2014-04-11 07:20:48 +00:00
|
|
|
|
|
|
|
*result = STREQ("yes", response) || STREQ("challenge", response);
|
|
|
|
|
2020-09-09 14:42:42 +00:00
|
|
|
return 0;
|
2014-04-11 07:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int virSystemdCanSuspend(bool *result)
|
|
|
|
{
|
|
|
|
return virSystemdPMSupportTarget("CanSuspend", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int virSystemdCanHibernate(bool *result)
|
|
|
|
{
|
|
|
|
return virSystemdPMSupportTarget("CanHibernate", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int virSystemdCanHybridSleep(bool *result)
|
|
|
|
{
|
|
|
|
return virSystemdPMSupportTarget("CanHybridSleep", result);
|
|
|
|
}
|
2019-06-25 12:03:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
2019-11-21 19:27:58 +00:00
|
|
|
virSystemdActivationEntryFree(void *data)
|
2019-06-25 12:03:56 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivationEntry *ent = data;
|
2019-06-25 12:03:56 +00:00
|
|
|
size_t i;
|
|
|
|
|
2019-11-21 19:27:58 +00:00
|
|
|
VIR_DEBUG("Closing activation FDs");
|
2019-06-25 12:03:56 +00:00
|
|
|
for (i = 0; i < ent->nfds; i++) {
|
|
|
|
VIR_DEBUG("Closing activation FD %d", ent->fds[i]);
|
|
|
|
VIR_FORCE_CLOSE(ent->fds[i]);
|
|
|
|
}
|
|
|
|
|
2021-02-03 19:32:34 +00:00
|
|
|
g_free(ent->fds);
|
|
|
|
g_free(ent);
|
2019-06-25 12:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivationAddFD(virSystemdActivation *act,
|
2019-06-25 12:03:56 +00:00
|
|
|
const char *name,
|
|
|
|
int fd)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivationEntry *ent = virHashLookup(act->fds, name);
|
2019-06-25 12:03:56 +00:00
|
|
|
|
|
|
|
if (!ent) {
|
2020-10-05 17:09:00 +00:00
|
|
|
ent = g_new0(virSystemdActivationEntry, 1);
|
|
|
|
ent->fds = g_new0(int, 1);
|
2019-06-25 12:03:56 +00:00
|
|
|
ent->fds[ent->nfds++] = fd;
|
|
|
|
|
|
|
|
VIR_DEBUG("Record first FD %d with name %s", fd, name);
|
|
|
|
if (virHashAddEntry(act->fds, name, ent) < 0) {
|
2019-11-21 19:27:58 +00:00
|
|
|
virSystemdActivationEntryFree(ent);
|
2019-06-25 12:03:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:37:03 +00:00
|
|
|
VIR_EXPAND_N(ent->fds, ent->nfds, 1);
|
2019-06-25 12:03:56 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Record extra FD %d with name %s", fd, name);
|
|
|
|
ent->fds[ent->nfds - 1] = fd;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivationInitFromNames(virSystemdActivation *act,
|
2019-06-25 12:03:56 +00:00
|
|
|
int nfds,
|
|
|
|
const char *fdnames)
|
|
|
|
{
|
2020-12-01 08:21:32 +00:00
|
|
|
g_auto(GStrv) fdnamelistptr = NULL;
|
2019-06-25 12:03:56 +00:00
|
|
|
char **fdnamelist;
|
|
|
|
size_t i;
|
|
|
|
int nextfd = STDERR_FILENO + 1;
|
|
|
|
|
|
|
|
VIR_DEBUG("FD names %s", fdnames);
|
|
|
|
|
2021-04-01 13:35:39 +00:00
|
|
|
if (!(fdnamelistptr = g_strsplit(fdnames, ":", 0)))
|
2019-06-25 12:03:56 +00:00
|
|
|
goto error;
|
|
|
|
|
2021-04-01 13:35:39 +00:00
|
|
|
if (g_strv_length(fdnamelistptr) != nfds) {
|
2019-06-25 12:03:56 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2021-04-01 13:35:39 +00:00
|
|
|
_("Expecting %d FD names but got %u"),
|
|
|
|
nfds, g_strv_length(fdnamelistptr));
|
2019-06-25 12:03:56 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
fdnamelist = fdnamelistptr;
|
|
|
|
while (nfds) {
|
|
|
|
if (virSystemdActivationAddFD(act, *fdnamelist, nextfd) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
fdnamelist++;
|
|
|
|
nextfd++;
|
|
|
|
nfds--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
int fd = nextfd + i;
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Back compat for systemd < v227 which lacks LISTEN_FDNAMES.
|
|
|
|
* Delete when min systemd is increased ie RHEL7 dropped
|
|
|
|
*/
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivationInitFromMap(virSystemdActivation *act,
|
2019-06-25 12:03:56 +00:00
|
|
|
int nfds,
|
|
|
|
virSystemdActivationMap *map,
|
|
|
|
size_t nmap)
|
|
|
|
{
|
|
|
|
int nextfd = STDERR_FILENO + 1;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
while (nfds) {
|
|
|
|
virSocketAddr addr;
|
|
|
|
const char *name = NULL;
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
|
|
|
|
addr.len = sizeof(addr.data);
|
|
|
|
if (getsockname(nextfd, &addr.data.sa, &addr.len) < 0) {
|
|
|
|
virReportSystemError(errno, "%s", _("Unable to get local socket name"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:05:12 +00:00
|
|
|
VIR_DEBUG("Got socket family %d for FD %d",
|
|
|
|
addr.data.sa.sa_family, nextfd);
|
|
|
|
|
2019-06-25 12:03:56 +00:00
|
|
|
for (i = 0; i < nmap && !name; i++) {
|
|
|
|
if (map[i].name == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (addr.data.sa.sa_family == AF_INET) {
|
2019-08-20 15:05:12 +00:00
|
|
|
if (map[i].family == AF_INET) {
|
|
|
|
VIR_DEBUG("Expect %d got %d",
|
|
|
|
map[i].port, ntohs(addr.data.inet4.sin_port));
|
|
|
|
if (addr.data.inet4.sin_port == htons(map[i].port))
|
|
|
|
name = map[i].name;
|
|
|
|
}
|
2019-06-25 12:03:56 +00:00
|
|
|
} else if (addr.data.sa.sa_family == AF_INET6) {
|
|
|
|
/* NB use of AF_INET here is correct. The "map" struct
|
|
|
|
* only refers to AF_INET. The socket may be AF_INET
|
|
|
|
* or AF_INET6
|
|
|
|
*/
|
2019-08-20 15:05:12 +00:00
|
|
|
if (map[i].family == AF_INET) {
|
|
|
|
VIR_DEBUG("Expect %d got %d",
|
|
|
|
map[i].port, ntohs(addr.data.inet6.sin6_port));
|
|
|
|
if (addr.data.inet6.sin6_port == htons(map[i].port))
|
|
|
|
name = map[i].name;
|
|
|
|
}
|
2019-06-25 12:03:56 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
} else if (addr.data.sa.sa_family == AF_UNIX) {
|
2019-08-20 15:05:12 +00:00
|
|
|
if (map[i].family == AF_UNIX) {
|
|
|
|
VIR_DEBUG("Expect %s got %s", map[i].path, addr.data.un.sun_path);
|
|
|
|
if (STREQLEN(map[i].path,
|
|
|
|
addr.data.un.sun_path,
|
|
|
|
sizeof(addr.data.un.sun_path)))
|
|
|
|
name = map[i].name;
|
|
|
|
}
|
2019-06-25 12:03:56 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unexpected socket family %d"),
|
|
|
|
addr.data.sa.sa_family);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot find name for FD %d socket family %d"),
|
|
|
|
nextfd, addr.data.sa.sa_family);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virSystemdActivationAddFD(act, name, nextfd) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
nfds--;
|
|
|
|
nextfd++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
int fd = nextfd + i;
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-25 17:35:11 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSystemdGetListenFDs:
|
|
|
|
*
|
|
|
|
* Parse LISTEN_PID and LISTEN_FDS passed from caller.
|
|
|
|
*
|
|
|
|
* Returns number of passed FDs.
|
|
|
|
*/
|
|
|
|
static unsigned int
|
|
|
|
virSystemdGetListenFDs(void)
|
|
|
|
{
|
|
|
|
const char *pidstr;
|
|
|
|
const char *fdstr;
|
|
|
|
size_t i = 0;
|
|
|
|
unsigned long long procid;
|
|
|
|
unsigned int nfds;
|
|
|
|
|
|
|
|
VIR_DEBUG("Setting up networking from caller");
|
|
|
|
|
2019-08-01 12:35:56 +00:00
|
|
|
if (!(pidstr = getenv("LISTEN_PID"))) {
|
2019-06-25 17:35:11 +00:00
|
|
|
VIR_DEBUG("No LISTEN_PID from caller");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStrToLong_ull(pidstr, NULL, 10, &procid) < 0) {
|
|
|
|
VIR_DEBUG("Malformed LISTEN_PID from caller %s", pidstr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pid_t)procid != getpid()) {
|
|
|
|
VIR_DEBUG("LISTEN_PID %s is not for us %lld",
|
|
|
|
pidstr, (long long) getpid());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-01 12:35:56 +00:00
|
|
|
if (!(fdstr = getenv("LISTEN_FDS"))) {
|
2019-06-25 17:35:11 +00:00
|
|
|
VIR_DEBUG("No LISTEN_FDS from caller");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStrToLong_ui(fdstr, NULL, 10, &nfds) < 0) {
|
|
|
|
VIR_DEBUG("Malformed LISTEN_FDS from caller %s", fdstr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-18 17:16:19 +00:00
|
|
|
g_unsetenv("LISTEN_PID");
|
|
|
|
g_unsetenv("LISTEN_FDS");
|
2019-06-25 17:35:11 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Got %u file descriptors", nfds);
|
|
|
|
|
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
int fd = STDERR_FILENO + i + 1;
|
|
|
|
|
|
|
|
VIR_DEBUG("Disabling inheritance of passed FD %d", fd);
|
|
|
|
|
|
|
|
if (virSetInherit(fd, false) < 0)
|
|
|
|
VIR_WARN("Couldn't disable inheritance of passed FD %d", fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nfds;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* WIN32 */
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
virSystemdGetListenFDs(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WIN32 */
|
2019-06-25 12:03:56 +00:00
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static virSystemdActivation *
|
2019-06-25 12:03:56 +00:00
|
|
|
virSystemdActivationNew(virSystemdActivationMap *map,
|
|
|
|
size_t nmap,
|
|
|
|
int nfds)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivation *act;
|
2019-06-25 12:03:56 +00:00
|
|
|
const char *fdnames;
|
|
|
|
|
|
|
|
VIR_DEBUG("Activated with %d FDs", nfds);
|
2020-10-05 17:09:00 +00:00
|
|
|
act = g_new0(virSystemdActivation, 1);
|
2019-06-25 12:03:56 +00:00
|
|
|
|
2020-10-20 16:43:20 +00:00
|
|
|
if (!(act->fds = virHashNew(virSystemdActivationEntryFree)))
|
2019-06-25 12:03:56 +00:00
|
|
|
goto error;
|
|
|
|
|
2019-08-01 12:35:56 +00:00
|
|
|
fdnames = getenv("LISTEN_FDNAMES");
|
2019-06-25 12:03:56 +00:00
|
|
|
if (fdnames) {
|
|
|
|
if (virSystemdActivationInitFromNames(act, nfds, fdnames) < 0)
|
|
|
|
goto error;
|
|
|
|
} else {
|
|
|
|
if (virSystemdActivationInitFromMap(act, nfds, map, nmap) < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Created activation object for %d FDs", nfds);
|
|
|
|
return act;
|
|
|
|
|
|
|
|
error:
|
2019-10-01 09:50:34 +00:00
|
|
|
virSystemdActivationFree(act);
|
2019-06-25 12:03:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSystemdGetActivation:
|
|
|
|
* @map: mapping of socket addresses to names
|
|
|
|
* @nmap: number of entries in @map
|
|
|
|
* @act: filled with allocated activation object
|
|
|
|
*
|
|
|
|
* Acquire an object for handling systemd activation.
|
|
|
|
* If no activation FDs have been provided the returned object
|
2020-07-09 04:42:21 +00:00
|
|
|
* will be NULL, indicating normal service setup can be performed
|
2019-06-25 12:03:56 +00:00
|
|
|
* If the returned object is non-NULL then at least one file
|
|
|
|
* descriptor will be present. No normal service setup should
|
|
|
|
* be performed.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on failure
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virSystemdGetActivation(virSystemdActivationMap *map,
|
|
|
|
size_t nmap,
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivation **act)
|
2019-06-25 12:03:56 +00:00
|
|
|
{
|
|
|
|
int nfds = 0;
|
|
|
|
|
2019-06-25 17:35:11 +00:00
|
|
|
if ((nfds = virSystemdGetListenFDs()) < 0)
|
2019-06-25 12:03:56 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (nfds == 0) {
|
|
|
|
VIR_DEBUG("No activation FDs present");
|
|
|
|
*act = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*act = virSystemdActivationNew(map, nmap, nfds);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSystemdActivationHasName:
|
|
|
|
* @act: the activation object
|
|
|
|
* @name: the file descriptor name
|
|
|
|
*
|
|
|
|
* Check whether there is a file descriptor present
|
|
|
|
* for the requested name.
|
|
|
|
*
|
|
|
|
* Returns: true if a FD is present, false otherwise
|
|
|
|
*/
|
|
|
|
bool
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivationHasName(virSystemdActivation *act,
|
2019-06-25 12:03:56 +00:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
return virHashLookup(act->fds, name) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSystemdActivationComplete:
|
|
|
|
* @act: the activation object
|
|
|
|
*
|
|
|
|
* Indicate that processing of activation has been
|
|
|
|
* completed. All provided file descriptors should
|
|
|
|
* have been claimed. If any are unclaimed then
|
|
|
|
* an error will be reported
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 if some FDs are unclaimed
|
|
|
|
*/
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivationComplete(virSystemdActivation *act)
|
2019-06-25 12:03:56 +00:00
|
|
|
{
|
|
|
|
if (virHashSize(act->fds) != 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Some activation file descriptors are unclaimed"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSystemdActivationClaimFDs:
|
|
|
|
* @act: the activation object
|
|
|
|
* @name: the file descriptor name
|
|
|
|
* @fds: to be filled with claimed FDs
|
|
|
|
* @nfds: to be filled with number of FDs in @fds
|
|
|
|
*
|
|
|
|
* Claims the file descriptors associated with
|
|
|
|
* @name.
|
|
|
|
*
|
|
|
|
* The caller is responsible for closing all
|
|
|
|
* returned file descriptors when they are no
|
|
|
|
* longer required. The caller must also free
|
|
|
|
* the array memory in @fds.
|
|
|
|
*/
|
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivationClaimFDs(virSystemdActivation *act,
|
2019-06-25 12:03:56 +00:00
|
|
|
const char *name,
|
|
|
|
int **fds,
|
|
|
|
size_t *nfds)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivationEntry *ent = virHashSteal(act->fds, name);
|
2019-06-25 12:03:56 +00:00
|
|
|
|
|
|
|
if (!ent) {
|
|
|
|
*fds = NULL;
|
|
|
|
*nfds = 0;
|
|
|
|
VIR_DEBUG("No FD with name %s", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Found %zu FDs with name %s", ent->nfds, name);
|
|
|
|
*fds = ent->fds;
|
|
|
|
*nfds = ent->nfds;
|
|
|
|
|
|
|
|
VIR_FREE(ent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSystemdActivationFree:
|
|
|
|
* @act: the activation object
|
|
|
|
*
|
|
|
|
* Free memory and close unclaimed file descriptors
|
|
|
|
* associated with the activation object
|
|
|
|
*/
|
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virSystemdActivationFree(virSystemdActivation *act)
|
2019-06-25 12:03:56 +00:00
|
|
|
{
|
2019-10-01 09:50:34 +00:00
|
|
|
if (!act)
|
2019-06-25 12:03:56 +00:00
|
|
|
return;
|
|
|
|
|
2019-10-01 09:50:34 +00:00
|
|
|
virHashFree(act->fds);
|
2019-06-25 12:03:56 +00:00
|
|
|
|
2021-02-03 19:32:34 +00:00
|
|
|
g_free(act);
|
2019-06-25 12:03:56 +00:00
|
|
|
}
|