]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-util.c
Merge pull request #7540 from fbuihuu/systemd-delta-tweaks
[thirdparty/systemd.git] / src / core / dbus-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "bus-util.h"
22 #include "dbus-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "unit-printf.h"
26 #include "user-util.h"
27 #include "unit.h"
28
29 BUS_DEFINE_SET_TRANSIENT(mode_t, "u", uint32_t, mode_t, "%040o");
30 BUS_DEFINE_SET_TRANSIENT(unsigned, "u", uint32_t, unsigned, "%" PRIu32);
31 BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(user, valid_user_group_name_or_id);
32 BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(path, path_is_absolute);
33
34 int bus_set_transient_string(
35 Unit *u,
36 const char *name,
37 char **p,
38 sd_bus_message *message,
39 UnitWriteFlags flags,
40 sd_bus_error *error) {
41
42 const char *v;
43 int r;
44
45 assert(p);
46
47 r = sd_bus_message_read(message, "s", &v);
48 if (r < 0)
49 return r;
50
51 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
52 r = free_and_strdup(p, empty_to_null(v));
53 if (r < 0)
54 return r;
55
56 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
57 "%s=%s", name, strempty(v));
58 }
59
60 return 1;
61 }
62
63 int bus_set_transient_bool(
64 Unit *u,
65 const char *name,
66 bool *p,
67 sd_bus_message *message,
68 UnitWriteFlags flags,
69 sd_bus_error *error) {
70
71 int v, r;
72
73 assert(p);
74
75 r = sd_bus_message_read(message, "b", &v);
76 if (r < 0)
77 return r;
78
79 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
80 *p = v;
81 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(v));
82 }
83
84 return 1;
85 }
86
87 int bus_set_transient_usec_internal(
88 Unit *u,
89 const char *name,
90 usec_t *p,
91 bool fix_0,
92 sd_bus_message *message,
93 UnitWriteFlags flags,
94 sd_bus_error *error) {
95
96 uint64_t v;
97 int r;
98
99 assert(p);
100
101 r = sd_bus_message_read(message, "t", &v);
102 if (r < 0)
103 return r;
104
105 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
106 char *n, ts[FORMAT_TIMESPAN_MAX];
107
108 if (fix_0)
109 *p = v != 0 ? v: USEC_INFINITY;
110 else
111 *p = v;
112
113 n = strndupa(name, strlen(name) - 4);
114 unit_write_settingf(u, flags, name, "%sSec=%s", n,
115 format_timespan(ts, sizeof(ts), v, USEC_PER_MSEC));
116 }
117
118 return 1;
119 }