2013-10-11 20:18:10 +02:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2013 Lennart Poettering
|
|
|
|
|
|
|
|
systemd 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.
|
|
|
|
|
|
|
|
systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include "bus-internal.h"
|
|
|
|
#include "bus-message.h"
|
|
|
|
#include "bus-signature.h"
|
2013-10-31 03:02:49 +01:00
|
|
|
#include "bus-type.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "bus-util.h"
|
|
|
|
#include "string-util.h"
|
2013-10-11 20:18:10 +02:00
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_emit_signal(
|
2013-10-11 20:18:10 +02:00
|
|
|
sd_bus *bus,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *member,
|
|
|
|
const char *types, ...) {
|
|
|
|
|
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_message_unrefp) sd_bus_message *m = NULL;
|
2013-10-11 20:18:10 +02:00
|
|
|
int r;
|
|
|
|
|
2013-10-11 20:49:19 +02:00
|
|
|
assert_return(bus, -EINVAL);
|
|
|
|
assert_return(!bus_pid_changed(bus), -ECHILD);
|
2013-10-11 20:18:10 +02:00
|
|
|
|
2014-03-19 21:41:21 +01:00
|
|
|
if (!BUS_IS_OPEN(bus->state))
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_bus_message_new_signal(bus, &m, path, interface, member);
|
2013-10-11 20:18:10 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (!isempty(types)) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, types);
|
|
|
|
r = bus_message_append_ap(m, types, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sd_bus_send(bus, m, NULL);
|
|
|
|
}
|
|
|
|
|
2015-06-08 10:00:17 +02:00
|
|
|
_public_ int sd_bus_call_method_async(
|
|
|
|
sd_bus *bus,
|
|
|
|
sd_bus_slot **slot,
|
|
|
|
const char *destination,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *member,
|
|
|
|
sd_bus_message_handler_t callback,
|
|
|
|
void *userdata,
|
|
|
|
const char *types, ...) {
|
|
|
|
|
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_message_unrefp) sd_bus_message *m = NULL;
|
2015-06-08 10:00:17 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(bus, -EINVAL);
|
|
|
|
assert_return(!bus_pid_changed(bus), -ECHILD);
|
|
|
|
|
|
|
|
if (!BUS_IS_OPEN(bus->state))
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
|
|
|
r = sd_bus_message_new_method_call(bus, &m, destination, path, interface, member);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (!isempty(types)) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, types);
|
|
|
|
r = bus_message_append_ap(m, types, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sd_bus_call_async(bus, slot, m, callback, userdata, 0);
|
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_call_method(
|
2013-10-11 20:18:10 +02:00
|
|
|
sd_bus *bus,
|
|
|
|
const char *destination,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *member,
|
|
|
|
sd_bus_error *error,
|
|
|
|
sd_bus_message **reply,
|
|
|
|
const char *types, ...) {
|
|
|
|
|
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_message_unrefp) sd_bus_message *m = NULL;
|
2013-10-11 20:18:10 +02:00
|
|
|
int r;
|
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
bus_assert_return(bus, -EINVAL, error);
|
|
|
|
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
|
2013-10-11 20:18:10 +02:00
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
if (!BUS_IS_OPEN(bus->state)) {
|
|
|
|
r = -ENOTCONN;
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-19 21:41:21 +01:00
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_bus_message_new_method_call(bus, &m, destination, path, interface, member);
|
2013-10-11 20:18:10 +02:00
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-10-11 20:18:10 +02:00
|
|
|
|
|
|
|
if (!isempty(types)) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, types);
|
|
|
|
r = bus_message_append_ap(m, types, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-10-11 20:18:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-11 18:55:34 +01:00
|
|
|
return sd_bus_call(bus, m, 0, error, reply);
|
2015-08-16 22:13:34 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-10-11 20:18:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_reply_method_return(
|
2013-10-11 20:18:10 +02:00
|
|
|
sd_bus_message *call,
|
|
|
|
const char *types, ...) {
|
|
|
|
|
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_message_unrefp) sd_bus_message *m = NULL;
|
2013-10-11 20:18:10 +02:00
|
|
|
int r;
|
|
|
|
|
2013-10-11 20:49:19 +02:00
|
|
|
assert_return(call, -EINVAL);
|
|
|
|
assert_return(call->sealed, -EPERM);
|
2013-10-16 06:10:04 +02:00
|
|
|
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
|
2014-03-19 21:41:21 +01:00
|
|
|
assert_return(call->bus, -EINVAL);
|
2013-11-21 01:51:16 +01:00
|
|
|
assert_return(!bus_pid_changed(call->bus), -ECHILD);
|
2013-10-11 20:18:10 +02:00
|
|
|
|
2014-03-19 21:41:21 +01:00
|
|
|
if (!BUS_IS_OPEN(call->bus->state))
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
2013-12-03 18:13:48 +01:00
|
|
|
if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
|
2013-10-11 20:18:10 +02:00
|
|
|
return 0;
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
r = sd_bus_message_new_method_return(call, &m);
|
2013-10-11 20:18:10 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (!isempty(types)) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, types);
|
|
|
|
r = bus_message_append_ap(m, types, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
return sd_bus_send(call->bus, m, NULL);
|
2013-10-11 20:18:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_reply_method_error(
|
2013-10-11 20:18:10 +02:00
|
|
|
sd_bus_message *call,
|
|
|
|
const sd_bus_error *e) {
|
|
|
|
|
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_message_unrefp) sd_bus_message *m = NULL;
|
2013-10-11 20:18:10 +02:00
|
|
|
int r;
|
|
|
|
|
2013-10-11 20:49:19 +02:00
|
|
|
assert_return(call, -EINVAL);
|
|
|
|
assert_return(call->sealed, -EPERM);
|
2013-10-16 06:10:04 +02:00
|
|
|
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
|
2013-10-11 20:49:19 +02:00
|
|
|
assert_return(sd_bus_error_is_set(e), -EINVAL);
|
2014-03-19 21:41:21 +01:00
|
|
|
assert_return(call->bus, -EINVAL);
|
2013-11-21 01:51:16 +01:00
|
|
|
assert_return(!bus_pid_changed(call->bus), -ECHILD);
|
2013-10-11 20:18:10 +02:00
|
|
|
|
2014-03-19 21:41:21 +01:00
|
|
|
if (!BUS_IS_OPEN(call->bus->state))
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
2013-12-03 18:13:48 +01:00
|
|
|
if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
|
2013-10-11 20:18:10 +02:00
|
|
|
return 0;
|
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_bus_message_new_method_error(call, &m, e);
|
2013-10-11 20:18:10 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
return sd_bus_send(call->bus, m, NULL);
|
2013-10-11 20:18:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_reply_method_errorf(
|
2013-10-11 20:18:10 +02:00
|
|
|
sd_bus_message *call,
|
|
|
|
const char *name,
|
|
|
|
const char *format,
|
|
|
|
...) {
|
|
|
|
|
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;
|
2013-10-11 20:18:10 +02:00
|
|
|
va_list ap;
|
|
|
|
|
2013-10-11 20:49:19 +02:00
|
|
|
assert_return(call, -EINVAL);
|
|
|
|
assert_return(call->sealed, -EPERM);
|
2013-10-16 06:10:04 +02:00
|
|
|
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
|
2014-03-19 21:41:21 +01:00
|
|
|
assert_return(call->bus, -EINVAL);
|
2013-11-21 01:51:16 +01:00
|
|
|
assert_return(!bus_pid_changed(call->bus), -ECHILD);
|
2013-10-11 20:49:19 +02:00
|
|
|
|
2014-03-19 21:41:21 +01:00
|
|
|
if (!BUS_IS_OPEN(call->bus->state))
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
2013-12-03 18:13:48 +01:00
|
|
|
if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
|
2013-10-11 20:49:19 +02:00
|
|
|
return 0;
|
|
|
|
|
2013-10-16 06:10:04 +02:00
|
|
|
va_start(ap, format);
|
2013-11-22 04:01:46 +01:00
|
|
|
bus_error_setfv(&error, name, format, ap);
|
2013-10-16 06:10:04 +02:00
|
|
|
va_end(ap);
|
2013-10-11 20:18:10 +02:00
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
return sd_bus_reply_method_error(call, &error);
|
2013-10-16 06:10:04 +02:00
|
|
|
}
|
2013-10-11 20:18:10 +02:00
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_reply_method_errno(
|
2013-10-16 06:10:04 +02:00
|
|
|
sd_bus_message *call,
|
|
|
|
int error,
|
|
|
|
const sd_bus_error *p) {
|
2013-10-11 20:18:10 +02:00
|
|
|
|
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 berror = SD_BUS_ERROR_NULL;
|
2013-10-16 06:10:04 +02:00
|
|
|
|
|
|
|
assert_return(call, -EINVAL);
|
|
|
|
assert_return(call->sealed, -EPERM);
|
|
|
|
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
|
2014-03-19 21:41:21 +01:00
|
|
|
assert_return(call->bus, -EINVAL);
|
2013-11-21 01:51:16 +01:00
|
|
|
assert_return(!bus_pid_changed(call->bus), -ECHILD);
|
2013-10-16 06:10:04 +02:00
|
|
|
|
2014-03-19 21:41:21 +01:00
|
|
|
if (!BUS_IS_OPEN(call->bus->state))
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
2013-12-03 18:13:48 +01:00
|
|
|
if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
|
2013-10-16 06:10:04 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (sd_bus_error_is_set(p))
|
2013-11-21 01:51:16 +01:00
|
|
|
return sd_bus_reply_method_error(call, p);
|
2013-10-16 06:10:04 +02:00
|
|
|
|
|
|
|
sd_bus_error_set_errno(&berror, error);
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
return sd_bus_reply_method_error(call, &berror);
|
2013-10-16 06:10:04 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_reply_method_errnof(
|
2013-10-16 06:10:04 +02:00
|
|
|
sd_bus_message *call,
|
|
|
|
int error,
|
|
|
|
const char *format,
|
|
|
|
...) {
|
|
|
|
|
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 berror = SD_BUS_ERROR_NULL;
|
2013-10-16 06:10:04 +02:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
assert_return(call, -EINVAL);
|
|
|
|
assert_return(call->sealed, -EPERM);
|
|
|
|
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
|
2014-03-19 21:41:21 +01:00
|
|
|
assert_return(call->bus, -EINVAL);
|
2013-11-21 01:51:16 +01:00
|
|
|
assert_return(!bus_pid_changed(call->bus), -ECHILD);
|
2013-10-16 06:10:04 +02:00
|
|
|
|
2014-03-19 21:41:21 +01:00
|
|
|
if (!BUS_IS_OPEN(call->bus->state))
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
2013-12-03 18:13:48 +01:00
|
|
|
if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
|
2013-10-16 06:10:04 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2015-02-13 15:34:54 +01:00
|
|
|
sd_bus_error_set_errnofv(&berror, error, format, ap);
|
2013-10-16 06:10:04 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
return sd_bus_reply_method_error(call, &berror);
|
2013-10-11 20:18:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_get_property(
|
2013-10-11 20:18:10 +02:00
|
|
|
sd_bus *bus,
|
|
|
|
const char *destination,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *member,
|
|
|
|
sd_bus_error *error,
|
|
|
|
sd_bus_message **reply,
|
|
|
|
const char *type) {
|
|
|
|
|
|
|
|
sd_bus_message *rep = NULL;
|
|
|
|
int r;
|
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
bus_assert_return(bus, -EINVAL, error);
|
|
|
|
bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL, error);
|
|
|
|
bus_assert_return(member_name_is_valid(member), -EINVAL, error);
|
|
|
|
bus_assert_return(reply, -EINVAL, error);
|
|
|
|
bus_assert_return(signature_is_single(type, false), -EINVAL, error);
|
|
|
|
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
|
2013-10-11 20:18:10 +02:00
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
if (!BUS_IS_OPEN(bus->state)) {
|
|
|
|
r = -ENOTCONN;
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-19 21:41:21 +01:00
|
|
|
|
2013-10-11 20:18:10 +02:00
|
|
|
r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &rep, "ss", strempty(interface), member);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_enter_container(rep, 'v', type);
|
|
|
|
if (r < 0) {
|
|
|
|
sd_bus_message_unref(rep);
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-10-11 20:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*reply = rep;
|
|
|
|
return 0;
|
2015-08-16 22:13:34 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-10-11 20:18:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_get_property_trivial(
|
2013-10-31 03:02:49 +01:00
|
|
|
sd_bus *bus,
|
|
|
|
const char *destination,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *member,
|
|
|
|
sd_bus_error *error,
|
|
|
|
char type, void *ptr) {
|
|
|
|
|
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_message_unrefp) sd_bus_message *reply = NULL;
|
2013-10-31 03:02:49 +01:00
|
|
|
int r;
|
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
bus_assert_return(bus, -EINVAL, error);
|
|
|
|
bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL, error);
|
|
|
|
bus_assert_return(member_name_is_valid(member), -EINVAL, error);
|
|
|
|
bus_assert_return(bus_type_is_trivial(type), -EINVAL, error);
|
|
|
|
bus_assert_return(ptr, -EINVAL, error);
|
|
|
|
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
|
2013-10-31 03:02:49 +01:00
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
if (!BUS_IS_OPEN(bus->state)) {
|
|
|
|
r = -ENOTCONN;
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-19 21:41:21 +01:00
|
|
|
|
2013-10-31 03:02:49 +01:00
|
|
|
r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", strempty(interface), member);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_enter_container(reply, 'v', CHAR_TO_STR(type));
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-10-31 03:02:49 +01:00
|
|
|
|
|
|
|
r = sd_bus_message_read_basic(reply, type, ptr);
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-10-31 03:02:49 +01:00
|
|
|
|
|
|
|
return 0;
|
2015-08-16 22:13:34 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-10-31 03:02:49 +01:00
|
|
|
}
|
|
|
|
|
2013-11-07 19:28:54 +01:00
|
|
|
_public_ int sd_bus_get_property_string(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *destination,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *member,
|
|
|
|
sd_bus_error *error,
|
|
|
|
char **ret) {
|
|
|
|
|
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_message_unrefp) sd_bus_message *reply = NULL;
|
2013-11-07 19:28:54 +01:00
|
|
|
const char *s;
|
|
|
|
char *n;
|
|
|
|
int r;
|
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
bus_assert_return(bus, -EINVAL, error);
|
|
|
|
bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL, error);
|
|
|
|
bus_assert_return(member_name_is_valid(member), -EINVAL, error);
|
|
|
|
bus_assert_return(ret, -EINVAL, error);
|
|
|
|
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
|
2013-11-07 19:28:54 +01:00
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
if (!BUS_IS_OPEN(bus->state)) {
|
|
|
|
r = -ENOTCONN;
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-19 21:41:21 +01:00
|
|
|
|
2013-11-07 19:28:54 +01:00
|
|
|
r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", strempty(interface), member);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_enter_container(reply, 'v', "s");
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-11-07 19:28:54 +01:00
|
|
|
|
|
|
|
r = sd_bus_message_read_basic(reply, 's', &s);
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-11-07 19:28:54 +01:00
|
|
|
|
|
|
|
n = strdup(s);
|
2015-08-16 22:13:34 +02:00
|
|
|
if (!n) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-11-07 19:28:54 +01:00
|
|
|
|
|
|
|
*ret = n;
|
|
|
|
return 0;
|
2015-08-16 22:13:34 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-11-07 19:28:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_bus_get_property_strv(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *destination,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *member,
|
|
|
|
sd_bus_error *error,
|
|
|
|
char ***ret) {
|
|
|
|
|
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_message_unrefp) sd_bus_message *reply = NULL;
|
2013-11-07 19:28:54 +01:00
|
|
|
int r;
|
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
bus_assert_return(bus, -EINVAL, error);
|
|
|
|
bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL, error);
|
|
|
|
bus_assert_return(member_name_is_valid(member), -EINVAL, error);
|
|
|
|
bus_assert_return(ret, -EINVAL, error);
|
|
|
|
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
|
2013-11-07 19:28:54 +01:00
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
if (!BUS_IS_OPEN(bus->state)) {
|
|
|
|
r = -ENOTCONN;
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-19 21:41:21 +01:00
|
|
|
|
2013-11-07 19:28:54 +01:00
|
|
|
r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", strempty(interface), member);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_enter_container(reply, 'v', NULL);
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-11-07 19:28:54 +01:00
|
|
|
|
|
|
|
r = sd_bus_message_read_strv(reply, ret);
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-11-07 19:28:54 +01:00
|
|
|
|
|
|
|
return 0;
|
2015-08-16 22:13:34 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-11-07 19:28:54 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 08:33:42 +01:00
|
|
|
_public_ int sd_bus_set_property(
|
2013-10-11 20:18:10 +02:00
|
|
|
sd_bus *bus,
|
|
|
|
const char *destination,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *member,
|
|
|
|
sd_bus_error *error,
|
|
|
|
const char *type, ...) {
|
|
|
|
|
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_message_unrefp) sd_bus_message *m = NULL;
|
2013-10-11 20:18:10 +02:00
|
|
|
va_list ap;
|
|
|
|
int r;
|
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
bus_assert_return(bus, -EINVAL, error);
|
|
|
|
bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL, error);
|
|
|
|
bus_assert_return(member_name_is_valid(member), -EINVAL, error);
|
|
|
|
bus_assert_return(signature_is_single(type, false), -EINVAL, error);
|
|
|
|
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
|
2013-10-11 20:18:10 +02:00
|
|
|
|
2015-08-16 22:13:34 +02:00
|
|
|
if (!BUS_IS_OPEN(bus->state)) {
|
|
|
|
r = -ENOTCONN;
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-19 21:41:21 +01:00
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_bus_message_new_method_call(bus, &m, destination, path, "org.freedesktop.DBus.Properties", "Set");
|
2013-10-11 20:18:10 +02:00
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-10-11 20:18:10 +02:00
|
|
|
|
|
|
|
r = sd_bus_message_append(m, "ss", strempty(interface), member);
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-10-11 20:18:10 +02:00
|
|
|
|
|
|
|
r = sd_bus_message_open_container(m, 'v', type);
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-10-11 20:18:10 +02:00
|
|
|
|
|
|
|
va_start(ap, type);
|
|
|
|
r = bus_message_append_ap(m, type, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-10-11 20:18:10 +02:00
|
|
|
|
|
|
|
r = sd_bus_message_close_container(m);
|
|
|
|
if (r < 0)
|
2015-08-16 22:13:34 +02:00
|
|
|
goto fail;
|
2013-10-11 20:18:10 +02:00
|
|
|
|
2013-11-11 18:55:34 +01:00
|
|
|
return sd_bus_call(bus, m, 0, error, NULL);
|
2015-08-16 22:13:34 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-10-11 20:18:10 +02:00
|
|
|
}
|
2013-11-28 17:50:02 +01:00
|
|
|
|
|
|
|
_public_ int sd_bus_query_sender_creds(sd_bus_message *call, uint64_t mask, sd_bus_creds **creds) {
|
|
|
|
sd_bus_creds *c;
|
|
|
|
|
|
|
|
assert_return(call, -EINVAL);
|
|
|
|
assert_return(call->sealed, -EPERM);
|
2014-03-19 21:41:21 +01:00
|
|
|
assert_return(call->bus, -EINVAL);
|
2013-11-28 17:50:02 +01:00
|
|
|
assert_return(!bus_pid_changed(call->bus), -ECHILD);
|
|
|
|
|
2014-03-19 21:41:21 +01:00
|
|
|
if (!BUS_IS_OPEN(call->bus->state))
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
c = sd_bus_message_get_creds(call);
|
|
|
|
|
|
|
|
/* All data we need? */
|
|
|
|
if (c && (mask & ~c->mask) == 0) {
|
|
|
|
*creds = sd_bus_creds_ref(c);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No data passed? Or not enough data passed to retrieve the missing bits? */
|
|
|
|
if (!c || !(c->mask & SD_BUS_CREDS_PID)) {
|
|
|
|
/* We couldn't read anything from the call, let's try
|
2015-04-16 17:27:51 +02:00
|
|
|
* to get it from the sender or peer. */
|
2013-11-28 17:50:02 +01:00
|
|
|
|
|
|
|
if (call->sender)
|
2015-04-16 17:27:51 +02:00
|
|
|
/* There's a sender, but the creds are
|
|
|
|
* missing. This means we are talking via
|
|
|
|
* dbus1, or are getting a message that was
|
|
|
|
* sent to us via kdbus, but was converted
|
|
|
|
* from a dbus1 message by the bus-proxy and
|
|
|
|
* thus also lacks the creds. */
|
2014-10-22 19:17:24 +02:00
|
|
|
return sd_bus_get_name_creds(call->bus, call->sender, mask, creds);
|
2013-11-28 17:50:02 +01:00
|
|
|
else
|
2015-04-16 17:27:51 +02:00
|
|
|
/* There's no sender, hence we are on a dbus1
|
|
|
|
* direct connection. For direct connections
|
|
|
|
* the credentials of the AF_UNIX peer matter,
|
|
|
|
* which may be queried via
|
|
|
|
* sd_bus_get_owner_creds(). */
|
2014-10-22 19:17:24 +02:00
|
|
|
return sd_bus_get_owner_creds(call->bus, mask, creds);
|
2013-11-28 17:50:02 +01:00
|
|
|
}
|
|
|
|
|
2013-11-30 04:14:10 +01:00
|
|
|
return bus_creds_extend_by_pid(c, mask, creds);
|
2013-11-28 17:50:02 +01:00
|
|
|
}
|
2014-08-15 20:08:51 +02:00
|
|
|
|
|
|
|
_public_ int sd_bus_query_sender_privilege(sd_bus_message *call, int capability) {
|
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_creds_unrefp) sd_bus_creds *creds = NULL;
|
2014-08-15 20:08:51 +02:00
|
|
|
uid_t our_uid;
|
sd-bus: update to current kernel version, by splitting off the extended KDBUS_ITEM_PIDS structure from KDBUS_ITEM_CREDS
Also:
- adds support for euid, suid, fsuid, egid, sgid, fsgid fields.
- makes augmentation of creds with data from /proc explicitly
controllable to give apps better control over this, given that this is
racy.
- enables augmentation for kdbus connections (previously we only did it
for dbus1). This is useful since with recent kdbus versions it is
possible for clients to control the metadata they want to send.
- changes sd_bus_query_sender_privilege() to take the euid of the client
into consideration, if known
- when we don't have permissions to read augmentation data from /proc,
don't fail, just don't add the data in
2014-11-24 21:41:40 +01:00
|
|
|
bool know_caps = false;
|
2014-08-15 20:08:51 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(call, -EINVAL);
|
|
|
|
assert_return(call->sealed, -EPERM);
|
|
|
|
assert_return(call->bus, -EINVAL);
|
|
|
|
assert_return(!bus_pid_changed(call->bus), -ECHILD);
|
|
|
|
|
|
|
|
if (!BUS_IS_OPEN(call->bus->state))
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
sd-bus: update to current kernel version, by splitting off the extended KDBUS_ITEM_PIDS structure from KDBUS_ITEM_CREDS
Also:
- adds support for euid, suid, fsuid, egid, sgid, fsgid fields.
- makes augmentation of creds with data from /proc explicitly
controllable to give apps better control over this, given that this is
racy.
- enables augmentation for kdbus connections (previously we only did it
for dbus1). This is useful since with recent kdbus versions it is
possible for clients to control the metadata they want to send.
- changes sd_bus_query_sender_privilege() to take the euid of the client
into consideration, if known
- when we don't have permissions to read augmentation data from /proc,
don't fail, just don't add the data in
2014-11-24 21:41:40 +01:00
|
|
|
if (capability >= 0) {
|
2015-04-21 00:58:08 +02:00
|
|
|
|
sd-bus: update to current kernel version, by splitting off the extended KDBUS_ITEM_PIDS structure from KDBUS_ITEM_CREDS
Also:
- adds support for euid, suid, fsuid, egid, sgid, fsgid fields.
- makes augmentation of creds with data from /proc explicitly
controllable to give apps better control over this, given that this is
racy.
- enables augmentation for kdbus connections (previously we only did it
for dbus1). This is useful since with recent kdbus versions it is
possible for clients to control the metadata they want to send.
- changes sd_bus_query_sender_privilege() to take the euid of the client
into consideration, if known
- when we don't have permissions to read augmentation data from /proc,
don't fail, just don't add the data in
2014-11-24 21:41:40 +01:00
|
|
|
r = sd_bus_query_sender_creds(call, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS, &creds);
|
2014-08-15 20:08:51 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-04-21 00:58:08 +02:00
|
|
|
/* We cannot use augmented caps for authorization,
|
|
|
|
* since then data is acquired raceful from
|
|
|
|
* /proc. This can never actually happen, but let's
|
|
|
|
* better be safe than sorry, and do an extra check
|
|
|
|
* here. */
|
|
|
|
assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EFFECTIVE_CAPS) == 0, -EPERM);
|
|
|
|
|
sd-bus: update to current kernel version, by splitting off the extended KDBUS_ITEM_PIDS structure from KDBUS_ITEM_CREDS
Also:
- adds support for euid, suid, fsuid, egid, sgid, fsgid fields.
- makes augmentation of creds with data from /proc explicitly
controllable to give apps better control over this, given that this is
racy.
- enables augmentation for kdbus connections (previously we only did it
for dbus1). This is useful since with recent kdbus versions it is
possible for clients to control the metadata they want to send.
- changes sd_bus_query_sender_privilege() to take the euid of the client
into consideration, if known
- when we don't have permissions to read augmentation data from /proc,
don't fail, just don't add the data in
2014-11-24 21:41:40 +01:00
|
|
|
/* Note that not even on kdbus we might have the caps
|
|
|
|
* field, due to faked identities, or namespace
|
|
|
|
* translation issues. */
|
2014-08-15 20:08:51 +02:00
|
|
|
r = sd_bus_creds_has_effective_cap(creds, capability);
|
|
|
|
if (r > 0)
|
|
|
|
return 1;
|
sd-bus: update to current kernel version, by splitting off the extended KDBUS_ITEM_PIDS structure from KDBUS_ITEM_CREDS
Also:
- adds support for euid, suid, fsuid, egid, sgid, fsgid fields.
- makes augmentation of creds with data from /proc explicitly
controllable to give apps better control over this, given that this is
racy.
- enables augmentation for kdbus connections (previously we only did it
for dbus1). This is useful since with recent kdbus versions it is
possible for clients to control the metadata they want to send.
- changes sd_bus_query_sender_privilege() to take the euid of the client
into consideration, if known
- when we don't have permissions to read augmentation data from /proc,
don't fail, just don't add the data in
2014-11-24 21:41:40 +01:00
|
|
|
if (r == 0)
|
|
|
|
know_caps = true;
|
2014-08-15 20:08:51 +02:00
|
|
|
} else {
|
sd-bus: update to current kernel version, by splitting off the extended KDBUS_ITEM_PIDS structure from KDBUS_ITEM_CREDS
Also:
- adds support for euid, suid, fsuid, egid, sgid, fsgid fields.
- makes augmentation of creds with data from /proc explicitly
controllable to give apps better control over this, given that this is
racy.
- enables augmentation for kdbus connections (previously we only did it
for dbus1). This is useful since with recent kdbus versions it is
possible for clients to control the metadata they want to send.
- changes sd_bus_query_sender_privilege() to take the euid of the client
into consideration, if known
- when we don't have permissions to read augmentation data from /proc,
don't fail, just don't add the data in
2014-11-24 21:41:40 +01:00
|
|
|
r = sd_bus_query_sender_creds(call, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID, &creds);
|
2014-08-15 20:08:51 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, check the UID, but only if the capability check wasn't
|
|
|
|
* sufficient */
|
|
|
|
our_uid = getuid();
|
sd-bus: update to current kernel version, by splitting off the extended KDBUS_ITEM_PIDS structure from KDBUS_ITEM_CREDS
Also:
- adds support for euid, suid, fsuid, egid, sgid, fsgid fields.
- makes augmentation of creds with data from /proc explicitly
controllable to give apps better control over this, given that this is
racy.
- enables augmentation for kdbus connections (previously we only did it
for dbus1). This is useful since with recent kdbus versions it is
possible for clients to control the metadata they want to send.
- changes sd_bus_query_sender_privilege() to take the euid of the client
into consideration, if known
- when we don't have permissions to read augmentation data from /proc,
don't fail, just don't add the data in
2014-11-24 21:41:40 +01:00
|
|
|
if (our_uid != 0 || !know_caps || capability < 0) {
|
2014-08-15 20:08:51 +02:00
|
|
|
uid_t sender_uid;
|
|
|
|
|
2015-04-21 00:58:08 +02:00
|
|
|
/* We cannot use augmented uid/euid for authorization,
|
|
|
|
* since then data is acquired raceful from
|
|
|
|
* /proc. This can never actually happen, but let's
|
|
|
|
* better be safe than sorry, and do an extra check
|
|
|
|
* here. */
|
|
|
|
assert_return((sd_bus_creds_get_augmented_mask(creds) & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID)) == 0, -EPERM);
|
|
|
|
|
sd-bus: update to current kernel version, by splitting off the extended KDBUS_ITEM_PIDS structure from KDBUS_ITEM_CREDS
Also:
- adds support for euid, suid, fsuid, egid, sgid, fsgid fields.
- makes augmentation of creds with data from /proc explicitly
controllable to give apps better control over this, given that this is
racy.
- enables augmentation for kdbus connections (previously we only did it
for dbus1). This is useful since with recent kdbus versions it is
possible for clients to control the metadata they want to send.
- changes sd_bus_query_sender_privilege() to take the euid of the client
into consideration, if known
- when we don't have permissions to read augmentation data from /proc,
don't fail, just don't add the data in
2014-11-24 21:41:40 +01:00
|
|
|
/* Try to use the EUID, if we have it. */
|
|
|
|
r = sd_bus_creds_get_euid(creds, &sender_uid);
|
|
|
|
if (r < 0)
|
|
|
|
r = sd_bus_creds_get_uid(creds, &sender_uid);
|
|
|
|
|
2014-08-15 20:08:51 +02:00
|
|
|
if (r >= 0) {
|
|
|
|
/* Sender has same UID as us, then let's grant access */
|
|
|
|
if (sender_uid == our_uid)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Sender is root, we are not root. */
|
|
|
|
if (our_uid != 0 && sender_uid == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|