]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/reply-password/reply-password.c
core: use an AF_UNIX/SOCK_DGRAM socket for cgroup agent notification
[thirdparty/systemd.git] / src / reply-password / reply-password.c
CommitLineData
490aed58
LP
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
5430f7f2
LP
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
490aed58
LP
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
5430f7f2 14 Lesser General Public License for more details.
490aed58 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
490aed58
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
490aed58 20#include <errno.h>
0e098b15 21#include <stddef.h>
07630cea
LP
22#include <string.h>
23#include <sys/socket.h>
24#include <sys/un.h>
490aed58 25
3ffd4af2 26#include "fd-util.h"
490aed58
LP
27#include "log.h"
28#include "macro.h"
07630cea 29#include "string-util.h"
490aed58
LP
30#include "util.h"
31
32static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
33 union {
34 struct sockaddr sa;
35 struct sockaddr_un un;
b92bea5d
ZJS
36 } sa = {
37 .un.sun_family = AF_UNIX,
38 };
490aed58
LP
39
40 assert(fd >= 0);
41 assert(socket_name);
42 assert(packet);
43
ec863ba6 44 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
490aed58 45
4a62c710
MS
46 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0)
47 return log_error_errno(errno, "Failed to send: %m");
490aed58
LP
48
49 return 0;
50}
51
52int main(int argc, char *argv[]) {
1602b008 53 _cleanup_close_ int fd = -1;
490aed58
LP
54 char packet[LINE_MAX];
55 size_t length;
1602b008 56 int r;
490aed58 57
4cfa2c99 58 log_set_target(LOG_TARGET_AUTO);
490aed58
LP
59 log_parse_environment();
60 log_open();
61
62 if (argc != 3) {
63 log_error("Wrong number of arguments.");
1602b008 64 return EXIT_FAILURE;
490aed58
LP
65 }
66
67 if (streq(argv[1], "1")) {
68
69 packet[0] = '+';
70 if (!fgets(packet+1, sizeof(packet)-1, stdin)) {
1602b008 71 r = log_error_errno(errno, "Failed to read password: %m");
490aed58
LP
72 goto finish;
73 }
74
75 truncate_nl(packet+1);
d55f4f3f 76 length = 1 + strlen(packet+1) + 1;
490aed58
LP
77 } else if (streq(argv[1], "0")) {
78 packet[0] = '-';
79 length = 1;
80 } else {
81 log_error("Invalid first argument %s", argv[1]);
1602b008 82 r = -EINVAL;
490aed58
LP
83 goto finish;
84 }
85
03e334a1
LP
86 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
87 if (fd < 0) {
1602b008 88 r = log_error_errno(errno, "socket() failed: %m");
490aed58
LP
89 goto finish;
90 }
91
1602b008 92 r = send_on_socket(fd, argv[2], packet, length);
490aed58
LP
93
94finish:
1602b008 95 memory_erase(packet, sizeof(packet));
490aed58 96
1602b008 97 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
490aed58 98}