Remi Locherer([email protected]) on 2018.08.31 15:49:32 +0200:
> Hi,
>
> this is the adaption of the recent ospfd commit to ospf6d.
>
> Early in the startup the main process checks if another process is listening
> on the control socket and exits if that is the case. Otherwise the master
> process opens the control socket and passes it on to the ospf engine.
>
> OK?
reads ok, tested
/Benno
> Remi
>
>
>
> Index: control.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/ospf6d/control.c,v
> retrieving revision 1.26
> diff -u -p -r1.26 control.c
> --- control.c 12 Aug 2017 16:27:50 -0000 1.26
> +++ control.c 31 Aug 2018 13:23:15 -0000
> @@ -39,6 +39,33 @@ struct ctl_conn *control_connbypid(pid_t
> void control_close(int);
>
> int
> +control_check(char *path)
> +{
> + struct sockaddr_un sun;
> + int fd;
> +
> + bzero(&sun, sizeof(sun));
> + sun.sun_family = AF_UNIX;
> + strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
> +
> + if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
> + log_warn("control_check: socket check");
> + return (-1);
> + }
> +
> + if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == 0) {
> + log_warnx("control_check: socket in use");
> + close(fd);
> + return (-1);
> + }
> +
> + close(fd);
> +
> + return (0);
> +}
> +
> +
> +int
> control_init(char *path)
> {
> struct sockaddr_un sun;
> @@ -78,9 +105,7 @@ control_init(char *path)
> return (-1);
> }
>
> - control_state.fd = fd;
> -
> - return (0);
> + return (fd);
> }
>
> int
> Index: control.h
> ===================================================================
> RCS file: /cvs/src/usr.sbin/ospf6d/control.h,v
> retrieving revision 1.5
> diff -u -p -r1.5 control.h
> --- control.h 10 Feb 2015 05:39:10 -0000 1.5
> +++ control.h 31 Aug 2018 11:41:05 -0000
> @@ -34,6 +34,7 @@ struct ctl_conn {
> struct imsgev iev;
> };
>
> +int control_check(char *);
> int control_init(char *);
> int control_listen(void);
> void control_accept(int, short, void *);
> Index: ospf6d.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/ospf6d/ospf6d.c,v
> retrieving revision 1.38
> diff -u -p -r1.38 ospf6d.c
> --- ospf6d.c 12 Jul 2018 13:45:03 -0000 1.38
> +++ ospf6d.c 31 Aug 2018 13:39:58 -0000
> @@ -115,6 +115,7 @@ main(int argc, char *argv[])
> int mib[4];
> size_t len;
> char *sockname = NULL;
> + int control_fd;
>
> conffile = CONF_FILE;
> ospfd_process = PROC_MAIN;
> @@ -209,6 +210,9 @@ main(int argc, char *argv[])
> log_init(debug, LOG_DAEMON);
> log_setverbose(ospfd_conf->opts & OSPFD_OPT_VERBOSE);
>
> + if ((control_check(ospfd_conf->csock)) == -1)
> + fatalx("control socket check failed");
> +
> if (!debug)
> daemon(1, 0);
>
> @@ -266,6 +270,10 @@ main(int argc, char *argv[])
> iev_rde->handler, iev_rde);
> event_add(&iev_rde->ev, NULL);
>
> + if ((control_fd = control_init(ospfd_conf->csock)) == -1)
> + fatalx("control socket setup failed");
> + main_imsg_compose_ospfe_fd(IMSG_CONTROLFD, 0, control_fd);
> +
> if (kr_init(!(ospfd_conf->flags & OSPFD_FLAG_NO_FIB_UPDATE),
> ospfd_conf->rdomain) == -1)
> fatalx("kr_init failed");
> @@ -454,6 +462,14 @@ main_imsg_compose_ospfe(int type, pid_t
> if (iev_ospfe == NULL)
> return;
> imsg_compose_event(iev_ospfe, type, 0, pid, -1, data, datalen);
> +}
> +
> +void
> +main_imsg_compose_ospfe_fd(int type, pid_t pid, int fd)
> +{
> + if (iev_ospfe == NULL)
> + return;
> + imsg_compose_event(iev_ospfe, type, 0, pid, fd, NULL, 0);
> }
>
> void
> Index: ospf6d.h
> ===================================================================
> RCS file: /cvs/src/usr.sbin/ospf6d/ospf6d.h,v
> retrieving revision 1.37
> diff -u -p -r1.37 ospf6d.h
> --- ospf6d.h 12 Jul 2018 13:45:03 -0000 1.37
> +++ ospf6d.h 31 Aug 2018 11:56:38 -0000
> @@ -99,6 +99,7 @@ enum imsg_type {
> IMSG_CTL_KROUTE_ADDR,
> IMSG_CTL_END,
> IMSG_CTL_LOG_VERBOSE,
> + IMSG_CONTROLFD,
> IMSG_KROUTE_CHANGE,
> IMSG_KROUTE_DELETE,
> IMSG_IFINFO,
> @@ -577,6 +578,7 @@ void rtlabel_tag(u_int16_t, u_int32_t)
>
> /* ospf6d.c */
> void main_imsg_compose_ospfe(int, pid_t, void *, u_int16_t);
> +void main_imsg_compose_ospfe_fd(int, pid_t, int);
> void main_imsg_compose_rde(int, pid_t, void *, u_int16_t);
> int ospf_redistribute(struct kroute *, u_int32_t *);
> void merge_config(struct ospfd_conf *, struct ospfd_conf *);
> Index: ospfe.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/ospf6d/ospfe.c,v
> retrieving revision 1.54
> diff -u -p -r1.54 ospfe.c
> --- ospfe.c 12 Jul 2018 13:45:03 -0000 1.54
> +++ ospfe.c 31 Aug 2018 11:45:42 -0000
> @@ -88,10 +88,6 @@ ospfe(struct ospfd_conf *xconf, int pipe
> return (pid);
> }
>
> - /* create ospfd control socket outside chroot */
> - if (control_init(xconf->csock) == -1)
> - fatalx("control socket setup failed");
> -
> /* create the raw ip socket */
> if ((xconf->ospf_socket = socket(AF_INET6,
> SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_OSPF)) == -1)
> @@ -133,7 +129,7 @@ ospfe(struct ospfd_conf *xconf, int pipe
> setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
> fatal("can't drop privileges");
>
> - if (pledge("stdio inet mcast", NULL) == -1)
> + if (pledge("stdio inet mcast recvfd", NULL) == -1)
> fatal("pledge");
>
> event_init();
> @@ -444,6 +440,17 @@ ospfe_dispatch_main(int fd, short event,
> case IMSG_CTL_KROUTE_ADDR:
> case IMSG_CTL_END:
> control_imsg_relay(&imsg);
> + break;
> + case IMSG_CONTROLFD:
> + if ((fd = imsg.fd) == -1)
> + fatalx("%s: expected to receive imsg control"
> + "fd but didn't receive any", __func__);
> + control_state.fd = fd;
> + /* Listen on control socket. */
> + TAILQ_INIT(&ctl_conns);
> + control_listen();
> + if (pledge("stdio inet mcast", NULL) == -1)
> + fatal("pledge");
> break;
> default:
> log_debug("ospfe_dispatch_main: error handling imsg %d",
>