]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
11 #include <sys/socket.h>
17 #include "socket-util.h"
18 #include "string-util.h"
21 static int send_on_socket(int fd
, const char *socket_name
, const void *packet
, size_t size
) {
22 union sockaddr_union sa
= {
23 .un
.sun_family
= AF_UNIX
,
30 strncpy(sa
.un
.sun_path
, socket_name
, sizeof(sa
.un
.sun_path
));
32 if (sendto(fd
, packet
, size
, MSG_NOSIGNAL
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0)
33 return log_error_errno(errno
, "Failed to send: %m");
38 int main(int argc
, char *argv
[]) {
39 _cleanup_close_
int fd
= -1;
40 char packet
[LINE_MAX
];
44 log_set_target(LOG_TARGET_AUTO
);
45 log_parse_environment();
49 log_error("Wrong number of arguments.");
53 if (streq(argv
[1], "1")) {
56 if (!fgets(packet
+1, sizeof(packet
)-1, stdin
)) {
57 r
= log_error_errno(errno
, "Failed to read password: %m");
61 truncate_nl(packet
+1);
62 length
= 1 + strlen(packet
+1) + 1;
63 } else if (streq(argv
[1], "0")) {
67 log_error("Invalid first argument %s", argv
[1]);
72 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
74 r
= log_error_errno(errno
, "socket() failed: %m");
78 r
= send_on_socket(fd
, argv
[2], packet
, length
);
81 explicit_bzero(packet
, sizeof(packet
));
83 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;