mirror of
https://github.com/systemd/systemd.git
synced 2025-03-13 00:58:27 +03:00
sd-varlink/sd-json: add two new API calls (#36137)
These are kinda no-brainers, should have always existed. Split out of #36133 which needs them.
This commit is contained in:
commit
0d9ee9220b
@ -806,6 +806,7 @@ manpages = [
|
||||
'sd_journal_seek_tail'],
|
||||
''],
|
||||
['sd_journal_stream_fd', '3', ['sd_journal_stream_fd_with_namespace'], ''],
|
||||
['sd_json_variant_unset_field', '3', [], ''],
|
||||
['sd_listen_fds',
|
||||
'3',
|
||||
['SD_LISTEN_FDS_START', 'sd_listen_fds_with_names'],
|
||||
@ -889,6 +890,7 @@ manpages = [
|
||||
'sd_uid_get_sessions',
|
||||
'sd_uid_is_on_seat'],
|
||||
'HAVE_PAM'],
|
||||
['sd_varlink_set_description', '3', ['sd_varlink_get_description'], ''],
|
||||
['sd_watchdog_enabled', '3', [], ''],
|
||||
['shutdown', '8', [], ''],
|
||||
['smbios-type-11', '7', [], ''],
|
||||
|
95
man/sd_json_variant_unset_field.xml
Normal file
95
man/sd_json_variant_unset_field.xml
Normal file
@ -0,0 +1,95 @@
|
||||
<?xml version='1.0'?>
|
||||
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
|
||||
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
|
||||
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
|
||||
|
||||
<refentry id="sd_json_variant_unset_field" xmlns:xi="http://www.w3.org/2001/XInclude">
|
||||
|
||||
<refentryinfo>
|
||||
<title>sd_json_variant_unset_field</title>
|
||||
<productname>systemd</productname>
|
||||
</refentryinfo>
|
||||
|
||||
<refmeta>
|
||||
<refentrytitle>sd_json_variant_unset_field</refentrytitle>
|
||||
<manvolnum>3</manvolnum>
|
||||
</refmeta>
|
||||
|
||||
<refnamediv>
|
||||
<refname>sd_json_variant_unset_field</refname>
|
||||
<refpurpose>Remove a specific field from a JSON object</refpurpose>
|
||||
</refnamediv>
|
||||
|
||||
<refsynopsisdiv>
|
||||
<funcsynopsis>
|
||||
<funcsynopsisinfo>#include <systemd/sd-json.h></funcsynopsisinfo>
|
||||
|
||||
<funcprototype>
|
||||
<funcdef>int <function>sd_json_variant_unset_field</function></funcdef>
|
||||
<paramdef>sd_json_variant **<parameter>object</parameter></paramdef>
|
||||
<paramdef>const char *<parameter>field</parameter></paramdef>
|
||||
</funcprototype>
|
||||
</funcsynopsis>
|
||||
</refsynopsisdiv>
|
||||
|
||||
<refsect1>
|
||||
<title>Description</title>
|
||||
|
||||
<para><function>sd_json_variant_unset_field()</function> takes a pointer to a pointer to a JSON object
|
||||
and a field name. If the object contains a field under the specified name, a new JSON object is allocated
|
||||
that is a copy of the original one, however the specified field is removed. The object parameter is
|
||||
updated to point to the new object, which has an initial reference counter of one. The reference counter
|
||||
of the original object is decremented by one. Or in other words: the specified object is replaced by one
|
||||
with the field removed.</para>
|
||||
|
||||
<para>If the referenced object does not contain a field matching the specified name, no operation is
|
||||
executed.</para>
|
||||
|
||||
<para>Similar, if a <constant>NULL</constant> pointer is referenced, no operation is executed.</para>
|
||||
</refsect1>
|
||||
|
||||
<refsect1>
|
||||
<title>Return Value</title>
|
||||
|
||||
<para>On success, and if a field was removed <function>sd_json_variant_unset_field()</function> returns a
|
||||
positive non-zero integer. If no matching field was found it returns zero. On failure, it returns a
|
||||
negative errno-style error code.</para>
|
||||
|
||||
<refsect2>
|
||||
<title>Errors</title>
|
||||
|
||||
<para>Returned errors may indicate the following problems:</para>
|
||||
|
||||
<variablelist>
|
||||
<varlistentry>
|
||||
<term><constant>-EINVAL</constant></term>
|
||||
|
||||
<listitem><para>An argument is invalid.</para></listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><constant>-ENOMEM</constant></term>
|
||||
|
||||
<listitem><para>Memory allocation failed.</para></listitem>
|
||||
</varlistentry>
|
||||
</variablelist>
|
||||
</refsect2>
|
||||
</refsect1>
|
||||
|
||||
<xi:include href="libsystemd-pkgconfig.xml" />
|
||||
|
||||
<refsect1>
|
||||
<title>History</title>
|
||||
<para><function>sd_json_variant_unset_field()</function> was added in version 258.</para>
|
||||
</refsect1>
|
||||
|
||||
<refsect1>
|
||||
<title>See Also</title>
|
||||
|
||||
<para><simplelist type="inline">
|
||||
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
|
||||
<member><citerefentry><refentrytitle>sd-json</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
|
||||
</simplelist></para>
|
||||
</refsect1>
|
||||
|
||||
</refentry>
|
101
man/sd_varlink_set_description.xml
Normal file
101
man/sd_varlink_set_description.xml
Normal file
@ -0,0 +1,101 @@
|
||||
<?xml version='1.0'?>
|
||||
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
|
||||
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
|
||||
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
|
||||
|
||||
<refentry id="sd_varlink_set_description" xmlns:xi="http://www.w3.org/2001/XInclude">
|
||||
|
||||
<refentryinfo>
|
||||
<title>sd_varlink_set_description</title>
|
||||
<productname>systemd</productname>
|
||||
</refentryinfo>
|
||||
|
||||
<refmeta>
|
||||
<refentrytitle>sd_varlink_set_description</refentrytitle>
|
||||
<manvolnum>3</manvolnum>
|
||||
</refmeta>
|
||||
|
||||
<refnamediv>
|
||||
<refname>sd_varlink_set_description</refname>
|
||||
<refname>sd_varlink_get_description</refname>
|
||||
|
||||
<refpurpose>Set or query description of a Varlink connection object</refpurpose>
|
||||
</refnamediv>
|
||||
|
||||
<refsynopsisdiv>
|
||||
<funcsynopsis>
|
||||
<funcsynopsisinfo>#include <systemd/sd-link.h></funcsynopsisinfo>
|
||||
|
||||
<funcprototype>
|
||||
<funcdef>int <function>sd_varlink_set_description</function></funcdef>
|
||||
<paramdef>sd_varlink *<parameter>link</parameter></paramdef>
|
||||
<paramdef>const char *<parameter>description</parameter></paramdef>
|
||||
</funcprototype>
|
||||
|
||||
<funcprototype>
|
||||
<funcdef>const char* <function>sd_varlink_get_description</function></funcdef>
|
||||
<paramdef>sd_varlink *<parameter>link</parameter></paramdef>
|
||||
</funcprototype>
|
||||
</funcsynopsis>
|
||||
</refsynopsisdiv>
|
||||
|
||||
<refsect1>
|
||||
<title>Description</title>
|
||||
|
||||
<para><function>sd_varlink_set_description()</function> sets the description string that is used in
|
||||
logging to the specified string. The string is copied internally and freed when the Varlink connection
|
||||
object is deallocated. The <parameter>description</parameter> argument may be <constant>NULL</constant>,
|
||||
in which case the description is unset.</para>
|
||||
|
||||
<para><function>sd_varlink_get_description()</function> returns a description string for the specified
|
||||
Varlink connection. This string may have been previously set with
|
||||
<function>sd_varlink_set_description()</function>. If not set this way, a default string or
|
||||
<constant>NULL</constant> may be returned, depending how the connection was allocated and set up.</para>
|
||||
</refsect1>
|
||||
|
||||
<refsect1>
|
||||
<title>Return Value</title>
|
||||
|
||||
<para>On success, <function>sd_varlink_set_description()</function> returns a non-negative integer. On
|
||||
failure, it returns a negative errno-style error code. <function>sd_varlink_get_description()</function>
|
||||
returns either <constant>NULL</constant> or a pointer to the description string.</para>
|
||||
|
||||
<refsect2>
|
||||
<title>Errors</title>
|
||||
|
||||
<para>Returned errors may indicate the following problems:</para>
|
||||
|
||||
<variablelist>
|
||||
<varlistentry>
|
||||
<term><constant>-EINVAL</constant></term>
|
||||
|
||||
<listitem><para>An argument is invalid.</para></listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><constant>-ENOMEM</constant></term>
|
||||
|
||||
<listitem><para>Memory allocation failed.</para></listitem>
|
||||
</varlistentry>
|
||||
</variablelist>
|
||||
</refsect2>
|
||||
</refsect1>
|
||||
|
||||
<xi:include href="libsystemd-pkgconfig.xml" />
|
||||
|
||||
<refsect1>
|
||||
<title>History</title>
|
||||
<para><function>sd_varlink_set_description()</function> was added in version 257.</para>
|
||||
<para><function>sd_varlink_get_description()</function> was added in version 258.</para>
|
||||
</refsect1>
|
||||
|
||||
<refsect1>
|
||||
<title>See Also</title>
|
||||
|
||||
<para><simplelist type="inline">
|
||||
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
|
||||
<member><citerefentry><refentrytitle>sd-varlink</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
|
||||
</simplelist></para>
|
||||
</refsect1>
|
||||
|
||||
</refentry>
|
@ -1065,7 +1065,9 @@ global:
|
||||
sd_device_enumerator_add_all_parents;
|
||||
sd_json_variant_type_from_string;
|
||||
sd_json_variant_type_to_string;
|
||||
sd_json_variant_unset_field;
|
||||
sd_varlink_get_current_method;
|
||||
sd_varlink_get_description;
|
||||
sd_varlink_get_input_fd;
|
||||
sd_varlink_get_output_fd;
|
||||
sd_varlink_reset_fds;
|
||||
|
@ -2129,6 +2129,55 @@ _public_ int sd_json_variant_set_field_strv(sd_json_variant **v, const char *fie
|
||||
return sd_json_variant_set_field(v, field, m);
|
||||
}
|
||||
|
||||
_public_ int sd_json_variant_unset_field(sd_json_variant **v, const char *field) {
|
||||
int r;
|
||||
|
||||
assert_return(v, -EINVAL);
|
||||
assert_return(field, -EINVAL);
|
||||
|
||||
if (sd_json_variant_is_blank_object(*v))
|
||||
return 0;
|
||||
if (!sd_json_variant_is_object(*v))
|
||||
return -EINVAL;
|
||||
|
||||
_cleanup_free_ sd_json_variant **array = NULL;
|
||||
size_t k = 0;
|
||||
for (size_t i = 0; i < sd_json_variant_elements(*v); i += 2) {
|
||||
sd_json_variant *p;
|
||||
|
||||
p = sd_json_variant_by_index(*v, i);
|
||||
if (!sd_json_variant_is_string(p))
|
||||
return -EINVAL;
|
||||
|
||||
if (streq(sd_json_variant_string(p), field)) {
|
||||
if (!array) {
|
||||
array = new(sd_json_variant*, sd_json_variant_elements(*v) - 2);
|
||||
if (!array)
|
||||
return -ENOMEM;
|
||||
|
||||
for (k = 0; k < i; k++)
|
||||
array[k] = sd_json_variant_by_index(*v, k);
|
||||
}
|
||||
} else if (array) {
|
||||
array[k++] = p;
|
||||
array[k++] = sd_json_variant_by_index(*v, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!array)
|
||||
return 0;
|
||||
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
|
||||
r = sd_json_variant_new_object(&w, array, k);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
json_variant_propagate_sensitive(*v, w);
|
||||
JSON_VARIANT_REPLACE(*v, TAKE_PTR(w));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
_public_ int sd_json_variant_merge_object(sd_json_variant **v, sd_json_variant *m) {
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
|
||||
_cleanup_free_ sd_json_variant **array = NULL;
|
||||
|
@ -2916,6 +2916,12 @@ _public_ int sd_varlink_set_description(sd_varlink *v, const char *description)
|
||||
return free_and_strdup(&v->description, description);
|
||||
}
|
||||
|
||||
_public_ const char* sd_varlink_get_description(sd_varlink *v) {
|
||||
assert_return(v, NULL);
|
||||
|
||||
return v->description;
|
||||
}
|
||||
|
||||
static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
||||
sd_varlink *v = ASSERT_PTR(userdata);
|
||||
|
||||
|
@ -168,6 +168,8 @@ int sd_json_variant_set_field_unsigned(sd_json_variant **v, const char *field, u
|
||||
int sd_json_variant_set_field_boolean(sd_json_variant **v, const char *field, int b);
|
||||
int sd_json_variant_set_field_strv(sd_json_variant **v, const char *field, char **l);
|
||||
|
||||
int sd_json_variant_unset_field(sd_json_variant **v, const char *field);
|
||||
|
||||
sd_json_variant* sd_json_variant_find(sd_json_variant *haystack, sd_json_variant *needle);
|
||||
|
||||
int sd_json_variant_append_array(sd_json_variant **v, sd_json_variant *element);
|
||||
|
@ -215,6 +215,7 @@ int sd_varlink_set_relative_timeout(sd_varlink *v, uint64_t usec);
|
||||
sd_varlink_server* sd_varlink_get_server(sd_varlink *v);
|
||||
|
||||
int sd_varlink_set_description(sd_varlink *v, const char *d);
|
||||
const char* sd_varlink_get_description(sd_varlink *v);
|
||||
|
||||
/* Automatically mark the parameters part of incoming messages as security sensitive */
|
||||
int sd_varlink_set_input_sensitive(sd_varlink *v);
|
||||
|
@ -1419,4 +1419,42 @@ TEST(fd_info) {
|
||||
pidref_done(&pidref);
|
||||
}
|
||||
|
||||
TEST(json_variant_unset_field) {
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
|
||||
|
||||
ASSERT_OK_POSITIVE(sd_json_variant_is_blank_object(v));
|
||||
ASSERT_OK_ZERO(sd_json_variant_unset_field(&v, "foo"));
|
||||
ASSERT_OK_POSITIVE(sd_json_variant_is_blank_object(v));
|
||||
|
||||
ASSERT_NULL(v);
|
||||
|
||||
ASSERT_OK(sd_json_buildo(&v,
|
||||
SD_JSON_BUILD_PAIR_STRING("foo", "bar"),
|
||||
SD_JSON_BUILD_PAIR_STRING("quux", "waldo"),
|
||||
SD_JSON_BUILD_PAIR_STRING("piff", "paff")));
|
||||
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *w = sd_json_variant_ref(v);
|
||||
|
||||
ASSERT_OK_POSITIVE(sd_json_variant_equal(v, w));
|
||||
ASSERT_OK_ZERO(sd_json_variant_unset_field(&v, "fooxxx"));
|
||||
ASSERT_OK_POSITIVE(sd_json_variant_equal(v, w));
|
||||
ASSERT_OK_POSITIVE(sd_json_variant_unset_field(&v, "foo"));
|
||||
ASSERT_OK_ZERO(sd_json_variant_equal(v, w));
|
||||
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *x = NULL;
|
||||
ASSERT_OK(sd_json_buildo(&x,
|
||||
SD_JSON_BUILD_PAIR_STRING("quux", "waldo"),
|
||||
SD_JSON_BUILD_PAIR_STRING("piff", "paff")));
|
||||
ASSERT_OK_POSITIVE(sd_json_variant_equal(v, x));
|
||||
|
||||
ASSERT_OK_POSITIVE(sd_json_variant_unset_field(&v, "piff"));
|
||||
x = sd_json_variant_unref(x);
|
||||
ASSERT_OK(sd_json_buildo(&x,
|
||||
SD_JSON_BUILD_PAIR_STRING("quux", "waldo")));
|
||||
ASSERT_OK_POSITIVE(sd_json_variant_equal(x, v));
|
||||
|
||||
ASSERT_OK_POSITIVE(sd_json_variant_unset_field(&v, "quux"));
|
||||
ASSERT_OK_POSITIVE(sd_json_variant_is_blank_object(v));
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_DEBUG);
|
||||
|
Loading…
x
Reference in New Issue
Block a user