57d9ccfacca6a895cff93939462117884c3fc7bf
5 * Admin interface for configuration
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
33 /*----- Global variables --------------------------------------------------*/
37 const trace_opt tr_opts
[] = {
38 { 't', T_TUNNEL
, "tunnel events" },
39 { 'r', T_PEER
, "peer events" },
40 { 'a', T_ADMIN
, "admin interface" },
41 { 'p', T_PACKET
, "packet contents" },
42 { 'c', T_CRYPTO
, "crypto details" },
43 { 's', T_KEYSET
, "symmetric keyset management" },
44 { 'x', T_KEYEXCH
, "key exchange" },
45 { 'm', T_KEYMGMT
, "key management" },
46 { 'A', T_ALL
, "all of the above" },
50 unsigned tr_flags
= 0;
53 static const trace_opt w_opts
[] = {
54 { 't', AF_TRACE
, "trace messages" },
55 { 'n', AF_NOTE
, "asynchronous notifications" },
56 { 'w', AF_WARN
, "warnings" },
57 { 'A', AF_ALLMSGS
, "all of the above" },
61 /*----- Static variables --------------------------------------------------*/
65 static const char *sockname
;
66 static unsigned flags
= 0;
67 static admin
*a_stdin
= 0;
68 static sig s_term
, s_int
, s_hup
;
73 #define T_RESOLVE SEC(30)
75 static void a_destroy(admin */
*a*/
);
76 static void a_lock(admin */
*a*/
);
77 static void a_unlock(admin */
*a*/
);
79 /*----- Output functions --------------------------------------------------*/
81 /* --- @mystrieq@ --- *
83 * Arguments: @const char *x, *y@ = two strings
85 * Returns: True if @x@ and @y are equal, up to case.
88 static int mystrieq(const char *x
, const char *y
)
91 if (!*x
&& !*y
) return (1);
92 if (tolower((unsigned char)*x
) != tolower((unsigned char)*y
))
98 /* --- @trywrite@ --- *
100 * Arguments: @admin *a@ = pointer to an admin block
101 * @const char *p@ = pointer to buffer to write
102 * @size_t sz@ = size of data to write
104 * Returns: The number of bytes written, or less than zero on error.
106 * Use: Attempts to write data to a client.
109 static ssize_t
trywrite(admin
*a
, const char *p
, size_t sz
)
116 n
= write(a
->w
.fd
, p
, sz
);
126 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
128 a_warn("ADMIN client-read-error -- %s", strerror(errno
));
135 /* --- @dosend@ --- *
137 * Arguemnts: @admin *a@ = pointer to an admin block
138 * @const char *p@ = pointer to buffer to write
139 * @size_t sz@ = size of data to write
143 * Use: Sends data to an admin client.
146 static void dosend(admin
*a
, const char *p
, size_t sz
)
154 /* --- Try to send the data immediately --- */
157 if ((n
= trywrite(a
, p
, sz
)) < 0)
165 /* --- Fill buffers with the data until it's all gone --- */
170 else if (o
->p_in
< o
->buf
+ OBUFSZ
)
174 o
= xmalloc(sizeof(obuf
));
176 o
->p_in
= o
->p_out
= o
->buf
;
184 n
= o
->buf
+ OBUFSZ
- o
->p_in
;
187 memcpy(o
->p_in
, p
, n
);
194 /* --- @a_flush@ --- *
196 * Arguments: @int fd@ = file descriptor
197 * @unsigned mode@ = what's happening
198 * @void *v@ = pointer to my admin block
202 * Use: Flushes buffers when a client is ready to read again.
205 static void a_flush(int fd
, unsigned mode
, void *v
)
213 if ((n
= trywrite(a
, o
->p_out
, o
->p_in
- o
->p_out
)) < 0)
216 if (o
->p_in
< o
->p_out
)
229 /*----- Utility functions -------------------------------------------------*/
231 /* --- @a_write@, @a_vwrite@ --- *
233 * Arguments: @admin *a@ = admin connection to write to
234 * @const char *tag@ = tag prefix string, or null
235 * @const char *fmt@ = pointer to format string
236 * @va_list ap@ = arguments in list
237 * @...@ = other arguments
241 * Use: Sends a message to an admin connection.
244 static void a_vwrite(admin
*a
, const char *tag
, const char *fmt
, va_list ap
)
253 dstr_vputf(&d
, fmt
, &ap
);
255 dosend(a
, d
.buf
, d
.len
);
259 static void a_write(admin
*a
, const char *tag
, const char *fmt
, ...)
263 a_vwrite(a
, tag
, fmt
, ap
);
267 /* --- @a_ok@, @a_info@, @a_fail@ --- *
269 * Arguments: @admin *a@ = connection
270 * @const char *fmt@ = format string
271 * @...@ = other arguments
275 * Use: Convenience functions for @a_write@.
278 static void a_ok(admin
*a
) { a_write(a
, "OK", 0); }
280 static void a_info(admin
*a
, const char *fmt
, ...)
284 a_vwrite(a
, "INFO", fmt
, ap
);
288 static void a_fail(admin
*a
, const char *fmt
, ...)
292 a_vwrite(a
, "FAIL", fmt
, ap
);
296 /* --- @a_alert@, @a_valert@, @a_rawalert@ --- *
298 * Arguments: @unsigned f_and, f_eq@ = filter for connections
299 * @const char *tag@ = tag prefix string
300 * @const char *fmt@ = pointer to format string
301 @ @const char *p@ = pointer to raw string
302 * @size_t sz@ = size of raw string
303 * @va_list ap@ = arguments in list
304 * @...@ = other arguments
308 * Use: Write a message to all admin connections matched by the given
312 static void a_rawalert(unsigned f_and
, unsigned f_eq
, const char *tag
,
313 const char *p
, size_t sz
)
318 if (!(flags
& F_INIT
))
326 dstr_putm(&d
, p
, sz
);
330 for (a
= admins
; a
; a
= aa
) {
332 if ((a
->f
& f_and
) == f_eq
)
333 dosend(a
, d
.buf
, d
.len
);
338 static void a_valert(unsigned f_and
, unsigned f_eq
, const char *tag
,
339 const char *fmt
, va_list ap
)
343 if (!(flags
& F_INIT
))
346 dstr_vputf(&d
, fmt
, &ap
);
347 a_rawalert(f_and
, f_eq
, tag
, fmt ? d
.buf
: 0, fmt ? d
.len
: 0);
352 static void a_alert(unsigned f_and
, unsigned f_eq
, const char *tag
,
353 const char *fmt
, ...)
357 a_valert(f_and
, f_eq
, tag
, fmt
, ap
);
362 /* --- @a_warn@ --- *
364 * Arguments: @const char *fmt@ = pointer to format string
365 * @...@ = other arguments
369 * Use: Informs all admin connections of a warning.
372 void a_warn(const char *fmt
, ...)
378 a_valert(0, 0, "WARN", fmt
, ap
);
380 fprintf(stderr
, "%s: ", QUIS
);
381 vfprintf(stderr
, fmt
, ap
);
387 /* --- @a_trace@ --- *
389 * Arguments: @const char *p@ = pointer to a buffer
390 * @size_t sz@ = size of the buffer
391 * @void *v@ = uninteresting pointer
395 * Use: Custom trace output handler. Sends trace messages to
396 * interested admin connections.
400 static void a_trace(const char *p
, size_t sz
, void *v
)
402 a_rawalert(AF_TRACE
, AF_TRACE
, "TRACE", p
, sz
);
406 /* --- @a_notify@ --- *
408 * Arguments: @const char *fmt@ = pointer to format string
409 * @...@ = other arguments
413 * Use: Sends a notification to interested admin connections.
416 void a_notify(const char *fmt
, ...)
421 a_valert(AF_NOTE
, AF_NOTE
, "NOTE", fmt
, ap
);
425 /* --- @a_quit@ --- *
431 * Use: Shuts things down nicely.
441 /* --- @a_sigdie@ --- *
443 * Arguments: @int sig@ = signal number
444 * @void *v@ = an uninteresting argument
448 * Use Shuts down on receipt of a fatal signal.
451 static void a_sigdie(int sig
, void *v
)
457 case SIGTERM
: p
= "SIGTERM"; break;
458 case SIGINT
: p
= "SIGINT"; break;
460 sprintf(buf
, "%i", sig
);
464 a_warn("SERVER quit signal %s", p
);
468 /* --- @a_sighup@ --- *
470 * Arguments: @int sig@ = signal number
471 * @void *v@ = an uninteresting argument
475 * Use Logs a message about SIGHUP not being useful.
478 static void a_sighup(int sig
, void *v
)
480 a_warn("SERVER ignore signal SIGHUP");
483 /*----- Adding peers ------------------------------------------------------*/
485 /* --- @a_resolve@ --- *
487 * Arguments: @struct hostent *h@ = pointer to resolved hostname
488 * @void *v@ = pointer to admin block
492 * Use: Handles a completed name resolution.
495 static void a_resolve(struct hostent
*h
, void *v
)
500 T( trace(T_ADMIN
, "admin: %u resolved", a
->seq
); )
504 a_fail(a
, "resolve-error %s", a
->paddr
);
505 else if (p_find(a
->pname
))
506 a_fail(a
, "peer-exists %s", a
->pname
);
508 memcpy(&a
->peer
.sin
.sin_addr
, h
->h_addr
, sizeof(struct in_addr
));
509 if (!p_create(a
->pname
, &a
->peer
.sa
, a
->sasz
))
510 a_fail(a
, "peer-create-fail %s", a
->pname
);
517 selbuf_enable(&a
->b
);
521 /* --- @a_timer@ --- *
523 * Arguments: @struct timeval *tv@ = timer
524 * @void *v@ = pointer to admin block
528 * Use: Times out a resolver.
531 static void a_timer(struct timeval
*tv
, void *v
)
536 T( trace(T_ADMIN
, "admin: %u resolver timeout", a
->seq
); )
538 a_fail(a
, "resolver-timeout %s\n", a
->paddr
);
542 selbuf_enable(&a
->b
);
546 /* --- @acmd_add@ --- *
548 * Arguments: @admin *a@ = connection which requested the addition
549 * @unsigned ac@ = argument count
550 * @char *av[]@ = pointer to the argument list
554 * Use: Adds a new peer.
557 static void acmd_add(admin
*a
, unsigned ac
, char *av
[])
564 /* --- Make sure someone's not got there already --- */
567 a_fail(a
, "peer-exists %s", av
[0]);
571 /* --- Fill in the easy bits of address --- */
575 if (mystrieq(av
[i
], "inet")) i
++;
577 a_fail(a
, "bad-syntax -- add PEER [inet] ADDRESS PORT");
580 a
->peer
.sin
.sin_family
= AF_INET
;
581 a
->sasz
= sizeof(a
->peer
.sin
);
582 pt
= strtoul(av
[i
+ 1], &p
, 0);
584 struct servent
*s
= getservbyname(av
[i
+ 1], "udp");
586 a_fail(a
, "unknown-service %s", av
[i
+ 1]);
589 pt
= ntohs(s
->s_port
);
591 if (pt
== 0 || pt
>= 65536) {
592 a_fail(a
, "invalid-port %lu", pt
);
595 a
->peer
.sin
.sin_port
= htons(pt
);
597 /* --- If the name is numeric, do it the easy way --- */
599 if (inet_aton(av
[i
], &a
->peer
.sin
.sin_addr
)) {
600 if (!p_create(av
[0], &a
->peer
.sa
, a
->sasz
))
601 a_fail(a
, "peer-create-fail %s", a
->pname
);
607 /* --- Store everything for later and crank up the resolver --- *
609 * We disable the line buffer until the resolver completes (or times out).
610 * This prevents other commands on the same connection (though the rest of
611 * the system continues regardless), but makes life simpler for the client.
614 a
->pname
= xstrdup(av
[0]);
615 a
->paddr
= xstrdup(av
[i
]);
616 selbuf_disable(&a
->b
);
617 gettimeofday(&tv
, 0);
618 tv
.tv_sec
+= T_RESOLVE
;
619 sel_addtimer(&sel
, &a
->t
, &tv
, a_timer
, a
);
620 bres_byname(&a
->r
, a
->paddr
, a_resolve
, a
);
621 T( trace(T_ADMIN
, "admin: %u resolving hostname `%s'",
625 /*----- Administration commands -------------------------------------------*/
627 /* --- Miscellaneous commands --- */
629 /* --- @traceish@ --- *
631 * Arguments: @admin *a@ = connection to complain on
632 * @unsigned ac@ = number of arguments
633 * @char *av[]@ = vector of arguments
634 * @const char *what@ = what we're messing with
635 * @const trace_opt *tt@ = options table
636 * @unsigned *ff@ = where the flags are
638 * Returns: Nonzero if anything changed.
640 * Use: Guts of trace-ish commands like `trace' and `watch'.
643 static int traceish(admin
*a
, unsigned ac
, char *av
[],
644 const char *what
, const trace_opt
*tt
, unsigned *ff
)
648 if (!ac
|| strcmp(av
[0], "?") == 0) {
650 a_info(a
, "Current %s status:", what
);
651 for (t
= tt
; t
->ch
; t
++) {
652 a_info(a
, "%c %c %s",
653 t
->ch
, (*ff
& t
->f
) == t
->f ?
'*' : ' ', t
->help
);
663 case '+': sense
= 1; break;
664 case '-': sense
= 0; break;
666 for (t
= tt
; t
->ch
; t
++) {
668 if (sense
) f
|= t
->f
;
673 a_fail(a
, "bad-%s-option %c", what
, *p
);
689 static void acmd_trace(admin
*a
, unsigned ac
, char *av
[])
691 if (traceish(a
, ac
, av
, "trace", tr_opts
, &tr_flags
))
692 trace_level(tr_flags
);
697 static void acmd_watch(admin
*a
, unsigned ac
, char *av
[])
699 traceish(a
, ac
, av
, "watch", w_opts
, &a
->f
);
702 static void quotify(dstr
*d
, const char *p
)
706 if (*p
&& !p
[strcspn(p
, "\"' \t\n\v")])
711 if (*p
== '\\' || *p
== '\"')
719 static void alertcmd(admin
*a
, unsigned f_and
, unsigned f_eq
,
720 const char *tag
, unsigned ac
, char *av
[])
725 dstr_puts(&d
, "USER");
726 for (i
= 0; i
< ac
; i
++)
729 a_rawalert(f_and
, f_eq
, tag
, d
.buf
, d
.len
);
734 static void acmd_notify(admin
*a
, unsigned ac
, char *av
[])
735 { alertcmd(a
, AF_NOTE
, AF_NOTE
, "NOTE", ac
, av
); }
736 static void acmd_warn(admin
*a
, unsigned ac
, char *av
[])
737 { alertcmd(a
, AF_WARN
, AF_WARN
, "WARN", ac
, av
); }
739 static void acmd_port(admin
*a
, unsigned ac
, char *av
[])
741 a_info(a
, "%u", p_port());
745 static void acmd_daemon(admin
*a
, unsigned ac
, char *av
[])
747 if (flags
& F_DAEMON
)
748 a_fail(a
, "already-daemon");
754 a_fail(a
, "daemon-error -- %s", strerror(errno
));
762 static void acmd_list(admin
*a
, unsigned ac
, char *av
[])
765 for (p
= p_first(); p
; p
= p_next(p
))
766 a_info(a
, "%s", p_name(p
));
770 static void acmd_ifname(admin
*a
, unsigned ac
, char *av
[])
774 if ((p
= p_find(av
[0])) == 0)
775 a_fail(a
, "unknown-peer %s", av
[0]);
777 a_info(a
, "%s", p_ifname(p
));
782 static void acmd_addr(admin
*a
, unsigned ac
, char *av
[])
787 if ((p
= p_find(av
[0])) == 0)
788 a_fail(a
, "unknown-peer %s", av
[0]);
791 assert(ad
->sa
.sa_family
== AF_INET
);
792 a_info(a
, "INET %s %u",
793 inet_ntoa(ad
->sin
.sin_addr
),
794 (unsigned)ntohs(ad
->sin
.sin_port
));
799 static void acmd_stats(admin
*a
, unsigned ac
, char *av
[])
804 if ((p
= p_find(av
[0])) == 0)
805 a_fail(a
, "unknown-peer %s", av
[0]);
808 a_info(a
, "start-time=%s", timestr(st
->t_start
));
809 a_info(a
, "last-packet-time=%s", timestr(st
->t_last
));
810 a_info(a
, "last-keyexch-time=%s", timestr(st
->t_kx
));
811 a_info(a
, "packets-in=%lu bytes-in=%lu", st
->n_in
, st
->sz_in
);
812 a_info(a
, "packets-out=%lu bytes-out=%lu",
813 st
->n_out
, st
->sz_out
);
814 a_info(a
, "keyexch-packets-in=%lu keyexch-bytes-in=%lu",
815 st
->n_kxin
, st
->sz_kxin
);
816 a_info(a
, "keyexch-packets-out=%lu keyexch-bytes-out=%lu",
817 st
->n_kxout
, st
->sz_kxout
);
818 a_info(a
, "ip-packets-in=%lu ip-bytes-in=%lu",
819 st
->n_ipin
, st
->sz_ipin
);
820 a_info(a
, "ip-packets-out=%lu ip-bytes-out=%lu",
821 st
->n_ipout
, st
->sz_ipout
);
822 a_info(a
, "rejected-packets=%lu", st
->n_reject
);
827 static void acmd_kill(admin
*a
, unsigned ac
, char *av
[])
830 if ((p
= p_find(av
[0])) == 0)
831 a_fail(a
, "unknown-peer %s", av
[0]);
838 static void acmd_quit(admin
*a
, unsigned ac
, char *av
[])
840 a_warn("SERVER quit admin-request");
845 static void acmd_version(admin
*a
, unsigned ac
, char *av
[])
847 a_info(a
, "%s %s", PACKAGE
, VERSION
);
851 /* --- The command table and help --- */
853 typedef struct acmd
{
856 unsigned argmin
, argmax
;
857 void (*func
)(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
860 static void acmd_help(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
862 static const acmd acmdtab
[] = {
863 { "help", "help", 0, 0, acmd_help
},
864 { "version", "version", 0, 0, acmd_version
},
866 { "trace", "trace [OPTIONS]", 0, 1, acmd_trace
},
868 { "watch", "watch [OPTIONS]", 0, 1, acmd_watch
},
869 { "notify", "notify MESSAGE ...", 1, 0xffff, acmd_notify
},
870 { "warn", "warn MESSAGE ...", 1, 0xffff, acmd_warn
},
871 { "port", "port", 0, 0, acmd_port
},
872 { "daemon", "daemon", 0, 0, acmd_daemon
},
873 { "list", "list", 0, 0, acmd_list
},
874 { "ifname", "ifname PEER", 1, 1, acmd_ifname
},
875 { "addr", "addr PEER", 1, 1, acmd_addr
},
876 { "stats", "stats PEER", 1, 1, acmd_stats
},
877 { "kill", "kill PEER", 1, 1, acmd_kill
},
878 { "add", "add PEER ADDR ...", 2, 0xffff, acmd_add
},
879 { "quit", "quit", 0, 0, acmd_quit
},
883 static void acmd_help(admin
*a
, unsigned ac
, char *av
[])
886 for (c
= acmdtab
; c
->name
; c
++)
887 a_info(a
, "%s", c
->help
);
891 /*----- Connection handling -----------------------------------------------*/
893 /* --- @a_lock@ --- *
895 * Arguments: @admin *a@ = pointer to an admin block
899 * Use: Locks an admin block so that it won't be destroyed
903 static void a_lock(admin
*a
) { assert(!(a
->f
& AF_LOCK
)); a
->f
|= AF_LOCK
; }
905 /* --- @a_unlock@ --- *
907 * Arguments: @admin *a@ = pointer to an admin block
911 * Use: Unlocks an admin block, allowing its destruction. This is
912 * also the second half of @a_destroy@.
915 static void a_unlock(admin
*a
)
917 assert(a
->f
& AF_LOCK
);
918 if (!(a
->f
& AF_DEAD
)) {
923 T( trace(T_ADMIN
, "admin: completing destruction of connection %u",
926 selbuf_destroy(&a
->b
);
933 if (a
->b
.reader
.fd
!= a
->w
.fd
)
934 close(a
->b
.reader
.fd
);
940 a
->next
->prev
= a
->prev
;
942 a
->prev
->next
= a
->next
;
948 /* --- @a_destroy@ --- *
950 * Arguments: @admin *a@ = pointer to an admin block
954 * Use: Destroys an admin block. This requires a certain amount of
958 static void a_destroy(admin
*a
)
960 /* --- Don't multiply destroy admin blocks --- */
965 /* --- Make sure nobody expects it to work --- */
968 T( trace(T_ADMIN
, "admin: destroying connection %u", a
->seq
); )
970 /* --- Free the output buffers --- */
975 for (o
= a
->o_head
; o
; o
= oo
) {
982 /* --- If the block is locked, that's all we can manage --- */
984 if (a
->f
& AF_LOCK
) {
985 T( trace(T_ADMIN
, "admin: deferring destruction..."); )
992 /* --- @a_line@ --- *
994 * Arguments: @char *p@ = pointer to the line read
995 * @size_t len@ = length of the line
996 * @void *vp@ = pointer to my admin block
1000 * Use: Handles a line of input.
1003 static void a_line(char *p
, size_t len
, void *vp
)
1017 ac
= str_qsplit(p
, av
, 16, 0, STRF_QUOTE
);
1020 for (c
= acmdtab
; c
->name
; c
++) {
1021 if (mystrieq(av
[0], c
->name
)) {
1023 if (c
->argmin
> ac
|| ac
> c
->argmax
)
1024 a_fail(a
, "bad-syntax -- %s", c
->help
);
1027 c
->func(a
, ac
, av
+ 1);
1033 a_fail(a
, "unknown-command %s", av
[0]);
1036 /* --- @a_create@ --- *
1038 * Arguments: @int fd_in, fd_out@ = file descriptors to use
1039 * @unsigned f@ = initial flags to set
1043 * Use: Creates a new admin connection.
1046 void a_create(int fd_in
, int fd_out
, unsigned f
)
1048 admin
*a
= CREATE(admin
);
1050 T( static unsigned seq
= 0;
1052 T( trace(T_ADMIN
, "admin: accepted connection %u", a
->seq
); )
1055 if (fd_in
== STDIN_FILENO
)
1057 fdflags(fd_in
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
1058 if (fd_out
!= fd_in
)
1059 fdflags(fd_out
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
1060 selbuf_init(&a
->b
, &sel
, fd_in
, a_line
, a
);
1061 sel_initfile(&sel
, &a
->w
, fd_out
, SEL_WRITE
, a_flush
, a
);
1071 /* --- @a_accept@ --- *
1073 * Arguments: @int fd@ = file descriptor to accept
1074 * @unsigned mode@ = what to do
1075 * @void *v@ = uninteresting pointer
1079 * Use: Accepts a new admin connection.
1082 static void a_accept(int fd
, unsigned mode
, void *v
)
1085 struct sockaddr_un sun
;
1086 size_t sz
= sizeof(sun
);
1088 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &sz
)) < 0) {
1089 if (errno
!= EINTR
&& errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
1090 errno
!= ECONNABORTED
&& errno
!= EPROTO
)
1091 a_warn("ADMIN accept-error -- %s", strerror(errno
));
1094 a_create(nfd
, nfd
, 0);
1097 /* --- @a_daemon@ --- *
1103 * Use: Informs the admin module that it's a daemon.
1111 /* --- @a_init@ --- *
1113 * Arguments: @const char *name@ = socket name to create
1117 * Use: Creates the admin listening socket.
1120 void a_init(const char *name
)
1124 struct sockaddr_un sun
;
1125 struct sigaction sa
;
1128 /* --- Set up the socket address --- */
1130 sz
= strlen(name
) + 1;
1131 if (sz
> sizeof(sun
.sun_path
))
1132 die(EXIT_FAILURE
, "socket name `%s' too long", name
);
1134 sun
.sun_family
= AF_UNIX
;
1135 memcpy(sun
.sun_path
, name
, sz
);
1136 sz
+= offsetof(struct sockaddr_un
, sun_path
);
1138 /* --- Attempt to bind to the socket --- */
1142 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
1143 die(EXIT_FAILURE
, "couldn't create socket: %s", strerror(errno
));
1144 if (bind(fd
, (struct sockaddr
*)&sun
, sz
) < 0) {
1147 if (errno
!= EADDRINUSE
) {
1148 die(EXIT_FAILURE
, "couldn't bind to address `%s': %s",
1149 sun
.sun_path
, strerror(e
));
1152 die(EXIT_FAILURE
, "too many retries; giving up");
1154 if (!connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
1155 die(EXIT_FAILURE
, "server already listening on admin socket `%s'",
1158 if (errno
!= ECONNREFUSED
)
1159 die(EXIT_FAILURE
, "couldn't bind to address: %s", strerror(e
));
1160 if (stat(sun
.sun_path
, &st
)) {
1161 die(EXIT_FAILURE
, "couldn't stat `%s': %s",
1162 sun
.sun_path
, strerror(errno
));
1164 if (!S_ISSOCK(st
.st_mode
))
1165 die(EXIT_FAILURE
, "object `%s' isn't a socket", sun
.sun_path
);
1166 T( trace(T_ADMIN
, "admin: stale socket found; removing it"); )
1167 unlink(sun
.sun_path
);
1171 chmod(sun
.sun_path
, 0600);
1172 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
1174 die(EXIT_FAILURE
, "couldn't listen on socket: %s", strerror(errno
));
1176 /* --- Listen to the socket --- */
1178 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, a_accept
, 0);
1182 T( trace_custom(a_trace
, 0);
1183 trace(T_ADMIN
, "admin: enabled custom tracing"); )
1186 /* --- Set up signal handlers --- */
1188 sig_add(&s_term
, SIGTERM
, a_sigdie
, 0);
1189 sig_add(&s_hup
, SIGHUP
, a_sighup
, 0);
1190 signal(SIGPIPE
, SIG_IGN
);
1191 sigaction(SIGINT
, 0, &sa
);
1192 if (sa
.sa_handler
!= SIG_IGN
)
1193 sig_add(&s_int
, SIGINT
, a_sigdie
, 0);
1196 /*----- That's all, folks -------------------------------------------------*/