]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
Merge pull request #8700 from keszybz/hibernation
[thirdparty/systemd.git] / src / reply-password / reply-password.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <stddef.h>
10 #include <string.h>
11 #include <sys/socket.h>
12 #include <sys/un.h>
13
14 #include "fd-util.h"
15 #include "log.h"
16 #include "macro.h"
17 #include "socket-util.h"
18 #include "string-util.h"
19 #include "util.h"
20
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,
24 };
25
26 assert(fd >= 0);
27 assert(socket_name);
28 assert(packet);
29
30 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
31
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");
34
35 return 0;
36 }
37
38 int main(int argc, char *argv[]) {
39 _cleanup_close_ int fd = -1;
40 char packet[LINE_MAX];
41 size_t length;
42 int r;
43
44 log_set_target(LOG_TARGET_AUTO);
45 log_parse_environment();
46 log_open();
47
48 if (argc != 3) {
49 log_error("Wrong number of arguments.");
50 return EXIT_FAILURE;
51 }
52
53 if (streq(argv[1], "1")) {
54
55 packet[0] = '+';
56 if (!fgets(packet+1, sizeof(packet)-1, stdin)) {
57 r = log_error_errno(errno, "Failed to read password: %m");
58 goto finish;
59 }
60
61 truncate_nl(packet+1);
62 length = 1 + strlen(packet+1) + 1;
63 } else if (streq(argv[1], "0")) {
64 packet[0] = '-';
65 length = 1;
66 } else {
67 log_error("Invalid first argument %s", argv[1]);
68 r = -EINVAL;
69 goto finish;
70 }
71
72 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
73 if (fd < 0) {
74 r = log_error_errno(errno, "socket() failed: %m");
75 goto finish;
76 }
77
78 r = send_on_socket(fd, argv[2], packet, length);
79
80 finish:
81 explicit_bzero(packet, sizeof(packet));
82
83 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
84 }