chiark / gitweb /
core: refuse doing %h, %s, %U specifier resolving in PID 1
[elogind.git] / src / core / dbus-scope.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 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 "scope.h"
24 #include "dbus-unit.h"
25 #include "dbus-cgroup.h"
26 #include "dbus-kill.h"
27 #include "dbus-scope.h"
28 #include "bus-util.h"
29
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, scope_result, ScopeResult);
31
32 const sd_bus_vtable bus_scope_vtable[] = {
33         SD_BUS_VTABLE_START(0),
34         SD_BUS_PROPERTY("TimeoutStopUSec", "t", bus_property_get_usec, offsetof(Scope, timeout_stop_usec), 0),
35         SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Scope, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
36         SD_BUS_VTABLE_END
37 };
38
39 const char* const bus_scope_changing_properties[] = {
40         "Result",
41         NULL
42 };
43
44 static int bus_scope_set_transient_property(
45                 Scope *s,
46                 const char *name,
47                 sd_bus_message *message,
48                 UnitSetPropertiesMode mode,
49                 sd_bus_error *error) {
50
51         int r;
52
53         assert(s);
54         assert(name);
55         assert(message);
56
57         if (streq(name, "PIDs")) {
58                 unsigned n = 0;
59                 uint32_t pid;
60
61                 r = set_ensure_allocated(&s->pids, trivial_hash_func, trivial_compare_func);
62                 if (r < 0)
63                         return r;
64
65                 r = sd_bus_message_enter_container(message, 'a', "u");
66                 if (r < 0)
67                         return r;
68
69                 while ((r = sd_bus_message_read(message, "u", &pid)) > 0) {
70
71                         if (pid <= 1)
72                                 return -EINVAL;
73
74                         if (mode != UNIT_CHECK) {
75                                 r = set_put(s->pids, LONG_TO_PTR(pid));
76                                 if (r < 0 && r != -EEXIST)
77                                         return r;
78                         }
79
80                         n++;
81                 }
82                 if (r < 0)
83                         return r;
84
85                 r = sd_bus_message_exit_container(message);
86                 if (r < 0)
87                         return r;
88
89                 if (n <= 0)
90                         return -EINVAL;
91
92                 return 1;
93
94         } else if (streq(name, "TimeoutStopUSec")) {
95
96                 if (mode != UNIT_CHECK) {
97                         r = sd_bus_message_read(message, "t", &s->timeout_stop_usec);
98                         if (r < 0)
99                                 return r;
100
101                         unit_write_drop_in_format(UNIT(s), mode, name, "[Scope]\nTimeoutStopSec=%lluus\n", (unsigned long long) s->timeout_stop_usec);
102                 } else {
103                         r = sd_bus_message_skip(message, "t");
104                         if (r < 0)
105                                 return r;
106                 }
107
108                 return 1;
109         }
110
111         return 0;
112 }
113
114 int bus_scope_set_property(
115                 Unit *u,
116                 const char *name,
117                 sd_bus_message *message,
118                 UnitSetPropertiesMode mode,
119                 sd_bus_error *error) {
120
121         Scope *s = SCOPE(u);
122         int r;
123
124         assert(s);
125         assert(name);
126         assert(message);
127
128         r = bus_cgroup_set_property(u, &s->cgroup_context, name, message, mode, error);
129         if (r != 0)
130                 return r;
131
132         if (u->load_state == UNIT_STUB) {
133                 /* While we are created we still accept PIDs */
134
135                 r = bus_scope_set_transient_property(s, name, message, mode, error);
136                 if (r != 0)
137                         return r;
138
139                 r = bus_kill_context_set_transient_property(u, &s->kill_context, name, message, mode, error);
140                 if (r != 0)
141                         return r;
142         }
143
144         return 0;
145 }
146
147 int bus_scope_commit_properties(Unit *u) {
148         assert(u);
149
150         unit_realize_cgroup(u);
151         return 0;
152 }