diff --git a/THANKS b/THANKS index 07b1f68..331b868 100644 --- a/THANKS +++ b/THANKS @@ -35,3 +35,4 @@ Wolfgang Steudel Aaron Paetznick Tim Cantin Martijn van Brummelen +Anton Tkachev diff --git a/bootstrap.conf b/bootstrap.conf index b815859..b51e08a 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -20,6 +20,7 @@ gnulib_modules=" close-stream closeout + error fclose fcntl-h fdopen diff --git a/build-aux/.gitignore b/build-aux/.gitignore index a7f0fb2..a85ce13 100644 --- a/build-aux/.gitignore +++ b/build-aux/.gitignore @@ -1,3 +1,4 @@ +/ar-lib /compile /config.guess /config.sub diff --git a/lib/.gitignore b/lib/.gitignore index 2e2c50e..b0e5275 100644 --- a/lib/.gitignore +++ b/lib/.gitignore @@ -7,6 +7,7 @@ /Makefile.am /Makefile.in /_Exit.c +/alloca.h /alloca.in.h /arg-nonnull.h /arpa/ diff --git a/src/dhcpd-pools.c b/src/dhcpd-pools.c index bfb312b..267d185 100644 --- a/src/dhcpd-pools.c +++ b/src/dhcpd-pools.c @@ -38,10 +38,9 @@ */ #include + #include #include -#include -#include #include #include #include @@ -49,11 +48,13 @@ #include "close-stream.h" #include "closeout.h" -#include "defaults.h" -#include "dhcpd-pools.h" +#include "error.h" #include "progname.h" #include "xalloc.h" +#include "dhcpd-pools.h" +#include "defaults.h" + /* Global variables */ int prefix_length[2][NUM_OF_PREFIX]; struct configuration_t config; @@ -165,7 +166,7 @@ int main(int argc, char **argv) /* Output sorting option */ sorts = strlen(optarg); if (5 < sorts) { - warnx("main: only first 5 sort orders will be used"); + error(0, 0, "main: only first 5 sort orders will be used"); strncpy(config.sort, optarg, (size_t)5); sorts = 5; } else @@ -188,8 +189,8 @@ int main(int argc, char **argv) config.output_limit[i] = optarg[i] - '0'; else { clean_up(); - errx(EXIT_FAILURE, - "main: output mask `%s' is illegal", optarg); + error(EXIT_FAILURE, 0, + "main: output mask `%s' is illegal", optarg); } } break; @@ -211,7 +212,8 @@ int main(int argc, char **argv) /* Print help */ usage(EXIT_SUCCESS); default: - errx(EXIT_FAILURE, "Try `%s --help' for more information.", program_name); + error(EXIT_FAILURE, 0, "Try `%s --help' for more information.", + program_name); } } /* Output function selection */ @@ -246,7 +248,7 @@ int main(int argc, char **argv) break; default: clean_up(); - errx(EXIT_FAILURE, "main: unknown output format `%c'", config.output_format[0]); + error(EXIT_FAILURE, 0, "main: unknown output format `%c'", config.output_format[0]); } /* Do the job */ prepare_memory(); diff --git a/src/getdata.c b/src/getdata.c index bf2de86..591d743 100644 --- a/src/getdata.c +++ b/src/getdata.c @@ -42,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -53,10 +52,12 @@ #include #include -#include "defaults.h" -#include "dhcpd-pools.h" +#include "error.h" #include "xalloc.h" +#include "dhcpd-pools.h" +#include "defaults.h" + /*! \brief Lease file parser. The parser can only read ISC DHCPD * dhcpd.leases file format. */ int parse_leases(void) @@ -70,15 +71,15 @@ int parse_leases(void) dhcpd_leases = fopen(config.dhcpdlease_file, "r"); if (dhcpd_leases == NULL) - err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file); + error(EXIT_FAILURE, errno, "parse_leases: %s", config.dhcpdlease_file); #ifdef HAVE_POSIX_FADVISE # ifdef POSIX_FADV_NOREUSE if (posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_NOREUSE) != 0) - err(EXIT_FAILURE, "parse_leases: fadvise %s", config.dhcpdlease_file); + error(EXIT_FAILURE, errno, "parse_leases: fadvise %s", config.dhcpdlease_file); # endif /* POSIX_FADV_NOREUSE */ # ifdef POSIX_FADV_SEQUENTIAL if (posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_SEQUENTIAL) != 0) - err(EXIT_FAILURE, "parse_leases: fadvise %s", config.dhcpdlease_file); + error(EXIT_FAILURE, errno, "parse_leases: fadvise %s", config.dhcpdlease_file); # endif /* POSIX_FADV_SEQUENTIAL */ #endif /* HAVE_POSIX_FADVISE */ /* I found out that there's one lease address per 300 bytes in @@ -86,7 +87,7 @@ int parse_leases(void) * If someone has higher density in lease file I'm interested to * hear about that. */ if (stat(config.dhcpdlease_file, &lease_file_stats)) - err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file); + error(EXIT_FAILURE, errno, "parse_leases: %s", config.dhcpdlease_file); line = xmalloc(sizeof(char) * MAXLEN); line[0] = '\0'; ipstring = xmalloc(sizeof(char) * MAXLEN); @@ -95,7 +96,7 @@ int parse_leases(void) ethernets = true; while (!feof(dhcpd_leases)) { if (!fgets(line, MAXLEN, dhcpd_leases) && ferror(dhcpd_leases)) - err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file); + error(EXIT_FAILURE, errno, "parse_leases: %s", config.dhcpdlease_file); switch (xstrstr(line)) { /* It's a lease, save IP */ case PREFIX_LEASE: @@ -186,15 +187,15 @@ void parse_config(int is_include, const char *restrict config_file, /* Open configuration file */ dhcpd_config = fopen(config_file, "r"); if (dhcpd_config == NULL) - err(EXIT_FAILURE, "parse_config: %s", config_file); + error(EXIT_FAILURE, errno, "parse_config: %s", config_file); #ifdef HAVE_POSIX_FADVISE # ifdef POSIX_FADV_NOREUSE if (posix_fadvise(fileno(dhcpd_config), 0, 0, POSIX_FADV_NOREUSE) != 0) - err(EXIT_FAILURE, "parse_config: fadvise %s", config_file); + error(EXIT_FAILURE, errno, "parse_config: fadvise %s", config_file); # endif /* POSIX_FADV_NOREUSE */ # ifdef POSIX_FADV_SEQUENTIAL if (posix_fadvise(fileno(dhcpd_config), 0, 0, POSIX_FADV_SEQUENTIAL) != 0) - err(EXIT_FAILURE, "parse_config: fadvise %s", config_file); + error(EXIT_FAILURE, errno, "parse_config: fadvise %s", config_file); # endif /* POSIX_FADV_SEQUENTIAL */ #endif /* HAVE_POSIX_FADVISE */ /* Very hairy stuff begins. */ @@ -364,8 +365,8 @@ void parse_config(int is_include, const char *restrict config_file, shared_p->backups = 0; if (SHARED_NETWORKS < num_shared_networks + 2) /* FIXME: make this to go away by reallocating more space. */ - errx(EXIT_FAILURE, - "parse_config: increase default.h SHARED_NETWORKS and recompile"); + error(EXIT_FAILURE, 0, + "parse_config: increase default.h SHARED_NETWORKS and recompile"); argument = ITS_NOTHING_INTERESTING; braces_shared = braces; break; @@ -380,7 +381,7 @@ void parse_config(int is_include, const char *restrict config_file, argument = ITS_NOTHING_INTERESTING; break; default: - warnx("impossible occurred, report a bug"); + error(0, 0, "impossible occurred, report a bug"); assert(0); } } diff --git a/src/hash.c b/src/hash.c index a75e9f4..08b95b4 100644 --- a/src/hash.c +++ b/src/hash.c @@ -38,9 +38,12 @@ * analysis happen as quick as possible.. */ -#include "dhcpd-pools.h" +#include + #include "xalloc.h" +#include "dhcpd-pools.h" + #define HASH_FIND_V6(head, findv6, out) HASH_FIND(hh, head, findv6, 16, out) #define HASH_ADD_V6(head, v6field, add) HASH_ADD(hh, head, v6field, 16, add) diff --git a/src/other.c b/src/other.c index e9f49c8..67c2f4d 100644 --- a/src/other.c +++ b/src/other.c @@ -39,13 +39,9 @@ #include -#include "dhcpd-pools.h" -#include "defaults.h" -#include "progname.h" - #include -#include #include +#include #include #include #include @@ -53,7 +49,12 @@ #include #include #include -#include + +#include "error.h" +#include "progname.h" + +#include "dhcpd-pools.h" +#include "defaults.h" /*! \brief Set function pointers depending on IP version. * \param ip IP version. @@ -378,9 +379,7 @@ double strtod_or_err(const char *restrict str, const char *restrict errmesg) goto err; return num; err: - if (errno) - err(EXIT_FAILURE, "%s: '%s'", errmesg, str); - errx(EXIT_FAILURE, "%s: '%s'", errmesg, str); + error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str); } /*! \brief Reverse range. @@ -404,7 +403,7 @@ void clean_up(void) { /* Just in case there something in buffers */ if (fflush(NULL)) - warn("clean_up: fflush"); + error(0, 0, "clean_up: fflush"); free(config.dhcpdconf_file); free(config.dhcpdlease_file); free(config.output_file); diff --git a/src/output.c b/src/output.c index 28d6775..06a5469 100644 --- a/src/output.c +++ b/src/output.c @@ -40,7 +40,6 @@ #include #include -#include #include #include #include @@ -53,10 +52,12 @@ #include #include "close-stream.h" -#include "dhcpd-pools.h" +#include "error.h" #include "progname.h" #include "strftime.h" +#include "dhcpd-pools.h" + /*! \brief Text output format, which is the default. * FIXME: This function should return void. */ int output_txt(void) @@ -72,7 +73,7 @@ int output_txt(void) if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { - err(EXIT_FAILURE, "output_txt: %s", config.output_file); + error(EXIT_FAILURE, errno, "output_txt: %s", config.output_file); } } else { outfile = stdout; @@ -201,12 +202,12 @@ int output_txt(void) if (outfile == stdout) { ret = fflush(stdout); if (ret) { - warn("output_txt: fflush"); + error(0, 0, "output_txt: fflush"); } } else { ret = close_stream(outfile); if (ret) { - warn("output_txt: fclose"); + error(0, 0, "output_txt: fclose"); } } @@ -228,7 +229,7 @@ int output_xml(void) if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { - err(EXIT_FAILURE, "output_xml: %s", config.output_file); + error(EXIT_FAILURE, errno, "output_xml: %s", config.output_file); } } else { outfile = stdout; @@ -242,6 +243,7 @@ int output_xml(void) if (config.output_format[0] == 'X' || config.output_format[0] == 'J') { struct leases_t *l; + for (l = leases; l != NULL; l = l->hh.next) { if (l->type == ACTIVE) { fputs("\n\t", outfile); @@ -305,12 +307,12 @@ int output_xml(void) if (outfile == stdout) { ret = fflush(stdout); if (ret) { - warn("output_xml: fflush"); + error(0, 0, "output_xml: fflush"); } } else { ret = close_stream(outfile); if (ret) { - warn("output_xml: fclose"); + error(0, 0, "output_xml: fclose"); } } @@ -333,7 +335,7 @@ int output_json(void) if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { - err(EXIT_FAILURE, "output_json: %s", config.output_file); + error(EXIT_FAILURE, errno, "output_json: %s", config.output_file); } } else { outfile = stdout; @@ -348,6 +350,7 @@ int output_json(void) if (config.output_format[0] == 'X' || config.output_format[0] == 'J') { struct leases_t *l; + fprintf(outfile, " \"active_leases\": ["); for (l = leases; l != NULL; l = l->hh.next) { if (l->type == ACTIVE) { @@ -443,12 +446,12 @@ int output_json(void) if (outfile == stdout) { ret = fflush(stdout); if (ret) { - warn("output_json: fflush"); + error(0, 0, "output_json: fflush"); } } else { ret = close_stream(outfile); if (ret) { - warn("output_json: fclose"); + error(0, 0, "output_json: fclose"); } } @@ -465,14 +468,15 @@ static void html_header(FILE *restrict f) struct tm *tmp, result; struct stat statbuf; + stat(config.dhcpdlease_file, &statbuf); tmp = localtime_r(&statbuf.st_mtime, &result); if (tmp == NULL) { - err(EXIT_FAILURE, "html_header: localtime"); + error(EXIT_FAILURE, errno, "html_header: localtime"); } if (strftime(outstr, sizeof(outstr), nl_langinfo(D_T_FMT), &result) == 0) { - errx(EXIT_FAILURE, "html_header: strftime returned 0"); + error(EXIT_FAILURE, 0, "html_header: strftime returned 0"); } fprintf(f, "backups); output_float(outfile, "td", shared_p->available == 0 ? -NAN : (float)(100 * - shared_p-> - backups) / - shared_p->available); + shared_p->backups) + / shared_p->available); } endrow(outfile); @@ -792,18 +796,16 @@ int output_html(void) output_double(outfile, "td", shared_networks->touched + shared_networks->used); output_float(outfile, "td", shared_networks->available == 0 ? -NAN : (float)(100 * - (shared_networks-> - touched + - shared_networks-> - used)) / - shared_networks->available); + (shared_networks->touched + + + shared_networks->used)) + / shared_networks->available); if (config.backups_found == true) { output_double(outfile, "td", shared_networks->backups); output_float(outfile, "td", shared_networks->available == 0 ? -NAN : (float)(100 * - shared_networks-> - backups) / - shared_networks->available); + shared_networks->backups) + / shared_networks->available); } endrow(outfile); } @@ -814,12 +816,12 @@ int output_html(void) if (outfile == stdout) { ret = fflush(stdout); if (ret) { - warn("output_html: fflush"); + error(0, 0, "output_html: fflush"); } } else { ret = close_stream(outfile); if (ret) { - warn("output_html: fclose"); + error(0, 0, "output_html: fclose"); } } return 0; @@ -836,10 +838,11 @@ int output_csv(void) struct shared_network_t *shared_p; FILE *outfile; int ret; + if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { - err(EXIT_FAILURE, "output_csv: %s", config.output_file); + error(EXIT_FAILURE, errno, "output_csv: %s", config.output_file); } } else { outfile = stdout; @@ -957,12 +960,12 @@ int output_csv(void) if (outfile == stdout) { ret = fflush(stdout); if (ret) { - warn("output_cvs: fflush"); + error(0, 0, "output_cvs: fflush"); } } else { ret = close_stream(outfile); if (ret) { - warn("output_cvs: fclose"); + error(0, 0, "output_cvs: fclose"); } } return 0; @@ -989,7 +992,7 @@ int output_alarming(void) if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { - err(EXIT_FAILURE, "output_alarming: %s", config.output_file); + error(EXIT_FAILURE, errno, "output_alarming: %s", config.output_file); } } else { outfile = stdout; @@ -1077,12 +1080,12 @@ int output_alarming(void) if (outfile == stdout) { ret = fflush(stdout); if (ret) { - warn("output_alarming: fflush"); + error(0, 0, "output_alarming: fflush"); } } else { ret = close_stream(outfile); if (ret) { - warn("output_alarming: fclose"); + error(0, 0, "output_alarming: fclose"); } } return ret_val; diff --git a/src/sort.c b/src/sort.c index b01db91..d0af3cb 100644 --- a/src/sort.c +++ b/src/sort.c @@ -40,15 +40,15 @@ #include -#include -#include #include #include #include -#include "dhcpd-pools.h" +#include "error.h" #include "progname.h" +#include "dhcpd-pools.h" + /*! \brief Compare IP address, with IPv4/v6 determination. * \param a Binary IP address. * \param b Binary IP address. @@ -237,8 +237,7 @@ comparer_t field_selector(char c) return comp_tcperc; default: clean_up(); - warnx("field_selector: unknown sort order `%c'", c); - errx(EXIT_FAILURE, "Try `%s --help' for more information.", program_name); + error(EXIT_FAILURE, 0, "field_selector: unknown sort order `%c'", c); } return NULL; } @@ -289,6 +288,7 @@ void mergesort_ranges(struct range_t *restrict orig, int size, struct range_t *r { int left, right, i; struct range_t hold; + /* Merge sort split size */ static const int MIN_MERGE_SIZE = 8;