[dvblast-devel] [PATCH 8/9] dvblastctl: Add --timeout/-t option and set default to 15 seconds

Georgi Chorbadzhiyski gf at unixsol.org
Thu Jan 25 01:22:37 CET 2018


-------------- next part --------------
From e75a953b5262b2ea99c244543236571c6d67ba20 Mon Sep 17 00:00:00 2001
From: Georgi Chorbadzhiyski <gf at unixsol.org>
Date: Sat, 13 Jan 2018 19:02:48 +0200
Subject: [PATCH 8/9] dvblastctl: Add --timeout/-t option and set default to 15
 seconds.

It is possible that 'dvblast' does not answer on the command socket
and in this case 'dvblastctl' just hangs waiting forever.

This commit introduces timeout for send/receive operations in dvblastctl.
The timeout is set in seconds and the default value is 15 seconds.
---
 NEWS         |  1 +
 dvblastctl.c | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 3d30ba0..9b175fa 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@ Changes between 3.2 and -next
   * Add support for getting EIT schedule for chosen service in dvblastctl
   * Switch default string charset to UTF-8//IGNORE
   * Add --system-charset/-j option to dvblastctl
+  * Add --timeout/-t option to dvblastctl and set default to 15 seconds
 
 Changes between 3.1 and 3.2:
 ----------------------------
diff --git a/dvblastctl.c b/dvblastctl.c
index cf88027..37d96e8 100644
--- a/dvblastctl.c
+++ b/dvblastctl.c
@@ -51,6 +51,7 @@
 
 int i_verbose = 3;
 int i_syslog = 0;
+unsigned int i_timeout_seconds = 15;
 
 print_type_t i_print_type = PRINT_TEXT;
 mtime_t now;
@@ -358,6 +359,7 @@ void usage()
     printf("Usage: dvblastctl -r <remote socket> [-x <text|xml>] [cmd]\n");
     printf("Options:\n");
     printf("  -r --remote-socket <name>       Set socket name to <name>.\n" );
+    printf("  -t --timeout <seconds>          Set socket read/write timeout in seconds (default 15).\n" );
     printf("  -j --system-charset <name>      Character set used for output (default UTF-8//IGNORE)\n" );
     printf("  -x --print <text|xml>           Choose output format for info commands.\n" );
     printf("Control commands:\n");
@@ -409,13 +411,14 @@ int main( int i_argc, char **ppsz_argv )
         static const struct option long_options[] =
         {
             {"remote-socket", required_argument, NULL, 'r'},
+            {"timeout", required_argument, NULL, 't'},
             {"system-charset", required_argument, NULL, 'j'},
             {"print", required_argument, NULL, 'x'},
             {"help", no_argument, NULL, 'h'},
             {0, 0, 0, 0}
         };
 
-        if ( (c = getopt_long(i_argc, ppsz_argv, "r:x:j:h", long_options, NULL)) == -1 )
+        if ( (c = getopt_long(i_argc, ppsz_argv, "r:t:x:j:h", long_options, NULL)) == -1 )
             break;
 
         switch ( c )
@@ -424,6 +427,10 @@ int main( int i_argc, char **ppsz_argv )
             psz_srv_socket = optarg;
             break;
 
+        case 't':
+            i_timeout_seconds = (unsigned int)strtoul(optarg, NULL, 10);
+            break;
+
         case 'j':
             psz_native_charset = optarg;
             break;
@@ -606,6 +613,18 @@ int main( int i_argc, char **ppsz_argv )
         return_error( "Unhandled option (%d)", opt.cmd );
     }
 
+    if ( i_timeout_seconds > 0 ) {
+        struct timeval tv_timeout = {
+            .tv_sec  = i_timeout_seconds,
+            .tv_usec = 0,
+        };
+        if ( setsockopt( i_fd, SOL_SOCKET, SO_SNDTIMEO, &tv_timeout, sizeof( tv_timeout ) ) != 0 )
+            return_error( "Cannot set SO_SNDTIMEO (%s)", strerror(errno) );
+
+        if ( setsockopt( i_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof( tv_timeout ) ) != 0 )
+            return_error( "Cannot set SO_RCVTIMEO (%s)", strerror(errno) );
+    }
+
     /* Send command and receive answer */
     if ( sendto( i_fd, p_buffer, i_size, 0, (struct sockaddr *)&sun_server,
                  SUN_LEN(&sun_server) ) < 0 )
-- 
2.16.0



More information about the dvblast-devel mailing list