]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
23 #include <sys/socket.h>
29 #include "socket-util.h"
30 #include "string-util.h"
33 static int send_on_socket(int fd
, const char *socket_name
, const void *packet
, size_t size
) {
34 union sockaddr_union sa
= {
35 .un
.sun_family
= AF_UNIX
,
42 strncpy(sa
.un
.sun_path
, socket_name
, sizeof(sa
.un
.sun_path
));
44 if (sendto(fd
, packet
, size
, MSG_NOSIGNAL
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0)
45 return log_error_errno(errno
, "Failed to send: %m");
50 int main(int argc
, char *argv
[]) {
51 _cleanup_close_
int fd
= -1;
52 char packet
[LINE_MAX
];
56 log_set_target(LOG_TARGET_AUTO
);
57 log_parse_environment();
61 log_error("Wrong number of arguments.");
65 if (streq(argv
[1], "1")) {
68 if (!fgets(packet
+1, sizeof(packet
)-1, stdin
)) {
69 r
= log_error_errno(errno
, "Failed to read password: %m");
73 truncate_nl(packet
+1);
74 length
= 1 + strlen(packet
+1) + 1;
75 } else if (streq(argv
[1], "0")) {
79 log_error("Invalid first argument %s", argv
[1]);
84 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
86 r
= log_error_errno(errno
, "socket() failed: %m");
90 r
= send_on_socket(fd
, argv
[2], packet
, length
);
93 memory_erase(packet
, sizeof(packet
));
95 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;