chiark / gitweb /
fanotify: use uint64_t instead of __u64
[elogind.git] / src / reply-password.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <sys/socket.h>
23 #include <sys/poll.h>
24 #include <sys/types.h>
25 #include <assert.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/un.h>
31 #include <sys/stat.h>
32 #include <sys/signalfd.h>
33 #include <getopt.h>
34
35 #include "log.h"
36 #include "macro.h"
37 #include "util.h"
38
39 static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
40         union {
41                 struct sockaddr sa;
42                 struct sockaddr_un un;
43         } sa;
44
45         assert(fd >= 0);
46         assert(socket_name);
47         assert(packet);
48
49         zero(sa);
50         sa.un.sun_family = AF_UNIX;
51         strncpy(sa.un.sun_path+1, socket_name, sizeof(sa.un.sun_path)-1);
52
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");
55                 return -1;
56         }
57
58         return 0;
59 }
60
61 int main(int argc, char *argv[]) {
62         int fd = -1, r = EXIT_FAILURE;
63         char packet[LINE_MAX];
64         size_t length;
65
66         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
67         log_parse_environment();
68         log_open();
69
70         if (argc != 3) {
71                 log_error("Wrong number of arguments.");
72                 goto finish;
73         }
74
75         if (streq(argv[1], "1")) {
76
77                 packet[0] = '+';
78                 if (!fgets(packet+1, sizeof(packet)-1, stdin)) {
79                         log_error("Failed to read password: %m");
80                         goto finish;
81                 }
82
83                 truncate_nl(packet+1);
84                 length = strlen(packet+1) + 1;
85         } else if (streq(argv[1], "0")) {
86                 packet[0] = '-';
87                 length = 1;
88         } else {
89                 log_error("Invalid first argument %s", argv[1]);
90                 goto finish;
91         }
92
93         if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
94                 log_error("socket() failed: %m");
95                 goto finish;
96         }
97
98         if (send_on_socket(fd, argv[2], packet, length) < 0)
99                 goto finish;
100
101         r = EXIT_SUCCESS;
102
103 finish:
104         if (fd >= 0)
105                 close_nointr_nofail(fd);
106
107         return r;
108 }