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