]> mj.ucw.cz Git - subauth.git/blob - subauth.c
cb64f94dd2c3b36875edac9bb91b72d6208f560c
[subauth.git] / subauth.c
1 /*
2  *      Sub-authentication Client
3  *
4  *      (c) 2017 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/opt.h>
9
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <sys/un.h>
14 #include <unistd.h>
15
16 #include "autoconf.h"
17
18 static char *socket_path = INSTALL_RUN_DIR "/subauthd.socket";
19
20 static const struct opt_section options = {
21   OPT_ITEMS {
22     OPT_HELP("A client to the sub-authentication daemon."),
23     OPT_HELP("Usage: subauth [options]"),
24     OPT_HELP(""),
25     OPT_HELP("Options:"),
26     OPT_HELP_OPTION,
27     OPT_END
28   }
29 };
30
31 int main(int argc UNUSED, char **argv)
32 {
33   opt_parse(&options, argv+1);
34
35   int sk = socket(PF_UNIX, SOCK_SEQPACKET, 0);
36   if (sk < 0)
37     die("socket(PF_UNIX, SOCK_SEQPACKET): %m");
38
39   struct sockaddr_un sun;
40   sun.sun_family = AF_UNIX;
41   if (strlen(socket_path) >= sizeof(sun.sun_path))
42     die("Socket path too long");
43   strcpy(sun.sun_path, socket_path);
44
45   if (connect(sk, (struct sockaddr *) &sun, sizeof(sun)) < 0)
46     die("Cannot connect to %s: %m", socket_path);
47
48   char msg[] = "Brum!";
49   if (send(sk, msg, sizeof(msg), 0) < 0)
50     die("Send failed: %m");
51
52   return 0;
53 }