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

Generated by: LCOV version 1.14