udhcpc uses 100% cpu

Denis Vlasenko vda.linux at googlemail.com
Thu May 24 10:38:29 UTC 2007


On 5/23/07, William Thompson <wt at electro-mechanical.com> wrote:
> I hate replying to myself.  I ran an strace on it as it started and it turns
> out that there's no error checking on socketpair()
> (networking/udhcp/signalpipe.c:39).  The stage that I'm running this didn't
> have the unix module loaded so it was unable to open a socket.

Adding error check is trivial. But maybe we can use pipe instead of socket pair?
Can you test the following - just replace signalpipe.c with the following code
(// marks disabled code, non-indented lines is added code, the rest is
unchaged):

#include "common.h"

static int signal_pipe[2];

static void signal_handler(int sig)
{
//        if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0)
//                bb_perror_msg("cannot send signal");
if (write(signal_pipe[1], &sig, sizeof(sig)) < 0)
bb_perror_msg("cannot send signal");
}


/* Call this before doing anything else. Sets up the socket pair
 * and installs the signal handler */
void udhcp_sp_setup(void)
{
//        if (socketpair(AF_UNIX, SOCK_STREAM, 0, signal_pipe))
//                bb_perror_msg_and_die("socketpair");
if (pipe(signal_pipe)) bb_perror_msg_and_die("pipe");
        fcntl(signal_pipe[0], F_SETFD, FD_CLOEXEC);
        fcntl(signal_pipe[1], F_SETFD, FD_CLOEXEC);
fcntl(signal_pipe[1], F_SETFL, O_NONBLOCK);
        signal(SIGUSR1, signal_handler);
        signal(SIGUSR2, signal_handler);
        signal(SIGTERM, signal_handler);
}


/* Quick little function to setup the rfds. Will return the
 * max_fd for use with select. Limited in that you can only pass
 * one extra fd */
int udhcp_sp_fd_set(fd_set *rfds, int extra_fd)
{
        FD_ZERO(rfds);
        FD_SET(signal_pipe[0], rfds);
        if (extra_fd >= 0) {
                fcntl(extra_fd, F_SETFD, FD_CLOEXEC);
                FD_SET(extra_fd, rfds);
        }
        return signal_pipe[0] > extra_fd ? signal_pipe[0] : extra_fd;
}


/* Read a signal from the signal pipe. Returns 0 if there is
 * no signal, -1 on error (and sets errno appropriately), and
 * your signal on success */
int udhcp_sp_read(fd_set *rfds)
{
        int sig;

        if (!FD_ISSET(signal_pipe[0], rfds))
                return 0;

        if (read(signal_pipe[0], &sig, sizeof(sig)) < 0)
                return -1;

        return sig;
}



More information about the busybox mailing list