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