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