Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fd_boot.h"
3 :
4 : #include "../fd_config.h"
5 : #include "../fd_action.h"
6 : #include "../../platform/fd_file_util.h"
7 : #include "../../../disco/topo/fd_topo.h"
8 :
9 : #include <errno.h>
10 : #include <unistd.h>
11 : #include <fcntl.h>
12 : #include <sys/mman.h>
13 :
14 : extern action_t * ACTIONS[];
15 : extern fd_topo_run_tile_t * TILES[];
16 :
17 : extern int * fd_log_private_shared_lock;
18 :
19 : fd_topo_run_tile_t
20 0 : fdctl_tile_run( fd_topo_tile_t const * tile ) {
21 0 : for( ulong i=0UL; TILES[ i ]; i++ ) {
22 0 : if( !strcmp( tile->name, TILES[ i ]->name ) ) return *TILES[ i ];
23 0 : }
24 0 : FD_LOG_ERR(( "tile `%s` not found", tile->name ));
25 0 : return (fd_topo_run_tile_t){0};
26 0 : }
27 :
28 : static void
29 : copy_config_from_fd( int config_fd,
30 0 : config_t * config ) {
31 0 : uchar * bytes = mmap( NULL, sizeof( config_t ), PROT_READ, MAP_PRIVATE, config_fd, 0 );
32 0 : if( FD_UNLIKELY( bytes == MAP_FAILED ) ) FD_LOG_ERR(( "mmap() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
33 0 : fd_memcpy( config, bytes, sizeof( config_t ) );
34 0 : if( FD_UNLIKELY( munmap( bytes, sizeof( config_t ) ) ) ) FD_LOG_ERR(( "munmap() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
35 0 : if( FD_UNLIKELY( close( config_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
36 0 : }
37 :
38 : static int *
39 0 : map_log_memfd( int log_memfd ) {
40 0 : void * shmem = mmap( NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, log_memfd, (off_t)0 );
41 0 : if( FD_UNLIKELY( shmem==MAP_FAILED ) ) {
42 0 : FD_LOG_ERR(( "mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED,memfd,(off_t)0) (%i-%s); ", errno, fd_io_strerror( errno ) ));
43 0 : } else {
44 0 : if( FD_UNLIKELY( mlock( shmem, 4096 ) ) ) {
45 0 : FD_LOG_ERR(( "mlock(%p,4096) (%i-%s); unable to lock log file shared lock in memory\n", shmem, errno, fd_io_strerror( errno ) ));
46 0 : }
47 0 : }
48 0 : return shmem;
49 0 : }
50 :
51 : /* Try to allocate an anonymous page of memory in a file descriptor
52 : (memfd) for fd_log_private_shared_lock such that the log can strictly
53 : sequence messages written by clones of the caller made after the
54 : caller has finished booting the log. Must be a file descriptor so
55 : we can pass it through `execve` calls. */
56 : static int
57 0 : init_log_memfd( void ) {
58 0 : int memfd = memfd_create( "fd_log_lock_page", 0U );
59 0 : if( FD_UNLIKELY( -1==memfd) ) FD_LOG_ERR(( "memfd_create(\"fd_log_lock_page\",0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
60 0 : if( FD_UNLIKELY( -1==ftruncate( memfd, 4096 ) ) ) FD_LOG_ERR(( "ftruncate(memfd,4096) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
61 0 : return memfd;
62 0 : }
63 :
64 : static void
65 : determine_override_config( int * pargc,
66 : char *** pargv,
67 : fd_config_file_t * const * configs,
68 : char const ** override_config,
69 : char const ** override_config_path,
70 0 : ulong * override_config_sz ) {
71 0 : int testnet = fd_env_strip_cmdline_contains( pargc, pargv, "--testnet" );
72 0 : if( FD_UNLIKELY( testnet ) ) {
73 0 : for( ulong i=0UL; configs[ i ]; i++ ) {
74 0 : if( FD_UNLIKELY( !strcmp( configs[ i ]->name, "testnet" ) ) ) {
75 0 : *override_config = (char const *)configs[ i ]->data;
76 0 : *override_config_path = configs[ i ]->name;
77 0 : *override_config_sz = configs[ i ]->data_sz;
78 0 : return;
79 0 : }
80 0 : }
81 :
82 0 : if( FD_UNLIKELY( !*override_config ) ) FD_LOG_ERR(( "no testnet config found" ));
83 0 : }
84 :
85 0 : int devnet = fd_env_strip_cmdline_contains( pargc, pargv, "--devnet" );
86 0 : if( FD_UNLIKELY( devnet ) ) {
87 0 : if( FD_UNLIKELY( testnet ) ) FD_LOG_ERR(( "cannot specify both --testnet and --devnet" ));
88 0 : for( ulong i=0UL; configs[ i ]; i++ ) {
89 0 : if( FD_UNLIKELY( !strcmp( configs[ i ]->name, "devnet" ) ) ) {
90 0 : *override_config = (char const *)configs[ i ]->data;
91 0 : *override_config_path = configs[ i ]->name;
92 0 : *override_config_sz = configs[ i ]->data_sz;
93 0 : return;
94 0 : }
95 0 : }
96 :
97 0 : if( FD_UNLIKELY( !*override_config ) ) FD_LOG_ERR(( "no devnet config found" ));
98 0 : }
99 :
100 0 : int mainnet = fd_env_strip_cmdline_contains( pargc, pargv, "--mainnet" );
101 0 : if( FD_UNLIKELY( mainnet ) ) {
102 0 : if( FD_UNLIKELY( testnet || devnet ) ) FD_LOG_ERR(( "cannot specify both --testnet or --devnet and --mainnet" ));
103 0 : for( ulong i=0UL; configs[ i ]; i++ ) {
104 0 : if( FD_UNLIKELY( !strcmp( configs[ i ]->name, "mainnet" ) ) ) {
105 0 : *override_config = (char const *)configs[ i ]->data;
106 0 : *override_config_path = configs[ i ]->name;
107 0 : *override_config_sz = configs[ i ]->data_sz;
108 0 : return;
109 0 : }
110 0 : }
111 :
112 0 : if( FD_UNLIKELY( !*override_config ) ) FD_LOG_ERR(( "no mainnet config found" ));
113 0 : }
114 :
115 0 : int testnet_jito = fd_env_strip_cmdline_contains( pargc, pargv, "--testnet-jito" );
116 0 : if( FD_UNLIKELY( testnet_jito ) ) {
117 0 : for( ulong i=0UL; configs[ i ]; i++ ) {
118 0 : if( FD_UNLIKELY( !strcmp( configs[ i ]->name, "testnet-jito" ) ) ) {
119 0 : *override_config = (char const *)configs[ i ]->data;
120 0 : *override_config_path = configs[ i ]->name;
121 0 : *override_config_sz = configs[ i ]->data_sz;
122 0 : return;
123 0 : }
124 0 : }
125 :
126 0 : if( FD_UNLIKELY( !*override_config ) ) FD_LOG_ERR(( "no testnet-jito config found" ));
127 0 : }
128 :
129 0 : int mainnet_jito = fd_env_strip_cmdline_contains( pargc, pargv, "--mainnet-jito" );
130 0 : if( FD_UNLIKELY( mainnet_jito ) ) {
131 0 : for( ulong i=0UL; configs[ i ]; i++ ) {
132 0 : if( FD_UNLIKELY( !strcmp( configs[ i ]->name, "mainnet-jito" ) ) ) {
133 0 : *override_config = (char const *)configs[ i ]->data;
134 0 : *override_config_path = configs[ i ]->name;
135 0 : *override_config_sz = configs[ i ]->data_sz;
136 0 : return;
137 0 : }
138 0 : }
139 :
140 0 : if( FD_UNLIKELY( !*override_config ) ) FD_LOG_ERR(( "no mainnet-jito config found" ));
141 0 : }
142 0 : }
143 :
144 : int
145 : fd_main_init( int * pargc,
146 : char *** pargv,
147 : config_t * config,
148 : const char * opt_user_config_path,
149 : int is_firedancer,
150 : int is_local_cluster,
151 : char const * log_path,
152 0 : fd_config_file_t * const * configs ) {
153 0 : fd_log_enable_unclean_exit(); /* Don't call atexit handlers on FD_LOG_ERR */
154 0 : fd_log_level_core_set( 5 ); /* Don't dump core for FD_LOG_ERR during boot */
155 0 : fd_log_colorize_set( fd_log_should_colorize() ); /* Colorize during boot until we can determine from config */
156 0 : fd_log_level_stderr_set( 2 ); /* Only NOTICE and above will be logged during boot until fd_log is initialized */
157 :
158 0 : int config_fd = fd_env_strip_cmdline_int( pargc, pargv, "--config-fd", NULL, -1 );
159 :
160 0 : fd_memset( config, 0, sizeof( config_t ) );
161 0 : char * thread = "";
162 0 : if( FD_UNLIKELY( config_fd >= 0 ) ) {
163 0 : copy_config_from_fd( config_fd, config );
164 : /* tick_per_ns needs to be synchronized across processes so that
165 : they can coordinate on metrics measurement. */
166 0 : fd_tempo_set_tick_per_ns( config->tick_per_ns_mu, config->tick_per_ns_sigma );
167 0 : } else {
168 0 : char * user_config = NULL;
169 0 : ulong user_config_sz = 0UL;
170 0 : if( FD_LIKELY( opt_user_config_path ) ) {
171 0 : user_config = fd_file_util_read_all( opt_user_config_path, &user_config_sz );
172 0 : if( FD_UNLIKELY( user_config==MAP_FAILED ) ) FD_LOG_ERR(( "failed to read user config file `%s` (%d-%s)", opt_user_config_path, errno, fd_io_strerror( errno ) ));
173 0 : }
174 :
175 0 : char const * default_config = NULL;
176 0 : ulong default_config_sz = 0UL;
177 0 : for( ulong i=0UL; configs[ i ]; i++ ) {
178 0 : if( FD_UNLIKELY( !strcmp( configs[ i ]->name, "default" ) ) ) {
179 0 : default_config = (char const *)configs[ i ]->data;
180 0 : default_config_sz = configs[ i ]->data_sz;
181 0 : break;
182 0 : }
183 0 : }
184 0 : if( FD_UNLIKELY( !default_config ) ) FD_LOG_ERR(( "no default config found" ));
185 :
186 0 : char const * override_config = NULL;
187 0 : char const * override_config_path = NULL;
188 0 : ulong override_config_sz = 0UL;
189 0 : determine_override_config( pargc, pargv, configs,
190 0 : &override_config, &override_config_path, &override_config_sz );
191 :
192 0 : fd_config_load( is_firedancer, is_local_cluster, default_config, default_config_sz, override_config, override_config_path, override_config_sz, user_config, user_config_sz, opt_user_config_path, config );
193 :
194 0 : if( FD_UNLIKELY( user_config && -1==munmap( user_config, user_config_sz ) ) ) FD_LOG_ERR(( "munmap() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
195 :
196 0 : config->log.lock_fd = init_log_memfd();
197 0 : config->log.log_fd = -1;
198 0 : thread = "main";
199 0 : if( FD_UNLIKELY( log_path ) )
200 0 : strncpy( config->log.path, log_path, sizeof( config->log.path ) - 1 );
201 0 : }
202 :
203 0 : char * shmem_args[ 3 ];
204 : /* pass in --shmem-path value from the config */
205 0 : shmem_args[ 0 ] = "--shmem-path";
206 0 : shmem_args[ 1 ] = config->hugetlbfs.mount_path;
207 0 : shmem_args[ 2 ] = NULL;
208 0 : char ** argv = shmem_args;
209 0 : int argc = 2;
210 :
211 0 : int * log_lock = map_log_memfd( config->log.lock_fd );
212 0 : ulong pid = fd_sandbox_getpid(); /* Need to read /proc since we might be in a PID namespace now */;
213 :
214 0 : log_path = config->log.path;
215 0 : if( FD_LIKELY( config->log.path[ 0 ]=='\0' ) ) log_path = NULL;
216 :
217 : /* Switch to the sandbox uid/gid for log file creation, so it's always
218 : owned by that user. */
219 :
220 0 : gid_t gid = getgid();
221 0 : uid_t uid = getuid();
222 0 : if( FD_LIKELY( !gid && setegid( config->gid ) ) ) FD_LOG_ERR(( "setegid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
223 0 : if( FD_LIKELY( !uid && seteuid( config->uid ) ) ) FD_LOG_ERR(( "seteuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
224 :
225 0 : int boot_silent = config_fd>=0;
226 0 : fd_log_private_boot_custom( log_lock,
227 0 : 0UL,
228 0 : config->name,
229 0 : 0UL, /* Thread ID will be initialized later */
230 0 : thread, /* Thread will be initialized later */
231 0 : 0UL,
232 0 : config->hostname,
233 0 : fd_log_private_cpu_id_default(),
234 0 : NULL,
235 0 : pid,
236 0 : NULL,
237 0 : pid,
238 0 : config->uid,
239 0 : config->user,
240 0 : 1,
241 0 : config->log.colorize1,
242 0 : boot_silent ? 2 : config->log.level_logfile1,
243 0 : boot_silent ? 2 : config->log.level_stderr1,
244 0 : boot_silent ? 3 : config->log.level_flush1,
245 0 : 5,
246 0 : config->log.log_fd,
247 0 : log_path );
248 :
249 0 : if( FD_UNLIKELY( seteuid( uid ) ) ) FD_LOG_ERR(( "seteuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
250 0 : if( FD_UNLIKELY( setegid( gid ) ) ) FD_LOG_ERR(( "setegid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
251 :
252 0 : config->log.log_fd = fd_log_private_logfile_fd();
253 0 : fd_shmem_private_boot( &argc, &argv );
254 0 : fd_tile_private_boot( 0, NULL );
255 :
256 0 : fd_log_level_logfile_set( config->log.level_logfile1 );
257 0 : fd_log_level_stderr_set( config->log.level_stderr1 );
258 0 : fd_log_level_flush_set( config->log.level_flush1 );
259 :
260 0 : return config_fd<0;
261 0 : }
262 :
263 : static config_t config;
264 :
265 : int
266 : fd_main( int argc,
267 : char ** _argv,
268 : int is_firedancer,
269 : fd_config_file_t * const * configs,
270 0 : void (* topo_init )( config_t * config ) ) {
271 0 : char ** argv = _argv;
272 0 : argc--; argv++;
273 :
274 : /* Short circuit evaluating help and version commands so that we don't
275 : need to load and evaluate the entire config file to run them.
276 : This is useful for some operators in CI environments where, for
277 : example, they want to show the version or validate the produced
278 : binary without yet setting up the full TOML. */
279 :
280 0 : action_t * help_action = NULL;
281 0 : for( ulong i=0UL; ACTIONS[ i ]; i++ ) {
282 0 : if( FD_UNLIKELY( ACTIONS[ i ]->is_help ) ) {
283 0 : help_action = ACTIONS[ i ];
284 0 : break;
285 0 : }
286 0 : }
287 :
288 0 : if( FD_UNLIKELY( !argc ) ) {
289 0 : help_action->fn( NULL, NULL );
290 0 : FD_LOG_WARNING(( "no subcommand specified, exiting" ));
291 0 : return 1;
292 0 : }
293 :
294 : /* We need to strip away (potentially leading) cmdline flags first,
295 : since the parser assumes the action is the leading argument */
296 0 : const char * opt_user_config_path = fd_env_strip_cmdline_cstr(
297 0 : &argc,
298 0 : &argv,
299 0 : "--config",
300 0 : "FIREDANCER_CONFIG_TOML",
301 0 : NULL );
302 :
303 0 : action_t * action = NULL;
304 0 : for( ulong i=0UL; ACTIONS[ i ]; i++ ) {
305 0 : if( FD_UNLIKELY( !strcmp( argv[ 0 ], ACTIONS[ i ]->name ) ||
306 0 : (!strcmp( argv[ 0 ], "--version" ) && !strcmp( "version", ACTIONS[ i ]->name )) ||
307 0 : (!strcmp( argv[ 0 ], "--help" ) && !strcmp( "help", ACTIONS[ i ]->name ))
308 0 : ) ) {
309 0 : action = ACTIONS[ i ];
310 0 : if( FD_UNLIKELY( action->is_immediate ) ) {
311 0 : action->fn( NULL, NULL );
312 0 : return 0;
313 0 : }
314 0 : break;
315 0 : }
316 0 : }
317 :
318 0 : int is_local_cluster = action ? action->is_local_cluster : 0;
319 0 : int load_topo = fd_main_init( &argc, &argv, &config, opt_user_config_path, is_firedancer, is_local_cluster, NULL, configs );
320 0 : if( FD_LIKELY( load_topo ) ) topo_init( &config );
321 :
322 0 : if( FD_UNLIKELY( !action ) ) {
323 0 : help_action->fn( NULL, NULL );
324 0 : FD_LOG_ERR(( "unknown subcommand `%s`", argv[ 0 ] ));
325 0 : }
326 :
327 0 : if( FD_UNLIKELY( action->require_config && !opt_user_config_path ) ) FD_LOG_ERR(( "missing required `--config` argument" ));
328 :
329 0 : argc--; argv++;
330 :
331 0 : args_t args = {0};
332 0 : if( FD_LIKELY( action->args ) ) action->args( &argc, &argv, &args );
333 0 : if( FD_UNLIKELY( argc ) ) FD_LOG_ERR(( "unknown argument `%s`", argv[ 0 ] ));
334 :
335 0 : if( FD_LIKELY( action->perm ) ) {
336 0 : fd_cap_chk_t * chk = fd_cap_chk_join( fd_cap_chk_new( __builtin_alloca_with_align( fd_cap_chk_footprint(), FD_CAP_CHK_ALIGN ) ) );
337 :
338 0 : action->perm( &args, chk, &config );
339 :
340 0 : ulong err_cnt = fd_cap_chk_err_cnt( chk );
341 0 : if( FD_UNLIKELY( err_cnt ) ) {
342 0 : for( ulong i=0UL; i<err_cnt; i++ ) FD_LOG_WARNING(( "%s", fd_cap_chk_err( chk, i ) ));
343 :
344 0 : if( FD_LIKELY( action->permission_err ) ) FD_LOG_ERR(( action->permission_err, action->name ));
345 0 : else FD_LOG_ERR(( "insufficient permissions to execute command `%s`", action->name ));
346 0 : }
347 0 : }
348 :
349 0 : action->fn( &args, &config );
350 :
351 0 : return 0;
352 0 : }
|