]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
abbbea81 LP |
2 | /*** |
3 | Copyright 2010 Lennart Poettering | |
0ebee881 | 4 | ***/ |
8c47c732 | 5 | |
abbbea81 | 6 | #include <errno.h> |
916abb21 | 7 | #include <limits.h> |
0ebee881 | 8 | #include <mqueue.h> |
8dd4c05b LP |
9 | #include <netinet/in.h> |
10 | #include <stdarg.h> | |
11 | #include <stddef.h> | |
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <sys/socket.h> | |
16 | #include <sys/stat.h> | |
17 | #include <sys/un.h> | |
18 | #include <unistd.h> | |
abbbea81 | 19 | |
07630cea LP |
20 | #include "sd-daemon.h" |
21 | ||
b5efdb8a | 22 | #include "alloc-util.h" |
3ffd4af2 | 23 | #include "fd-util.h" |
f4f15635 | 24 | #include "fs-util.h" |
6bedfcbb | 25 | #include "parse-util.h" |
be8f4e9e | 26 | #include "path-util.h" |
dccca82b | 27 | #include "process-util.h" |
3cb46740 | 28 | #include "socket-util.h" |
8dd4c05b LP |
29 | #include "strv.h" |
30 | #include "util.h" | |
31 | ||
a47806fa LP |
32 | #define SNDBUF_SIZE (8*1024*1024) |
33 | ||
8dd4c05b LP |
34 | static void unsetenv_all(bool unset_environment) { |
35 | ||
36 | if (!unset_environment) | |
37 | return; | |
38 | ||
39 | unsetenv("LISTEN_PID"); | |
40 | unsetenv("LISTEN_FDS"); | |
41 | unsetenv("LISTEN_FDNAMES"); | |
42 | } | |
43 | ||
0ebee881 | 44 | _public_ int sd_listen_fds(int unset_environment) { |
abbbea81 | 45 | const char *e; |
046c93f8 | 46 | int n, r, fd; |
be8f4e9e | 47 | pid_t pid; |
abbbea81 | 48 | |
50425d16 MS |
49 | e = getenv("LISTEN_PID"); |
50 | if (!e) { | |
abbbea81 LP |
51 | r = 0; |
52 | goto finish; | |
53 | } | |
54 | ||
be8f4e9e LP |
55 | r = parse_pid(e, &pid); |
56 | if (r < 0) | |
abbbea81 | 57 | goto finish; |
abbbea81 LP |
58 | |
59 | /* Is this for us? */ | |
df0ff127 | 60 | if (getpid_cached() != pid) { |
abbbea81 LP |
61 | r = 0; |
62 | goto finish; | |
63 | } | |
64 | ||
50425d16 MS |
65 | e = getenv("LISTEN_FDS"); |
66 | if (!e) { | |
abbbea81 LP |
67 | r = 0; |
68 | goto finish; | |
69 | } | |
70 | ||
046c93f8 | 71 | r = safe_atoi(e, &n); |
be8f4e9e | 72 | if (r < 0) |
abbbea81 | 73 | goto finish; |
8640e111 | 74 | |
046c93f8 VC |
75 | assert_cc(SD_LISTEN_FDS_START < INT_MAX); |
76 | if (n <= 0 || n > INT_MAX - SD_LISTEN_FDS_START) { | |
77 | r = -EINVAL; | |
78 | goto finish; | |
79 | } | |
80 | ||
81 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { | |
be8f4e9e LP |
82 | r = fd_cloexec(fd, true); |
83 | if (r < 0) | |
8640e111 | 84 | goto finish; |
8640e111 LP |
85 | } |
86 | ||
046c93f8 | 87 | r = n; |
abbbea81 LP |
88 | |
89 | finish: | |
8dd4c05b LP |
90 | unsetenv_all(unset_environment); |
91 | return r; | |
92 | } | |
93 | ||
94 | _public_ int sd_listen_fds_with_names(int unset_environment, char ***names) { | |
95 | _cleanup_strv_free_ char **l = NULL; | |
96 | bool have_names; | |
97 | int n_names = 0, n_fds; | |
98 | const char *e; | |
99 | int r; | |
100 | ||
101 | if (!names) | |
102 | return sd_listen_fds(unset_environment); | |
103 | ||
104 | e = getenv("LISTEN_FDNAMES"); | |
105 | if (e) { | |
106 | n_names = strv_split_extract(&l, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
107 | if (n_names < 0) { | |
108 | unsetenv_all(unset_environment); | |
109 | return n_names; | |
110 | } | |
111 | ||
112 | have_names = true; | |
113 | } else | |
114 | have_names = false; | |
115 | ||
116 | n_fds = sd_listen_fds(unset_environment); | |
117 | if (n_fds <= 0) | |
118 | return n_fds; | |
119 | ||
120 | if (have_names) { | |
121 | if (n_names != n_fds) | |
122 | return -EINVAL; | |
123 | } else { | |
124 | r = strv_extend_n(&l, "unknown", n_fds); | |
125 | if (r < 0) | |
126 | return r; | |
abbbea81 LP |
127 | } |
128 | ||
ae2a15bc | 129 | *names = TAKE_PTR(l); |
8dd4c05b LP |
130 | |
131 | return n_fds; | |
abbbea81 | 132 | } |
7c394faa | 133 | |
0ebee881 | 134 | _public_ int sd_is_fifo(int fd, const char *path) { |
7c394faa LP |
135 | struct stat st_fd; |
136 | ||
e6803801 | 137 | assert_return(fd >= 0, -EBADF); |
7c394faa | 138 | |
7c394faa LP |
139 | if (fstat(fd, &st_fd) < 0) |
140 | return -errno; | |
141 | ||
142 | if (!S_ISFIFO(st_fd.st_mode)) | |
143 | return 0; | |
144 | ||
145 | if (path) { | |
146 | struct stat st_path; | |
147 | ||
fd8bccfb | 148 | if (stat(path, &st_path) < 0) { |
7c394faa | 149 | |
945c2931 | 150 | if (IN_SET(errno, ENOENT, ENOTDIR)) |
7c394faa LP |
151 | return 0; |
152 | ||
153 | return -errno; | |
154 | } | |
155 | ||
156 | return | |
157 | st_path.st_dev == st_fd.st_dev && | |
158 | st_path.st_ino == st_fd.st_ino; | |
159 | } | |
160 | ||
161 | return 1; | |
162 | } | |
163 | ||
0ebee881 | 164 | _public_ int sd_is_special(int fd, const char *path) { |
4160ec67 WD |
165 | struct stat st_fd; |
166 | ||
e6803801 | 167 | assert_return(fd >= 0, -EBADF); |
4160ec67 WD |
168 | |
169 | if (fstat(fd, &st_fd) < 0) | |
170 | return -errno; | |
171 | ||
172 | if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode)) | |
173 | return 0; | |
174 | ||
175 | if (path) { | |
176 | struct stat st_path; | |
177 | ||
178 | if (stat(path, &st_path) < 0) { | |
179 | ||
945c2931 | 180 | if (IN_SET(errno, ENOENT, ENOTDIR)) |
4160ec67 WD |
181 | return 0; |
182 | ||
183 | return -errno; | |
184 | } | |
185 | ||
186 | if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode)) | |
187 | return | |
188 | st_path.st_dev == st_fd.st_dev && | |
189 | st_path.st_ino == st_fd.st_ino; | |
190 | else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode)) | |
191 | return st_path.st_rdev == st_fd.st_rdev; | |
192 | else | |
193 | return 0; | |
194 | } | |
195 | ||
196 | return 1; | |
197 | } | |
198 | ||
e6a3081a | 199 | static int sd_is_socket_internal(int fd, int type, int listening) { |
7c394faa LP |
200 | struct stat st_fd; |
201 | ||
e6803801 | 202 | assert_return(fd >= 0, -EBADF); |
be8f4e9e | 203 | assert_return(type >= 0, -EINVAL); |
7c394faa LP |
204 | |
205 | if (fstat(fd, &st_fd) < 0) | |
206 | return -errno; | |
207 | ||
208 | if (!S_ISSOCK(st_fd.st_mode)) | |
209 | return 0; | |
210 | ||
211 | if (type != 0) { | |
212 | int other_type = 0; | |
213 | socklen_t l = sizeof(other_type); | |
214 | ||
215 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0) | |
216 | return -errno; | |
217 | ||
218 | if (l != sizeof(other_type)) | |
219 | return -EINVAL; | |
220 | ||
221 | if (other_type != type) | |
222 | return 0; | |
223 | } | |
224 | ||
225 | if (listening >= 0) { | |
226 | int accepting = 0; | |
227 | socklen_t l = sizeof(accepting); | |
228 | ||
229 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0) | |
230 | return -errno; | |
231 | ||
232 | if (l != sizeof(accepting)) | |
233 | return -EINVAL; | |
234 | ||
dde770cf | 235 | if (!accepting != !listening) |
7c394faa LP |
236 | return 0; |
237 | } | |
238 | ||
239 | return 1; | |
240 | } | |
241 | ||
0ebee881 | 242 | _public_ int sd_is_socket(int fd, int family, int type, int listening) { |
88ce42f6 LP |
243 | int r; |
244 | ||
e6803801 | 245 | assert_return(fd >= 0, -EBADF); |
be8f4e9e | 246 | assert_return(family >= 0, -EINVAL); |
88ce42f6 | 247 | |
50425d16 MS |
248 | r = sd_is_socket_internal(fd, type, listening); |
249 | if (r <= 0) | |
88ce42f6 LP |
250 | return r; |
251 | ||
252 | if (family > 0) { | |
1c633045 ZJS |
253 | union sockaddr_union sockaddr = {}; |
254 | socklen_t l = sizeof(sockaddr); | |
88ce42f6 LP |
255 | |
256 | if (getsockname(fd, &sockaddr.sa, &l) < 0) | |
257 | return -errno; | |
258 | ||
b7f42664 | 259 | if (l < sizeof(sa_family_t)) |
88ce42f6 LP |
260 | return -EINVAL; |
261 | ||
262 | return sockaddr.sa.sa_family == family; | |
263 | } | |
264 | ||
265 | return 1; | |
266 | } | |
267 | ||
0ebee881 | 268 | _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) { |
1c633045 ZJS |
269 | union sockaddr_union sockaddr = {}; |
270 | socklen_t l = sizeof(sockaddr); | |
7c394faa LP |
271 | int r; |
272 | ||
e6803801 | 273 | assert_return(fd >= 0, -EBADF); |
be8f4e9e | 274 | assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL); |
88ce42f6 | 275 | |
50425d16 MS |
276 | r = sd_is_socket_internal(fd, type, listening); |
277 | if (r <= 0) | |
7c394faa LP |
278 | return r; |
279 | ||
7c394faa LP |
280 | if (getsockname(fd, &sockaddr.sa, &l) < 0) |
281 | return -errno; | |
282 | ||
b7f42664 | 283 | if (l < sizeof(sa_family_t)) |
7c394faa LP |
284 | return -EINVAL; |
285 | ||
945c2931 | 286 | if (!IN_SET(sockaddr.sa.sa_family, AF_INET, AF_INET6)) |
7c394faa LP |
287 | return 0; |
288 | ||
be8f4e9e | 289 | if (family != 0) |
88ce42f6 LP |
290 | if (sockaddr.sa.sa_family != family) |
291 | return 0; | |
292 | ||
7c394faa | 293 | if (port > 0) { |
dfde7e8c | 294 | unsigned sa_port; |
7c394faa | 295 | |
dfde7e8c LP |
296 | r = sockaddr_port(&sockaddr.sa, &sa_port); |
297 | if (r < 0) | |
298 | return r; | |
7c394faa | 299 | |
dfde7e8c | 300 | return port == sa_port; |
7c394faa LP |
301 | } |
302 | ||
303 | return 1; | |
304 | } | |
305 | ||
f6f372d2 ZJS |
306 | _public_ int sd_is_socket_sockaddr(int fd, int type, const struct sockaddr* addr, unsigned addr_len, int listening) { |
307 | union sockaddr_union sockaddr = {}; | |
308 | socklen_t l = sizeof(sockaddr); | |
309 | int r; | |
310 | ||
311 | assert_return(fd >= 0, -EBADF); | |
312 | assert_return(addr, -EINVAL); | |
313 | assert_return(addr_len >= sizeof(sa_family_t), -ENOBUFS); | |
314 | assert_return(IN_SET(addr->sa_family, AF_INET, AF_INET6), -EPFNOSUPPORT); | |
315 | ||
316 | r = sd_is_socket_internal(fd, type, listening); | |
317 | if (r <= 0) | |
318 | return r; | |
319 | ||
320 | if (getsockname(fd, &sockaddr.sa, &l) < 0) | |
321 | return -errno; | |
322 | ||
323 | if (l < sizeof(sa_family_t)) | |
324 | return -EINVAL; | |
325 | ||
326 | if (sockaddr.sa.sa_family != addr->sa_family) | |
327 | return 0; | |
328 | ||
329 | if (sockaddr.sa.sa_family == AF_INET) { | |
330 | const struct sockaddr_in *in = (const struct sockaddr_in *) addr; | |
331 | ||
332 | if (l < sizeof(struct sockaddr_in) || addr_len < sizeof(struct sockaddr_in)) | |
333 | return -EINVAL; | |
334 | ||
335 | if (in->sin_port != 0 && | |
336 | sockaddr.in.sin_port != in->sin_port) | |
337 | return false; | |
338 | ||
339 | return sockaddr.in.sin_addr.s_addr == in->sin_addr.s_addr; | |
340 | ||
341 | } else { | |
342 | const struct sockaddr_in6 *in = (const struct sockaddr_in6 *) addr; | |
343 | ||
344 | if (l < sizeof(struct sockaddr_in6) || addr_len < sizeof(struct sockaddr_in6)) | |
345 | return -EINVAL; | |
346 | ||
347 | if (in->sin6_port != 0 && | |
348 | sockaddr.in6.sin6_port != in->sin6_port) | |
349 | return false; | |
350 | ||
351 | if (in->sin6_flowinfo != 0 && | |
352 | sockaddr.in6.sin6_flowinfo != in->sin6_flowinfo) | |
353 | return false; | |
354 | ||
355 | if (in->sin6_scope_id != 0 && | |
356 | sockaddr.in6.sin6_scope_id != in->sin6_scope_id) | |
357 | return false; | |
358 | ||
359 | return memcmp(sockaddr.in6.sin6_addr.s6_addr, in->sin6_addr.s6_addr, | |
360 | sizeof(in->sin6_addr.s6_addr)) == 0; | |
361 | } | |
362 | } | |
363 | ||
0ebee881 | 364 | _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) { |
1c633045 ZJS |
365 | union sockaddr_union sockaddr = {}; |
366 | socklen_t l = sizeof(sockaddr); | |
7c394faa LP |
367 | int r; |
368 | ||
e6803801 | 369 | assert_return(fd >= 0, -EBADF); |
be8f4e9e | 370 | |
50425d16 MS |
371 | r = sd_is_socket_internal(fd, type, listening); |
372 | if (r <= 0) | |
7c394faa LP |
373 | return r; |
374 | ||
7c394faa LP |
375 | if (getsockname(fd, &sockaddr.sa, &l) < 0) |
376 | return -errno; | |
377 | ||
b7f42664 | 378 | if (l < sizeof(sa_family_t)) |
7c394faa LP |
379 | return -EINVAL; |
380 | ||
381 | if (sockaddr.sa.sa_family != AF_UNIX) | |
382 | return 0; | |
383 | ||
384 | if (path) { | |
d1d7caee | 385 | if (length == 0) |
7c394faa LP |
386 | length = strlen(path); |
387 | ||
d1d7caee | 388 | if (length == 0) |
7c394faa | 389 | /* Unnamed socket */ |
0e098b15 | 390 | return l == offsetof(struct sockaddr_un, sun_path); |
7c394faa | 391 | |
7c394faa LP |
392 | if (path[0]) |
393 | /* Normal path socket */ | |
cd250a39 | 394 | return |
0e098b15 | 395 | (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) && |
cd250a39 | 396 | memcmp(path, sockaddr.un.sun_path, length+1) == 0; |
7c394faa LP |
397 | else |
398 | /* Abstract namespace socket */ | |
cd250a39 | 399 | return |
0e098b15 | 400 | (l == offsetof(struct sockaddr_un, sun_path) + length) && |
cd250a39 | 401 | memcmp(path, sockaddr.un.sun_path, length) == 0; |
7c394faa LP |
402 | } |
403 | ||
404 | return 1; | |
405 | } | |
8c47c732 | 406 | |
0ebee881 | 407 | _public_ int sd_is_mq(int fd, const char *path) { |
916abb21 LP |
408 | struct mq_attr attr; |
409 | ||
0260d1d5 ZJS |
410 | /* Check that the fd is valid */ |
411 | assert_return(fcntl(fd, F_GETFD) >= 0, -errno); | |
916abb21 | 412 | |
0260d1d5 ZJS |
413 | if (mq_getattr(fd, &attr) < 0) { |
414 | if (errno == EBADF) | |
415 | /* A non-mq fd (or an invalid one, but we ruled that out above) */ | |
416 | return 0; | |
916abb21 | 417 | return -errno; |
0260d1d5 | 418 | } |
916abb21 LP |
419 | |
420 | if (path) { | |
421 | char fpath[PATH_MAX]; | |
422 | struct stat a, b; | |
423 | ||
be8f4e9e | 424 | assert_return(path_is_absolute(path), -EINVAL); |
916abb21 LP |
425 | |
426 | if (fstat(fd, &a) < 0) | |
427 | return -errno; | |
428 | ||
429 | strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12); | |
430 | fpath[sizeof(fpath)-1] = 0; | |
431 | ||
432 | if (stat(fpath, &b) < 0) | |
433 | return -errno; | |
434 | ||
435 | if (a.st_dev != b.st_dev || | |
436 | a.st_ino != b.st_ino) | |
437 | return 0; | |
438 | } | |
439 | ||
440 | return 1; | |
916abb21 LP |
441 | } |
442 | ||
9e1d021e LP |
443 | _public_ int sd_pid_notify_with_fds( |
444 | pid_t pid, | |
445 | int unset_environment, | |
446 | const char *state, | |
447 | const int *fds, | |
448 | unsigned n_fds) { | |
449 | ||
a354329f LP |
450 | union sockaddr_union sockaddr = { |
451 | .sa.sa_family = AF_UNIX, | |
452 | }; | |
453 | struct iovec iovec = { | |
454 | .iov_base = (char*) state, | |
455 | }; | |
456 | struct msghdr msghdr = { | |
457 | .msg_iov = &iovec, | |
458 | .msg_iovlen = 1, | |
459 | .msg_name = &sockaddr, | |
460 | }; | |
a354329f LP |
461 | _cleanup_close_ int fd = -1; |
462 | struct cmsghdr *cmsg = NULL; | |
463 | const char *e; | |
9e1d021e | 464 | bool send_ucred; |
be8f4e9e | 465 | int r; |
8c47c732 LP |
466 | |
467 | if (!state) { | |
468 | r = -EINVAL; | |
469 | goto finish; | |
470 | } | |
471 | ||
a354329f LP |
472 | if (n_fds > 0 && !fds) { |
473 | r = -EINVAL; | |
474 | goto finish; | |
475 | } | |
476 | ||
50425d16 MS |
477 | e = getenv("NOTIFY_SOCKET"); |
478 | if (!e) | |
08bfb810 | 479 | return 0; |
8c47c732 LP |
480 | |
481 | /* Must be an abstract socket, or an absolute path */ | |
945c2931 | 482 | if (!IN_SET(e[0], '@', '/') || e[1] == 0) { |
8c47c732 LP |
483 | r = -EINVAL; |
484 | goto finish; | |
485 | } | |
486 | ||
638b56cd LP |
487 | if (strlen(e) > sizeof(sockaddr.un.sun_path)) { |
488 | r = -EINVAL; | |
489 | goto finish; | |
490 | } | |
491 | ||
50425d16 MS |
492 | fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); |
493 | if (fd < 0) { | |
8c47c732 LP |
494 | r = -errno; |
495 | goto finish; | |
496 | } | |
497 | ||
9e1d021e | 498 | (void) fd_inc_sndbuf(fd, SNDBUF_SIZE); |
a47806fa | 499 | |
a354329f | 500 | iovec.iov_len = strlen(state); |
8c47c732 | 501 | |
a354329f | 502 | strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path)); |
8c47c732 LP |
503 | if (sockaddr.un.sun_path[0] == '@') |
504 | sockaddr.un.sun_path[0] = 0; | |
505 | ||
fc2fffe7 | 506 | msghdr.msg_namelen = SOCKADDR_UN_LEN(sockaddr.un); |
a013bd94 | 507 | |
9e1d021e LP |
508 | send_ucred = |
509 | (pid != 0 && pid != getpid_cached()) || | |
510 | getuid() != geteuid() || | |
511 | getgid() != getegid(); | |
d4a144fa | 512 | |
9e1d021e | 513 | if (n_fds > 0 || send_ucred) { |
96d49011 | 514 | /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */ |
c463a6f1 LP |
515 | msghdr.msg_controllen = |
516 | (n_fds > 0 ? CMSG_SPACE(sizeof(int) * n_fds) : 0) + | |
9e1d021e | 517 | (send_ucred ? CMSG_SPACE(sizeof(struct ucred)) : 0); |
c463a6f1 | 518 | |
40f44238 | 519 | msghdr.msg_control = alloca0(msghdr.msg_controllen); |
a354329f LP |
520 | |
521 | cmsg = CMSG_FIRSTHDR(&msghdr); | |
64144440 ZJS |
522 | if (n_fds > 0) { |
523 | cmsg->cmsg_level = SOL_SOCKET; | |
524 | cmsg->cmsg_type = SCM_RIGHTS; | |
525 | cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds); | |
a354329f | 526 | |
64144440 | 527 | memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds); |
be8f4e9e | 528 | |
9e1d021e | 529 | if (send_ucred) |
64144440 ZJS |
530 | assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg)); |
531 | } | |
a354329f | 532 | |
9e1d021e | 533 | if (send_ucred) { |
64144440 | 534 | struct ucred *ucred; |
be8f4e9e | 535 | |
64144440 ZJS |
536 | cmsg->cmsg_level = SOL_SOCKET; |
537 | cmsg->cmsg_type = SCM_CREDENTIALS; | |
538 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred)); | |
be8f4e9e | 539 | |
64144440 | 540 | ucred = (struct ucred*) CMSG_DATA(cmsg); |
9e1d021e | 541 | ucred->pid = pid != 0 ? pid : getpid_cached(); |
64144440 ZJS |
542 | ucred->uid = getuid(); |
543 | ucred->gid = getgid(); | |
544 | } | |
be8f4e9e LP |
545 | } |
546 | ||
547 | /* First try with fake ucred data, as requested */ | |
548 | if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) { | |
549 | r = 1; | |
8c47c732 LP |
550 | goto finish; |
551 | } | |
552 | ||
a354329f | 553 | /* If that failed, try with our own ucred instead */ |
9e1d021e | 554 | if (send_ucred) { |
64144440 ZJS |
555 | msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred)); |
556 | if (msghdr.msg_controllen == 0) | |
a354329f | 557 | msghdr.msg_control = NULL; |
be8f4e9e LP |
558 | |
559 | if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) { | |
560 | r = 1; | |
561 | goto finish; | |
562 | } | |
563 | } | |
564 | ||
565 | r = -errno; | |
8c47c732 LP |
566 | |
567 | finish: | |
568 | if (unset_environment) | |
569 | unsetenv("NOTIFY_SOCKET"); | |
570 | ||
8c47c732 | 571 | return r; |
8c47c732 LP |
572 | } |
573 | ||
a354329f LP |
574 | _public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) { |
575 | return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0); | |
576 | } | |
577 | ||
be8f4e9e | 578 | _public_ int sd_notify(int unset_environment, const char *state) { |
a354329f | 579 | return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0); |
be8f4e9e LP |
580 | } |
581 | ||
582 | _public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) { | |
583 | _cleanup_free_ char *p = NULL; | |
584 | int r; | |
585 | ||
586 | if (format) { | |
587 | va_list ap; | |
588 | ||
589 | va_start(ap, format); | |
590 | r = vasprintf(&p, format, ap); | |
591 | va_end(ap); | |
592 | ||
593 | if (r < 0 || !p) | |
594 | return -ENOMEM; | |
595 | } | |
596 | ||
597 | return sd_pid_notify(pid, unset_environment, p); | |
598 | } | |
599 | ||
0ebee881 | 600 | _public_ int sd_notifyf(int unset_environment, const char *format, ...) { |
be8f4e9e | 601 | _cleanup_free_ char *p = NULL; |
8c47c732 LP |
602 | int r; |
603 | ||
be8f4e9e LP |
604 | if (format) { |
605 | va_list ap; | |
8c47c732 | 606 | |
be8f4e9e LP |
607 | va_start(ap, format); |
608 | r = vasprintf(&p, format, ap); | |
609 | va_end(ap); | |
8c47c732 | 610 | |
be8f4e9e LP |
611 | if (r < 0 || !p) |
612 | return -ENOMEM; | |
613 | } | |
8c47c732 | 614 | |
be8f4e9e | 615 | return sd_pid_notify(0, unset_environment, p); |
8c47c732 | 616 | } |
40473a70 | 617 | |
0ebee881 | 618 | _public_ int sd_booted(void) { |
66e41181 LP |
619 | /* We test whether the runtime unit file directory has been |
620 | * created. This takes place in mount-setup.c, so is | |
621 | * guaranteed to happen very early during boot. */ | |
40473a70 | 622 | |
31021ba0 | 623 | return laccess("/run/systemd/system/", F_OK) >= 0; |
40473a70 | 624 | } |
09812eb7 | 625 | |
0ebee881 | 626 | _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) { |
a9becdd6 | 627 | const char *s, *p = ""; /* p is set to dummy value to do unsetting */ |
be8f4e9e | 628 | uint64_t u; |
a9becdd6 | 629 | int r = 0; |
09812eb7 | 630 | |
a9becdd6 ZJS |
631 | s = getenv("WATCHDOG_USEC"); |
632 | if (!s) | |
09812eb7 | 633 | goto finish; |
09812eb7 | 634 | |
a9becdd6 | 635 | r = safe_atou64(s, &u); |
be8f4e9e | 636 | if (r < 0) |
09812eb7 | 637 | goto finish; |
caffe412 | 638 | if (u <= 0 || u >= USEC_INFINITY) { |
09812eb7 LP |
639 | r = -EINVAL; |
640 | goto finish; | |
641 | } | |
642 | ||
a9becdd6 ZJS |
643 | p = getenv("WATCHDOG_PID"); |
644 | if (p) { | |
645 | pid_t pid; | |
646 | ||
647 | r = parse_pid(p, &pid); | |
648 | if (r < 0) | |
649 | goto finish; | |
650 | ||
651 | /* Is this for us? */ | |
df0ff127 | 652 | if (getpid_cached() != pid) { |
a9becdd6 ZJS |
653 | r = 0; |
654 | goto finish; | |
655 | } | |
09812eb7 LP |
656 | } |
657 | ||
658 | if (usec) | |
be8f4e9e | 659 | *usec = u; |
09812eb7 LP |
660 | |
661 | r = 1; | |
662 | ||
663 | finish: | |
a9becdd6 | 664 | if (unset_environment && s) |
09812eb7 | 665 | unsetenv("WATCHDOG_USEC"); |
a9becdd6 ZJS |
666 | if (unset_environment && p) |
667 | unsetenv("WATCHDOG_PID"); | |
09812eb7 LP |
668 | |
669 | return r; | |
09812eb7 | 670 | } |