LCOV - code coverage report
Current view: top level - app/shared_dev/commands - dev.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 167 0.0 %
Date: 2025-11-20 04:31:33 Functions: 0 7 0.0 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : 
       3             : #include "../../platform/fd_sys_util.h"
       4             : #include "../../shared/commands/configure/configure.h"
       5             : #include "../../shared/commands/run/run.h"
       6             : #include "../../shared/commands/watch/watch.h"
       7             : #include "../../../discof/genesis/genesis_hash.h"
       8             : #include "../../../disco/net/fd_net_tile.h"
       9             : 
      10             : #include <stdio.h>
      11             : #include <stdlib.h> /* setenv */
      12             : #include <unistd.h>
      13             : #include <sched.h>
      14             : #include <fcntl.h>
      15             : #include <pthread.h>
      16             : #include <sys/wait.h>
      17             : 
      18             : fd_topo_run_tile_t
      19             : fdctl_tile_run( fd_topo_tile_t const * tile );
      20             : 
      21             : void
      22             : dev_cmd_args( int *    pargc,
      23             :               char *** pargv,
      24           0 :               args_t * args ) {
      25           0 :   args->dev.parent_pipefd = -1;
      26           0 :   args->dev.no_watch = fd_env_strip_cmdline_contains( pargc, pargv, "--no-watch" );
      27           0 :   args->dev.no_configure = fd_env_strip_cmdline_contains( pargc, pargv, "--no-configure" );
      28           0 :   args->dev.no_init_workspaces = fd_env_strip_cmdline_contains( pargc, pargv, "--no-init-workspaces" );
      29           0 :   args->dev.no_agave = fd_env_strip_cmdline_contains( pargc, pargv, "--no-agave"  ) ||
      30           0 :                        fd_env_strip_cmdline_contains( pargc, pargv, "--no-solana" );
      31           0 :   const char * debug_tile = fd_env_strip_cmdline_cstr( pargc, pargv, "--debug-tile", NULL, NULL );
      32           0 :   if( FD_UNLIKELY( debug_tile ) )
      33           0 :     strncpy( args->dev.debug_tile, debug_tile, sizeof( args->dev.debug_tile ) - 1 );
      34           0 : }
      35             : 
      36             : void
      37             : dev_cmd_perm( args_t *         args,
      38             :               fd_cap_chk_t *   chk,
      39           0 :               config_t const * config ) {
      40           0 :   if( FD_LIKELY( !args->dev.no_configure ) ) {
      41           0 :     args_t configure_args = {
      42           0 :       .configure.command = CONFIGURE_CMD_INIT,
      43           0 :     };
      44           0 :     for( ulong i=0UL; STAGES[ i ]; i++ )
      45           0 :       configure_args.configure.stages[ i ] = STAGES[ i ];
      46           0 :     configure_cmd_perm( &configure_args, chk, config );
      47           0 :   }
      48             : 
      49           0 :   run_cmd_perm( NULL, chk, config );
      50           0 : }
      51             : 
      52             : pid_t firedancer_pid, watch_pid;
      53             : extern char fd_log_private_path[ 1024 ]; /* empty string on start */
      54             : 
      55           0 : #define FD_LOG_ERR_NOEXIT(a) do { long _fd_log_msg_now = fd_log_wallclock(); fd_log_private_1( 4, _fd_log_msg_now, __FILE__, __LINE__, __func__, fd_log_private_0 a ); } while(0)
      56             : 
      57             : extern int fd_log_private_restore_stderr;
      58             : extern int * fd_log_private_shared_lock;
      59             : 
      60             : static void
      61           0 : parent_signal( int sig ) {
      62           0 :   if( FD_LIKELY( firedancer_pid ) ) kill( firedancer_pid, SIGINT );
      63           0 :   if( FD_LIKELY( watch_pid ) )      kill( watch_pid, SIGKILL );
      64             : 
      65             :   /* Same hack as in run.c, see comments there. */
      66           0 :   int lock = 0;
      67           0 :   fd_log_private_shared_lock = &lock;
      68             : 
      69           0 :   if( FD_LIKELY( -1!=fd_log_private_restore_stderr ) ) {
      70           0 :     if( FD_UNLIKELY( -1==dup2( fd_log_private_restore_stderr, STDERR_FILENO ) ) ) FD_LOG_STDOUT(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      71           0 :   }
      72             : 
      73           0 :   if( -1!=fd_log_private_logfile_fd() ) FD_LOG_ERR_NOEXIT(( "Received signal %s\nLog at \"%s\"", fd_io_strsignal( sig ), fd_log_private_path ));
      74           0 :   else                                  FD_LOG_ERR_NOEXIT(( "Received signal %s",                fd_io_strsignal( sig ) ));
      75             : 
      76           0 :   if( FD_LIKELY( sig==SIGINT ) ) fd_sys_util_exit_group( 128+SIGINT );
      77           0 :   else                           fd_sys_util_exit_group( 0          );
      78           0 : }
      79             : 
      80             : static void
      81           0 : install_parent_signals( void ) {
      82           0 :   struct sigaction sa = {
      83           0 :     .sa_handler = parent_signal,
      84           0 :     .sa_flags   = 0,
      85           0 :   };
      86           0 :   if( FD_UNLIKELY( sigaction( SIGTERM, &sa, NULL ) ) )
      87           0 :     FD_LOG_ERR(( "sigaction(SIGTERM) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      88           0 :   if( FD_UNLIKELY( sigaction( SIGINT, &sa, NULL ) ) )
      89           0 :     FD_LOG_ERR(( "sigaction(SIGINT) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      90           0 : }
      91             : 
      92             : void
      93           0 : update_config_for_dev( fd_config_t * config ) {
      94             :   /* Automatically compute the shred version from genesis if it
      95             :      exists and we don't know it.  If it doesn't exist, we'll keep it
      96             :      set to zero and get from gossip. */
      97           0 :   char genesis_path[ PATH_MAX ];
      98           0 :   if( FD_LIKELY( config->is_firedancer ) ) fd_memcpy( genesis_path, config->paths.genesis, PATH_MAX );
      99           0 :   else FD_TEST( fd_cstr_printf_check( genesis_path, PATH_MAX, NULL, "%s/genesis.bin", config->frankendancer.paths.ledger ) );
     100             : 
     101           0 :   ushort shred_version = 0;
     102           0 :   int result = compute_shred_version( genesis_path, &shred_version, NULL );
     103           0 :   if( FD_UNLIKELY( -1==result && errno!=ENOENT ) ) FD_LOG_ERR(( "could not compute shred version from genesis file `%s` (%i-%s)", genesis_path, errno, fd_io_strerror( errno ) ));
     104             : 
     105           0 :   for( ulong i=0UL; i<config->layout.shred_tile_count; i++ ) {
     106           0 :     ulong shred_id = fd_topo_find_tile( &config->topo, "shred", i );
     107           0 :     if( FD_UNLIKELY( shred_id==ULONG_MAX ) ) FD_LOG_ERR(( "could not find shred tile %lu", i ));
     108           0 :     fd_topo_tile_t * shred = &config->topo.tiles[ shred_id ];
     109           0 :     if( FD_LIKELY( shred->shred.expected_shred_version==(ushort)0 ) ) {
     110           0 :       shred->shred.expected_shred_version = shred_version;
     111           0 :     }
     112           0 :   }
     113           0 : }
     114             : 
     115             : /* Run Firedancer entirely in a single process for development and
     116             :    debugging convenience. */
     117             : 
     118             : static void
     119             : run_firedancer_threaded( config_t * config,
     120             :                          int        init_workspaces,
     121           0 :                          void ( * agave_main )( config_t const * ) ) {
     122           0 :   install_parent_signals();
     123             : 
     124           0 :   fd_topo_print_log( 0, &config->topo );
     125             : 
     126           0 :   run_firedancer_init( config, init_workspaces, 1 );
     127           0 :   fdctl_setup_netns( config, 1 );
     128             : 
     129           0 :   if( FD_UNLIKELY( config->development.debug_tile ) ) {
     130           0 :     fd_log_private_shared_lock[ 1 ] = 1;
     131           0 :   }
     132             : 
     133             :   /* This is kind of a hack, but we have to join all the workspaces as read-write
     134             :      if we are running things threaded.  The reason is that if one of the earlier
     135             :      tiles maps it in as read-only, later tiles will reuse the same cached shmem
     136             :      join (the key is only on shmem name, when it should be (name, mode)). */
     137             : 
     138           0 :   if( 0==strcmp( config->net.provider, "xdp" ) ) {
     139           0 :     fd_topo_install_xdp_simple( &config->topo, config->net.bind_address_parsed );
     140           0 :   }
     141             : 
     142           0 :   fd_topo_join_workspaces( &config->topo, FD_SHMEM_JOIN_MODE_READ_WRITE );
     143           0 :   fd_topo_run_single_process( &config->topo, 2, config->uid, config->gid, fdctl_tile_run );
     144             : 
     145           0 :   if( FD_UNLIKELY( agave_main && !config->development.no_agave ) ) {
     146           0 :     agave_main( config );
     147           0 :   }
     148           0 : }
     149             : 
     150             : void
     151             : dev_cmd_fn( args_t *   args,
     152             :             config_t * config,
     153           0 :             void ( * agave_main )( config_t const * ) ) {
     154           0 :   if( FD_LIKELY( !args->dev.no_configure ) ) {
     155           0 :     args_t configure_args = {
     156           0 :       .configure.command = CONFIGURE_CMD_INIT,
     157           0 :     };
     158           0 :     for( ulong i=0UL; STAGES[ i ]; i++ )
     159           0 :       configure_args.configure.stages[ i ] = STAGES[ i ];
     160           0 :     configure_cmd_fn( &configure_args, config );
     161           0 :   }
     162             : 
     163           0 :   update_config_for_dev( config );
     164           0 :   if( FD_UNLIKELY( args->dev.no_agave ) ) config->development.no_agave = 1;
     165             : 
     166           0 :   if( FD_UNLIKELY( strcmp( "", args->dev.debug_tile ) ) ) {
     167           0 :     if( FD_LIKELY( config->development.sandbox ) ) {
     168           0 :       FD_LOG_WARNING(( "disabling sandbox to debug tile `%s`", args->dev.debug_tile ));
     169           0 :       config->development.sandbox = 0;
     170           0 :     }
     171             : 
     172           0 :     if( !strcmp( args->dev.debug_tile, "solana" ) ||
     173           0 :         !strcmp( args->dev.debug_tile, "agave" ) ) {
     174           0 :       config->development.debug_tile = UINT_MAX; /* Sentinel value representing Agave */
     175           0 :     } else {
     176           0 :       ulong tile_id = fd_topo_find_tile( &config->topo, args->dev.debug_tile, 0UL );
     177           0 :       if( FD_UNLIKELY( tile_id==ULONG_MAX ) ) FD_LOG_ERR(( "--debug-tile `%s` not present in topology", args->dev.debug_tile ));
     178           0 :       config->development.debug_tile = 1U+(uint)tile_id;
     179           0 :     }
     180           0 :   }
     181             : 
     182           0 :   if( FD_LIKELY( args->dev.no_watch ) ) {
     183           0 :     if( FD_LIKELY( !config->development.no_clone ) ) run_firedancer( config, args->dev.parent_pipefd, !args->dev.no_init_workspaces );
     184           0 :     else {
     185           0 :       run_firedancer_threaded( config , !args->dev.no_init_workspaces, agave_main );
     186           0 :       for(;;) pause();
     187           0 :     }
     188           0 :   } else {
     189           0 :     if( FD_LIKELY( !config->development.no_clone ) ) {
     190           0 :       install_parent_signals();
     191             : 
     192           0 :       int pipefd[2];
     193           0 :       if( FD_UNLIKELY( pipe2( pipefd, O_NONBLOCK ) ) ) FD_LOG_ERR(( "pipe2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     194           0 :       initialize_workspaces(config);
     195           0 :       firedancer_pid = fork();
     196           0 :       if( !firedancer_pid ) {
     197           0 :         if( FD_UNLIKELY( -1==close( pipefd[0] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     198           0 :         if( FD_UNLIKELY( -1==close( STDERR_FILENO ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     199           0 :         if( FD_UNLIKELY( dup2( pipefd[1], STDERR_FILENO ) == -1 ) )
     200           0 :           FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     201           0 :         if( FD_UNLIKELY( -1==close( pipefd[1] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     202           0 :         if( FD_UNLIKELY( setenv( "RUST_LOG_STYLE", "always", 1 ) ) ) /* otherwise RUST_LOG will not be colorized to the pipe */
     203           0 :           FD_LOG_ERR(( "setenv() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     204           0 :         run_firedancer( config, -1, 0 );
     205           0 :       } else {
     206           0 :         if( FD_UNLIKELY( -1==close( pipefd[1] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     207           0 :       }
     208             : 
     209           0 :       args_t watch_args;
     210           0 :       watch_args.watch.drain_output_fd = pipefd[0];
     211             : 
     212           0 :       watch_pid = fork();
     213           0 :       if( !watch_pid ) watch_cmd_fn( &watch_args, config );
     214             : 
     215           0 :       if( FD_UNLIKELY( close( pipefd[0] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     216             : 
     217           0 :       int wstatus;
     218           0 :       pid_t exited_pid = wait4( -1, &wstatus, (int)__WALL, NULL );
     219           0 :       if( FD_UNLIKELY( exited_pid == -1 ) ) FD_LOG_ERR(( "wait4() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     220             : 
     221           0 :       char * exited_child = exited_pid == firedancer_pid ? "firedancer" : exited_pid == watch_pid ? "watch" : "unknown";
     222           0 :       int exit_code = 0;
     223           0 :       if( FD_UNLIKELY( !WIFEXITED( wstatus ) ) ) {
     224           0 :         FD_LOG_ERR(( "%s exited unexpectedly with signal %d (%s)", exited_child, WTERMSIG( wstatus ), fd_io_strsignal( WTERMSIG( wstatus ) ) ));
     225           0 :         exit_code = WTERMSIG( wstatus );
     226           0 :       } else {
     227           0 :         FD_LOG_ERR(( "%s exited unexpectedly with code %d", exited_child, WEXITSTATUS( wstatus ) ));
     228           0 :         if( FD_UNLIKELY( exited_pid==watch_pid && !WEXITSTATUS( wstatus ) ) ) exit_code = EXIT_FAILURE;
     229           0 :         else exit_code = WEXITSTATUS( wstatus );
     230           0 :       }
     231             : 
     232           0 :       if( FD_UNLIKELY( exited_pid==watch_pid ) ) {
     233           0 :         if( FD_UNLIKELY( kill( firedancer_pid, SIGKILL ) ) ) FD_LOG_ERR(( "failed to kill all processes (%i-%s)", errno, fd_io_strerror( errno ) ));
     234           0 :       } else {
     235           0 :         if( FD_UNLIKELY( kill( watch_pid, SIGKILL ) ) ) FD_LOG_ERR(( "failed to kill all processes (%i-%s)", errno, fd_io_strerror( errno ) ));
     236           0 :       }
     237           0 :       fd_sys_util_exit_group( exit_code );
     238           0 :     } else {
     239           0 :       int pipefd[2];
     240           0 :       if( FD_UNLIKELY( pipe2( pipefd, O_NONBLOCK ) ) ) FD_LOG_ERR(( "pipe2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     241             : 
     242           0 :       if( FD_LIKELY( !args->dev.no_init_workspaces ) ) initialize_workspaces( config );
     243             : 
     244           0 :       args_t watch_args;
     245           0 :       watch_args.watch.drain_output_fd = pipefd[0];
     246           0 :       fd_log_private_restore_stderr = dup( STDERR_FILENO );
     247           0 :       if( FD_UNLIKELY( fd_log_private_restore_stderr==-1 ) ) FD_LOG_ERR(( "dup() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     248           0 :       if( FD_UNLIKELY( -1==dup2( pipefd[ 1 ], STDERR_FILENO ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     249             : 
     250           0 :       run_firedancer_threaded( config, 0, agave_main );
     251           0 :       watch_cmd_fn( &watch_args, config );
     252           0 :     }
     253           0 :   }
     254           0 : }

Generated by: LCOV version 1.14