2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2013-11-22 04:01:46 +01:00
|
|
|
|
|
|
|
#include "sd-bus.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
|
|
|
|
#include "bus-common-errors.h"
|
2015-08-27 16:32:22 +02:00
|
|
|
#include "bus-error.h"
|
2013-11-22 04:01:46 +01:00
|
|
|
#include "bus-util.h"
|
2014-12-06 02:39:15 +01:00
|
|
|
#include "errno-list.h"
|
2019-07-03 16:56:17 +02:00
|
|
|
#include "errno-util.h"
|
2013-11-22 04:01:46 +01:00
|
|
|
|
2014-10-31 01:31:48 +01:00
|
|
|
static void test_error(void) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL, second = SD_BUS_ERROR_NULL;
|
2013-11-30 19:45:32 +01:00
|
|
|
const sd_bus_error const_error = SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "const error");
|
|
|
|
const sd_bus_error temporarily_const_error = {
|
|
|
|
.name = SD_BUS_ERROR_ACCESS_DENIED,
|
|
|
|
.message = "oh! no",
|
|
|
|
._need_free = -1
|
|
|
|
};
|
2013-11-22 04:01:46 +01:00
|
|
|
|
|
|
|
assert_se(!sd_bus_error_is_set(&error));
|
2015-03-13 14:08:00 +01:00
|
|
|
assert_se(sd_bus_error_set(&error, SD_BUS_ERROR_NOT_SUPPORTED, "xxx") == -EOPNOTSUPP);
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(streq(error.name, SD_BUS_ERROR_NOT_SUPPORTED));
|
|
|
|
assert_se(streq(error.message, "xxx"));
|
|
|
|
assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_NOT_SUPPORTED));
|
2020-08-22 18:24:43 +02:00
|
|
|
assert_se(sd_bus_error_has_names_sentinel(&error, SD_BUS_ERROR_NOT_SUPPORTED, NULL));
|
|
|
|
assert_se(sd_bus_error_has_names(&error, SD_BUS_ERROR_NOT_SUPPORTED));
|
|
|
|
assert_se(sd_bus_error_has_names(&error, SD_BUS_ERROR_NOT_SUPPORTED, SD_BUS_ERROR_FILE_NOT_FOUND));
|
|
|
|
assert_se(sd_bus_error_has_names(&error, SD_BUS_ERROR_FILE_NOT_FOUND, SD_BUS_ERROR_NOT_SUPPORTED, NULL));
|
|
|
|
assert_se(!sd_bus_error_has_names(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
|
2015-03-13 14:08:00 +01:00
|
|
|
assert_se(sd_bus_error_get_errno(&error) == EOPNOTSUPP);
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(sd_bus_error_is_set(&error));
|
|
|
|
sd_bus_error_free(&error);
|
|
|
|
|
2016-01-13 04:44:44 +01:00
|
|
|
/* Check with no error */
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(!sd_bus_error_is_set(&error));
|
2016-01-13 04:44:44 +01:00
|
|
|
assert_se(sd_bus_error_setf(&error, NULL, "yyy %i", -1) == 0);
|
|
|
|
assert_se(error.name == NULL);
|
|
|
|
assert_se(error.message == NULL);
|
|
|
|
assert_se(!sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
|
2020-08-22 18:24:43 +02:00
|
|
|
assert_se(!sd_bus_error_has_names(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
|
2016-01-13 04:44:44 +01:00
|
|
|
assert_se(sd_bus_error_get_errno(&error) == 0);
|
|
|
|
assert_se(!sd_bus_error_is_set(&error));
|
|
|
|
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(sd_bus_error_setf(&error, SD_BUS_ERROR_FILE_NOT_FOUND, "yyy %i", -1) == -ENOENT);
|
|
|
|
assert_se(streq(error.name, SD_BUS_ERROR_FILE_NOT_FOUND));
|
|
|
|
assert_se(streq(error.message, "yyy -1"));
|
|
|
|
assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
|
2020-08-22 18:24:43 +02:00
|
|
|
assert_se(sd_bus_error_has_names(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(sd_bus_error_get_errno(&error) == ENOENT);
|
|
|
|
assert_se(sd_bus_error_is_set(&error));
|
|
|
|
|
|
|
|
assert_se(!sd_bus_error_is_set(&second));
|
2013-11-30 19:45:32 +01:00
|
|
|
assert_se(second._need_free == 0);
|
|
|
|
assert_se(error._need_free > 0);
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(sd_bus_error_copy(&second, &error) == -ENOENT);
|
2013-11-30 19:45:32 +01:00
|
|
|
assert_se(second._need_free > 0);
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(streq(error.name, second.name));
|
|
|
|
assert_se(streq(error.message, second.message));
|
|
|
|
assert_se(sd_bus_error_get_errno(&second) == ENOENT);
|
|
|
|
assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_NOT_FOUND));
|
2020-08-22 18:24:43 +02:00
|
|
|
assert_se(sd_bus_error_has_names(&second, SD_BUS_ERROR_FILE_NOT_FOUND));
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(sd_bus_error_is_set(&second));
|
|
|
|
|
|
|
|
sd_bus_error_free(&error);
|
2013-11-30 19:45:32 +01:00
|
|
|
sd_bus_error_free(&second);
|
|
|
|
|
|
|
|
assert_se(!sd_bus_error_is_set(&second));
|
|
|
|
assert_se(const_error._need_free == 0);
|
|
|
|
assert_se(sd_bus_error_copy(&second, &const_error) == -EEXIST);
|
|
|
|
assert_se(second._need_free == 0);
|
|
|
|
assert_se(streq(const_error.name, second.name));
|
|
|
|
assert_se(streq(const_error.message, second.message));
|
|
|
|
assert_se(sd_bus_error_get_errno(&second) == EEXIST);
|
|
|
|
assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_EXISTS));
|
|
|
|
assert_se(sd_bus_error_is_set(&second));
|
|
|
|
sd_bus_error_free(&second);
|
|
|
|
|
|
|
|
assert_se(!sd_bus_error_is_set(&second));
|
|
|
|
assert_se(temporarily_const_error._need_free < 0);
|
|
|
|
assert_se(sd_bus_error_copy(&second, &temporarily_const_error) == -EACCES);
|
|
|
|
assert_se(second._need_free > 0);
|
|
|
|
assert_se(streq(temporarily_const_error.name, second.name));
|
|
|
|
assert_se(streq(temporarily_const_error.message, second.message));
|
|
|
|
assert_se(sd_bus_error_get_errno(&second) == EACCES);
|
|
|
|
assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_ACCESS_DENIED));
|
|
|
|
assert_se(sd_bus_error_is_set(&second));
|
2013-11-22 04:01:46 +01:00
|
|
|
|
|
|
|
assert_se(!sd_bus_error_is_set(&error));
|
2013-11-30 19:53:33 +01:00
|
|
|
assert_se(sd_bus_error_set_const(&error, "System.Error.EUCLEAN", "Hallo") == -EUCLEAN);
|
|
|
|
assert_se(streq(error.name, "System.Error.EUCLEAN"));
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(streq(error.message, "Hallo"));
|
2013-11-30 19:53:33 +01:00
|
|
|
assert_se(sd_bus_error_has_name(&error, "System.Error.EUCLEAN"));
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(sd_bus_error_get_errno(&error) == EUCLEAN);
|
|
|
|
assert_se(sd_bus_error_is_set(&error));
|
|
|
|
sd_bus_error_free(&error);
|
|
|
|
|
|
|
|
assert_se(!sd_bus_error_is_set(&error));
|
|
|
|
assert_se(sd_bus_error_set_errno(&error, EBUSY) == -EBUSY);
|
2013-11-30 19:53:33 +01:00
|
|
|
assert_se(streq(error.name, "System.Error.EBUSY"));
|
2019-07-03 16:56:17 +02:00
|
|
|
assert_se(streq(error.message, strerror_safe(EBUSY)));
|
2013-11-30 19:53:33 +01:00
|
|
|
assert_se(sd_bus_error_has_name(&error, "System.Error.EBUSY"));
|
2013-11-22 04:01:46 +01:00
|
|
|
assert_se(sd_bus_error_get_errno(&error) == EBUSY);
|
|
|
|
assert_se(sd_bus_error_is_set(&error));
|
|
|
|
sd_bus_error_free(&error);
|
|
|
|
|
|
|
|
assert_se(!sd_bus_error_is_set(&error));
|
|
|
|
assert_se(sd_bus_error_set_errnof(&error, EIO, "Waldi %c", 'X') == -EIO);
|
|
|
|
assert_se(streq(error.name, SD_BUS_ERROR_IO_ERROR));
|
|
|
|
assert_se(streq(error.message, "Waldi X"));
|
|
|
|
assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_IO_ERROR));
|
|
|
|
assert_se(sd_bus_error_get_errno(&error) == EIO);
|
|
|
|
assert_se(sd_bus_error_is_set(&error));
|
2016-01-13 04:44:44 +01:00
|
|
|
sd_bus_error_free(&error);
|
|
|
|
|
|
|
|
/* Check with no error */
|
|
|
|
assert_se(!sd_bus_error_is_set(&error));
|
|
|
|
assert_se(sd_bus_error_set_errnof(&error, 0, "Waldi %c", 'X') == 0);
|
|
|
|
assert_se(error.name == NULL);
|
|
|
|
assert_se(error.message == NULL);
|
|
|
|
assert_se(!sd_bus_error_has_name(&error, SD_BUS_ERROR_IO_ERROR));
|
|
|
|
assert_se(sd_bus_error_get_errno(&error) == 0);
|
|
|
|
assert_se(!sd_bus_error_is_set(&error));
|
2014-10-31 01:31:48 +01:00
|
|
|
}
|
|
|
|
|
sd-bus,static-destruct: clean up how we do our ELF section magic
This cleans up a bit how we set up things for the ELF section magic:
1. Let's always use our gcc macros, instead of __attribute__ directly
2. Align our structures to sizeof(void*), i.e. the pointer size, rather
than a fixed 8 or __BIGGEST_ALIGNMENT__. The former is unnecessarily
high for 32bit systems, the latter too high for 64bit systems. gcc
seems to use ptr alignment for static variables itself, hence this
should be good enough for us too. Moreover, the Linux kernel also
uses pointer alginment for all its ELF section registration magic,
hence this should be good enough for us too.
3. Let's always prefix the sections we create ourself with SYSTEMD_,
just to make clear where they come from.
4. Always align the pointer we start from when iterating through these
lists. This should be unnecessary, but makes things nicely
systematic, as we'll align all pointers we use to access these
sections properly.
2018-12-03 13:22:15 +01:00
|
|
|
extern const sd_bus_error_map __start_SYSTEMD_BUS_ERROR_MAP[];
|
|
|
|
extern const sd_bus_error_map __stop_SYSTEMD_BUS_ERROR_MAP[];
|
2014-10-31 14:31:02 +01:00
|
|
|
|
|
|
|
static void dump_mapping_table(void) {
|
2014-12-06 02:39:15 +01:00
|
|
|
const sd_bus_error_map *m;
|
2014-10-31 14:31:02 +01:00
|
|
|
|
|
|
|
printf("----- errno mappings ------\n");
|
sd-bus,static-destruct: clean up how we do our ELF section magic
This cleans up a bit how we set up things for the ELF section magic:
1. Let's always use our gcc macros, instead of __attribute__ directly
2. Align our structures to sizeof(void*), i.e. the pointer size, rather
than a fixed 8 or __BIGGEST_ALIGNMENT__. The former is unnecessarily
high for 32bit systems, the latter too high for 64bit systems. gcc
seems to use ptr alignment for static variables itself, hence this
should be good enough for us too. Moreover, the Linux kernel also
uses pointer alginment for all its ELF section registration magic,
hence this should be good enough for us too.
3. Let's always prefix the sections we create ourself with SYSTEMD_,
just to make clear where they come from.
4. Always align the pointer we start from when iterating through these
lists. This should be unnecessary, but makes things nicely
systematic, as we'll align all pointers we use to access these
sections properly.
2018-12-03 13:22:15 +01:00
|
|
|
m = ALIGN_TO_PTR(__start_SYSTEMD_BUS_ERROR_MAP, sizeof(void*));
|
|
|
|
while (m < __stop_SYSTEMD_BUS_ERROR_MAP) {
|
2014-12-06 02:39:15 +01:00
|
|
|
|
|
|
|
if (m->code == BUS_ERROR_MAP_END_MARKER) {
|
sd-bus,static-destruct: clean up how we do our ELF section magic
This cleans up a bit how we set up things for the ELF section magic:
1. Let's always use our gcc macros, instead of __attribute__ directly
2. Align our structures to sizeof(void*), i.e. the pointer size, rather
than a fixed 8 or __BIGGEST_ALIGNMENT__. The former is unnecessarily
high for 32bit systems, the latter too high for 64bit systems. gcc
seems to use ptr alignment for static variables itself, hence this
should be good enough for us too. Moreover, the Linux kernel also
uses pointer alginment for all its ELF section registration magic,
hence this should be good enough for us too.
3. Let's always prefix the sections we create ourself with SYSTEMD_,
just to make clear where they come from.
4. Always align the pointer we start from when iterating through these
lists. This should be unnecessary, but makes things nicely
systematic, as we'll align all pointers we use to access these
sections properly.
2018-12-03 13:22:15 +01:00
|
|
|
m = ALIGN_TO_PTR(m + 1, sizeof(void*));
|
2014-12-06 02:39:15 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s -> %i/%s\n", strna(m->name), m->code, strna(errno_to_name(m->code)));
|
2016-02-23 05:32:04 +01:00
|
|
|
m++;
|
2014-12-06 02:39:15 +01:00
|
|
|
}
|
2014-10-31 14:31:02 +01:00
|
|
|
printf("---------------------------\n");
|
|
|
|
}
|
|
|
|
|
2014-10-31 01:31:48 +01:00
|
|
|
static void test_errno_mapping_standard(void) {
|
|
|
|
assert_se(sd_bus_error_set(NULL, "System.Error.EUCLEAN", NULL) == -EUCLEAN);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "System.Error.EBUSY", NULL) == -EBUSY);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "System.Error.EINVAL", NULL) == -EINVAL);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "System.Error.WHATSIT", NULL) == -EIO);
|
|
|
|
}
|
|
|
|
|
2014-12-06 02:39:15 +01:00
|
|
|
BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map test_errors[] = {
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error", 5),
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-2", 52),
|
|
|
|
SD_BUS_ERROR_MAP_END
|
|
|
|
};
|
|
|
|
|
|
|
|
BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map test_errors2[] = {
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-3", 33),
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-4", 44),
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-33", 333),
|
|
|
|
SD_BUS_ERROR_MAP_END
|
|
|
|
};
|
|
|
|
|
|
|
|
static const sd_bus_error_map test_errors3[] = {
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-88", 888),
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-99", 999),
|
|
|
|
SD_BUS_ERROR_MAP_END
|
|
|
|
};
|
|
|
|
|
|
|
|
static const sd_bus_error_map test_errors4[] = {
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-77", 777),
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-78", 778),
|
|
|
|
SD_BUS_ERROR_MAP_END
|
2014-10-31 01:31:48 +01:00
|
|
|
};
|
|
|
|
|
2016-01-13 20:23:54 +01:00
|
|
|
static const sd_bus_error_map test_errors_bad1[] = {
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-1", 0),
|
|
|
|
SD_BUS_ERROR_MAP_END
|
|
|
|
};
|
|
|
|
|
|
|
|
static const sd_bus_error_map test_errors_bad2[] = {
|
|
|
|
SD_BUS_ERROR_MAP("org.freedesktop.custom-dbus-error-1", -1),
|
|
|
|
SD_BUS_ERROR_MAP_END
|
|
|
|
};
|
|
|
|
|
2014-10-31 01:31:48 +01:00
|
|
|
static void test_errno_mapping_custom(void) {
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error", NULL) == -5);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-2", NULL) == -52);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-x", NULL) == -EIO);
|
2014-12-06 02:39:15 +01:00
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-33", NULL) == -333);
|
|
|
|
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-88", NULL) == -EIO);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-99", NULL) == -EIO);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-77", NULL) == -EIO);
|
|
|
|
|
|
|
|
assert_se(sd_bus_error_add_map(test_errors3) > 0);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-88", NULL) == -888);
|
|
|
|
assert_se(sd_bus_error_add_map(test_errors4) > 0);
|
|
|
|
assert_se(sd_bus_error_add_map(test_errors4) == 0);
|
|
|
|
assert_se(sd_bus_error_add_map(test_errors3) == 0);
|
|
|
|
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-99", NULL) == -999);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-77", NULL) == -777);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-78", NULL) == -778);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-2", NULL) == -52);
|
|
|
|
assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-y", NULL) == -EIO);
|
|
|
|
|
|
|
|
assert_se(sd_bus_error_set(NULL, BUS_ERROR_NO_SUCH_UNIT, NULL) == -ENOENT);
|
2016-01-13 20:23:54 +01:00
|
|
|
|
|
|
|
assert_se(sd_bus_error_add_map(test_errors_bad1) == -EINVAL);
|
|
|
|
assert_se(sd_bus_error_add_map(test_errors_bad2) == -EINVAL);
|
2014-10-31 01:31:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2014-10-31 14:31:02 +01:00
|
|
|
dump_mapping_table();
|
2014-10-31 01:31:48 +01:00
|
|
|
|
|
|
|
test_error();
|
|
|
|
test_errno_mapping_standard();
|
|
|
|
test_errno_mapping_custom();
|
2013-11-22 04:01:46 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|