1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-18 10:04:04 +03:00

Merge pull request #27347 from bluca/sd_bus_nonce

sd: avoid closing sd-bus in a fork, store module-global id for sd-bus/sd-session/sd-journal
This commit is contained in:
Lennart Poettering 2023-04-25 17:40:15 +02:00 committed by GitHub
commit d30d5a0374
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
56 changed files with 420 additions and 261 deletions

View File

@ -111,7 +111,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus was created in a different process.</para></listitem> <listitem><para>The bus was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -649,7 +649,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus was created in a different process.</para></listitem> <listitem><para>The bus was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -90,7 +90,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus was created in a different process.</para></listitem> <listitem><para>The bus was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -94,7 +94,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection has been created in a different process.</para></listitem> <listitem><para>The bus connection has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>
</refsect2> </refsect2>

View File

@ -96,7 +96,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection has been created in a different process.</para> <listitem><para>The bus connection has been created in a different process, library or module instance.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>

View File

@ -226,7 +226,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus was created in a different process.</para></listitem> <listitem><para>The bus was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -67,7 +67,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection has been created in a different process.</para></listitem> <listitem><para>The bus connection has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>
</refsect2> </refsect2>

View File

@ -78,7 +78,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection was created in a different process.</para></listitem> <listitem><para>The bus connection was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>

View File

@ -95,7 +95,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus was created in a different process.</para></listitem> <listitem><para>The bus was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -72,7 +72,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus was created in a different process.</para></listitem> <listitem><para>The bus was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -83,7 +83,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection has been created in a different process.</para></listitem> <listitem><para>The bus connection has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>
</refsect2> </refsect2>

View File

@ -79,7 +79,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus was created in a different process.</para></listitem> <listitem><para>The bus was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -137,7 +137,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus was created in a different process.</para></listitem> <listitem><para>The bus was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>
</refsect2> </refsect2>

View File

@ -103,7 +103,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus of <parameter>m</parameter> was created in a different process. <listitem><para>The bus of <parameter>m</parameter> was created in a different process, library or module instance.
</para></listitem> </para></listitem>
</varlistentry> </varlistentry>

View File

@ -84,7 +84,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection was created in a different process.</para> <listitem><para>The bus connection was created in a different process, library or module instance.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>

View File

@ -86,7 +86,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection has been created in a different process.</para></listitem> <listitem><para>The bus connection has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>
</refsect2> </refsect2>

View File

@ -202,7 +202,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus was created in a different process.</para></listitem> <listitem><para>The bus was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -92,7 +92,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection was created in a different process.</para></listitem> <listitem><para>The bus connection was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>
</refsect2> </refsect2>

View File

@ -75,7 +75,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection has been created in a different process.</para></listitem> <listitem><para>The bus connection has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -147,7 +147,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection has been created in a different process.</para></listitem> <listitem><para>The bus connection has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -92,7 +92,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection has been created in a different process.</para></listitem> <listitem><para>The bus connection has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>
</refsect2> </refsect2>

View File

@ -88,7 +88,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection has been created in a different process.</para></listitem> <listitem><para>The bus connection has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -289,7 +289,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -168,7 +168,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>

View File

@ -170,7 +170,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -270,7 +270,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -236,7 +236,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -165,7 +165,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -270,7 +270,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -109,7 +109,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop was created in a different process.</para></listitem> <listitem><para>The event loop was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -77,7 +77,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>

View File

@ -91,7 +91,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop object was created in a different process.</para></listitem> <listitem><para>The event loop object was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>
</refsect2> </refsect2>

View File

@ -129,7 +129,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -72,7 +72,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -108,7 +108,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -109,7 +109,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -106,7 +106,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -127,7 +127,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -89,7 +89,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -104,7 +104,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -139,7 +139,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -120,7 +120,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -291,7 +291,7 @@
<varlistentry> <varlistentry>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The event loop has been created in a different process.</para></listitem> <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>

View File

@ -168,7 +168,7 @@
<varlistentry id='ECHILD'> <varlistentry id='ECHILD'>
<term><constant>-ECHILD</constant></term> <term><constant>-ECHILD</constant></term>
<listitem><para>The journal object was created in a different process.</para></listitem> <listitem><para>The journal object was created in a different process, library or module instance.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry id='EADDRNOTAVAIL'> <varlistentry id='EADDRNOTAVAIL'>

36
src/basic/origin-id.h Normal file
View File

@ -0,0 +1,36 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <pthread.h>
#include "random-util.h"
/* This pattern needs to be repeated exactly in multiple modules, so macro it.
* To ensure an object is not passed into a different module (e.g.: when two shared objects statically
* linked to libsystemd get loaded in the same process, and the object created by one is passed to the
* other, see https://github.com/systemd/systemd/issues/27216), create a random static global random
* (mixed with PID, so that we can also check for reuse after fork) that is stored in the object and
* checked by public API on use. */
#define _DEFINE_ORIGIN_ID_HELPERS(type, name, scope) \
static uint64_t origin_id; \
\
static void origin_id_initialize(void) { \
origin_id = random_u64(); \
} \
\
static uint64_t origin_id_query(void) { \
static pthread_once_t once = PTHREAD_ONCE_INIT; \
assert_se(pthread_once(&once, origin_id_initialize) == 0); \
return origin_id ^ getpid_cached(); \
} \
\
scope bool name##_origin_changed(type *p) { \
assert(p); \
return p->origin_id != origin_id_query(); \
}
#define DEFINE_ORIGIN_ID_HELPERS(type, name) \
_DEFINE_ORIGIN_ID_HELPERS(type, name,);
#define DEFINE_PRIVATE_ORIGIN_ID_HELPERS(type, name) \
_DEFINE_ORIGIN_ID_HELPERS(type, name, static);

View File

@ -26,7 +26,7 @@ _public_ int sd_bus_get_unique_name(sd_bus *bus, const char **unique) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(unique, -EINVAL); assert_return(unique, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!bus->bus_client) if (!bus->bus_client)
return -EINVAL; return -EINVAL;
@ -89,7 +89,7 @@ _public_ int sd_bus_request_name(
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(name, -EINVAL); assert_return(name, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
r = validate_request_name_parameters(bus, name, flags, &param); r = validate_request_name_parameters(bus, name, flags, &param);
if (r < 0) if (r < 0)
@ -193,7 +193,7 @@ _public_ int sd_bus_request_name_async(
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(name, -EINVAL); assert_return(name, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
r = validate_request_name_parameters(bus, name, flags, &param); r = validate_request_name_parameters(bus, name, flags, &param);
if (r < 0) if (r < 0)
@ -247,7 +247,7 @@ _public_ int sd_bus_release_name(
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(name, -EINVAL); assert_return(name, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
r = validate_release_name_parameters(bus, name); r = validate_release_name_parameters(bus, name);
if (r < 0) if (r < 0)
@ -340,7 +340,7 @@ _public_ int sd_bus_release_name_async(
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(name, -EINVAL); assert_return(name, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
r = validate_release_name_parameters(bus, name); r = validate_release_name_parameters(bus, name);
if (r < 0) if (r < 0)
@ -367,7 +367,7 @@ _public_ int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatabl
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(acquired || activatable, -EINVAL); assert_return(acquired || activatable, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!bus->bus_client) if (!bus->bus_client)
return -EINVAL; return -EINVAL;
@ -438,7 +438,7 @@ _public_ int sd_bus_get_name_creds(
assert_return(name, -EINVAL); assert_return(name, -EINVAL);
assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP); assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP);
assert_return(mask == 0 || creds, -EINVAL); assert_return(mask == 0 || creds, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
assert_return(service_name_is_valid(name), -EINVAL); assert_return(service_name_is_valid(name), -EINVAL);
if (!bus->bus_client) if (!bus->bus_client)
@ -732,7 +732,7 @@ _public_ int sd_bus_get_owner_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **r
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP); assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP);
assert_return(ret, -EINVAL); assert_return(ret, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -903,7 +903,7 @@ _public_ int sd_bus_get_name_machine_id(sd_bus *bus, const char *name, sd_id128_
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(name, -EINVAL); assert_return(name, -EINVAL);
assert_return(machine, -EINVAL); assert_return(machine, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
assert_return(service_name_is_valid(name), -EINVAL); assert_return(service_name_is_valid(name), -EINVAL);
if (!bus->bus_client) if (!bus->bus_client)

View File

@ -12,7 +12,7 @@
_public_ int sd_bus_message_send(sd_bus_message *reply) { _public_ int sd_bus_message_send(sd_bus_message *reply) {
assert_return(reply, -EINVAL); assert_return(reply, -EINVAL);
assert_return(reply->bus, -EINVAL); assert_return(reply->bus, -EINVAL);
assert_return(!bus_pid_changed(reply->bus), -ECHILD); assert_return(!bus_origin_changed(reply->bus), -ECHILD);
return sd_bus_send(reply->bus, reply, NULL); return sd_bus_send(reply->bus, reply, NULL);
} }
@ -30,7 +30,7 @@ _public_ int sd_bus_emit_signal_tov(
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -109,7 +109,7 @@ _public_ int sd_bus_call_method_asyncv(
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -163,7 +163,7 @@ _public_ int sd_bus_call_methodv(
bus_assert_return(bus, -EINVAL, error); bus_assert_return(bus, -EINVAL, error);
bus_assert_return(bus = bus_resolve(bus), -ENOPKG, error); bus_assert_return(bus = bus_resolve(bus), -ENOPKG, error);
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error); bus_assert_return(!bus_origin_changed(bus), -ECHILD, error);
if (!BUS_IS_OPEN(bus->state)) { if (!BUS_IS_OPEN(bus->state)) {
r = -ENOTCONN; r = -ENOTCONN;
@ -217,7 +217,7 @@ _public_ int sd_bus_reply_method_returnv(
assert_return(call->sealed, -EPERM); assert_return(call->sealed, -EPERM);
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
assert_return(call->bus, -EINVAL); assert_return(call->bus, -EINVAL);
assert_return(!bus_pid_changed(call->bus), -ECHILD); assert_return(!bus_origin_changed(call->bus), -ECHILD);
if (!BUS_IS_OPEN(call->bus->state)) if (!BUS_IS_OPEN(call->bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -264,7 +264,7 @@ _public_ int sd_bus_reply_method_error(
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
assert_return(sd_bus_error_is_set(e), -EINVAL); assert_return(sd_bus_error_is_set(e), -EINVAL);
assert_return(call->bus, -EINVAL); assert_return(call->bus, -EINVAL);
assert_return(!bus_pid_changed(call->bus), -ECHILD); assert_return(!bus_origin_changed(call->bus), -ECHILD);
if (!BUS_IS_OPEN(call->bus->state)) if (!BUS_IS_OPEN(call->bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -291,7 +291,7 @@ _public_ int sd_bus_reply_method_errorfv(
assert_return(call->sealed, -EPERM); assert_return(call->sealed, -EPERM);
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
assert_return(call->bus, -EINVAL); assert_return(call->bus, -EINVAL);
assert_return(!bus_pid_changed(call->bus), -ECHILD); assert_return(!bus_origin_changed(call->bus), -ECHILD);
if (!BUS_IS_OPEN(call->bus->state)) if (!BUS_IS_OPEN(call->bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -331,7 +331,7 @@ _public_ int sd_bus_reply_method_errno(
assert_return(call->sealed, -EPERM); assert_return(call->sealed, -EPERM);
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
assert_return(call->bus, -EINVAL); assert_return(call->bus, -EINVAL);
assert_return(!bus_pid_changed(call->bus), -ECHILD); assert_return(!bus_origin_changed(call->bus), -ECHILD);
if (!BUS_IS_OPEN(call->bus->state)) if (!BUS_IS_OPEN(call->bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -359,7 +359,7 @@ _public_ int sd_bus_reply_method_errnofv(
assert_return(call->sealed, -EPERM); assert_return(call->sealed, -EPERM);
assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
assert_return(call->bus, -EINVAL); assert_return(call->bus, -EINVAL);
assert_return(!bus_pid_changed(call->bus), -ECHILD); assert_return(!bus_origin_changed(call->bus), -ECHILD);
if (!BUS_IS_OPEN(call->bus->state)) if (!BUS_IS_OPEN(call->bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -407,7 +407,7 @@ _public_ int sd_bus_get_property(
bus_assert_return(member_name_is_valid(member), -EINVAL, error); bus_assert_return(member_name_is_valid(member), -EINVAL, error);
bus_assert_return(reply, -EINVAL, error); bus_assert_return(reply, -EINVAL, error);
bus_assert_return(signature_is_single(type, false), -EINVAL, error); bus_assert_return(signature_is_single(type, false), -EINVAL, error);
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error); bus_assert_return(!bus_origin_changed(bus), -ECHILD, error);
if (!BUS_IS_OPEN(bus->state)) { if (!BUS_IS_OPEN(bus->state)) {
r = -ENOTCONN; r = -ENOTCONN;
@ -452,7 +452,7 @@ _public_ int sd_bus_get_property_trivial(
bus_assert_return(member_name_is_valid(member), -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(bus_type_is_trivial(type), -EINVAL, error);
bus_assert_return(ptr, -EINVAL, error); bus_assert_return(ptr, -EINVAL, error);
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error); bus_assert_return(!bus_origin_changed(bus), -ECHILD, error);
if (!BUS_IS_OPEN(bus->state)) { if (!BUS_IS_OPEN(bus->state)) {
r = -ENOTCONN; r = -ENOTCONN;
@ -496,7 +496,7 @@ _public_ int sd_bus_get_property_string(
bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -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(member_name_is_valid(member), -EINVAL, error);
bus_assert_return(ret, -EINVAL, error); bus_assert_return(ret, -EINVAL, error);
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error); bus_assert_return(!bus_origin_changed(bus), -ECHILD, error);
if (!BUS_IS_OPEN(bus->state)) { if (!BUS_IS_OPEN(bus->state)) {
r = -ENOTCONN; r = -ENOTCONN;
@ -545,7 +545,7 @@ _public_ int sd_bus_get_property_strv(
bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -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(member_name_is_valid(member), -EINVAL, error);
bus_assert_return(ret, -EINVAL, error); bus_assert_return(ret, -EINVAL, error);
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error); bus_assert_return(!bus_origin_changed(bus), -ECHILD, error);
if (!BUS_IS_OPEN(bus->state)) { if (!BUS_IS_OPEN(bus->state)) {
r = -ENOTCONN; r = -ENOTCONN;
@ -587,7 +587,7 @@ _public_ int sd_bus_set_propertyv(
bus_assert_return(isempty(interface) || interface_name_is_valid(interface), -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(member_name_is_valid(member), -EINVAL, error);
bus_assert_return(signature_is_single(type, false), -EINVAL, error); bus_assert_return(signature_is_single(type, false), -EINVAL, error);
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error); bus_assert_return(!bus_origin_changed(bus), -ECHILD, error);
if (!BUS_IS_OPEN(bus->state)) { if (!BUS_IS_OPEN(bus->state)) {
r = -ENOTCONN; r = -ENOTCONN;
@ -646,7 +646,7 @@ _public_ int sd_bus_query_sender_creds(sd_bus_message *call, uint64_t mask, sd_b
assert_return(call, -EINVAL); assert_return(call, -EINVAL);
assert_return(call->sealed, -EPERM); assert_return(call->sealed, -EPERM);
assert_return(call->bus, -EINVAL); assert_return(call->bus, -EINVAL);
assert_return(!bus_pid_changed(call->bus), -ECHILD); assert_return(!bus_origin_changed(call->bus), -ECHILD);
assert_return(ret, -EINVAL); assert_return(ret, -EINVAL);
if (!BUS_IS_OPEN(call->bus->state)) if (!BUS_IS_OPEN(call->bus->state))
@ -694,7 +694,7 @@ _public_ int sd_bus_query_sender_privilege(sd_bus_message *call, int capability)
assert_return(call, -EINVAL); assert_return(call, -EINVAL);
assert_return(call->sealed, -EPERM); assert_return(call->sealed, -EPERM);
assert_return(call->bus, -EINVAL); assert_return(call->bus, -EINVAL);
assert_return(!bus_pid_changed(call->bus), -ECHILD); assert_return(!bus_origin_changed(call->bus), -ECHILD);
if (!BUS_IS_OPEN(call->bus->state)) if (!BUS_IS_OPEN(call->bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -786,7 +786,7 @@ _public_ int sd_bus_match_signal(
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
assert_return(!sender || service_name_is_valid(sender), -EINVAL); assert_return(!sender || service_name_is_valid(sender), -EINVAL);
assert_return(!path || object_path_is_valid(path), -EINVAL); assert_return(!path || object_path_is_valid(path), -EINVAL);
assert_return(!interface || interface_name_is_valid(interface), -EINVAL); assert_return(!interface || interface_name_is_valid(interface), -EINVAL);
@ -812,7 +812,7 @@ _public_ int sd_bus_match_signal_async(
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
assert_return(!sender || service_name_is_valid(sender), -EINVAL); assert_return(!sender || service_name_is_valid(sender), -EINVAL);
assert_return(!path || object_path_is_valid(path), -EINVAL); assert_return(!path || object_path_is_valid(path), -EINVAL);
assert_return(!interface || interface_name_is_valid(interface), -EINVAL); assert_return(!interface || interface_name_is_valid(interface), -EINVAL);

View File

@ -286,7 +286,7 @@ struct sd_bus {
struct memfd_cache memfd_cache[MEMFD_CACHE_MAX]; struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
unsigned n_memfd_cache; unsigned n_memfd_cache;
pid_t original_pid; uint64_t origin_id;
pid_t busexec_pid; pid_t busexec_pid;
unsigned iteration_counter; unsigned iteration_counter;
@ -377,7 +377,7 @@ int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m);
int bus_rqueue_make_room(sd_bus *bus); int bus_rqueue_make_room(sd_bus *bus);
bool bus_pid_changed(sd_bus *bus); bool bus_origin_changed(sd_bus *bus);
char *bus_address_escape(const char *v); char *bus_address_escape(const char *v);

View File

@ -1610,7 +1610,7 @@ static int bus_add_object(
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(callback, -EINVAL); assert_return(callback, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
n = bus_node_allocate(bus, path); n = bus_node_allocate(bus, path);
if (!n) if (!n)
@ -1806,7 +1806,7 @@ static int add_object_vtable_internal(
assert_return(vtable[0].x.start.element_size == VTABLE_ELEMENT_SIZE_221 || assert_return(vtable[0].x.start.element_size == VTABLE_ELEMENT_SIZE_221 ||
vtable[0].x.start.element_size >= VTABLE_ELEMENT_SIZE_242, vtable[0].x.start.element_size >= VTABLE_ELEMENT_SIZE_242,
-EINVAL); -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
assert_return(!streq(interface, "org.freedesktop.DBus.Properties") && assert_return(!streq(interface, "org.freedesktop.DBus.Properties") &&
!streq(interface, "org.freedesktop.DBus.Introspectable") && !streq(interface, "org.freedesktop.DBus.Introspectable") &&
!streq(interface, "org.freedesktop.DBus.Peer") && !streq(interface, "org.freedesktop.DBus.Peer") &&
@ -2028,7 +2028,7 @@ _public_ int sd_bus_add_node_enumerator(
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(callback, -EINVAL); assert_return(callback, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
n = bus_node_allocate(bus, path); n = bus_node_allocate(bus, path);
if (!n) if (!n)
@ -2280,7 +2280,7 @@ _public_ int sd_bus_emit_properties_changed_strv(
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(interface_name_is_valid(interface), -EINVAL); assert_return(interface_name_is_valid(interface), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -2334,7 +2334,7 @@ _public_ int sd_bus_emit_properties_changed(
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(interface_name_is_valid(interface), -EINVAL); assert_return(interface_name_is_valid(interface), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -2523,7 +2523,7 @@ _public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -2703,7 +2703,7 @@ _public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -2862,7 +2862,7 @@ _public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, ch
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -2932,7 +2932,7 @@ _public_ int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const c
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -2950,7 +2950,7 @@ _public_ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path,
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -2986,7 +2986,7 @@ _public_ int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -3004,7 +3004,7 @@ _public_ int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const ch
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(object_path_is_valid(path), -EINVAL); assert_return(object_path_is_valid(path), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
n = bus_node_allocate(bus, path); n = bus_node_allocate(bus, path);
if (!n) if (!n)

View File

@ -38,6 +38,7 @@
#include "memory-util.h" #include "memory-util.h"
#include "missing_syscall.h" #include "missing_syscall.h"
#include "missing_threads.h" #include "missing_threads.h"
#include "origin-id.h"
#include "parse-util.h" #include "parse-util.h"
#include "path-util.h" #include "path-util.h"
#include "process-util.h" #include "process-util.h"
@ -232,6 +233,8 @@ static sd_bus* bus_free(sd_bus *b) {
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, bus_free); DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, bus_free);
DEFINE_ORIGIN_ID_HELPERS(sd_bus, bus);
_public_ int sd_bus_new(sd_bus **ret) { _public_ int sd_bus_new(sd_bus **ret) {
_cleanup_free_ sd_bus *b = NULL; _cleanup_free_ sd_bus *b = NULL;
@ -249,7 +252,7 @@ _public_ int sd_bus_new(sd_bus **ret) {
.message_version = 1, .message_version = 1,
.creds_mask = SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME, .creds_mask = SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME,
.accept_fd = true, .accept_fd = true,
.original_pid = getpid_cached(), .origin_id = origin_id_query(),
.n_groups = SIZE_MAX, .n_groups = SIZE_MAX,
.close_on_exit = true, .close_on_exit = true,
.ucred = UCRED_INVALID, .ucred = UCRED_INVALID,
@ -271,7 +274,7 @@ _public_ int sd_bus_set_address(sd_bus *bus, const char *address) {
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(address, -EINVAL); assert_return(address, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return free_and_strdup(&bus->address, address); return free_and_strdup(&bus->address, address);
} }
@ -282,7 +285,7 @@ _public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(input_fd >= 0, -EBADF); assert_return(input_fd >= 0, -EBADF);
assert_return(output_fd >= 0, -EBADF); assert_return(output_fd >= 0, -EBADF);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus->input_fd = input_fd; bus->input_fd = input_fd;
bus->output_fd = output_fd; bus->output_fd = output_fd;
@ -298,7 +301,7 @@ _public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const *argv) {
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(path, -EINVAL); assert_return(path, -EINVAL);
assert_return(!strv_isempty(argv), -EINVAL); assert_return(!strv_isempty(argv), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
a = strv_copy(argv); a = strv_copy(argv);
if (!a) if (!a)
@ -316,7 +319,7 @@ _public_ int sd_bus_set_bus_client(sd_bus *bus, int b) {
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus->patch_sender, -EPERM); assert_return(!bus->patch_sender, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus->bus_client = !!b; bus->bus_client = !!b;
return 0; return 0;
@ -326,7 +329,7 @@ _public_ int sd_bus_set_monitor(sd_bus *bus, int b) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus->is_monitor = !!b; bus->is_monitor = !!b;
return 0; return 0;
@ -336,7 +339,7 @@ _public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus->accept_fd = !!b; bus->accept_fd = !!b;
return 0; return 0;
@ -346,7 +349,7 @@ _public_ int sd_bus_negotiate_timestamp(sd_bus *bus, int b) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED), -EPERM); assert_return(!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED), -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
/* This is not actually supported by any of our transports these days, but we do honour it for synthetic /* This is not actually supported by any of our transports these days, but we do honour it for synthetic
* replies, and maybe one day classic D-Bus learns this too */ * replies, and maybe one day classic D-Bus learns this too */
@ -360,7 +363,7 @@ _public_ int sd_bus_negotiate_creds(sd_bus *bus, int b, uint64_t mask) {
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL); assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL);
assert_return(!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED), -EPERM); assert_return(!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED), -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
SET_FLAG(bus->creds_mask, mask, b); SET_FLAG(bus->creds_mask, mask, b);
@ -375,7 +378,7 @@ _public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL); assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus->is_server = !!b; bus->is_server = !!b;
bus->server_id = server_id; bus->server_id = server_id;
@ -386,7 +389,7 @@ _public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus->anonymous_auth = !!b; bus->anonymous_auth = !!b;
return 0; return 0;
@ -396,7 +399,7 @@ _public_ int sd_bus_set_trusted(sd_bus *bus, int b) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus->trusted = !!b; bus->trusted = !!b;
return 0; return 0;
@ -406,7 +409,7 @@ _public_ int sd_bus_set_description(sd_bus *bus, const char *description) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return free_and_strdup(&bus->description, description); return free_and_strdup(&bus->description, description);
} }
@ -414,7 +417,7 @@ _public_ int sd_bus_set_description(sd_bus *bus, const char *description) {
_public_ int sd_bus_set_allow_interactive_authorization(sd_bus *bus, int b) { _public_ int sd_bus_set_allow_interactive_authorization(sd_bus *bus, int b) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus->allow_interactive_authorization = !!b; bus->allow_interactive_authorization = !!b;
return 0; return 0;
@ -423,7 +426,7 @@ _public_ int sd_bus_set_allow_interactive_authorization(sd_bus *bus, int b) {
_public_ int sd_bus_get_allow_interactive_authorization(sd_bus *bus) { _public_ int sd_bus_get_allow_interactive_authorization(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return bus->allow_interactive_authorization; return bus->allow_interactive_authorization;
} }
@ -432,7 +435,7 @@ _public_ int sd_bus_set_watch_bind(sd_bus *bus, int b) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus->watch_bind = !!b; bus->watch_bind = !!b;
return 0; return 0;
@ -441,7 +444,7 @@ _public_ int sd_bus_set_watch_bind(sd_bus *bus, int b) {
_public_ int sd_bus_get_watch_bind(sd_bus *bus) { _public_ int sd_bus_get_watch_bind(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return bus->watch_bind; return bus->watch_bind;
} }
@ -450,7 +453,7 @@ _public_ int sd_bus_set_connected_signal(sd_bus *bus, int b) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus->connected_signal = !!b; bus->connected_signal = !!b;
return 0; return 0;
@ -459,7 +462,7 @@ _public_ int sd_bus_set_connected_signal(sd_bus *bus, int b) {
_public_ int sd_bus_get_connected_signal(sd_bus *bus) { _public_ int sd_bus_get_connected_signal(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return bus->connected_signal; return bus->connected_signal;
} }
@ -1197,7 +1200,7 @@ _public_ int sd_bus_start(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
bus_set_state(bus, BUS_OPENING); bus_set_state(bus, BUS_OPENING);
@ -1758,7 +1761,7 @@ _public_ void sd_bus_close(sd_bus *bus) {
return; return;
if (bus->state == BUS_CLOSED) if (bus->state == BUS_CLOSED)
return; return;
if (bus_pid_changed(bus)) if (bus_origin_changed(bus))
return; return;
/* Don't leave ssh hanging around */ /* Don't leave ssh hanging around */
@ -1779,6 +1782,8 @@ _public_ void sd_bus_close(sd_bus *bus) {
_public_ sd_bus *sd_bus_close_unref(sd_bus *bus) { _public_ sd_bus *sd_bus_close_unref(sd_bus *bus) {
if (!bus) if (!bus)
return NULL; return NULL;
if (bus_origin_changed(bus))
return NULL;
sd_bus_close(bus); sd_bus_close(bus);
@ -1788,6 +1793,8 @@ _public_ sd_bus *sd_bus_close_unref(sd_bus *bus) {
_public_ sd_bus* sd_bus_flush_close_unref(sd_bus *bus) { _public_ sd_bus* sd_bus_flush_close_unref(sd_bus *bus) {
if (!bus) if (!bus)
return NULL; return NULL;
if (bus_origin_changed(bus))
return NULL;
/* Have to do this before flush() to prevent hang */ /* Have to do this before flush() to prevent hang */
bus_kill_exec(bus); bus_kill_exec(bus);
@ -1805,14 +1812,37 @@ void bus_enter_closing(sd_bus *bus) {
bus_set_state(bus, BUS_CLOSING); bus_set_state(bus, BUS_CLOSING);
} }
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus, sd_bus, bus_free); /* Define manually so we can add the PID check */
_public_ sd_bus *sd_bus_ref(sd_bus *bus) {
if (!bus)
return NULL;
if (bus_origin_changed(bus))
return NULL;
bus->n_ref++;
return bus;
}
_public_ sd_bus* sd_bus_unref(sd_bus *bus) {
if (!bus)
return NULL;
if (bus_origin_changed(bus))
return NULL;
assert(bus->n_ref > 0);
if (--bus->n_ref > 0)
return NULL;
return bus_free(bus);
}
_public_ int sd_bus_is_open(sd_bus *bus) { _public_ int sd_bus_is_open(sd_bus *bus) {
if (!bus) if (!bus)
return 0; return 0;
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return BUS_IS_OPEN(bus->state); return BUS_IS_OPEN(bus->state);
} }
@ -1822,7 +1852,7 @@ _public_ int sd_bus_is_ready(sd_bus *bus) {
return 0; return 0;
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return bus->state == BUS_RUNNING; return bus->state == BUS_RUNNING;
} }
@ -1833,7 +1863,7 @@ _public_ int sd_bus_can_send(sd_bus *bus, char type) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state != BUS_UNSET, -ENOTCONN); assert_return(bus->state != BUS_UNSET, -ENOTCONN);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (bus->is_monitor) if (bus->is_monitor)
return 0; return 0;
@ -1858,7 +1888,7 @@ _public_ int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *id) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(id, -EINVAL); assert_return(id, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
r = bus_ensure_running(bus); r = bus_ensure_running(bus);
if (r < 0) if (r < 0)
@ -2111,7 +2141,7 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie) {
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
else else
assert_return(bus = m->bus, -ENOTCONN); assert_return(bus = m->bus, -ENOTCONN);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -2196,7 +2226,7 @@ _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destinat
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
else else
assert_return(bus = m->bus, -ENOTCONN); assert_return(bus = m->bus, -ENOTCONN);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -2264,7 +2294,7 @@ _public_ int sd_bus_call_async(
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
else else
assert_return(bus = m->bus, -ENOTCONN); assert_return(bus = m->bus, -ENOTCONN);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;
@ -2372,7 +2402,7 @@ _public_ int sd_bus_call(
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
else else
assert_return(bus = m->bus, -ENOTCONN); assert_return(bus = m->bus, -ENOTCONN);
bus_assert_return(!bus_pid_changed(bus), -ECHILD, error); bus_assert_return(!bus_origin_changed(bus), -ECHILD, error);
if (!BUS_IS_OPEN(bus->state)) { if (!BUS_IS_OPEN(bus->state)) {
r = -ENOTCONN; r = -ENOTCONN;
@ -2505,7 +2535,7 @@ _public_ int sd_bus_get_fd(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->input_fd == bus->output_fd, -EPERM); assert_return(bus->input_fd == bus->output_fd, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (bus->state == BUS_CLOSED) if (bus->state == BUS_CLOSED)
return -ENOTCONN; return -ENOTCONN;
@ -2524,7 +2554,7 @@ _public_ int sd_bus_get_events(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
switch (bus->state) { switch (bus->state) {
@ -2571,7 +2601,7 @@ _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(timeout_usec, -EINVAL); assert_return(timeout_usec, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING) if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
return -ENOTCONN; return -ENOTCONN;
@ -3210,7 +3240,7 @@ static int bus_process_internal(sd_bus *bus, sd_bus_message **ret) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
/* We don't allow recursively invoking sd_bus_process(). */ /* We don't allow recursively invoking sd_bus_process(). */
assert_return(!bus->current_message, -EBUSY); assert_return(!bus->current_message, -EBUSY);
@ -3344,7 +3374,7 @@ _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (bus->state == BUS_CLOSING) if (bus->state == BUS_CLOSING)
return 0; return 0;
@ -3367,7 +3397,7 @@ _public_ int sd_bus_flush(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (bus->state == BUS_CLOSING) if (bus->state == BUS_CLOSING)
return 0; return 0;
@ -3421,7 +3451,7 @@ _public_ int sd_bus_add_filter(
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(callback, -EINVAL); assert_return(callback, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
s = bus_slot_allocate(bus, !slot, BUS_FILTER_CALLBACK, sizeof(struct filter_callback), userdata); s = bus_slot_allocate(bus, !slot, BUS_FILTER_CALLBACK, sizeof(struct filter_callback), userdata);
if (!s) if (!s)
@ -3518,7 +3548,7 @@ static int bus_add_match_full(
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(match, -EINVAL); assert_return(match, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
CLEANUP_ARRAY(components, n_components, bus_match_parse_free); CLEANUP_ARRAY(components, n_components, bus_match_parse_free);
@ -3603,15 +3633,6 @@ _public_ int sd_bus_add_match_async(
return bus_add_match_full(bus, slot, true, match, callback, install_callback, userdata); return bus_add_match_full(bus, slot, true, match, callback, install_callback, userdata);
} }
bool bus_pid_changed(sd_bus *bus) {
assert(bus);
/* We don't support people creating a bus connection and
* keeping it around over a fork(). Let's complain. */
return bus->original_pid != getpid_cached();
}
static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) { static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
sd_bus *bus = ASSERT_PTR(userdata); sd_bus *bus = ASSERT_PTR(userdata);
int r; int r;
@ -3942,7 +3963,7 @@ _public_ int sd_bus_default(sd_bus **ret) {
_public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) { _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
assert_return(b, -EINVAL); assert_return(b, -EINVAL);
assert_return(tid, -EINVAL); assert_return(tid, -EINVAL);
assert_return(!bus_pid_changed(b), -ECHILD); assert_return(!bus_origin_changed(b), -ECHILD);
if (b->tid != 0) { if (b->tid != 0) {
*tid = b->tid; *tid = b->tid;
@ -4168,7 +4189,7 @@ _public_ int sd_bus_path_decode_many(const char *path, const char *path_template
_public_ int sd_bus_try_close(sd_bus *bus) { _public_ int sd_bus_try_close(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
@ -4178,7 +4199,7 @@ _public_ int sd_bus_get_description(sd_bus *bus, const char **description) {
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(description, -EINVAL); assert_return(description, -EINVAL);
assert_return(bus->description, -ENXIO); assert_return(bus->description, -ENXIO);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (bus->description) if (bus->description)
*description = bus->description; *description = bus->description;
@ -4192,7 +4213,7 @@ _public_ int sd_bus_get_scope(sd_bus *bus, const char **scope) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(scope, -EINVAL); assert_return(scope, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (bus->runtime_scope < 0) if (bus->runtime_scope < 0)
return -ENODATA; return -ENODATA;
@ -4205,7 +4226,7 @@ _public_ int sd_bus_get_address(sd_bus *bus, const char **address) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(address, -EINVAL); assert_return(address, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (bus->address) { if (bus->address) {
*address = bus->address; *address = bus->address;
@ -4219,7 +4240,7 @@ _public_ int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *mask) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(mask, -EINVAL); assert_return(mask, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
*mask = bus->creds_mask; *mask = bus->creds_mask;
return 0; return 0;
@ -4228,7 +4249,7 @@ _public_ int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *mask) {
_public_ int sd_bus_is_bus_client(sd_bus *bus) { _public_ int sd_bus_is_bus_client(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return bus->bus_client; return bus->bus_client;
} }
@ -4236,7 +4257,7 @@ _public_ int sd_bus_is_bus_client(sd_bus *bus) {
_public_ int sd_bus_is_server(sd_bus *bus) { _public_ int sd_bus_is_server(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return bus->is_server; return bus->is_server;
} }
@ -4244,7 +4265,7 @@ _public_ int sd_bus_is_server(sd_bus *bus) {
_public_ int sd_bus_is_anonymous(sd_bus *bus) { _public_ int sd_bus_is_anonymous(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return bus->anonymous_auth; return bus->anonymous_auth;
} }
@ -4252,7 +4273,7 @@ _public_ int sd_bus_is_anonymous(sd_bus *bus) {
_public_ int sd_bus_is_trusted(sd_bus *bus) { _public_ int sd_bus_is_trusted(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return bus->trusted; return bus->trusted;
} }
@ -4260,7 +4281,7 @@ _public_ int sd_bus_is_trusted(sd_bus *bus) {
_public_ int sd_bus_is_monitor(sd_bus *bus) { _public_ int sd_bus_is_monitor(sd_bus *bus) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
return bus->is_monitor; return bus->is_monitor;
} }
@ -4326,7 +4347,7 @@ _public_ int sd_bus_get_sender(sd_bus *bus, const char **ret) {
_public_ int sd_bus_get_n_queued_read(sd_bus *bus, uint64_t *ret) { _public_ int sd_bus_get_n_queued_read(sd_bus *bus, uint64_t *ret) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
assert_return(ret, -EINVAL); assert_return(ret, -EINVAL);
*ret = bus->rqueue_size; *ret = bus->rqueue_size;
@ -4336,7 +4357,7 @@ _public_ int sd_bus_get_n_queued_read(sd_bus *bus, uint64_t *ret) {
_public_ int sd_bus_get_n_queued_write(sd_bus *bus, uint64_t *ret) { _public_ int sd_bus_get_n_queued_write(sd_bus *bus, uint64_t *ret) {
assert_return(bus, -EINVAL); assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
assert_return(ret, -EINVAL); assert_return(ret, -EINVAL);
*ret = bus->wqueue_size; *ret = bus->wqueue_size;
@ -4398,7 +4419,7 @@ _public_ int sd_bus_enqueue_for_read(sd_bus *bus, sd_bus_message *m) {
assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(m, -EINVAL); assert_return(m, -EINVAL);
assert_return(m->sealed, -EINVAL); assert_return(m->sealed, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!bus_origin_changed(bus), -ECHILD);
if (!BUS_IS_OPEN(bus->state)) if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN; return -ENOTCONN;

View File

@ -1,11 +1,13 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include "sd-bus.h" #include "sd-bus.h"
#include "bus-internal.h" #include "bus-internal.h"
#include "bus-message.h" #include "bus-message.h"
#include "process-util.h"
#include "tests.h" #include "tests.h"
static bool use_system_bus = false; static bool use_system_bus = false;
@ -17,6 +19,30 @@ static void test_bus_new(void) {
assert_se(bus->n_ref == 1); assert_se(bus->n_ref == 1);
} }
static void test_bus_fork(void) {
_cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
int r;
assert_se(sd_bus_new(&bus) == 0);
assert_se(bus->n_ref == 1);
/* Check that after a fork the cleanup functions return NULL */
r = safe_fork("(bus-fork-test)", FORK_WAIT|FORK_LOG, NULL);
if (r == 0) {
assert_se(bus);
assert_se(sd_bus_is_ready(bus) == -ECHILD);
assert_se(sd_bus_flush_close_unref(bus) == NULL);
assert_se(sd_bus_close_unref(bus) == NULL);
assert_se(sd_bus_unref(bus) == NULL);
sd_bus_close(bus);
assert_se(bus->n_ref == 1);
_exit(EXIT_SUCCESS);
}
assert_se(r >= 0);
assert_se(bus->n_ref == 1);
}
static int test_bus_open(void) { static int test_bus_open(void) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r; int r;
@ -67,6 +93,7 @@ int main(int argc, char **argv) {
test_setup_logging(LOG_INFO); test_setup_logging(LOG_INFO);
test_bus_new(); test_bus_new();
test_bus_fork();
if (test_bus_open() < 0) if (test_bus_open() < 0)
return log_tests_skipped("Failed to connect to bus"); return log_tests_skipped("Failed to connect to bus");

View File

@ -25,6 +25,7 @@
#include "missing_magic.h" #include "missing_magic.h"
#include "missing_syscall.h" #include "missing_syscall.h"
#include "missing_threads.h" #include "missing_threads.h"
#include "origin-id.h"
#include "path-util.h" #include "path-util.h"
#include "prioq.h" #include "prioq.h"
#include "process-util.h" #include "process-util.h"
@ -144,7 +145,7 @@ struct sd_event {
/* A list of memory pressure event sources that still need their subscription string written */ /* A list of memory pressure event sources that still need their subscription string written */
LIST_HEAD(sd_event_source, memory_pressure_write_list); LIST_HEAD(sd_event_source, memory_pressure_write_list);
pid_t original_pid; uint64_t origin_id;
uint64_t iteration; uint64_t iteration;
triple_timestamp timestamp; triple_timestamp timestamp;
@ -174,6 +175,8 @@ struct sd_event {
unsigned delays[sizeof(usec_t) * 8]; unsigned delays[sizeof(usec_t) * 8];
}; };
DEFINE_PRIVATE_ORIGIN_ID_HELPERS(sd_event, event);
static thread_local sd_event *default_event = NULL; static thread_local sd_event *default_event = NULL;
static void source_disconnect(sd_event_source *s); static void source_disconnect(sd_event_source *s);
@ -410,7 +413,7 @@ _public_ int sd_event_new(sd_event** ret) {
.boottime_alarm.fd = -EBADF, .boottime_alarm.fd = -EBADF,
.boottime_alarm.next = USEC_INFINITY, .boottime_alarm.next = USEC_INFINITY,
.perturb = USEC_INFINITY, .perturb = USEC_INFINITY,
.original_pid = getpid_cached(), .origin_id = origin_id_query(),
}; };
r = prioq_ensure_allocated(&e->pending, pending_prioq_compare); r = prioq_ensure_allocated(&e->pending, pending_prioq_compare);
@ -439,7 +442,31 @@ fail:
return r; return r;
} }
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event, sd_event, event_free); /* Define manually so we can add the origin check */
_public_ sd_event *sd_event_ref(sd_event *e) {
if (!e)
return NULL;
if (event_origin_changed(e))
return NULL;
e->n_ref++;
return e;
}
_public_ sd_event* sd_event_unref(sd_event *e) {
if (!e)
return NULL;
if (event_origin_changed(e))
return NULL;
assert(e->n_ref > 0);
if (--e->n_ref > 0)
return NULL;
return event_free(e);
}
#define PROTECT_EVENT(e) \ #define PROTECT_EVENT(e) \
_unused_ _cleanup_(sd_event_unrefp) sd_event *_ref = sd_event_ref(e); _unused_ _cleanup_(sd_event_unrefp) sd_event *_ref = sd_event_ref(e);
@ -449,20 +476,11 @@ _public_ sd_event_source* sd_event_source_disable_unref(sd_event_source *s) {
return sd_event_source_unref(s); return sd_event_source_unref(s);
} }
static bool event_pid_changed(sd_event *e) {
assert(e);
/* We don't support people creating an event loop and keeping
* it around over a fork(). Let's complain. */
return e->original_pid != getpid_cached();
}
static void source_io_unregister(sd_event_source *s) { static void source_io_unregister(sd_event_source *s) {
assert(s); assert(s);
assert(s->type == SOURCE_IO); assert(s->type == SOURCE_IO);
if (event_pid_changed(s->event)) if (event_origin_changed(s->event))
return; return;
if (!s->io.registered) if (!s->io.registered)
@ -503,7 +521,7 @@ static void source_child_pidfd_unregister(sd_event_source *s) {
assert(s); assert(s);
assert(s->type == SOURCE_CHILD); assert(s->type == SOURCE_CHILD);
if (event_pid_changed(s->event)) if (event_origin_changed(s->event))
return; return;
if (!s->child.registered) if (!s->child.registered)
@ -542,7 +560,7 @@ static void source_memory_pressure_unregister(sd_event_source *s) {
assert(s); assert(s);
assert(s->type == SOURCE_MEMORY_PRESSURE); assert(s->type == SOURCE_MEMORY_PRESSURE);
if (event_pid_changed(s->event)) if (event_origin_changed(s->event))
return; return;
if (!s->memory_pressure.registered) if (!s->memory_pressure.registered)
@ -694,7 +712,7 @@ static int event_make_signal_data(
assert(e); assert(e);
if (event_pid_changed(e)) if (event_origin_changed(e))
return -ECHILD; return -ECHILD;
if (e->signal_sources && e->signal_sources[sig]) if (e->signal_sources && e->signal_sources[sig])
@ -791,7 +809,7 @@ static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig
return; return;
} }
if (event_pid_changed(e)) if (event_origin_changed(e))
return; return;
assert(d->fd >= 0); assert(d->fd >= 0);
@ -954,7 +972,7 @@ static void source_disconnect(sd_event_source *s) {
break; break;
case SOURCE_CHILD: case SOURCE_CHILD:
if (event_pid_changed(s->event)) if (event_origin_changed(s->event))
s->child.process_owned = false; s->child.process_owned = false;
if (s->child.pid > 0) { if (s->child.pid > 0) {
@ -1231,7 +1249,7 @@ _public_ int sd_event_add_io(
assert_return(fd >= 0, -EBADF); assert_return(fd >= 0, -EBADF);
assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL); assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (!callback) if (!callback)
callback = io_exit_callback; callback = io_exit_callback;
@ -1378,7 +1396,7 @@ _public_ int sd_event_add_time(
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(accuracy != UINT64_MAX, -EINVAL); assert_return(accuracy != UINT64_MAX, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (!clock_supported(clock)) /* Checks whether the kernel supports the clock */ if (!clock_supported(clock)) /* Checks whether the kernel supports the clock */
return -EOPNOTSUPP; return -EOPNOTSUPP;
@ -1465,7 +1483,7 @@ _public_ int sd_event_add_signal(
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
/* Let's make sure our special flag stays outside of the valid signal range */ /* Let's make sure our special flag stays outside of the valid signal range */
assert_cc(_NSIG < SD_EVENT_SIGNAL_PROCMASK); assert_cc(_NSIG < SD_EVENT_SIGNAL_PROCMASK);
@ -1575,7 +1593,7 @@ _public_ int sd_event_add_child(
assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL); assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
assert_return(options != 0, -EINVAL); assert_return(options != 0, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (!callback) if (!callback)
callback = child_exit_callback; callback = child_exit_callback;
@ -1673,7 +1691,7 @@ _public_ int sd_event_add_child_pidfd(
assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL); assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
assert_return(options != 0, -EINVAL); assert_return(options != 0, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (!callback) if (!callback)
callback = child_exit_callback; callback = child_exit_callback;
@ -1754,7 +1772,7 @@ _public_ int sd_event_add_defer(
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (!callback) if (!callback)
callback = generic_exit_callback; callback = generic_exit_callback;
@ -1790,7 +1808,7 @@ _public_ int sd_event_add_post(
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (!callback) if (!callback)
callback = generic_exit_callback; callback = generic_exit_callback;
@ -1828,7 +1846,7 @@ _public_ int sd_event_add_exit(
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(callback, -EINVAL); assert_return(callback, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
r = prioq_ensure_allocated(&e->exit, exit_prioq_compare); r = prioq_ensure_allocated(&e->exit, exit_prioq_compare);
if (r < 0) if (r < 0)
@ -1928,7 +1946,7 @@ _public_ int sd_event_add_memory_pressure(
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (!callback) if (!callback)
callback = memory_pressure_callback; callback = memory_pressure_callback;
@ -2126,7 +2144,7 @@ static void event_free_inotify_data(sd_event *e, struct inotify_data *d) {
assert_se(hashmap_remove(e->inotify_data, &d->priority) == d); assert_se(hashmap_remove(e->inotify_data, &d->priority) == d);
if (d->fd >= 0) { if (d->fd >= 0) {
if (!event_pid_changed(e) && if (!event_origin_changed(e) &&
epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, d->fd, NULL) < 0) epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, d->fd, NULL) < 0)
log_debug_errno(errno, "Failed to remove inotify fd from epoll, ignoring: %m"); log_debug_errno(errno, "Failed to remove inotify fd from epoll, ignoring: %m");
@ -2237,7 +2255,7 @@ static void event_free_inode_data(
if (d->inotify_data) { if (d->inotify_data) {
if (d->wd >= 0) { if (d->wd >= 0) {
if (d->inotify_data->fd >= 0 && !event_pid_changed(e)) { if (d->inotify_data->fd >= 0 && !event_origin_changed(e)) {
/* So here's a problem. At the time this runs the watch descriptor might already be /* So here's a problem. At the time this runs the watch descriptor might already be
* invalidated, because an IN_IGNORED event might be queued right the moment we enter * invalidated, because an IN_IGNORED event might be queued right the moment we enter
* the syscall. Hence, whenever we get EINVAL, ignore it entirely, since it's a very * the syscall. Hence, whenever we get EINVAL, ignore it entirely, since it's a very
@ -2444,7 +2462,7 @@ static int event_add_inotify_fd_internal(
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(fd >= 0, -EBADF); assert_return(fd >= 0, -EBADF);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (!callback) if (!callback)
callback = inotify_exit_callback; callback = inotify_exit_callback;
@ -2577,7 +2595,7 @@ DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event_source, sd_event_source, event_sou
_public_ int sd_event_source_set_description(sd_event_source *s, const char *description) { _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
return free_and_strdup(&s->description, description); return free_and_strdup(&s->description, description);
} }
@ -2585,7 +2603,7 @@ _public_ int sd_event_source_set_description(sd_event_source *s, const char *des
_public_ int sd_event_source_get_description(sd_event_source *s, const char **description) { _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(description, -EINVAL); assert_return(description, -EINVAL);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
if (!s->description) if (!s->description)
return -ENXIO; return -ENXIO;
@ -2596,6 +2614,7 @@ _public_ int sd_event_source_get_description(sd_event_source *s, const char **de
_public_ sd_event *sd_event_source_get_event(sd_event_source *s) { _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
assert_return(s, NULL); assert_return(s, NULL);
assert_return(!event_origin_changed(s->event), NULL);
return s->event; return s->event;
} }
@ -2604,7 +2623,7 @@ _public_ int sd_event_source_get_pending(sd_event_source *s) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type != SOURCE_EXIT, -EDOM); assert_return(s->type != SOURCE_EXIT, -EDOM);
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE); assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
return s->pending; return s->pending;
} }
@ -2612,7 +2631,7 @@ _public_ int sd_event_source_get_pending(sd_event_source *s) {
_public_ int sd_event_source_get_io_fd(sd_event_source *s) { _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_IO, -EDOM); assert_return(s->type == SOURCE_IO, -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
return s->io.fd; return s->io.fd;
} }
@ -2623,7 +2642,7 @@ _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(fd >= 0, -EBADF); assert_return(fd >= 0, -EBADF);
assert_return(s->type == SOURCE_IO, -EDOM); assert_return(s->type == SOURCE_IO, -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
if (s->io.fd == fd) if (s->io.fd == fd)
return 0; return 0;
@ -2656,6 +2675,7 @@ _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
_public_ int sd_event_source_get_io_fd_own(sd_event_source *s) { _public_ int sd_event_source_get_io_fd_own(sd_event_source *s) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_IO, -EDOM); assert_return(s->type == SOURCE_IO, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
return s->io.owned; return s->io.owned;
} }
@ -2663,6 +2683,7 @@ _public_ int sd_event_source_get_io_fd_own(sd_event_source *s) {
_public_ int sd_event_source_set_io_fd_own(sd_event_source *s, int own) { _public_ int sd_event_source_set_io_fd_own(sd_event_source *s, int own) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_IO, -EDOM); assert_return(s->type == SOURCE_IO, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
s->io.owned = own; s->io.owned = own;
return 0; return 0;
@ -2672,7 +2693,7 @@ _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events)
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(events, -EINVAL); assert_return(events, -EINVAL);
assert_return(s->type == SOURCE_IO, -EDOM); assert_return(s->type == SOURCE_IO, -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
*events = s->io.events; *events = s->io.events;
return 0; return 0;
@ -2685,7 +2706,7 @@ _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events)
assert_return(s->type == SOURCE_IO, -EDOM); assert_return(s->type == SOURCE_IO, -EDOM);
assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL); assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE); assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
/* edge-triggered updates are never skipped, so we can reset edges */ /* edge-triggered updates are never skipped, so we can reset edges */
if (s->io.events == events && !(events & EPOLLET)) if (s->io.events == events && !(events & EPOLLET))
@ -2711,7 +2732,7 @@ _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revent
assert_return(revents, -EINVAL); assert_return(revents, -EINVAL);
assert_return(s->type == SOURCE_IO, -EDOM); assert_return(s->type == SOURCE_IO, -EDOM);
assert_return(s->pending, -ENODATA); assert_return(s->pending, -ENODATA);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
*revents = s->io.revents; *revents = s->io.revents;
return 0; return 0;
@ -2720,14 +2741,14 @@ _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revent
_public_ int sd_event_source_get_signal(sd_event_source *s) { _public_ int sd_event_source_get_signal(sd_event_source *s) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_SIGNAL, -EDOM); assert_return(s->type == SOURCE_SIGNAL, -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
return s->signal.sig; return s->signal.sig;
} }
_public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) { _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
*priority = s->priority; *priority = s->priority;
return 0; return 0;
@ -2741,7 +2762,7 @@ _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority)
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE); assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
if (s->priority == priority) if (s->priority == priority)
return 0; return 0;
@ -2841,7 +2862,7 @@ _public_ int sd_event_source_get_enabled(sd_event_source *s, int *ret) {
return false; return false;
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
if (ret) if (ret)
*ret = s->enabled; *ret = s->enabled;
@ -3035,7 +3056,7 @@ _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
return 0; return 0;
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
/* If we are dead anyway, we are fine with turning off sources, but everything else needs to fail. */ /* If we are dead anyway, we are fine with turning off sources, but everything else needs to fail. */
if (s->event->state == SD_EVENT_FINISHED) if (s->event->state == SD_EVENT_FINISHED)
@ -3067,7 +3088,7 @@ _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(usec, -EINVAL); assert_return(usec, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM); assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
*usec = s->time.next; *usec = s->time.next;
return 0; return 0;
@ -3079,7 +3100,7 @@ _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM); assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE); assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
r = source_set_pending(s, false); r = source_set_pending(s, false);
if (r < 0) if (r < 0)
@ -3097,6 +3118,7 @@ _public_ int sd_event_source_set_time_relative(sd_event_source *s, uint64_t usec
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM); assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (usec == USEC_INFINITY) if (usec == USEC_INFINITY)
return sd_event_source_set_time(s, USEC_INFINITY); return sd_event_source_set_time(s, USEC_INFINITY);
@ -3116,7 +3138,7 @@ _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *use
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(usec, -EINVAL); assert_return(usec, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM); assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
*usec = s->time.accuracy; *usec = s->time.accuracy;
return 0; return 0;
@ -3129,7 +3151,7 @@ _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec
assert_return(usec != UINT64_MAX, -EINVAL); assert_return(usec != UINT64_MAX, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM); assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE); assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
r = source_set_pending(s, false); r = source_set_pending(s, false);
if (r < 0) if (r < 0)
@ -3148,7 +3170,7 @@ _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(clock, -EINVAL); assert_return(clock, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM); assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
*clock = event_source_type_to_clock(s->type); *clock = event_source_type_to_clock(s->type);
return 0; return 0;
@ -3158,7 +3180,7 @@ _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(pid, -EINVAL); assert_return(pid, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM); assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
*pid = s->child.pid; *pid = s->child.pid;
return 0; return 0;
@ -3167,7 +3189,7 @@ _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
_public_ int sd_event_source_get_child_pidfd(sd_event_source *s) { _public_ int sd_event_source_get_child_pidfd(sd_event_source *s) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM); assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
if (s->child.pidfd < 0) if (s->child.pidfd < 0)
return -EOPNOTSUPP; return -EOPNOTSUPP;
@ -3178,7 +3200,7 @@ _public_ int sd_event_source_get_child_pidfd(sd_event_source *s) {
_public_ int sd_event_source_send_child_signal(sd_event_source *s, int sig, const siginfo_t *si, unsigned flags) { _public_ int sd_event_source_send_child_signal(sd_event_source *s, int sig, const siginfo_t *si, unsigned flags) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM); assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
assert_return(SIGNAL_VALID(sig), -EINVAL); assert_return(SIGNAL_VALID(sig), -EINVAL);
/* If we already have seen indication the process exited refuse sending a signal early. This way we /* If we already have seen indication the process exited refuse sending a signal early. This way we
@ -3223,6 +3245,7 @@ _public_ int sd_event_source_send_child_signal(sd_event_source *s, int sig, cons
_public_ int sd_event_source_get_child_pidfd_own(sd_event_source *s) { _public_ int sd_event_source_get_child_pidfd_own(sd_event_source *s) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM); assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (s->child.pidfd < 0) if (s->child.pidfd < 0)
return -EOPNOTSUPP; return -EOPNOTSUPP;
@ -3233,6 +3256,7 @@ _public_ int sd_event_source_get_child_pidfd_own(sd_event_source *s) {
_public_ int sd_event_source_set_child_pidfd_own(sd_event_source *s, int own) { _public_ int sd_event_source_set_child_pidfd_own(sd_event_source *s, int own) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM); assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (s->child.pidfd < 0) if (s->child.pidfd < 0)
return -EOPNOTSUPP; return -EOPNOTSUPP;
@ -3244,6 +3268,7 @@ _public_ int sd_event_source_set_child_pidfd_own(sd_event_source *s, int own) {
_public_ int sd_event_source_get_child_process_own(sd_event_source *s) { _public_ int sd_event_source_get_child_process_own(sd_event_source *s) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM); assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
return s->child.process_owned; return s->child.process_owned;
} }
@ -3251,6 +3276,7 @@ _public_ int sd_event_source_get_child_process_own(sd_event_source *s) {
_public_ int sd_event_source_set_child_process_own(sd_event_source *s, int own) { _public_ int sd_event_source_set_child_process_own(sd_event_source *s, int own) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM); assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
s->child.process_owned = own; s->child.process_owned = own;
return 0; return 0;
@ -3260,7 +3286,7 @@ _public_ int sd_event_source_get_inotify_mask(sd_event_source *s, uint32_t *mask
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(mask, -EINVAL); assert_return(mask, -EINVAL);
assert_return(s->type == SOURCE_INOTIFY, -EDOM); assert_return(s->type == SOURCE_INOTIFY, -EDOM);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
*mask = s->inotify.mask; *mask = s->inotify.mask;
return 0; return 0;
@ -3272,7 +3298,7 @@ _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type != SOURCE_EXIT, -EDOM); assert_return(s->type != SOURCE_EXIT, -EDOM);
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE); assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(s->event), -ECHILD); assert_return(!event_origin_changed(s->event), -ECHILD);
if (s->prepare == callback) if (s->prepare == callback)
return 0; return 0;
@ -3300,6 +3326,7 @@ _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t
_public_ void* sd_event_source_get_userdata(sd_event_source *s) { _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
assert_return(s, NULL); assert_return(s, NULL);
assert_return(!event_origin_changed(s->event), NULL);
return s->userdata; return s->userdata;
} }
@ -3308,6 +3335,7 @@ _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata)
void *ret; void *ret;
assert_return(s, NULL); assert_return(s, NULL);
assert_return(!event_origin_changed(s->event), NULL);
ret = s->userdata; ret = s->userdata;
s->userdata = userdata; s->userdata = userdata;
@ -4423,7 +4451,7 @@ _public_ int sd_event_prepare(sd_event *e) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(e->state == SD_EVENT_INITIAL, -EBUSY); assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
@ -4662,7 +4690,7 @@ _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(e->state == SD_EVENT_ARMED, -EBUSY); assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
@ -4765,7 +4793,7 @@ _public_ int sd_event_dispatch(sd_event *e) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(e->state == SD_EVENT_PENDING, -EBUSY); assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
@ -4805,7 +4833,7 @@ _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(e->state == SD_EVENT_INITIAL, -EBUSY); assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
@ -4853,9 +4881,10 @@ _public_ int sd_event_loop(sd_event *e) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
assert_return(e->state == SD_EVENT_INITIAL, -EBUSY); assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
PROTECT_EVENT(e); PROTECT_EVENT(e);
while (e->state != SD_EVENT_FINISHED) { while (e->state != SD_EVENT_FINISHED) {
@ -4870,7 +4899,7 @@ _public_ int sd_event_loop(sd_event *e) {
_public_ int sd_event_get_fd(sd_event *e) { _public_ int sd_event_get_fd(sd_event *e) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
return e->epoll_fd; return e->epoll_fd;
} }
@ -4878,7 +4907,7 @@ _public_ int sd_event_get_fd(sd_event *e) {
_public_ int sd_event_get_state(sd_event *e) { _public_ int sd_event_get_state(sd_event *e) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
return e->state; return e->state;
} }
@ -4887,7 +4916,7 @@ _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(code, -EINVAL); assert_return(code, -EINVAL);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (!e->exit_requested) if (!e->exit_requested)
return -ENODATA; return -ENODATA;
@ -4900,7 +4929,7 @@ _public_ int sd_event_exit(sd_event *e, int code) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
e->exit_requested = true; e->exit_requested = true;
e->exit_code = code; e->exit_code = code;
@ -4912,7 +4941,7 @@ _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(usec, -EINVAL); assert_return(usec, -EINVAL);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (!TRIPLE_TIMESTAMP_HAS_CLOCK(clock)) if (!TRIPLE_TIMESTAMP_HAS_CLOCK(clock))
return -EOPNOTSUPP; return -EOPNOTSUPP;
@ -4955,7 +4984,7 @@ _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(tid, -EINVAL); assert_return(tid, -EINVAL);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (e->tid != 0) { if (e->tid != 0) {
*tid = e->tid; *tid = e->tid;
@ -4970,7 +4999,7 @@ _public_ int sd_event_set_watchdog(sd_event *e, int b) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
if (e->watchdog == !!b) if (e->watchdog == !!b)
return e->watchdog; return e->watchdog;
@ -5020,7 +5049,7 @@ fail:
_public_ int sd_event_get_watchdog(sd_event *e) { _public_ int sd_event_get_watchdog(sd_event *e) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
return e->watchdog; return e->watchdog;
} }
@ -5028,7 +5057,7 @@ _public_ int sd_event_get_watchdog(sd_event *e) {
_public_ int sd_event_get_iteration(sd_event *e, uint64_t *ret) { _public_ int sd_event_get_iteration(sd_event *e, uint64_t *ret) {
assert_return(e, -EINVAL); assert_return(e, -EINVAL);
assert_return(e = event_resolve(e), -ENOPKG); assert_return(e = event_resolve(e), -ENOPKG);
assert_return(!event_pid_changed(e), -ECHILD); assert_return(!event_origin_changed(e), -ECHILD);
*ret = e->iteration; *ret = e->iteration;
return 0; return 0;
@ -5036,6 +5065,8 @@ _public_ int sd_event_get_iteration(sd_event *e, uint64_t *ret) {
_public_ int sd_event_source_set_destroy_callback(sd_event_source *s, sd_event_destroy_t callback) { _public_ int sd_event_source_set_destroy_callback(sd_event_source *s, sd_event_destroy_t callback) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->event, -EINVAL);
assert_return(!event_origin_changed(s->event), -ECHILD);
s->destroy_callback = callback; s->destroy_callback = callback;
return 0; return 0;
@ -5043,6 +5074,7 @@ _public_ int sd_event_source_set_destroy_callback(sd_event_source *s, sd_event_d
_public_ int sd_event_source_get_destroy_callback(sd_event_source *s, sd_event_destroy_t *ret) { _public_ int sd_event_source_get_destroy_callback(sd_event_source *s, sd_event_destroy_t *ret) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (ret) if (ret)
*ret = s->destroy_callback; *ret = s->destroy_callback;
@ -5052,12 +5084,14 @@ _public_ int sd_event_source_get_destroy_callback(sd_event_source *s, sd_event_d
_public_ int sd_event_source_get_floating(sd_event_source *s) { _public_ int sd_event_source_get_floating(sd_event_source *s) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_origin_changed(s->event), -ECHILD);
return s->floating; return s->floating;
} }
_public_ int sd_event_source_set_floating(sd_event_source *s, int b) { _public_ int sd_event_source_set_floating(sd_event_source *s, int b) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (s->floating == !!b) if (s->floating == !!b)
return 0; return 0;
@ -5081,6 +5115,7 @@ _public_ int sd_event_source_set_floating(sd_event_source *s, int b) {
_public_ int sd_event_source_get_exit_on_failure(sd_event_source *s) { _public_ int sd_event_source_get_exit_on_failure(sd_event_source *s) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type != SOURCE_EXIT, -EDOM); assert_return(s->type != SOURCE_EXIT, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
return s->exit_on_failure; return s->exit_on_failure;
} }
@ -5088,6 +5123,7 @@ _public_ int sd_event_source_get_exit_on_failure(sd_event_source *s) {
_public_ int sd_event_source_set_exit_on_failure(sd_event_source *s, int b) { _public_ int sd_event_source_set_exit_on_failure(sd_event_source *s, int b) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type != SOURCE_EXIT, -EDOM); assert_return(s->type != SOURCE_EXIT, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (s->exit_on_failure == !!b) if (s->exit_on_failure == !!b)
return 0; return 0;
@ -5100,6 +5136,7 @@ _public_ int sd_event_source_set_ratelimit(sd_event_source *s, uint64_t interval
int r; int r;
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_origin_changed(s->event), -ECHILD);
/* Turning on ratelimiting on event source types that don't support it, is a loggable offense. Doing /* Turning on ratelimiting on event source types that don't support it, is a loggable offense. Doing
* so is a programming error. */ * so is a programming error. */
@ -5117,6 +5154,7 @@ _public_ int sd_event_source_set_ratelimit(sd_event_source *s, uint64_t interval
_public_ int sd_event_source_set_ratelimit_expire_callback(sd_event_source *s, sd_event_handler_t callback) { _public_ int sd_event_source_set_ratelimit_expire_callback(sd_event_source *s, sd_event_handler_t callback) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_origin_changed(s->event), -ECHILD);
s->ratelimit_expire_callback = callback; s->ratelimit_expire_callback = callback;
return 0; return 0;
@ -5124,6 +5162,7 @@ _public_ int sd_event_source_set_ratelimit_expire_callback(sd_event_source *s, s
_public_ int sd_event_source_get_ratelimit(sd_event_source *s, uint64_t *ret_interval, unsigned *ret_burst) { _public_ int sd_event_source_get_ratelimit(sd_event_source *s, uint64_t *ret_interval, unsigned *ret_burst) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_origin_changed(s->event), -ECHILD);
/* Querying whether an event source has ratelimiting configured is not a loggable offense, hence /* Querying whether an event source has ratelimiting configured is not a loggable offense, hence
* don't use assert_return(). Unlike turning on ratelimiting it's not really a programming error. */ * don't use assert_return(). Unlike turning on ratelimiting it's not really a programming error. */
@ -5143,6 +5182,7 @@ _public_ int sd_event_source_get_ratelimit(sd_event_source *s, uint64_t *ret_int
_public_ int sd_event_source_is_ratelimited(sd_event_source *s) { _public_ int sd_event_source_is_ratelimited(sd_event_source *s) {
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (!EVENT_SOURCE_CAN_RATE_LIMIT(s->type)) if (!EVENT_SOURCE_CAN_RATE_LIMIT(s->type))
return false; return false;
@ -5212,6 +5252,7 @@ _public_ int sd_event_source_set_memory_pressure_type(sd_event_source *s, const
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_MEMORY_PRESSURE, -EDOM); assert_return(s->type == SOURCE_MEMORY_PRESSURE, -EDOM);
assert_return(ty, -EINVAL); assert_return(ty, -EINVAL);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (!STR_IN_SET(ty, "some", "full")) if (!STR_IN_SET(ty, "some", "full"))
return -EINVAL; return -EINVAL;
@ -5253,6 +5294,7 @@ _public_ int sd_event_source_set_memory_pressure_period(sd_event_source *s, uint
assert_return(s, -EINVAL); assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_MEMORY_PRESSURE, -EDOM); assert_return(s->type == SOURCE_MEMORY_PRESSURE, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (threshold_usec <= 0 || threshold_usec >= UINT64_MAX) if (threshold_usec <= 0 || threshold_usec >= UINT64_MAX)
return -ERANGE; return -ERANGE;

View File

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h>
#include "sd-event.h" #include "sd-event.h"
@ -808,4 +809,23 @@ TEST(inotify_process_buffered_data) {
assert_se(sd_event_wait(e, 0) == 0); assert_se(sd_event_wait(e, 0) == 0);
} }
TEST(fork) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
int r;
assert_se(sd_event_default(&e) >= 0);
assert_se(sd_event_prepare(e) == 0);
/* Check that after a fork the cleanup functions return NULL */
r = safe_fork("(bus-fork-test)", FORK_WAIT|FORK_LOG, NULL);
if (r == 0) {
assert_se(e);
assert_se(sd_event_ref(e) == NULL);
assert_se(sd_event_unref(e) == NULL);
_exit(EXIT_SUCCESS);
}
assert_se(r >= 0);
}
DEFINE_TEST_MAIN(LOG_DEBUG); DEFINE_TEST_MAIN(LOG_DEBUG);

View File

@ -87,7 +87,7 @@ struct sd_journal {
Match *level0, *level1, *level2; Match *level0, *level1, *level2;
pid_t original_pid; uint64_t origin_id;
int inotify_fd; int inotify_fd;
unsigned current_invalidate_counter, last_invalidate_counter; unsigned current_invalidate_counter, last_invalidate_counter;

View File

@ -33,6 +33,7 @@
#include "list.h" #include "list.h"
#include "lookup3.h" #include "lookup3.h"
#include "nulstr-util.h" #include "nulstr-util.h"
#include "origin-id.h"
#include "path-util.h" #include "path-util.h"
#include "prioq.h" #include "prioq.h"
#include "process-util.h" #include "process-util.h"
@ -51,19 +52,12 @@
#define DEFAULT_DATA_THRESHOLD (64*1024) #define DEFAULT_DATA_THRESHOLD (64*1024)
DEFINE_PRIVATE_ORIGIN_ID_HELPERS(sd_journal, journal);
static void remove_file_real(sd_journal *j, JournalFile *f); static void remove_file_real(sd_journal *j, JournalFile *f);
static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f); static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f);
static void journal_file_unlink_newest_by_bood_id(sd_journal *j, JournalFile *f); static void journal_file_unlink_newest_by_bood_id(sd_journal *j, JournalFile *f);
static bool journal_pid_changed(sd_journal *j) {
assert(j);
/* We don't support people creating a journal object and
* keeping it around over a fork(). Let's complain. */
return j->original_pid != getpid_cached();
}
static int journal_put_error(sd_journal *j, int r, const char *path) { static int journal_put_error(sd_journal *j, int r, const char *path) {
_cleanup_free_ char *copy = NULL; _cleanup_free_ char *copy = NULL;
int k; int k;
@ -234,7 +228,7 @@ _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size)
uint64_t hash; uint64_t hash;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(data, -EINVAL); assert_return(data, -EINVAL);
if (size == 0) if (size == 0)
@ -330,7 +324,7 @@ fail:
_public_ int sd_journal_add_conjunction(sd_journal *j) { _public_ int sd_journal_add_conjunction(sd_journal *j) {
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
if (!j->level0) if (!j->level0)
return 0; return 0;
@ -349,7 +343,7 @@ _public_ int sd_journal_add_conjunction(sd_journal *j) {
_public_ int sd_journal_add_disjunction(sd_journal *j) { _public_ int sd_journal_add_disjunction(sd_journal *j) {
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
if (!j->level0) if (!j->level0)
return 0; return 0;
@ -406,7 +400,7 @@ char *journal_make_match_string(sd_journal *j) {
} }
_public_ void sd_journal_flush_matches(sd_journal *j) { _public_ void sd_journal_flush_matches(sd_journal *j) {
if (!j) if (!j || journal_origin_changed(j))
return; return;
if (j->level0) if (j->level0)
@ -930,7 +924,7 @@ static int real_journal_next(sd_journal *j, direction_t direction) {
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
r = iterated_cache_get(j->files_cache, NULL, &files, &n_files); r = iterated_cache_get(j->files_cache, NULL, &files, &n_files);
if (r < 0) if (r < 0)
@ -988,7 +982,7 @@ static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t
int c = 0, r; int c = 0, r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(skip <= INT_MAX, -ERANGE); assert_return(skip <= INT_MAX, -ERANGE);
if (skip == 0) { if (skip == 0) {
@ -1031,7 +1025,7 @@ _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(cursor, -EINVAL); assert_return(cursor, -EINVAL);
if (!j->current_file || j->current_file->current_offset <= 0) if (!j->current_file || j->current_file->current_offset <= 0)
@ -1064,7 +1058,7 @@ _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(!isempty(cursor), -EINVAL); assert_return(!isempty(cursor), -EINVAL);
for (const char *p = cursor;;) { for (const char *p = cursor;;) {
@ -1160,7 +1154,7 @@ _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
Object *o; Object *o;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(!isempty(cursor), -EINVAL); assert_return(!isempty(cursor), -EINVAL);
if (!j->current_file || j->current_file->current_offset <= 0) if (!j->current_file || j->current_file->current_offset <= 0)
@ -1239,7 +1233,7 @@ _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
_public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) { _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
detach_location(j); detach_location(j);
@ -1255,7 +1249,7 @@ _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, u
_public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) { _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
detach_location(j); detach_location(j);
@ -1270,7 +1264,7 @@ _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
_public_ int sd_journal_seek_head(sd_journal *j) { _public_ int sd_journal_seek_head(sd_journal *j) {
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
detach_location(j); detach_location(j);
@ -1283,7 +1277,7 @@ _public_ int sd_journal_seek_head(sd_journal *j) {
_public_ int sd_journal_seek_tail(sd_journal *j) { _public_ int sd_journal_seek_tail(sd_journal *j) {
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
detach_location(j); detach_location(j);
@ -2001,7 +1995,7 @@ static sd_journal *journal_new(int flags, const char *path, const char *namespac
return NULL; return NULL;
*j = (sd_journal) { *j = (sd_journal) {
.original_pid = getpid_cached(), .origin_id = origin_id_query(),
.toplevel_fd = -EBADF, .toplevel_fd = -EBADF,
.inotify_fd = -EBADF, .inotify_fd = -EBADF,
.flags = flags, .flags = flags,
@ -2254,7 +2248,7 @@ _public_ void sd_journal_close(sd_journal *j) {
Directory *d; Directory *d;
Prioq *p; Prioq *p;
if (!j) if (!j || journal_origin_changed(j))
return; return;
while ((p = hashmap_first(j->newest_by_boot_id))) while ((p = hashmap_first(j->newest_by_boot_id)))
@ -2470,7 +2464,7 @@ _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
f = j->current_file; f = j->current_file;
if (!f) if (!f)
@ -2498,7 +2492,7 @@ _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id12
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
f = j->current_file; f = j->current_file;
if (!f) if (!f)
@ -2543,7 +2537,7 @@ _public_ int sd_journal_get_seqnum(
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
f = j->current_file; f = j->current_file;
if (!f) if (!f)
@ -2597,7 +2591,7 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(field, -EINVAL); assert_return(field, -EINVAL);
assert_return(data, -EINVAL); assert_return(data, -EINVAL);
assert_return(size, -EINVAL); assert_return(size, -EINVAL);
@ -2654,7 +2648,7 @@ _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(data, -EINVAL); assert_return(data, -EINVAL);
assert_return(size, -EINVAL); assert_return(size, -EINVAL);
@ -2715,7 +2709,7 @@ _public_ int sd_journal_enumerate_available_data(sd_journal *j, const void **dat
} }
_public_ void sd_journal_restart_data(sd_journal *j) { _public_ void sd_journal_restart_data(sd_journal *j) {
if (!j) if (!j || journal_origin_changed(j))
return; return;
j->current_field = 0; j->current_field = 0;
@ -2743,7 +2737,7 @@ _public_ int sd_journal_get_fd(sd_journal *j) {
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
if (j->no_inotify) if (j->no_inotify)
return -EMEDIUMTYPE; return -EMEDIUMTYPE;
@ -2769,7 +2763,7 @@ _public_ int sd_journal_get_events(sd_journal *j) {
int fd; int fd;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
fd = sd_journal_get_fd(j); fd = sd_journal_get_fd(j);
if (fd < 0) if (fd < 0)
@ -2782,7 +2776,7 @@ _public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
int fd; int fd;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(timeout_usec, -EINVAL); assert_return(timeout_usec, -EINVAL);
fd = sd_journal_get_fd(j); fd = sd_journal_get_fd(j);
@ -2905,7 +2899,7 @@ _public_ int sd_journal_process(sd_journal *j) {
bool got_something = false; bool got_something = false;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
if (j->inotify_fd < 0) /* We have no inotify fd yet? Then there's noting to process. */ if (j->inotify_fd < 0) /* We have no inotify fd yet? Then there's noting to process. */
return 0; return 0;
@ -2937,7 +2931,7 @@ _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
uint64_t t; uint64_t t;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
if (j->inotify_fd < 0) { if (j->inotify_fd < 0) {
JournalFile *f; JournalFile *f;
@ -2990,7 +2984,7 @@ _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from,
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(from || to, -EINVAL); assert_return(from || to, -EINVAL);
assert_return(from != to, -EINVAL); assert_return(from != to, -EINVAL);
@ -3035,7 +3029,7 @@ _public_ int sd_journal_get_cutoff_monotonic_usec(
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(ret_from != ret_to, -EINVAL); assert_return(ret_from != ret_to, -EINVAL);
ORDERED_HASHMAP_FOREACH(f, j->files) { ORDERED_HASHMAP_FOREACH(f, j->files) {
@ -3088,7 +3082,7 @@ _public_ int sd_journal_get_usage(sd_journal *j, uint64_t *ret) {
uint64_t sum = 0; uint64_t sum = 0;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(ret, -EINVAL); assert_return(ret, -EINVAL);
ORDERED_HASHMAP_FOREACH(f, j->files) { ORDERED_HASHMAP_FOREACH(f, j->files) {
@ -3116,7 +3110,7 @@ _public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(!isempty(field), -EINVAL); assert_return(!isempty(field), -EINVAL);
assert_return(field_is_valid(field), -EINVAL); assert_return(field_is_valid(field), -EINVAL);
@ -3139,7 +3133,7 @@ _public_ int sd_journal_enumerate_unique(
size_t k; size_t k;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(j->unique_field, -EINVAL); assert_return(j->unique_field, -EINVAL);
k = strlen(j->unique_field); k = strlen(j->unique_field);
@ -3272,7 +3266,7 @@ _public_ int sd_journal_enumerate_available_unique(sd_journal *j, const void **d
} }
_public_ void sd_journal_restart_unique(sd_journal *j) { _public_ void sd_journal_restart_unique(sd_journal *j) {
if (!j) if (!j || journal_origin_changed(j))
return; return;
j->unique_file = NULL; j->unique_file = NULL;
@ -3284,7 +3278,7 @@ _public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(field, -EINVAL); assert_return(field, -EINVAL);
if (!j->fields_file) { if (!j->fields_file) {
@ -3423,7 +3417,7 @@ _public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
} }
_public_ void sd_journal_restart_fields(sd_journal *j) { _public_ void sd_journal_restart_fields(sd_journal *j) {
if (!j) if (!j || journal_origin_changed(j))
return; return;
j->fields_file = NULL; j->fields_file = NULL;
@ -3434,7 +3428,7 @@ _public_ void sd_journal_restart_fields(sd_journal *j) {
_public_ int sd_journal_reliable_fd(sd_journal *j) { _public_ int sd_journal_reliable_fd(sd_journal *j) {
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
return !j->on_network; return !j->on_network;
} }
@ -3466,7 +3460,7 @@ _public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
int r; int r;
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(ret, -EINVAL); assert_return(ret, -EINVAL);
r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size); r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
@ -3501,7 +3495,7 @@ _public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
_public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) { _public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
j->data_threshold = sz; j->data_threshold = sz;
return 0; return 0;
@ -3509,7 +3503,7 @@ _public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
_public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) { _public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
assert_return(j, -EINVAL); assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD); assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(sz, -EINVAL); assert_return(sz, -EINVAL);
*sz = j->data_threshold; *sz = j->data_threshold;

View File

@ -1,10 +1,14 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <unistd.h>
#include "sd-journal.h" #include "sd-journal.h"
#include "chattr-util.h" #include "chattr-util.h"
#include "journal-internal.h"
#include "log.h" #include "log.h"
#include "parse-util.h" #include "parse-util.h"
#include "process-util.h"
#include "rm-rf.h" #include "rm-rf.h"
#include "tests.h" #include "tests.h"
@ -35,6 +39,21 @@ int main(int argc, char *argv[]) {
r = sd_journal_open_directory(&j, t, 0); r = sd_journal_open_directory(&j, t, 0);
assert_se(r == 0); assert_se(r == 0);
assert_se(sd_journal_seek_head(j) == 0);
assert_se(j->current_location.type == LOCATION_HEAD);
r = safe_fork("(journal-fork-test)", FORK_WAIT|FORK_LOG, NULL);
if (r == 0) {
assert_se(j);
assert_se(sd_journal_get_realtime_usec(j, NULL) == -ECHILD);
assert_se(sd_journal_seek_tail(j) == -ECHILD);
assert_se(j->current_location.type == LOCATION_HEAD);
sd_journal_close(j);
_exit(EXIT_SUCCESS);
}
assert_se(r >= 0);
sd_journal_close(j); sd_journal_close(j);
j = NULL; j = NULL;