22
33 Copyright (C) 1999 Russell Kroll <rkroll@exploits.org>
44 Copyright (C) 2012 Arnaud Quette <arnaud.quette@free.fr>
5- Copyright (C) 2020-2024 Jim Klimov <jimklimov+nut@gmail.com>
5+ Copyright (C) 2020-2025 Jim Klimov <jimklimov+nut@gmail.com>
66
77 This program is free software; you can redistribute it and/or modify
88 it under the terms of the GNU General Public License as published by
3131#include "nut_stdint.h"
3232#include "upsclient.h"
3333
34+ /* network timeout for initial connection, in seconds */
35+ #define UPSCLI_DEFAULT_CONNECT_TIMEOUT "10"
36+
3437static char * upsname = NULL , * hostname = NULL ;
3538static UPSCONN_t * ups = NULL ;
3639
@@ -59,6 +62,8 @@ static void usage(const char *prog)
5962
6063 printf ("\nCommon arguments:\n" );
6164 printf (" -V - display the version of this software\n" );
65+ printf (" -W <secs> - network timeout for initial connections (default: %s)\n" ,
66+ UPSCLI_DEFAULT_CONNECT_TIMEOUT );
6267 printf (" -h - display this help text\n" );
6368
6469 nut_report_config_flags ();
@@ -223,6 +228,7 @@ int main(int argc, char **argv)
223228 uint16_t port ;
224229 int varlist = 0 , clientlist = 0 , verbose = 0 ;
225230 const char * prog = xbasename (argv [0 ]);
231+ const char * net_connect_timeout = NULL ;
226232 char * s = NULL ;
227233
228234 /* NOTE: Caller must `export NUT_DEBUG_LEVEL` to see debugs for upsc
@@ -236,7 +242,7 @@ int main(int argc, char **argv)
236242 }
237243 upsdebugx (1 , "Starting NUT client: %s" , prog );
238244
239- while ((i = getopt (argc , argv , "+hlLcV " )) != -1 ) {
245+ while ((i = getopt (argc , argv , "+hlLcVW: " )) != -1 ) {
240246
241247 switch (i )
242248 {
@@ -247,6 +253,7 @@ int main(int argc, char **argv)
247253 fallthrough_case_l :
248254 varlist = 1 ;
249255 break ;
256+
250257 case 'c' :
251258 clientlist = 1 ;
252259 break ;
@@ -258,13 +265,22 @@ int main(int argc, char **argv)
258265 nut_report_config_flags ();
259266 exit (EXIT_SUCCESS );
260267
268+ case 'W' :
269+ net_connect_timeout = optarg ;
270+ break ;
271+
261272 case 'h' :
262273 default :
263274 usage (prog );
264275 exit (EXIT_SUCCESS );
265276 }
266277 }
267278
279+ if (upscli_init_default_connect_timeout (net_connect_timeout , NULL , UPSCLI_DEFAULT_CONNECT_TIMEOUT ) < 0 ) {
280+ fatalx (EXIT_FAILURE , "Error: invalid network timeout: %s" ,
281+ net_connect_timeout );
282+ }
283+
268284 argc -= optind ;
269285 argv += optind ;
270286
0 commit comments