chiark / gitweb /
treewide: use stdio_unset_cloexec() function
[elogind.git] / src / cgroups-agent / cgroups-agent.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 <stdlib.h>
21 #include <sys/socket.h>
22
23 #include "fd-util.h"
24 #include "log.h"
25 #include "socket-util.h"
26
27 /// Additional includes needed by elogind
28 #include "musl_missing.h"
29
30 int main(int argc, char *argv[]) {
31
32         static const union sockaddr_union sa = {
33                 .un.sun_family = AF_UNIX,
34                 .un.sun_path = "/run/systemd/cgroups-agent",
35         };
36
37         _cleanup_close_ int fd = -1;
38         ssize_t n;
39         size_t l;
40
41         if (argc != 2) {
42                 log_error("Incorrect number of arguments.");
43                 return EXIT_FAILURE;
44         }
45
46         elogind_set_program_name(argv[0]);
47         log_set_target(LOG_TARGET_AUTO);
48         log_parse_environment();
49         log_open();
50
51         fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
52         if (fd < 0) {
53                 log_debug_errno(errno, "Failed to allocate socket: %m");
54                 return EXIT_FAILURE;
55         }
56
57         l = strlen(argv[1]);
58
59         n = sendto(fd, argv[1], l, 0, &sa.sa, SOCKADDR_UN_LEN(sa.un));
60         if (n < 0) {
61                 log_debug_errno(errno, "Failed to send cgroups agent message: %m");
62                 return EXIT_FAILURE;
63         }
64
65         if ((size_t) n != l) {
66                 log_debug("Datagram size mismatch");
67                 return EXIT_FAILURE;
68         }
69
70         return EXIT_SUCCESS;
71 }