Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fddev.h"
3 :
4 : #include "genesis_hash.h"
5 :
6 : #include "../fdctl/configure/configure.h"
7 : #include "../fdctl/run/run.h"
8 :
9 : #include <stdio.h>
10 : #include <unistd.h>
11 : #include <sched.h>
12 : #include <fcntl.h>
13 : #include <pthread.h>
14 : #include <sys/wait.h>
15 :
16 : void
17 : dev_cmd_args( int * pargc,
18 : char *** pargv,
19 0 : args_t * args ) {
20 0 : args->dev.parent_pipefd = -1;
21 0 : args->dev.monitor = fd_env_strip_cmdline_contains( pargc, pargv, "--monitor" );
22 0 : args->dev.no_configure = fd_env_strip_cmdline_contains( pargc, pargv, "--no-configure" );
23 0 : args->dev.no_init_workspaces = fd_env_strip_cmdline_contains( pargc, pargv, "--no-init-workspaces" );
24 0 : args->dev.no_agave = fd_env_strip_cmdline_contains( pargc, pargv, "--no-agave" ) ||
25 0 : fd_env_strip_cmdline_contains( pargc, pargv, "--no-solana" );
26 0 : const char * debug_tile = fd_env_strip_cmdline_cstr( pargc, pargv, "--debug-tile", NULL, NULL );
27 0 : if( FD_UNLIKELY( debug_tile ) )
28 0 : strncpy( args->dev.debug_tile, debug_tile, sizeof( args->dev.debug_tile ) - 1 );
29 0 : }
30 :
31 : void
32 : dev_cmd_perm( args_t * args,
33 : fd_caps_ctx_t * caps,
34 0 : config_t * const config ) {
35 0 : if( FD_LIKELY( !args->dev.no_configure ) ) {
36 0 : args_t configure_args = {
37 0 : .configure.command = CONFIGURE_CMD_INIT,
38 0 : };
39 0 : for( ulong i=0; i<CONFIGURE_STAGE_COUNT; i++ )
40 0 : configure_args.configure.stages[ i ] = STAGES[ i ];
41 0 : configure_cmd_perm( &configure_args, caps, config );
42 0 : }
43 :
44 0 : run_cmd_perm( NULL, caps, config );
45 0 : }
46 :
47 : pid_t firedancer_pid, monitor_pid;
48 : extern char fd_log_private_path[ 1024 ]; /* empty string on start */
49 :
50 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)
51 :
52 : extern int * fd_log_private_shared_lock;
53 :
54 : static void
55 0 : parent_signal( int sig ) {
56 0 : if( FD_LIKELY( firedancer_pid ) ) kill( firedancer_pid, SIGINT );
57 0 : if( FD_LIKELY( monitor_pid ) ) kill( monitor_pid, SIGKILL );
58 :
59 : /* Same hack as in run.c, see comments there. */
60 0 : int lock = 0;
61 0 : fd_log_private_shared_lock = &lock;
62 :
63 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 ));
64 0 : else FD_LOG_ERR_NOEXIT(( "Received signal %s", fd_io_strsignal( sig ) ));
65 :
66 0 : if( FD_LIKELY( sig==SIGINT ) ) exit_group( 128+SIGINT );
67 0 : else exit_group( 0 );
68 0 : }
69 :
70 : static void
71 0 : install_parent_signals( void ) {
72 0 : struct sigaction sa = {
73 0 : .sa_handler = parent_signal,
74 0 : .sa_flags = 0,
75 0 : };
76 0 : if( FD_UNLIKELY( sigaction( SIGTERM, &sa, NULL ) ) )
77 0 : FD_LOG_ERR(( "sigaction(SIGTERM) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
78 0 : if( FD_UNLIKELY( sigaction( SIGINT, &sa, NULL ) ) )
79 0 : FD_LOG_ERR(( "sigaction(SIGINT) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
80 0 : }
81 :
82 :
83 : void
84 0 : update_config_for_dev( config_t * const config ) {
85 : /* By default only_known is true for validators to ensure secure
86 : snapshot download, but in development it doesn't matter and
87 : often the developer does not provide known peers. */
88 0 : config->rpc.only_known = 0;
89 :
90 : /* When starting from a new genesis block, this needs to be off else the
91 : validator will get stuck forever. */
92 0 : config->consensus.wait_for_vote_to_start_leader = 0;
93 :
94 : /* We have to wait until we get a snapshot before we can join a second
95 : validator to this one, so make this smaller than the default. */
96 0 : config->snapshots.full_snapshot_interval_slots = fd_uint_min( config->snapshots.full_snapshot_interval_slots, 200U );
97 :
98 : /* Automatically compute the shred version from genesis if it
99 : exists and we don't know it. If it doesn't exist, we'll keep it
100 : set to zero and get from gossip. */
101 0 : char genesis_path[ PATH_MAX ];
102 0 : FD_TEST( fd_cstr_printf_check( genesis_path, PATH_MAX, NULL, "%s/genesis.bin", config->ledger.path ) );
103 0 : ushort shred_version = compute_shred_version( genesis_path, NULL );
104 0 : for( ulong i=0UL; i<config->layout.shred_tile_count; i++ ) {
105 0 : ulong shred_id = fd_topo_find_tile( &config->topo, "shred", i );
106 0 : if( FD_UNLIKELY( shred_id==ULONG_MAX ) ) FD_LOG_ERR(( "could not find shred tile %lu", i ));
107 0 : fd_topo_tile_t * shred = &config->topo.tiles[ shred_id ];
108 0 : if( FD_LIKELY( shred->shred.expected_shred_version==(ushort)0 ) ) {
109 0 : shred->shred.expected_shred_version = shred_version;
110 0 : }
111 0 : }
112 0 : ulong store_id = fd_topo_find_tile( &config->topo, "storei", 0 );
113 0 : if( FD_UNLIKELY( store_id!=ULONG_MAX ) ) {
114 0 : fd_topo_tile_t * storei = &config->topo.tiles[ store_id ];
115 0 : if( FD_LIKELY( storei->store_int.expected_shred_version==(ushort)0 ) ) {
116 0 : storei->store_int.expected_shred_version = shred_version;
117 0 : }
118 0 : }
119 :
120 0 : if( FD_LIKELY( !strcmp( config->consensus.vote_account_path, "" ) ) )
121 0 : FD_TEST( fd_cstr_printf_check( config->consensus.vote_account_path,
122 0 : sizeof( config->consensus.vote_account_path ),
123 0 : NULL,
124 0 : "%s/vote-account.json",
125 0 : config->scratch_directory ) );
126 :
127 0 : ulong gui_idx = fd_topo_find_tile( &config->topo, "gui", 0UL );
128 0 : if( FD_LIKELY( gui_idx!=ULONG_MAX ) ) {
129 0 : fd_topo_tile_t * gui = &config->topo.tiles[ gui_idx ];
130 0 : gui->gui.is_voting = 1;
131 0 : if( FD_LIKELY( !strcmp( gui->gui.cluster, "unknown" ) ) ) {
132 0 : strcpy( gui->gui.cluster, "development" );
133 0 : }
134 0 : }
135 0 : }
136 :
137 : static void *
138 0 : agave_main1( void * args ) {
139 0 : agave_boot( args );
140 0 : return NULL;
141 0 : }
142 :
143 : /* Run Firedancer entirely in a single process for development and
144 : debugging convenience. */
145 :
146 : static void
147 0 : run_firedancer_threaded( config_t * config , int init_workspaces) {
148 0 : install_parent_signals();
149 :
150 0 : fd_topo_print_log( 0, &config->topo );
151 :
152 0 : run_firedancer_init( config, init_workspaces );
153 :
154 0 : if( FD_UNLIKELY( config->development.debug_tile ) ) {
155 0 : fd_log_private_shared_lock[ 1 ] = 1;
156 0 : }
157 :
158 : /* This is kind of a hack, but we have to join all the workspaces as read-write
159 : if we are running things threaded. The reason is that if one of the earlier
160 : tiles maps it in as read-only, later tiles will reuse the same cached shmem
161 : join (the key is only on shmem name, when it should be (name, mode)). */
162 :
163 0 : fd_xdp_fds_t fds = fd_topo_install_xdp( &config->topo );
164 0 : (void)fds;
165 :
166 0 : fd_topo_join_workspaces( &config->topo, FD_SHMEM_JOIN_MODE_READ_WRITE );
167 0 : fd_topo_run_single_process( &config->topo, 2, config->uid, config->gid, fdctl_tile_run, NULL );
168 :
169 0 : if( FD_LIKELY( !config->development.no_agave ) ) {
170 0 : pthread_t pthread;
171 0 : if( FD_UNLIKELY( pthread_create( &pthread, NULL, agave_main1, config ) ) ) FD_LOG_ERR(( "pthread_create() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
172 0 : if( FD_UNLIKELY( pthread_setname_np( pthread, "fdSolMain" ) ) ) FD_LOG_ERR(( "pthread_setname_np() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
173 0 : }
174 :
175 : /* None of the threads will ever exit, they just abort the process, so sleep forever. */
176 0 : for(;;) pause();
177 0 : }
178 :
179 : void
180 : dev_cmd_fn( args_t * args,
181 0 : config_t * const config ) {
182 0 : if( FD_LIKELY( !args->dev.no_configure ) ) {
183 0 : args_t configure_args = {
184 0 : .configure.command = CONFIGURE_CMD_INIT,
185 0 : };
186 0 : for( ulong i=0; i<CONFIGURE_STAGE_COUNT; i++ )
187 0 : configure_args.configure.stages[ i ] = STAGES[ i ];
188 0 : configure_cmd_fn( &configure_args, config );
189 0 : }
190 :
191 0 : update_config_for_dev( config );
192 0 : if( FD_UNLIKELY( args->dev.no_agave ) ) config->development.no_agave = 1;
193 :
194 0 : if( FD_UNLIKELY( config->development.netns.enabled ) ) {
195 : /* if we entered a network namespace during configuration, leave it
196 : so that `run_firedancer` starts from a clean namespace */
197 0 : leave_network_namespace();
198 0 : }
199 :
200 0 : if( FD_UNLIKELY( strcmp( "", args->dev.debug_tile ) ) ) {
201 0 : if( FD_LIKELY( config->development.sandbox ) ) {
202 0 : FD_LOG_WARNING(( "disabling sandbox to debug tile `%s`", args->dev.debug_tile ));
203 0 : config->development.sandbox = 0;
204 0 : }
205 :
206 0 : if( !strcmp( args->dev.debug_tile, "solana" ) ||
207 0 : !strcmp( args->dev.debug_tile, "agave" ) ) {
208 0 : config->development.debug_tile = UINT_MAX; /* Sentinel value representing Agave */
209 0 : } else {
210 0 : ulong tile_id = fd_topo_find_tile( &config->topo, args->dev.debug_tile, 0UL );
211 0 : if( FD_UNLIKELY( tile_id==ULONG_MAX ) ) FD_LOG_ERR(( "--debug-tile `%s` not present in topology", args->dev.debug_tile ));
212 0 : config->development.debug_tile = 1U+(uint)tile_id;
213 0 : }
214 0 : }
215 :
216 0 : if( FD_LIKELY( !args->dev.monitor ) ) {
217 0 : if( FD_LIKELY( !config->development.no_clone ) ) run_firedancer( config, args->dev.parent_pipefd, !args->dev.no_init_workspaces );
218 0 : else run_firedancer_threaded( config , !args->dev.no_init_workspaces);
219 0 : } else {
220 0 : install_parent_signals();
221 :
222 0 : int pipefd[2];
223 0 : if( FD_UNLIKELY( pipe2( pipefd, O_NONBLOCK ) ) ) FD_LOG_ERR(( "pipe2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
224 0 : initialize_workspaces(config);
225 0 : firedancer_pid = fork();
226 0 : if( !firedancer_pid ) {
227 0 : if( FD_UNLIKELY( close( pipefd[0] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
228 0 : if( FD_UNLIKELY( dup2( pipefd[1], STDERR_FILENO ) == -1 ) )
229 0 : FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
230 0 : if( FD_UNLIKELY( close( pipefd[1] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
231 0 : if( FD_UNLIKELY( setenv( "RUST_LOG_STYLE", "always", 1 ) ) ) /* otherwise RUST_LOG will not be colorized to the pipe */
232 0 : FD_LOG_ERR(( "setenv() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
233 0 : if( FD_LIKELY( !config->development.no_clone ) ) run_firedancer( config, -1, 0 );
234 0 : else run_firedancer_threaded( config , 0);
235 0 : } else {
236 0 : if( FD_UNLIKELY( close( pipefd[1] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
237 0 : }
238 :
239 0 : args_t monitor_args;
240 0 : int argc = 0;
241 0 : char * argv[] = { NULL };
242 0 : char ** pargv = (char**)argv;
243 0 : monitor_cmd_args( &argc, &pargv, &monitor_args );
244 0 : monitor_args.monitor.drain_output_fd = pipefd[0];
245 :
246 0 : monitor_pid = fork();
247 0 : if( !monitor_pid ) monitor_cmd_fn( &monitor_args, config );
248 0 : if( FD_UNLIKELY( close( pipefd[0] ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
249 :
250 0 : int wstatus;
251 0 : pid_t exited_pid = wait4( -1, &wstatus, (int)__WALL, NULL );
252 0 : if( FD_UNLIKELY( exited_pid == -1 ) ) FD_LOG_ERR(( "wait4() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
253 :
254 0 : char * exited_child = exited_pid == firedancer_pid ? "firedancer" : exited_pid == monitor_pid ? "monitor" : "unknown";
255 0 : int exit_code = 0;
256 0 : if( FD_UNLIKELY( !WIFEXITED( wstatus ) ) ) {
257 0 : FD_LOG_ERR(( "%s exited unexpectedly with signal %d (%s)", exited_child, WTERMSIG( wstatus ), fd_io_strsignal( WTERMSIG( wstatus ) ) ));
258 0 : exit_code = WTERMSIG( wstatus );
259 0 : } else {
260 0 : FD_LOG_ERR(( "%s exited unexpectedly with code %d", exited_child, WEXITSTATUS( wstatus ) ));
261 0 : if( FD_UNLIKELY( exited_pid == monitor_pid && !WEXITSTATUS( wstatus ) ) ) exit_code = EXIT_FAILURE;
262 0 : else exit_code = WEXITSTATUS( wstatus );
263 0 : }
264 :
265 0 : if( FD_UNLIKELY( exited_pid == monitor_pid ) ) {
266 0 : if( FD_UNLIKELY( kill( firedancer_pid, SIGKILL ) ) )
267 0 : FD_LOG_ERR(( "failed to kill all processes (%i-%s)", errno, fd_io_strerror( errno ) ));
268 0 : } else {
269 0 : if( FD_UNLIKELY( kill( monitor_pid, SIGKILL ) ) )
270 0 : FD_LOG_ERR(( "failed to kill all processes (%i-%s)", errno, fd_io_strerror( errno ) ));
271 0 : }
272 0 : exit_group( exit_code );
273 0 : }
274 0 : }
|