@@ -355,6 +355,20 @@ dhcpcd_daemonised(struct dhcpcd_ctx *ctx)
355355}
356356#endif
357357
358+ static ssize_t
359+ dhcpcd_write_fork (struct dhcpcd_ctx * ctx , int exit_code )
360+ {
361+ struct iovec iov [] = {
362+ { .iov_base = & exit_code , .iov_len = sizeof (exit_code ) }
363+ };
364+ struct msghdr msg = {
365+ .msg_iov = iov ,
366+ .msg_iovlen = __arraycount (iov ),
367+ };
368+
369+ return sendmsg (ctx -> fork_fd , & msg , MSG_EOR );
370+ }
371+
358372/* Returns the pid of the child, otherwise 0. */
359373void
360374dhcpcd_daemonise (struct dhcpcd_ctx * ctx )
@@ -393,7 +407,7 @@ dhcpcd_daemonise(struct dhcpcd_ctx *ctx)
393407
394408 eloop_event_delete (ctx -> eloop , ctx -> fork_fd );
395409 exit_code = EXIT_SUCCESS ;
396- if (write (ctx -> fork_fd , & exit_code , sizeof ( exit_code ) ) == -1 )
410+ if (dhcpcd_write_fork (ctx , exit_code ) == -1 )
397411 logerr (__func__ );
398412 close (ctx -> fork_fd );
399413 ctx -> fork_fd = -1 ;
@@ -1448,9 +1462,8 @@ dhcpcd_signal_cb(int sig, void *arg)
14481462 }
14491463
14501464 if (sig != SIGCHLD && ctx -> options & DHCPCD_FORKED ) {
1451- if (sig != SIGHUP &&
1452- write (ctx -> fork_fd , & sig , sizeof (sig )) == -1 )
1453- logerr ("%s: write" , __func__ );
1465+ if (sig != SIGHUP && dhcpcd_write_fork (ctx , sig ) == -1 )
1466+ logerr ("%s: dhcpcd_write_fork" , __func__ );
14541467 return ;
14551468 }
14561469
@@ -2401,7 +2414,7 @@ main(int argc, char **argv, char **envp)
24012414 if (!(ctx .options & DHCPCD_DAEMONISE ))
24022415 goto start_manager ;
24032416
2404- if (xsocketpair (AF_UNIX , SOCK_STREAM |SOCK_CXNB , 0 , fork_fd ) == -1 ) {
2417+ if (xsocketpair (AF_UNIX , SOCK_SEQPACKET |SOCK_CXNB , 0 , fork_fd ) == -1 ) {
24052418 logerr ("socketpair" );
24062419 goto exit_failure ;
24072420 }
@@ -2712,8 +2725,15 @@ main(int argc, char **argv, char **envp)
27122725#ifdef USE_SIGNALS
27132726 /* If still attached, detach from the launcher */
27142727 if (ctx .options & DHCPCD_STARTED && ctx .fork_fd != -1 ) {
2715- if (write (ctx .fork_fd , & i , sizeof (i )) == -1 )
2716- logerr ("%s: write" , __func__ );
2728+ struct iovec iov [] = {
2729+ { .iov_base = & i , .iov_len = sizeof (i ) }
2730+ };
2731+ struct msghdr msg = {
2732+ .msg_iov = iov ,
2733+ .msg_iovlen = __arraycount (iov ),
2734+ };
2735+ if (sendmsg (ctx .fork_fd , & msg , MSG_EOR ) == -1 )
2736+ logerr ("%s: sendmsg" , __func__ );
27172737 }
27182738#endif
27192739
0 commit comments