chiark / gitweb /
swap: properly expose timeout property on the bus
[elogind.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_UNIT_CGROUP_INTERFACE                                       \
39         BUS_EXEC_COMMAND_INTERFACE("ExecActivate")                      \
40         BUS_EXEC_COMMAND_INTERFACE("ExecDeactivate")                    \
41         BUS_EXEC_CONTEXT_INTERFACE                                      \
42         BUS_KILL_CONTEXT_INTERFACE                                      \
43         BUS_CGROUP_CONTEXT_INTERFACE                                    \
44         "  <property name=\"ControlPID\" type=\"u\" access=\"read\"/>\n" \
45         "  <property name=\"Result\" type=\"s\" access=\"read\"/>\n"    \
46         " </interface>\n"
47
48 #define INTROSPECTION                                                   \
49         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
50         "<node>\n"                                                      \
51         BUS_UNIT_INTERFACE                                              \
52         BUS_SWAP_INTERFACE                                              \
53         BUS_PROPERTIES_INTERFACE                                        \
54         BUS_PEER_INTERFACE                                              \
55         BUS_INTROSPECTABLE_INTERFACE                                    \
56         "</node>\n"
57
58 #define INTERFACES_LIST                              \
59         BUS_UNIT_INTERFACES_LIST                     \
60         "org.freedesktop.systemd1.Swap\0"
61
62 const char bus_swap_interface[] _introspect_("Swap") = BUS_SWAP_INTERFACE;
63
64 const char bus_swap_invalidating_properties[] =
65         "What\0"
66         "Priority\0"
67         "ExecActivate\0"
68         "ExecDeactivate\0"
69         "ControlPID\0"
70         "Result\0";
71
72 static int bus_swap_append_priority(DBusMessageIter *i, const char *property, void *data) {
73         Swap *s = data;
74         dbus_int32_t j;
75
76         assert(i);
77         assert(property);
78         assert(s);
79
80         if (s->from_proc_swaps)
81                 j = s->parameters_proc_swaps.priority;
82         else if (s->from_fragment)
83                 j = s->parameters_fragment.priority;
84         else
85                 j = -1;
86
87         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &j))
88                 return -ENOMEM;
89
90         return 0;
91 }
92
93 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_swap_append_swap_result, swap_result, SwapResult);
94
95 static const BusProperty bus_swap_properties[] = {
96         { "What",       bus_property_append_string, "s", offsetof(Swap, what),  true },
97         { "Priority",   bus_swap_append_priority,   "i", 0 },
98         { "TimeoutUSec",bus_property_append_usec,   "t", offsetof(Swap, timeout_usec)},
99         BUS_EXEC_COMMAND_PROPERTY("ExecActivate",   offsetof(Swap, exec_command[SWAP_EXEC_ACTIVATE]),   false),
100         BUS_EXEC_COMMAND_PROPERTY("ExecDeactivate", offsetof(Swap, exec_command[SWAP_EXEC_DEACTIVATE]), false),
101         { "ControlPID", bus_property_append_pid,    "u", offsetof(Swap, control_pid) },
102         { "Result",     bus_swap_append_swap_result,"s", offsetof(Swap, result)      },
103         { NULL, }
104 };
105
106 DBusHandlerResult bus_swap_message_handler(Unit *u, DBusConnection *c, DBusMessage *message) {
107         Swap *s = SWAP(u);
108         const BusBoundProperties bps[] = {
109                 { "org.freedesktop.systemd1.Unit", bus_unit_properties,           u },
110                 { "org.freedesktop.systemd1.Swap", bus_unit_cgroup_properties,    u },
111                 { "org.freedesktop.systemd1.Swap", bus_swap_properties,           s },
112                 { "org.freedesktop.systemd1.Swap", bus_exec_context_properties,   &s->exec_context },
113                 { "org.freedesktop.systemd1.Swap", bus_kill_context_properties,   &s->kill_context },
114                 { "org.freedesktop.systemd1.Swap", bus_cgroup_context_properties, &s->cgroup_context },
115                 { NULL, }
116         };
117
118         SELINUX_UNIT_ACCESS_CHECK(u, c, message, "status");
119
120         return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, bps);
121 }
122
123 int bus_swap_set_property(
124                 Unit *u,
125                 const char *name,
126                 DBusMessageIter *i,
127                 UnitSetPropertiesMode mode,
128                 DBusError *error) {
129
130         Swap *s = SWAP(u);
131         int r;
132
133         assert(name);
134         assert(u);
135         assert(i);
136
137         r = bus_cgroup_set_property(u, &s->cgroup_context, name, i, mode, error);
138         if (r != 0)
139                 return r;
140
141         return 0;
142 }
143
144 int bus_swap_commit_properties(Unit *u) {
145         assert(u);
146
147         unit_realize_cgroup(u);
148         return 0;
149 }