Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fd_dev_boot.h"
3 :
4 : #include "../../shared/fd_config.h"
5 : #include "../../shared/fd_action.h"
6 : #include "../../shared/boot/fd_boot.h"
7 : #include "../../platform/fd_file_util.h"
8 :
9 : #include <errno.h>
10 : #include <unistd.h>
11 : #include <stdlib.h>
12 : #include <stdio.h>
13 : #include <sys/types.h>
14 : #include <sys/stat.h>
15 :
16 : extern char fd_log_private_path[ 1024 ];
17 :
18 : extern action_t * ACTIONS[];
19 :
20 0 : #define MAX_ARGC 32
21 :
22 : /* Rerun the currently executing process as root. This will never return,
23 : instead it replaces the currently executing process with a new one. */
24 : static void
25 : execve_as_root( int argc,
26 0 : char ** argv ) {
27 0 : char _current_executable_path[ PATH_MAX ];
28 0 : FD_TEST( -1!=fd_file_util_self_exe( _current_executable_path ) );
29 :
30 0 : char * args[ MAX_ARGC+4 ];
31 0 : for( int i=1; i<argc; i++ ) args[i+2] = argv[i];
32 0 : args[ 0 ] = "sudo";
33 0 : args[ 1 ] = "-E";
34 0 : args[ 2 ] = _current_executable_path;
35 : /* always override the log path to use the same one we just opened for ourselves */
36 0 : args[ argc+2 ] = "--log-path";
37 0 : args[ argc+3 ] = fd_log_private_path;
38 0 : args[ argc+4 ] = NULL;
39 :
40 : /* ok to leak these dynamic strings because we are about to execve anyway */
41 0 : char * envp[ 3 ] = {0};
42 0 : char * env;
43 0 : int idx = 0;
44 0 : if( FD_LIKELY(( env = getenv( "FIREDANCER_CONFIG_TOML" ) )) ) {
45 0 : if( FD_UNLIKELY( asprintf( &envp[ idx++ ], "FIREDANCER_CONFIG_TOML=%s", env ) == -1 ) )
46 0 : FD_LOG_ERR(( "asprintf() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
47 0 : }
48 0 : if( FD_LIKELY(( env = getenv( "TERM" ) )) ) {
49 0 : if( FD_UNLIKELY( asprintf( &envp[ idx++ ], "TERM=%s", env ) == -1 ) )
50 0 : FD_LOG_ERR(( "asprintf() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
51 0 : }
52 :
53 0 : execve( "/usr/bin/sudo", args, envp );
54 0 : FD_LOG_ERR(( "execve(sudo) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
55 0 : }
56 :
57 : config_t config;
58 :
59 : int
60 : fd_dev_main( int argc,
61 : char ** _argv,
62 : int is_firedancer,
63 : char const * default_config,
64 : ulong default_config_sz,
65 0 : void (* topo_init )( config_t * config ) ) {
66 : /* save original arguments list in case we need to respawn the process
67 : as privileged */
68 0 : int orig_argc = argc;
69 0 : char * orig_argv[ MAX_ARGC+1 ] = {0};
70 0 : for( int i=0; i<fd_int_min( MAX_ARGC, argc ); i++ ) orig_argv[ i ] = _argv[ i ];
71 :
72 0 : if( FD_UNLIKELY( argc >= MAX_ARGC ) ) FD_LOG_ERR(( "too many arguments (%i)", argc ));
73 0 : char ** argv = _argv;
74 :
75 0 : argc--; argv++;
76 :
77 0 : fd_env_strip_cmdline_cstr( &argc, &argv, "--log-level-stderr", NULL, NULL );
78 0 : char const * log_path = fd_env_strip_cmdline_cstr( &argc, &argv, "--log-path", NULL, NULL );
79 :
80 0 : int no_sandbox = fd_env_strip_cmdline_contains( &argc, &argv, "--no-sandbox" );
81 0 : int no_clone = fd_env_strip_cmdline_contains( &argc, &argv, "--no-clone" );
82 :
83 0 : const char * opt_user_config_path = fd_env_strip_cmdline_cstr(
84 0 : &argc,
85 0 : &argv,
86 0 : "--config",
87 0 : "FIREDANCER_CONFIG_TOML",
88 0 : NULL );
89 :
90 0 : const char * action_name = "dev";
91 0 : if( FD_LIKELY( argc > 0 && !strcmp( argv[ 0 ], "--version" ) ) ) {
92 0 : action_name = "version";
93 0 : argc--; argv++;
94 0 : } else if( FD_LIKELY( argc > 0 && !strcmp( argv[ 0 ], "--help" ) ) ) {
95 0 : action_name = "help";
96 0 : argc--; argv++;
97 0 : } else if( FD_UNLIKELY( argc > 0 && argv[ 0 ][ 0 ] != '-' ) ) {
98 0 : action_name = argv[ 0 ];
99 0 : argc--; argv++;
100 0 : }
101 :
102 0 : action_t * action = NULL;
103 0 : for( ulong i=0UL; ACTIONS[ i ]; i++ ) {
104 0 : if( FD_UNLIKELY( !strcmp( action_name, ACTIONS[ i ]->name ) ) ) {
105 0 : action = ACTIONS[ i ];
106 0 : if( FD_UNLIKELY( action->is_immediate ) ) {
107 0 : action->fn( NULL, NULL );
108 0 : return 0;
109 0 : }
110 0 : break;
111 0 : }
112 0 : }
113 :
114 0 : if( FD_UNLIKELY( !action ) ) {
115 0 : fprintf( stderr, "unknown subcommand `%s`\n", action_name );
116 0 : exit( 1 );
117 0 : }
118 :
119 0 : fd_main_init( &argc, &argv, &config, opt_user_config_path, is_firedancer, action->is_local_cluster, log_path, default_config, default_config_sz, topo_init );
120 :
121 0 : config.development.no_clone = config.development.no_clone || no_clone;
122 0 : config.development.sandbox = config.development.sandbox && !no_sandbox && !no_clone;
123 :
124 0 : int is_allowed_live = action->is_diagnostic==1;
125 0 : if( FD_UNLIKELY( config.is_live_cluster && !is_allowed_live ) )
126 0 : FD_LOG_ERR(( "The `fddev` command is for development and test environments but your "
127 0 : "configuration targets a live cluster. Use `fdctl` if this is a "
128 0 : "production environment" ));
129 :
130 0 : if( FD_LIKELY( action->topo ) ) action->topo( &config );
131 :
132 0 : args_t args = {0};
133 0 : if( FD_LIKELY( action->args ) ) action->args( &argc, &argv, &args );
134 0 : if( FD_UNLIKELY( argc ) ) FD_LOG_ERR(( "unknown argument `%s`", argv[ 0 ] ));
135 :
136 : /* Check if we are appropriately permissioned to run the desired
137 : command. */
138 0 : if( FD_LIKELY( action->perm ) ) {
139 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 ) ) );
140 0 : action->perm( &args, chk, &config );
141 0 : ulong err_cnt = fd_cap_chk_err_cnt( chk );
142 0 : if( FD_UNLIKELY( err_cnt ) ) {
143 0 : if( FD_UNLIKELY( !geteuid() ) ) {
144 0 : for( ulong i=0UL; i<err_cnt; i++ ) FD_LOG_WARNING(( "%s", fd_cap_chk_err( chk, i ) ));
145 0 : FD_LOG_ERR(( "insufficient permissions to execute command `%s` when running as root. "
146 0 : "fddev is likely being run with a reduced capability bounding set.", action_name ));
147 0 : }
148 0 : FD_LOG_INFO(( "insufficient permissions to execute command `%s`, rerunning as root", action_name ));
149 0 : execve_as_root( orig_argc, orig_argv );
150 0 : }
151 0 : }
152 :
153 : /* run the command */
154 0 : action->fn( &args, &config );
155 0 : return 0;
156 0 : }
|