chiark / gitweb /
b98c0432cd96aa7c00c79aa10cd71434ce64c138
[elogind.git] / src / cgroups-agent / cgroups-agent.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright 2010 Lennart Poettering
4 ***/
5
6 #include <stdlib.h>
7 #include <sys/socket.h>
8
9 #include "fd-util.h"
10 #include "log.h"
11 #include "socket-util.h"
12
13 /// Additional includes needed by elogind
14 #include "musl_missing.h"
15
16 int main(int argc, char *argv[]) {
17
18         static const union sockaddr_union sa = {
19                 .un.sun_family = AF_UNIX,
20                 .un.sun_path = "/run/systemd/cgroups-agent",
21         };
22
23         _cleanup_close_ int fd = -1;
24         ssize_t n;
25         size_t l;
26
27         if (argc != 2) {
28                 log_error("Incorrect number of arguments.");
29                 return EXIT_FAILURE;
30         }
31
32         elogind_set_program_name(argv[0]);
33         log_set_target(LOG_TARGET_AUTO);
34         log_parse_environment();
35         log_open();
36
37         fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
38         if (fd < 0) {
39                 log_debug_errno(errno, "Failed to allocate socket: %m");
40                 return EXIT_FAILURE;
41         }
42
43         l = strlen(argv[1]);
44
45         n = sendto(fd, argv[1], l, 0, &sa.sa, SOCKADDR_UN_LEN(sa.un));
46         if (n < 0) {
47                 log_debug_errno(errno, "Failed to send cgroups agent message: %m");
48                 return EXIT_FAILURE;
49         }
50
51         if ((size_t) n != l) {
52                 log_debug("Datagram size mismatch");
53                 return EXIT_FAILURE;
54         }
55
56         return EXIT_SUCCESS;
57 }