1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
24 #include <sys/types.h>
32 #include <sys/signalfd.h>
40 static int send_on_socket(int fd
, const char *socket_name
, const void *packet
, size_t size
) {
43 struct sockaddr_un un
;
51 sa
.un
.sun_family
= AF_UNIX
;
52 strncpy(sa
.un
.sun_path
, socket_name
, sizeof(sa
.un
.sun_path
));
54 if (sendto(fd
, packet
, size
, MSG_NOSIGNAL
, &sa
.sa
, offsetof(struct sockaddr_un
, sun_path
) + strlen(socket_name
)) < 0) {
55 log_error("Failed to send: %m");
62 int main(int argc
, char *argv
[]) {
63 int fd
= -1, r
= EXIT_FAILURE
;
64 char packet
[LINE_MAX
];
67 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG
);
68 log_parse_environment();
72 log_error("Wrong number of arguments.");
76 if (streq(argv
[1], "1")) {
79 if (!fgets(packet
+1, sizeof(packet
)-1, stdin
)) {
80 log_error("Failed to read password: %m");
84 truncate_nl(packet
+1);
85 length
= strlen(packet
+1) + 1;
86 } else if (streq(argv
[1], "0")) {
90 log_error("Invalid first argument %s", argv
[1]);
94 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0)) < 0) {
95 log_error("socket() failed: %m");
99 if (send_on_socket(fd
, argv
[2], packet
, length
) < 0)
106 close_nointr_nofail(fd
);