diff options
author | Chris Johns <chrisj@rtems.org> | 2023-06-08 16:46:40 +1000 |
---|---|---|
committer | Chris Johns <chrisj@rtems.org> | 2023-06-08 16:46:40 +1000 |
commit | fe0706060e5687a99c01d155e05d25bc34915ae1 (patch) | |
tree | f3e12120a7182d84e4cf73363cbf26a8d409bb60 | |
parent | bsd/ntp: Wrap the ntpq query in the bsd program wrapper (diff) | |
download | rtems-net-services-fe0706060e5687a99c01d155e05d25bc34915ae1.tar.bz2 |
bsd/ntpq: Add an output buffer and size to the ntpq query
The output buffer and size lets other tasks make queries.
The ntpq lock makes sure only command runs at a time and
providing a user output buffer lets each query complete
without corrupting each other.
-rw-r--r-- | bsd/rtemsbsd/include/rtems/ntpq.h | 15 | ||||
-rw-r--r-- | bsd/rtemsbsd/rtems/rtems-ntpq.c | 41 |
2 files changed, 36 insertions, 20 deletions
diff --git a/bsd/rtemsbsd/include/rtems/ntpq.h b/bsd/rtemsbsd/include/rtems/ntpq.h index 50d797d..ce2321a 100644 --- a/bsd/rtemsbsd/include/rtems/ntpq.h +++ b/bsd/rtemsbsd/include/rtems/ntpq.h @@ -77,24 +77,25 @@ void rtems_ntpq_destroy(void); /** * @brief Query the NTP service * - * Refer to the commands the ntpq command accepts. The output if held - * in the output buffer. This command is not designed to run in - * separate threads. The single output buffer will corrupt. + * Refer to the commands the ntpq command accepts. The output is placed + * in the provided output buffer. * * @param argc Argument count * * @param argv Argument string pointers * + * @param output Buffer to write the output into + * + * @param size Size of the putput buffer + * * @return This function returns the result. */ -int rtems_ntpq_query(const int argc, const char** argv); +int rtems_ntpq_query(const int argc, const char** argv, + char* output, const size_t size); int rtems_ntpq_error_code(void); const char* rtems_ntpq_error_text(void); int rtems_ntpq_create_check(void); -const char* rtems_ntpq_output(void); -FILE* rtems_ntpq_stdout(void); -FILE* rtems_ntpq_stderr(void); #ifdef __cplusplus } diff --git a/bsd/rtemsbsd/rtems/rtems-ntpq.c b/bsd/rtemsbsd/rtems/rtems-ntpq.c index 523aaa3..b8390b3 100644 --- a/bsd/rtemsbsd/rtems/rtems-ntpq.c +++ b/bsd/rtemsbsd/rtems/rtems-ntpq.c @@ -102,13 +102,13 @@ static rtems_recursive_mutex ntpq_lock = RTEMS_RECURSIVE_MUTEX_INITIALIZER("ntpq * safe without a lot of changes and the BSD command support is not * fully ported to net seervices. */ -int rtems_ntpq_error_value; -char rtems_ntpq_error_str[128]; fd_set* rtems_ntpq_fd_set; -size_t rtems_ntpq_fd_set_size; -FILE* rtems_ntpq_outputfp; -char* rtems_ntpq_output_buf; -size_t rtems_ntpq_output_buf_size; + size_t rtems_ntpq_fd_set_size; +static int rtems_ntpq_error_value; +static char rtems_ntpq_error_str[128]; +static FILE* rtems_ntpq_outputfp; +static char* rtems_ntpq_output_buf; +static size_t rtems_ntpq_output_buf_size; /** * SSL support stubs @@ -402,7 +402,8 @@ int rtems_ntpq_query_main(int argc, char** argv) { return 0; } -int rtems_ntpq_query(const int argc, const char** argv) { +int rtems_ntpq_query( + const int argc, const char** argv, char* output, const size_t size) { extern struct xcmd builtins[]; extern struct xcmd opcmds[]; char* prog_main_argv[] = { NULL, NULL, NULL, NULL }; @@ -412,6 +413,7 @@ int rtems_ntpq_query(const int argc, const char** argv) { size_t keyword_len; int args = argc; int arg; + memset(output, 0, size); rtems_recursive_mutex_lock(&ntpq_lock); if (!rtems_ntpq_create_check()) { rtems_recursive_mutex_unlock(&ntpq_lock); @@ -467,11 +469,15 @@ int rtems_ntpq_query(const int argc, const char** argv) { prog_main_argv[2] = (char*) rtems_ntpq_outputfp; (void) rtems_bsd_program_call_main( "ntpq", rtems_ntpq_query_main, 3, prog_main_argv); + memcpy(output, rtems_ntpq_output_buf, min(size, rtems_ntpq_output_buf_size)); + output[size - 1] = '\0'; rtems_recursive_mutex_unlock(&ntpq_lock); return 0; } int rtems_shell_ntpq_command(int argc, char **argv) { + const size_t size = 2048; + char* output = NULL; int r; argc--; argv++; @@ -489,18 +495,27 @@ int rtems_shell_ntpq_command(int argc, char **argv) { printf("ntpq: closed"); r = 0; } else { - r = rtems_ntpq_query(argc, (const char**) argv); + output = malloc(size); + if (output == NULL) { + printf("ntpq: no memory for output\n"); + } else { + r = rtems_ntpq_query(argc, (const char**) argv, output, size); + } } if (r == 0) { - const char* output = rtems_ntpq_output(); - const size_t len = strlen(output); - printf(rtems_ntpq_output()); - if (len > 0 && output[len - 1] != '\n') { - printf("\n"); + if (output != NULL) { + const size_t len = strlen(output); + printf(rtems_ntpq_output()); + if (len > 0 && output[len - 1] != '\n') { + printf("\n"); + } } } else { printf("%s\n", rtems_ntpq_error_text()); } + if (output != NULL) { + free(output); + } return r; } |