From: Roy Marples Date: Thu, 15 May 2008 16:09:42 +0000 (+0000) Subject: Check correct lengths and use closefrom where available. X-Git-Tag: v4.0.2~391 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8dc6afb7e808c5e24a69fd035914a1a67432a950;p=thirdparty%2Fdhcpcd.git Check correct lengths and use closefrom where available. --- diff --git a/common.c b/common.c index 16ef01b1..cad633ee 100644 --- a/common.c +++ b/common.c @@ -109,6 +109,20 @@ strlcpy(char *dst, const char *src, size_t size) } #endif +#ifndef HAVE_CLOSEFROM +int +closefrom(int fd) +{ + int max = getdtablesize(); + int i; + int r = 0; + + for (i = fd; i < max; i++) + r += close(i); + return r; +} +#endif + /* Close our fd's */ int close_fds(void) diff --git a/common.h b/common.h index 8689cf1e..90d84fe9 100644 --- a/common.h +++ b/common.h @@ -55,6 +55,12 @@ size_t strlcpy(char *, const char *, size_t); void srandomdev(void); #endif +#define HAVE_CLOSEFROM +#if defined(__linux__) || defined(__FreeBSD__) +# undef HAVE_CLOSEFROM +int closefrom(int); +#endif + int close_fds(void); int close_on_exec(int); ssize_t get_line(char **, size_t *, FILE *); diff --git a/dhcpcd.c b/dhcpcd.c index 71005657..435c99b6 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -171,7 +171,7 @@ parse_option(int opt, char *oarg, struct options *options) case 'h': if (!oarg) *options->hostname = '\0'; - else if (strlen(oarg) > MAXHOSTNAMELEN) { + else if (strlen(oarg) >= MAXHOSTNAMELEN) { logger(LOG_ERR, "`%s' too long for HostName string, max is %d", oarg, MAXHOSTNAMELEN); @@ -183,7 +183,7 @@ parse_option(int opt, char *oarg, struct options *options) case 'i': if (!oarg) { *options->classid = '\0'; - } else if (strlen(oarg) > CLASS_ID_MAX_LEN) { + } else if (strlen(oarg) >= CLASS_ID_MAX_LEN) { logger(LOG_ERR, "`%s' too long for ClassID string, max is %d", oarg, CLASS_ID_MAX_LEN); @@ -265,7 +265,7 @@ parse_option(int opt, char *oarg, struct options *options) j = 0; for (i = 0; i < userclasses; i++) j += (int)options->userclass[j] + 1; - if (j + 1 + strlen(oarg) > USERCLASS_MAX_LEN) { + if (j + 1 + strlen(oarg) >= USERCLASS_MAX_LEN) { logger(LOG_ERR, "userclass overrun, max is %d", USERCLASS_MAX_LEN); @@ -310,7 +310,7 @@ parse_option(int opt, char *oarg, struct options *options) break; case 'I': if (oarg) { - if (strlen(oarg) > CLIENT_ID_MAX_LEN) { + if (strlen(oarg) >= CLIENT_ID_MAX_LEN) { logger(LOG_ERR, "`%s' is too long for" " ClientID, max is %d", oarg, CLIENT_ID_MAX_LEN); @@ -382,10 +382,7 @@ main(int argc, char **argv) char *cf = NULL; char *intf = NULL; - /* Close any un-needed fd's */ - for (i = getdtablesize() - 1; i >= 3; --i) - close (i); - + closefrom(3); openlog(PACKAGE, LOG_PID, LOG_LOCAL0); #ifdef THERE_IS_NO_FORK @@ -480,7 +477,7 @@ main(int argc, char **argv) usage(); if (optind < argc) { - if (strlen(argv[optind]) > IF_NAMESIZE) { + if (strlen(argv[optind]) >= IF_NAMESIZE) { logger(LOG_ERR, "`%s' too long for an interface name (max=%d)", argv[optind], IF_NAMESIZE);