]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-swap.c
dbus: fix minor memory leak when sending job change signals
[thirdparty/systemd.git] / src / core / dbus-swap.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7 Copyright 2010 Maarten Lankhorst
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <errno.h>
24
25 #include "dbus-unit.h"
26 #include "dbus-execute.h"
27 #include "dbus-kill.h"
28 #include "dbus-cgroup.h"
29 #include "dbus-common.h"
30 #include "selinux-access.h"
31 #include "dbus-swap.h"
32
33 #define BUS_SWAP_INTERFACE \
34 " <interface name=\"org.freedesktop.systemd1.Swap\">\n" \
35 " <property name=\"What\" type=\"s\" access=\"read\"/>\n" \
36 " <property name=\"Priority\" type=\"i\" access=\"read\"/>\n" \
37 " <property name=\"TimeoutUSec\" type=\"t\" access=\"read\"/>\n" \
38 BUS_EXEC_COMMAND_INTERFACE("ExecActivate") \
39 BUS_EXEC_COMMAND_INTERFACE("ExecDeactivate") \
40 BUS_EXEC_CONTEXT_INTERFACE \
41 BUS_KILL_CONTEXT_INTERFACE \
42 " <property name=\"ControlPID\" type=\"u\" access=\"read\"/>\n" \
43 " <property name=\"Result\" type=\"s\" access=\"read\"/>\n" \
44 " </interface>\n"
45
46 #define INTROSPECTION \
47 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
48 "<node>\n" \
49 BUS_UNIT_INTERFACE \
50 BUS_SWAP_INTERFACE \
51 BUS_PROPERTIES_INTERFACE \
52 BUS_PEER_INTERFACE \
53 BUS_INTROSPECTABLE_INTERFACE \
54 "</node>\n"
55
56 #define INTERFACES_LIST \
57 BUS_UNIT_INTERFACES_LIST \
58 "org.freedesktop.systemd1.Swap\0"
59
60 const char bus_swap_interface[] _introspect_("Swap") = BUS_SWAP_INTERFACE;
61
62 const char bus_swap_invalidating_properties[] =
63 "What\0"
64 "Priority\0"
65 "ExecActivate\0"
66 "ExecDeactivate\0"
67 "ControlPID\0"
68 "Result\0";
69
70 static int bus_swap_append_priority(DBusMessageIter *i, const char *property, void *data) {
71 Swap *s = data;
72 dbus_int32_t j;
73
74 assert(i);
75 assert(property);
76 assert(s);
77
78 if (s->from_proc_swaps)
79 j = s->parameters_proc_swaps.priority;
80 else if (s->from_fragment)
81 j = s->parameters_fragment.priority;
82 else
83 j = -1;
84
85 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &j))
86 return -ENOMEM;
87
88 return 0;
89 }
90
91 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_swap_append_swap_result, swap_result, SwapResult);
92
93 static const BusProperty bus_swap_properties[] = {
94 { "What", bus_property_append_string, "s", offsetof(Swap, what), true },
95 { "Priority", bus_swap_append_priority, "i", 0 },
96 BUS_EXEC_COMMAND_PROPERTY("ExecActivate", offsetof(Swap, exec_command[SWAP_EXEC_ACTIVATE]), false),
97 BUS_EXEC_COMMAND_PROPERTY("ExecDeactivate", offsetof(Swap, exec_command[SWAP_EXEC_DEACTIVATE]), false),
98 { "ControlPID", bus_property_append_pid, "u", offsetof(Swap, control_pid) },
99 { "Result", bus_swap_append_swap_result,"s", offsetof(Swap, result) },
100 { NULL, }
101 };
102
103 DBusHandlerResult bus_swap_message_handler(Unit *u, DBusConnection *c, DBusMessage *message) {
104 Swap *s = SWAP(u);
105 const BusBoundProperties bps[] = {
106 { "org.freedesktop.systemd1.Unit", bus_unit_properties, u },
107 { "org.freedesktop.systemd1.Swap", bus_swap_properties, s },
108 { "org.freedesktop.systemd1.Swap", bus_exec_context_properties, &s->exec_context },
109 { "org.freedesktop.systemd1.Swap", bus_kill_context_properties, &s->kill_context },
110 { "org.freedesktop.systemd1.Swap", bus_cgroup_context_properties, &s->cgroup_context },
111 { NULL, }
112 };
113
114 SELINUX_UNIT_ACCESS_CHECK(u, c, message, "status");
115
116 return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, bps);
117 }
118
119 int bus_swap_set_property(
120 Unit *u,
121 const char *name,
122 DBusMessageIter *i,
123 UnitSetPropertiesMode mode,
124 DBusError *error) {
125
126 Swap *s = SWAP(u);
127 int r;
128
129 assert(name);
130 assert(u);
131 assert(i);
132
133 r = bus_cgroup_set_property(u, &s->cgroup_context, name, i, mode, error);
134 if (r != 0)
135 return r;
136
137 return 0;
138 }
139
140 int bus_swap_commit_properties(Unit *u) {
141 assert(u);
142
143 unit_realize_cgroup(u);
144 return 0;
145 }