diff --git a/configure b/configure index 4734a5f..8272b69 100644 --- a/configure +++ b/configure @@ -64,6 +64,7 @@ SRCS=' ntp_peer.c ntp_peerset.c ntp_tools.c + ocx.c ocx_stdio.c param.c pll_std.c diff --git a/main.c b/main.c index 449ba9a..c6aebd8 100644 --- a/main.c +++ b/main.c @@ -44,7 +44,7 @@ dummy(void) } static int -main_run_tests(int argc, char * const * argv) +main_run_tests(struct ocx *ocx, int argc, char * const * argv) { (void)argc; @@ -52,7 +52,7 @@ main_run_tests(int argc, char * const * argv) Time_Unix_Passive(); - TS_RunTest(NULL); + TS_RunTest(ocx); return (0); } @@ -64,11 +64,11 @@ main(int argc, char * const *argv) dummy(); if (argc > 1 && !strcmp(argv[1], "--poll-server")) - return (main_poll_server(argc - 1, argv + 1)); + return (main_poll_server(NULL, argc - 1, argv + 1)); if (argc > 1 && !strcmp(argv[1], "--sim-client")) - return (main_sim_client(argc - 1, argv + 1)); + return (main_sim_client(NULL, argc - 1, argv + 1)); if (argc > 1 && !strcmp(argv[1], "--run-tests")) - return (main_run_tests(argc - 1, argv + 1)); + return (main_run_tests(NULL, argc - 1, argv + 1)); - return (main_client(argc, argv)); + return (main_client(NULL, argc, argv)); } diff --git a/main_client.c b/main_client.c index d87834f..d2d5695 100644 --- a/main_client.c +++ b/main_client.c @@ -56,7 +56,7 @@ sig_hup(int siginfo) } int -main_client(int argc, char *const *argv) +main_client(struct ocx *ocx, int argc, char *const *argv) { int ch; struct ntp_peer *np; @@ -74,7 +74,7 @@ main_client(int argc, char *const *argv) PLL_Init(); - nps = NTP_PeerSet_New(NULL); + nps = NTP_PeerSet_New(ocx); Param_Register(client_param_table); NF_Init(); @@ -82,13 +82,13 @@ main_client(int argc, char *const *argv) while ((ch = getopt(argc, argv, "p:t:")) != -1) { switch(ch) { case 'p': - Param_Tweak(NULL, optarg); + Param_Tweak(ocx, optarg); break; case 't': - ArgTracefile(optarg); + ArgTracefile(ocx, optarg); break; default: - Fail(NULL, 0, + Fail(ocx, 0, "Usage %s [-p param] [-t tracefile] servers...", argv[0]); break; @@ -98,18 +98,18 @@ main_client(int argc, char *const *argv) argv += optind; for (ch = 0; ch < argc; ch++) - npeer += NTP_PeerSet_Add(NULL, nps, argv[ch]); + npeer += NTP_PeerSet_Add(ocx, nps, argv[ch]); if (npeer == 0) - Fail(NULL, 0, "No NTP peers found"); + Fail(ocx, 0, "No NTP peers found"); - Put(NULL, OCX_TRACE, "# NTIMED Format client 1.0\n"); - Put(NULL, OCX_TRACE, "# Found %d peers\n", npeer); + Put(ocx, OCX_TRACE, "# NTIMED Format client 1.0\n"); + Put(ocx, OCX_TRACE, "# Found %d peers\n", npeer); - Param_Report(NULL, OCX_TRACE); + Param_Report(ocx, OCX_TRACE); - usc = UdpTimedSocket(NULL); + usc = UdpTimedSocket(ocx); if (usc == NULL) - Fail(NULL, errno, "Could not open UDP socket"); + Fail(ocx, errno, "Could not open UDP socket"); cd = CD_New(); @@ -120,13 +120,13 @@ main_client(int argc, char *const *argv) do { if (restart) { - Debug(NULL, "RESTART\n"); + Debug(ocx, "RESTART\n"); TB_generation++; - NTP_PeerSet_Poll(NULL, nps, usc, tdl); + NTP_PeerSet_Poll(ocx, nps, usc, tdl); restart = 0; } (void)signal(SIGHUP, sig_hup); - (void)TODO_Run(NULL, tdl); + (void)TODO_Run(ocx, tdl); } while (restart); return (0); diff --git a/main_poll_server.c b/main_poll_server.c index 1da8381..6f5224c 100644 --- a/main_poll_server.c +++ b/main_poll_server.c @@ -83,7 +83,7 @@ mps_end(struct ocx *ocx, struct todolist *tdl, void *priv) } int -main_poll_server(int argc, char *const *argv) +main_poll_server(struct ocx *ocx, int argc, char *const *argv) { int ch; int npeer = 0; @@ -97,12 +97,12 @@ main_poll_server(int argc, char *const *argv) setbuf(stdout, NULL); setbuf(stderr, NULL); - ArgTracefile("-"); + ArgTracefile(ocx, "-"); tdl = TODO_NewList(); Time_Unix_Passive(); - npl = NTP_PeerSet_New(NULL); + npl = NTP_PeerSet_New(ocx); AN(npl); while ((ch = getopt(argc, argv, "d:m:t:")) != -1) { @@ -110,18 +110,18 @@ main_poll_server(int argc, char *const *argv) case 'd': duration = strtod(optarg, &p); if (*p != '\0' || duration < 1.0) - Fail(NULL, 0, "Invalid -d argument"); + Fail(ocx, 0, "Invalid -d argument"); break; case 'm': - mon = NTP_Peer_NewLookup(NULL, optarg); + mon = NTP_Peer_NewLookup(ocx, optarg); if (mon == NULL) - Fail(NULL, 0, "Monitor (-m) didn't resolve."); + Fail(ocx, 0, "Monitor (-m) didn't resolve."); break; case 't': - ArgTracefile(optarg); + ArgTracefile(ocx, optarg); break; default: - Fail(NULL, 0, + Fail(ocx, 0, "Usage %s [-d duration] [-m monitor] " "[-t tracefile] server...", argv[0]); break; @@ -131,22 +131,22 @@ main_poll_server(int argc, char *const *argv) argv += optind; for (ch = 0; ch < argc; ch++) - npeer += NTP_PeerSet_Add(NULL, npl, argv[ch]); - Put(NULL, OCX_TRACE, "# NTIMED Format poll-server 1.0\n"); - Put(NULL, OCX_TRACE, "# Found %d peers\n", npeer); + npeer += NTP_PeerSet_Add(ocx, npl, argv[ch]); + Put(ocx, OCX_TRACE, "# NTIMED Format poll-server 1.0\n"); + Put(ocx, OCX_TRACE, "# Found %d peers\n", npeer); if (npeer == 0) - Fail(NULL, 0, "No peers found"); + Fail(ocx, 0, "No peers found"); NTP_PeerSet_Foreach(np, npl) { - Put(NULL, OCX_TRACE, "# Peer %s %s\n", np->hostname, np->ip); + Put(ocx, OCX_TRACE, "# Peer %s %s\n", np->hostname, np->ip); np->filter_func = mps_filter; } if (mon != NULL) - Put(NULL, OCX_TRACE, + Put(ocx, OCX_TRACE, "# Monitor %s %s\n", mon->hostname, mon->ip); - usc = UdpTimedSocket(NULL); + usc = UdpTimedSocket(ocx); assert(usc != NULL); TODO_ScheduleRel(tdl, mps_end, NULL, duration, 0, "End task"); @@ -154,7 +154,7 @@ main_poll_server(int argc, char *const *argv) if (mon != NULL) TODO_ScheduleRel(tdl, mps_mon, mon, 0, 32, "Monitor"); - NTP_PeerSet_Poll(NULL, npl, usc, tdl); - (void)TODO_Run(NULL, tdl); + NTP_PeerSet_Poll(ocx, npl, usc, tdl); + (void)TODO_Run(ocx, tdl); return (0); } diff --git a/main_sim_client.c b/main_sim_client.c index 4da8972..d4ae4a6 100644 --- a/main_sim_client.c +++ b/main_sim_client.c @@ -214,12 +214,12 @@ SimFile_Open(struct ocx *ocx, const char *fn, struct todolist *tdl, "XXX: Wrong state (%d) in open_sim_file", s); } } - (void)simfile_readline(NULL, tdl, sf); + (void)simfile_readline(ocx, tdl, sf); return (sf); } int -main_sim_client(int argc, char *const *argv) +main_sim_client(struct ocx *ocx, int argc, char *const *argv) { int ch; const char *s_filename = NULL; @@ -238,7 +238,7 @@ main_sim_client(int argc, char *const *argv) PLL_Init(); - npl = NTP_PeerSet_New(NULL); + npl = NTP_PeerSet_New(ocx); Param_Register(client_param_table); NF_Init(); @@ -248,7 +248,7 @@ main_sim_client(int argc, char *const *argv) case 'B': ch = sscanf(optarg, "%lg,%lg,%lg", &a, &b, &c); if (ch != 3) - Fail(NULL, 0, + Fail(ocx, 0, "bad -B argument \"when,freq,phase\""); Time_Sim_Bump(tdl, a, b, c); break; @@ -256,13 +256,13 @@ main_sim_client(int argc, char *const *argv) s_filename = optarg; break; case 'p': - Param_Tweak(NULL, optarg); + Param_Tweak(ocx, optarg); break; case 't': - ArgTracefile(optarg); + ArgTracefile(ocx, optarg); break; default: - Fail(NULL, 0, + Fail(ocx, 0, "Usage %s [-s simfile] [-p params] [-t tracefile]" " [-B when,freq,phase]", argv[0]); break; @@ -271,12 +271,12 @@ main_sim_client(int argc, char *const *argv) // argc -= optind; // argv += optind; - Param_Report(NULL, OCX_TRACE); + Param_Report(ocx, OCX_TRACE); if (s_filename == NULL) - Fail(NULL, 1, "You must specify -s file."); + Fail(ocx, 1, "You must specify -s file."); - sf = SimFile_Open(NULL, s_filename, tdl, npl); + sf = SimFile_Open(ocx, s_filename, tdl, npl); AN(sf); cd = CD_New(); @@ -286,7 +286,7 @@ main_sim_client(int argc, char *const *argv) np->combiner = CD_AddSource(cd, np->hostname, np->ip); } - (void)TODO_Run(NULL, tdl); + (void)TODO_Run(ocx, tdl); return (0); } diff --git a/ntimed.h b/ntimed.h index 491491a..a2a70e6 100644 --- a/ntimed.h +++ b/ntimed.h @@ -32,6 +32,7 @@ #endif #define NTIMED_H_INCLUDED +#include #include #include #include "ntimed_queue.h" @@ -64,7 +65,10 @@ void Fail(struct ocx *, int err, const char *, ...) \ #define Debug(ocx, ...) Put(ocx, OCX_DEBUG, __VA_ARGS__) #define DebugHex(ocx, ptr, len) PutHex(ocx, OCX_DEBUG, ptr, len) -void ArgTracefile(const char *fn); +/* ocx_stdio */ +void __match_proto__() +putv(struct ocx *ocx, enum ocx_chan chan, const char *fmt, va_list ap); +void ArgTracefile(struct ocx *ocx, const char *fn); /* param.c -- Parameters **********************************************/ @@ -186,6 +190,6 @@ struct combiner *CD_AddSource(struct combine_delta *, * Main functions */ -int main_client(int argc, char *const *argv); -int main_poll_server(int argc, char *const *argv); -int main_sim_client(int argc, char *const *argv); +int main_client(struct ocx *ocx, int argc, char *const *argv); +int main_poll_server(struct ocx *ocx, int argc, char *const *argv); +int main_sim_client(struct ocx *ocx, int argc, char *const *argv); diff --git a/ntp_filter.c b/ntp_filter.c index bd9cb9e..f5fbb16 100644 --- a/ntp_filter.c +++ b/ntp_filter.c @@ -77,7 +77,7 @@ nf_filter(struct ocx *ocx, const struct ntp_peer *np) NTP_Tool_Format(buf, sizeof buf, rxp); - Put(NULL, OCX_TRACE, "NTP_Packet %s %s %s\n", + Put(ocx, OCX_TRACE, "NTP_Packet %s %s %s\n", np->hostname, np->ip, buf); if (rxp->ntp_leap == NTP_LEAP_UNKNOWN) diff --git a/ocx.c b/ocx.c new file mode 100644 index 0000000..06e5f03 --- /dev/null +++ b/ocx.c @@ -0,0 +1,81 @@ +/*- + * Copyright (c) 2014 Poul-Henning Kamp + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Operational Context STDIO + * ========================= + * + * Theis contains the generic stuff. Se ocx_stdio.c for the details. + * + */ + +#include +#include +#include +#include + +#include "ntimed.h" + +void +Put(struct ocx *ocx, enum ocx_chan chan, const char *fmt, ...) +{ + va_list ap; + + AZ(ocx); + va_start(ap, fmt); + putv(ocx, chan, fmt, ap); + va_end(ap); +} + +void +PutHex(struct ocx *ocx, enum ocx_chan chan, const void *ptr, ssize_t len) +{ + const uint8_t *p = ptr; + const char *s = ""; + + AN(ptr); + assert(len >= 0); + + while(len--) { + Put(ocx, chan, "%s%02x", s, *p++); + s = " "; + } +} + +void +Fail(struct ocx *ocx, int err, const char *fmt, ...) +{ + va_list ap; + + if (err) + err = errno; + Put(ocx, OCX_DIAG, "Failure: "); + va_start(ap, fmt); + putv(ocx, OCX_DIAG, fmt, ap); + va_end(ap); + Put(ocx, OCX_DIAG, "\n"); + if (err) + Put(ocx, OCX_DIAG, "errno = %d (%s)\n", err, strerror(err)); + exit(1); +} diff --git a/ocx_stdio.c b/ocx_stdio.c index af08f9f..5d35597 100644 --- a/ocx_stdio.c +++ b/ocx_stdio.c @@ -82,7 +82,7 @@ getdst(enum ocx_chan chan) NEEDLESS_RETURN(NULL); } -static void __match_proto__() +void __match_proto__() putv(struct ocx *ocx, enum ocx_chan chan, const char *fmt, va_list ap) { FILE *dst = getdst(chan); @@ -102,7 +102,7 @@ putv(struct ocx *ocx, enum ocx_chan chan, const char *fmt, va_list ap) */ void -ArgTracefile(const char *fn) +ArgTracefile(struct ocx *ocx, const char *fn) { if (tracefile != NULL && tracefile != stdout) { @@ -120,54 +120,7 @@ ArgTracefile(const char *fn) tracefile = fopen(fn, "w"); if (tracefile == NULL) - Fail(NULL, 1, "Could not open '%s' for writing", fn); + Fail(ocx, 1, "Could not open '%s' for writing", fn); setbuf(tracefile, NULL); } -/********************************************************************** - * XXX: The stuff below is generic and really ought to be in ocx.c on - * XXX: its own. - */ - -void -Put(struct ocx *ocx, enum ocx_chan chan, const char *fmt, ...) -{ - va_list ap; - - AZ(ocx); - va_start(ap, fmt); - putv(ocx, chan, fmt, ap); - va_end(ap); -} - -void -PutHex(struct ocx *ocx, enum ocx_chan chan, const void *ptr, ssize_t len) -{ - const uint8_t *p = ptr; - const char *s = ""; - - AN(ptr); - assert(len >= 0); - - while(len--) { - Put(ocx, chan, "%s%02x", s, *p++); - s = " "; - } -} - -void -Fail(struct ocx *ocx, int err, const char *fmt, ...) -{ - va_list ap; - - if (err) - err = errno; - Put(ocx, OCX_DIAG, "Failure: "); - va_start(ap, fmt); - putv(ocx, OCX_DIAG, fmt, ap); - va_end(ap); - Put(ocx, OCX_DIAG, "\n"); - if (err) - Put(ocx, OCX_DIAG, "errno = %d (%s)\n", err, strerror(err)); - exit(1); -}