chiark / gitweb /
load-fragment: a few modernizations
[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-swap.h"
27 #include "dbus-execute.h"
28 #include "dbus-common.h"
29
30 #define BUS_SWAP_INTERFACE                                              \
31         " <interface name=\"org.freedesktop.systemd1.Swap\">\n"         \
32         "  <property name=\"What\" type=\"s\" access=\"read\"/>\n"      \
33         "  <property name=\"Priority\" type=\"i\" access=\"read\"/>\n"  \
34         "  <property name=\"TimeoutUSec\" type=\"t\" access=\"read\"/>\n" \
35         BUS_EXEC_COMMAND_INTERFACE("ExecActivate")                      \
36         BUS_EXEC_COMMAND_INTERFACE("ExecDeactivate")                    \
37         BUS_EXEC_CONTEXT_INTERFACE                                      \
38         "  <property name=\"ControlPID\" type=\"u\" access=\"read\"/>\n" \
39         "  <property name=\"Result\" type=\"s\" access=\"read\"/>\n"    \
40         " </interface>\n"
41
42 #define INTROSPECTION                                                   \
43         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
44         "<node>\n"                                                      \
45         BUS_UNIT_INTERFACE                                              \
46         BUS_SWAP_INTERFACE                                              \
47         BUS_PROPERTIES_INTERFACE                                        \
48         BUS_PEER_INTERFACE                                              \
49         BUS_INTROSPECTABLE_INTERFACE                                    \
50         "</node>\n"
51
52 #define INTERFACES_LIST                              \
53         BUS_UNIT_INTERFACES_LIST                     \
54         "org.freedesktop.systemd1.Swap\0"
55
56 const char bus_swap_interface[] _introspect_("Swap") = BUS_SWAP_INTERFACE;
57
58 const char bus_swap_invalidating_properties[] =
59         "What\0"
60         "Priority\0"
61         "ExecActivate\0"
62         "ExecDeactivate\0"
63         "ControlPID\0"
64         "Result\0";
65
66 static int bus_swap_append_priority(DBusMessageIter *i, const char *property, void *data) {
67         Swap *s = data;
68         dbus_int32_t j;
69
70         assert(i);
71         assert(property);
72         assert(s);
73
74         if (s->from_proc_swaps)
75                 j = s->parameters_proc_swaps.priority;
76         else if (s->from_fragment)
77                 j = s->parameters_fragment.priority;
78         else
79                 j = -1;
80
81         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &j))
82                 return -ENOMEM;
83
84         return 0;
85 }
86
87 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_swap_append_swap_result, swap_result, SwapResult);
88
89 static const BusProperty bus_swap_properties[] = {
90         { "What",       bus_property_append_string, "s", offsetof(Swap, what),  true },
91         { "Priority",   bus_swap_append_priority,   "i", 0 },
92         BUS_EXEC_COMMAND_PROPERTY("ExecActivate",   offsetof(Swap, exec_command[SWAP_EXEC_ACTIVATE]),   false),
93         BUS_EXEC_COMMAND_PROPERTY("ExecDeactivate", offsetof(Swap, exec_command[SWAP_EXEC_DEACTIVATE]), false),
94         { "ControlPID", bus_property_append_pid,    "u", offsetof(Swap, control_pid) },
95         { "Result",     bus_swap_append_swap_result,"s", offsetof(Swap, result)      },
96         { NULL, }
97 };
98
99 DBusHandlerResult bus_swap_message_handler(Unit *u, DBusConnection *c, DBusMessage *message) {
100         Swap *s = SWAP(u);
101         const BusBoundProperties bps[] = {
102                 { "org.freedesktop.systemd1.Unit", bus_unit_properties,         u },
103                 { "org.freedesktop.systemd1.Swap", bus_swap_properties,         s },
104                 { "org.freedesktop.systemd1.Swap", bus_exec_context_properties, &s->exec_context },
105                 { NULL, }
106         };
107
108         return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, bps);
109 }