LCOV - code coverage report
Current view: top level - app/shared/commands/run - run.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 569 0.0 %
Date: 2025-07-13 05:02:02 Functions: 0 17 0.0 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "run.h"
       3             : 
       4             : #include <sys/wait.h>
       5             : #include "generated/main_seccomp.h"
       6             : #if defined(__aarch64__)
       7             : #include "generated/pidns_arm64_seccomp.h"
       8             : #else
       9             : #include "generated/pidns_seccomp.h"
      10             : #endif
      11             : 
      12             : #include "../../../platform/fd_sys_util.h"
      13             : #include "../../../platform/fd_file_util.h"
      14             : #include "../../../platform/fd_net_util.h"
      15             : 
      16             : #include "../configure/configure.h"
      17             : 
      18             : #include <dirent.h>
      19             : #include <sched.h>
      20             : #include <stdio.h>
      21             : #include <stdlib.h> /* getenv */
      22             : #include <poll.h>
      23             : #include <unistd.h>
      24             : #include <errno.h>
      25             : #include <fcntl.h>
      26             : #include <sys/prctl.h>
      27             : #include <sys/resource.h>
      28             : #include <sys/mman.h>
      29             : #include <sys/stat.h>
      30             : #include <linux/capability.h>
      31             : #include <linux/unistd.h>
      32             : 
      33             : #include "../../../../util/tile/fd_tile_private.h"
      34             : 
      35             : extern fd_topo_obj_callbacks_t * CALLBACKS[];
      36             : 
      37           0 : #define NAME "run"
      38             : 
      39             : void
      40             : run_cmd_perm( args_t *         args,
      41             :               fd_cap_chk_t *   chk,
      42           0 :               config_t const * config ) {
      43           0 :   (void)args;
      44             : 
      45           0 :   ulong mlock_limit = fd_topo_mlock_max_tile( &config->topo );
      46             : 
      47           0 :   fd_cap_chk_raise_rlimit( chk, NAME, RLIMIT_MEMLOCK, mlock_limit, "call `rlimit(2)` to increase `RLIMIT_MEMLOCK` so all memory can be locked with `mlock(2)`" );
      48           0 :   fd_cap_chk_raise_rlimit( chk, NAME, RLIMIT_NICE,    40,          "call `setpriority(2)` to increase thread priorities" );
      49           0 :   fd_cap_chk_raise_rlimit( chk, NAME, RLIMIT_NOFILE,  CONFIGURE_NR_OPEN_FILES,
      50           0 :                                                                        "call `rlimit(2)  to increase `RLIMIT_NOFILE` to allow more open files for Agave" );
      51           0 :   fd_cap_chk_cap(          chk, NAME, CAP_NET_RAW,                 "call `socket(2)` to bind to a raw socket for use by XDP" );
      52           0 :   fd_cap_chk_cap(          chk, NAME, CAP_SYS_ADMIN,               "call `bpf(2)` with the `BPF_OBJ_GET` command to initialize XDP" );
      53           0 :   if( fd_sandbox_requires_cap_sys_admin( config->uid, config->gid ) )
      54           0 :     fd_cap_chk_cap(        chk, NAME, CAP_SYS_ADMIN,               "call `unshare(2)` with `CLONE_NEWUSER` to sandbox the process in a user namespace" );
      55           0 :   if( FD_LIKELY( getuid() != config->uid ) )
      56           0 :     fd_cap_chk_cap(        chk, NAME, CAP_SETUID,                  "call `setresuid(2)` to switch uid to the sandbox user" );
      57           0 :   if( FD_LIKELY( getgid()!=config->gid ) )
      58           0 :     fd_cap_chk_cap(        chk, NAME, CAP_SETGID,                  "call `setresgid(2)` to switch gid to the sandbox user" );
      59           0 :   if( FD_UNLIKELY( config->development.netns.enabled ) )
      60           0 :     fd_cap_chk_cap(        chk, NAME, CAP_SYS_ADMIN,               "call `setns(2)` to enter a network namespace" );
      61           0 :   if( FD_UNLIKELY( config->tiles.metric.prometheus_listen_port<1024 ) )
      62           0 :     fd_cap_chk_cap(        chk, NAME, CAP_NET_BIND_SERVICE,        "call `bind(2)` to bind to a privileged port for serving metrics" );
      63           0 :   if( FD_UNLIKELY( config->tiles.gui.gui_listen_port<1024 ) )
      64           0 :     fd_cap_chk_cap(        chk, NAME, CAP_NET_BIND_SERVICE,        "call `bind(2)` to bind to a privileged port for serving the GUI" );
      65           0 : }
      66             : 
      67             : struct pidns_clone_args {
      68             :   config_t const * config;
      69             :   int *            pipefd;
      70             :   int              closefd;
      71             : };
      72             : 
      73             : extern char fd_log_private_path[ 1024 ]; /* empty string on start */
      74             : 
      75             : static pid_t pid_namespace;
      76             : 
      77           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)
      78             : 
      79             : extern int * fd_log_private_shared_lock;
      80             : 
      81             : static void
      82           0 : parent_signal( int sig ) {
      83           0 :   if( FD_LIKELY( pid_namespace ) ) kill( pid_namespace, SIGKILL );
      84             : 
      85             :   /* A pretty gross hack.  For the local process, clear the lock so that
      86             :      we can always print the messages without waiting on another process,
      87             :      particularly if one of those processes might have just died.  The
      88             :      signal handler is re-entrant so this also avoids a deadlock since
      89             :      the log lock is not re-entrant. */
      90           0 :   int lock = 0;
      91           0 :   fd_log_private_shared_lock = &lock;
      92             : 
      93           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 ));
      94           0 :   else                                  FD_LOG_ERR_NOEXIT(( "Received signal %s",                fd_io_strsignal( sig ) ));
      95             : 
      96           0 :   if( FD_LIKELY( sig==SIGINT ) ) fd_sys_util_exit_group( 128+SIGINT );
      97           0 :   else                           fd_sys_util_exit_group( 0          );
      98           0 : }
      99             : 
     100             : static void
     101           0 : install_parent_signals( void ) {
     102           0 :   struct sigaction sa = {
     103           0 :     .sa_handler = parent_signal,
     104           0 :     .sa_flags   = 0,
     105           0 :   };
     106           0 :   if( FD_UNLIKELY( sigaction( SIGTERM, &sa, NULL ) ) )
     107           0 :     FD_LOG_ERR(( "sigaction(SIGTERM) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     108           0 :   if( FD_UNLIKELY( sigaction( SIGINT, &sa, NULL ) ) )
     109           0 :     FD_LOG_ERR(( "sigaction(SIGINT) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     110             : 
     111           0 :   sa.sa_handler = SIG_IGN;
     112           0 :   if( FD_UNLIKELY( sigaction( SIGUSR1, &sa, NULL ) ) )
     113           0 :     FD_LOG_ERR(( "sigaction(SIGUSR1) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     114           0 :   if( FD_UNLIKELY( sigaction( SIGUSR2, &sa, NULL ) ) )
     115           0 :     FD_LOG_ERR(( "sigaction(SIGUSR2) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     116           0 : }
     117             : 
     118             : void *
     119           0 : create_clone_stack( void ) {
     120           0 :   ulong mmap_sz = FD_TILE_PRIVATE_STACK_SZ + 2UL*FD_SHMEM_NORMAL_PAGE_SZ;
     121           0 :   uchar * stack = (uchar *)mmap( NULL, mmap_sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, (off_t)0 );
     122           0 :   if( FD_UNLIKELY( stack==MAP_FAILED ) )
     123           0 :     FD_LOG_ERR(( "mmap() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     124             : 
     125             :   /* Make space for guard lo and guard hi */
     126           0 :   if( FD_UNLIKELY( munmap( stack, FD_SHMEM_NORMAL_PAGE_SZ ) ) )
     127           0 :     FD_LOG_ERR(( "munmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     128           0 :   stack += FD_SHMEM_NORMAL_PAGE_SZ;
     129           0 :   if( FD_UNLIKELY( munmap( stack + FD_TILE_PRIVATE_STACK_SZ, FD_SHMEM_NORMAL_PAGE_SZ ) ) )
     130           0 :     FD_LOG_ERR(( "munmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     131             : 
     132             :   /* Create the guard regions in the extra space */
     133           0 :   void * guard_lo = (void *)(stack - FD_SHMEM_NORMAL_PAGE_SZ );
     134           0 :   if( FD_UNLIKELY( mmap( guard_lo, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
     135           0 :                          MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_lo ) )
     136           0 :     FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     137             : 
     138           0 :   void * guard_hi = (void *)(stack + FD_TILE_PRIVATE_STACK_SZ);
     139           0 :   if( FD_UNLIKELY( mmap( guard_hi, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
     140           0 :                          MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_hi ) )
     141           0 :     FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     142             : 
     143           0 :   return stack;
     144           0 : }
     145             : 
     146             : 
     147             : static int
     148             : execve_agave( int config_memfd,
     149           0 :                     int pipefd ) {
     150           0 :   if( FD_UNLIKELY( -1==fcntl( pipefd, F_SETFD, 0 ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     151           0 :   pid_t child = fork();
     152           0 :   if( FD_UNLIKELY( -1==child ) ) FD_LOG_ERR(( "fork() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     153           0 :   if( FD_LIKELY( !child ) ) {
     154           0 :     char _current_executable_path[ PATH_MAX ];
     155           0 :     FD_TEST( -1!=fd_file_util_self_exe( _current_executable_path ) );
     156             : 
     157           0 :     char config_fd[ 32 ];
     158           0 :     FD_TEST( fd_cstr_printf_check( config_fd, sizeof( config_fd ), NULL, "%d", config_memfd ) );
     159           0 :     char * args[ 5 ] = { _current_executable_path, "run-agave", "--config-fd", config_fd, NULL };
     160             : 
     161           0 :     char * envp[] = { NULL, NULL };
     162           0 :     char * google_creds = getenv( "GOOGLE_APPLICATION_CREDENTIALS" );
     163           0 :     char provide_creds[ PATH_MAX+30UL ];
     164           0 :     if( FD_UNLIKELY( google_creds ) ) {
     165           0 :       FD_TEST( fd_cstr_printf_check( provide_creds, sizeof( provide_creds ), NULL, "GOOGLE_APPLICATION_CREDENTIALS=%s", google_creds ) );
     166           0 :       envp[ 0 ] = provide_creds;
     167           0 :     }
     168             : 
     169           0 :     if( FD_UNLIKELY( -1==execve( _current_executable_path, args, envp ) ) ) FD_LOG_ERR(( "execve() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     170           0 :   } else {
     171           0 :     if( FD_UNLIKELY( -1==fcntl( pipefd, F_SETFD, FD_CLOEXEC ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,FD_CLOEXEC) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     172           0 :     return child;
     173           0 :   }
     174           0 :   return 0;
     175           0 : }
     176             : 
     177             : static pid_t
     178             : execve_tile( fd_topo_tile_t const * tile,
     179             :              fd_cpuset_t const *    floating_cpu_set,
     180             :              int                    floating_priority,
     181             :              int                    config_memfd,
     182           0 :              int                    pipefd ) {
     183           0 :   FD_CPUSET_DECL( cpu_set );
     184           0 :   if( FD_LIKELY( tile->cpu_idx!=ULONG_MAX ) ) {
     185             :     /* set the thread affinity before we clone the new process to ensure
     186             :         kernel first touch happens on the desired thread. */
     187           0 :     fd_cpuset_insert( cpu_set, tile->cpu_idx );
     188           0 :     if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     189           0 :   } else {
     190           0 :     fd_memcpy( cpu_set, floating_cpu_set, fd_cpuset_footprint() );
     191           0 :     if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     192           0 :   }
     193             : 
     194           0 :   if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, cpu_set ) ) ) {
     195           0 :     if( FD_LIKELY( errno==EINVAL ) ) {
     196           0 :       FD_LOG_ERR(( "Unable to set the thread affinity for tile %s:%lu on cpu %lu. It is likely that the affinity "
     197           0 :                    "you have specified for this tile in [layout.affinity] of your configuration file contains a "
     198           0 :                    "CPU (%lu) which does not exist on this machine.",
     199           0 :                    tile->name, tile->kind_id, tile->cpu_idx, tile->cpu_idx ));
     200           0 :     } else {
     201           0 :       FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     202           0 :     }
     203           0 :   }
     204             : 
     205             :   /* Clear CLOEXEC on the side of the pipe we want to pass to the tile. */
     206           0 :   if( FD_UNLIKELY( -1==fcntl( pipefd, F_SETFD, 0 ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     207           0 :   pid_t child = fork();
     208           0 :   if( FD_UNLIKELY( -1==child ) ) FD_LOG_ERR(( "fork() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     209           0 :   if( FD_LIKELY( !child ) ) {
     210           0 :     char _current_executable_path[ PATH_MAX ];
     211           0 :     FD_TEST( -1!=fd_file_util_self_exe( _current_executable_path ) );
     212             : 
     213           0 :     char kind_id[ 32 ], config_fd[ 32 ], pipe_fd[ 32 ];
     214           0 :     FD_TEST( fd_cstr_printf_check( kind_id,   sizeof( kind_id ),   NULL, "%lu", tile->kind_id ) );
     215           0 :     FD_TEST( fd_cstr_printf_check( config_fd, sizeof( config_fd ), NULL, "%d",  config_memfd ) );
     216           0 :     FD_TEST( fd_cstr_printf_check( pipe_fd,   sizeof( pipe_fd ),   NULL, "%d",  pipefd ) );
     217           0 :     char const * args[ 9 ] = { _current_executable_path, "run1", tile->name, kind_id, "--pipe-fd", pipe_fd, "--config-fd", config_fd, NULL };
     218           0 :     if( FD_UNLIKELY( -1==execve( _current_executable_path, (char **)args, NULL ) ) ) FD_LOG_ERR(( "execve() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     219           0 :   } else {
     220           0 :     if( FD_UNLIKELY( -1==fcntl( pipefd, F_SETFD, FD_CLOEXEC ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,FD_CLOEXEC) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     221           0 :     return child;
     222           0 :   }
     223           0 :   return 0;
     224           0 : }
     225             : 
     226             : extern int * fd_log_private_shared_lock;
     227             : 
     228             : int
     229           0 : main_pid_namespace( void * _args ) {
     230           0 :   struct pidns_clone_args * args = _args;
     231           0 :   if( FD_UNLIKELY( close( args->pipefd[ 0 ] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     232           0 :   if( FD_UNLIKELY( -1!=args->closefd ) ) {
     233           0 :     if( FD_UNLIKELY( close( args->closefd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     234           0 :   }
     235             : 
     236           0 :   config_t const * config = args->config;
     237             : 
     238           0 :   fd_log_thread_set( "pidns" );
     239           0 :   ulong pid = fd_sandbox_getpid(); /* Need to read /proc again.. we got a new PID from clone */
     240           0 :   fd_log_private_group_id_set( pid );
     241           0 :   fd_log_private_thread_id_set( pid );
     242           0 :   fd_log_private_stack_discover( FD_TILE_PRIVATE_STACK_SZ,
     243           0 :                                  &fd_tile_private_stack0, &fd_tile_private_stack1 );
     244             : 
     245           0 :   if( FD_UNLIKELY( !config->development.sandbox ) ) {
     246             :     /* If no sandbox, then there's no actual PID namespace so we can't
     247             :        wait() grandchildren for the exit code.  Do this as a workaround. */
     248           0 :     if( FD_UNLIKELY( -1==prctl( PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0 ) ) )
     249           0 :       FD_LOG_ERR(( "prctl(PR_SET_CHILD_SUBREAPER) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     250           0 :   }
     251             : 
     252             :   /* Save the current affinity, it will be restored after creating any child tiles */
     253           0 :   FD_CPUSET_DECL( floating_cpu_set );
     254           0 :   if( FD_UNLIKELY( fd_cpuset_getaffinity( 0, floating_cpu_set ) ) )
     255           0 :     FD_LOG_ERR(( "fd_cpuset_getaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     256             : 
     257           0 :   pid_t child_pids[ FD_TOPO_MAX_TILES+1 ];
     258           0 :   char  child_names[ FD_TOPO_MAX_TILES+1 ][ 32 ];
     259           0 :   struct pollfd fds[ FD_TOPO_MAX_TILES+2 ];
     260             : 
     261           0 :   int config_memfd = fd_config_to_memfd( config );
     262           0 :   if( FD_UNLIKELY( -1==config_memfd ) ) FD_LOG_ERR(( "fd_config_to_memfd() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     263             : 
     264           0 :   if( FD_UNLIKELY( config->development.debug_tile ) ) {
     265           0 :     fd_log_private_shared_lock[1] = 1;
     266           0 :   }
     267             : 
     268           0 :   ulong child_cnt = 0UL;
     269           0 :   if( FD_LIKELY( !config->is_firedancer && !config->development.no_agave ) ) {
     270           0 :     int pipefd[ 2 ];
     271           0 :     if( FD_UNLIKELY( pipe2( pipefd, O_CLOEXEC ) ) ) FD_LOG_ERR(( "pipe2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     272           0 :     fds[ child_cnt ] = (struct pollfd){ .fd = pipefd[ 0 ], .events = 0 };
     273           0 :     child_pids[ child_cnt ] = execve_agave( config_memfd, pipefd[ 1 ] );
     274           0 :     if( FD_UNLIKELY( close( pipefd[ 1 ] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     275           0 :     strncpy( child_names[ child_cnt ], "agave", 32 );
     276           0 :     child_cnt++;
     277           0 :   }
     278             : 
     279           0 :   if( FD_UNLIKELY( config->development.netns.enabled ) ) {
     280           0 :     if( FD_UNLIKELY( -1==fd_net_util_netns_enter( config->net.interface, NULL ) ) )
     281           0 :       FD_LOG_ERR(( "failed to enter network namespace `%s` (%i-%s)", config->net.interface, errno, fd_io_strerror( errno ) ));
     282           0 :   }
     283             : 
     284           0 :   errno = 0;
     285           0 :   int save_priority = getpriority( PRIO_PROCESS, 0 );
     286           0 :   if( FD_UNLIKELY( -1==save_priority && errno ) ) FD_LOG_ERR(( "getpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     287             : 
     288           0 :   int need_xdp = 0==strcmp( config->net.provider, "xdp" );
     289           0 :   fd_xdp_fds_t xdp_fds = {0};
     290           0 :   if( need_xdp ) {
     291           0 :     xdp_fds = fd_topo_install_xdp( &config->topo, config->net.bind_address_parsed );
     292           0 :   }
     293             : 
     294           0 :   for( ulong i=0UL; i<config->topo.tile_cnt; i++ ) {
     295           0 :     fd_topo_tile_t const * tile = &config->topo.tiles[ i ];
     296           0 :     if( FD_UNLIKELY( tile->is_agave ) ) continue;
     297             : 
     298           0 :     if( need_xdp ) {
     299           0 :       if( FD_UNLIKELY( strcmp( tile->name, "net" ) ) ) {
     300             :         /* close XDP related file descriptors */
     301           0 :         if( FD_UNLIKELY( -1==fcntl( xdp_fds.xsk_map_fd,   F_SETFD, FD_CLOEXEC ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,FD_CLOEXEC) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     302           0 :         if( FD_UNLIKELY( -1==fcntl( xdp_fds.prog_link_fd, F_SETFD, FD_CLOEXEC ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,FD_CLOEXEC) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     303           0 :       } else {
     304           0 :         if( FD_UNLIKELY( -1==fcntl( xdp_fds.xsk_map_fd,   F_SETFD, 0 ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     305           0 :         if( FD_UNLIKELY( -1==fcntl( xdp_fds.prog_link_fd, F_SETFD, 0 ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     306           0 :       }
     307           0 :     }
     308             : 
     309           0 :     int pipefd[ 2 ];
     310           0 :     if( FD_UNLIKELY( pipe2( pipefd, O_CLOEXEC ) ) ) FD_LOG_ERR(( "pipe2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     311           0 :     fds[ child_cnt ] = (struct pollfd){ .fd = pipefd[ 0 ], .events = 0 };
     312           0 :     child_pids[ child_cnt ] = execve_tile( tile, floating_cpu_set, save_priority, config_memfd, pipefd[ 1 ] );
     313           0 :     if( FD_UNLIKELY( close( pipefd[ 1 ] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     314           0 :     strncpy( child_names[ child_cnt ], tile->name, 32 );
     315           0 :     child_cnt++;
     316           0 :   }
     317             : 
     318           0 :   if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, save_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     319           0 :   if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, floating_cpu_set ) ) )
     320           0 :     FD_LOG_ERR(( "fd_cpuset_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     321             : 
     322           0 :   if( FD_UNLIKELY( close( config_memfd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     323           0 :   if( FD_UNLIKELY( close( config->log.lock_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     324           0 :   if( need_xdp ) {
     325           0 :     if( FD_UNLIKELY( close( xdp_fds.xsk_map_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     326           0 :     if( FD_UNLIKELY( close( xdp_fds.prog_link_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     327           0 :   }
     328             : 
     329           0 :   int allow_fds[ 4+FD_TOPO_MAX_TILES ];
     330           0 :   ulong allow_fds_cnt = 0;
     331           0 :   allow_fds[ allow_fds_cnt++ ] = 2; /* stderr */
     332           0 :   if( FD_LIKELY( fd_log_private_logfile_fd()!=-1 ) )
     333           0 :     allow_fds[ allow_fds_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     334           0 :   allow_fds[ allow_fds_cnt++ ] = args->pipefd[ 1 ]; /* write end of main pipe */
     335           0 :   for( ulong i=0; i<child_cnt; i++ )
     336           0 :     allow_fds[ allow_fds_cnt++ ] = fds[ i ].fd; /* read end of child pipes */
     337             : 
     338           0 :   struct sock_filter seccomp_filter[ 128UL ];
     339           0 :   unsigned int instr_cnt;
     340             :   #if defined(__aarch64__)
     341             :   populate_sock_filter_policy_pidns_arm64( 128UL, seccomp_filter, (uint)fd_log_private_logfile_fd() );
     342             :   instr_cnt = sock_filter_policy_pidns_arm64_instr_cnt;
     343             :   #else
     344           0 :   populate_sock_filter_policy_pidns( 128UL, seccomp_filter, (uint)fd_log_private_logfile_fd() );
     345           0 :   instr_cnt = sock_filter_policy_pidns_instr_cnt;
     346           0 :   #endif
     347             : 
     348           0 :   if( FD_LIKELY( config->development.sandbox ) ) {
     349           0 :     fd_sandbox_enter( config->uid,
     350           0 :                       config->gid,
     351           0 :                       0,
     352           0 :                       0,
     353           0 :                       0,
     354           0 :                       0,
     355           0 :                       1UL+child_cnt, /* RLIMIT_NOFILE needs to be set to the nfds argument of poll() */
     356           0 :                       0UL,
     357           0 :                       0UL,
     358           0 :                       allow_fds_cnt,
     359           0 :                       allow_fds,
     360           0 :                       instr_cnt,
     361           0 :                       seccomp_filter );
     362           0 :   } else {
     363           0 :     fd_sandbox_switch_uid_gid( config->uid, config->gid );
     364           0 :   }
     365             : 
     366             :   /* The supervsior process should not share the log lock, because a
     367             :      child process might die while holding it and we still need to
     368             :      reap and print errors. */
     369           0 :   int lock = 0;
     370           0 :   fd_log_private_shared_lock = &lock;
     371             : 
     372             :   /* Reap child process PIDs so they don't show up in `ps` etc.  All of
     373             :      these children should have exited immediately after clone(2)'ing
     374             :      another child with a huge page based stack. */
     375           0 :   for( ulong i=0; i<child_cnt; i++ ) {
     376           0 :     int wstatus;
     377           0 :     int exited_pid = wait4( child_pids[ i ], &wstatus, (int)__WALL, NULL );
     378           0 :     if( FD_UNLIKELY( -1==exited_pid ) ) {
     379           0 :       FD_LOG_ERR(( "pidns wait4() failed (%i-%s) %lu %hu", errno, fd_io_strerror( errno ), i, fds[i].revents ));
     380           0 :     } else if( FD_UNLIKELY( child_pids[ i ]!=exited_pid ) ) {
     381           0 :       FD_LOG_ERR(( "pidns wait4() returned unexpected pid %d %d", child_pids[ i ], exited_pid ));
     382           0 :     } else if( FD_UNLIKELY( !WIFEXITED( wstatus ) ) ) {
     383           0 :       FD_LOG_ERR_NOEXIT(( "tile %lu (%s) exited while booting with signal %d (%s)\n", i, child_names[ i ], WTERMSIG( wstatus ), fd_io_strsignal( WTERMSIG( wstatus ) ) ));
     384           0 :       fd_sys_util_exit_group( WTERMSIG( wstatus ) ? WTERMSIG( wstatus ) : 1 );
     385           0 :     }
     386           0 :     if( FD_UNLIKELY( WEXITSTATUS( wstatus ) ) ) {
     387           0 :       FD_LOG_ERR_NOEXIT(( "tile %lu (%s) exited while booting with code %d\n", i, child_names[ i ], WEXITSTATUS( wstatus ) ));
     388           0 :       fd_sys_util_exit_group( WEXITSTATUS( wstatus ) ? WEXITSTATUS( wstatus ) : 1 );
     389           0 :     }
     390           0 :   }
     391             : 
     392           0 :   fds[ child_cnt ] = (struct pollfd){ .fd = args->pipefd[ 1 ], .events = 0 };
     393             : 
     394             :   /* We are now the init process of the pid namespace.  If the init
     395             :      process dies, all children are terminated.  If any child dies, we
     396             :      terminate the init process, which will cause the kernel to
     397             :      terminate all other children bringing all of our processes down as
     398             :      a group.  The parent process will also die if this process dies,
     399             :      due to getting SIGHUP on the pipe. */
     400           0 :   while( 1 ) {
     401           0 :     if( FD_UNLIKELY( -1==poll( fds, 1+child_cnt, -1 ) ) ) FD_LOG_ERR(( "poll() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     402             : 
     403           0 :     for( ulong i=0UL; i<1UL+child_cnt; i++ ) {
     404           0 :       if( FD_UNLIKELY( fds[ i ].revents ) ) {
     405             :         /* Must have been POLLHUP, POLLERR and POLLNVAL are not possible. */
     406           0 :         if( FD_UNLIKELY( i==child_cnt ) ) {
     407             :           /* Parent process died, probably SIGINT, exit gracefully. */
     408           0 :           fd_sys_util_exit_group( 0 );
     409           0 :         }
     410             : 
     411           0 :         char * tile_name = child_names[ i ];
     412           0 :         ulong  tile_idx = 0UL;
     413           0 :         if( FD_LIKELY( i>0UL ) ) tile_idx = (!config->is_firedancer && config->development.no_agave) ? i : i-1UL;
     414           0 :         ulong  tile_id = config->topo.tiles[ tile_idx ].kind_id;
     415             : 
     416             :         /* Child process died, reap it to figure out exit code. */
     417           0 :         int wstatus;
     418           0 :         int exited_pid = wait4( -1, &wstatus, (int)__WALL | (int)WNOHANG, NULL );
     419           0 :         if( FD_UNLIKELY( -1==exited_pid ) ) {
     420           0 :           FD_LOG_ERR(( "pidns wait4() failed (%i-%s) %lu %hu", errno, fd_io_strerror( errno ), i, fds[ i ].revents ));
     421           0 :         } else if( FD_UNLIKELY( !exited_pid ) ) {
     422             :           /* Spurious wakeup, no child actually dead yet. */
     423           0 :           continue;
     424           0 :         }
     425             : 
     426           0 :         if( FD_UNLIKELY( !WIFEXITED( wstatus ) ) ) {
     427           0 :           FD_LOG_ERR_NOEXIT(( "tile %s:%lu exited with signal %d (%s)", tile_name, tile_id, WTERMSIG( wstatus ), fd_io_strsignal( WTERMSIG( wstatus ) ) ));
     428           0 :           fd_sys_util_exit_group( WTERMSIG( wstatus ) ? WTERMSIG( wstatus ) : 1 );
     429           0 :         } else {
     430           0 :           int exit_code = WEXITSTATUS( wstatus );
     431           0 :           if( FD_LIKELY( !exit_code && config->topo.tiles[ tile_idx ].allow_shutdown ) ) {
     432           0 :             FD_LOG_INFO(( "tile %s:%lu exited gracefully with code %d", tile_name, tile_id, exit_code ));
     433           0 :           } else {
     434           0 :             FD_LOG_ERR_NOEXIT(( "tile %s:%lu exited with code %d", tile_name, tile_id, exit_code ));
     435           0 :             fd_sys_util_exit_group( exit_code ? exit_code : 1 );
     436           0 :           }
     437           0 :         }
     438           0 :       }
     439           0 :     }
     440           0 :   }
     441           0 :   return 0;
     442           0 : }
     443             : 
     444             : int
     445             : clone_firedancer( config_t const * config,
     446             :                   int              close_fd,
     447           0 :                   int *            out_pipe ) {
     448             :   /* This pipe is here just so that the child process knows when the
     449             :      parent has died (it will get a HUP). */
     450           0 :   int pipefd[2];
     451           0 :   if( FD_UNLIKELY( pipe2( pipefd, O_CLOEXEC | O_NONBLOCK ) ) ) FD_LOG_ERR(( "pipe2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     452             : 
     453             :   /* clone into a pid namespace */
     454           0 :   int flags = config->development.sandbox ? CLONE_NEWPID : 0;
     455           0 :   struct pidns_clone_args args = { .config = config, .closefd = close_fd, .pipefd = pipefd, };
     456             : 
     457           0 :   void * stack = create_clone_stack();
     458             : 
     459           0 :   int pid_namespace = clone( main_pid_namespace, (uchar *)stack + FD_TILE_PRIVATE_STACK_SZ, flags, &args );
     460           0 :   if( FD_UNLIKELY( pid_namespace<0 ) ) FD_LOG_ERR(( "clone() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     461             : 
     462           0 :   if( FD_UNLIKELY( close( pipefd[ 1 ] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     463             : 
     464           0 :   *out_pipe = pipefd[ 0 ];
     465           0 :   return pid_namespace;
     466           0 : }
     467             : 
     468             : static void
     469             : workspace_path( config_t const *       config,
     470             :                 fd_topo_wksp_t const * wksp,
     471           0 :                 char                   out[ PATH_MAX ] ) {
     472           0 :   char const * mount_path;
     473           0 :   switch( wksp->page_sz ) {
     474           0 :     case FD_SHMEM_HUGE_PAGE_SZ:
     475           0 :       mount_path = config->hugetlbfs.huge_page_mount_path;
     476           0 :       break;
     477           0 :     case FD_SHMEM_GIGANTIC_PAGE_SZ:
     478           0 :       mount_path = config->hugetlbfs.gigantic_page_mount_path;
     479           0 :       break;
     480           0 :     default:
     481           0 :       FD_LOG_ERR(( "invalid page size %lu", wksp->page_sz ));
     482           0 :   }
     483             : 
     484           0 :   FD_TEST( fd_cstr_printf_check( out, PATH_MAX, NULL, "%s/%s_%s.wksp", mount_path, config->name, wksp->name ) );
     485           0 : }
     486             : 
     487             : static void
     488             : warn_unknown_files( config_t const * config,
     489           0 :                     ulong            mount_type ) {
     490           0 :   char const * mount_path;
     491           0 :   switch( mount_type ) {
     492           0 :     case 0UL:
     493           0 :       mount_path = config->hugetlbfs.huge_page_mount_path;
     494           0 :       break;
     495           0 :     case 1UL:
     496           0 :       mount_path = config->hugetlbfs.gigantic_page_mount_path;
     497           0 :       break;
     498           0 :     default:
     499           0 :       FD_LOG_ERR(( "invalid mount type %lu", mount_type ));
     500           0 :   }
     501             : 
     502             :   /* Check if there are any files in mount_path */
     503           0 :   DIR * dir = opendir( mount_path );
     504           0 :   if( FD_UNLIKELY( !dir ) ) {
     505           0 :     if( FD_UNLIKELY( errno!=ENOENT ) ) FD_LOG_ERR(( "error opening `%s` (%i-%s)", mount_path, errno, fd_io_strerror( errno ) ));
     506           0 :     return;
     507           0 :   }
     508             : 
     509           0 :   struct dirent * entry;
     510           0 :   while(( FD_LIKELY( entry = readdir( dir ) ) )) {
     511           0 :     if( FD_UNLIKELY( !strcmp( entry->d_name, ".") || !strcmp( entry->d_name, ".." ) ) ) continue;
     512             : 
     513           0 :     char entry_path[ PATH_MAX ];
     514           0 :     FD_TEST( fd_cstr_printf_check( entry_path, PATH_MAX, NULL, "%s/%s", mount_path, entry->d_name ));
     515             : 
     516           0 :     int known_file = 0;
     517           0 :     for( ulong i=0UL; i<config->topo.wksp_cnt; i++ ) {
     518           0 :       fd_topo_wksp_t const * wksp = &config->topo.workspaces[ i ];
     519             : 
     520           0 :       char expected_path[ PATH_MAX ];
     521           0 :       workspace_path( config, wksp, expected_path );
     522             : 
     523           0 :       if( !strcmp( entry_path, expected_path ) ) {
     524           0 :         known_file = 1;
     525           0 :         break;
     526           0 :       }
     527           0 :     }
     528             : 
     529           0 :     if( mount_type==0UL ) {
     530           0 :       for( ulong i=0UL; i<config->topo.tile_cnt; i++ ) {
     531           0 :         fd_topo_tile_t const * tile = &config->topo.tiles [ i ];
     532             : 
     533           0 :         char expected_path[ PATH_MAX ];
     534           0 :         FD_TEST( fd_cstr_printf_check( expected_path, PATH_MAX, NULL, "%s/%s_stack_%s%lu", config->hugetlbfs.huge_page_mount_path, config->name, tile->name, tile->kind_id ) );
     535             : 
     536           0 :         if( !strcmp( entry_path, expected_path ) ) {
     537           0 :           known_file = 1;
     538           0 :           break;
     539           0 :         }
     540           0 :       }
     541           0 :     }
     542             : 
     543           0 :     if( FD_UNLIKELY( !known_file ) ) FD_LOG_WARNING(( "unknown file `%s` found in `%s`", entry->d_name, mount_path ));
     544           0 :   }
     545             : 
     546           0 :   if( FD_UNLIKELY( closedir( dir ) ) ) FD_LOG_ERR(( "error closing `%s` (%i-%s)", mount_path, errno, fd_io_strerror( errno ) ));
     547           0 : }
     548             : 
     549             : void
     550           0 : initialize_workspaces( config_t * config ) {
     551             :   /* Switch to non-root uid/gid for workspace creation.  Permissions
     552             :      checks are still done as the current user. */
     553           0 :   uint gid = getgid();
     554           0 :   uint uid = getuid();
     555           0 :   if( FD_LIKELY( gid!=config->gid && -1==setegid( config->gid ) ) )
     556           0 :     FD_LOG_ERR(( "setegid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     557           0 :   if( FD_LIKELY( uid!=config->uid && -1==seteuid( config->uid ) ) )
     558           0 :     FD_LOG_ERR(( "seteuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     559             : 
     560           0 :   for( ulong i=0UL; i<config->topo.wksp_cnt; i++ ) {
     561           0 :     fd_topo_wksp_t * wksp = &config->topo.workspaces[ i ];
     562             : 
     563           0 :     char path[ PATH_MAX ];
     564           0 :     workspace_path( config, wksp, path );
     565             : 
     566           0 :     struct stat st;
     567           0 :     int result = stat( path, &st );
     568             : 
     569           0 :     int update_existing;
     570           0 :     if( FD_UNLIKELY( !result && config->is_live_cluster ) ) {
     571           0 :       if( FD_UNLIKELY( -1==unlink( path ) && errno!=ENOENT ) ) FD_LOG_ERR(( "unlink() failed when trying to create workspace `%s` (%i-%s)", path, errno, fd_io_strerror( errno ) ));
     572           0 :       update_existing = 0;
     573           0 :     } else if( FD_UNLIKELY( !result ) ) {
     574             :       /* Creating all of the workspaces is very expensive because the
     575             :          kernel has to zero out all of the pages.  There can be tens or
     576             :          hundreds of gigabytes of zeroing to do.
     577             : 
     578             :          What would be really nice is if the kernel let us create huge
     579             :          pages without zeroing them, but it's not possible.  The
     580             :          ftruncate and fallocate calls do not support this type of
     581             :          resize with the hugetlbfs filesystem.
     582             : 
     583             :          Instead.. to prevent repeatedly doing this zeroing every time
     584             :          we start the validator, we have a small hack here to re-use the
     585             :          workspace files if they exist. */
     586           0 :       update_existing = 1;
     587           0 :     } else if( FD_LIKELY( result && errno==ENOENT ) ) {
     588           0 :       update_existing = 0;
     589           0 :     } else {
     590           0 :       FD_LOG_ERR(( "stat failed when trying to create workspace `%s` (%i-%s)", path, errno, fd_io_strerror( errno ) ));
     591           0 :     }
     592             : 
     593           0 :     if( FD_UNLIKELY( -1==fd_topo_create_workspace( &config->topo, wksp, update_existing ) ) ) {
     594           0 :       FD_TEST( errno==ENOMEM );
     595             : 
     596           0 :       warn_unknown_files( config, wksp->page_sz!=FD_SHMEM_HUGE_PAGE_SZ );
     597             : 
     598           0 :       char path[ PATH_MAX ];
     599           0 :       workspace_path( config, wksp, path );
     600           0 :       FD_LOG_ERR(( "ENOMEM-Out of memory when trying to create workspace `%s` at `%s` "
     601           0 :                    "with %lu %s pages. Firedancer reserves enough memory for all of its workspaces "
     602           0 :                    "during the `hugetlbfs` configure step, so it is likely you have unknown files "
     603           0 :                    "left over in this directory which are consuming memory, or another program on "
     604           0 :                    "the system is using pages from the same mount.",
     605           0 :                    wksp->name, path, wksp->page_cnt, fd_shmem_page_sz_to_cstr( wksp->page_sz ) ));
     606           0 :     }
     607           0 :     fd_topo_join_workspace( &config->topo, wksp, FD_SHMEM_JOIN_MODE_READ_WRITE );
     608           0 :     fd_topo_wksp_new( &config->topo, wksp, CALLBACKS );
     609           0 :     fd_topo_leave_workspace( &config->topo, wksp );
     610           0 :   }
     611             : 
     612           0 :   if( FD_UNLIKELY( seteuid( uid ) ) ) FD_LOG_ERR(( "seteuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     613           0 :   if( FD_UNLIKELY( setegid( gid ) ) ) FD_LOG_ERR(( "setegid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     614           0 : }
     615             : 
     616             : void
     617           0 : initialize_stacks( config_t const * config ) {
     618             : # if FD_HAS_MSAN
     619             :   /* MSan calls an external symbolizer using fork() on crashes, which is
     620             :      incompatible with Firedancer's MAP_SHARED stacks. */
     621             :   (void)config;
     622             :   return;
     623             : # endif
     624             : 
     625             :   /* Switch to non-root uid/gid for workspace creation.  Permissions
     626             :      checks are still done as the current user. */
     627           0 :   uint gid = getgid();
     628           0 :   uint uid = getuid();
     629           0 :   if( FD_LIKELY( gid!=config->gid && -1==setegid( config->gid ) ) )
     630           0 :     FD_LOG_ERR(( "setegid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     631           0 :   if( FD_LIKELY( uid!=config->uid && -1==seteuid( config->uid ) ) )
     632           0 :     FD_LOG_ERR(( "seteuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     633             : 
     634           0 :   for( ulong i=0UL; i<config->topo.tile_cnt; i++ ) {
     635           0 :     fd_topo_tile_t const * tile = &config->topo.tiles[ i ];
     636             : 
     637           0 :     char path[ PATH_MAX ];
     638           0 :     FD_TEST( fd_cstr_printf_check( path, PATH_MAX, NULL, "%s/%s_stack_%s%lu", config->hugetlbfs.huge_page_mount_path, config->name, tile->name, tile->kind_id ) );
     639             : 
     640           0 :     int result = unlink( path );
     641           0 :     if( -1==result && errno!=ENOENT ) FD_LOG_ERR(( "unlink() failed when trying to create stack `%s` (%i-%s)", path, errno, fd_io_strerror( errno ) ));
     642             : 
     643             :     /* TODO: Use a better CPU idx for the stack if tile is floating */
     644           0 :     ulong stack_cpu_idx = 0UL;
     645           0 :     if( FD_LIKELY( tile->cpu_idx<65535UL ) ) stack_cpu_idx = tile->cpu_idx;
     646             : 
     647           0 :     char name[ PATH_MAX ];
     648           0 :     FD_TEST( fd_cstr_printf_check( name, PATH_MAX, NULL, "%s_stack_%s%lu", config->name, tile->name, tile->kind_id ) );
     649             : 
     650           0 :     ulong sub_page_cnt[ 1 ] = { 6 };
     651           0 :     ulong sub_cpu_idx [ 1 ] = { stack_cpu_idx };
     652           0 :     int err = fd_shmem_create_multi( name, FD_SHMEM_HUGE_PAGE_SZ, 1, sub_page_cnt, sub_cpu_idx, S_IRUSR | S_IWUSR ); /* logs details */
     653           0 :     if( FD_UNLIKELY( err && errno==ENOMEM ) ) {
     654           0 :       warn_unknown_files( config, 0UL );
     655             : 
     656           0 :       char path[ PATH_MAX ];
     657           0 :       FD_TEST( fd_cstr_printf_check( path, PATH_MAX, NULL, "%s/%s_stack_%s%lu", config->hugetlbfs.huge_page_mount_path, config->name, tile->name, tile->kind_id ) );
     658           0 :       FD_LOG_ERR(( "ENOMEM-Out of memory when trying to create huge page stack for tile `%s` at `%s`. "
     659           0 :                    "Firedancer reserves enough memory for all of its stacks during the `hugetlbfs` configure "
     660           0 :                    "step, so it is likely you have unknown files left over in this directory which are "
     661           0 :                    "consuming memory, or another program on the system is using pages from the same mount.",
     662           0 :                    tile->name, path ));
     663           0 :     } else if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "fd_shmem_create_multi failed" ));
     664           0 :   }
     665             : 
     666           0 :   if( FD_UNLIKELY( seteuid( uid ) ) ) FD_LOG_ERR(( "seteuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     667           0 :   if( FD_UNLIKELY( setegid( gid ) ) ) FD_LOG_ERR(( "setegid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     668           0 : }
     669             : 
     670             : extern configure_stage_t fd_cfg_stage_hugetlbfs;
     671             : extern configure_stage_t fd_cfg_stage_ethtool_channels;
     672             : extern configure_stage_t fd_cfg_stage_ethtool_gro;
     673             : extern configure_stage_t fd_cfg_stage_ethtool_loopback;
     674             : extern configure_stage_t fd_cfg_stage_sysctl;
     675             : extern configure_stage_t fd_cfg_stage_hyperthreads;
     676             : 
     677             : void
     678           0 : fdctl_check_configure( config_t const * config ) {
     679           0 :   configure_result_t check = fd_cfg_stage_hugetlbfs.check( config );
     680           0 :   if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
     681           0 :     FD_LOG_ERR(( "Huge pages are not configured correctly: %s. You can run `fdctl configure init hugetlbfs` "
     682           0 :                  "to create the mounts correctly. This must be done after every system restart before running "
     683           0 :                  "Firedancer.", check.message ));
     684             : 
     685           0 :   if( FD_LIKELY( !config->development.netns.enabled && 0==strcmp( config->net.provider, "xdp" ) ) ) {
     686           0 :     check = fd_cfg_stage_ethtool_channels.check( config );
     687           0 :     if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
     688           0 :       FD_LOG_ERR(( "Network %s. You can run `fdctl configure init ethtool-channels` to set the number of channels on the "
     689           0 :                   "network device correctly.", check.message ));
     690             : 
     691           0 :     check = fd_cfg_stage_ethtool_gro.check( config );
     692           0 :     if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
     693           0 :       FD_LOG_ERR(( "Network %s. You can run `fdctl configure init ethtool-gro` to disable generic-receive-offload "
     694           0 :                   "as required.", check.message ));
     695             : 
     696           0 :     check = fd_cfg_stage_ethtool_loopback.check( config );
     697           0 :     if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
     698           0 :       FD_LOG_ERR(( "Network %s. You can run `fdctl configure init ethtool-loopback` to disable tx-udp-segmentation "
     699           0 :                   "on the loopback device.", check.message ));
     700           0 :   }
     701             : 
     702           0 :   check = fd_cfg_stage_sysctl.check( config );
     703           0 :   if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
     704           0 :     FD_LOG_ERR(( "Kernel parameters are not configured correctly: %s. You can run `fdctl configure init sysctl` "
     705           0 :                  "to set kernel parameters correctly.", check.message ));
     706             : 
     707           0 :   check = fd_cfg_stage_hyperthreads.check( config );
     708           0 :   if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
     709           0 :     FD_LOG_ERR(( "Hyperthreading is not configured correctly: %s. You can run `fdctl configure init hyperthreads` "
     710           0 :                  "to configure hyperthreading correctly.", check.message ));
     711           0 : }
     712             : 
     713             : void
     714             : run_firedancer_init( config_t * config,
     715           0 :                      int        init_workspaces ) {
     716           0 :   struct stat st;
     717           0 :   int err = stat( config->paths.identity_key, &st );
     718           0 :   if( FD_UNLIKELY( -1==err && errno==ENOENT ) ) FD_LOG_ERR(( "[consensus.identity_path] key does not exist `%s`. You can generate an identity key at this path by running `fdctl keys new identity --config <toml>`", config->paths.identity_key ));
     719           0 :   else if( FD_UNLIKELY( -1==err ) )             FD_LOG_ERR(( "could not stat [consensus.identity_path] `%s` (%i-%s)", config->paths.identity_key, errno, fd_io_strerror( errno ) ));
     720             : 
     721           0 :   if( FD_UNLIKELY( !config->is_firedancer ) ) {
     722           0 :     for( ulong i=0UL; i<config->frankendancer.paths.authorized_voter_paths_cnt; i++ ) {
     723           0 :       err = stat( config->frankendancer.paths.authorized_voter_paths[ i ], &st );
     724           0 :       if( FD_UNLIKELY( -1==err && errno==ENOENT ) ) FD_LOG_ERR(( "[consensus.authorized_voter_paths] key does not exist `%s`", config->frankendancer.paths.authorized_voter_paths[ i ] ));
     725           0 :       else if( FD_UNLIKELY( -1==err ) )             FD_LOG_ERR(( "could not stat [consensus.authorized_voter_paths] `%s` (%i-%s)", config->frankendancer.paths.authorized_voter_paths[ i ], errno, fd_io_strerror( errno ) ));
     726           0 :     }
     727           0 :   }
     728             : 
     729           0 :   fdctl_check_configure( config );
     730           0 :   if( FD_LIKELY( init_workspaces ) ) initialize_workspaces( config );
     731           0 :   initialize_stacks( config );
     732           0 : }
     733             : 
     734             : void
     735             : fdctl_setup_netns( config_t * config,
     736           0 :                    int        stay ) {
     737           0 :   if( !config->development.netns.enabled ) return;
     738             : 
     739           0 :   int original_netns_;
     740           0 :   int * original_netns = stay ? NULL : &original_netns_;
     741           0 :   if( FD_UNLIKELY( -1==fd_net_util_netns_enter( config->net.interface, original_netns ) ) )
     742           0 :     FD_LOG_ERR(( "failed to enter network namespace `%s` (%i-%s)", config->net.interface, errno, fd_io_strerror( errno ) ));
     743             : 
     744           0 :   if( 0==strcmp( config->net.provider, "xdp" ) ) {
     745           0 :     fd_cfg_stage_ethtool_channels.init( config );
     746           0 :     fd_cfg_stage_ethtool_gro     .init( config );
     747           0 :     fd_cfg_stage_ethtool_loopback.init( config );
     748           0 :   }
     749             : 
     750           0 :   if( FD_UNLIKELY( original_netns && -1==fd_net_util_netns_restore( original_netns_ ) ) )
     751           0 :     FD_LOG_ERR(( "failed to restore network namespace (fd=%d) (%i-%s)", original_netns_, errno, fd_io_strerror( errno ) ));
     752           0 : }
     753             : 
     754             : /* The boot sequence is a little bit involved...
     755             : 
     756             :    A process tree is created that looks like,
     757             : 
     758             :    + main
     759             :    +-- pidns
     760             :        +-- agave
     761             :        +-- tile 0
     762             :        +-- tile 1
     763             :        ...
     764             : 
     765             :    What we want is that if any process in the tree dies, all other
     766             :    processes will also die.  This is done as follows,
     767             : 
     768             :     (a) pidns is the init process of a PID namespace, so if it dies the
     769             :         kernel will terminate the child processes.
     770             : 
     771             :     (b) main is the parent of pidns, so it can issue a waitpid() on the
     772             :         child PID, and when it completes terminate itself.
     773             : 
     774             :     (c) pidns is the parent of agave and the tiles, so it could
     775             :         issue a waitpid() of -1 to wait for any of them to terminate,
     776             :         but how would it know if main has died?
     777             : 
     778             :     (d) main creates a pipe, and passes the write end to pidns.  If main
     779             :         dies, the pipe will be closed, and pidns will get a HUP on the
     780             :         read end.  Then pidns creates a pipe per child and passes the
     781             :         write end to the child.  If any of the children die, the pipe
     782             :         will be closed, and pidns will get a HUP on the read end.
     783             : 
     784             :         Then pidns can call poll() on both the write end of the main
     785             :         pipe and the read end of all the child pipes.  If any of them
     786             :         raises SIGHUP, then pidns knows that the parent or a child has
     787             :         died, and it can terminate itself, which due to (a) and (b)
     788             :         will kill all other processes. */
     789             : void
     790             : run_firedancer( config_t * config,
     791             :                 int        parent_pipefd,
     792           0 :                 int        init_workspaces ) {
     793             :   /* dump the topology we are using to the output log */
     794           0 :   fd_topo_print_log( 0, &config->topo );
     795             : 
     796           0 :   run_firedancer_init( config, init_workspaces );
     797             : 
     798           0 : #if defined(__x86_64__) || defined(__aarch64__)
     799             : 
     800             : #ifndef SYS_landlock_create_ruleset
     801             : #define SYS_landlock_create_ruleset 444
     802             : #endif
     803             : 
     804           0 : #ifndef LANDLOCK_CREATE_RULESET_VERSION
     805           0 : #define LANDLOCK_CREATE_RULESET_VERSION (1U << 0)
     806           0 : #endif
     807             : 
     808           0 : #endif
     809           0 :   long abi = syscall( SYS_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION );
     810           0 :   if( -1L==abi && (errno==ENOSYS || errno==EOPNOTSUPP ) ) {
     811           0 :     FD_LOG_WARNING(( "The Landlock access control system is not supported by your Linux kernel. Firedancer uses landlock to "
     812           0 :                      "provide an additional layer of security to the sandbox, but it is not required." ));
     813           0 :   }
     814             : 
     815           0 :   if( FD_UNLIKELY( close( 0 ) ) ) FD_LOG_ERR(( "close(0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     816           0 :   if( FD_UNLIKELY( fd_log_private_logfile_fd()!=1 && close( 1 ) ) ) FD_LOG_ERR(( "close(1) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     817             : 
     818           0 :   int pipefd;
     819           0 :   pid_namespace = clone_firedancer( config, parent_pipefd, &pipefd );
     820             : 
     821             :   /* Print the location of the logfile on SIGINT or SIGTERM, and also
     822             :      kill the child.  They are connected by a pipe which the child is
     823             :      polling so we don't strictly need to kill the child, but its helpful
     824             :      to do that before printing the log location line, else it might
     825             :      get interleaved due to timing windows in the shutdown. */
     826           0 :   install_parent_signals();
     827             : 
     828           0 :   if( FD_UNLIKELY( close( config->log.lock_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     829             : 
     830           0 :   struct sock_filter seccomp_filter[ 128UL ];
     831           0 :   populate_sock_filter_policy_main( 128UL, seccomp_filter, (uint)fd_log_private_logfile_fd(), (uint)pid_namespace );
     832             : 
     833           0 :   int allow_fds[ 4 ];
     834           0 :   ulong allow_fds_cnt = 0;
     835           0 :   allow_fds[ allow_fds_cnt++ ] = 2; /* stderr */
     836           0 :   if( FD_LIKELY( fd_log_private_logfile_fd()!=-1 ) )
     837           0 :     allow_fds[ allow_fds_cnt++ ] = fd_log_private_logfile_fd(); /* logfile, or maybe stdout */
     838           0 :   allow_fds[ allow_fds_cnt++ ] = pipefd; /* read end of main pipe */
     839           0 :   if( FD_UNLIKELY( parent_pipefd!=-1 ) )
     840           0 :     allow_fds[ allow_fds_cnt++ ] = parent_pipefd; /* write end of parent pipe */
     841             : 
     842           0 :   if( FD_LIKELY( config->development.sandbox ) ) {
     843           0 :     fd_sandbox_enter( config->uid,
     844           0 :                       config->gid,
     845           0 :                       0,
     846           0 :                       0,
     847           0 :                       1, /* Keep controlling terminal for main so it can receive Ctrl+C */
     848           0 :                       0,
     849           0 :                       0UL,
     850           0 :                       0UL,
     851           0 :                       0UL,
     852           0 :                       allow_fds_cnt,
     853           0 :                       allow_fds,
     854           0 :                       sock_filter_policy_main_instr_cnt,
     855           0 :                       seccomp_filter );
     856           0 :   } else {
     857           0 :     fd_sandbox_switch_uid_gid( config->uid, config->gid );
     858           0 :   }
     859             : 
     860             :   /* The supervsior process should not share the log lock, because a
     861             :      child process might die while holding it and we still need to
     862             :      reap and print errors. */
     863           0 :   int lock = 0;
     864           0 :   fd_log_private_shared_lock = &lock;
     865             : 
     866             :   /* the only clean way to exit is SIGINT or SIGTERM on this parent process,
     867             :      so if wait4() completes, it must be an error */
     868           0 :   int wstatus;
     869           0 :   if( FD_UNLIKELY( -1==wait4( pid_namespace, &wstatus, (int)__WALL, NULL ) ) )
     870           0 :     FD_LOG_ERR(( "main wait4() failed (%i-%s)\nLog at \"%s\"", errno, fd_io_strerror( errno ), fd_log_private_path ));
     871             : 
     872           0 :   if( FD_UNLIKELY( WIFSIGNALED( wstatus ) ) ) fd_sys_util_exit_group( WTERMSIG( wstatus ) ? WTERMSIG( wstatus ) : 1 );
     873           0 :   else fd_sys_util_exit_group( WEXITSTATUS( wstatus ) ? WEXITSTATUS( wstatus ) : 1 );
     874           0 : }
     875             : 
     876             : void
     877             : run_cmd_fn( args_t *   args FD_PARAM_UNUSED,
     878           0 :             config_t * config ) {
     879           0 :   if( FD_UNLIKELY( !config->gossip.entrypoints_cnt && !config->development.bootstrap ) )
     880           0 :     FD_LOG_ERR(( "No entrypoints specified in configuration file under [gossip.entrypoints], but "
     881           0 :                  "at least one is needed to determine how to connect to the Solana cluster. If "
     882           0 :                  "you want to start a new cluster in a development environment, use `fddev` instead "
     883           0 :                  "of `fdctl`. If you want to use an existing genesis, set [development.bootstrap] "
     884           0 :                  "to \"true\" in the configuration file." ));
     885             : 
     886           0 :   for( ulong i=0; i<config->gossip.entrypoints_cnt; i++ ) {
     887           0 :     if( FD_UNLIKELY( !strcmp( config->gossip.entrypoints[ i ], "" ) ) )
     888           0 :       FD_LOG_ERR(( "One of the entrypoints in your configuration file under [gossip.entrypoints] is "
     889           0 :                    "empty. Please remove the empty entrypoint or set it correctly. "));
     890           0 :   }
     891             : 
     892           0 :   run_firedancer( config, -1, 1 );
     893           0 : }
     894             : 
     895             : action_t fd_action_run1 = {
     896             :   .name        = "run1",
     897             :   .args        = run1_cmd_args,
     898             :   .fn          = run1_cmd_fn,
     899             :   .perm        = NULL,
     900             :   .description = "Start up a single Firedancer tile"
     901             : };
     902             : 
     903             : action_t fd_action_run = {
     904             :   .name           = "run",
     905             :   .args           = NULL,
     906             :   .fn             = run_cmd_fn,
     907             :   .require_config = 1,
     908             :   .perm           = run_cmd_perm,
     909             :   .description    = "Start up a Firedancer validator",
     910             :   .permission_err = "insufficient permissions to execute command `%s`. It is recommended "
     911             :                     "to start Firedancer as the root user, but you can also start it "
     912             :                     "with the missing capabilities listed above. The program only needs "
     913             :                     "to start with elevated permissions to do privileged operations at "
     914             :                     "boot, and will immediately drop permissions and switch to the user "
     915             :                     "specified in your configuration file once they are complete. Firedancer "
     916             :                     "will not execute outside of the boot process as root, and will refuse "
     917             :                     "to start if it cannot drop privileges. Firedancer needs to be started "
     918             :                     "privileged to configure high performance networking with XDP.",
     919             : };

Generated by: LCOV version 1.14