From: Dave Hart Date: Wed, 30 Dec 2009 21:55:52 +0000 (+0000) Subject: [Bug 702] ntpd service logic should use libopts to examine cmdline. X-Git-Tag: NTP_4_2_7P8~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d51741ec897faef14ba274ab7c91bdba9ebe0ccf;p=thirdparty%2Fntp.git [Bug 702] ntpd service logic should use libopts to examine cmdline. bk: 4b3bcc68Y2usiso76vdzIQqosrspBQ --- diff --git a/ChangeLog b/ChangeLog index 253776737..a8f0e0918 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ +* [Bug 702] ntpd service logic should use libopts to examine cmdline. (4.2.7p7) 2009/12/30 Released by Harlan Stenn * [Bug 620] ntpdc getresponse() esize != *rsize s/b size != *rsize. * [Bug 1446] 4.2.7p6 requires autogen, missing ntpd.1, *.texi, *.menu. diff --git a/include/ntp_cmdargs.h b/include/ntp_cmdargs.h index ee41b4a56..de45d8d5e 100644 --- a/include/ntp_cmdargs.h +++ b/include/ntp_cmdargs.h @@ -1,4 +1 @@ -#include "ntp_types.h" - -extern void getstartup (int, char **); -extern void getCmdOpts (int, char **); +extern void getCmdOpts(int, char **); diff --git a/include/ntpd.h b/include/ntpd.h index 14cf0add2..10fb7d057 100644 --- a/include/ntpd.h +++ b/include/ntpd.h @@ -475,6 +475,7 @@ extern int stats_write_period; /* # of seconds between writes. */ extern double stats_write_tolerance; /* ntpd.c */ +extern void parse_cmdline_opts(int *, char ***); extern volatile int debug; /* debugging flag */ extern int nofork; /* no-fork flag */ extern int initializing; /* initializing flag */ diff --git a/ntpd/cmd_args.c b/ntpd/cmd_args.c index 49a9b4652..98236f944 100644 --- a/ntpd/cmd_args.c +++ b/ntpd/cmd_args.c @@ -23,12 +23,15 @@ extern int check_netinfo; /* - * getCmdOpts - get command line options + * getCmdOpts - apply most command line options + * + * A few options are examined earlier in ntpd.c ntpdmain() and + * ports/winnt/ntpd/ntservice.c main(). */ void getCmdOpts( - int argc, - char *argv[] + int argc, + char ** argv ) { extern const char *config_file; diff --git a/ntpd/ntp_config.c b/ntpd/ntp_config.c index 9426e0a0f..aedffd86c 100644 --- a/ntpd/ntp_config.c +++ b/ntpd/ntp_config.c @@ -252,7 +252,7 @@ void destroy_restrict_node(struct restrict_node *my_node); static int is_sane_resolved_address(sockaddr_u *peeraddr, int hmode); static int get_correct_host_mode(int hmode); static void save_and_apply_config_tree(void); -void getconfig(int argc,char *argv[]); +void getconfig(int, char **); #if !defined(SIM) static sockaddr_u *get_next_address(struct address_node *addr); #endif @@ -3761,8 +3761,8 @@ config_remotely( */ void getconfig( - int argc, - char *argv[] + int argc, + char ** argv ) { char line[MAXLINE]; diff --git a/ntpd/ntpd.c b/ntpd/ntpd.c index 62e4f9805..040a48a0d 100644 --- a/ntpd/ntpd.c +++ b/ntpd/ntpd.c @@ -222,7 +222,6 @@ int ntpdmain (int, char **); static void set_process_priority (void); void init_logging (char const *, int); void setup_logfile (void); -static void process_commandline_opts(int *, char ***); static void assertion_failed (const char *file, int line, isc_assertiontype_t type, const char *cond); @@ -316,15 +315,20 @@ setup_logfile( } -static void -process_commandline_opts( - int *pargc, +void +parse_cmdline_opts( + int * pargc, char ***pargv ) { - int optct; + static int parsed; + static int optct; + + if (!parsed) + optct = optionProcess(&ntpdOptions, *pargc, *pargv); + + parsed = 1; - optct = optionProcess(&ntpdOptions, *pargc, *pargv); *pargc -= optct; *pargv += optct; } @@ -337,7 +341,7 @@ main( char *argv[] ) { - process_commandline_opts(&argc, &argv); + parse_cmdline_opts(&argc, &argv); return ntpsim(argc, argv); } @@ -496,7 +500,7 @@ ntpdmain( progname = argv[0]; initializing = 1; /* mark that we are initializing */ - process_commandline_opts(&argc, &argv); + parse_cmdline_opts(&argc, &argv); init_logging(progname, 1); /* Open the log file */ #ifdef HAVE_UMASK @@ -524,8 +528,6 @@ ntpdmain( } #endif - /* getstartup(argc, argv); / * startup configuration, may set debug */ - #ifdef DEBUG debug = DESC(DEBUG_LEVEL).optOccCt; DPRINTF(1, ("%s\n", Version)); diff --git a/ports/winnt/include/ntservice.h b/ports/winnt/include/ntservice.h index c5e5f42c3..f44335fdb 100644 --- a/ports/winnt/include/ntservice.h +++ b/ports/winnt/include/ntservice.h @@ -15,22 +15,17 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: ntservice.h,v 1.2 2002/08/03 01:31:48 mayer Exp $ */ - #ifndef NTSERVICE_H #define NTSERVICE_H -#include - #define NTP_DISPLAY_NAME "NetworkTimeProtocol" #define NTP_SERVICE_NAME "ntpd" -void ntservice_init(); +void ntservice_init(void); void UpdateSCM(DWORD); void WINAPI ServiceControl(DWORD dwCtrlCode); -void ntservice_shutdown(); -BOOL ntservice_isservice(); -BOOL ntservice_systemisshuttingdown(); +void ntservice_shutdown(void); +BOOL ntservice_systemisshuttingdown(void); BOOL WINAPI OnConsoleEvent(DWORD dwCtrlType); -#endif \ No newline at end of file +#endif /* NTSERVICE_H */ diff --git a/ports/winnt/ntpd/ntservice.c b/ports/winnt/ntpd/ntservice.c index cf184381d..906e88253 100644 --- a/ports/winnt/ntpd/ntservice.c +++ b/ports/winnt/ntpd/ntservice.c @@ -15,20 +15,19 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: ntservice.c,v 1.3.2.1.10.3 2004/03/08 04:04:22 marka Exp $ */ - #ifdef HAVE_CONFIG_H # include #endif #include -#include #include #include "syslog.h" +#include "ntpd.h" #include "ntservice.h" #include "clockstuff.h" #include "ntp_iocompletionport.h" +#include "ntpd-opts.h" #include "isc/win32os.h" #include @@ -59,10 +58,13 @@ void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l); void wrap_dbg_free(void *p); #endif -void WINAPI service_main( DWORD argc, LPTSTR *argv ) +void WINAPI +service_main( + DWORD argc, + LPTSTR *argv + ) { - if ( argc > 1 ) - { + if (argc > 1) { /* * Let command line parameters from the Windows SCM GUI * override the standard parameters from the ImagePath registry key. @@ -71,7 +73,7 @@ void WINAPI service_main( DWORD argc, LPTSTR *argv ) glb_argv = argv; } - ntpdmain( glb_argc, glb_argv ); + ntpdmain(glb_argc, glb_argv); } @@ -80,11 +82,14 @@ void WINAPI service_main( DWORD argc, LPTSTR *argv ) * We can call ntpdmain() explicitly or via StartServiceCtrlDispatcher() * as we need to. */ -int main( int argc, char *argv[] ) +int main( + int argc, + char ** argv + ) { - int rc; - int i = 1; - + int rc; + int argc_after_opts; + char ** argv_after_opts; ssl_applink(); @@ -94,33 +99,21 @@ int main( int argc, char *argv[] ) /* Under original Windows NT we must not discard the wildcard */ /* socket to workaround a bug in NT's getsockname(). */ - if ( isc_win32os_majorversion() <= 4 ) + if (isc_win32os_majorversion() <= 4) accept_wildcard_if_for_winnt = 1; - /* - * This is a hack in the Windows port of ntpd. Before the - * portable ntpd libopts processing of the command line, we - * need to know if we're "daemonizing" (attempting to start as - * a service). There is undoubtedly a better way. Legitimate - * option combinations are broken by this code , such as: - * ntpd -nc debug.conf - */ - while (argv[i]) { - if (!_strnicmp(argv[i], "-d", 2) - || !strcmp(argv[i], "--debug_level") - || !strcmp(argv[i], "--set-debug_level") - || !strcmp(argv[i], "-q") - || !strcmp(argv[i], "--quit") - || !strcmp(argv[i], "-?") - || !strcmp(argv[i], "--help") - || !_strnicmp(argv[i], "-n", 2) - || !strcmp(argv[i], "--nofork") - || !strcmp(argv[i], "--saveconfigquit")) { - foreground = TRUE; - break; - } - i++; - } + argc_after_opts = argc; + argv_after_opts = argv; + parse_cmdline_opts(&argc_after_opts, &argv_after_opts); + + if (HAVE_OPT(QUIT) + || HAVE_OPT(SAVECONFIGQUIT) + || HAVE_OPT(HELP) +#ifdef DEBUG + || DESC(DEBUG_LEVEL).optOccCt != 0 +#endif + || HAVE_OPT(NOFORK)) + foreground = TRUE; if (foreground) /* run in console window */ rc = ntpdmain(argc, argv); @@ -137,15 +130,19 @@ int main( int argc, char *argv[] ) rc = 0; else { rc = GetLastError(); -#ifdef DEBUG - fprintf(stderr, "%s: unable to start as service, rc: %i\n\n", argv[0], rc); -#endif - fprintf(stderr, "\nUse -d, -q, --help or -n to run from the command line.\n"); + fprintf(stderr, + "%s: unable to start as service:\n" + "%s\n" + "Use -d, -q, -n, -?, --help or " + "--saveconfigquit to run " + "interactive.\n", + argv[0], ntp_strerror(rc)); } } return rc; } + /* * Initialize the Service by registering it. */ @@ -164,8 +161,9 @@ ntservice_init() { } UpdateSCM(SERVICE_RUNNING); } else { - strcpy(ConsoleTitle, "NTP Version "); - strcat(ConsoleTitle, Version); + snprintf(ConsoleTitle, sizeof(ConsoleTitle), + "NTP Version %s", Version); + ConsoleTitle[sizeof(ConsoleTitle) - 1] = '\0'; SetConsoleTitle(ConsoleTitle); } @@ -194,13 +192,6 @@ ntservice_init() { atexit( ntservice_exit ); } -/* - * Routine to check if this is a service or a foreground program - */ -BOOL -ntservice_isservice() { - return(!foreground); -} /* * Routine to check if the service is stopping diff --git a/ports/winnt/vc6/ntpd.dsp b/ports/winnt/vc6/ntpd.dsp index 032873278..57a5520af 100644 --- a/ports/winnt/vc6/ntpd.dsp +++ b/ports/winnt/vc6/ntpd.dsp @@ -42,7 +42,7 @@ RSC=rc.exe # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c -# ADD CPP /nologo /MD /W4 /GX /O2 /I "." /I "..\include" /I "..\..\..\include" /I "..\..\..\lib\isc\win32\include" /I "..\..\..\lib\isc\include" /I "$(OPENSSL_INC)" /I "..\..\..\libopts" /D "NDEBUG" /D "_CONSOLE" /D "WIN32" /D "_MBCS" /D "__STDC__" /D "SYS_WINNT" /D "HAVE_CONFIG_H" /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1 /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1 /D "_CRT_SECURE_NO_DEPRECATE" /FR /YX"windows.h" /FD /c +# ADD CPP /nologo /MD /W4 /GX /O2 /I "." /I "..\..\..\ntpd" /I "..\include" /I "..\..\..\include" /I "..\..\..\lib\isc\win32\include" /I "..\..\..\lib\isc\include" /I "$(OPENSSL_INC)" /I "..\..\..\libopts" /D "NDEBUG" /D "_CONSOLE" /D "WIN32" /D "_MBCS" /D "__STDC__" /D "SYS_WINNT" /D "HAVE_CONFIG_H" /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1 /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1 /D "_CRT_SECURE_NO_DEPRECATE" /FR /YX"windows.h" /FD /c # ADD BASE RSC /l 0x409 /d "NDEBUG" # ADD RSC /l 0x409 /d "NDEBUG" BSC32=bscmake.exe @@ -66,7 +66,7 @@ LINK32=link.exe # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c -# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "." /I "..\include" /I "..\..\..\include" /I "..\..\..\lib\isc\win32\include" /I "..\..\..\lib\isc\include" /I "$(OPENSSL_INC)" /I "..\..\..\libopts" /D "_DEBUG" /D "_CONSOLE" /D "WIN32" /D "_MBCS" /D "__STDC__" /D "SYS_WINNT" /D "HAVE_CONFIG_H" /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1 /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1 /D "_CRT_SECURE_NO_DEPRECATE" /FR /FD /GZ /c +# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "." /I "..\..\..\ntpd" /I "..\include" /I "..\..\..\include" /I "..\..\..\lib\isc\win32\include" /I "..\..\..\lib\isc\include" /I "$(OPENSSL_INC)" /I "..\..\..\libopts" /D "_DEBUG" /D "_CONSOLE" /D "WIN32" /D "_MBCS" /D "__STDC__" /D "SYS_WINNT" /D "HAVE_CONFIG_H" /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1 /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1 /D "_CRT_SECURE_NO_DEPRECATE" /FR /FD /GZ /c # SUBTRACT CPP /YX # ADD BASE RSC /l 0x409 /d "_DEBUG" # ADD RSC /l 0x409 /d "_DEBUG" diff --git a/ports/winnt/vs2003/ntpd.vcproj b/ports/winnt/vs2003/ntpd.vcproj index 5ca9f61dd..975297aa3 100644 --- a/ports/winnt/vs2003/ntpd.vcproj +++ b/ports/winnt/vs2003/ntpd.vcproj @@ -22,7 +22,7 @@ Name="VCCLCompilerTool" Optimization="2" InlineFunctionExpansion="1" - AdditionalIncludeDirectories=".,..\include,..\..\..\include,..\..\..\lib\isc\win32\include,..\..\..\lib\isc\include,$(OPENSSL_INC),..\..\..\libopts" + AdditionalIncludeDirectories=".,..\..\..\ntpd,..\include,..\..\..\include,..\..\..\lib\isc\win32\include,..\..\..\lib\isc\include,$(OPENSSL_INC),..\..\..\libopts" PreprocessorDefinitions="NDEBUG;_CONSOLE;WIN32;__STDC__;SYS_WINNT;HAVE_CONFIG_H;_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1;_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1;_CRT_SECURE_NO_DEPRECATE" StringPooling="TRUE" RuntimeLibrary="2" @@ -84,7 +84,7 @@