tizen: gcc14 fix: Fix pointer type mismatch
[platform/upstream/systemd.git] / src / reply-password / reply-password.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stddef.h>
5 #include <sys/un.h>
6
7 #include "alloc-util.h"
8 #include "main-func.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "log.h"
12 #include "macro.h"
13 #include "memory-util.h"
14 #include "socket-util.h"
15 #include "string-util.h"
16 #include "util.h"
17
18 static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
19         union sockaddr_union sa = {};
20         int salen;
21
22         assert(fd >= 0);
23         assert(socket_name);
24         assert(packet);
25
26         salen = sockaddr_un_set_path(&sa.un, socket_name);
27         if (salen < 0)
28                 return log_error_errno(salen, "Specified socket path for AF_UNIX socket invalid, refusing: %s", socket_name);
29
30         if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, salen) < 0)
31                 return log_error_errno(errno, "Failed to send: %m");
32
33         return 0;
34 }
35
36 static int run(int argc, char *argv[]) {
37         _cleanup_(erase_and_freep) char *packet = NULL;
38         _cleanup_close_ int fd = -1;
39         size_t length = 0;
40         int r;
41
42         log_setup_service();
43
44         if (argc != 3)
45                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments.");
46
47         if (streq(argv[1], "1")) {
48                 _cleanup_(erase_and_freep) char *line = NULL;
49
50                 r = read_line(stdin, LONG_LINE_MAX, &line);
51                 if (r < 0)
52                         return log_error_errno(r, "Failed to read password: %m");
53                 if (r == 0)
54                         return log_error_errno(SYNTHETIC_ERRNO(EIO),
55                                                "Got EOF while reading password.");
56
57                 packet = strjoin("+", line);
58                 if (!packet)
59                         return log_oom();
60
61                 length = 1 + strlen(line) + 1;
62
63         } else if (streq(argv[1], "0")) {
64                 packet = strdup("-");
65                 if (!packet)
66                         return log_oom();
67
68                 length = 1;
69
70         } else
71                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
72                                        "Invalid first argument %s", argv[1]);
73
74         fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
75         if (fd < 0)
76                 return log_error_errno(errno, "socket() failed: %m");
77
78         return send_on_socket(fd, argv[2], packet, length);
79 }
80
81 DEFINE_MAIN_FUNCTION(run);