LCOV - code coverage report
Current view: top level - app/shared_dev/boot - fd_dev_boot.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 98 0.0 %
Date: 2025-09-18 04:41:32 Functions: 0 2 0.0 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "fd_dev_boot.h"
       3             : 
       4             : #include "../../shared/fd_config.h"
       5             : #include "../../shared/fd_action.h"
       6             : #include "../../shared/boot/fd_boot.h"
       7             : #include "../../platform/fd_file_util.h"
       8             : 
       9             : #include <errno.h>
      10             : #include <unistd.h>
      11             : #include <stdlib.h>
      12             : #include <stdio.h>
      13             : #include <sys/types.h>
      14             : #include <sys/stat.h>
      15             : 
      16             : extern char fd_log_private_path[ 1024 ];
      17             : 
      18             : extern action_t * ACTIONS[];
      19             : 
      20           0 : #define MAX_ARGC 32
      21             : 
      22             : /* Rerun the currently executing process as root. This will never return,
      23             :    instead it replaces the currently executing process with a new one. */
      24             : static void
      25             : execve_as_root( int     argc,
      26           0 :                 char ** argv ) {
      27           0 :   char _current_executable_path[ PATH_MAX ];
      28           0 :   FD_TEST( -1!=fd_file_util_self_exe( _current_executable_path ) );
      29             : 
      30           0 :   char * args[ MAX_ARGC+4 ];
      31           0 :   for( int i=1; i<argc; i++ ) args[i+2] = argv[i];
      32           0 :   args[ 0 ]      = "sudo";
      33           0 :   args[ 1 ]      = "-E";
      34           0 :   args[ 2 ]      = _current_executable_path;
      35             :   /* always override the log path to use the same one we just opened for ourselves */
      36           0 :   args[ argc+2 ] = "--log-path";
      37           0 :   args[ argc+3 ] = fd_log_private_path;
      38           0 :   args[ argc+4 ] = NULL;
      39             : 
      40             :   /* ok to leak these dynamic strings because we are about to execve anyway */
      41           0 :   char * envp[ 3 ] = {0};
      42           0 :   char * env;
      43           0 :   int    idx = 0;
      44           0 :   if( FD_LIKELY(( env = getenv( "FIREDANCER_CONFIG_TOML" ) )) ) {
      45           0 :     if( FD_UNLIKELY( asprintf( &envp[ idx++ ], "FIREDANCER_CONFIG_TOML=%s", env ) == -1 ) )
      46           0 :       FD_LOG_ERR(( "asprintf() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      47           0 :   }
      48           0 :   if( FD_LIKELY(( env = getenv( "TERM" ) )) ) {
      49           0 :     if( FD_UNLIKELY( asprintf( &envp[ idx++ ], "TERM=%s", env ) == -1 ) )
      50           0 :       FD_LOG_ERR(( "asprintf() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      51           0 :   }
      52             : 
      53           0 :   execve( "/usr/bin/sudo", args, envp );
      54           0 :   FD_LOG_ERR(( "execve(sudo) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      55           0 : }
      56             : 
      57             : config_t config;
      58             : 
      59             : int
      60             : fd_dev_main( int                        argc,
      61             :              char **                    _argv,
      62             :              int                        is_firedancer,
      63             :              fd_config_file_t * const * configs,
      64           0 :              void (* topo_init )( config_t * config ) ) {
      65             :   /* save original arguments list in case we need to respawn the process
      66             :      as privileged */
      67           0 :   int    orig_argc = argc;
      68           0 :   char * orig_argv[ MAX_ARGC+1 ] = {0};
      69           0 :   for( int i=0; i<fd_int_min( MAX_ARGC, argc ); i++ ) orig_argv[ i ] = _argv[ i ];
      70             : 
      71           0 :   if( FD_UNLIKELY( argc >= MAX_ARGC ) ) FD_LOG_ERR(( "too many arguments (%i)", argc ));
      72           0 :   char ** argv = _argv;
      73             : 
      74           0 :   argc--; argv++;
      75             : 
      76           0 :   fd_env_strip_cmdline_cstr( &argc, &argv, "--log-level-stderr", NULL, NULL );
      77           0 :   char const * log_path = fd_env_strip_cmdline_cstr( &argc, &argv, "--log-path", NULL, NULL );
      78             : 
      79           0 :   int no_sandbox = fd_env_strip_cmdline_contains( &argc, &argv, "--no-sandbox" );
      80           0 :   int no_clone = fd_env_strip_cmdline_contains( &argc, &argv, "--no-clone" );
      81             : 
      82           0 :   const char * opt_user_config_path = fd_env_strip_cmdline_cstr(
      83           0 :     &argc,
      84           0 :     &argv,
      85           0 :     "--config",
      86           0 :     "FIREDANCER_CONFIG_TOML",
      87           0 :     NULL );
      88             : 
      89           0 :   const char * action_name = "dev";
      90           0 :   if( FD_LIKELY( argc > 0 && !strcmp( argv[ 0 ], "--version" ) ) ) {
      91           0 :     action_name = "version";
      92           0 :     argc--; argv++;
      93           0 :   } else if( FD_LIKELY( argc > 0 && !strcmp( argv[ 0 ], "--help" ) ) ) {
      94           0 :     action_name = "help";
      95           0 :     argc--; argv++;
      96           0 :   } else if( FD_UNLIKELY( argc > 0 && argv[ 0 ][ 0 ] != '-' ) ) {
      97           0 :     action_name = argv[ 0 ];
      98           0 :     argc--; argv++;
      99           0 :   }
     100             : 
     101           0 :   action_t * action = NULL;
     102           0 :   for( ulong i=0UL; ACTIONS[ i ]; i++ ) {
     103           0 :     if( FD_UNLIKELY( !strcmp( action_name, ACTIONS[ i ]->name ) ) ) {
     104           0 :       action = ACTIONS[ i ];
     105           0 :       if( FD_UNLIKELY( action->is_immediate ) ) {
     106           0 :         action->fn( NULL, NULL );
     107           0 :         return 0;
     108           0 :       }
     109           0 :       break;
     110           0 :     }
     111           0 :   }
     112             : 
     113           0 :   if( FD_UNLIKELY( !action ) ) {
     114           0 :     fprintf( stderr, "unknown subcommand `%s`\n", action_name );
     115           0 :     exit( 1 );
     116           0 :   }
     117             : 
     118           0 :   fd_main_init( &argc, &argv, &config, opt_user_config_path, is_firedancer, action->is_local_cluster, log_path, configs, topo_init );
     119             : 
     120           0 :   config.development.no_clone = config.development.no_clone || no_clone;
     121           0 :   config.development.sandbox = config.development.sandbox && !no_sandbox && !no_clone;
     122             : 
     123           0 :   int is_allowed_live = action->is_diagnostic==1;
     124           0 :   if( FD_UNLIKELY( config.is_live_cluster && !is_allowed_live ) )
     125           0 :     FD_LOG_ERR(( "The `fddev` command is for development and test environments but your "
     126           0 :                  "configuration targets a live cluster. Use `fdctl` if this is a "
     127           0 :                  "production environment" ));
     128             : 
     129           0 :   if( FD_LIKELY( action->topo ) ) action->topo( &config );
     130             : 
     131           0 :   args_t args = {0};
     132           0 :   if( FD_LIKELY( action->args ) ) action->args( &argc, &argv, &args );
     133           0 :   if( FD_UNLIKELY( argc ) ) FD_LOG_ERR(( "unknown argument `%s`", argv[ 0 ] ));
     134             : 
     135             :   /* Check if we are appropriately permissioned to run the desired
     136             :      command. */
     137           0 :   if( FD_LIKELY( action->perm ) ) {
     138           0 :     fd_cap_chk_t * chk = fd_cap_chk_join( fd_cap_chk_new( __builtin_alloca_with_align( fd_cap_chk_footprint(), FD_CAP_CHK_ALIGN ) ) );
     139           0 :     action->perm( &args, chk, &config );
     140           0 :     ulong err_cnt = fd_cap_chk_err_cnt( chk );
     141           0 :     if( FD_UNLIKELY( err_cnt ) ) {
     142           0 :       if( FD_UNLIKELY( !geteuid() ) ) {
     143           0 :         for( ulong i=0UL; i<err_cnt; i++ ) FD_LOG_WARNING(( "%s", fd_cap_chk_err( chk, i ) ));
     144           0 :         FD_LOG_ERR(( "insufficient permissions to execute command `%s` when running as root. "
     145           0 :                      "fddev is likely being run with a reduced capability bounding set.", action_name ));
     146           0 :       }
     147           0 :       FD_LOG_INFO(( "insufficient permissions to execute command `%s`, rerunning as root", action_name ));
     148           0 :       execve_as_root( orig_argc, orig_argv );
     149           0 :     }
     150           0 :   }
     151             : 
     152             :   /* run the command */
     153           0 :   action->fn( &args, &config );
     154           0 :   return 0;
     155           0 : }

Generated by: LCOV version 1.14