@@ -92,6 +92,8 @@ static void server_commit_cb(int fd, int what, void *v);
9292static void tls_handshake_cb (int fd , int what , void * v );
9393#endif
9494
95+ static double random_drop ;
96+
9597/*
9698 * Free a struct connection_closure container and its contents.
9799 */
@@ -717,6 +719,17 @@ handle_iobuf(int iofd, const IoBuffer *iobuf, const uint8_t *buf, size_t len,
717719 sudo_debug_printf (SUDO_DEBUG_INFO , "%s: received IoBuffer from %s" ,
718720 source , __func__ );
719721
722+ /* Random drop is a debugging tool to test client restart. */
723+ if (random_drop > 0.0 ) {
724+ double randval = arc4random () / (double )UINT32_MAX ;
725+ if (randval < random_drop ) {
726+ closure -> errstr = _ ("randomly dropping connection" );
727+ sudo_debug_printf (SUDO_DEBUG_WARN |SUDO_DEBUG_LINENO ,
728+ "randomly dropping connection (%f < %f)" , randval , random_drop );
729+ debug_return_bool (false);
730+ }
731+ }
732+
720733 if (!closure -> cms -> iobuf (iofd , iobuf , buf , len , closure ))
721734 debug_return_bool (false);
722735 if (!enable_commit (closure ))
@@ -1186,8 +1199,11 @@ client_msg_cb(int fd, int what, void *v)
11861199 "%s: parsing ClientMessage, size %u" , __func__ , msg_len );
11871200 buf -> off += sizeof (msg_len );
11881201 if (!handle_client_message (buf -> data + buf -> off , msg_len , closure )) {
1189- sudo_warnx (U_ ("%s: %s" ), source , U_ ("invalid ClientMessage" ));
1190- closure -> errstr = _ ("invalid ClientMessage" );
1202+ /* Use specific error string if one is set. */
1203+ if (closure -> errstr == NULL ) {
1204+ sudo_warnx (U_ ("%s: %s" ), source , U_ ("invalid ClientMessage" ));
1205+ closure -> errstr = _ ("invalid ClientMessage" );
1206+ }
11911207 goto send_error ;
11921208 }
11931209 buf -> off += msg_len ;
@@ -2054,6 +2070,7 @@ main(int argc, char *argv[])
20542070{
20552071 struct sudo_event_base * evbase ;
20562072 bool nofork = false;
2073+ char * ep ;
20572074 int ch ;
20582075 debug_decl_vars (main , SUDO_DEBUG_MAIN );
20592076
@@ -2097,8 +2114,11 @@ main(int argc, char *argv[])
20972114 break ;
20982115 case 'R' :
20992116 /* random connection drop probability as a percentage (debug) */
2100- if (!set_random_drop (optarg ))
2101- sudo_fatalx (U_ ("invalid random drop value: %s" ), optarg );
2117+ errno = 0 ;
2118+ random_drop = strtod (optarg , & ep );
2119+ if (* ep != '\0' || errno != 0 )
2120+ sudo_fatalx (U_ ("invalid random drop value: %s" ), optarg );
2121+ random_drop /= 100.0 ; /* convert from percentage */
21022122 break ;
21032123 case 'V' :
21042124 (void )printf (_ ("%s version %s\n" ), getprogname (),
0 commit comments