1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
13 #include "memory-util.h"
14 #include "socket-util.h"
15 #include "string-util.h"
18 static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
19 union sockaddr_union sa = {};
26 salen = sockaddr_un_set_path(&sa.un, socket_name);
28 return log_error_errno(salen, "Specified socket path for AF_UNIX socket invalid, refusing: %s", socket_name);
30 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, salen) < 0)
31 return log_error_errno(errno, "Failed to send: %m");
36 static int run(int argc, char *argv[]) {
37 _cleanup_(erase_and_freep) char *packet = NULL;
38 _cleanup_close_ int fd = -1;
45 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments.");
47 if (streq(argv[1], "1")) {
48 _cleanup_(erase_and_freep) char *line = NULL;
50 r = read_line(stdin, LONG_LINE_MAX, &line);
52 return log_error_errno(r, "Failed to read password: %m");
54 return log_error_errno(SYNTHETIC_ERRNO(EIO),
55 "Got EOF while reading password.");
57 packet = strjoin("+", line);
61 length = 1 + strlen(line) + 1;
63 } else if (streq(argv[1], "0")) {
71 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
72 "Invalid first argument %s", argv[1]);
74 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
76 return log_error_errno(errno, "socket() failed: %m");
78 return send_on_socket(fd, argv[2], packet, length);
81 DEFINE_MAIN_FUNCTION(run);