bus: make sd_bus_request_name() and sd_bus_release_name() behave more like other calls

Instead of returning an enum of return codes, make them return error
codes like kdbus does internally.

Also, document this behaviour so that clients can stick to it.

(Also rework bus-control.c to always have to functions for dbus1 vs.
kernel implementation of the various calls.)
This commit is contained in:
Lennart Poettering 2013-12-03 18:01:26 +01:00
parent 059d9fbb5a
commit e7176abbe8
11 changed files with 818 additions and 542 deletions

View file

@ -20,6 +20,7 @@ MANPAGES += \
man/sd-id128.3 \
man/sd-journal.3 \
man/sd_booted.3 \
man/sd_bus_request_name.3 \
man/sd_id128_get_machine.3 \
man/sd_id128_randomize.3 \
man/sd_id128_to_string.3 \
@ -131,6 +132,7 @@ MANPAGES_ALIAS += \
man/init.1 \
man/poweroff.8 \
man/reboot.8 \
man/sd_bus_release_name.3 \
man/sd_id128_equal.3 \
man/sd_id128_from_string.3 \
man/sd_id128_get_boot.3 \
@ -232,6 +234,7 @@ man/SD_WARNING.3: man/sd-daemon.3
man/init.1: man/systemd.1
man/poweroff.8: man/halt.8
man/reboot.8: man/halt.8
man/sd_bus_release_name.3: man/sd_bus_request_name.3
man/sd_id128_equal.3: man/sd-id128.3
man/sd_id128_from_string.3: man/sd_id128_to_string.3
man/sd_id128_get_boot.3: man/sd_id128_get_machine.3
@ -389,6 +392,9 @@ man/poweroff.html: man/halt.html
man/reboot.html: man/halt.html
$(html-alias)
man/sd_bus_release_name.html: man/sd_bus_request_name.html
$(html-alias)
man/sd_id128_equal.html: man/sd-id128.html
$(html-alias)

1
TODO
View file

@ -130,6 +130,7 @@ Features:
- longer term:
* priority queues
* priority inheritance
- sort out error codes for sd_bus_release_name()
* sd-event
- allow multiple signal handlers per signal

213
man/sd_bus_request_name.xml Normal file
View file

@ -0,0 +1,213 @@
<?xml version='1.0'?> <!--*-nxml-*-->
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
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/>.
-->
<refentry id="sd_bus_request_name">
<refentryinfo>
<title>sd_bus_request_name</title>
<productname>systemd</productname>
<authorgroup>
<author>
<contrib>Developer</contrib>
<firstname>Lennart</firstname>
<surname>Poettering</surname>
<email>lennart@poettering.net</email>
</author>
</authorgroup>
</refentryinfo>
<refmeta>
<refentrytitle>sd_bus_request_name</refentrytitle>
<manvolnum>3</manvolnum>
</refmeta>
<refnamediv>
<refname>sd_bus_request_name</refname>
<refname>sd_bus_release_name</refname>
<refpurpose>Request or release a well-known name on a bus</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
<funcprototype>
<funcdef>int <function>sd_bus_request_name</function></funcdef>
<paramdef>sd_bus* <parameter>bus</parameter></paramdef>
<paramdef>const char *<parameter>name</parameter></paramdef>
<paramdef>unsigned <parameter>flags</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_bus_release_name</function></funcdef>
<paramdef>sd_bus* <parameter>bus</parameter></paramdef>
<paramdef>const char *<parameter>name</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para><function>sd_bus_request_name()</function> requests
a well-known name on a bus. It takes a bus connection,
a valid bus name and a flags parameter. The flags
parameter is a combination of the following
flags:</para>
<variablelist>
<varlistentry>
<term><varname>SD_BUS_NAME_ALLOW_REPLACEMENT</varname></term>
<listitem><para>After acquiring the
name successuflly, permit other peers
to take over the name when they try to
acquire it with the
<varname>SD_BUS_NAME_REPLACE_EXISTING</varname>
flag set. If
<varname>SD_BUS_NAME_ALLOW_REPLACEMENT</varname>
is not set on the original request
such a request by other peers will be
denied.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>SD_BUS_NAME_REPLACE_EXISTING</varname></term>
<listitem><para>Take over a the name
if it is already acquired by a another
peer, and that other peer permitted
taking it over by setting
<varname>SD_BUS_NAME_ALLOW_REPLACEMENT</varname>
while acquiring it.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>SD_BUS_NAME_DO_NOT_QUEUE</varname></term>
<listitem><para>Do not queue name
acquisition when the name is already
taken.</para></listitem>
</varlistentry>
</variablelist>
<para><function>sd_bus_release_name()</function> releases
an acquired well-known name. It takes a bus connection
and a valid bus name as parameters.</para>
</refsect1>
<refsect1>
<title>Return Value</title>
<para>On success, these calls return 0 or a positive
integer. On failure, these calls return a negative
errno-style error code.</para>
<para><function>sd_bus_request_name()</function> will
return 0 when the name is already taken by another
peer and the client has been added to the queue for
the name, unless
<varname>SD_BUS_NAME_DO_NOT_QUEUE</varname> has been
set. The caller can subscribe to
<literal>NameOwnerChanged</literal> signals to be
notified when the name is successfully
acquired. <function>sd_bus_request_name()</function> returns
&gt; 0 when the name has been
acquired successfully.</para>
</refsect1>
<refsect1>
<title>Errors</title>
<para>Returned errors may indicate the following problems:</para>
<variablelist>
<varlistentry>
<term><varname>-EALREADY</varname></term>
<listitem><para>The caller already is
the owner of the specified
name.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>-EEXIST</varname></term>
<listitem><para>The name has already
been acquired by a different peer, and
SD_BUS_NAME_REPLACE_EXISTING was not
specified or the other peer did not
specify SD_BUS_NAME_ALLOW_REPLACEMENT
while acquiring the
name.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>-EINVAL</varname></term>
<listitem><para>A specified parameter
is invalid.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>-ENOTCONN</varname></term>
<listitem><para>The bus connection has
been disconnected.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>-ECHILD</varname></term>
<listitem><para>The bus connection has
been created in a different process
than the current.</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para>The <function>sd_bus_acquire_name()</function>
and <function>sd_bus_release_bame()</function>
interfaces are available as shared library, which can
be compiled and linked to with the
<constant>libsystemd-bus</constant> <citerefentry><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
file.</para>
</refsect1>
<refsect1>
<title>See Also</title>
<para>
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
</refsect1>
</refentry>

View file

@ -819,17 +819,12 @@ static int bus_setup_api(Manager *m, sd_bus *bus) {
* after the new connection is set up and the name installed
* to allow clients to synchronously wait for reexecution to
* finish */
r = sd_bus_request_name(bus,"org.freedesktop.systemd1", SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING);
r = sd_bus_request_name(bus,"org.freedesktop.systemd1", SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_DO_NOT_QUEUE);
if (r < 0) {
log_error("Failed to register name: %s", strerror(-r));
return r;
}
if (r != SD_BUS_NAME_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
return -EEXIST;
}
bus_list_names(m, bus);
log_debug("Successfully connected to API bus.");

View file

@ -574,17 +574,12 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
return r;
}
r = sd_bus_request_name(bus, "org.freedesktop.hostname1", SD_BUS_NAME_DO_NOT_QUEUE);
r = sd_bus_request_name(bus, "org.freedesktop.hostname1", SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_DO_NOT_QUEUE);
if (r < 0) {
log_error("Failed to register name: %s", strerror(-r));
return r;
}
if (r != SD_BUS_NAME_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
return -EEXIST;
}
r = sd_bus_attach_event(bus, event, 0);
if (r < 0) {
log_error("Failed to attach bus to event loop: %s", strerror(-r));

File diff suppressed because it is too large Load diff

View file

@ -1094,17 +1094,12 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
return r;
}
r = sd_bus_request_name(bus, "org.freedesktop.locale1", SD_BUS_NAME_DO_NOT_QUEUE);
r = sd_bus_request_name(bus, "org.freedesktop.locale1", SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_DO_NOT_QUEUE);
if (r < 0) {
log_error("Failed to register name: %s", strerror(-r));
return r;
}
if (r != SD_BUS_NAME_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
return -EEXIST;
}
r = sd_bus_attach_event(bus, event, 0);
if (r < 0) {
log_error("Failed to attach bus to event loop: %s", strerror(-r));

View file

@ -725,17 +725,12 @@ static int manager_connect_bus(Manager *m) {
return r;
}
r = sd_bus_request_name(m->bus, "org.freedesktop.login1", SD_BUS_NAME_DO_NOT_QUEUE);
r = sd_bus_request_name(m->bus, "org.freedesktop.login1", SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_DO_NOT_QUEUE);
if (r < 0) {
log_error("Failed to register name: %s", strerror(-r));
return r;
}
if (r != SD_BUS_NAME_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
return -EEXIST;
}
r = sd_bus_attach_event(m->bus, m->event, 0);
if (r < 0) {
log_error("Failed to attach bus to event loop: %s", strerror(-r));

View file

@ -217,17 +217,12 @@ static int manager_connect_bus(Manager *m) {
return r;
}
r = sd_bus_request_name(m->bus, "org.freedesktop.machine1", SD_BUS_NAME_DO_NOT_QUEUE);
r = sd_bus_request_name(m->bus, "org.freedesktop.machine1", SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_DO_NOT_QUEUE);
if (r < 0) {
log_error("Failed to register name: %s", strerror(-r));
return r;
}
if (r != SD_BUS_NAME_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
return -EEXIST;
}
r = sd_bus_attach_event(m->bus, m->event, 0);
if (r < 0) {
log_error("Failed to attach bus to event loop: %s", strerror(-r));

View file

@ -233,7 +233,7 @@ int sd_bus_message_rewind(sd_bus_message *m, int complete);
/* Bus management */
int sd_bus_get_unique_name(sd_bus *bus, const char **unique);
int sd_bus_request_name(sd_bus *bus, const char *name, int flags);
int sd_bus_request_name(sd_bus *bus, const char *name, unsigned flags);
int sd_bus_release_name(sd_bus *bus, const char *name);
int sd_bus_list_names(sd_bus *bus, char ***l); /* free the results */
int sd_bus_get_owner(sd_bus *bus, const char *name, uint64_t mask, sd_bus_creds **creds); /* unref the result! */

View file

@ -786,17 +786,12 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
return r;
}
r = sd_bus_request_name(bus, "org.freedesktop.timedate1", SD_BUS_NAME_DO_NOT_QUEUE);
r = sd_bus_request_name(bus, "org.freedesktop.timedate1", SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_DO_NOT_QUEUE);
if (r < 0) {
log_error("Failed to register name: %s", strerror(-r));
return r;
}
if (r != SD_BUS_NAME_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
return -EEXIST;
}
r = sd_bus_attach_event(bus, event, 0);
if (r < 0) {
log_error("Failed to attach bus to event loop: %s", strerror(-r));