Line data Source code
1 : #define _GNU_SOURCE
2 : #include "run.h"
3 : #include "../../../../flamenco/accdb/fd_accdb.h"
4 : #include "../../../../flamenco/stakes/fd_epoch_credits.h"
5 : #include "../../../../flamenco/runtime/fd_cost_tracker_store.h"
6 : #include "../../../../flamenco/stakes/fd_vote_stakes.h"
7 : #include "../../../../flamenco/stakes/fd_stake_delegations.h"
8 : #include "../../../../disco/store/fd_store.h"
9 :
10 : #include <sys/wait.h>
11 : #include "generated/main_seccomp.h"
12 : #if defined(__aarch64__) || defined(__riscv)
13 : #include "generated/pidns_no_poll_seccomp.h"
14 : #else
15 : #include "generated/pidns_seccomp.h"
16 : #endif
17 :
18 : #include "../../fd_bootinfo.h"
19 : #include "../../../platform/fd_sys_util.h"
20 : #include "../../../platform/fd_file_util.h"
21 : #include "../../../platform/fd_net_util.h"
22 : #include "../../../../disco/net/fd_net_tile.h"
23 : #include "../../../../discof/backup/fd_backup.h"
24 : #include "../../../../discof/backup/fd_snap_pool.h"
25 : #include "../../../../discof/restore/utils/fd_ssarchive.h"
26 : #include "../../../../disco/waker/fd_waker.h"
27 : #include "../../../../disco/sleep/fd_sleep.h"
28 : #include "../../../../util/pod/fd_pod_format.h"
29 : #include "../../../../discof/restore/utils/fd_wfs.h"
30 :
31 : #include "../configure/configure.h"
32 : #include "../configure/fd_cpu_isolation.h"
33 : #include "../../../../disco/topo/fd_cpu_topo.h"
34 :
35 : #include <dirent.h>
36 : #include <sched.h>
37 : #include <stdio.h>
38 : #include <stdlib.h> /* getenv */
39 : #include <poll.h>
40 : #include <unistd.h>
41 : #include <errno.h>
42 : #include <fcntl.h>
43 : #include <sys/prctl.h>
44 : #include <sys/resource.h>
45 : #include <sys/mman.h>
46 : #include <sys/stat.h>
47 : #include <linux/capability.h>
48 :
49 : #if defined(__x86_64__)
50 : #include <cpuid.h>
51 : #endif
52 :
53 : #include "../../../../util/tile/fd_tile_private.h"
54 :
55 : extern fd_topo_obj_callbacks_t * CALLBACKS[];
56 :
57 0 : #define NAME "run"
58 :
59 : void
60 : run_cmd_perm( args_t * args,
61 : fd_cap_chk_t * chk,
62 0 : config_t const * config ) {
63 0 : (void)args;
64 :
65 0 : ulong mlock_limit = fd_topo_mlock_max_tile( &config->topo );
66 :
67 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)`" );
68 0 : fd_cap_chk_raise_rlimit( chk, NAME, RLIMIT_NICE, 40, "call `setpriority(2)` to increase thread priorities" );
69 0 : fd_cap_chk_raise_rlimit( chk, NAME, RLIMIT_NOFILE, CONFIGURE_NR_OPEN_FILES,
70 0 : "call `rlimit(2) to increase `RLIMIT_NOFILE` to allow more open files" );
71 0 : fd_cap_chk_cap( chk, NAME, CAP_NET_RAW, "call `socket(2)` to bind to a raw socket for use by XDP" );
72 0 : fd_cap_chk_cap( chk, NAME, CAP_SYS_ADMIN, "call `bpf(2)` with the `BPF_OBJ_GET` command to initialize XDP" );
73 0 : if( fd_sandbox_requires_cap_sys_admin( config->uid, config->gid ) )
74 0 : fd_cap_chk_cap( chk, NAME, CAP_SYS_ADMIN, "call `unshare(2)` with `CLONE_NEWUSER` to sandbox the process in a user namespace" );
75 0 : if( FD_LIKELY( getuid() != config->uid ) )
76 0 : fd_cap_chk_cap( chk, NAME, CAP_SETUID, "call `setresuid(2)` to switch uid to the sandbox user" );
77 0 : if( FD_LIKELY( getgid()!=config->gid ) )
78 0 : fd_cap_chk_cap( chk, NAME, CAP_SETGID, "call `setresgid(2)` to switch gid to the sandbox user" );
79 0 : if( FD_UNLIKELY( config->tiles.metric.prometheus_listen_port<1024 ) )
80 0 : fd_cap_chk_cap( chk, NAME, CAP_NET_BIND_SERVICE, "call `bind(2)` to bind to a privileged port for serving metrics" );
81 0 : if( FD_UNLIKELY( config->tiles.gui.gui_listen_port<1024 ) )
82 0 : fd_cap_chk_cap( chk, NAME, CAP_NET_BIND_SERVICE, "call `bind(2)` to bind to a privileged port for serving the GUI" );
83 0 : }
84 :
85 : struct pidns_clone_args {
86 : config_t const * config;
87 : int * pipefd;
88 : int closefd;
89 : };
90 :
91 : extern char fd_log_private_path[ 1024 ]; /* empty string on start */
92 :
93 : static pid_t pid_namespace;
94 :
95 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)
96 :
97 : static void
98 0 : parent_signal( int sig ) {
99 0 : if( FD_LIKELY( pid_namespace ) ) kill( pid_namespace, SIGKILL );
100 :
101 0 : if( -1!=fd_log_private_logfile_fd() ) FD_LOG_ERR_NOEXIT(( "Received signal %s%s%s %s(%s)%s\n%sLog at \"%s\"%s", fd_log_style_bold(), fd_io_strsignal_name( sig ), fd_log_style_normal(), fd_log_style_dim(), fd_io_strsignal_desc( sig ), fd_log_style_normal(), fd_log_style_dim(), fd_log_private_path, fd_log_style_normal() ));
102 0 : else FD_LOG_ERR_NOEXIT(( "Received signal %s%s%s %s(%s)%s", fd_log_style_bold(), fd_io_strsignal_name( sig ), fd_log_style_normal(), fd_log_style_dim(), fd_io_strsignal_desc( sig ), fd_log_style_normal() ));
103 :
104 0 : if( FD_LIKELY( sig==SIGINT ) ) fd_sys_util_exit_group( 128+SIGINT );
105 0 : else fd_sys_util_exit_group( 0 );
106 0 : }
107 :
108 : static void
109 0 : install_parent_signals( void ) {
110 0 : struct sigaction sa = {
111 0 : .sa_handler = parent_signal,
112 0 : .sa_flags = 0,
113 0 : };
114 0 : if( FD_UNLIKELY( sigaction( SIGTERM, &sa, NULL ) ) )
115 0 : FD_LOG_ERR(( "sigaction(SIGTERM) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
116 0 : if( FD_UNLIKELY( sigaction( SIGINT, &sa, NULL ) ) )
117 0 : FD_LOG_ERR(( "sigaction(SIGINT) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
118 :
119 0 : sa.sa_handler = SIG_IGN;
120 0 : if( FD_UNLIKELY( sigaction( SIGUSR1, &sa, NULL ) ) )
121 0 : FD_LOG_ERR(( "sigaction(SIGUSR1) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
122 0 : if( FD_UNLIKELY( sigaction( SIGUSR2, &sa, NULL ) ) )
123 0 : FD_LOG_ERR(( "sigaction(SIGUSR2) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
124 0 : }
125 :
126 : void *
127 0 : create_clone_stack( void ) {
128 0 : ulong mmap_sz = FD_TILE_PRIVATE_STACK_SZ + 2UL*FD_SHMEM_NORMAL_PAGE_SZ;
129 0 : uchar * stack = (uchar *)mmap( NULL, mmap_sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, (off_t)0 );
130 0 : if( FD_UNLIKELY( stack==MAP_FAILED ) )
131 0 : FD_LOG_ERR(( "mmap() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
132 :
133 : /* Make space for guard lo and guard hi */
134 0 : if( FD_UNLIKELY( munmap( stack, FD_SHMEM_NORMAL_PAGE_SZ ) ) )
135 0 : FD_LOG_ERR(( "munmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
136 0 : stack += FD_SHMEM_NORMAL_PAGE_SZ;
137 0 : if( FD_UNLIKELY( munmap( stack + FD_TILE_PRIVATE_STACK_SZ, FD_SHMEM_NORMAL_PAGE_SZ ) ) )
138 0 : FD_LOG_ERR(( "munmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
139 :
140 : /* Create the guard regions in the extra space */
141 0 : void * guard_lo = (void *)(stack - FD_SHMEM_NORMAL_PAGE_SZ );
142 0 : if( FD_UNLIKELY( mmap( guard_lo, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
143 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_lo ) )
144 0 : FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
145 :
146 0 : void * guard_hi = (void *)(stack + FD_TILE_PRIVATE_STACK_SZ);
147 0 : if( FD_UNLIKELY( mmap( guard_hi, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
148 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_hi ) )
149 0 : FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
150 :
151 0 : return stack;
152 0 : }
153 :
154 : static int
155 0 : cgroup_procs_write( char const * path ) {
156 0 : int fd = open( path, O_WRONLY );
157 0 : if( FD_UNLIKELY( fd<0 ) ) {
158 0 : if( FD_LIKELY( errno==ENOENT ) ) return 0;
159 0 : FD_LOG_ERR(( "open(%s) failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
160 0 : }
161 :
162 0 : char pid[ 32 ];
163 0 : ulong pid_len;
164 0 : FD_TEST( fd_cstr_printf_check( pid, sizeof(pid), &pid_len, "%ld", (long)getpid() ) );
165 0 : if( FD_UNLIKELY( write( fd, pid, pid_len )!=(long)pid_len ) )
166 0 : FD_LOG_ERR(( "write(%s,\"%s\") failed (%i-%s)", path, pid, errno, fd_io_strerror( errno ) ));
167 0 : if( FD_UNLIKELY( close( fd ) ) ) FD_LOG_ERR(( "close(%s) failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
168 0 : return 1;
169 0 : }
170 :
171 : struct spawn_cgroup {
172 : int probed; /* isolation cgroup existence checked, original saved */
173 : int present; /* isolation cgroup exists */
174 : int joined; /* currently a member of the isolation cgroup */
175 : char isolation[ PATH_MAX ];
176 : char original[ PATH_MAX ];
177 : };
178 :
179 : static void
180 : join_isolation_cgroup( char const * name,
181 0 : struct spawn_cgroup * cg ) {
182 0 : if( FD_UNLIKELY( !cg->probed ) ) {
183 0 : cg->probed = 1;
184 0 : FD_TEST( fd_cstr_printf_check( cg->isolation, sizeof(cg->isolation), NULL, "/sys/fs/cgroup/%s/cgroup.procs", name ) );
185 :
186 : /* The cpuset stage not being configured (no cgroup) is the common
187 : case and must be decided FIRST: on cgroup v1-only or hybrid
188 : hosts /proc/self/cgroup does not have the v2 format, and
189 : validating it before knowing the stage is even in use would
190 : turn every tile launch on such hosts into a fatal error. */
191 0 : cg->present = !access( cg->isolation, F_OK );
192 0 : if( FD_LIKELY( !cg->present ) ) return;
193 :
194 : /* Remember where we came from. The v2 entry in /proc/self/cgroup
195 : is the line "0::<path>"; on a pure v2 hierarchy it is the only
196 : line, but on hybrid systems v1 controller lines precede it, so
197 : search rather than assume. */
198 0 : char buf[ 4096 ];
199 0 : int fd = open( "/proc/self/cgroup", O_RDONLY );
200 0 : if( FD_UNLIKELY( fd<0 ) ) FD_LOG_ERR(( "open(/proc/self/cgroup) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
201 0 : long n = read( fd, buf, sizeof(buf)-1UL );
202 0 : if( FD_UNLIKELY( n<0L ) ) FD_LOG_ERR(( "read(/proc/self/cgroup) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
203 0 : if( FD_UNLIKELY( close( fd ) ) ) FD_LOG_ERR(( "close(/proc/self/cgroup) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
204 0 : buf[ n ] = '\0';
205 :
206 0 : char * line = buf;
207 0 : while( line && strncmp( line, "0::", 3UL ) ) {
208 0 : line = strchr( line, '\n' );
209 0 : if( FD_LIKELY( line ) ) line++;
210 0 : }
211 0 : if( FD_UNLIKELY( !line || !line[ 0 ] ) )
212 0 : FD_LOG_ERR(( "no cgroup v2 entry in /proc/self/cgroup while the cpuset isolation cgroup `/sys/fs/cgroup/%s` "
213 0 : "exists. Remove it with `%s configure fini cpuset`", name, FD_BINARY_NAME ));
214 0 : char * nl = strchr( line, '\n' ); if( FD_LIKELY( nl ) ) *nl = '\0';
215 0 : FD_TEST( fd_cstr_printf_check( cg->original, PATH_MAX, NULL,
216 0 : "/sys/fs/cgroup%s/cgroup.procs", line+3UL ) );
217 0 : }
218 :
219 0 : if( FD_UNLIKELY( !cg->present || cg->joined ) ) return;
220 0 : cg->joined = cgroup_procs_write( cg->isolation );
221 0 : }
222 :
223 : static void
224 0 : leave_isolation_cgroup( struct spawn_cgroup * cg ) {
225 0 : if( FD_LIKELY( !cg->joined ) ) return;
226 0 : if( FD_UNLIKELY( !cgroup_procs_write( cg->original ) ) ) FD_LOG_ERR(( "could not return to original cgroup `%s`", cg->original ));
227 0 : cg->joined = 0;
228 0 : }
229 :
230 : static pid_t
231 : execve_tile( char const * name,
232 : fd_topo_tile_t const * tile,
233 : fd_cpuset_t const * float_cpu_set,
234 : fd_cpuset_t const * cpuless_cpu_set,
235 : int floating_priority,
236 : int config_memfd,
237 : int pipefd,
238 0 : struct spawn_cgroup * cg ) {
239 0 : FD_CPUSET_DECL( cpu_set );
240 0 : if( FD_LIKELY( tile->cpu_idx!=ULONG_MAX ) ) {
241 : /* Join the cpuset isolation cgroup (if configured) and set the
242 : thread affinity before we clone the new process, to ensure
243 : kernel first touch happens on the desired thread. The child
244 : inherits both. */
245 0 : join_isolation_cgroup( name, cg );
246 0 : if( FD_UNLIKELY( tile->floats ) ) {
247 : /* the floating CPUs on this tile's NUMA node: memory was placed
248 : by cpu_idx */
249 0 : ulong numa_idx = fd_shmem_numa_idx( tile->cpu_idx );
250 0 : for( ulong cpu=0UL; cpu<FD_TILE_MAX; cpu++ )
251 0 : if( fd_cpuset_test( float_cpu_set, cpu ) && fd_shmem_numa_idx( cpu )==numa_idx ) fd_cpuset_insert( cpu_set, cpu );
252 0 : if( FD_UNLIKELY( !fd_cpuset_cnt( cpu_set ) ) ) fd_memcpy( cpu_set, float_cpu_set, fd_cpuset_footprint() );
253 0 : }
254 0 : if( FD_UNLIKELY( !fd_cpuset_cnt( cpu_set ) ) ) fd_cpuset_insert( cpu_set, tile->cpu_idx );
255 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
256 0 : } else {
257 0 : leave_isolation_cgroup( cg );
258 0 : fd_cpuset_copy( cpu_set, cpuless_cpu_set );
259 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
260 0 : }
261 :
262 0 : if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, cpu_set ) ) ) {
263 0 : if( FD_LIKELY( errno==EINVAL ) ) {
264 0 : FD_LOG_ERR(( "Unable to set the thread affinity for tile %s:%lu on cpu %lu. It is likely that the affinity "
265 0 : "you have specified for this tile in [layout.affinity] of your configuration file contains a "
266 0 : "CPU (%lu) which does not exist on this machine.",
267 0 : tile->name, tile->kind_id, tile->cpu_idx, tile->cpu_idx ));
268 0 : } else {
269 0 : FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
270 0 : }
271 0 : }
272 :
273 : /* Clear CLOEXEC on the side of the pipe we want to pass to the tile. */
274 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 ) ));
275 0 : pid_t child = fork();
276 0 : if( FD_UNLIKELY( -1==child ) ) FD_LOG_ERR(( "fork() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
277 0 : if( FD_LIKELY( !child ) ) {
278 0 : char _current_executable_path[ PATH_MAX ];
279 0 : FD_TEST( -1!=fd_file_util_self_exe( _current_executable_path ) );
280 :
281 0 : char kind_id[ 32 ], config_fd[ 32 ], pipe_fd[ 32 ];
282 0 : FD_TEST( fd_cstr_printf_check( kind_id, sizeof( kind_id ), NULL, "%lu", tile->kind_id ) );
283 0 : FD_TEST( fd_cstr_printf_check( config_fd, sizeof( config_fd ), NULL, "%d", config_memfd ) );
284 0 : FD_TEST( fd_cstr_printf_check( pipe_fd, sizeof( pipe_fd ), NULL, "%d", pipefd ) );
285 0 : char const * args[ 9 ] = { _current_executable_path, "run1", tile->name, kind_id, "--pipe-fd", pipe_fd, "--config-fd", config_fd, NULL };
286 0 : if( FD_UNLIKELY( -1==execve( _current_executable_path, (char **)args, NULL ) ) ) FD_LOG_ERR(( "execve() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
287 0 : } else {
288 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 ) ));
289 0 : return child;
290 0 : }
291 0 : return 0;
292 0 : }
293 :
294 : int
295 0 : main_pid_namespace( void * _args ) {
296 0 : struct pidns_clone_args * args = _args;
297 0 : if( FD_UNLIKELY( close( args->pipefd[ 0 ] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
298 0 : if( FD_UNLIKELY( -1!=args->closefd ) ) {
299 0 : if( FD_UNLIKELY( close( args->closefd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
300 0 : }
301 :
302 0 : config_t const * config = args->config;
303 :
304 0 : fd_log_thread_set( "pidns" );
305 0 : ulong pid = fd_sandbox_getpid(); /* Need to read /proc again.. we got a new PID from clone */
306 0 : fd_log_private_group_id_set( pid );
307 0 : fd_log_private_thread_id_set( pid );
308 0 : fd_log_private_stack_discover( FD_TILE_PRIVATE_STACK_SZ,
309 0 : &fd_tile_private_stack0, &fd_tile_private_stack1 );
310 :
311 0 : if( FD_UNLIKELY( !config->development.sandbox ) ) {
312 : /* If no sandbox, then there's no actual PID namespace so we can't
313 : wait() grandchildren for the exit code. Do this as a workaround. */
314 0 : if( FD_UNLIKELY( -1==prctl( PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0 ) ) )
315 0 : FD_LOG_ERR(( "prctl(PR_SET_CHILD_SUBREAPER) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
316 0 : }
317 :
318 : /* Save the current affinity, it will be restored after creating any child tiles */
319 0 : FD_CPUSET_DECL( floating_cpu_set );
320 0 : if( FD_UNLIKELY( fd_cpuset_getaffinity( 0, floating_cpu_set ) ) )
321 0 : FD_LOG_ERR(( "fd_cpuset_getaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
322 :
323 0 : FD_CPUSET_DECL( float_cpu_set );
324 0 : FD_CPUSET_DECL( cpuless_cpu_set );
325 0 : static fd_topo_cpus_t cpus[1];
326 0 : fd_topo_cpus_init( cpus );
327 0 : fd_topo_cpus_floating( cpus, &config->topo, floating_cpu_set, float_cpu_set, cpuless_cpu_set );
328 0 : int any_floats = 0;
329 0 : for( ulong i=0UL; i<config->topo.tile_cnt; i++ ) any_floats |= config->topo.tiles[ i ].floats;
330 :
331 0 : pid_t child_pids[ FD_TOPO_MAX_TILES+1 ];
332 0 : ulong actual_pids[ FD_TOPO_MAX_TILES+1 ];
333 0 : for( ulong i=0UL; i<FD_TOPO_MAX_TILES+1; i++ ) actual_pids[ i ] = ULONG_MAX;
334 0 : char child_names[ FD_TOPO_MAX_TILES+1 ][ 32 ];
335 0 : ulong child_idxs[ FD_TOPO_MAX_TILES+1 ];
336 0 : struct pollfd fds[ FD_TOPO_MAX_TILES+2 ];
337 :
338 0 : int config_memfd = fd_config_to_memfd( config );
339 0 : if( FD_UNLIKELY( -1==config_memfd ) ) FD_LOG_ERR(( "fd_config_to_memfd() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
340 :
341 0 : int need_mlx5 = 0==strcmp( config->net.provider, "mlx5" );
342 0 : fd_mlx5_fds_t mlx5_fds = { .cmd_fd=-1, .async_fd=-1 };
343 0 : if( need_mlx5 ) {
344 0 : fd_topo_install_mlx5( (fd_topo_t *)&config->topo, &mlx5_fds );
345 0 : }
346 :
347 0 : ulong child_cnt = 0UL;
348 :
349 0 : errno = 0;
350 0 : int save_priority = getpriority( PRIO_PROCESS, 0 );
351 0 : if( FD_UNLIKELY( -1==save_priority && errno ) ) FD_LOG_ERR(( "getpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
352 :
353 0 : int need_xdp = 0==strcmp( config->net.provider, "xdp" );
354 0 : fd_xdp_fds_t xdp_fds[ FD_TOPO_XDP_FDS_MAX ];
355 0 : uint xdp_fds_cnt = FD_TOPO_XDP_FDS_MAX;
356 0 : if( need_xdp ) {
357 0 : fd_topo_install_xdp( &config->topo, xdp_fds, &xdp_fds_cnt, config->net.bind_address_parsed, 0 );
358 0 : }
359 :
360 0 : initialize_accdb_fd( config );
361 0 : initialize_epoch_credits_fd( config );
362 0 : initialize_cost_tracker_fd( config );
363 0 : initialize_vote_stakes_fd( config );
364 0 : initialize_stake_delegations_fd( config );
365 0 : initialize_store_fds( config );
366 0 : ulong store_obj_id = fd_pod_query_ulong( config->topo.props, "store", ULONG_MAX );
367 0 : int has_store = store_obj_id!=ULONG_MAX;
368 0 : ulong snap_max = 0UL;
369 0 : int snapshot_upload_enabled = 0;
370 0 : int snapshot_dio_enabled = 0;
371 0 : snap_max = initialize_snapshot_fds( config );
372 0 : snapshot_upload_enabled = fd_topo_find_tile( &config->topo, "snapsv", 0UL )!=ULONG_MAX;
373 0 : snapshot_dio_enabled = fd_topo_find_tile( &config->topo, "snapzp", 0UL )!=ULONG_MAX;
374 :
375 0 : ulong waker_client_cnt = 0UL;
376 0 : for( ulong i=0UL; i<config->topo.tile_cnt; i++ ) {
377 0 : ulong idx = config->topo.tiles[ i ].waker_client_idx;
378 0 : if( FD_UNLIKELY( idx!=ULONG_MAX ) ) waker_client_cnt = fd_ulong_max( waker_client_cnt, idx+1UL );
379 0 : }
380 0 : fd_waker_install( waker_client_cnt );
381 0 : fd_sleep_eventfd_install( &config->topo );
382 :
383 0 : struct spawn_cgroup spawn_cg = {0};
384 :
385 0 : for( ulong pass=0UL; pass<2UL; pass++ ) {
386 0 : for( ulong i=0UL; i<config->topo.tile_cnt; i++ ) {
387 0 : fd_topo_tile_t const * tile = &config->topo.tiles[ i ];
388 0 : if( FD_UNLIKELY( (tile->cpu_idx!=ULONG_MAX)!=pass ) ) continue;
389 :
390 0 : if( need_xdp ) {
391 0 : if( FD_UNLIKELY( strcmp( tile->name, "net" ) ) ) {
392 0 : for( uint i=0U; i<xdp_fds_cnt; i++ ) {
393 : /* close XDP related file descriptors */
394 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 ) ));
395 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 ) ));
396 0 : }
397 0 : } else {
398 0 : for( uint i=0U; i<xdp_fds_cnt; i++ ) {
399 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 ) ));
400 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 ) ));
401 0 : }
402 0 : }
403 0 : }
404 :
405 0 : if( need_mlx5 ) {
406 0 : int const fd_flags = strcmp( tile->name, "mlx5" ) ? FD_CLOEXEC : 0;
407 0 : if( FD_UNLIKELY( -1==fcntl( mlx5_fds.cmd_fd, F_SETFD, fd_flags ) ) ) {
408 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
409 0 : }
410 0 : if( FD_UNLIKELY( -1==fcntl( mlx5_fds.async_fd, F_SETFD, fd_flags ) ) ) {
411 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
412 0 : }
413 0 : for( ulong i=0UL; i<mlx5_fds.rx_comp_channel_fd_cnt; i++ ) {
414 0 : int const rx_comp_channel_fd_flags = fd_flags || i!=tile->kind_id ? FD_CLOEXEC : 0;
415 0 : if( FD_UNLIKELY( -1==fcntl( mlx5_fds.rx_comp_channel_fd[ i ],
416 0 : F_SETFD, rx_comp_channel_fd_flags ) ) ) {
417 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
418 0 : }
419 0 : }
420 0 : }
421 :
422 0 : int tile_uses_accdb = 0;
423 0 : int tile_uses_accdb_ro = 0;
424 0 : for( ulong i=0UL; i<tile->uses_obj_cnt; i++ ) {
425 0 : fd_topo_obj_t const * obj = &config->topo.objs[ tile->uses_obj_id[ i ] ];
426 0 : if( FD_UNLIKELY( !strcmp( obj->name, "accdb" ) ) ) {
427 0 : if( FD_UNLIKELY( tile->uses_obj_mode[ i ]==FD_SHMEM_JOIN_MODE_READ_ONLY ) ) tile_uses_accdb_ro = 1;
428 0 : else tile_uses_accdb = 1;
429 0 : break;
430 0 : }
431 0 : }
432 :
433 : /* The gui joins the accdb shmem read-only (for partition stats)
434 : but never reads account data from the on-disk file, so it does
435 : not need the accounts.db fd. Withhold it to keep the gui at
436 : least privilege. */
437 0 : if( FD_UNLIKELY( !strcmp( tile->name, "gui" ) ) ) tile_uses_accdb_ro = 0;
438 0 : if( FD_UNLIKELY( !strcmp( tile->name, "snapmk" ) ) ) { tile_uses_accdb = 0; tile_uses_accdb_ro = 1; }
439 :
440 0 : if( FD_UNLIKELY( tile_uses_accdb ) ) {
441 0 : if( FD_UNLIKELY( -1==fcntl( FD_ACCDB_FD_RW, F_SETFD, 0 ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
442 0 : } else {
443 0 : if( FD_UNLIKELY( -1==fcntl( FD_ACCDB_FD_RW, F_SETFD, FD_CLOEXEC ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,FD_CLOEXEC) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
444 0 : }
445 :
446 0 : if( FD_UNLIKELY( tile_uses_accdb_ro ) ) {
447 0 : if( FD_UNLIKELY( -1==fcntl( FD_ACCDB_FD_RO, F_SETFD, 0 ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
448 0 : } else {
449 0 : if( FD_UNLIKELY( -1==fcntl( FD_ACCDB_FD_RO, F_SETFD, FD_CLOEXEC ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD,FD_CLOEXEC) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
450 0 : }
451 :
452 0 : if( FD_LIKELY( has_store ) ) {
453 0 : int tile_uses_store = 0;
454 0 : for( ulong i=0UL; i<tile->uses_obj_cnt; i++ ) tile_uses_store |= tile->uses_obj_id[ i ]==store_obj_id;
455 0 : int tile_uses_store_rw = tile_uses_store &&
456 0 : (!strcmp( tile->name, "shred" ) ||
457 0 : !strcmp( tile->name, "backt" ) ||
458 0 : !strcmp( tile->name, "rserve" ));
459 0 : int tile_uses_store_ro = tile_uses_store && !strcmp( tile->name, "replay" );
460 0 : if( FD_UNLIKELY( fcntl( FD_STORE_FD_RW, F_SETFD, tile_uses_store_rw ? 0 : FD_CLOEXEC )<0 ) )
461 0 : FD_LOG_ERR(( "fcntl(FD_STORE_FD_RW,F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
462 0 : if( FD_UNLIKELY( fcntl( FD_STORE_FD_RO, F_SETFD, tile_uses_store_ro ? 0 : FD_CLOEXEC )<0 ) )
463 0 : FD_LOG_ERR(( "fcntl(FD_STORE_FD_RO,F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
464 0 : }
465 :
466 0 : if( FD_UNLIKELY( -1==fcntl( FD_EPOCH_CREDITS_FD, F_SETFD, !strcmp( tile->name, "replay" ) ? 0 : FD_CLOEXEC ) ) )
467 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
468 :
469 0 : if( FD_UNLIKELY( -1==fcntl( FD_COST_TRACKER_FD, F_SETFD, !strcmp( tile->name, "replay" ) ? 0 : FD_CLOEXEC ) ) )
470 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
471 :
472 0 : int tile_uses_vote_stakes = !strcmp( tile->name, "replay" ) || !strcmp( tile->name, "execle" ) ||
473 0 : !strcmp( tile->name, "execrp" ) || !strcmp( tile->name, "snapmk" );
474 0 : if( FD_UNLIKELY( -1==fcntl( FD_VOTE_STAKES_FD, F_SETFD, tile_uses_vote_stakes ? 0 : FD_CLOEXEC ) ) )
475 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
476 :
477 0 : int tile_uses_stake_spill = !strcmp( tile->name, "replay" ) || !strcmp( tile->name, "execle" ) ||
478 0 : !strcmp( tile->name, "execrp" ) || !strcmp( tile->name, "snapin" );
479 0 : if( FD_UNLIKELY( -1==fcntl( FD_STAKE_DELEGATIONS_FD, F_SETFD, tile_uses_stake_spill ? 0 : FD_CLOEXEC ) ) )
480 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
481 :
482 0 : int tile_uses_snap_fd = !strcmp( tile->name, "snapct" ) ||
483 0 : !strcmp( tile->name, "snapmk" );
484 0 : int tile_uses_snap_dio_fd = !strcmp( tile->name, "snapzp" );
485 0 : int tile_uses_snap_rd_fd = !strcmp( tile->name, "snapsv" );
486 0 : for( ulong j=0UL; j<snap_max; j++ ) {
487 0 : if( FD_UNLIKELY( -1==fcntl( FD_SNAP_FD( j ), F_SETFD, tile_uses_snap_fd ? 0 : FD_CLOEXEC ) ) )
488 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
489 0 : if( snapshot_dio_enabled ) {
490 0 : if( FD_UNLIKELY( -1==fcntl( FD_SNAP_DIO_FD( j ), F_SETFD, tile_uses_snap_dio_fd ? 0 : FD_CLOEXEC ) ) )
491 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
492 0 : }
493 0 : if( snapshot_upload_enabled ) {
494 0 : if( FD_UNLIKELY( -1==fcntl( FD_SNAP_RO_FD( j ), F_SETFD, tile_uses_snap_rd_fd ? 0 : FD_CLOEXEC ) ) )
495 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
496 0 : }
497 0 : }
498 :
499 0 : int is_waker = !strcmp( tile->name, "waker" );
500 0 : int outer_entitled = is_waker || tile->waker_client_idx!=ULONG_MAX;
501 0 : if( FD_UNLIKELY( -1==fcntl( FD_WAKER_OUTER_FD, F_SETFD, outer_entitled ? 0 : FD_CLOEXEC ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
502 0 : for( ulong j=0UL; j<waker_client_cnt; j++ ) {
503 0 : int inner_entitled = is_waker || tile->waker_client_idx==j;
504 0 : if( FD_UNLIKELY( -1==fcntl( FD_WAKER_INNER_FD( j ), F_SETFD, inner_entitled ? 0 : FD_CLOEXEC ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
505 0 : }
506 :
507 0 : int is_mwaitx = !strcmp( tile->name, "mwaitx" );
508 0 : for( ulong j=0UL; j<config->topo.tile_cnt; j++ ) {
509 0 : if( FD_LIKELY( !config->topo.tiles[ j ].sleep_eventfd ) ) continue;
510 :
511 0 : int eventfd_entitled = is_mwaitx || tile->id==j;
512 0 : if( FD_UNLIKELY( -1==fcntl( FD_SLEEP_EVENTFD( j ), F_SETFD, eventfd_entitled ? 0 : FD_CLOEXEC ) ) ) FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
513 0 : }
514 :
515 0 : int pipefd[ 2 ];
516 0 : if( FD_UNLIKELY( pipe2( pipefd, O_CLOEXEC ) ) ) FD_LOG_ERR(( "pipe2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
517 0 : fds[ child_cnt ] = (struct pollfd){ .fd = pipefd[ 0 ], .events = 0 };
518 :
519 0 : int floating_priority = ( any_floats && !strcmp( tile->name, "waker" ) ) ? -19 : save_priority;
520 0 : child_pids[ child_cnt ] = execve_tile( config->name, tile, float_cpu_set, cpuless_cpu_set, floating_priority, config_memfd, pipefd[ 1 ], &spawn_cg );
521 0 : child_idxs[ child_cnt ] = i;
522 0 : if( FD_UNLIKELY( close( pipefd[ 1 ] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
523 0 : strncpy( child_names[ child_cnt ], tile->name, 32 );
524 0 : child_cnt++;
525 0 : }
526 0 : }
527 :
528 0 : leave_isolation_cgroup( &spawn_cg );
529 :
530 : /* Obtain the actual grandchild PID from the pipe */
531 0 : for( ulong i=0UL; i<child_cnt; i++ ) {
532 0 : if( FD_UNLIKELY( actual_pids[ i ]!=ULONG_MAX ) ) continue;
533 0 : FD_TEST( 8UL==read( fds[ i ].fd, &actual_pids[ i ], 8UL ) );
534 0 : }
535 :
536 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, save_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
537 0 : if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, floating_cpu_set ) ) )
538 0 : FD_LOG_ERR(( "fd_cpuset_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
539 :
540 0 : if( FD_UNLIKELY( close( config_memfd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
541 0 : if( need_xdp ) {
542 0 : for( uint i=0U; i<xdp_fds_cnt; i++ ) {
543 0 : if( FD_UNLIKELY( close( xdp_fds[i].xsk_map_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
544 0 : if( FD_UNLIKELY( close( xdp_fds[i].prog_link_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
545 0 : }
546 0 : }
547 :
548 0 : if( FD_UNLIKELY( -1==close( FD_ACCDB_FD_RW ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
549 0 : if( FD_UNLIKELY( -1==close( FD_ACCDB_FD_RO ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
550 0 : if( FD_LIKELY( has_store ) ) {
551 0 : if( FD_UNLIKELY( -1==close( FD_STORE_FD_RW ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
552 0 : if( FD_UNLIKELY( -1==close( FD_STORE_FD_RO ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
553 0 : }
554 0 : if( FD_UNLIKELY( -1==close( FD_EPOCH_CREDITS_FD ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
555 0 : if( FD_UNLIKELY( -1==close( FD_COST_TRACKER_FD ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
556 0 : if( FD_UNLIKELY( -1==close( FD_VOTE_STAKES_FD ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
557 0 : if( FD_UNLIKELY( -1==close( FD_STAKE_DELEGATIONS_FD ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
558 0 : for( ulong j=0UL; j<snap_max; j++ ) {
559 0 : if( FD_UNLIKELY( -1==close( FD_SNAP_FD( j ) ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
560 0 : if( snapshot_dio_enabled )
561 0 : if( FD_UNLIKELY( -1==close( FD_SNAP_DIO_FD( j ) ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
562 0 : if( snapshot_upload_enabled )
563 0 : if( FD_UNLIKELY( -1==close( FD_SNAP_RO_FD( j ) ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
564 0 : }
565 :
566 0 : if( FD_UNLIKELY( -1==close( FD_WAKER_OUTER_FD ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
567 0 : for( ulong j=0UL; j<waker_client_cnt; j++ ) {
568 0 : if( FD_UNLIKELY( -1==close( FD_WAKER_INNER_FD( j ) ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
569 0 : }
570 0 : for( ulong j=0UL; j<config->topo.tile_cnt; j++ ) {
571 0 : if( FD_LIKELY( !config->topo.tiles[ j ].sleep_eventfd ) ) continue;
572 0 : if( FD_UNLIKELY( -1==close( FD_SLEEP_EVENTFD( j ) ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
573 0 : }
574 :
575 0 : int allow_fds[ 4+FD_TOPO_MAX_TILES ];
576 0 : ulong allow_fds_cnt = 0;
577 0 : allow_fds[ allow_fds_cnt++ ] = 2; /* stderr */
578 0 : if( FD_LIKELY( fd_log_private_logfile_fd()!=-1 ) )
579 0 : allow_fds[ allow_fds_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
580 0 : allow_fds[ allow_fds_cnt++ ] = args->pipefd[ 1 ]; /* write end of main pipe */
581 0 : for( ulong i=0UL; i<child_cnt; i++ )
582 0 : allow_fds[ allow_fds_cnt++ ] = fds[ i ].fd; /* read end of child pipes */
583 0 : if( need_mlx5 ) {
584 0 : if( FD_UNLIKELY( -1==close( mlx5_fds.cmd_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
585 0 : if( FD_UNLIKELY( -1==close( mlx5_fds.async_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
586 0 : for( ulong i=0UL; i<mlx5_fds.rx_comp_channel_fd_cnt; i++ ) {
587 0 : if( FD_UNLIKELY( -1==close( mlx5_fds.rx_comp_channel_fd[ i ] ) ) )
588 0 : FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
589 0 : }
590 0 : }
591 :
592 0 : struct sock_filter seccomp_filter[ 128UL ];
593 0 : unsigned int instr_cnt;
594 : #if defined(__aarch64__) || defined(__riscv)
595 : populate_sock_filter_policy_pidns_no_poll( 128UL, seccomp_filter, (uint)fd_log_private_logfile_fd() );
596 : instr_cnt = sock_filter_policy_pidns_no_poll_instr_cnt;
597 : #else
598 0 : populate_sock_filter_policy_pidns( 128UL, seccomp_filter, (uint)fd_log_private_logfile_fd() );
599 0 : instr_cnt = sock_filter_policy_pidns_instr_cnt;
600 0 : #endif
601 :
602 0 : if( FD_LIKELY( config->development.sandbox ) ) {
603 0 : fd_sandbox_enter( config->uid,
604 0 : config->gid,
605 0 : 0,
606 0 : 0,
607 0 : 0,
608 0 : 0,
609 0 : 0,
610 0 : 1UL+child_cnt, /* RLIMIT_NOFILE needs to be set to the nfds argument of poll() */
611 0 : 0UL,
612 0 : 0UL,
613 0 : 0UL,
614 0 : allow_fds_cnt,
615 0 : allow_fds,
616 0 : instr_cnt,
617 0 : seccomp_filter );
618 0 : } else {
619 0 : fd_sandbox_switch_uid_gid( config->uid, config->gid );
620 0 : }
621 :
622 : /* Reap child process PIDs so they don't show up in `ps` etc. All of
623 : these children should have exited immediately after clone(2)'ing
624 : another child with a huge page based stack. */
625 0 : for( ulong i=0UL; i<child_cnt; i++ ) {
626 0 : int wstatus;
627 0 : int exited_pid = wait4( child_pids[ i ], &wstatus, (int)__WALL, NULL );
628 0 : if( FD_UNLIKELY( -1==exited_pid ) ) {
629 0 : FD_LOG_ERR(( "pidns wait4() failed (%i-%s) %lu %hu", errno, fd_io_strerror( errno ), i, fds[i].revents ));
630 0 : } else if( FD_UNLIKELY( child_pids[ i ]!=exited_pid ) ) {
631 0 : FD_LOG_ERR(( "pidns wait4() returned unexpected pid %d %d", child_pids[ i ], exited_pid ));
632 0 : } else if( FD_UNLIKELY( !WIFEXITED( wstatus ) ) ) {
633 0 : FD_LOG_ERR_NOEXIT(( "tile %lu (%s) exited while booting with signal %d (%s)", i, child_names[ i ], WTERMSIG( wstatus ), fd_io_strsignal( WTERMSIG( wstatus ) ) ));
634 0 : fd_sys_util_exit_group( WTERMSIG( wstatus ) ? WTERMSIG( wstatus ) : 1 );
635 0 : }
636 0 : if( FD_UNLIKELY( WEXITSTATUS( wstatus ) ) ) {
637 0 : FD_LOG_ERR_NOEXIT(( "tile %lu (%s) exited while booting with code %d", i, child_names[ i ], WEXITSTATUS( wstatus ) ));
638 0 : fd_sys_util_exit_group( WEXITSTATUS( wstatus ) ? WEXITSTATUS( wstatus ) : 1 );
639 0 : }
640 0 : }
641 :
642 0 : fds[ child_cnt ] = (struct pollfd){ .fd = args->pipefd[ 1 ], .events = 0 };
643 0 : strncpy( child_names[ child_cnt ], "parent", 32UL );
644 0 : child_idxs[ child_cnt ] = ULONG_MAX;
645 :
646 : /* We are now the init process of the pid namespace. If the init
647 : process dies, all children are terminated. If any child dies, we
648 : terminate the init process, which will cause the kernel to
649 : terminate all other children bringing all of our processes down as
650 : a group. The parent process will also die if this process dies,
651 : due to getting SIGHUP on the pipe. */
652 0 : while( 1 ) {
653 0 : if( FD_UNLIKELY( -1==poll( fds, 1UL+child_cnt, (int)-1 ) ) ) FD_LOG_ERR(( "poll() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
654 :
655 : /* Parent process died, probably SIGINT, exit gracefully. */
656 0 : if( FD_UNLIKELY( fds[ child_cnt ].revents ) ) fd_sys_util_exit_group( 0 );
657 :
658 : /* Child process died, reap it to figure out exit code. */
659 0 : int wstatus;
660 0 : int exited_pid = wait4( -1, &wstatus, (int)__WALL | (int)WNOHANG, NULL );
661 0 : if( FD_UNLIKELY( -1==exited_pid ) ) {
662 0 : FD_LOG_ERR(( "pidns wait4() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
663 0 : } else if( FD_UNLIKELY( !exited_pid ) ) {
664 : /* Spurious wakeup, no child actually dead yet. */
665 0 : continue;
666 0 : }
667 :
668 : /* Now find the tile corresponding to that PID */
669 0 : FD_TEST( exited_pid>0 );
670 0 : int found = 0;
671 0 : for( ulong i=0UL; i<child_cnt; i++ ) {
672 0 : if( FD_LIKELY( actual_pids[ i ]!=(ulong)exited_pid ) ) continue;
673 :
674 0 : found = 1;
675 0 : fds[ i ].fd = -1; /* Don't poll on this tile anymore */
676 :
677 0 : char * tile_name = child_names[ i ];
678 0 : ulong tile_idx = child_idxs[ i ];
679 0 : ulong tile_id = config->topo.tiles[ tile_idx ].kind_id;
680 :
681 0 : if( FD_UNLIKELY( !WIFEXITED( wstatus ) ) ) {
682 0 : FD_LOG_ERR_NOEXIT(( "tile %s%s:%lu%s exited with signal %d %s(%s)%s", fd_log_style_bold(), tile_name, tile_id, fd_log_style_normal(), WTERMSIG( wstatus ), fd_log_style_dim(), fd_io_strsignal( WTERMSIG( wstatus ) ), fd_log_style_normal() ));
683 0 : fd_sys_util_exit_group( WTERMSIG( wstatus ) ? WTERMSIG( wstatus ) : 1 );
684 0 : } else {
685 0 : int exit_code = WEXITSTATUS( wstatus );
686 0 : if( FD_LIKELY( !exit_code && tile_idx!=ULONG_MAX && config->topo.tiles[ tile_idx ].allow_shutdown ) ) {
687 0 : found = 1;
688 0 : FD_LOG_INFO(( "tile %s:%lu exited gracefully with code %d", tile_name, tile_id, exit_code ));
689 0 : } else {
690 0 : FD_LOG_ERR_NOEXIT(( "tile %s%s:%lu%s exited with code %d", fd_log_style_bold(), tile_name, tile_id, fd_log_style_normal(), exit_code ));
691 0 : fd_sys_util_exit_group( exit_code ? exit_code : 1 );
692 0 : }
693 0 : }
694 0 : }
695 :
696 0 : if( FD_UNLIKELY( !found ) ) FD_LOG_ERR(( "wait4() returned unexpected pid %d", exited_pid ));
697 0 : }
698 :
699 0 : return 0;
700 0 : }
701 :
702 : int
703 : clone_firedancer( config_t const * config,
704 : int close_fd,
705 0 : int * out_pipe ) {
706 : /* This pipe is here just so that the child process knows when the
707 : parent has died (it will get a HUP). */
708 0 : int pipefd[2];
709 0 : if( FD_UNLIKELY( pipe2( pipefd, O_CLOEXEC | O_NONBLOCK ) ) ) FD_LOG_ERR(( "pipe2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
710 :
711 : /* clone into a pid namespace */
712 0 : int flags = config->development.sandbox ? CLONE_NEWPID : 0;
713 0 : struct pidns_clone_args args = { .config = config, .closefd = close_fd, .pipefd = pipefd, };
714 :
715 0 : void * stack = create_clone_stack();
716 :
717 0 : int pid_namespace = clone( main_pid_namespace, (uchar *)stack + FD_TILE_PRIVATE_STACK_SZ, flags, &args );
718 0 : if( FD_UNLIKELY( pid_namespace<0 ) ) FD_LOG_ERR(( "clone() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
719 :
720 0 : if( FD_UNLIKELY( close( pipefd[ 1 ] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
721 :
722 0 : *out_pipe = pipefd[ 0 ];
723 0 : return pid_namespace;
724 0 : }
725 :
726 : static void
727 : workspace_path( config_t const * config,
728 : fd_topo_wksp_t const * wksp,
729 0 : char out[ PATH_MAX ] ) {
730 0 : char const * mount_path;
731 0 : switch( wksp->page_sz ) {
732 0 : case FD_SHMEM_HUGE_PAGE_SZ:
733 0 : mount_path = config->hugetlbfs.huge_page_mount_path;
734 0 : break;
735 0 : case FD_SHMEM_GIGANTIC_PAGE_SZ:
736 0 : mount_path = config->hugetlbfs.gigantic_page_mount_path;
737 0 : break;
738 0 : case FD_SHMEM_NORMAL_PAGE_SZ:
739 0 : mount_path = config->hugetlbfs.normal_page_mount_path;
740 0 : break;
741 0 : default:
742 0 : FD_LOG_ERR(( "invalid page size %lu", wksp->page_sz ));
743 0 : }
744 :
745 0 : FD_TEST( fd_cstr_printf_check( out, PATH_MAX, NULL, "%s/%s_%s.wksp", mount_path, config->name, wksp->name ) );
746 0 : }
747 :
748 : static void
749 : warn_unknown_files( config_t const * config,
750 0 : ulong mount_type ) {
751 0 : char const * mount_path;
752 0 : switch( mount_type ) {
753 0 : case 0UL:
754 0 : mount_path = config->hugetlbfs.huge_page_mount_path;
755 0 : break;
756 0 : case 1UL:
757 0 : mount_path = config->hugetlbfs.gigantic_page_mount_path;
758 0 : break;
759 0 : default:
760 0 : FD_LOG_ERR(( "invalid mount type %lu", mount_type ));
761 0 : }
762 :
763 : /* Check if there are any files in mount_path */
764 0 : DIR * dir = opendir( mount_path );
765 0 : if( FD_UNLIKELY( !dir ) ) {
766 0 : if( FD_UNLIKELY( errno!=ENOENT ) ) FD_LOG_ERR(( "error opening `%s` (%i-%s)", mount_path, errno, fd_io_strerror( errno ) ));
767 0 : return;
768 0 : }
769 :
770 0 : struct dirent * entry;
771 0 : for(;;) {
772 0 : errno = 0;
773 0 : entry = readdir( dir );
774 0 : if( FD_UNLIKELY( !entry ) ) break;
775 0 : if( FD_UNLIKELY( !strcmp( entry->d_name, ".") || !strcmp( entry->d_name, ".." ) ) ) continue;
776 :
777 0 : char entry_path[ PATH_MAX ];
778 0 : FD_TEST( fd_cstr_printf_check( entry_path, PATH_MAX, NULL, "%s/%s", mount_path, entry->d_name ));
779 :
780 0 : int known_file = 0;
781 0 : for( ulong i=0UL; i<config->topo.wksp_cnt; i++ ) {
782 0 : fd_topo_wksp_t const * wksp = &config->topo.workspaces[ i ];
783 :
784 0 : char expected_path[ PATH_MAX ];
785 0 : workspace_path( config, wksp, expected_path );
786 :
787 0 : if( !strcmp( entry_path, expected_path ) ) {
788 0 : known_file = 1;
789 0 : break;
790 0 : }
791 0 : }
792 :
793 0 : if( mount_type==0UL ) {
794 0 : for( ulong i=0UL; i<config->topo.tile_cnt; i++ ) {
795 0 : fd_topo_tile_t const * tile = &config->topo.tiles [ i ];
796 :
797 0 : char expected_path[ PATH_MAX ];
798 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 ) );
799 :
800 0 : if( !strcmp( entry_path, expected_path ) ) {
801 0 : known_file = 1;
802 0 : break;
803 0 : }
804 0 : }
805 0 : }
806 :
807 0 : if( FD_UNLIKELY( !known_file ) ) FD_LOG_WARNING(( "unknown file `%s` found in `%s`", entry->d_name, mount_path ));
808 0 : }
809 :
810 0 : if( FD_UNLIKELY( errno ) ) FD_LOG_ERR(( "error reading dir `%s` (%i-%s)", mount_path, errno, fd_io_strerror( errno ) ));
811 0 : if( FD_UNLIKELY( closedir( dir ) ) ) FD_LOG_ERR(( "error closing `%s` (%i-%s)", mount_path, errno, fd_io_strerror( errno ) ));
812 0 : }
813 :
814 : void
815 0 : initialize_workspaces( config_t * config ) {
816 : /* Switch to non-root uid/gid for workspace creation. Permissions
817 : checks are still done as the current user. */
818 0 : uint gid = getgid();
819 0 : uint uid = getuid();
820 0 : if( FD_LIKELY( gid!=config->gid && -1==setegid( config->gid ) ) )
821 0 : FD_LOG_ERR(( "setegid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
822 0 : if( FD_LIKELY( uid!=config->uid && -1==seteuid( config->uid ) ) )
823 0 : FD_LOG_ERR(( "seteuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
824 :
825 0 : for( ulong i=0UL; i<config->topo.wksp_cnt; i++ ) {
826 0 : fd_topo_wksp_t * wksp = &config->topo.workspaces[ i ];
827 :
828 0 : char path[ PATH_MAX ];
829 0 : workspace_path( config, wksp, path );
830 :
831 0 : struct stat st;
832 0 : int result = stat( path, &st );
833 :
834 0 : int update_existing;
835 0 : if( FD_UNLIKELY( !result && config->is_live_cluster && !config->is_dev ) ) {
836 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 ) ));
837 0 : update_existing = 0;
838 0 : } else if( FD_UNLIKELY( !result ) ) {
839 : /* Creating all of the workspaces is very expensive because the
840 : kernel has to zero out all of the pages. There can be tens or
841 : hundreds of gigabytes of zeroing to do.
842 :
843 : What would be really nice is if the kernel let us create huge
844 : pages without zeroing them, but it's not possible. The
845 : ftruncate and fallocate calls do not support this type of
846 : resize with the hugetlbfs filesystem.
847 :
848 : Instead.. to prevent repeatedly doing this zeroing every time
849 : we start the validator, we have a small hack here to re-use the
850 : workspace files if they exist. */
851 0 : update_existing = 1;
852 0 : } else if( FD_LIKELY( result && errno==ENOENT ) ) {
853 0 : update_existing = 0;
854 0 : } else {
855 0 : FD_LOG_ERR(( "stat failed when trying to create workspace `%s` (%i-%s)", path, errno, fd_io_strerror( errno ) ));
856 0 : }
857 :
858 0 : if( FD_UNLIKELY( -1==fd_topo_create_workspace( &config->topo, wksp, update_existing ) ) ) {
859 0 : FD_TEST( errno==ENOMEM );
860 :
861 0 : warn_unknown_files( config, wksp->page_sz!=FD_SHMEM_HUGE_PAGE_SZ );
862 :
863 0 : char path[ PATH_MAX ];
864 0 : workspace_path( config, wksp, path );
865 0 : FD_LOG_ERR(( "ENOMEM-Out of memory when trying to create workspace `%s` at `%s` "
866 0 : "with %lu %s pages. Firedancer reserves enough memory for all of its workspaces "
867 0 : "during the `hugetlbfs` configure step, so it is likely you have unknown files "
868 0 : "left over in this directory which are consuming memory, or another program on "
869 0 : "the system is using pages from the same mount.",
870 0 : wksp->name, path, wksp->page_cnt, fd_shmem_page_sz_to_cstr( wksp->page_sz ) ));
871 0 : }
872 0 : fd_topo_join_workspace( &config->topo, wksp, FD_SHMEM_JOIN_MODE_READ_WRITE, 0 );
873 0 : fd_topo_wksp_new( &config->topo, wksp, CALLBACKS );
874 0 : fd_topo_leave_workspace( &config->topo, wksp );
875 0 : }
876 :
877 0 : if( FD_UNLIKELY( seteuid( uid ) ) ) FD_LOG_ERR(( "seteuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
878 0 : if( FD_UNLIKELY( setegid( gid ) ) ) FD_LOG_ERR(( "setegid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
879 0 : }
880 :
881 : void
882 0 : initialize_stacks( config_t const * config ) {
883 : # if FD_HAS_MSAN
884 : /* MSan calls an external symbolizer using fork() on crashes, which is
885 : incompatible with Firedancer's MAP_SHARED stacks. */
886 : (void)config;
887 : return;
888 : # endif
889 :
890 : /* Switch to non-root uid/gid for workspace creation. Permissions
891 : checks are still done as the current user. */
892 0 : uint gid = getgid();
893 0 : uint uid = getuid();
894 0 : if( FD_LIKELY( gid!=config->gid && -1==setegid( config->gid ) ) )
895 0 : FD_LOG_ERR(( "setegid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
896 0 : if( FD_LIKELY( uid!=config->uid && -1==seteuid( config->uid ) ) )
897 0 : FD_LOG_ERR(( "seteuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
898 :
899 0 : for( ulong i=0UL; i<config->topo.tile_cnt; i++ ) {
900 0 : fd_topo_tile_t const * tile = &config->topo.tiles[ i ];
901 :
902 0 : char path[ PATH_MAX ];
903 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 ) );
904 :
905 0 : struct stat st;
906 0 : int result = stat( path, &st );
907 :
908 0 : int update_existing;
909 0 : if( FD_UNLIKELY( !result && config->is_live_cluster ) ) {
910 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 ) ));
911 0 : update_existing = 0;
912 0 : } else if( FD_UNLIKELY( !result ) ) {
913 : /* See above note about zeroing out pages. */
914 0 : update_existing = 1;
915 0 : } else if( FD_LIKELY( result && errno==ENOENT ) ) {
916 0 : update_existing = 0;
917 0 : } else {
918 0 : FD_LOG_ERR(( "stat failed when trying to create workspace `%s` (%i-%s)", path, errno, fd_io_strerror( errno ) ));
919 0 : }
920 :
921 : /* TODO: Use a better CPU idx for the stack if tile is floating */
922 0 : ulong stack_cpu_idx = 0UL;
923 0 : if( FD_LIKELY( tile->cpu_idx<65535UL ) ) stack_cpu_idx = tile->cpu_idx;
924 :
925 0 : char name[ PATH_MAX ];
926 0 : FD_TEST( fd_cstr_printf_check( name, PATH_MAX, NULL, "%s_stack_%s%lu", config->name, tile->name, tile->kind_id ) );
927 :
928 0 : ulong sub_page_cnt[ 1 ] = { 6 };
929 0 : ulong sub_cpu_idx [ 1 ] = { stack_cpu_idx };
930 0 : int err;
931 0 : if( FD_UNLIKELY( update_existing ) ) {
932 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 */
933 0 : } else {
934 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 */
935 0 : }
936 0 : if( FD_UNLIKELY( err && errno==ENOMEM ) ) {
937 0 : warn_unknown_files( config, 0UL );
938 :
939 0 : char path[ PATH_MAX ];
940 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 ) );
941 0 : FD_LOG_ERR(( "ENOMEM-Out of memory when trying to create huge page stack for tile `%s` at `%s`. "
942 0 : "Firedancer reserves enough memory for all of its stacks during the `hugetlbfs` configure "
943 0 : "step, so it is likely you have unknown files left over in this directory which are "
944 0 : "consuming memory, or another program on the system is using pages from the same mount.",
945 0 : tile->name, path ));
946 0 : } else if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "fd_shmem_create_multi failed" ));
947 0 : }
948 :
949 0 : if( FD_UNLIKELY( seteuid( uid ) ) ) FD_LOG_ERR(( "seteuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
950 0 : if( FD_UNLIKELY( setegid( gid ) ) ) FD_LOG_ERR(( "setegid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
951 0 : }
952 :
953 : void
954 0 : fdctl_check_configure( config_t const * config ) {
955 0 : # if defined(__x86_64__)
956 0 : uint eax, ebx, ecx, edx;
957 0 : if( FD_LIKELY( __get_cpuid_count( 7U, 0U, &eax, &ebx, &ecx, &edx ) ) ) {
958 0 : int cpu_avx512 = ((ebx>>16)&1U) && ((ebx>>21)&1U); /* avx512f, avx512ifma */
959 0 : int cpu_gfni = (int)((ecx>>8)&1U);
960 0 : if( FD_UNLIKELY( (cpu_avx512 && !FD_HAS_AVX512) || (cpu_gfni && !FD_HAS_GFNI) ) )
961 0 : FD_LOG_WARNING(( "This CPU supports%s%s but the binary was built without them, so it runs slower code paths. "
962 0 : "Rebuild on this host with MACHINE=native, or with a MACHINE matching this CPU.",
963 0 : (cpu_avx512 && !FD_HAS_AVX512) ? " AVX-512" : "", (cpu_gfni && !FD_HAS_GFNI) ? " GFNI" : "" ));
964 0 : }
965 0 : # endif
966 :
967 0 : configure_result_t check = fd_cfg_stage_hugetlbfs.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
968 0 : if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
969 0 : FD_LOG_ERR(( "Huge pages are not configured correctly: %s. You can run `%s configure init hugetlbfs` "
970 0 : "to create the mounts correctly. This must be done after every system restart before running "
971 0 : "Firedancer.", check.message, FD_BINARY_NAME ));
972 :
973 0 : if( FD_LIKELY( 0==strcmp( config->net.provider, "xdp" ) ) ) {
974 0 : if( fd_cfg_stage_bonding.enabled( config ) ) {
975 0 : check = fd_cfg_stage_bonding.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
976 0 : if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
977 0 : FD_LOG_ERR(( "Bonded network device is not configured correctly: %s. You can run `%s configure init bonding` "
978 0 : "to configure the bonding driver.", check.message, FD_BINARY_NAME ));
979 0 : }
980 :
981 0 : check = fd_cfg_stage_ethtool_channels.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
982 0 : if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
983 0 : FD_LOG_ERR(( "Network %s. You can run `%s configure init ethtool-channels` to set the number of channels on the "
984 0 : "network device correctly.", check.message, FD_BINARY_NAME ));
985 :
986 0 : check = fd_cfg_stage_ethtool_offloads.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
987 0 : if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
988 0 : FD_LOG_ERR(( "Network %s. You can run `%s configure init ethtool-offloads` to disable features "
989 0 : "as required.", check.message, FD_BINARY_NAME ));
990 :
991 0 : check = fd_cfg_stage_ethtool_loopback.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
992 0 : if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
993 0 : FD_LOG_ERR(( "Network %s. You can run `%s configure init ethtool-loopback` to disable tx-udp-segmentation "
994 0 : "on the loopback device.", check.message, FD_BINARY_NAME ));
995 0 : }
996 :
997 0 : if( FD_LIKELY( 0==strcmp( config->net.provider, "mlx5" ) ) ) {
998 0 : check = fd_cfg_stage_mlx5.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
999 0 : if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
1000 0 : FD_LOG_ERR(( "Network %s. You can run `%s configure init mlx5` to configure mlx5 network devices and load the ib_uverbs kernel module.",
1001 0 : check.message, FD_BINARY_NAME ));
1002 0 : }
1003 :
1004 0 : check = fd_cfg_stage_sysctl.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
1005 0 : if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
1006 0 : FD_LOG_ERR(( "Kernel parameters are not configured correctly: %s. You can run `%s configure init sysctl` "
1007 0 : "to set kernel parameters correctly.", check.message, FD_BINARY_NAME ));
1008 :
1009 : /* hyperthreads, nohz-full and rcu-nocbs are check-only stages: they
1010 : emit warnings themselves and always return OK, so there is no
1011 : result to act on (and no init to point the operator at). */
1012 0 : (void)fd_cfg_stage_hyperthreads.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
1013 0 : (void)fd_cfg_stage_nohz_full.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
1014 0 : (void)fd_cfg_stage_rcu_nocbs.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
1015 :
1016 : /* kworkers and cpuset are optional (but recommended) hardening: an
1017 : unconfigured stage only warns. A PARTIALLY_CONFIGURED cpuset is
1018 : fatal however: the isolation cgroup exists but covers the wrong
1019 : CPUs (e.g. stale from a previous [layout.affinity]), and the tile
1020 : launcher would join it and then fail to pin with a confusing
1021 : EINVAL. Fail up front with the fix instead. */
1022 0 : if( FD_UNLIKELY( fd_cfg_stage_kworkers.enabled( config ) ) ) {
1023 0 : check = fd_cfg_stage_kworkers.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
1024 0 : if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
1025 0 : FD_LOG_WARNING(( "Kernel workqueues may steal CPU time from Firedancer tiles: %s. For lower jitter, run "
1026 0 : "`%s configure init kworkers`.", check.message, FD_BINARY_NAME ));
1027 0 : }
1028 :
1029 : /* Floating tiles need a scheduler domain to be balanced across their
1030 : CPUs; isolcpus= removes it, and every floater would stay on the
1031 : CPU it was forked on. */
1032 0 : FD_CPUSET_DECL( isolated );
1033 0 : if( FD_LIKELY( fd_cpu_isolation_read_list( "/sys/devices/system/cpu/isolated", isolated ) ) ) {
1034 0 : for( ulong i=0UL; i<config->topo.tile_cnt; i++ ) {
1035 0 : fd_topo_tile_t const * tile = &config->topo.tiles[ i ];
1036 0 : if( FD_UNLIKELY( tile->floats && fd_cpuset_test( isolated, tile->cpu_idx ) ) )
1037 0 : FD_LOG_ERR(( "tile %s:%lu floats on CPU %lu, which the isolcpus= boot parameter removed from the kernel scheduler. "
1038 0 : "Floating tiles need their CPUs scheduled: drop them from isolcpus= and isolate them with "
1039 0 : "`%s configure init cpuset` instead.", tile->name, tile->kind_id, tile->cpu_idx, FD_BINARY_NAME ));
1040 0 : }
1041 0 : }
1042 :
1043 0 : FD_CPUSET_DECL( partition );
1044 0 : fd_cpu_isolation_partition_cpus( partition, &config->topo );
1045 0 : FD_CPUSET_DECL( housekeeping );
1046 0 : fd_cpuset_subtract( housekeeping, fd_cpu_isolation_online_cpus( housekeeping ), partition );
1047 0 : if( FD_UNLIKELY( fd_cpuset_cnt( partition ) && fd_cpuset_cnt( housekeeping )<2UL ) ) {
1048 0 : char list[ FD_CPU_ISOLATION_LIST_MAX ];
1049 0 : FD_LOG_WARNING(( "only %lu online CPU(s) (%s) remain outside Firedancer tile CPUs and their isolated hyperthread "
1050 0 : "siblings for the kernel, interrupts and other processes. Consider reducing tile counts or "
1051 0 : "adding to [layout.blocklist_cores].", fd_cpuset_cnt( housekeeping ),
1052 0 : fd_cpu_isolation_format_list( list, sizeof(list), housekeeping ) ));
1053 0 : }
1054 :
1055 0 : if( FD_LIKELY( fd_cfg_stage_cpuset.enabled( config ) ) ) {
1056 0 : check = fd_cfg_stage_cpuset.check( config, FD_CONFIGURE_CHECK_TYPE_RUN );
1057 0 : if( FD_UNLIKELY( check.result==CONFIGURE_PARTIALLY_CONFIGURED ) )
1058 0 : FD_LOG_ERR(( "The CPU isolation cgroup exists but does not match the topology: %s. Tiles would fail to pin "
1059 0 : "to their CPUs. Run `%s configure init cpuset` to fix it, or `%s configure fini cpuset` to "
1060 0 : "remove it.", check.message, FD_BINARY_NAME, FD_BINARY_NAME ));
1061 0 : else if( FD_UNLIKELY( check.result!=CONFIGURE_OK ) )
1062 0 : FD_LOG_WARNING(( "Firedancer tile CPUs are not isolated from other processes: %s. For lower jitter, run "
1063 0 : "`%s configure init cpuset`.", check.message, FD_BINARY_NAME ));
1064 0 : }
1065 :
1066 0 : fd_cpu_isolation_check_cgroup( config );
1067 0 : }
1068 :
1069 : void
1070 : run_firedancer_init( config_t * config,
1071 : int init_workspaces,
1072 0 : int check_configure ) {
1073 0 : struct stat st;
1074 0 : int err = stat( config->paths.identity_key, &st );
1075 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 `%s keys new %s --config <toml>`", config->paths.identity_key, FD_BINARY_NAME, config->paths.identity_key ));
1076 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 ) ));
1077 :
1078 : /* FIXME: fdctl_check_configure unconditionally checks for network
1079 : stack prerequisites even if the command being run does not
1080 : require networking. Hack around that here for now. */
1081 0 : if( check_configure ) fdctl_check_configure( config );
1082 0 : if( FD_LIKELY( init_workspaces ) ) initialize_workspaces( config );
1083 0 : initialize_stacks( config );
1084 0 : fd_bootinfo_write( config );
1085 0 : }
1086 :
1087 : void
1088 0 : initialize_accdb_fd( config_t const * config ) {
1089 : /* O_TRUNC of a previous run's accounts.db frees all its extents
1090 : synchronously. In development skip the truncate to keep reboots
1091 : fast. */
1092 0 : int oflags = O_RDWR|O_CREAT|O_NOATIME;
1093 0 : if( FD_LIKELY( !config->is_dev ) ) oflags |= O_TRUNC;
1094 :
1095 0 : int accounts_fd = open( config->paths.accounts, oflags, S_IRUSR|S_IWUSR );
1096 0 : if( FD_UNLIKELY( -1==accounts_fd ) ) FD_LOG_ERR(( "failed to open accounts.db (%i-%s)", errno, fd_io_strerror( errno ) ));
1097 0 : if( FD_UNLIKELY( -1==dup2( accounts_fd, FD_ACCDB_FD_RW ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1098 0 : if( FD_UNLIKELY( -1==close( accounts_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1099 :
1100 : /* Read-only fd for tiles (e.g. rpc) that consume accdb but must not
1101 : be able to mutate the on-disk file. Reopen via /proc/self/fd to
1102 : guarantee it refers to the same inode as the RW fd, avoiding any
1103 : race where the file at the path could be replaced between opens. */
1104 0 : char proc_path[ PATH_MAX ];
1105 0 : FD_TEST( fd_cstr_printf_check( proc_path, sizeof(proc_path), NULL, "/proc/self/fd/%d", FD_ACCDB_FD_RW ) );
1106 0 : int accounts_ro_fd = open( proc_path, O_RDONLY|O_NOATIME );
1107 0 : if( FD_UNLIKELY( -1==accounts_ro_fd ) ) FD_LOG_ERR(( "failed to open accounts.db read-only (%i-%s)", errno, fd_io_strerror( errno ) ));
1108 0 : if( FD_UNLIKELY( -1==dup2( accounts_ro_fd, FD_ACCDB_FD_RO ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1109 0 : if( FD_UNLIKELY( -1==close( accounts_ro_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1110 0 : }
1111 :
1112 : void
1113 0 : initialize_epoch_credits_fd( config_t const * config ) {
1114 0 : char const * spill_path = config->paths.epoch_credits;
1115 0 : int spill_fd = open( spill_path, O_RDWR|O_CREAT|O_TRUNC|O_NOATIME, S_IRUSR|S_IWUSR );
1116 0 : if( FD_UNLIKELY( -1==spill_fd ) ) FD_LOG_ERR(( "failed to open %s (%i-%s)", spill_path, errno, fd_io_strerror( errno ) ));
1117 0 : if( FD_UNLIKELY( -1==unlink( spill_path ) ) ) FD_LOG_ERR(( "unlink(%s) failed (%i-%s)", spill_path, errno, fd_io_strerror( errno ) ));
1118 :
1119 0 : if( FD_LIKELY( spill_fd!=FD_EPOCH_CREDITS_FD ) ) {
1120 0 : if( FD_UNLIKELY( -1==dup2( spill_fd, FD_EPOCH_CREDITS_FD ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1121 0 : if( FD_UNLIKELY( -1==close( spill_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1122 0 : }
1123 0 : }
1124 :
1125 : void
1126 0 : initialize_cost_tracker_fd( config_t const * config ) {
1127 0 : char const * spill_path = config->paths.cost_tracker;
1128 0 : int spill_fd = open( spill_path, O_RDWR|O_CREAT|O_TRUNC|O_NOATIME, S_IRUSR|S_IWUSR );
1129 0 : if( FD_UNLIKELY( -1==spill_fd ) ) FD_LOG_ERR(( "failed to open %s (%i-%s)", spill_path, errno, fd_io_strerror( errno ) ));
1130 0 : if( FD_UNLIKELY( -1==unlink( spill_path ) ) ) FD_LOG_ERR(( "unlink(%s) failed (%i-%s)", spill_path, errno, fd_io_strerror( errno ) ));
1131 :
1132 0 : if( FD_LIKELY( spill_fd!=FD_COST_TRACKER_FD ) ) {
1133 0 : if( FD_UNLIKELY( -1==dup2( spill_fd, FD_COST_TRACKER_FD ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1134 0 : if( FD_UNLIKELY( -1==close( spill_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1135 0 : }
1136 0 : }
1137 :
1138 : void
1139 0 : initialize_vote_stakes_fd( config_t const * config ) {
1140 0 : char const * spill_path = config->paths.vote_stakes;
1141 0 : int spill_fd = open( spill_path, O_RDWR|O_CREAT|O_TRUNC|O_NOATIME, S_IRUSR|S_IWUSR );
1142 0 : if( FD_UNLIKELY( -1==spill_fd ) ) FD_LOG_ERR(( "failed to open %s (%i-%s)", spill_path, errno, fd_io_strerror( errno ) ));
1143 0 : if( FD_UNLIKELY( -1==unlink( spill_path ) ) ) FD_LOG_ERR(( "unlink(%s) failed (%i-%s)", spill_path, errno, fd_io_strerror( errno ) ));
1144 :
1145 0 : if( FD_LIKELY( spill_fd!=FD_VOTE_STAKES_FD ) ) {
1146 0 : if( FD_UNLIKELY( -1==dup2( spill_fd, FD_VOTE_STAKES_FD ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1147 0 : if( FD_UNLIKELY( -1==close( spill_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1148 0 : }
1149 0 : }
1150 :
1151 : void
1152 0 : initialize_stake_delegations_fd( config_t const * config ) {
1153 0 : char const * spill_path = config->paths.stake_delegations;
1154 0 : int spill_fd = open( spill_path, O_RDWR|O_CREAT|O_TRUNC|O_NOATIME, S_IRUSR|S_IWUSR );
1155 0 : if( FD_UNLIKELY( -1==spill_fd ) ) FD_LOG_ERR(( "failed to open %s (%i-%s)", spill_path, errno, fd_io_strerror( errno ) ));
1156 0 : if( FD_UNLIKELY( -1==unlink( spill_path ) ) ) FD_LOG_ERR(( "unlink(%s) failed (%i-%s)", spill_path, errno, fd_io_strerror( errno ) ));
1157 :
1158 0 : if( FD_LIKELY( spill_fd!=FD_STAKE_DELEGATIONS_FD ) ) {
1159 0 : if( FD_UNLIKELY( -1==dup2( spill_fd, FD_STAKE_DELEGATIONS_FD ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1160 0 : if( FD_UNLIKELY( -1==close( spill_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1161 0 : }
1162 0 : }
1163 :
1164 : void
1165 0 : initialize_store_fds( config_t const * config ) {
1166 0 : fd_topo_t const * topo = &config->topo;
1167 0 : ulong store_obj_id = fd_pod_query_ulong( topo->props, "store", ULONG_MAX );
1168 0 : if( FD_UNLIKELY( store_obj_id==ULONG_MAX ) ) return;
1169 :
1170 0 : char const * path = fd_pod_queryf_cstr( topo->props, NULL, "obj.%lu.disk_path", store_obj_id );
1171 0 : ulong fec_max = fd_pod_queryf_ulong( topo->props, 0UL, "obj.%lu.fec_max", store_obj_id );
1172 0 : ulong fec_data_max = fd_pod_queryf_ulong( topo->props, 0UL, "obj.%lu.fec_data_max", store_obj_id );
1173 0 : ulong shred_storage_gib = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.shred_storage_gib", store_obj_id );
1174 0 : ulong payload_slot_sz = fd_store_payload_slot_sz( fec_data_max );
1175 0 : ulong wire_off;
1176 0 : if( FD_UNLIKELY( !path || !fec_max || !payload_slot_sz || shred_storage_gib>FD_SHREDB_MAX_SIZE_GIB ||
1177 0 : __builtin_umull_overflow( fec_max, payload_slot_sz, &wire_off ) ) )
1178 0 : FD_LOG_ERR(( "invalid Store backing-file configuration" ));
1179 :
1180 0 : int store_fd = fd_store_file_create( path, wire_off, fd_shredb_max_shreds( shred_storage_gib ) );
1181 0 : if( FD_UNLIKELY( store_fd<0 ) )
1182 0 : FD_LOG_ERR(( "failed to create Store backing file `%s` (%i-%s)", path, errno, fd_io_strerror( errno ) ));
1183 0 : if( FD_LIKELY( store_fd!=FD_STORE_FD_RW ) ) {
1184 0 : if( FD_UNLIKELY( dup2( store_fd, FD_STORE_FD_RW )<0 ) ) FD_LOG_ERR(( "dup2(Store RW) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1185 0 : if( FD_UNLIKELY( close( store_fd ) ) ) FD_LOG_ERR(( "close(Store RW source) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1186 0 : }
1187 :
1188 : /* Reopen through procfs so the read-only descriptor is guaranteed to
1189 : name the same inode even if the configured path is replaced. */
1190 0 : char proc_path[ PATH_MAX ];
1191 0 : FD_TEST( fd_cstr_printf_check( proc_path, sizeof(proc_path), NULL, "/proc/self/fd/%d", FD_STORE_FD_RW ) );
1192 0 : int store_ro_fd = open( proc_path, O_RDONLY|O_NOATIME );
1193 0 : if( FD_UNLIKELY( store_ro_fd<0 ) )
1194 0 : FD_LOG_ERR(( "failed to open Store backing file read-only (%i-%s)", errno, fd_io_strerror( errno ) ));
1195 0 : if( FD_LIKELY( store_ro_fd!=FD_STORE_FD_RO ) ) {
1196 0 : if( FD_UNLIKELY( dup2( store_ro_fd, FD_STORE_FD_RO )<0 ) ) FD_LOG_ERR(( "dup2(Store RO) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1197 0 : if( FD_UNLIKELY( close( store_ro_fd ) ) ) FD_LOG_ERR(( "close(Store RO source) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1198 0 : }
1199 0 : }
1200 :
1201 : /* Snapshot production prep
1202 : (On startup, reconcile with files in snapshot dirs, and drop old
1203 : files.) */
1204 :
1205 : static int
1206 0 : matches_partial_snap_filename( char const * name ) {
1207 0 : if( FD_UNLIKELY( !strcmp( name, ".snapshot.tar.bz2-partial" ) ||
1208 0 : !strcmp( name, ".incremental-snapshot.tar.bz2-partial" ) ) ) return 1;
1209 :
1210 0 : if( *name=='.' ) name++;
1211 0 : char const * p;
1212 0 : if( !strncmp( name, "snapshot-x", 10UL ) ) p = name+10UL;
1213 0 : else if( !strncmp( name, "snapshot", 8UL ) ) p = name+8UL;
1214 0 : else return 0;
1215 0 : if( *p<'0' || *p>'9' ) return 0;
1216 0 : while( *p>='0' && *p<='9' ) p++;
1217 0 : return 0==strcmp( p, ".partial" );
1218 0 : }
1219 :
1220 : static int
1221 : snap_inode_desc( void const * _a,
1222 0 : void const * _b ) {
1223 0 : fd_backup_inode_t const * a = (fd_backup_inode_t const *)_a;
1224 0 : fd_backup_inode_t const * b = (fd_backup_inode_t const *)_b;
1225 0 : ulong a_slot = a->incr_slot==ULONG_MAX ? a->full_slot : a->incr_slot;
1226 0 : ulong b_slot = b->incr_slot==ULONG_MAX ? b->full_slot : b->incr_slot;
1227 0 : if( FD_LIKELY( a_slot!=b_slot ) ) return a_slot>b_slot ? -1 : 1;
1228 0 : return 0;
1229 0 : }
1230 :
1231 : static void
1232 : drop_old_snaps( int snap_dir_fd,
1233 : char const * snap_dir,
1234 : fd_backup_inode_t * snaps,
1235 : ulong * snap_cnt_p,
1236 0 : ulong snap_max ) {
1237 0 : ulong snap_cnt = *snap_cnt_p;
1238 0 : if( snap_cnt<=snap_max ) return;
1239 : /* drop oldest */
1240 0 : qsort( snaps, snap_cnt, sizeof(fd_backup_inode_t), snap_inode_desc );
1241 0 : for( ulong i=snap_max; i<snap_cnt; i++ ) {
1242 0 : int err = unlinkat( snap_dir_fd, snaps[ i ].name, 0 );
1243 0 : if( FD_UNLIKELY( err && errno!=ENOENT ) ) {
1244 0 : FD_LOG_WARNING(( "unlinkat(%s/%s) failed (%i-%s)", snap_dir, snaps[ i ].name, errno, fd_io_strerror( errno ) ));
1245 0 : } else if( FD_LIKELY( !err ) ) {
1246 0 : FD_LOG_INFO(( "deleted old snapshot `%s/%s`", snap_dir, snaps[ i ].name ));
1247 0 : }
1248 0 : }
1249 0 : *snap_cnt_p = snap_max;
1250 0 : }
1251 :
1252 : static void
1253 : snap_check_fd( char const * snap_dir,
1254 : char const * name,
1255 0 : int snap_fd ) {
1256 0 : struct stat st;
1257 0 : if( FD_UNLIKELY( -1==fstat( snap_fd, &st ) ) )
1258 0 : FD_LOG_ERR(( "fstat(%s/%s) failed (%i-%s)", snap_dir, name, errno, fd_io_strerror( errno ) ));
1259 :
1260 0 : if( FD_UNLIKELY( !S_ISREG( st.st_mode ) ) )
1261 0 : FD_LOG_ERR(( "snapshot `%s/%s` is not a regular file", snap_dir, name ));
1262 0 : }
1263 :
1264 : static void
1265 : snap_reperm_fd( char const * snap_dir,
1266 : char const * name,
1267 : int snap_fd,
1268 : uint uid,
1269 0 : uint gid ) {
1270 0 : if( FD_UNLIKELY( -1==fchown( snap_fd, uid, gid ) ) )
1271 0 : FD_LOG_ERR(( "fchown(%s/%s) failed (%i-%s)", snap_dir, name, errno, fd_io_strerror( errno ) ));
1272 :
1273 0 : if( FD_UNLIKELY( -1==fchmod( snap_fd, S_IRUSR|S_IWUSR ) ) )
1274 0 : FD_LOG_ERR(( "fchmod(%s/%s) failed (%i-%s)", snap_dir, name, errno, fd_io_strerror( errno ) ));
1275 0 : }
1276 :
1277 : ulong
1278 0 : initialize_snapshot_fds( config_t const * config ) {
1279 :
1280 0 : int download_enabled = fd_topo_find_tile( &config->topo, "snapct", 0UL )!=ULONG_MAX &&
1281 0 : (config->firedancer.snapshots.sources.gossip.allow_any ||
1282 0 : config->firedancer.snapshots.sources.gossip.allow_list_cnt ||
1283 0 : config->firedancer.snapshots.sources.servers_cnt);
1284 0 : int upload_enabled = fd_topo_find_tile( &config->topo, "snapsv", 0UL )!=ULONG_MAX;
1285 0 : int dio_enabled = fd_topo_find_tile( &config->topo, "snapzp", 0UL )!=ULONG_MAX;
1286 0 : fd_snap_pool_layout_t layout = fd_snap_pool_layout( config->firedancer.snapshots.max_full_snapshots_to_keep,
1287 0 : config->firedancer.snapshots.max_incremental_snapshots_to_keep,
1288 0 : fd_wfs_needs_incr( config->firedancer.snapshots.incremental_snapshots,
1289 0 : config->firedancer.consensus.wait_for_supermajority_at_slot,
1290 0 : !strcmp( config->firedancer.consensus.wait_for_supermajority_with_bank_hash, "" ),
1291 0 : (ulong)config->consensus.expected_shred_version ),
1292 0 : download_enabled );
1293 0 : ulong snap_full_max = layout.full_max;
1294 0 : ulong snap_incr_max = layout.incr_max;
1295 0 : ulong retained_snap_max = layout.retained_max;
1296 0 : ulong snap_max = layout.max;
1297 0 : if( FD_UNLIKELY( !snap_max ) ) return 0UL;
1298 :
1299 0 : char const * snap_dir = config->paths.snapshots;
1300 0 : int dir_fd = open( snap_dir, O_RDONLY|O_DIRECTORY|O_CLOEXEC );
1301 0 : if( FD_UNLIKELY( -1==dir_fd ) ) FD_LOG_ERR(( "open(%s) failed (%i-%s)", snap_dir, errno, fd_io_strerror( errno ) ));
1302 :
1303 : /* This is additionally validated in fd_config_validatef() */
1304 0 : FD_CHECK_ERR( snap_max<=FD_SNAP_MAX, "[snapshots.max_{full,incremental}_snapshots_to_keep] is set too high" );
1305 0 : fd_backup_inode_t snap_full[ FD_SNAP_MAX ];
1306 0 : fd_backup_inode_t snap_incr[ FD_SNAP_MAX ];
1307 0 : fd_backup_inode_t scratch[ 2 ] = {
1308 0 : { .full_slot=ULONG_MAX, .incr_slot=ULONG_MAX },
1309 0 : { .full_slot=ULONG_MAX, .incr_slot=ULONG_MAX }
1310 0 : };
1311 0 : ulong snap_full_cnt = 0UL;
1312 0 : ulong snap_incr_cnt = 0UL;
1313 0 : FD_STATIC_ASSERT( sizeof(snap_full)+sizeof(snap_incr) < 2UL<<20, "stack overflow" );
1314 :
1315 : /* First, reconcile with the existing snapshots */
1316 0 : int dir_fd2 = dup( dir_fd );
1317 0 : if( FD_UNLIKELY( -1==dir_fd2 ) ) FD_LOG_ERR(( "dup(%s) failed (%i-%s)", snap_dir, errno, fd_io_strerror( errno ) ));
1318 0 : DIR * dir = fdopendir( dir_fd2 );
1319 0 : if( FD_UNLIKELY( !dir ) ) FD_LOG_ERR(( "fdopendir(%s) failed (%i-%s)", snap_dir, errno, fd_io_strerror( errno ) ));
1320 0 : struct dirent * entry;
1321 0 : for(;;) {
1322 0 : errno = 0;
1323 0 : entry = readdir( dir );
1324 0 : if( FD_UNLIKELY( !entry ) ) break;
1325 0 : if( FD_UNLIKELY( !strcmp( entry->d_name, ".") || !strcmp( entry->d_name, ".." ) ) ) continue;
1326 :
1327 : /* delete partial files */
1328 0 : if( FD_UNLIKELY( matches_partial_snap_filename( entry->d_name ) ) ) {
1329 0 : if( FD_UNLIKELY( -1==unlinkat( dir_fd, entry->d_name, 0 ) && errno!=ENOENT ) )
1330 0 : FD_LOG_ERR(( "unlinkat(%s/%s) failed (%i-%s)", snap_dir, entry->d_name, errno, fd_io_strerror( errno ) ));
1331 0 : continue;
1332 0 : }
1333 :
1334 : /* decode file name */
1335 0 : int is_zstd;
1336 0 : ulong entry_full_slot, entry_incremental_slot;
1337 0 : uchar decoded_hash[ FD_HASH_FOOTPRINT ];
1338 0 : if( FD_UNLIKELY( -1==fd_ssarchive_parse_filename( entry->d_name, &entry_full_slot, &entry_incremental_slot, decoded_hash, &is_zstd ) ) ) continue;
1339 0 : if( FD_UNLIKELY( strlen( entry->d_name )>=FD_SNAP_NAME_MAX ) ) continue;
1340 0 : fd_backup_inode_t inode = (fd_backup_inode_t){
1341 0 : .full_slot = entry_full_slot,
1342 0 : .incr_slot = entry_incremental_slot
1343 0 : };
1344 0 : fd_cstr_ncpy( inode.name, entry->d_name, FD_SNAP_NAME_MAX );
1345 :
1346 : /* register snapshot (and lazily delete snaps if there are too many)
1347 : the lazy drop must free a slot for the append below, even if
1348 : retention is configured at the hard capacity */
1349 0 : if( entry_incremental_slot==ULONG_MAX ) {
1350 0 : if( FD_UNLIKELY( snap_full_cnt>=FD_SNAP_MAX ) ) {
1351 0 : drop_old_snaps( dir_fd, snap_dir, snap_full, &snap_full_cnt, fd_ulong_min( snap_full_max, FD_SNAP_MAX-1UL ) );
1352 0 : }
1353 0 : snap_full[ snap_full_cnt++ ] = inode;
1354 0 : } else {
1355 0 : if( FD_UNLIKELY( snap_incr_cnt>=FD_SNAP_MAX ) ) {
1356 0 : drop_old_snaps( dir_fd, snap_dir, snap_incr, &snap_incr_cnt, fd_ulong_min( snap_incr_max, FD_SNAP_MAX-1UL ) );
1357 0 : }
1358 0 : snap_incr[ snap_incr_cnt++ ] = inode;
1359 0 : }
1360 0 : }
1361 0 : if( FD_UNLIKELY( errno ) ) FD_LOG_ERR(( "readdir(%s) failed (%i-%s)", snap_dir, errno, fd_io_strerror( errno ) ));
1362 0 : if( FD_UNLIKELY( -1==closedir( dir ) ) ) FD_LOG_ERR(( "closedir(%s) failed (%i-%s)", snap_dir, errno, fd_io_strerror( errno ) ));
1363 :
1364 : /* FIXME this should be smart enough to drop incremental snapshots
1365 : that no longer have a matching full snapshot. */
1366 0 : drop_old_snaps( dir_fd, snap_dir, snap_full, &snap_full_cnt, snap_full_max );
1367 0 : drop_old_snaps( dir_fd, snap_dir, snap_incr, &snap_incr_cnt, snap_incr_max );
1368 :
1369 : /* zero pad */
1370 0 : for( ulong i=snap_full_cnt; i<snap_full_max; i++ ) snap_full[ i ] = (fd_backup_inode_t){ .full_slot=ULONG_MAX, .incr_slot=ULONG_MAX };
1371 0 : for( ulong i=snap_incr_cnt; i<snap_incr_max; i++ ) snap_incr[ i ] = (fd_backup_inode_t){ .full_slot=ULONG_MAX, .incr_slot=ULONG_MAX };
1372 :
1373 0 : for( ulong i=0UL; i<snap_max; i++ ) {
1374 0 : fd_backup_inode_t * inode;
1375 0 : if( i<snap_full_max ) inode = &snap_full[ i ];
1376 0 : else if( i<retained_snap_max ) inode = &snap_incr[ i-snap_full_max ];
1377 0 : else inode = &scratch [ i-retained_snap_max ];
1378 :
1379 0 : int snap_fd;
1380 0 : if( FD_UNLIKELY( !inode->name[ 0 ] ) ) {
1381 0 : fd_snap_pool_partial_name( inode->name, (uint)i );
1382 0 : snap_fd = openat( dir_fd, inode->name, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC|O_NOFOLLOW, S_IRUSR|S_IWUSR );
1383 0 : if( FD_UNLIKELY( -1==snap_fd ) ) FD_LOG_ERR(( "openat(%s/%s) failed (%i-%s)", snap_dir, inode->name, errno, fd_io_strerror( errno ) ));
1384 0 : } else {
1385 0 : snap_fd = openat( dir_fd, inode->name, O_RDWR|O_CLOEXEC|O_NOFOLLOW );
1386 0 : if( FD_UNLIKELY( -1==snap_fd ) ) FD_LOG_ERR(( "openat(%s/%s) failed (%i-%s)", snap_dir, inode->name, errno, fd_io_strerror( errno ) ));
1387 0 : snap_check_fd( snap_dir, inode->name, snap_fd );
1388 0 : }
1389 :
1390 0 : snap_reperm_fd( snap_dir, inode->name, snap_fd, config->uid, config->gid );
1391 :
1392 0 : if( FD_UNLIKELY( -1==dup2( snap_fd, FD_SNAP_FD( i ) ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1393 0 : if( FD_UNLIKELY( -1==close( snap_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1394 :
1395 0 : if( dio_enabled ) {
1396 0 : int dio_fd = openat( dir_fd, inode->name, O_WRONLY|O_DIRECT );
1397 0 : if( FD_UNLIKELY( -1==dio_fd ) ) FD_LOG_ERR(( "openat(%s/%s, O_DIRECT) failed (%i-%s)", snap_dir, inode->name, errno, fd_io_strerror( errno ) ));
1398 0 : if( FD_UNLIKELY( -1==dup2( dio_fd, FD_SNAP_DIO_FD( i ) ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1399 0 : if( FD_UNLIKELY( -1==close( dio_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1400 0 : }
1401 :
1402 0 : if( upload_enabled ) {
1403 0 : int ro_fd = openat( dir_fd, inode->name, O_RDONLY|O_NOFOLLOW );
1404 0 : if( FD_UNLIKELY( -1==ro_fd ) ) FD_LOG_ERR(( "openat(%s/%s, O_RDONLY) failed (%i-%s)", snap_dir, inode->name, errno, fd_io_strerror( errno ) ));
1405 0 : if( FD_UNLIKELY( -1==dup2( ro_fd, FD_SNAP_RO_FD( i ) ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1406 0 : if( FD_UNLIKELY( -1==close( ro_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1407 0 : }
1408 0 : }
1409 :
1410 0 : if( FD_UNLIKELY( -1==close( dir_fd ) ) ) FD_LOG_ERR(( "close(%s) failed (%i-%s)", snap_dir, errno, fd_io_strerror( errno ) ));
1411 0 : return snap_max;
1412 0 : }
1413 :
1414 : /* The boot sequence is a little bit involved...
1415 :
1416 : A process tree is created that looks like,
1417 :
1418 : + main
1419 : +-- pidns
1420 : +-- tile 0
1421 : +-- tile 1
1422 : ...
1423 :
1424 : What we want is that if any process in the tree dies, all other
1425 : processes will also die. This is done as follows,
1426 :
1427 : (a) pidns is the init process of a PID namespace, so if it dies the
1428 : kernel will terminate the child processes.
1429 :
1430 : (b) main is the parent of pidns, so it can issue a waitpid() on the
1431 : child PID, and when it completes terminate itself.
1432 :
1433 : (c) pidns is the parent of tiles, so it could issue a waitpid() of
1434 : -1 to wait for any of them to terminate, but how would it know
1435 : if main has died?
1436 :
1437 : (d) main creates a pipe, and passes the write end to pidns. If main
1438 : dies, the pipe will be closed, and pidns will get a HUP on the
1439 : read end. Then pidns creates a pipe per child and passes the
1440 : write end to the child. If any of the children die, the pipe
1441 : will be closed, and pidns will get a HUP on the read end.
1442 :
1443 : Then pidns can call poll() on both the write end of the main
1444 : pipe and the read end of all the child pipes. If any of them
1445 : raises SIGHUP, then pidns knows that the parent or a child has
1446 : died, and it can terminate itself, which due to (a) and (b)
1447 : will kill all other processes. */
1448 : void
1449 : run_firedancer( config_t * config,
1450 : int parent_pipefd,
1451 0 : int init_workspaces ) {
1452 : /* dump the topology we are using to the output log */
1453 0 : fd_topo_print_log( 0, &config->topo );
1454 :
1455 0 : run_firedancer_init( config, init_workspaces, 1 );
1456 :
1457 0 : if( FD_UNLIKELY( close( 0 ) ) ) FD_LOG_ERR(( "close(0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1458 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 ) ));
1459 :
1460 0 : int pipefd;
1461 0 : pid_namespace = clone_firedancer( config, parent_pipefd, &pipefd );
1462 :
1463 : /* Print the location of the logfile on SIGINT or SIGTERM, and also
1464 : kill the child. They are connected by a pipe which the child is
1465 : polling so we don't strictly need to kill the child, but its helpful
1466 : to do that before printing the log location line, else it might
1467 : get interleaved due to timing windows in the shutdown. */
1468 0 : install_parent_signals();
1469 :
1470 0 : struct sock_filter seccomp_filter[ 128UL ];
1471 0 : populate_sock_filter_policy_main( 128UL, seccomp_filter, (uint)fd_log_private_logfile_fd(), (uint)pid_namespace );
1472 :
1473 0 : int allow_fds[ 4 ];
1474 0 : ulong allow_fds_cnt = 0;
1475 0 : allow_fds[ allow_fds_cnt++ ] = 2; /* stderr */
1476 0 : if( FD_LIKELY( fd_log_private_logfile_fd()!=-1 ) )
1477 0 : allow_fds[ allow_fds_cnt++ ] = fd_log_private_logfile_fd(); /* logfile, or maybe stdout */
1478 0 : allow_fds[ allow_fds_cnt++ ] = pipefd; /* read end of main pipe */
1479 0 : if( FD_UNLIKELY( parent_pipefd!=-1 ) )
1480 0 : allow_fds[ allow_fds_cnt++ ] = parent_pipefd; /* write end of parent pipe */
1481 :
1482 0 : if( FD_LIKELY( config->development.sandbox ) ) {
1483 0 : fd_sandbox_enter( config->uid,
1484 0 : config->gid,
1485 0 : 0,
1486 0 : 0,
1487 0 : 0,
1488 0 : 1, /* Keep controlling terminal for main so it can receive Ctrl+C */
1489 0 : 0,
1490 0 : 0UL,
1491 0 : 0UL,
1492 0 : 0UL,
1493 0 : 0UL,
1494 0 : allow_fds_cnt,
1495 0 : allow_fds,
1496 0 : sock_filter_policy_main_instr_cnt,
1497 0 : seccomp_filter );
1498 0 : } else {
1499 0 : fd_sandbox_switch_uid_gid( config->uid, config->gid );
1500 0 : }
1501 :
1502 : /* the only clean way to exit is SIGINT or SIGTERM on this parent process,
1503 : so if wait4() completes, it must be an error */
1504 0 : int wstatus;
1505 0 : if( FD_UNLIKELY( -1==wait4( pid_namespace, &wstatus, (int)__WALL, NULL ) ) )
1506 0 : FD_LOG_ERR(( "main wait4() failed (%i-%s)\nLog at \"%s\"", errno, fd_io_strerror( errno ), fd_log_private_path ));
1507 :
1508 0 : if( FD_UNLIKELY( WIFSIGNALED( wstatus ) ) ) fd_sys_util_exit_group( WTERMSIG( wstatus ) ? WTERMSIG( wstatus ) : 1 );
1509 0 : else fd_sys_util_exit_group( WEXITSTATUS( wstatus ) ? WEXITSTATUS( wstatus ) : 1 );
1510 0 : }
1511 :
1512 : void
1513 : run_cmd_fn( args_t * args FD_PARAM_UNUSED,
1514 0 : config_t * config ) {
1515 0 : #define CHECK_PORT_NON_ZERO( field ) \
1516 0 : if( FD_UNLIKELY( config->field==0 ) ) { \
1517 0 : FD_LOG_ERR(( #field " is not set in your configuration file. Please set it to a non-zero value." )); \
1518 0 : }
1519 :
1520 0 : if( FD_UNLIKELY( !config->gossip.entrypoints_cnt && !config->development.bootstrap ) )
1521 0 : FD_LOG_ERR(( "No entrypoints specified in configuration file under [gossip.entrypoints], but "
1522 0 : "at least one is needed to determine how to connect to the Solana cluster. If "
1523 0 : "you want to start a new cluster in a development environment, use `fddev` instead "
1524 0 : "of `fdctl`. If you want to use an existing genesis, set [development.bootstrap] "
1525 0 : "to \"true\" in the configuration file." ));
1526 :
1527 0 : for( ulong i=0; i<config->gossip.entrypoints_cnt; i++ ) {
1528 0 : if( FD_UNLIKELY( !strcmp( config->gossip.entrypoints[ i ], "" ) ) )
1529 0 : FD_LOG_ERR(( "One of the entrypoints in your configuration file under [gossip.entrypoints] is "
1530 0 : "empty. Please remove the empty entrypoint or set it correctly. "));
1531 0 : }
1532 :
1533 0 : CHECK_PORT_NON_ZERO( gossip.port );
1534 0 : CHECK_PORT_NON_ZERO( tiles.quic.quic_transaction_listen_port );
1535 0 : CHECK_PORT_NON_ZERO( tiles.quic.regular_transaction_listen_port );
1536 0 : CHECK_PORT_NON_ZERO( tiles.shred.shred_listen_port );
1537 0 : CHECK_PORT_NON_ZERO( tiles.metric.prometheus_listen_port );
1538 0 : CHECK_PORT_NON_ZERO( tiles.gui.gui_listen_port );
1539 :
1540 0 : #undef CHECK_PORT_NON_ZERO
1541 :
1542 0 : run_firedancer( config, -1, 1 );
1543 0 : }
1544 :
1545 : static void
1546 0 : run1_args_help( fd_action_help_t * help ) {
1547 0 : fd_action_help_arg( help, "<tile-name>", NULL, "Type of tile to run (e.g. `net`, `quic`, `replay`). A tile is a single\n"
1548 0 : "thread pinned to a CPU core that performs one part of the validator's work" );
1549 0 : fd_action_help_arg( help, "<kind-id>", NULL, "Zero-based index selecting which instance of that tile type to run\n"
1550 0 : "when the topology has more than one" );
1551 0 : fd_action_help_arg( help, "--pipe-fd", "<fd>", "Internal use: file descriptor over which the parent supervisor process\n"
1552 0 : "communicates with this tile (default -1, standalone)" );
1553 0 : }
1554 :
1555 : action_t fd_action_run1 = {
1556 : .name = "run1",
1557 : .args = run1_cmd_args,
1558 : .fn = run1_cmd_fn,
1559 : .perm = NULL,
1560 : .description = "Start up a single Firedancer tile",
1561 : .detail = "Runs one tile of the validator topology in the current process. A tile is a\n"
1562 : "single thread pinned to a CPU core that performs one part of the validator's\n"
1563 : "work. This is primarily an internal command used by `run` to spawn individual\n"
1564 : "tiles; most operators should use `run` instead.",
1565 : .usage = "run1 <tile-name> <kind-id> [OPTIONS]",
1566 : .args_help = run1_args_help,
1567 : };
1568 :
1569 : action_t fd_action_run = {
1570 : .name = "run",
1571 : .args = NULL,
1572 : .fn = run_cmd_fn,
1573 : .require_config = 1,
1574 : .perm = run_cmd_perm,
1575 : .description = "Start up a Firedancer validator",
1576 : .detail = "Boots and runs the full validator described by the configuration file. This\n"
1577 : "is the main command operators use to run Firedancer. It must be started with\n"
1578 : "sufficient privileges to perform boot-time setup, after which it drops\n"
1579 : "privileges to the configured user.",
1580 : .usage = "run [OPTIONS]",
1581 : .permission_err = "insufficient permissions to execute command `%s`. It is recommended "
1582 : "to start Firedancer as the root user, but you can also start it "
1583 : "with the missing capabilities listed above. The program only needs "
1584 : "to start with elevated permissions to do privileged operations at "
1585 : "boot, and will immediately drop permissions and switch to the user "
1586 : "specified in your configuration file once they are complete. Firedancer "
1587 : "will not execute outside of the boot process as root, and will refuse "
1588 : "to start if it cannot drop privileges. Firedancer needs to be started "
1589 : "privileged to configure high performance networking with XDP.",
1590 : };
|