]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-mount.c
mount: add new ForceUnmount= setting for mount units, mapping to umount(8)'s "-f...
[thirdparty/systemd.git] / src / core / dbus-mount.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "bus-util.h"
21 #include "dbus-cgroup.h"
22 #include "dbus-execute.h"
23 #include "dbus-kill.h"
24 #include "dbus-mount.h"
25 #include "mount.h"
26 #include "string-util.h"
27 #include "unit.h"
28
29 static int property_get_what(
30 sd_bus *bus,
31 const char *path,
32 const char *interface,
33 const char *property,
34 sd_bus_message *reply,
35 void *userdata,
36 sd_bus_error *error) {
37
38 Mount *m = userdata;
39 const char *d;
40
41 assert(bus);
42 assert(reply);
43 assert(m);
44
45 if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.what)
46 d = m->parameters_proc_self_mountinfo.what;
47 else if (m->from_fragment && m->parameters_fragment.what)
48 d = m->parameters_fragment.what;
49 else
50 d = "";
51
52 return sd_bus_message_append(reply, "s", d);
53 }
54
55 static int property_get_options(
56 sd_bus *bus,
57 const char *path,
58 const char *interface,
59 const char *property,
60 sd_bus_message *reply,
61 void *userdata,
62 sd_bus_error *error) {
63
64 Mount *m = userdata;
65 const char *d;
66
67 assert(bus);
68 assert(reply);
69 assert(m);
70
71 if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.options)
72 d = m->parameters_proc_self_mountinfo.options;
73 else if (m->from_fragment && m->parameters_fragment.options)
74 d = m->parameters_fragment.options;
75 else
76 d = "";
77
78 return sd_bus_message_append(reply, "s", d);
79 }
80
81 static int property_get_type(
82 sd_bus *bus,
83 const char *path,
84 const char *interface,
85 const char *property,
86 sd_bus_message *reply,
87 void *userdata,
88 sd_bus_error *error) {
89
90 Mount *m = userdata;
91 const char *d;
92
93 assert(bus);
94 assert(reply);
95 assert(m);
96
97 if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.fstype)
98 d = m->parameters_proc_self_mountinfo.fstype;
99 else if (m->from_fragment && m->parameters_fragment.fstype)
100 d = m->parameters_fragment.fstype;
101 else
102 d = "";
103
104 return sd_bus_message_append(reply, "s", d);
105 }
106
107 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, mount_result, MountResult);
108
109 const sd_bus_vtable bus_mount_vtable[] = {
110 SD_BUS_VTABLE_START(0),
111 SD_BUS_PROPERTY("Where", "s", NULL, offsetof(Mount, where), SD_BUS_VTABLE_PROPERTY_CONST),
112 SD_BUS_PROPERTY("What", "s", property_get_what, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
113 SD_BUS_PROPERTY("Options","s", property_get_options, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
114 SD_BUS_PROPERTY("Type", "s", property_get_type, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
115 SD_BUS_PROPERTY("TimeoutUSec", "t", bus_property_get_usec, offsetof(Mount, timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
116 SD_BUS_PROPERTY("ControlPID", "u", bus_property_get_pid, offsetof(Mount, control_pid), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
117 SD_BUS_PROPERTY("DirectoryMode", "u", bus_property_get_mode, offsetof(Mount, directory_mode), SD_BUS_VTABLE_PROPERTY_CONST),
118 SD_BUS_PROPERTY("SloppyOptions", "b", bus_property_get_bool, offsetof(Mount, sloppy_options), SD_BUS_VTABLE_PROPERTY_CONST),
119 SD_BUS_PROPERTY("LazyUnmount", "b", bus_property_get_bool, offsetof(Mount, lazy_unmount), SD_BUS_VTABLE_PROPERTY_CONST),
120 SD_BUS_PROPERTY("ForceUnmount", "b", bus_property_get_bool, offsetof(Mount, force_unmount), SD_BUS_VTABLE_PROPERTY_CONST),
121 SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Mount, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
122 SD_BUS_PROPERTY("UID", "u", NULL, offsetof(Unit, ref_uid), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
123 SD_BUS_PROPERTY("GID", "u", NULL, offsetof(Unit, ref_gid), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
124 BUS_EXEC_COMMAND_VTABLE("ExecMount", offsetof(Mount, exec_command[MOUNT_EXEC_MOUNT]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
125 BUS_EXEC_COMMAND_VTABLE("ExecUnmount", offsetof(Mount, exec_command[MOUNT_EXEC_UNMOUNT]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
126 BUS_EXEC_COMMAND_VTABLE("ExecRemount", offsetof(Mount, exec_command[MOUNT_EXEC_REMOUNT]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
127 SD_BUS_VTABLE_END
128 };
129
130 static int bus_mount_set_transient_property(
131 Mount *m,
132 const char *name,
133 sd_bus_message *message,
134 UnitSetPropertiesMode mode,
135 sd_bus_error *error) {
136
137 const char *new_property;
138 char **property;
139 char *p;
140 int r;
141
142 assert(m);
143 assert(name);
144 assert(message);
145
146 if (streq(name, "What"))
147 property = &m->parameters_fragment.what;
148 else if (streq(name, "Options"))
149 property = &m->parameters_fragment.options;
150 else if (streq(name, "Type"))
151 property = &m->parameters_fragment.fstype;
152 else
153 return 0;
154
155 r = sd_bus_message_read(message, "s", &new_property);
156 if (r < 0)
157 return r;
158
159 if (mode != UNIT_CHECK) {
160 p = strdup(new_property);
161 if (!p)
162 return -ENOMEM;
163
164 unit_write_drop_in_format(UNIT(m), mode, name, "[Mount]\n%s=%s\n",
165 name, new_property);
166
167 free(*property);
168 *property = p;
169 }
170
171 return 1;
172 }
173
174 int bus_mount_set_property(
175 Unit *u,
176 const char *name,
177 sd_bus_message *message,
178 UnitSetPropertiesMode mode,
179 sd_bus_error *error) {
180
181 Mount *m = MOUNT(u);
182 int r;
183
184 assert(m);
185 assert(name);
186 assert(message);
187
188 r = bus_cgroup_set_property(u, &m->cgroup_context, name, message, mode, error);
189 if (r != 0)
190 return r;
191
192 if (u->transient && u->load_state == UNIT_STUB) {
193 /* This is a transient unit, let's load a little more */
194
195 r = bus_mount_set_transient_property(m, name, message, mode, error);
196 if (r != 0)
197 return r;
198
199 r = bus_exec_context_set_transient_property(u, &m->exec_context, name, message, mode, error);
200 if (r != 0)
201 return r;
202
203 r = bus_kill_context_set_transient_property(u, &m->kill_context, name, message, mode, error);
204 if (r != 0)
205 return r;
206 }
207
208 return 0;
209 }
210
211 int bus_mount_commit_properties(Unit *u) {
212 assert(u);
213
214 unit_update_cgroup_members_masks(u);
215 unit_realize_cgroup(u);
216
217 return 0;
218 }