chiark / gitweb /
dbus: shut down bus connection cleanly and fully when a direct client disconnects
[elogind.git] / src / dbus-mount.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23
24 #include "dbus-unit.h"
25 #include "dbus-mount.h"
26 #include "dbus-execute.h"
27
28 #define BUS_MOUNT_INTERFACE                                             \
29         " <interface name=\"org.freedesktop.systemd1.Mount\">\n"        \
30         "  <property name=\"Where\" type=\"s\" access=\"read\"/>\n"     \
31         "  <property name=\"What\" type=\"s\" access=\"read\"/>\n"      \
32         "  <property name=\"Options\" type=\"s\" access=\"read\"/>\n"   \
33         "  <property name=\"Type\" type=\"s\" access=\"read\"/>\n"      \
34         "  <property name=\"TimeoutUSec\" type=\"t\" access=\"read\"/>\n" \
35         BUS_EXEC_COMMAND_INTERFACE("ExecMount")                         \
36         BUS_EXEC_COMMAND_INTERFACE("ExecUnmount")                       \
37         BUS_EXEC_COMMAND_INTERFACE("ExecRemount")                       \
38         BUS_EXEC_CONTEXT_INTERFACE                                      \
39         "  <property name=\"KillMode\" type=\"s\" access=\"read\"/>\n"  \
40         "  <property name=\"ControlPID\" type=\"u\" access=\"read\"/>\n" \
41         "  <property name=\"DirectoryMode\" type=\"u\" access=\"read\"/>\n" \
42         " </interface>\n"
43
44 #define INTROSPECTION                                                   \
45         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
46         "<node>\n"                                                      \
47         BUS_UNIT_INTERFACE                                              \
48         BUS_MOUNT_INTERFACE                                             \
49         BUS_PROPERTIES_INTERFACE                                        \
50         BUS_INTROSPECTABLE_INTERFACE                                    \
51         "</node>\n"
52
53 const char bus_mount_interface[] = BUS_MOUNT_INTERFACE;
54
55 static int bus_mount_append_what(Manager *n, DBusMessageIter *i, const char *property, void *data) {
56         Mount *m = data;
57         const char *d;
58
59         assert(n);
60         assert(i);
61         assert(property);
62         assert(m);
63
64         if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.what)
65                 d = m->parameters_proc_self_mountinfo.what;
66         else if (m->from_fragment && m->parameters_fragment.what)
67                 d = m->parameters_fragment.what;
68         else if (m->from_etc_fstab && m->parameters_etc_fstab.what)
69                 d = m->parameters_etc_fstab.what;
70         else
71                 d = "";
72
73         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
74                 return -ENOMEM;
75
76         return 0;
77 }
78
79 static int bus_mount_append_options(Manager *n, DBusMessageIter *i, const char *property, void *data) {
80         Mount *m = data;
81         const char *d;
82
83         assert(n);
84         assert(i);
85         assert(property);
86         assert(m);
87
88         if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.options)
89                 d = m->parameters_proc_self_mountinfo.options;
90         else if (m->from_fragment && m->parameters_fragment.options)
91                 d = m->parameters_fragment.options;
92         else if (m->from_etc_fstab && m->parameters_etc_fstab.options)
93                 d = m->parameters_etc_fstab.options;
94         else
95                 d = "";
96
97         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
98                 return -ENOMEM;
99
100         return 0;
101 }
102
103 static int bus_mount_append_type(Manager *n, DBusMessageIter *i, const char *property, void *data) {
104         Mount *m = data;
105         const char *d;
106
107         assert(n);
108         assert(i);
109         assert(property);
110         assert(m);
111
112         if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.fstype)
113                 d = m->parameters_proc_self_mountinfo.fstype;
114         else if (m->from_fragment && m->parameters_fragment.fstype)
115                 d = m->parameters_fragment.fstype;
116         else if (m->from_etc_fstab && m->parameters_etc_fstab.fstype)
117                 d = m->parameters_etc_fstab.fstype;
118         else
119                 d = "";
120
121         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
122                 return -ENOMEM;
123
124         return 0;
125 }
126
127 DBusHandlerResult bus_mount_message_handler(Unit *u, DBusConnection *c, DBusMessage *message) {
128         const BusProperty properties[] = {
129                 BUS_UNIT_PROPERTIES,
130                 { "org.freedesktop.systemd1.Mount", "Where",         bus_property_append_string, "s", u->mount.where         },
131                 { "org.freedesktop.systemd1.Mount", "What",          bus_mount_append_what,      "s", u                      },
132                 { "org.freedesktop.systemd1.Mount", "Options",       bus_mount_append_options,   "s", u                      },
133                 { "org.freedesktop.systemd1.Mount", "Type",          bus_mount_append_type,      "s", u                      },
134                 { "org.freedesktop.systemd1.Mount", "TimeoutUSec",   bus_property_append_usec,   "t", &u->mount.timeout_usec },
135                 BUS_EXEC_COMMAND_PROPERTY("org.freedesktop.systemd1.Mount", u->mount.exec_command+MOUNT_EXEC_MOUNT,   "ExecMount"),
136                 BUS_EXEC_COMMAND_PROPERTY("org.freedesktop.systemd1.Mount", u->mount.exec_command+MOUNT_EXEC_UNMOUNT, "ExecUnmount"),
137                 BUS_EXEC_COMMAND_PROPERTY("org.freedesktop.systemd1.Mount", u->mount.exec_command+MOUNT_EXEC_REMOUNT, "ExecRemount"),
138                 BUS_EXEC_CONTEXT_PROPERTIES("org.freedesktop.systemd1.Mount", u->mount.exec_context),
139                 { "org.freedesktop.systemd1.Mount", "KillMode",      bus_unit_append_kill_mode,  "s", &u->mount.kill_mode    },
140                 { "org.freedesktop.systemd1.Mount", "ControlPID",    bus_property_append_pid,    "u", &u->mount.control_pid  },
141                 { "org.freedesktop.systemd1.Mount", "DirectoryMode", bus_property_append_mode,   "u", &u->mount.directory_mode },
142                 { NULL, NULL, NULL, NULL, NULL }
143         };
144
145         return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, properties);
146 }