NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: dhcpd dup my socket?

On Jun 23, 2009, at 6:46 AM, Water NB wrote:
[ ... ]
I think system("/etc/rc.d/dhcpd restart"); is the root issue.
So how to resolve it?

By default, child processes inherit any open descriptors such as the network socket you have opened. You can either switch to using fork() and exec() separately rather than system, and explicitly close the descriptor in the child process before running dhcpd, or you can use fcntl(fd, FD_SET, FD_CLOEXEC) [1] to have fd closed for you automagically...


[1]: It's probably safer to take a page from GNU libc recommendation, if more flags are added over time:

/* Set the FD_CLOEXEC flag of desc if value is nonzero,
   or clear the flag if value is 0.
   Return 0 on success, or -1 on error with errno set. */

set_cloexec_flag (int desc, int value)
  int oldflags = fcntl (desc, F_GETFD, 0);
  /* If reading the flags failed, return error indication now.
  if (oldflags < 0)
    return oldflags;
  /* Set just the flag we want to set. */
  if (value != 0)
    oldflags |= FD_CLOEXEC;
    oldflags &= ~FD_CLOEXEC;
  /* Store modified flag word in the descriptor. */
  return fcntl (desc, F_SETFD, oldflags);

Home | Main Index | Thread Index | Old Index