mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-21 20:15:17 +00:00
Add logic for handling systemd-machined non-existance
If systemd machine does not exist, return -2 instead of -1, so that applications don't need to repeat the tedious error checking code Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
parent
c64904b745
commit
666bff4faf
@ -26,6 +26,7 @@
|
||||
#include "virstring.h"
|
||||
#include "viralloc.h"
|
||||
#include "virutil.h"
|
||||
#include "virlog.h"
|
||||
|
||||
#define VIR_FROM_THIS VIR_FROM_SYSTEMD
|
||||
|
||||
@ -38,6 +39,8 @@
|
||||
* @rootdir: root directory of machine filesystem
|
||||
* @pidleader: PID of the leader process
|
||||
* @slice: name of the slice to place the machine in
|
||||
*
|
||||
* Returns 0 on success, -1 on fatal error, or -2 if systemd-machine is not available
|
||||
*/
|
||||
int virSystemdCreateMachine(const char *name,
|
||||
const char *drivername,
|
||||
@ -117,6 +120,7 @@ int virSystemdCreateMachine(const char *name,
|
||||
* allow further API calls to be made against the object.
|
||||
*/
|
||||
|
||||
VIR_DEBUG("Attempting to create machine via systemd");
|
||||
if (virDBusCallMethod(conn,
|
||||
NULL,
|
||||
"org.freedesktop.machine1",
|
||||
@ -135,8 +139,15 @@ int virSystemdCreateMachine(const char *name,
|
||||
(unsigned int)pidleader,
|
||||
rootdir ? rootdir : "",
|
||||
1, "Slice", "s",
|
||||
slicename) < 0)
|
||||
slicename) < 0) {
|
||||
virErrorPtr err = virGetLastError();
|
||||
if (err->code == VIR_ERR_DBUS_SERVICE &&
|
||||
STREQ(err->str2, "org.freedesktop.DBus.Error.ServiceUnknown")) {
|
||||
virResetLastError();
|
||||
ret = -2;
|
||||
}
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
|
@ -60,16 +60,20 @@ dbus_bool_t dbus_connection_set_watch_functions(DBusConnection *connection ATTRI
|
||||
DBusMessage *dbus_connection_send_with_reply_and_block(DBusConnection *connection ATTRIBUTE_UNUSED,
|
||||
DBusMessage *message,
|
||||
int timeout_milliseconds ATTRIBUTE_UNUSED,
|
||||
DBusError *error ATTRIBUTE_UNUSED)
|
||||
DBusError *error)
|
||||
{
|
||||
DBusMessage *reply;
|
||||
DBusMessage *reply = NULL;
|
||||
|
||||
dbus_message_set_serial(message, 7);
|
||||
|
||||
if (getenv("FAIL_NO_SERVICE"))
|
||||
if (getenv("FAIL_BAD_SERVICE"))
|
||||
reply = dbus_message_new_error(message,
|
||||
"org.freedesktop.DBus.Error.ServiceUnknown",
|
||||
"The name org.freedesktop.machine1 was not provided by any .service files");
|
||||
"org.freedesktop.systemd.badthing",
|
||||
"Something went wrong creating the machine");
|
||||
else if (getenv("FAIL_NO_SERVICE"))
|
||||
dbus_set_error(error,
|
||||
"org.freedesktop.DBus.Error.ServiceUnknown",
|
||||
"%s", "The name org.freedesktop.machine1 was not provided by any .service files");
|
||||
else
|
||||
reply = dbus_message_new_method_return(message);
|
||||
|
||||
|
@ -82,35 +82,60 @@ static int testCreateNoSystemd(const void *opaque ATTRIBUTE_UNUSED)
|
||||
3, 3, 3, 3,
|
||||
4, 4, 4, 4
|
||||
};
|
||||
int rv;
|
||||
|
||||
setenv("FAIL_NO_SERVICE", "1", 1);
|
||||
|
||||
if (virSystemdCreateMachine("demo",
|
||||
"qemu",
|
||||
true,
|
||||
uuid,
|
||||
NULL,
|
||||
123,
|
||||
false,
|
||||
NULL) == 0) {
|
||||
if ((rv = virSystemdCreateMachine("demo",
|
||||
"qemu",
|
||||
true,
|
||||
uuid,
|
||||
NULL,
|
||||
123,
|
||||
false,
|
||||
NULL)) == 0) {
|
||||
fprintf(stderr, "%s", "Unexpected create machine success\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
virErrorPtr err = virGetLastError();
|
||||
|
||||
if (!err) {
|
||||
fprintf(stderr, "No error raised");
|
||||
if (rv != -2) {
|
||||
fprintf(stderr, "%s", "Unexpected create machine error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (err->code == VIR_ERR_DBUS_SERVICE &&
|
||||
STREQ(err->str2, "org.freedesktop.DBus.Error.ServiceUnknown"))
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Unexpected error code %d / message %s\n",
|
||||
err->code, err->str2);
|
||||
return -1;
|
||||
static int testCreateBadSystemd(const void *opaque ATTRIBUTE_UNUSED)
|
||||
{
|
||||
unsigned char uuid[VIR_UUID_BUFLEN] = {
|
||||
1, 1, 1, 1,
|
||||
2, 2, 2, 2,
|
||||
3, 3, 3, 3,
|
||||
4, 4, 4, 4
|
||||
};
|
||||
int rv;
|
||||
|
||||
setenv("FAIL_BAD_SERVICE", "1", 1);
|
||||
|
||||
if ((rv = virSystemdCreateMachine("demo",
|
||||
"qemu",
|
||||
true,
|
||||
uuid,
|
||||
NULL,
|
||||
123,
|
||||
false,
|
||||
NULL)) == 0) {
|
||||
fprintf(stderr, "%s", "Unexpected create machine success\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (rv != -1) {
|
||||
fprintf(stderr, "%s", "Unexpected create machine error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
@ -122,7 +147,9 @@ mymain(void)
|
||||
ret = -1;
|
||||
if (virtTestRun("Test create machine ", 1, testCreateMachine, NULL) < 0)
|
||||
ret = -1;
|
||||
if (virtTestRun("Test create nosystemd ", 1, testCreateNoSystemd, NULL) < 0)
|
||||
if (virtTestRun("Test create no systemd ", 1, testCreateNoSystemd, NULL) < 0)
|
||||
ret = -1;
|
||||
if (virtTestRun("Test create bad systemd ", 1, testCreateBadSystemd, NULL) < 0)
|
||||
ret = -1;
|
||||
|
||||
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
|
Loading…
x
Reference in New Issue
Block a user