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>
39 static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
42 struct sockaddr_un un;
50 sa.un.sun_family = AF_UNIX;
51 strncpy(sa.un.sun_path+1, socket_name, sizeof(sa.un.sun_path)-1);
53 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, sizeof(sa_family_t) + 1 + strlen(socket_name)) < 0) {
54 log_error("Failed to send: %m");
61 int main(int argc, char *argv[]) {
62 int fd = -1, r = EXIT_FAILURE;
63 char packet[LINE_MAX];
66 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
67 log_parse_environment();
71 log_error("Wrong number of arguments.");
75 if (streq(argv[1], "1")) {
78 if (!fgets(packet+1, sizeof(packet)-1, stdin)) {
79 log_error("Failed to read password: %m");
83 truncate_nl(packet+1);
84 length = strlen(packet+1) + 1;
85 } else if (streq(argv[1], "0")) {
89 log_error("Invalid first argument %s", argv[1]);
93 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
94 log_error("socket() failed: %m");
98 if (send_on_socket(fd, argv[2], packet, length) < 0)
105 close_nointr_nofail(fd);