Commit 102b40d2 authored by Georgi Chorbadzhiyski's avatar Georgi Chorbadzhiyski

dvblastctl: Show errors as XML if XML output was requested.

XML output worked only for get_xxx and fe_status commands.
With this patch error messages are also available as XML.

Nice side effect is that error handling is now refactored
into return_error() function.
Signed-off-by: default avatarGeorgi Chorbadzhiyski <gf@unixsol.org>
parent f7f9850d
...@@ -54,6 +54,22 @@ int i_syslog = 0; ...@@ -54,6 +54,22 @@ int i_syslog = 0;
print_type_t i_print_type = PRINT_TEXT; print_type_t i_print_type = PRINT_TEXT;
mtime_t now; mtime_t now;
int i_fd = -1;
char psz_client_socket[PATH_MAX] = {0};
static void clean_client_socket() {
if ( i_fd > -1 )
{
close( i_fd );
i_fd = -1;
}
if ( psz_client_socket[0] )
{
unlink( psz_client_socket );
psz_client_socket[0] = '\0';
}
}
/***************************************************************************** /*****************************************************************************
* The following two functinos are from biTStream's examples and are under the * The following two functinos are from biTStream's examples and are under the
* WTFPL (see LICENSE.WTFPL). * WTFPL (see LICENSE.WTFPL).
...@@ -69,6 +85,24 @@ static void psi_print(void *_unused, const char *psz_format, ...) ...@@ -69,6 +85,24 @@ static void psi_print(void *_unused, const char *psz_format, ...)
vprintf(psz_fmt, args); vprintf(psz_fmt, args);
} }
__attribute__ ((format(printf, 1, 2)))
void return_error( const char *psz_format, ... )
{
va_list args;
char psz_fmt[1024];
clean_client_socket();
va_start( args, psz_format );
if ( i_print_type == PRINT_XML )
snprintf( psz_fmt, sizeof(psz_fmt) - 1, "<ERROR msg=\"%s\"/>\n", psz_format );
else
snprintf( psz_fmt, sizeof(psz_fmt) - 1, "ERROR: %s\n", psz_format );
psz_fmt[sizeof(psz_fmt) - 1] = '\0';
vfprintf( stderr, psz_fmt, args );
exit(255);
}
static char *iconv_append_null(const char *p_string, size_t i_length) static char *iconv_append_null(const char *p_string, size_t i_length)
{ {
char *psz_string = malloc(i_length + 1); char *psz_string = malloc(i_length + 1);
...@@ -199,7 +233,6 @@ void usage() ...@@ -199,7 +233,6 @@ void usage()
int main( int i_argc, char **ppsz_argv ) int main( int i_argc, char **ppsz_argv )
{ {
char psz_client_socket[PATH_MAX] = {0};
char *client_socket_tmpl = "dvblastctl.clientsock.XXXXXX"; char *client_socket_tmpl = "dvblastctl.clientsock.XXXXXX";
char *psz_srv_socket = NULL; char *psz_srv_socket = NULL;
int i_fd, i; int i_fd, i;
...@@ -295,15 +328,11 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -295,15 +328,11 @@ int main( int i_argc, char **ppsz_argv )
close(tmp_fd); close(tmp_fd);
unlink(psz_client_socket); unlink(psz_client_socket);
} else { } else {
msg_Err( NULL, "cannot build UNIX socket %s (%s)", psz_client_socket, strerror(errno) ); return_error( "Cannot build UNIX socket %s (%s)", psz_client_socket, strerror(errno) );
return -1;
} }
if ( (i_fd = socket( AF_UNIX, SOCK_DGRAM, 0 )) < 0 ) if ( (i_fd = socket( AF_UNIX, SOCK_DGRAM, 0 )) < 0 )
{ return_error( "Cannot create UNIX socket (%s)", strerror(errno) );
msg_Err( NULL, "cannot create UNIX socket (%s)", strerror(errno) );
return -1;
}
i = COMM_MAX_MSG_CHUNK; i = COMM_MAX_MSG_CHUNK;
setsockopt( i_fd, SOL_SOCKET, SO_RCVBUF, &i, sizeof(i) ); setsockopt( i_fd, SOL_SOCKET, SO_RCVBUF, &i, sizeof(i) );
...@@ -316,11 +345,7 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -316,11 +345,7 @@ int main( int i_argc, char **ppsz_argv )
if ( bind( i_fd, (struct sockaddr *)&sun_client, if ( bind( i_fd, (struct sockaddr *)&sun_client,
SUN_LEN(&sun_client) ) < 0 ) SUN_LEN(&sun_client) ) < 0 )
{ return_error( "Cannot bind (%s)", strerror(errno) );
msg_Err( NULL, "cannot bind (%s)", strerror(errno) );
close( i_fd );
exit(255);
}
memset( &sun_server, 0, sizeof(sun_server) ); memset( &sun_server, 0, sizeof(sun_server) );
sun_server.sun_family = AF_UNIX; sun_server.sun_family = AF_UNIX;
...@@ -385,12 +410,8 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -385,12 +410,8 @@ int main( int i_argc, char **ppsz_argv )
- ((void *)&p_cmd->object - (void *)p_cmd); - ((void *)&p_cmd->object - (void *)p_cmd);
if ( en50221_SerializeMMIObject( (uint8_t *)&p_cmd->object, if ( en50221_SerializeMMIObject( (uint8_t *)&p_cmd->object,
&i_size, &object ) == -1 ) &i_size, &object ) == -1 )
{ return_error( "Comm buffer is too small" );
msg_Err( NULL, "buffer too small" );
close( i_fd );
unlink( psz_client_socket );
exit(255);
}
i_size += COMM_HEADER_SIZE i_size += COMM_HEADER_SIZE
+ ((void *)&p_cmd->object - (void *)p_cmd); + ((void *)&p_cmd->object - (void *)p_cmd);
} }
...@@ -416,12 +437,7 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -416,12 +437,7 @@ int main( int i_argc, char **ppsz_argv )
/* Send command and receive answer */ /* Send command and receive answer */
if ( sendto( i_fd, p_buffer, i_size, 0, (struct sockaddr *)&sun_server, if ( sendto( i_fd, p_buffer, i_size, 0, (struct sockaddr *)&sun_server,
SUN_LEN(&sun_server) ) < 0 ) SUN_LEN(&sun_server) ) < 0 )
{ return_error( "Cannot send comm socket (%s)", strerror(errno) );
msg_Err( NULL, "cannot send comm socket (%s)", strerror(errno) );
close( i_fd );
unlink( psz_client_socket );
exit(255);
}
uint32_t i_packet_size = 0, i_received = 0; uint32_t i_packet_size = 0, i_received = 0;
do { do {
...@@ -438,21 +454,13 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -438,21 +454,13 @@ int main( int i_argc, char **ppsz_argv )
i_received += i_size; i_received += i_size;
} while ( i_received < i_packet_size ); } while ( i_received < i_packet_size );
close( i_fd ); clean_client_socket();
unlink( psz_client_socket );
if ( i_size < COMM_HEADER_SIZE ) if ( i_size < COMM_HEADER_SIZE )
{ return_error( "Cannot recv from comm socket, size:%zd (%s)", i_size, strerror(errno) );
msg_Err( NULL, "cannot recv comm socket (%zd:%s)", i_size,
strerror(errno) );
exit(255);
}
/* Process answer */ /* Process answer */
if ( p_buffer[0] != COMM_HEADER_MAGIC ) if ( p_buffer[0] != COMM_HEADER_MAGIC )
{ return_error( "Wrong protocol version 0x%x", p_buffer[0] );
msg_Err( NULL, "wrong protocol version 0x%x", p_buffer[0] );
exit(255);
}
now = mdate(); now = mdate();
...@@ -460,7 +468,6 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -460,7 +468,6 @@ int main( int i_argc, char **ppsz_argv )
switch ( c_answer ) switch ( c_answer )
{ {
case RET_OK: case RET_OK:
exit(0);
break; break;
case RET_MMI_WAIT: case RET_MMI_WAIT:
...@@ -468,18 +475,15 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -468,18 +475,15 @@ int main( int i_argc, char **ppsz_argv )
break; break;
case RET_ERR: case RET_ERR:
msg_Err( NULL, "request failed" ); return_error( "Request failed" );
exit(255);
break; break;
case RET_HUH: case RET_HUH:
msg_Err( NULL, "internal error" ); return_error( "Internal error" );
exit(255);
break; break;
case RET_NODATA: case RET_NODATA:
msg_Err( NULL, "no data" ); return_error( "No data" );
exit(255);
break; break;
case RET_PAT: case RET_PAT:
...@@ -502,14 +506,12 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -502,14 +506,12 @@ int main( int i_argc, char **ppsz_argv )
psi_table_free( pp_sections ); psi_table_free( pp_sections );
free( pp_sections ); free( pp_sections );
exit(0);
break; break;
} }
case RET_PMT: case RET_PMT:
{ {
pmt_print( p_data, psi_print, NULL, psi_iconv, NULL, i_print_type ); pmt_print( p_data, psi_print, NULL, psi_iconv, NULL, i_print_type );
exit(0);
break; break;
} }
...@@ -518,14 +520,12 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -518,14 +520,12 @@ int main( int i_argc, char **ppsz_argv )
print_pids_header(); print_pids_header();
print_pid( i_pid, (ts_pid_info_t *)p_data ); print_pid( i_pid, (ts_pid_info_t *)p_data );
print_pids_footer(); print_pids_footer();
exit(0);
break; break;
} }
case RET_PIDS: case RET_PIDS:
{ {
print_pids( p_data ); print_pids( p_data );
exit(0);
break; break;
} }
...@@ -535,10 +535,7 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -535,10 +535,7 @@ int main( int i_argc, char **ppsz_argv )
struct ret_frontend_status *p_ret = struct ret_frontend_status *p_ret =
(struct ret_frontend_status *)&p_buffer[COMM_HEADER_SIZE]; (struct ret_frontend_status *)&p_buffer[COMM_HEADER_SIZE];
if ( i_size != COMM_HEADER_SIZE + sizeof(struct ret_frontend_status) ) if ( i_size != COMM_HEADER_SIZE + sizeof(struct ret_frontend_status) )
{ return_error( "Bad frontend status" );
msg_Err( NULL, "bad frontend status" );
exit(255);
}
if ( i_print_type == PRINT_XML ) if ( i_print_type == PRINT_XML )
printf("<FRONTEND>\n"); printf("<FRONTEND>\n");
...@@ -678,10 +675,7 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -678,10 +675,7 @@ int main( int i_argc, char **ppsz_argv )
struct ret_mmi_status *p_ret = struct ret_mmi_status *p_ret =
(struct ret_mmi_status *)&p_buffer[COMM_HEADER_SIZE]; (struct ret_mmi_status *)&p_buffer[COMM_HEADER_SIZE];
if ( i_size != COMM_HEADER_SIZE + sizeof(struct ret_mmi_status) ) if ( i_size != COMM_HEADER_SIZE + sizeof(struct ret_mmi_status) )
{ return_error( "Bad MMI status" );
msg_Err( NULL, "bad MMI status" );
exit(255);
}
printf("CA interface with %d %s, type:\n", p_ret->caps.slot_num, printf("CA interface with %d %s, type:\n", p_ret->caps.slot_num,
p_ret->caps.slot_num == 1 ? "slot" : "slots"); p_ret->caps.slot_num == 1 ? "slot" : "slots");
...@@ -715,10 +709,7 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -715,10 +709,7 @@ int main( int i_argc, char **ppsz_argv )
struct ret_mmi_slot_status *p_ret = struct ret_mmi_slot_status *p_ret =
(struct ret_mmi_slot_status *)&p_buffer[COMM_HEADER_SIZE]; (struct ret_mmi_slot_status *)&p_buffer[COMM_HEADER_SIZE];
if ( i_size < COMM_HEADER_SIZE + sizeof(struct ret_mmi_slot_status) ) if ( i_size < COMM_HEADER_SIZE + sizeof(struct ret_mmi_slot_status) )
{ return_error( "Bad MMI slot status" );
msg_Err( NULL, "bad MMI slot status" );
exit(255);
}
printf("CA slot #%u: ", p_ret->sinfo.num); printf("CA slot #%u: ", p_ret->sinfo.num);
...@@ -751,10 +742,7 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -751,10 +742,7 @@ int main( int i_argc, char **ppsz_argv )
struct ret_mmi_recv *p_ret = struct ret_mmi_recv *p_ret =
(struct ret_mmi_recv *)&p_buffer[COMM_HEADER_SIZE]; (struct ret_mmi_recv *)&p_buffer[COMM_HEADER_SIZE];
if ( i_size < COMM_HEADER_SIZE + sizeof(struct ret_mmi_recv) ) if ( i_size < COMM_HEADER_SIZE + sizeof(struct ret_mmi_recv) )
{ return_error( "Bad MMI recv" );
msg_Err( NULL, "bad MMI recv" );
exit(255);
}
en50221_UnserializeMMIObject( &p_ret->object, i_size en50221_UnserializeMMIObject( &p_ret->object, i_size
- COMM_HEADER_SIZE - ((void *)&p_ret->object - (void *)p_ret) ); - COMM_HEADER_SIZE - ((void *)&p_ret->object - (void *)p_ret) );
...@@ -789,8 +777,7 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -789,8 +777,7 @@ int main( int i_argc, char **ppsz_argv )
break; break;
default: default:
printf("unknown MMI object\n"); return_error( "Unknown MMI object" );
exit(255);
break; break;
} }
...@@ -799,7 +786,8 @@ int main( int i_argc, char **ppsz_argv ) ...@@ -799,7 +786,8 @@ int main( int i_argc, char **ppsz_argv )
} }
default: default:
msg_Err( NULL, "wrong answer %u", c_answer ); return_error( "Unknown command answer: %u", c_answer );
exit(255);
} }
return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment