chiark / gitweb /
dbus: complete exec coverage
[elogind.git] / src / dbus-execute.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 #include <dbus/dbus.h>
24 #include <sys/prctl.h>
25
26 #include "dbus-execute.h"
27 #include "missing.h"
28 #include "ioprio.h"
29
30 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_input, exec_input, ExecInput);
31 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_output, exec_output, ExecOutput);
32
33 int bus_execute_append_oom_adjust(Manager *m, DBusMessageIter *i, const char *property, void *data) {
34         ExecContext *c = data;
35         int32_t n;
36
37         assert(m);
38         assert(i);
39         assert(property);
40         assert(c);
41
42         if (c->oom_adjust_set)
43                 n = c->oom_adjust;
44         else {
45                 char *t;
46
47                 n = 0;
48                 if (read_one_line_file("/proc/self/oom_adj", &t) >= 0) {
49                         safe_atoi(t, &n);
50                         free(t);
51                 }
52         }
53
54         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
55                 return -ENOMEM;
56
57         return 0;
58 }
59
60 int bus_execute_append_nice(Manager *m, DBusMessageIter *i, const char *property, void *data) {
61         ExecContext *c = data;
62         int32_t n;
63
64         assert(m);
65         assert(i);
66         assert(property);
67         assert(c);
68
69         if (c->nice_set)
70                 n = c->nice;
71         else
72                 n = getpriority(PRIO_PROCESS, 0);
73
74         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
75                 return -ENOMEM;
76
77         return 0;
78 }
79
80 int bus_execute_append_ioprio(Manager *m, DBusMessageIter *i, const char *property, void *data) {
81         ExecContext *c = data;
82         int32_t n;
83
84         assert(m);
85         assert(i);
86         assert(property);
87         assert(c);
88
89         if (c->ioprio_set)
90                 n = c->ioprio;
91         else
92                 n = ioprio_get(IOPRIO_WHO_PROCESS, 0);
93
94         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
95                 return -ENOMEM;
96
97         return 0;
98 }
99
100 int bus_execute_append_cpu_sched_policy(Manager *m, DBusMessageIter *i, const char *property, void *data) {
101         ExecContext *c = data;
102         int32_t n;
103
104         assert(m);
105         assert(i);
106         assert(property);
107         assert(c);
108
109         if (c->cpu_sched_set)
110                 n = c->cpu_sched_policy;
111         else
112                 n = sched_getscheduler(0);
113
114         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
115                 return -ENOMEM;
116
117         return 0;
118 }
119
120 int bus_execute_append_cpu_sched_priority(Manager *m, DBusMessageIter *i, const char *property, void *data) {
121         ExecContext *c = data;
122         int32_t n;
123
124         assert(m);
125         assert(i);
126         assert(property);
127         assert(c);
128
129         if (c->cpu_sched_set)
130                 n = c->cpu_sched_priority;
131         else {
132                 struct sched_param p;
133                 n = 0;
134
135                 zero(p);
136                 if (sched_getparam(0, &p) >= 0)
137                         n = p.sched_priority;
138         }
139
140         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
141                 return -ENOMEM;
142
143         return 0;
144 }
145
146 int bus_execute_append_affinity(Manager *m, DBusMessageIter *i, const char *property, void *data) {
147         ExecContext *c = data;
148         dbus_bool_t b;
149         DBusMessageIter sub;
150
151         assert(m);
152         assert(i);
153         assert(property);
154         assert(c);
155
156         if (!(dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "y", &sub)))
157                 return -ENOMEM;
158
159         if (c->cpuset)
160                 b = dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &c->cpuset, CPU_ALLOC_SIZE(c->cpuset_ncpus));
161         else
162                 b = dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &c->cpuset, 0);
163
164         if (!b)
165                 return -ENOMEM;
166
167         if (!dbus_message_iter_close_container(i, &sub))
168                 return -ENOMEM;
169
170         return 0;
171 }
172
173 int bus_execute_append_timer_slack_ns(Manager *m, DBusMessageIter *i, const char *property, void *data) {
174         ExecContext *c = data;
175         uint64_t u;
176
177         assert(m);
178         assert(i);
179         assert(property);
180         assert(c);
181
182         if (c->timer_slack_ns_set)
183                 u = (uint64_t) c->timer_slack_ns_set;
184         else
185                 u = (uint64_t) prctl(PR_GET_TIMERSLACK);
186
187         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
188                 return -ENOMEM;
189
190         return 0;
191 }
192
193 int bus_execute_append_capabilities(Manager *m, DBusMessageIter *i, const char *property, void *data) {
194         ExecContext *c = data;
195         char *t = NULL;
196         const char *s;
197         dbus_bool_t b;
198
199         assert(m);
200         assert(i);
201         assert(property);
202         assert(c);
203
204         if (c->capabilities)
205                 s = t = cap_to_text(c->capabilities, NULL);
206         else
207                 s = "";
208
209         if (!t)
210                 return -ENOMEM;
211
212         b = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &s);
213         cap_free(t);
214
215         if (!b)
216                 return -ENOMEM;
217
218         return 0;
219 }
220
221 int bus_execute_append_rlimits(Manager *m, DBusMessageIter *i, const char *property, void *data) {
222         ExecContext *c = data;
223         int r;
224         uint64_t u;
225
226         assert(m);
227         assert(i);
228         assert(property);
229         assert(c);
230
231         assert_se((r = rlimit_from_string(property)) >= 0);
232
233         if (c->rlimit[r])
234                 u = (uint64_t) c->rlimit[r]->rlim_max;
235         else {
236                 struct rlimit rl;
237
238                 zero(rl);
239                 getrlimit(r, &rl);
240
241                 u = (uint64_t) rl.rlim_max;
242         }
243
244         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
245                 return -ENOMEM;
246
247         return 0;
248 }