24d86c64ac40dcf7a33e61c84d6ccb2eac48ae6a
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 /* --- @trywrite@ --- *
83 * Arguments: @admin *a@ = pointer to an admin block
84 * @const char *p@ = pointer to buffer to write
85 * @size_t sz@ = size of data to write
87 * Returns: The number of bytes written, or less than zero on error.
89 * Use: Attempts to write data to a client.
92 static ssize_t
trywrite(admin
*a
, const char *p
, size_t sz
)
99 n
= write(a
->w
.fd
, p
, sz
);
109 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
111 a_warn("ADMIN client-read-error -- %s", strerror(errno
));
118 /* --- @dosend@ --- *
120 * Arguemnts: @admin *a@ = pointer to an admin block
121 * @const char *p@ = pointer to buffer to write
122 * @size_t sz@ = size of data to write
126 * Use: Sends data to an admin client.
129 static void dosend(admin
*a
, const char *p
, size_t sz
)
137 /* --- Try to send the data immediately --- */
140 if ((n
= trywrite(a
, p
, sz
)) < 0)
148 /* --- Fill buffers with the data until it's all gone --- */
153 else if (o
->p_in
< o
->buf
+ OBUFSZ
)
157 o
= xmalloc(sizeof(obuf
));
159 o
->p_in
= o
->p_out
= o
->buf
;
167 n
= o
->buf
+ OBUFSZ
- o
->p_in
;
170 memcpy(o
->p_in
, p
, n
);
177 /* --- @a_flush@ --- *
179 * Arguments: @int fd@ = file descriptor
180 * @unsigned mode@ = what's happening
181 * @void *v@ = pointer to my admin block
185 * Use: Flushes buffers when a client is ready to read again.
188 static void a_flush(int fd
, unsigned mode
, void *v
)
196 if ((n
= trywrite(a
, o
->p_out
, o
->p_in
- o
->p_out
)) < 0)
199 if (o
->p_in
< o
->p_out
)
212 /*----- Utility functions -------------------------------------------------*/
214 /* --- @a_write@, @a_vwrite@ --- *
216 * Arguments: @admin *a@ = admin connection to write to
217 * @const char *tag@ = tag prefix string, or null
218 * @const char *fmt@ = pointer to format string
219 * @va_list ap@ = arguments in list
220 * @...@ = other arguments
224 * Use: Sends a message to an admin connection.
227 static void a_vwrite(admin
*a
, const char *tag
, const char *fmt
, va_list ap
)
236 dstr_vputf(&d
, fmt
, &ap
);
238 dosend(a
, d
.buf
, d
.len
);
242 static void a_write(admin
*a
, const char *tag
, const char *fmt
, ...)
246 a_vwrite(a
, tag
, fmt
, ap
);
250 /* --- @a_ok@, @a_info@, @a_fail@ --- *
252 * Arguments: @admin *a@ = connection
253 * @const char *fmt@ = format string
254 * @...@ = other arguments
258 * Use: Convenience functions for @a_write@.
261 static void a_ok(admin
*a
) { a_write(a
, "OK", 0); }
263 static void a_info(admin
*a
, const char *fmt
, ...)
267 a_vwrite(a
, "INFO", fmt
, ap
);
271 static void a_fail(admin
*a
, const char *fmt
, ...)
275 a_vwrite(a
, "FAIL", fmt
, ap
);
279 /* --- @a_alert@, @a_valert@, @a_rawalert@ --- *
281 * Arguments: @unsigned f_and, f_eq@ = filter for connections
282 * @const char *tag@ = tag prefix string
283 * @const char *fmt@ = pointer to format string
284 @ @const char *p@ = pointer to raw string
285 * @size_t sz@ = size of raw string
286 * @va_list ap@ = arguments in list
287 * @...@ = other arguments
291 * Use: Write a message to all admin connections matched by the given
295 static void a_rawalert(unsigned f_and
, unsigned f_eq
, const char *tag
,
296 const char *p
, size_t sz
)
301 if (!(flags
& F_INIT
))
309 dstr_putm(&d
, p
, sz
);
313 for (a
= admins
; a
; a
= aa
) {
315 if ((a
->f
& f_and
) == f_eq
)
316 dosend(a
, d
.buf
, d
.len
);
321 static void a_valert(unsigned f_and
, unsigned f_eq
, const char *tag
,
322 const char *fmt
, va_list ap
)
326 if (!(flags
& F_INIT
))
329 dstr_vputf(&d
, fmt
, &ap
);
330 a_rawalert(f_and
, f_eq
, tag
, fmt ? d
.buf
: 0, fmt ? d
.len
: 0);
335 static void a_alert(unsigned f_and
, unsigned f_eq
, const char *tag
,
336 const char *fmt
, ...)
340 a_valert(f_and
, f_eq
, tag
, fmt
, ap
);
345 /* --- @a_warn@ --- *
347 * Arguments: @const char *fmt@ = pointer to format string
348 * @...@ = other arguments
352 * Use: Informs all admin connections of a warning.
355 void a_warn(const char *fmt
, ...)
361 a_valert(0, 0, "WARN", fmt
, ap
);
363 fprintf(stderr
, "%s: ", QUIS
);
364 vfprintf(stderr
, fmt
, ap
);
370 /* --- @a_trace@ --- *
372 * Arguments: @const char *p@ = pointer to a buffer
373 * @size_t sz@ = size of the buffer
374 * @void *v@ = uninteresting pointer
378 * Use: Custom trace output handler. Sends trace messages to
379 * interested admin connections.
383 static void a_trace(const char *p
, size_t sz
, void *v
)
385 a_rawalert(AF_TRACE
, AF_TRACE
, "TRACE", p
, sz
);
389 /* --- @a_notify@ --- *
391 * Arguments: @const char *fmt@ = pointer to format string
392 * @...@ = other arguments
396 * Use: Sends a notification to interested admin connections.
399 void a_notify(const char *fmt
, ...)
404 a_valert(AF_NOTE
, AF_NOTE
, "NOTE", fmt
, ap
);
408 /* --- @a_quit@ --- *
414 * Use: Shuts things down nicely.
421 while ((p
= p_first()) != 0)
428 /* --- @a_sigdie@ --- *
430 * Arguments: @int sig@ = signal number
431 * @void *v@ = an uninteresting argument
435 * Use Shuts down on receipt of a fatal signal.
438 static void a_sigdie(int sig
, void *v
)
444 case SIGTERM
: p
= "SIGTERM"; break;
445 case SIGINT
: p
= "SIGINT"; break;
447 sprintf(buf
, "%i", sig
);
451 a_warn("SERVER quit signal %s", p
);
455 /* --- @a_sighup@ --- *
457 * Arguments: @int sig@ = signal number
458 * @void *v@ = an uninteresting argument
462 * Use Logs a message about SIGHUP not being useful.
465 static void a_sighup(int sig
, void *v
)
467 a_warn("SERVER ignore signal SIGHUP");
470 /*----- Adding peers ------------------------------------------------------*/
472 /* --- @a_resolve@ --- *
474 * Arguments: @struct hostent *h@ = pointer to resolved hostname
475 * @void *v@ = pointer to admin block
479 * Use: Handles a completed name resolution.
482 static void a_resolve(struct hostent
*h
, void *v
)
487 T( trace(T_ADMIN
, "admin: %u resolved", a
->seq
); )
491 a_fail(a
, "resolve-error %s", a
->paddr
);
492 else if (p_find(a
->pname
))
493 a_fail(a
, "peer-exists %s", a
->pname
);
495 memcpy(&a
->peer
.sin
.sin_addr
, h
->h_addr
, sizeof(struct in_addr
));
496 if (!p_create(a
->pname
, a
->tops
, &a
->peer
.sa
, a
->sasz
))
497 a_fail(a
, "peer-create-fail %s", a
->pname
);
504 selbuf_enable(&a
->b
);
508 /* --- @a_timer@ --- *
510 * Arguments: @struct timeval *tv@ = timer
511 * @void *v@ = pointer to admin block
515 * Use: Times out a resolver.
518 static void a_timer(struct timeval
*tv
, void *v
)
523 T( trace(T_ADMIN
, "admin: %u resolver timeout", a
->seq
); )
525 a_fail(a
, "resolver-timeout %s\n", a
->paddr
);
529 selbuf_enable(&a
->b
);
533 /* --- @acmd_add@ --- *
535 * Arguments: @admin *a@ = connection which requested the addition
536 * @unsigned ac@ = argument count
537 * @char *av[]@ = pointer to the argument list
541 * Use: Adds a new peer.
544 static void acmd_add(admin
*a
, unsigned ac
, char *av
[])
549 const tunnel_ops
*tops
= tun_default
;
552 /* --- Make sure someone's not got there already --- */
555 a_fail(a
, "peer-exists %s", av
[0]);
559 /* --- Parse options --- */
565 if (mystrieq(av
[i
], "-tunnel")) {
571 a_fail(a
, "unknown-tunnel %s", av
[i
]);
574 if (mystrieq(av
[i
], tunnels
[j
]->name
)) {
580 } else if (mystrieq(av
[i
], "--")) {
587 /* --- Fill in the easy bits of address --- */
590 if (mystrieq(av
[i
], "inet")) i
++;
592 a_fail(a
, "bad-syntax -- add PEER [-tunnel TUN] [inet] ADDRESS PORT");
595 a
->peer
.sin
.sin_family
= AF_INET
;
596 a
->sasz
= sizeof(a
->peer
.sin
);
597 pt
= strtoul(av
[i
+ 1], &p
, 0);
599 struct servent
*s
= getservbyname(av
[i
+ 1], "udp");
601 a_fail(a
, "unknown-service %s", av
[i
+ 1]);
604 pt
= ntohs(s
->s_port
);
606 if (pt
== 0 || pt
>= 65536) {
607 a_fail(a
, "invalid-port %lu", pt
);
610 a
->peer
.sin
.sin_port
= htons(pt
);
612 /* --- If the name is numeric, do it the easy way --- */
614 if (inet_aton(av
[i
], &a
->peer
.sin
.sin_addr
)) {
615 if (!p_create(av
[0], tops
, &a
->peer
.sa
, a
->sasz
))
616 a_fail(a
, "peer-create-fail %s", a
->pname
);
622 /* --- Store everything for later and crank up the resolver --- *
624 * We disable the line buffer until the resolver completes (or times out).
625 * This prevents other commands on the same connection (though the rest of
626 * the system continues regardless), but makes life simpler for the client.
629 a
->pname
= xstrdup(av
[0]);
630 a
->paddr
= xstrdup(av
[i
]);
632 selbuf_disable(&a
->b
);
633 gettimeofday(&tv
, 0);
634 tv
.tv_sec
+= T_RESOLVE
;
635 sel_addtimer(&sel
, &a
->t
, &tv
, a_timer
, a
);
636 bres_byname(&a
->r
, a
->paddr
, a_resolve
, a
);
637 T( trace(T_ADMIN
, "admin: %u resolving hostname `%s'",
642 a_fail(a
, "bad-syntax -- add PEER [-tunnel TUN] ADDR ...");
646 /*----- Administration commands -------------------------------------------*/
648 /* --- Miscellaneous commands --- */
650 /* --- @traceish@ --- *
652 * Arguments: @admin *a@ = connection to complain on
653 * @unsigned ac@ = number of arguments
654 * @char *av[]@ = vector of arguments
655 * @const char *what@ = what we're messing with
656 * @const trace_opt *tt@ = options table
657 * @unsigned *ff@ = where the flags are
659 * Returns: Nonzero if anything changed.
661 * Use: Guts of trace-ish commands like `trace' and `watch'.
664 static int traceish(admin
*a
, unsigned ac
, char *av
[],
665 const char *what
, const trace_opt
*tt
, unsigned *ff
)
669 if (!ac
|| strcmp(av
[0], "?") == 0) {
671 a_info(a
, "Current %s status:", what
);
672 for (t
= tt
; t
->ch
; t
++) {
673 a_info(a
, "%c %c %s",
674 t
->ch
, (*ff
& t
->f
) == t
->f ?
'*' : ' ', t
->help
);
684 case '+': sense
= 1; break;
685 case '-': sense
= 0; break;
687 for (t
= tt
; t
->ch
; t
++) {
689 if (sense
) f
|= t
->f
;
694 a_fail(a
, "bad-%s-option %c", what
, *p
);
710 static void acmd_trace(admin
*a
, unsigned ac
, char *av
[])
712 if (traceish(a
, ac
, av
, "trace", tr_opts
, &tr_flags
))
713 trace_level(tr_flags
);
718 static void acmd_watch(admin
*a
, unsigned ac
, char *av
[])
720 traceish(a
, ac
, av
, "watch", w_opts
, &a
->f
);
723 static void quotify(dstr
*d
, const char *p
)
727 if (*p
&& !p
[strcspn(p
, "\"' \t\n\v")])
732 if (*p
== '\\' || *p
== '\"')
740 static void alertcmd(admin
*a
, unsigned f_and
, unsigned f_eq
,
741 const char *tag
, unsigned ac
, char *av
[])
746 dstr_puts(&d
, "USER");
747 for (i
= 0; i
< ac
; i
++)
750 a_rawalert(f_and
, f_eq
, tag
, d
.buf
, d
.len
);
755 static void acmd_notify(admin
*a
, unsigned ac
, char *av
[])
756 { alertcmd(a
, AF_NOTE
, AF_NOTE
, "NOTE", ac
, av
); }
757 static void acmd_warn(admin
*a
, unsigned ac
, char *av
[])
758 { alertcmd(a
, AF_WARN
, AF_WARN
, "WARN", ac
, av
); }
760 static void acmd_port(admin
*a
, unsigned ac
, char *av
[])
762 a_info(a
, "%u", p_port());
766 static void acmd_daemon(admin
*a
, unsigned ac
, char *av
[])
768 if (flags
& F_DAEMON
)
769 a_fail(a
, "already-daemon");
775 a_fail(a
, "daemon-error -- %s", strerror(errno
));
783 static void acmd_list(admin
*a
, unsigned ac
, char *av
[])
786 for (p
= p_first(); p
; p
= p_next(p
))
787 a_info(a
, "%s", p_name(p
));
791 static void acmd_ifname(admin
*a
, unsigned ac
, char *av
[])
795 if ((p
= p_find(av
[0])) == 0)
796 a_fail(a
, "unknown-peer %s", av
[0]);
798 a_info(a
, "%s", p_ifname(p
));
803 static void acmd_addr(admin
*a
, unsigned ac
, char *av
[])
808 if ((p
= p_find(av
[0])) == 0)
809 a_fail(a
, "unknown-peer %s", av
[0]);
812 assert(ad
->sa
.sa_family
== AF_INET
);
813 a_info(a
, "INET %s %u",
814 inet_ntoa(ad
->sin
.sin_addr
),
815 (unsigned)ntohs(ad
->sin
.sin_port
));
820 static void acmd_stats(admin
*a
, unsigned ac
, char *av
[])
825 if ((p
= p_find(av
[0])) == 0)
826 a_fail(a
, "unknown-peer %s", av
[0]);
829 a_info(a
, "start-time=%s", timestr(st
->t_start
));
830 a_info(a
, "last-packet-time=%s", timestr(st
->t_last
));
831 a_info(a
, "last-keyexch-time=%s", timestr(st
->t_kx
));
832 a_info(a
, "packets-in=%lu bytes-in=%lu", st
->n_in
, st
->sz_in
);
833 a_info(a
, "packets-out=%lu bytes-out=%lu",
834 st
->n_out
, st
->sz_out
);
835 a_info(a
, "keyexch-packets-in=%lu keyexch-bytes-in=%lu",
836 st
->n_kxin
, st
->sz_kxin
);
837 a_info(a
, "keyexch-packets-out=%lu keyexch-bytes-out=%lu",
838 st
->n_kxout
, st
->sz_kxout
);
839 a_info(a
, "ip-packets-in=%lu ip-bytes-in=%lu",
840 st
->n_ipin
, st
->sz_ipin
);
841 a_info(a
, "ip-packets-out=%lu ip-bytes-out=%lu",
842 st
->n_ipout
, st
->sz_ipout
);
843 a_info(a
, "rejected-packets=%lu", st
->n_reject
);
848 static void acmd_kill(admin
*a
, unsigned ac
, char *av
[])
851 if ((p
= p_find(av
[0])) == 0)
852 a_fail(a
, "unknown-peer %s", av
[0]);
859 static void acmd_quit(admin
*a
, unsigned ac
, char *av
[])
861 a_warn("SERVER quit admin-request");
866 static void acmd_version(admin
*a
, unsigned ac
, char *av
[])
868 a_info(a
, "%s %s", PACKAGE
, VERSION
);
872 static void acmd_tunnels(admin
*a
, unsigned ac
, char *av
[])
875 for (i
= 0; tunnels
[i
]; i
++)
876 a_info(a
, "%s", tunnels
[i
]->name
);
880 /* --- The command table and help --- */
882 typedef struct acmd
{
885 unsigned argmin
, argmax
;
886 void (*func
)(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
889 static void acmd_help(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
891 static const acmd acmdtab
[] = {
892 { "help", "help", 0, 0, acmd_help
},
893 { "version", "version", 0, 0, acmd_version
},
895 { "trace", "trace [OPTIONS]", 0, 1, acmd_trace
},
897 { "watch", "watch [OPTIONS]", 0, 1, acmd_watch
},
898 { "notify", "notify MESSAGE ...", 1, 0xffff, acmd_notify
},
899 { "warn", "warn MESSAGE ...", 1, 0xffff, acmd_warn
},
900 { "port", "port", 0, 0, acmd_port
},
901 { "daemon", "daemon", 0, 0, acmd_daemon
},
902 { "list", "list", 0, 0, acmd_list
},
903 { "ifname", "ifname PEER", 1, 1, acmd_ifname
},
904 { "addr", "addr PEER", 1, 1, acmd_addr
},
905 { "stats", "stats PEER", 1, 1, acmd_stats
},
906 { "kill", "kill PEER", 1, 1, acmd_kill
},
907 { "add", "add PEER [-tunnel TUN] ADDR ...",
908 2, 0xffff, acmd_add
},
909 { "tunnels", "tunnels", 0, 0, acmd_tunnels
},
910 { "quit", "quit", 0, 0, acmd_quit
},
914 static void acmd_help(admin
*a
, unsigned ac
, char *av
[])
917 for (c
= acmdtab
; c
->name
; c
++)
918 a_info(a
, "%s", c
->help
);
922 /*----- Connection handling -----------------------------------------------*/
924 /* --- @a_lock@ --- *
926 * Arguments: @admin *a@ = pointer to an admin block
930 * Use: Locks an admin block so that it won't be destroyed
934 static void a_lock(admin
*a
) { assert(!(a
->f
& AF_LOCK
)); a
->f
|= AF_LOCK
; }
936 /* --- @a_unlock@ --- *
938 * Arguments: @admin *a@ = pointer to an admin block
942 * Use: Unlocks an admin block, allowing its destruction. This is
943 * also the second half of @a_destroy@.
946 static void a_unlock(admin
*a
)
948 assert(a
->f
& AF_LOCK
);
949 if (!(a
->f
& AF_DEAD
)) {
954 T( trace(T_ADMIN
, "admin: completing destruction of connection %u",
957 selbuf_destroy(&a
->b
);
964 if (a
->b
.reader
.fd
!= a
->w
.fd
)
965 close(a
->b
.reader
.fd
);
971 a
->next
->prev
= a
->prev
;
973 a
->prev
->next
= a
->next
;
979 /* --- @a_destroy@ --- *
981 * Arguments: @admin *a@ = pointer to an admin block
985 * Use: Destroys an admin block. This requires a certain amount of
989 static void a_destroy(admin
*a
)
991 /* --- Don't multiply destroy admin blocks --- */
996 /* --- Make sure nobody expects it to work --- */
999 T( trace(T_ADMIN
, "admin: destroying connection %u", a
->seq
); )
1001 /* --- Free the output buffers --- */
1006 for (o
= a
->o_head
; o
; o
= oo
) {
1013 /* --- If the block is locked, that's all we can manage --- */
1015 if (a
->f
& AF_LOCK
) {
1016 T( trace(T_ADMIN
, "admin: deferring destruction..."); )
1023 /* --- @a_line@ --- *
1025 * Arguments: @char *p@ = pointer to the line read
1026 * @size_t len@ = length of the line
1027 * @void *vp@ = pointer to my admin block
1031 * Use: Handles a line of input.
1034 static void a_line(char *p
, size_t len
, void *vp
)
1048 ac
= str_qsplit(p
, av
, 16, 0, STRF_QUOTE
);
1051 for (c
= acmdtab
; c
->name
; c
++) {
1052 if (mystrieq(av
[0], c
->name
)) {
1054 if (c
->argmin
> ac
|| ac
> c
->argmax
)
1055 a_fail(a
, "bad-syntax -- %s", c
->help
);
1058 c
->func(a
, ac
, av
+ 1);
1064 a_fail(a
, "unknown-command %s", av
[0]);
1067 /* --- @a_create@ --- *
1069 * Arguments: @int fd_in, fd_out@ = file descriptors to use
1070 * @unsigned f@ = initial flags to set
1074 * Use: Creates a new admin connection.
1077 void a_create(int fd_in
, int fd_out
, unsigned f
)
1079 admin
*a
= CREATE(admin
);
1081 T( static unsigned seq
= 0;
1083 T( trace(T_ADMIN
, "admin: accepted connection %u", a
->seq
); )
1086 if (fd_in
== STDIN_FILENO
)
1088 fdflags(fd_in
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
1089 if (fd_out
!= fd_in
)
1090 fdflags(fd_out
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
1091 selbuf_init(&a
->b
, &sel
, fd_in
, a_line
, a
);
1092 sel_initfile(&sel
, &a
->w
, fd_out
, SEL_WRITE
, a_flush
, a
);
1102 /* --- @a_accept@ --- *
1104 * Arguments: @int fd@ = file descriptor to accept
1105 * @unsigned mode@ = what to do
1106 * @void *v@ = uninteresting pointer
1110 * Use: Accepts a new admin connection.
1113 static void a_accept(int fd
, unsigned mode
, void *v
)
1116 struct sockaddr_un sun
;
1117 size_t sz
= sizeof(sun
);
1119 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &sz
)) < 0) {
1120 if (errno
!= EINTR
&& errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
1121 errno
!= ECONNABORTED
&& errno
!= EPROTO
)
1122 a_warn("ADMIN accept-error -- %s", strerror(errno
));
1125 a_create(nfd
, nfd
, 0);
1128 /* --- @a_daemon@ --- *
1134 * Use: Informs the admin module that it's a daemon.
1142 /* --- @a_init@ --- *
1144 * Arguments: @const char *name@ = socket name to create
1148 * Use: Creates the admin listening socket.
1151 void a_init(const char *name
)
1155 struct sockaddr_un sun
;
1156 struct sigaction sa
;
1159 /* --- Set up the socket address --- */
1161 sz
= strlen(name
) + 1;
1162 if (sz
> sizeof(sun
.sun_path
))
1163 die(EXIT_FAILURE
, "socket name `%s' too long", name
);
1165 sun
.sun_family
= AF_UNIX
;
1166 memcpy(sun
.sun_path
, name
, sz
);
1167 sz
+= offsetof(struct sockaddr_un
, sun_path
);
1169 /* --- Attempt to bind to the socket --- */
1173 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
1174 die(EXIT_FAILURE
, "couldn't create socket: %s", strerror(errno
));
1175 if (bind(fd
, (struct sockaddr
*)&sun
, sz
) < 0) {
1178 if (errno
!= EADDRINUSE
) {
1179 die(EXIT_FAILURE
, "couldn't bind to address `%s': %s",
1180 sun
.sun_path
, strerror(e
));
1183 die(EXIT_FAILURE
, "too many retries; giving up");
1185 if (!connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
1186 die(EXIT_FAILURE
, "server already listening on admin socket `%s'",
1189 if (errno
!= ECONNREFUSED
)
1190 die(EXIT_FAILURE
, "couldn't bind to address: %s", strerror(e
));
1191 if (stat(sun
.sun_path
, &st
)) {
1192 die(EXIT_FAILURE
, "couldn't stat `%s': %s",
1193 sun
.sun_path
, strerror(errno
));
1195 if (!S_ISSOCK(st
.st_mode
))
1196 die(EXIT_FAILURE
, "object `%s' isn't a socket", sun
.sun_path
);
1197 T( trace(T_ADMIN
, "admin: stale socket found; removing it"); )
1198 unlink(sun
.sun_path
);
1202 chmod(sun
.sun_path
, 0600);
1203 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
1205 die(EXIT_FAILURE
, "couldn't listen on socket: %s", strerror(errno
));
1207 /* --- Listen to the socket --- */
1209 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, a_accept
, 0);
1213 T( trace_custom(a_trace
, 0);
1214 trace(T_ADMIN
, "admin: enabled custom tracing"); )
1217 /* --- Set up signal handlers --- */
1219 sig_add(&s_term
, SIGTERM
, a_sigdie
, 0);
1220 sig_add(&s_hup
, SIGHUP
, a_sighup
, 0);
1221 signal(SIGPIPE
, SIG_IGN
);
1222 sigaction(SIGINT
, 0, &sa
);
1223 if (sa
.sa_handler
!= SIG_IGN
)
1224 sig_add(&s_int
, SIGINT
, a_sigdie
, 0);
1227 /*----- That's all, folks -------------------------------------------------*/