Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fddev.h"
3 :
4 : #include "../fdctl/configure/configure.h"
5 :
6 : #include <unistd.h>
7 : #include <stdlib.h>
8 : #include <stdio.h>
9 : #include <sys/types.h>
10 : #include <sys/stat.h>
11 :
12 : extern configure_stage_t fd_cfg_stage_kill;
13 : extern configure_stage_t fd_cfg_stage_netns;
14 : extern configure_stage_t fd_cfg_stage_genesis;
15 : extern configure_stage_t fd_cfg_stage_blockstore;
16 : extern configure_stage_t fd_cfg_stage_keys;
17 :
18 : configure_stage_t * STAGES[ CONFIGURE_STAGE_COUNT ] = {
19 : &fd_cfg_stage_kill,
20 : &fd_cfg_stage_netns,
21 : &fd_cfg_stage_hugetlbfs,
22 : &fd_cfg_stage_sysctl,
23 : &fd_cfg_stage_ethtool_channels,
24 : &fd_cfg_stage_ethtool_gro,
25 : &fd_cfg_stage_keys,
26 : &fd_cfg_stage_genesis,
27 : #ifdef FD_HAS_NO_AGAVE
28 : NULL,
29 : #else
30 : &fd_cfg_stage_blockstore,
31 : #endif
32 : NULL,
33 : };
34 :
35 : extern fd_topo_run_tile_t fd_tile_net;
36 : extern fd_topo_run_tile_t fd_tile_quic;
37 : extern fd_topo_run_tile_t fd_tile_verify;
38 : extern fd_topo_run_tile_t fd_tile_dedup;
39 : extern fd_topo_run_tile_t fd_tile_resolv;
40 : extern fd_topo_run_tile_t fd_tile_pack;
41 : extern fd_topo_run_tile_t fd_tile_bank;
42 : extern fd_topo_run_tile_t fd_tile_poh;
43 : extern fd_topo_run_tile_t fd_tile_shred;
44 : extern fd_topo_run_tile_t fd_tile_store;
45 : extern fd_topo_run_tile_t fd_tile_sign;
46 : extern fd_topo_run_tile_t fd_tile_metric;
47 : extern fd_topo_run_tile_t fd_tile_cswtch;
48 : extern fd_topo_run_tile_t fd_tile_gui;
49 : extern fd_topo_run_tile_t fd_tile_plugin;
50 : extern fd_topo_run_tile_t fd_tile_blackhole;
51 : extern fd_topo_run_tile_t fd_tile_bencho;
52 : extern fd_topo_run_tile_t fd_tile_benchg;
53 : extern fd_topo_run_tile_t fd_tile_benchs;
54 :
55 : #ifdef FD_HAS_NO_AGAVE
56 : extern fd_topo_run_tile_t fd_tile_gossip;
57 : extern fd_topo_run_tile_t fd_tile_repair;
58 : extern fd_topo_run_tile_t fd_tile_store_int;
59 : extern fd_topo_run_tile_t fd_tile_replay;
60 : extern fd_topo_run_tile_t fd_tile_replay_thread;
61 : extern fd_topo_run_tile_t fd_tile_snaps_thread;
62 : extern fd_topo_run_tile_t fd_tile_poh_int;
63 : extern fd_topo_run_tile_t fd_tile_sender;
64 : extern fd_topo_run_tile_t fd_tile_eqvoc;
65 : extern fd_topo_run_tile_t fd_tile_rpcserv;
66 : extern fd_topo_run_tile_t fd_tile_snaps;
67 : #endif
68 :
69 : fd_topo_run_tile_t * TILES[] = {
70 : &fd_tile_net,
71 : &fd_tile_quic,
72 : &fd_tile_verify,
73 : &fd_tile_dedup,
74 : &fd_tile_resolv,
75 : &fd_tile_pack,
76 : &fd_tile_bank,
77 : &fd_tile_poh,
78 : &fd_tile_shred,
79 : &fd_tile_store,
80 : &fd_tile_sign,
81 : &fd_tile_metric,
82 : &fd_tile_cswtch,
83 : &fd_tile_gui,
84 : &fd_tile_plugin,
85 : &fd_tile_blackhole,
86 : &fd_tile_bencho,
87 : &fd_tile_benchg,
88 : &fd_tile_benchs,
89 : #ifdef FD_HAS_NO_AGAVE
90 : &fd_tile_gossip,
91 : &fd_tile_repair,
92 : &fd_tile_store_int,
93 : &fd_tile_replay,
94 : &fd_tile_replay_thread,
95 : &fd_tile_snaps_thread,
96 : &fd_tile_poh_int,
97 : &fd_tile_sender,
98 : &fd_tile_eqvoc,
99 : &fd_tile_rpcserv,
100 : &fd_tile_snaps,
101 : #endif
102 : NULL,
103 : };
104 :
105 : static action_t DEV_ACTIONS[] = {
106 : { .name = "dev", .args = dev_cmd_args, .fn = dev_cmd_fn, .perm = dev_cmd_perm },
107 : { .name = "wksp", .args = NULL, .fn = wksp_cmd_fn, .perm = wksp_cmd_perm },
108 : { .name = "dev1", .args = dev1_cmd_args, .fn = dev1_cmd_fn, .perm = dev_cmd_perm },
109 : { .name = "txn", .args = txn_cmd_args, .fn = txn_cmd_fn, .perm = txn_cmd_perm },
110 : { .name = "bench", .args = bench_cmd_args, .fn = bench_cmd_fn, .perm = bench_cmd_perm },
111 : { .name = "load", .args = load_cmd_args, .fn = load_cmd_fn, .perm = load_cmd_perm },
112 : { .name = "dump", .args = dump_cmd_args, .fn = dump_cmd_fn, .perm = NULL, .is_diagnostic=1 },
113 : { .name = "flame", .args = flame_cmd_args, .fn = flame_cmd_fn, .perm = flame_cmd_perm, .is_diagnostic=1 },
114 : { .name = "quic-trace", .args = quic_trace_cmd_args, .fn = quic_trace_cmd_fn, .perm = NULL, .is_diagnostic=1 },
115 : };
116 :
117 : extern char fd_log_private_path[ 1024 ];
118 :
119 0 : #define MAX_ARGC 32
120 :
121 : /* Rerun the currently executing process as root. This will never return,
122 : instead it replaces the currently executing process with a new one. */
123 : static void
124 : execve_as_root( int argc,
125 0 : char ** argv ) {
126 0 : char _current_executable_path[ PATH_MAX ];
127 0 : current_executable_path( _current_executable_path );
128 :
129 0 : char * args[ MAX_ARGC+4 ];
130 0 : for( int i=1; i<argc; i++ ) args[i+2] = argv[i];
131 0 : args[ 0 ] = "sudo";
132 0 : args[ 1 ] = "-E";
133 0 : args[ 2 ] = _current_executable_path;
134 : /* always override the log path to use the same one we just opened for ourselves */
135 0 : args[ argc+2 ] = "--log-path";
136 0 : args[ argc+3 ] = fd_log_private_path;
137 0 : args[ argc+4 ] = NULL;
138 :
139 : /* ok to leak these dynamic strings because we are about to execve anyway */
140 0 : char * envp[ 3 ] = {0};
141 0 : char * env;
142 0 : int idx = 0;
143 0 : if( FD_LIKELY(( env = getenv( "FIREDANCER_CONFIG_TOML" ) )) ) {
144 0 : if( FD_UNLIKELY( asprintf( &envp[ idx++ ], "FIREDANCER_CONFIG_TOML=%s", env ) == -1 ) )
145 0 : FD_LOG_ERR(( "asprintf() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
146 0 : }
147 0 : if( FD_LIKELY(( env = getenv( "TERM" ) )) ) {
148 0 : if( FD_UNLIKELY( asprintf( &envp[ idx++ ], "TERM=%s", env ) == -1 ) )
149 0 : FD_LOG_ERR(( "asprintf() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
150 0 : }
151 :
152 0 : execve( "/usr/bin/sudo", args, envp );
153 0 : FD_LOG_ERR(( "execve(sudo) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
154 0 : }
155 :
156 : static config_t config;
157 :
158 : int
159 : fddev_main( int argc,
160 0 : char ** _argv ) {
161 : /* save original arguments list in case we need to respawn the process
162 : as privileged */
163 0 : int orig_argc = argc;
164 0 : char * orig_argv[ MAX_ARGC+1 ] = {0};
165 0 : for( int i=0; i<fd_int_min( MAX_ARGC, argc ); i++ ) orig_argv[ i ] = _argv[ i ];
166 :
167 0 : if( FD_UNLIKELY( argc >= MAX_ARGC ) ) FD_LOG_ERR(( "too many arguments (%i)", argc ));
168 0 : char ** argv = _argv;
169 :
170 0 : argc--; argv++;
171 :
172 0 : fd_env_strip_cmdline_cstr( &argc, &argv, "--log-level-stderr", NULL, NULL );
173 0 : char const * log_path = fd_env_strip_cmdline_cstr( &argc, &argv, "--log-path", NULL, NULL );
174 :
175 0 : fdctl_boot( &argc, &argv, &config, log_path );
176 :
177 0 : int no_sandbox = fd_env_strip_cmdline_contains( &argc, &argv, "--no-sandbox" );
178 0 : int no_clone = fd_env_strip_cmdline_contains( &argc, &argv, "--no-clone" );
179 0 : config.development.no_clone = config.development.no_clone || no_clone;
180 0 : config.development.sandbox = config.development.sandbox && !no_sandbox && !no_clone;
181 :
182 0 : const char * action_name = "dev";
183 0 : if( FD_UNLIKELY( argc > 0 && argv[ 0 ][ 0 ] != '-' ) ) {
184 0 : action_name = argv[ 0 ];
185 0 : argc--; argv++;
186 0 : }
187 :
188 0 : action_t * action = NULL;
189 0 : for( ulong i=0; i<sizeof(ACTIONS)/sizeof(ACTIONS[ 0 ]); i++ ) {
190 0 : if( FD_UNLIKELY( !strcmp( action_name, ACTIONS[ i ].name ) ) ) {
191 0 : action = &ACTIONS[ i ];
192 0 : break;
193 0 : }
194 0 : }
195 0 : for( ulong i=0; i<sizeof(DEV_ACTIONS)/sizeof(DEV_ACTIONS[ 0 ]); i++ ) {
196 0 : if( FD_UNLIKELY( !strcmp( action_name, DEV_ACTIONS[ i ].name ) ) ) {
197 0 : action = &DEV_ACTIONS[ i ];
198 0 : break;
199 0 : }
200 0 : }
201 :
202 0 : if( FD_UNLIKELY( !action ) ) FD_LOG_ERR(( "unknown subcommand `%s`", action_name ));
203 :
204 0 : int is_allowed_live = action->is_diagnostic==1;
205 0 : if( FD_UNLIKELY( config.is_live_cluster && !is_allowed_live ) )
206 0 : FD_LOG_ERR(( "The `fddev` command is for development and test environments but your "
207 0 : "configuration targets a live cluster. Use `fdctl` if this is a "
208 0 : "production environment" ));
209 :
210 0 : args_t args = {0};
211 0 : if( FD_LIKELY( action->args ) ) action->args( &argc, &argv, &args );
212 0 : if( FD_UNLIKELY( argc ) ) FD_LOG_ERR(( "unknown argument `%s`", argv[ 0 ] ));
213 :
214 : /* Check if we are appropriately permissioned to run the desired
215 : command. */
216 0 : if( FD_LIKELY( action->perm ) ) {
217 0 : fd_caps_ctx_t caps[1] = {0};
218 0 : action->perm( &args, caps, &config );
219 0 : if( FD_UNLIKELY( caps->err_cnt ) ) {
220 0 : if( FD_UNLIKELY( !geteuid() ) ) {
221 0 : for( ulong i=0; i<caps->err_cnt; i++ ) FD_LOG_WARNING(( "%s", caps->err[ i ] ));
222 0 : FD_LOG_ERR(( "insufficient permissions to execute command `%s` when running as root. "
223 0 : "fddev is likely being run with a reduced capability bounding set.", action_name ));
224 0 : }
225 0 : execve_as_root( orig_argc, orig_argv );
226 0 : }
227 0 : }
228 :
229 : /* run the command */
230 0 : action->fn( &args, &config );
231 0 : return 0;
232 0 : }
|