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