From b53b8585d5f8075700f1637c84e8841ac349fe00 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Sat, 21 Jul 2012 14:02:42 -0400 Subject: [PATCH] Use clock_gettime(CLOCK_MONOTONIC) instead of time() in ifchd. Standardize license/copyright and version prints. --- LICENSE | 2 +- ifchd/ifchd.c | 45 ++++++++++++++++++++++++++++++++++++--------- ndhc/ndhc.c | 27 +++++++++++++++++++++------ 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/LICENSE b/LICENSE index 180af42..3c569fe 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2004-2011 Nicholas J. Kain +Copyright (c) 2004-2012 Nicholas J. Kain All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/ifchd/ifchd.c b/ifchd/ifchd.c index 7bae147..dde1f09 100644 --- a/ifchd/ifchd.c +++ b/ifchd/ifchd.c @@ -303,10 +303,18 @@ static void perform_ntpsrv(struct ifchd_client *cl, char *str) static void perform_wins(struct ifchd_client *cl, char *str) {} +static inline void clock_or_die(struct timespec *ts) +{ + if (clock_gettime(CLOCK_MONOTONIC, ts)) + suicide("clock_gettime failed %s", strerror(errno)); +} + static void ifchd_client_init(struct ifchd_client *p) { p->fd = -1; - p->idle_time = time(NULL); + struct timespec ts; + clock_or_die(&ts); + p->idle_time = ts.tv_sec; p->state = STATE_NOTHING; memset(p->ibuf, 0, sizeof p->ibuf); @@ -357,7 +365,9 @@ static void close_idle_sk(void) struct ifchd_client *p = &clients[i]; if (p->fd == -1) continue; - if (time(NULL) - p->idle_time > CONN_TIMEOUT) + struct timespec ts; + clock_or_die(&ts); + if (ts.tv_sec - p->idle_time > CONN_TIMEOUT) ifchd_client_wipe(p); } } @@ -661,7 +671,9 @@ static void process_client_fd(int fd) if (!cl) suicide("epoll returned pending read for untracked fd"); - cl->idle_time = time(NULL); + struct timespec ts; + clock_or_die(&ts); + cl->idle_time = ts.tv_sec; memset(buf, '\0', sizeof buf); r = safe_read(cl->fd, buf, sizeof buf - 1); @@ -787,12 +799,27 @@ int main(int argc, char** argv) { break; case 'v': - printf( -"ifchd %s, if change daemon. Licensed under 2-clause BSD.\n", IFCHD_VERSION); - printf( -"Copyright (C) 2004-2012 Nicholas J. Kain\nAll rights reserved.\n" -"This is free software; see the source for copying conditions. There is NO\n" -"WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"); + printf("ifchd %s, if change daemon.\n", IFCHD_VERSION); + printf("Copyright (c) 2004-2012 Nicholas J. Kain\n" + "All rights reserved.\n\n" + "Redistribution and use in source and binary forms, with or without\n" + "modification, are permitted provided that the following conditions are met:\n\n" + "- Redistributions of source code must retain the above copyright notice,\n" + " this list of conditions and the following disclaimer.\n" + "- Redistributions in binary form must reproduce the above copyright notice,\n" + " this list of conditions and the following disclaimer in the documentation\n" + " and/or other materials provided with the distribution.\n\n" + "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n" + "AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n" + "IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n" + "ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE\n" + "LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n" + "CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n" + "SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n" + "INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n" + "CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n" + "ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n" + "POSSIBILITY OF SUCH DAMAGE.\n"); exit(EXIT_FAILURE); break; diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c index e10875c..dde2247 100644 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -386,12 +386,27 @@ int main(int argc, char **argv) arp_relentless_def = 1; break; case 'v': - printf( -"ndhc %s, dhcp client. Licensed under 2-clause BSD.\n", VERSION); - printf( -"Copyright (C) 2004-2012 Nicholas J. Kain\nAll rights reserved.\n" -"This is free software; see the source for copying conditions. There is NO\n" -"WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"); + printf("ndhc %s, dhcp client.\n", VERSION); + printf("Copyright (c) 2004-2012 Nicholas J. Kain\n" + "All rights reserved.\n\n" + "Redistribution and use in source and binary forms, with or without\n" + "modification, are permitted provided that the following conditions are met:\n\n" + "- Redistributions of source code must retain the above copyright notice,\n" + " this list of conditions and the following disclaimer.\n" + "- Redistributions in binary form must reproduce the above copyright notice,\n" + " this list of conditions and the following disclaimer in the documentation\n" + " and/or other materials provided with the distribution.\n\n" + "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n" + "AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n" + "IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n" + "ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE\n" + "LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n" + "CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n" + "SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n" + "INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n" + "CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n" + "ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n" + "POSSIBILITY OF SUCH DAMAGE.\n"); exit(EXIT_SUCCESS); break; case 'V':