Don't leak descriptors to sendmail

This commit is contained in:
Miroslav Lichvar 2011-01-18 18:07:46 +01:00
parent 59c68d240c
commit 9d35b5deac
9 changed files with 38 additions and 0 deletions

View file

@ -168,6 +168,9 @@ prepare_socket(int family)
LOG_FATAL(LOGF_Acquire, "Could not open socket : %s", strerror(errno));
}
/* Close on exec */
UTI_FdSetCloexec(sock_fd);
if (port_number == 0) {
/* Don't bother binding this socket - we're not fussed what port
number it gets */

View file

@ -199,6 +199,9 @@ prepare_socket(int family)
return -1;
}
/* Close on exec */
UTI_FdSetCloexec(sock_fd);
/* Allow reuse of port number */
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on_off, sizeof(on_off)) < 0) {
LOG(LOGS_ERR, LOGF_CmdMon, "Could not set reuseaddr socket options");

View file

@ -35,6 +35,7 @@
#include "logging.h"
#include "version.h"
#include "mkdirpp.h"
#include "util.h"
/* ================================================== */
/* Flag indicating we have initialised */
@ -248,6 +249,9 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...)
logfiles[id].name = NULL;
return;
}
/* Close on exec */
UTI_FdSetCloexec(fileno(logfiles[id].file));
}
banner = CNF_GetLogBanner();

View file

@ -121,6 +121,9 @@ prepare_socket(int family)
return -1;
}
/* Close on exec */
UTI_FdSetCloexec(sock_fd);
/* Make the socket capable of re-using an old address */
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on_off, sizeof(on_off)) < 0) {
LOG(LOGS_ERR, LOGF_NtpIO, "Could not set reuseaddr socket options");

View file

@ -57,6 +57,8 @@ static int pps_initialise(RCL_Instance instance) {
return 0;
}
UTI_FdSetCloexec(fd);
if (time_pps_create(fd, &handle) < 0) {
LOG_FATAL(LOGF_Refclock, "time_pps_create() failed on %s", path);
return 0;
@ -93,6 +95,7 @@ static int pps_initialise(RCL_Instance instance) {
return 0;
}
pps = MallocNew(struct pps_instance);
pps->handle = handle;
pps->last_seq = 0;

View file

@ -105,6 +105,8 @@ static int sock_initialise(RCL_Instance instance)
return 0;
}
UTI_FdSetCloexec(sockfd);
unlink(path);
if (bind(sockfd, (struct sockaddr *)&s, sizeof (s)) < 0) {
LOG_FATAL(LOGF_Refclock, "bind() failed");

View file

@ -591,6 +591,9 @@ RTC_Linux_Initialise(void)
return 0;
}
/* Close on exec */
UTI_FdSetCloexec(fd);
n_samples = 0;
n_samples_since_regression = 0;
n_runs = 0;

14
util.c
View file

@ -600,3 +600,17 @@ UTI_FloatHostToNetwork(double x)
}
/* ================================================== */
void
UTI_FdSetCloexec(int fd)
{
int flags;
flags = fcntl(fd, F_GETFD);
if (flags != -1) {
flags |= FD_CLOEXEC;
fcntl(fd, F_SETFD, flags);
}
}
/* ================================================== */

3
util.h
View file

@ -102,6 +102,9 @@ extern void UTI_TimevalHostToNetwork(struct timeval *src, Timeval *dest);
extern double UTI_FloatNetworkToHost(Float x);
extern Float UTI_FloatHostToNetwork(double x);
/* Set FD_CLOEXEC on descriptor */
extern void UTI_FdSetCloexec(int fd);
#if defined (INLINE_UTILITIES)
#define INLINE_STATIC inline static
#include "util.c"