]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
490aed58 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
490aed58 LP |
6 | ***/ |
7 | ||
490aed58 | 8 | #include <errno.h> |
0e098b15 | 9 | #include <stddef.h> |
07630cea LP |
10 | #include <string.h> |
11 | #include <sys/socket.h> | |
12 | #include <sys/un.h> | |
490aed58 | 13 | |
3ffd4af2 | 14 | #include "fd-util.h" |
490aed58 LP |
15 | #include "log.h" |
16 | #include "macro.h" | |
fc2fffe7 | 17 | #include "socket-util.h" |
07630cea | 18 | #include "string-util.h" |
490aed58 LP |
19 | #include "util.h" |
20 | ||
21 | static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) { | |
fc2fffe7 | 22 | union sockaddr_union sa = { |
b92bea5d ZJS |
23 | .un.sun_family = AF_UNIX, |
24 | }; | |
490aed58 LP |
25 | |
26 | assert(fd >= 0); | |
27 | assert(socket_name); | |
28 | assert(packet); | |
29 | ||
ec863ba6 | 30 | strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path)); |
490aed58 | 31 | |
fc2fffe7 | 32 | if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) |
4a62c710 | 33 | return log_error_errno(errno, "Failed to send: %m"); |
490aed58 LP |
34 | |
35 | return 0; | |
36 | } | |
37 | ||
38 | int main(int argc, char *argv[]) { | |
1602b008 | 39 | _cleanup_close_ int fd = -1; |
490aed58 LP |
40 | char packet[LINE_MAX]; |
41 | size_t length; | |
1602b008 | 42 | int r; |
490aed58 | 43 | |
4cfa2c99 | 44 | log_set_target(LOG_TARGET_AUTO); |
490aed58 LP |
45 | log_parse_environment(); |
46 | log_open(); | |
47 | ||
48 | if (argc != 3) { | |
49 | log_error("Wrong number of arguments."); | |
1602b008 | 50 | return EXIT_FAILURE; |
490aed58 LP |
51 | } |
52 | ||
53 | if (streq(argv[1], "1")) { | |
54 | ||
55 | packet[0] = '+'; | |
56 | if (!fgets(packet+1, sizeof(packet)-1, stdin)) { | |
1602b008 | 57 | r = log_error_errno(errno, "Failed to read password: %m"); |
490aed58 LP |
58 | goto finish; |
59 | } | |
60 | ||
61 | truncate_nl(packet+1); | |
d55f4f3f | 62 | length = 1 + strlen(packet+1) + 1; |
490aed58 LP |
63 | } else if (streq(argv[1], "0")) { |
64 | packet[0] = '-'; | |
65 | length = 1; | |
66 | } else { | |
67 | log_error("Invalid first argument %s", argv[1]); | |
1602b008 | 68 | r = -EINVAL; |
490aed58 LP |
69 | goto finish; |
70 | } | |
71 | ||
03e334a1 LP |
72 | fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
73 | if (fd < 0) { | |
1602b008 | 74 | r = log_error_errno(errno, "socket() failed: %m"); |
490aed58 LP |
75 | goto finish; |
76 | } | |
77 | ||
1602b008 | 78 | r = send_on_socket(fd, argv[2], packet, length); |
490aed58 LP |
79 | |
80 | finish: | |
2d26d8e0 | 81 | explicit_bzero(packet, sizeof(packet)); |
490aed58 | 82 | |
1602b008 | 83 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
490aed58 | 84 | } |