]>
| Commit | Line | Data |
|---|---|---|
| db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 490aed58 | 2 | |
| 3ffd4af2 | 3 | #include "fd-util.h" |
| 1fd27861 | 4 | #include "fileio.h" |
| 490aed58 | 5 | #include "log.h" |
| 1cf40697 | 6 | #include "main-func.h" |
| fc2fffe7 | 7 | #include "socket-util.h" |
| 07630cea | 8 | #include "string-util.h" |
| 490aed58 LP |
9 | |
| 10 | static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) { | |
| 15a3e96f LP |
11 | union sockaddr_union sa = {}; |
| 12 | int salen; | |
| 490aed58 LP |
13 | |
| 14 | assert(fd >= 0); | |
| 15 | assert(socket_name); | |
| 16 | assert(packet); | |
| 17 | ||
| 15a3e96f LP |
18 | salen = sockaddr_un_set_path(&sa.un, socket_name); |
| 19 | if (salen < 0) | |
| 20 | return log_error_errno(salen, "Specified socket path for AF_UNIX socket invalid, refusing: %s", socket_name); | |
| 490aed58 | 21 | |
| 15a3e96f | 22 | if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, salen) < 0) |
| 4a62c710 | 23 | return log_error_errno(errno, "Failed to send: %m"); |
| 490aed58 LP |
24 | |
| 25 | return 0; | |
| 26 | } | |
| 27 | ||
| c7dd2095 | 28 | static int run(int argc, char *argv[]) { |
| e693a932 | 29 | _cleanup_(erase_and_freep) char *packet = NULL; |
| 254d1313 | 30 | _cleanup_close_ int fd = -EBADF; |
| fd5ad16c | 31 | size_t length = 0; |
| 1602b008 | 32 | int r; |
| 490aed58 | 33 | |
| d2acb93d | 34 | log_setup(); |
| 490aed58 | 35 | |
| c7dd2095 ZJS |
36 | if (argc != 3) |
| 37 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments."); | |
| 490aed58 LP |
38 | |
| 39 | if (streq(argv[1], "1")) { | |
| 9ae4ef49 | 40 | _cleanup_(erase_and_freep) char *line = NULL; |
| 490aed58 | 41 | |
| 1fd27861 | 42 | r = read_line(stdin, LONG_LINE_MAX, &line); |
| c7dd2095 ZJS |
43 | if (r < 0) |
| 44 | return log_error_errno(r, "Failed to read password: %m"); | |
| 45 | if (r == 0) | |
| 46 | return log_error_errno(SYNTHETIC_ERRNO(EIO), | |
| 47 | "Got EOF while reading password."); | |
| 1fd27861 LP |
48 | |
| 49 | packet = strjoin("+", line); | |
| c7dd2095 ZJS |
50 | if (!packet) |
| 51 | return log_oom(); | |
| 490aed58 | 52 | |
| 1fd27861 LP |
53 | length = 1 + strlen(line) + 1; |
| 54 | ||
| 490aed58 | 55 | } else if (streq(argv[1], "0")) { |
| 1fd27861 | 56 | packet = strdup("-"); |
| c7dd2095 ZJS |
57 | if (!packet) |
| 58 | return log_oom(); | |
| 1fd27861 | 59 | |
| 490aed58 | 60 | length = 1; |
| 1fd27861 | 61 | |
| c7dd2095 ZJS |
62 | } else |
| 63 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
| 64 | "Invalid first argument %s", argv[1]); | |
| 490aed58 | 65 | |
| 03e334a1 | 66 | fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| c7dd2095 ZJS |
67 | if (fd < 0) |
| 68 | return log_error_errno(errno, "socket() failed: %m"); | |
| 490aed58 | 69 | |
| c7dd2095 | 70 | return send_on_socket(fd, argv[2], packet, length); |
| 490aed58 | 71 | } |
| c7dd2095 ZJS |
72 | |
| 73 | DEFINE_MAIN_FUNCTION(run); |