# such as the need to link to libresolv and/or librt so please forgive the
# embedded code :)
-VERSION = 3.1.7_pre1
+VERSION = 3.1.7_pre2
CFLAGS += -O2 -pipe
INSTALL ?= install
#ifdef __linux__
# define _BSD_SOURCE
-# define _XOPEN_SOURCE 500 /* needed for pwrite */
#endif
#include <sys/types.h>
static pid_t daemonise (int *pidfd)
{
pid_t pid;
- char spid[16];
#ifndef THERE_IS_NO_FORK
logger (LOG_DEBUG, "forking to background");
free (argv);
#endif
+ /* Done with the fd now */
if (pid != 0) {
- if (ftruncate (*pidfd, 0) == -1) {
- logger (LOG_ERR, "ftruncate: %s", strerror (errno));
- } else {
- snprintf (spid, sizeof (spid), "%u", pid);
- if (pwrite (*pidfd, spid, strlen (spid), 0) != (ssize_t) strlen (spid))
- logger (LOG_ERR, "pwrite: %s", strerror (errno));
- }
+ writepid (*pidfd, pid);
+ close (*pidfd);
+ *pidfd = -1;
}
return (pid);
switch (sig) {
case SIGINT:
logger (LOG_INFO, "received SIGINT, stopping");
- retval = (! daemonised);
+ retval = daemonised ? EXIT_SUCCESS : EXIT_FAILURE;
goto eexit;
case SIGTERM:
logger (LOG_INFO, "received SIGTERM, stopping");
- retval = (! daemonised);
+ retval = daemonised ? EXIT_SUCCESS : EXIT_FAILURE;
goto eexit;
case SIGALRM:
else
logger (LOG_ERR,
"received SIGHUP, but we no have lease to release");
- retval = 0;
+ retval = daemonised ? EXIT_SUCCESS : EXIT_FAILURE;
goto eexit;
default:
free_route (iface->previous_routes);
free (iface);
}
-
free (buffer);
- if (*pidfd != -1) {
- close (*pidfd);
- *pidfd = -1;
- }
-
if (daemonised)
unlink (options->pidfile);
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef __linux__
+# define _XOPEN_SOURCE 500 /* needed for pwrite */
+#endif
+
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
return (tp.tv_sec);
}
+void writepid (int fd, pid_t pid)
+{
+ char spid[16];
+ if (ftruncate (fd, 0) == -1) {
+ logger (LOG_ERR, "ftruncate: %s", strerror (errno));
+ } else {
+ snprintf (spid, sizeof (spid), "%u", pid);
+ if (pwrite (fd, spid, strlen (spid), 0) != (ssize_t) strlen (spid))
+ logger (LOG_ERR, "pwrite: %s", strerror (errno));
+ }
+}
+
void *xmalloc (size_t s)
{
void *value = malloc (s);
void close_fds (void);
int get_time (struct timeval *tp);
time_t uptime (void);
+void writepid (int fd, pid_t pid);
void *xmalloc (size_t size);
char *xstrdup (const char *str);
fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) == -1)
logger (LOG_ERR, "fcntl: %s", strerror (errno));
+ writepid (pidfd, getpid ());
logger (LOG_INFO, PACKAGE " " VERSION " starting");
}
if (dhcp_run (options, &pidfd) == 0)
i = EXIT_SUCCESS;
+ /* If we didn't daemonise then we need to punt the pidfile now */
+ if (pidfd > -1) {
+ close (pidfd);
+ unlink (options->pidfile);
+ }
+
free (options);
#ifdef THERE_IS_NO_FORK
logger (LOG_INFO, "exiting");
- if (pidfd > -1)
- close (pidfd);
-
exit (i);
}
# define IN_LINKLOCAL(addr) ((ntohl (addr) & IN_CLASSB_NET) == LINKLOCAL_ADDR)
#endif
-
typedef struct route_t
{
struct in_addr destination;