From a60018362f16ca094bb4c985971fb6131dec31d0 Mon Sep 17 00:00:00 2001 From: Chris Johns Date: Sat, 20 May 2023 12:55:15 -1000 Subject: bsd/ntpd: Restart global variable destruction --- bsd/freebsd/contrib/ntp/libntp/emalloc.c | 10 ++++ bsd/freebsd/contrib/ntp/libntp/ntp_intres.c | 32 +++++++++++ bsd/freebsd/contrib/ntp/libntp/ntp_worker.c | 32 +++++++++++ bsd/freebsd/contrib/ntp/ntpd/ntp_config.c | 22 ++++++++ bsd/freebsd/contrib/ntp/ntpd/ntp_control.c | 57 ++++++++++++++++++++ bsd/freebsd/contrib/ntp/ntpd/ntp_crypto.c | 4 ++ bsd/freebsd/contrib/ntp/ntpd/ntp_io.c | 61 ++++++++++++++++++++- bsd/freebsd/contrib/ntp/ntpd/ntp_loopfilter.c | 54 +++++++++++++++++++ bsd/freebsd/contrib/ntp/ntpd/ntp_monitor.c | 16 +++++- bsd/freebsd/contrib/ntp/ntpd/ntp_peer.c | 22 ++++++++ bsd/freebsd/contrib/ntp/ntpd/ntp_proto.c | 76 +++++++++++++++++++++++++++ bsd/freebsd/contrib/ntp/ntpd/ntp_request.c | 22 +++++++- bsd/freebsd/contrib/ntp/ntpd/ntp_restrict.c | 34 ++++++++++++ bsd/freebsd/contrib/ntp/ntpd/ntp_timer.c | 22 ++++++++ bsd/freebsd/contrib/ntp/ntpd/ntpd.c | 61 ++++++++++++++++++++- 15 files changed, 520 insertions(+), 5 deletions(-) diff --git a/bsd/freebsd/contrib/ntp/libntp/emalloc.c b/bsd/freebsd/contrib/ntp/libntp/emalloc.c index 10c2d80..0092642 100644 --- a/bsd/freebsd/contrib/ntp/libntp/emalloc.c +++ b/bsd/freebsd/contrib/ntp/libntp/emalloc.c @@ -10,6 +10,16 @@ #include "ntp_stdlib.h" +#ifdef __rtems__ +#ifdef EREALLOC_IMPL +static void *rtems_ntp_realloc(void *ptr, size_t size, const char* file, int line) { + void* mem = realloc(ptr, size); + printf("[EREMALLOC] %s:%d: ptr=%p mem=%p..%p newsz=%zu\n", + file, line, ptr, mem, mem + size, size); + return mem; +} +#endif /* EREALLOC_IMPL */ +#endif /* __rtems__ */ /* * When using the debug MS CRT allocator, each allocation stores the * callsite __FILE__ and __LINE__, which is then displayed at process diff --git a/bsd/freebsd/contrib/ntp/libntp/ntp_intres.c b/bsd/freebsd/contrib/ntp/libntp/ntp_intres.c index ec9aa55..42d93a1 100644 --- a/bsd/freebsd/contrib/ntp/libntp/ntp_intres.c +++ b/bsd/freebsd/contrib/ntp/libntp/ntp_intres.c @@ -228,6 +228,36 @@ static void getnameinfo_sometime_complete(blocking_work_req, void *); +#ifdef __rtems__ +#define RTEMS_NTP_CLEAR(_var) memset(&_var, 0, sizeof(_var)) +static u_int shared_ctx = UINT_MAX; +void rtems_ntp_intres_globals_fini(void) { + if (dnschild_contexts != NULL) { + int c; + for (c = 0; c < dnschild_contexts_alloc; c++) { + if (NULL != dnschild_contexts[c]) { + free(dnschild_contexts[c]); + } + } + free(dnschild_contexts); + dnschild_contexts = NULL; + } + dnschild_contexts_alloc = 0; + if (dnsworker_contexts != NULL) { + int c; + for (c = 0; c < dnsworker_contexts_alloc; c++) { + if (NULL != dnsworker_contexts[c]) { + free(dnsworker_contexts[c]); + } + } + free(dnsworker_contexts); + dnsworker_contexts = NULL; + } + dnsworker_contexts_alloc = 0; + RTEMS_NTP_CLEAR(next_res_init); + shared_ctx = UINT_MAX; +} +#endif /* __rtems__ */ /* === functions === */ /* * getaddrinfo_sometime - uses blocking child to call getaddrinfo then @@ -957,7 +987,9 @@ reserve_dnschild_ctx(void) static u_int get_dnschild_ctx(void) { +#ifndef __rtems__ static u_int shared_ctx = UINT_MAX; +#endif /* __rtems__ */ if (worker_per_query) return reserve_dnschild_ctx(); diff --git a/bsd/freebsd/contrib/ntp/libntp/ntp_worker.c b/bsd/freebsd/contrib/ntp/libntp/ntp_worker.c index 7f2cda0..ce5443e 100644 --- a/bsd/freebsd/contrib/ntp/libntp/ntp_worker.c +++ b/bsd/freebsd/contrib/ntp/libntp/ntp_worker.c @@ -33,6 +33,36 @@ volatile u_int blocking_child_ready_seen; volatile u_int blocking_child_ready_done; +#ifdef __rtems__ +#define RTEMS_NTP_CLEAR(_var) memset(&_var, 0, sizeof(_var)) +static u_int intres_slot = UINT_MAX; +void rtems_ntp_worker_globals_fini(void); +void rtems_ntp_worker_globals_fini(void) { + size_t idx; + while (intres_req_pending > 0) { + harvest_blocking_responses(); + usleep(10 * 1000UL); + } + for (idx = 0; idx < blocking_children_alloc; idx++) { + blocking_child* c = blocking_children[idx]; + if (c != NULL) { + blocking_children[idx] = NULL; + req_child_exit(c); + while (!c->reusable) { + usleep(10 * 1000UL); + process_blocking_resp(c); + } + free(c); + } + } + if (blocking_children != NULL) { + free(blocking_children); + blocking_children = NULL; + blocking_children_alloc = 0; + } + intres_slot = UINT_MAX; +} +#endif /* __rtems__ */ #ifndef HAVE_IO_COMPLETION_PORT /* * pipe_socketpair() @@ -172,7 +202,9 @@ queue_blocking_request( void * context ) { +#ifndef __rtems__ static u_int intres_slot = UINT_MAX; +#endif /* __rtems__ */ u_int child_slot; blocking_child * c; blocking_pipe_header req_hdr; diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_config.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_config.c index 7a469ac..e6eb03e 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_config.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_config.c @@ -223,6 +223,9 @@ int *p_bcXXXX_enabled = &bc_list[0].enabled; /* FUNCTION PROTOTYPES */ +#ifdef __rtems__ +#define FREE_CFG_T +#endif /* __rtems__ */ static void init_syntax_tree(config_tree *); static void apply_enable_disable(attr_val_fifo *q, int enable); @@ -375,6 +378,25 @@ static int getnetnum(const char *num, sockaddr_u *addr, int complain, #endif +#ifdef __rtems__ +#define RTEMS_NTP_CLEAR(_var) memset(&_var, 0, sizeof(_var)) +void rtems_ntp_config_globals_fini(void); +void rtems_ntp_config_globals_fini(void) { + free_all_config_trees(); + cur_memlock = -1; + RTEMS_NTP_CLEAR(cfgt); + cfg_tree_history = NULL; + RTEMS_NTP_CLEAR(sys_phone); + strlcpy(default_keysdir, NTP_KEYSDIR, sizeof(default_keysdir)); + saveconfigdir = NULL; + config_priority_override = 0; + strlcpy(default_ntp_signd_socket, NTP_SIGND_PATH, sizeof(default_ntp_signd_socket)); + RTEMS_NTP_CLEAR(remote_config); + old_config_style = 1; + cryptosw = 0; + stats_drift_file = NULL; +} +#endif /* __rtems__ */ #if defined(__GNUC__) /* this covers CLANG, too */ static void __attribute__((noreturn,format(printf,1,2))) fatal_error(const char *fmt, ...) #elif defined(_MSC_VER) diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_control.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_control.c index 813ae8a..4292430 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_control.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_control.c @@ -822,6 +822,63 @@ static char *reqend; #define MIN(a, b) (((a) <= (b)) ? (a) : (b)) #endif +#ifdef __rtems__ +#define RTEMS_NTP_CLEAR(_var) memset(&_var, 0, sizeof(_var)) +void rtems_ntp_control_globals_fini(void); +void rtems_ntp_control_globals_fini(void) { + if (ext_sys_var != NULL) { + const u_short cv = count_var(ext_sys_var); + struct ctl_var *k; + u_short c; + k = ext_sys_var; + for (c = 0; c < cv; ++c) { + if (k[c].text != NULL) { + free((void*) k[c].text); + } + } + free(ext_sys_var); + ext_sys_var = NULL; + } + RTEMS_NTP_CLEAR(ctl_traps); + num_ctl_traps = 0; + RTEMS_NTP_CLEAR(ctl_auth_keyid); + ctl_sys_last_event = 0; + ctl_sys_num_events = 0; + ctltimereset = 0U; + numctlreq = 0U; + numctlbadpkts = 0U; + numctlresponses = 0U; + numctlfrags = 0U; + numctlerrors = 0U; + numctltooshort = 0U; + numctlinputresp = 0U; + numctlinputfrag = 0U; + numctlinputerr = 0U; + numctlbadoffset = 0U; + numctlbadversion = 0U; + numctldatatooshort = 0U; + numctlbadop = 0U; + numasyncmsgs = 0U; + RTEMS_NTP_CLEAR(rpkt); + res_version = 0U; + res_opcode = 0U; + res_associd = 0U; + res_frags = 0U; + res_offset = 0U; + datapt = NULL; + dataend = NULL; + datalinelen = 0; + datasent = 0; + datalinelen = 0; + datanotbinflag = 0; + lcl_inter = NULL; + res_authenticate = 0U; + res_authokay = 0U; + res_keyid = 0U; + reqpt = NULL; + reqend = NULL; +} +#endif /* __rtems__ */ /* * init_control - initialize request data */ diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_crypto.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_crypto.c index 3e5647d..0167ee1 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_crypto.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_crypto.c @@ -4171,4 +4171,8 @@ exten_payload_size( } # else /* !AUTOKEY follows */ int ntp_crypto_bs_pubkey; +void rtems_ntp_crypto_globals_ini(void); +void rtems_ntp_crypto_globals_ini(void) { + ntp_crypto_bs_pubkey = 0; +} # endif /* !AUTOKEY */ diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_io.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_io.c index 448a2d9..c24db18 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_io.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_io.c @@ -372,6 +372,50 @@ static void input_handler (l_fp*); #endif +#ifdef __rtems__ +static int rtems_ntp_already_opened; +#define already_opened rtems_ntp_already_opened +void rtems_ntp_io_globals_fini(void); +void rtems_ntp_io_globals_fini(void) { + while (ep_list != NULL) { + remove_interface(ep_list); + } + ep_list = NULL; + any_interface = NULL; + any6_interface = NULL; + loopback_interface = NULL; + mc4_list = NULL; + mc6_list = NULL; + wildipv4 = NULL; + wildipv6 = NULL; + while (fd_list != NULL) { + close_and_delete_fd_from_list(fd_list->fd); + } + fd_list = NULL; + while (remoteaddr_list != NULL) { + delete_addr_from_list(&remoteaddr_list->addr); + } + remoteaddr_list = NULL; + already_opened = 0; + packets_dropped = 0; + packets_ignored = 0; + packets_received = 0; + packets_sent = 0; + packets_notsent = 0; + handler_calls = 0; + handler_pkts = 0; + io_timereset = 0; + broadcast_client_enabled = 0; + sys_ifnum = 0; + ninterfaces = 0; + disable_dynamic_updates = 0; + sys_interphase = 0; + if (activefds_prealloc != NULL) { + memset(activefds_prealloc, 0, rtems_ntpd_fds_size); + } + maxactivefd = 0; +} +#endif /* __rtems__ */ #ifndef HAVE_IO_COMPLETION_PORT void maintain_activefds( @@ -399,6 +443,9 @@ maintain_activefds( } else { FD_CLR(fd, &activefds); if (maxactivefd && fd == maxactivefd) { +#ifdef __rtems__ + maxactivefd = 0; +#endif /* __rtems__ */ for (i = maxactivefd - 1; i >= 0; i--) if (FD_ISSET(i, &activefds)) { maxactivefd = i; @@ -519,7 +566,9 @@ ntpd_addremove_io_fd( void io_open_sockets(void) { +#ifndef __rtems__ static int already_opened; +#endif /* __rtems__ */ if (already_opened || HAVE_OPT( SAVECONFIGQUIT )) return; @@ -1049,7 +1098,6 @@ remove_interface( close_and_delete_fd_from_list(ep->fd); ep->fd = INVALID_SOCKET; } - if (ep->bfd != INVALID_SOCKET) { msyslog(LOG_INFO, "stop listening for broadcasts to %s on interface #%d %s", @@ -2101,7 +2149,9 @@ create_sockets( * I/O Completion Ports don't care about the select and FD_SET */ maxactivefd = 0; +#ifndef __rtems__ FD_ZERO(&activefds); +#endif /* __rtems__ */ #endif DPRINTF(2, ("create_sockets(%d)\n", port)); @@ -4639,6 +4689,11 @@ delete_addr_from_list( { remaddr_t *unlinked; +#ifdef __rtems__ + if (remoteaddr_list == NULL) { + return; + } +#endif /* __rtems__ */ UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, SOCK_EQ(addr, &(UNLINK_EXPR_SLIST_CURRENT()->addr)), link, remaddr_t); @@ -4657,7 +4712,11 @@ delete_interface_from_list( { remaddr_t *unlinked; +#ifndef __rtems__ for (;;) { +#else /* __rtems__ */ + for (;remoteaddr_list != NULL;) { +#endif /* __rtems__ */ UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, iface == UNLINK_EXPR_SLIST_CURRENT()->ep, link, remaddr_t); diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_loopfilter.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_loopfilter.c index 07ec14c..6fbfe8f 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_loopfilter.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_loopfilter.c @@ -193,6 +193,60 @@ static sigjmp_buf env; /* environment var. for pll_trap() */ #endif /* SIGSYS */ #endif /* KERNEL_PLL */ +#ifdef __rtems__ +#define RTEMS_NTP_CLEAR(_var) memset(&_var, 0, sizeof(_var)) +void rtems_ntp_loopfilter_globals_fini(void); +void rtems_ntp_loopfilter_globals_fini(void) { + clock_max_back = CLOCK_MAX; + clock_max_fwd = CLOCK_MAX; + clock_minstep = CLOCK_MINSTEP; + clock_panic = CLOCK_PANIC; + clock_phi = CLOCK_PHI; + allan_xpt = CLOCK_ALLAN; + clock_offset = 0; + clock_jitter = 0; + drift_comp = 0; + init_drift_comp = 0; + clock_stability = 0; + clock_codec = 0; + clock_epoch = 0; + sys_tai = 0; + loop_started = 0; + RTEMS_NTP_CLEAR(relative_path); + this_file = NULL; + ntp_enable = TRUE; + pll_control = 0; + kern_enable = TRUE; + hardpps_enable; + ext_enable = 0; + pps_stratum = 0; + kernel_status = 0; + force_step_once = FALSE; + mode_ntpdate = FALSE; + freq_cnt = 0; + freq_set = 0; + state = 0; + sys_poll = 0; + tc_counter = 0; + last_offset = 0.0; + +/* + * Clock state machine variables + */ +int state = 0; /* clock discipline state */ +u_char sys_poll; /* time constant/poll (log2 s) */ +int tc_counter; /* jiggle counter */ +double last_offset; /* last offset (s) */ + +/* + * Huff-n'-puff filter variables + */ +static double *sys_huffpuff; /* huff-n'-puff filter */ +static int sys_hufflen; /* huff-n'-puff filter stages */ +static int sys_huffptr; /* huff-n'-puff filter pointer */ +static double sys_mindly; /* huff-n'-puff filter min delay */ +} +#endif /* __rtems__ */ static void sync_status(const char *what, int ostatus, int nstatus) { diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_monitor.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_monitor.c index c0e4359..9dd85bb 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_monitor.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_monitor.c @@ -98,6 +98,20 @@ u_char ntp_minpoll = NTP_MINPOLL; /* increment (log 2 s) */ MRU_MAXDEPTH_DEF; int mon_age = 3000; /* preemption limit */ +#ifdef __rtems__ +#define RTEMS_NTP_CLEAR(_var) memset(&_var, 0, sizeof(_var)) +void rtems_ntp_monitor_globals_fini(void); +void rtems_ntp_monitor_globals_fini(void) { + mon_stop(MON_ON | MON_RES); + ntp_minpkt = NTP_MINPKT; + ntp_minpoll = NTP_MINPOLL; + mon_enabled = 0; + mru_mindepth = 600; + mru_maxage = 64; + mru_maxdepth = MRU_MAXDEPTH_DEF; + mon_age = 3000; +} +#endif /* __rtems__ */ static void mon_getmoremem(void); static void remove_from_hash(mon_entry *); static inline void mon_free_entry(mon_entry *); @@ -498,5 +512,3 @@ ntp_monitor( return mon->flags; } - - diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_peer.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_peer.c index e2c2152..522e611 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_peer.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_peer.c @@ -125,6 +125,28 @@ static void getmorepeermem(void); static int score(struct peer *); +#ifdef __rtems__ +#define RTEMS_NTP_CLEAR(_var) memset(&_var, 0, sizeof(_var)) +void rtems_ntp_peer_globals_fini(void); +void rtems_ntp_peer_globals_fini(void) { + /* we cannot clean up the peers list because eallocarray keeps + * no base to free; move to free */ + while (peer_list != NULL) { + unpeer(peer_list); + } + current_association_ID = 0; + initial_association_ID = 0; + peer_timereset = 0; + findpeer_calls = 0; + assocpeer_calls = 0; + peer_allocations = 0; + peer_demobilizations = 0; + total_peer_structs = 0; + total_peer_structs = 0; + peer_associations = 0; + peer_preempt = 0; +} +#endif /* __rtems__ */ /* * init_peer - initialize peer data structures and counters * diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_proto.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_proto.c index e4312db..79afd1d 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_proto.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_proto.c @@ -213,6 +213,80 @@ void pool_name_resolved (int, int, void *, const char *, const char * amtoa (int am); +#ifdef __rtems__ +#define RTEMS_NTP_CLEAR(_var) memset(&_var, 0, sizeof(_var)) +static struct endpoint *endpoint = NULL; +static int *indx = NULL; +static peer_select *peers = NULL; +static u_int endpoint_size = 0; +static u_int peers_size = 0; +static u_int indx_size = 0; +void rtems_ntp_proto_globals_fini(void); +void rtems_ntp_proto_globals_fini(void) { + sys_leap = 0; + xmt_leap = 0; + sys_stratum = 0; + sys_precision = 0; + sys_rootdelay = 0; + sys_rootdelay = 0.0; + sys_rootdisp = 0.0; + sys_refid = 0; + RTEMS_NTP_CLEAR(sys_reftime); + sys_peer = NULL; + sys_bclient = 0; + sys_bdelay = 0.0; + sys_authenticate = 0; + RTEMS_NTP_CLEAR(sys_authdelay); + sys_offset = 0.0; + sys_mindisp = MINDISPERSE; + sys_maxdist = MAXDISTANCE; + sys_jitter = 0.0; + sys_epoch = 0U; + sys_clockhop = 0.0; + leap_vote_ins = 0; + leap_vote_del = 0; + sys_private = 0; + sys_manycastserver = 0; + ntp_mode7 = 0; + peer_ntpdate = 0; + sys_survivors = 0; + sys_ident = NULL; + sys_floor = 0; + sys_bcpollbstep = 0; + sys_ceiling = STRATUM_UNSPEC - 1; + sys_minsane = 1; + sys_minclock = NTP_MINCLOCK; + sys_maxclock = NTP_MAXCLOCK; + sys_cohort = 0; + sys_orphan = STRATUM_UNSPEC + 1; + sys_orphwait = NTP_ORPHWAIT; + sys_beacon = BEACON; + sys_ttlmax = 0; + memset(sys_ttl, 0, sizeof(sys_ttl)); + sys_stattime = 0U; + sys_received = 0U; + sys_processed = 0U; + sys_newversion = 0U; + sys_oldversion = 0U; + sys_restricted = 0U; + sys_badlength = 0U; + sys_badauth = 0U; + sys_declined = 0U; + sys_limitrejected = 0U; + sys_kodsent = 0U; + peer_clear_digest_early = 1; + unpeer_crypto_early = 1; + unpeer_crypto_nak_early = 1; + unpeer_digest_early = 1; + dynamic_interleave = DYNAMIC_INTERLEAVE; + free(endpoint); + endpoint = NULL; + indx = NULL; + endpoint_size = 0; + peers_size = 0; + indx_size = 0; +} +#endif /* __rtems__ */ void set_sys_leap( u_char new_sys_leap @@ -3351,12 +3425,14 @@ clock_select(void) struct peer *typelocal = NULL; struct peer *typepps = NULL; #endif /* REFCLOCK */ +#ifndef __rtems__ static struct endpoint *endpoint = NULL; static int *indx = NULL; static peer_select *peers = NULL; static u_int endpoint_size = 0; static u_int peers_size = 0; static u_int indx_size = 0; +#endif /* __rtems__ */ size_t octets; /* diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_request.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_request.c index 0c8e7b3..3ed2632 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_request.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_request.c @@ -227,6 +227,27 @@ static int usingexbuf; static sockaddr_u *toaddr; static endpt *frominter; +#ifdef __rtems__ +#define RTEMS_NTP_CLEAR(_var) memset(&_var, 0, sizeof(_var)) +void rtems_ntp_request_globals_fini(void); +void rtems_ntp_request_globals_fini(void) { + info_auth_keyid = 0; + numrequests = 0U; + numresppkts = 0U; + RTEMS_NTP_CLEAR(errorcounter); + auth_timereset = 0U; + RTEMS_NTP_CLEAR(rpkt); + reqver = 0; + seqno = 0; + nitems = 0; + itemsize = 0; + databytes = 0; + RTEMS_NTP_CLEAR(exbuf); + usingexbuf = 0; + toaddr = NULL; + frominter = NULL; +} +#endif /* __rtems__ */ /* * init_request - initialize request data */ @@ -2786,4 +2807,3 @@ do_if_reload( flush_pkt(); } - diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_restrict.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_restrict.c index 077d4e3..c7444cd 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_restrict.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_restrict.c @@ -121,6 +121,40 @@ static char * roptoa(restrict_op op); void dump_restricts(void); +#ifdef __rtems__ +#define RTEMS_NTP_CLEAR(_var) memset(&_var, 0, sizeof(_var)) +void rtems_ntp_restrict_globals_fini(void); +void rtems_ntp_restrict_globals_fini(void) { + restrict_u* res; + restrict_u* next; + for (res = restrictlist4; res != NULL; res = next) { + next = res->link; + if (res != &restrict_def4) { + free_res(res, 0); + } + } + for (res = restrictlist6; res != NULL; res = next) { + next = res->link; + if (res != &restrict_def6) { + free_res(res, 1); + } + } + restrictlist4 = NULL; + restrictlist6 = NULL; + restrictcount = 0; + /* resfree4 and resfree6 hold free structs to use, leave them */ + res_calls = 0; + res_found = 0; + res_not_found = 0; + res_limited_refcnt = 0; + RTEMS_NTP_CLEAR(restrict_def4); + RTEMS_NTP_CLEAR(restrict_def6); + restrict_source_enabled = 0; + restrict_source_rflags = 0; + restrict_source_mflags = 0; + restrict_source_ippeerlimit = 0; +} +#endif /* __rtems__ */ /* * dump_restrict - spit out a restrict_u */ diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntp_timer.c b/bsd/freebsd/contrib/ntp/ntpd/ntp_timer.c index 5ff18da..af9d32f 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntp_timer.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntp_timer.c @@ -132,6 +132,28 @@ void set_timer_or_die(const intervaltimer *); #endif +#ifdef __rtems__ +void rtems_ntp_timer_globals_fini(void); +void rtems_ntp_timer_globals_fini(void) { + interface_interval = 0; + initializing = 0; + alarm_flag = 0; + interface_timer = 0U; + adjust_timer = 0U; + stats_timer = 0U; + leapf_timer = 0U; + huffpuff_timer = 0U; + worker_idle_timer = 0U; + leapsec = 0U; + leapdif = 0; + orphwait = 0U; + alarm_overflow = 0U; + current_time = 0U; + timer_timereset = 0U; + timer_overflows = 9U; + timer_xmtcalls = 0U; +} +#endif /* __rtems__ */ #if !defined(SYS_WINNT) && !defined(VMS) void set_timer_or_die( diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntpd.c b/bsd/freebsd/contrib/ntp/ntpd/ntpd.c index c8edaf3..ffaed30 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntpd.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntpd.c @@ -1567,22 +1567,78 @@ int scmp_sc[] = { return 1; } #ifdef __rtems__ +/* + * Manual variable destruction. We only destruct so the variables are + * left ready to run. Any heap memory is freed. + * + * Note, there are some places where the memory is not freed back to + * the heap because there is no clean means to do this. The + * related should be cleared or clean and made available for the + * next run. + */ +extern void rtems_ntp_config_globals_fini(void); +extern void rtems_ntp_control_globals_fini(void); +extern void rtems_ntp_intres_globals_fini(void); +extern void rtems_ntp_io_globals_fini(void); +extern void rtems_ntp_loopfilter_globals_fini(void); +extern void rtems_ntp_monitor_globals_fini(void); +extern void rtems_ntp_peer_globals_fini(void); +extern void rtems_ntp_proto_globals_fini(void); +extern void rtems_ntp_request_globals_fini(void); +extern void rtems_ntp_restrict_globals_fini(void); +extern void rtems_ntp_timer_globals_fini(void); +extern void rtems_ntp_worker_globals_fini(void); +extern void leapsec_ut_pristine(void); + +static void +rtems_ntpd_cleanup(void) { + rtems_ntp_peer_globals_fini(); + rtems_ntp_control_globals_fini(); + rtems_ntp_worker_globals_fini(); + rtems_ntp_intres_globals_fini(); + rtems_ntp_proto_globals_fini(); + rtems_ntp_io_globals_fini(); + rtems_ntp_request_globals_fini(); + rtems_ntp_restrict_globals_fini(); + rtems_ntp_loopfilter_globals_fini(); + rtems_ntp_timer_globals_fini(); + rtems_ntp_monitor_globals_fini(); + rtems_ntp_config_globals_fini(); + leapsec_ut_pristine(); +} + int rtems_ntpd_run(int argc, char **argv) { + int arg; int r; rtems_mutex_lock(&ntpd_lock); if (ntpd_running) { rtems_mutex_unlock(&ntpd_lock); return -1; } + priority_done = 2; + was_alarmed = 0; + listen_to_virtual_ips = TRUE; + signalled = 0; + signo = 0; + cmdline_server_count = 0; + cmdline_servers = NULL; + cur_memlock = -1; rtems_ntpd_log_to_term = 0; syslogit = FALSE; - signalled = 0; + for (arg = 0; arg < argc; ++arg) { + const char* debug_opt = "--set-debug-level="; + const size_t debug_opt_len = strlen(debug_opt); + if (strncmp(debug_opt, argv[arg], debug_opt_len) == 0) { + rtems_ntpd_log_to_term = 1; + } + } ntpd_running = true; rtems_mutex_unlock(&ntpd_lock); r = rtems_bsd_program_call_main("ntpd", ntpdmain, argc, argv); rtems_mutex_lock(&ntpd_lock); + rtems_ntpd_cleanup(); ntpd_running = false; rtems_mutex_unlock(&ntpd_lock); return r; @@ -1634,6 +1690,9 @@ finish_safe( DNSServiceRefDeallocate(mdns); # endif peer_cleanup(); +#ifdef __rtems__ + rtems_ntpd_cleanup(); +#endif /* __rtems__ */ exit(0); } -- cgit v1.2.3