@ -1,4 +1,3 @@
# include "status.h"
# include "utils.h"
# include "wire/wire.h"
# include "wire/wire_sync.h"
@ -8,18 +7,32 @@
# include <ccan/err/err.h>
# include <ccan/fdpass/fdpass.h>
# include <ccan/read_write_all/read_write_all.h>
# include <ccan/take/take.h>
# include <ccan/tal/str/str.h>
# include <lightningd/connection.h>
# include <lightningd/status.h>
# include <stdarg.h>
static int status_fd = - 1 ;
static struct daemon_conn * status_conn ;
const void * trc ;
void status_setup ( int fd )
void status_setup_sync ( int fd )
{
assert ( status_fd = = - 1 ) ;
assert ( ! status_conn ) ;
status_fd = fd ;
trc = tal_tmpctx ( NULL ) ;
}
void status_setup_async ( struct daemon_conn * master )
{
assert ( status_fd = = - 1 ) ;
assert ( ! status_conn ) ;
status_conn = master ;
trc = tal_tmpctx ( NULL ) ;
}
static bool too_large ( size_t len , int type )
{
if ( len > 65535 ) {
@ -30,7 +43,7 @@ static bool too_large(size_t len, int type)
return false ;
}
void status_send ( const u8 * p )
void status_send_sync ( const u8 * p )
{
const u8 * msg = p ;
assert ( status_fd > = 0 ) ;
@ -43,16 +56,6 @@ void status_send(const u8 *p)
tal_free ( p ) ;
}
void status_send_fd ( int fd )
{
assert ( status_fd > = 0 ) ;
assert ( fd > = 0 ) ;
if ( ! fdpass_send ( status_fd , fd ) )
err ( 1 , " Writing out status fd %i " , fd ) ;
close ( fd ) ;
}
static void status_send_with_hdr ( u16 type , const void * p , size_t len )
{
be16 be_type , be_len ;
@ -62,13 +65,19 @@ static void status_send_with_hdr(u16 type, const void *p, size_t len)
be_type = cpu_to_be16 ( type ) ;
be_len = cpu_to_be16 ( len + sizeof ( be_type ) ) ;
assert ( status_fd > = 0 ) ;
assert ( be16_to_cpu ( be_len ) = = len + sizeof ( be_type ) ) ;
if ( ! write_all ( status_fd , & be_len , sizeof ( be_len ) )
| | ! write_all ( status_fd , & be_type , sizeof ( be_type ) )
| | ! write_all ( status_fd , p , len ) )
err ( 1 , " Writing out status %u len %zu " , type , len ) ;
if ( status_fd > = 0 ) {
if ( ! write_all ( status_fd , & be_len , sizeof ( be_len ) )
| | ! write_all ( status_fd , & be_type , sizeof ( be_type ) )
| | ! write_all ( status_fd , p , len ) )
err ( 1 , " Writing out status %u len %zu " , type , len ) ;
} else {
u8 * msg = tal_arr ( NULL , u8 , sizeof ( be_type ) + len ) ;
memcpy ( msg , & be_type , sizeof ( be_type ) ) ;
memcpy ( msg + sizeof ( be_type ) , p , len ) ;
daemon_conn_send ( status_conn , take ( msg ) ) ;
}
}
void status_trace ( const char * fmt , . . . )
@ -99,5 +108,10 @@ void status_failed(u16 code, const char *fmt, ...)
status_send_with_hdr ( code , str , strlen ( str ) ) ;
va_end ( ap ) ;
/* Don't let it take forever. */
alarm ( 10 ) ;
if ( status_conn )
daemon_conn_sync_flush ( status_conn ) ;
exit ( 0x80 | ( code & 0xFF ) ) ;
}