From 20007312ba2b9bf31c6517c07703d185e6a792d0 Mon Sep 17 00:00:00 2001 From: Chris Johns Date: Tue, 16 May 2023 17:27:15 -1000 Subject: bsd/ntp: Add stop and running control to ntpd - Disable syslog and openlog output --- bsd/freebsd/contrib/ntp/libntp/msyslog.c | 8 +++++ bsd/freebsd/contrib/ntp/ntpd/ntpd.c | 61 +++++++++++++++++++++++++++++++- bsd/rtemsbsd/include/rtems/ntpd.h | 23 +++++++++++- testsuites/ntp01/test_main.c | 38 +++++++++++++------- 4 files changed, 116 insertions(+), 14 deletions(-) diff --git a/bsd/freebsd/contrib/ntp/libntp/msyslog.c b/bsd/freebsd/contrib/ntp/libntp/msyslog.c index 4a12672..98eb642 100644 --- a/bsd/freebsd/contrib/ntp/libntp/msyslog.c +++ b/bsd/freebsd/contrib/ntp/libntp/msyslog.c @@ -158,6 +158,9 @@ addto_syslog( const char * nl_or_empty; const char * human_time; +#ifdef __rtems__ + extern int rtems_ntpd_log_to_term; +#endif /* __rtems__ */ /* setup program basename static var prog if needed */ if (progname != prevcall_progname) { prevcall_progname = progname; @@ -178,6 +181,9 @@ addto_syslog( if (syslog_file != NULL) log_to_file = TRUE; else +#ifdef __rtems__ + if (rtems_ntpd_log_to_term) +#endif /* __rtems__ */ log_to_term = TRUE; #if DEBUG if (debug > 0) @@ -419,6 +425,7 @@ init_logging( *cp = '\0'; #endif +#ifndef __rtems__ #if !defined(VMS) if (is_daemon) @@ -441,6 +448,7 @@ init_logging( setlogmask(LOG_UPTO(LOG_DEBUG)); /* @@@ was INFO */ # endif /* LOG_DAEMON */ #endif /* !VMS */ +#endif /* __rtems__ */ } diff --git a/bsd/freebsd/contrib/ntp/ntpd/ntpd.c b/bsd/freebsd/contrib/ntp/ntpd/ntpd.c index b5086d1..c8edaf3 100644 --- a/bsd/freebsd/contrib/ntp/ntpd/ntpd.c +++ b/bsd/freebsd/contrib/ntp/ntpd/ntpd.c @@ -152,6 +152,7 @@ #endif #endif #else /* __rtems__ */ +#include #include #include #endif /* __rtems__ */ @@ -261,6 +262,10 @@ char ** saved_argv; #ifndef SIM #ifdef __rtems__ +static rtems_mutex ntpd_lock = RTEMS_MUTEX_INITIALIZER("ntpd"); +static bool ntpd_running; +int rtems_ntpd_log_to_term; + static #endif /* __rtems__ */ int ntpdmain (int, char **); @@ -1409,8 +1414,19 @@ int scmp_sc[] = { for (;;) { #if !defined(SIM) && defined(SIGDIE1) +#ifdef __rtems__ + rtems_mutex_lock(&ntpd_lock); +#endif /* __rtems__ */ if (signalled) +#ifdef __rtems__ + { + rtems_mutex_unlock(&ntpd_lock); +#endif /* __rtems__ */ finish_safe(signo); +#ifdef __rtems__ + } + rtems_mutex_unlock(&ntpd_lock); +#endif /* __rtems__ */ #endif GetReceivedBuffers(); # else /* normal I/O */ @@ -1420,8 +1436,19 @@ int scmp_sc[] = { for (;;) { #if !defined(SIM) && defined(SIGDIE1) +#ifdef __rtems__ + rtems_mutex_lock(&ntpd_lock); +#endif /* __rtems__ */ if (signalled) +#ifdef __rtems__ + { + rtems_mutex_unlock(&ntpd_lock); +#endif /* __rtems__ */ finish_safe(signo); +#ifdef __rtems__ + } + rtems_mutex_unlock(&ntpd_lock); +#endif /* __rtems__ */ #endif if (alarm_flag) { /* alarmed? */ was_alarmed = TRUE; @@ -1543,8 +1570,40 @@ int scmp_sc[] = { int rtems_ntpd_run(int argc, char **argv) { + int r; + rtems_mutex_lock(&ntpd_lock); + if (ntpd_running) { + rtems_mutex_unlock(&ntpd_lock); + return -1; + } + rtems_ntpd_log_to_term = 0; + syslogit = FALSE; + signalled = 0; + ntpd_running = true; + rtems_mutex_unlock(&ntpd_lock); + r = rtems_bsd_program_call_main("ntpd", ntpdmain, argc, argv); + rtems_mutex_lock(&ntpd_lock); + ntpd_running = false; + rtems_mutex_unlock(&ntpd_lock); + return r; +} - return (rtems_bsd_program_call_main("ntpd", ntpdmain, argc, argv)); +void +rtems_ntpd_stop(void) +{ + rtems_mutex_lock(&ntpd_lock); + signalled = 1; + rtems_mutex_unlock(&ntpd_lock); +} + +int +rtems_ntpd_running(void) +{ + int r; + rtems_mutex_lock(&ntpd_lock); + r = ntpd_running ? 1 : 0; + rtems_mutex_unlock(&ntpd_lock); + return r; } #endif /* __rtems__ */ #endif /* !SIM */ diff --git a/bsd/rtemsbsd/include/rtems/ntpd.h b/bsd/rtemsbsd/include/rtems/ntpd.h index 00af1ea..096f87e 100644 --- a/bsd/rtemsbsd/include/rtems/ntpd.h +++ b/bsd/rtemsbsd/include/rtems/ntpd.h @@ -50,11 +50,32 @@ extern "C" { * * @param argv is the vector of arguments. * - * @return This function should never return. If it returns, then there is a + * @return This function only returns if @ref rtems_ntpd_stop is + * called or the daemon is already running Any other reason is a * serious error. */ int rtems_ntpd_run(int argc, char **argv); +/** + * @brief Stops the NTP daemon (nptd). + * + * The ntpd loop will exit when it next runs cleaning up. Use the + * @ref rtems_ntpd_running call to check if the daemon has stopped + * running. + * + * @return This function should never return. If it returns, then there is a + * serious error. + */ +void rtems_ntpd_stop(void); + +/** + * @brief Checks if the NTP daemon (nptd) is running? + * + * @return Return 1 if ntpd is running else 0 is returned. + */ +int rtems_ntpd_running(void); + + #ifdef __cplusplus } #endif diff --git a/testsuites/ntp01/test_main.c b/testsuites/ntp01/test_main.c index cb28554..8a3ebf5 100644 --- a/testsuites/ntp01/test_main.c +++ b/testsuites/ntp01/test_main.c @@ -348,7 +348,7 @@ static const char etc_services[] = "ntp 123/tcp # Network Time Protocol [Dave_Mills] [RFC5905]\n" "ntp 123/udp # Network Time Protocol [Dave_Mills] [RFC5905]\n"; -static bool ntp_finished; +static int ntp_run_count; static rtems_id ntpd_id; static void setup_etc(void) @@ -379,18 +379,22 @@ static rtems_task ntpd_runner( rtems_task_argument argument ) { - char *argv[] = { - "ntpd", - "-g", + while (ntp_run_count++ < 2) { + char *argv[] = { + "ntpd", + "-g", #if NTP_DEBUG - "--set-debug-level=" NTP_DEBUG_STR, + "--set-debug-level=" NTP_DEBUG_STR, #endif - NULL - }; - const int argc = ((sizeof(argv) / sizeof(argv[0])) - 1); - - (void)rtems_ntpd_run(argc, argv); - ntp_finished = true; + NULL + }; + const int argc = ((sizeof(argv) / sizeof(argv[0])) - 1); + int r; + + printf("ntpd starting\n"); + r = rtems_ntpd_run(argc, argv); + printf("ntpd finished: %d\n", r); + } } static void run_test(void) @@ -402,6 +406,7 @@ static void run_test(void) NULL }; const int argc = ((sizeof(argv) / sizeof(argv[0])) - 1); + int restart_secs = 0; setup_etc(); @@ -427,9 +432,18 @@ static void run_test(void) sc = rtems_task_start( ntpd_id, ntpd_runner, 0 ); directive_failed( sc, "rtems_task_start of TA1" ); - while (!ntp_finished) { + sleep(1); + + while (rtems_ntpd_running()) { sleep(2); + restart_secs += 2; + if (restart_secs == 10) { + printf("ntpd forced stop\n"); + rtems_ntpd_stop(); + sleep(2); + } } + printf("ntpd: not running!\n"); } static rtems_task Init( rtems_task_argument argument ) -- cgit v1.2.3