0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-17 11:12:42 +00:00

detect the path the netdata-claim.sh script is in ()

This commit is contained in:
Costa Tsaousis 2023-07-26 19:55:53 +03:00 committed by GitHub
parent 85d122f8a6
commit cca0d2649c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 31 deletions

View file

@ -57,7 +57,8 @@ CLAIM_AGENT_RESPONSE claim_agent(const char *claiming_arguments, bool force, con
#ifndef DISABLE_CLOUD
int exit_code;
pid_t command_pid;
char command_buffer[CLAIMING_COMMAND_LENGTH + 1];
char command_exec_buffer[CLAIMING_COMMAND_LENGTH + 1];
char command_line_buffer[CLAIMING_COMMAND_LENGTH + 1];
FILE *fp_child_output, *fp_child_input;
// This is guaranteed to be set early in main via post_conf_load()
@ -76,42 +77,55 @@ CLAIM_AGENT_RESPONSE claim_agent(const char *claiming_arguments, bool force, con
if (proxy_type == PROXY_TYPE_SOCKS5 || proxy_type == PROXY_TYPE_HTTP)
snprintf(proxy_flag, CLAIMING_PROXY_LENGTH, "-proxy=\"%s\"", proxy_str);
snprintfz(command_buffer,
snprintfz(command_exec_buffer, CLAIMING_COMMAND_LENGTH,
"exec \"%s%snetdata-claim.sh\"",
netdata_exe_path[0] ? netdata_exe_path : "",
netdata_exe_path[0] ? "/" : ""
);
snprintfz(command_line_buffer,
CLAIMING_COMMAND_LENGTH,
"exec netdata-claim.sh %s -hostname=%s -id=%s -url=%s -noreload %s",
"%s %s -hostname=%s -id=%s -url=%s -noreload %s",
command_exec_buffer,
proxy_flag,
netdata_configured_hostname,
rrdb.localhost->machine_guid,
cloud_base_url,
claiming_arguments);
netdata_log_info("Executing agent claiming command 'netdata-claim.sh'");
fp_child_output = netdata_popen(command_buffer, &command_pid, &fp_child_input);
netdata_log_info("Executing agent claiming command: %s", command_exec_buffer);
fp_child_output = netdata_popen(command_line_buffer, &command_pid, &fp_child_input);
if(!fp_child_output) {
netdata_log_error("Cannot popen(\"%s\").", command_buffer);
netdata_log_error("Cannot popen(\"%s\").", command_exec_buffer);
return CLAIM_AGENT_CANNOT_EXECUTE_CLAIM_SCRIPT;
}
netdata_log_info("Waiting for claiming command to finish.");
while (fgets(command_buffer, CLAIMING_COMMAND_LENGTH, fp_child_output) != NULL) {;}
netdata_log_info("Waiting for claiming command '%s' to finish.", command_exec_buffer);
char read_buffer[100 + 1];
while (fgets(read_buffer, 100, fp_child_output) != NULL) {;}
exit_code = netdata_pclose(fp_child_input, fp_child_output, command_pid);
netdata_log_info("Agent claiming command returned with code %d", exit_code);
netdata_log_info("Agent claiming command '%s' returned with code %d", command_exec_buffer, exit_code);
if (0 == exit_code) {
load_claiming_state();
return CLAIM_AGENT_OK;
}
if (exit_code < 0) {
netdata_log_error("Agent claiming command failed to complete its run.");
netdata_log_error("Agent claiming command '%s' failed to complete its run", command_exec_buffer);
return CLAIM_AGENT_CLAIM_SCRIPT_FAILED;
}
errno = 0;
unsigned maximum_known_exit_code = sizeof(claiming_errors) / sizeof(claiming_errors[0]) - 1;
if ((unsigned)exit_code > maximum_known_exit_code) {
netdata_log_error("Agent failed to be claimed with an unknown error.");
netdata_log_error("Agent failed to be claimed with an unknown error. Cmd: '%s'", command_exec_buffer);
return CLAIM_AGENT_CLAIM_SCRIPT_RETURNED_INVALID_CODE;
}
netdata_log_error("Agent failed to be claimed with the following error message:");
netdata_log_error("Agent failed to be claimed using the command '%s' with the following error message:",
command_exec_buffer);
netdata_log_error("\"%s\"", claiming_errors[exit_code]);
if(msg) *msg = claiming_errors[exit_code];

View file

@ -407,7 +407,7 @@ static void pipe_write_cb(uv_write_t* req, int status)
uv_close((uv_handle_t *)client, pipe_close_cb);
--clients;
buffer_free(client->data);
netdata_log_info("Command Clients = %u\n", clients);
// netdata_log_info("Command Clients = %u", clients);
}
static inline void add_char_to_command_reply(BUFFER *reply_string, unsigned *reply_string_size, char character)
@ -557,7 +557,7 @@ static void pipe_read_cb(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf
if (nread < 0 && UV_EOF != nread) {
uv_close((uv_handle_t *)client, pipe_close_cb);
--clients;
netdata_log_info("Command Clients = %u\n", clients);
// netdata_log_info("Command Clients = %u", clients);
}
}
@ -593,7 +593,7 @@ static void connection_cb(uv_stream_t *server, int status)
}
++clients;
netdata_log_info("Command Clients = %u\n", clients);
// netdata_log_info("Command Clients = %u", clients);
/* Start parsing a new command */
cmd_ctx->command_string_size = 0;
cmd_ctx->command_string[0] = '\0';
@ -603,7 +603,7 @@ static void connection_cb(uv_stream_t *server, int status)
netdata_log_error("uv_read_start(): %s", uv_strerror(ret));
uv_close((uv_handle_t *)client, pipe_close_cb);
--clients;
netdata_log_info("Command Clients = %u\n", clients);
// netdata_log_info("Command Clients = %u", clients);
return;
}
}

View file

@ -4,11 +4,11 @@
#include <sched.h>
char pidfile[FILENAME_MAX + 1] = "";
char claimingdirectory[FILENAME_MAX + 1];
char exepath[FILENAME_MAX + 1];
char claiming_directory[FILENAME_MAX + 1];
char netdata_exe_path[FILENAME_MAX + 1];
char netdata_exe_file[FILENAME_MAX + 1];
void get_netdata_execution_path(void)
{
void get_netdata_execution_path(void) {
int ret;
size_t exepath_size = 0;
struct passwd *passwd = NULL;
@ -17,14 +17,18 @@ void get_netdata_execution_path(void)
passwd = getpwuid(getuid());
user = (passwd && passwd->pw_name) ? passwd->pw_name : "";
exepath_size = sizeof(exepath) - 1;
ret = uv_exepath(exepath, &exepath_size);
exepath_size = sizeof(netdata_exe_file) - 1;
ret = uv_exepath(netdata_exe_file, &exepath_size);
if (0 != ret) {
netdata_log_error("uv_exepath(\"%s\", %u) (user: %s) failed (%s).", exepath, (unsigned)exepath_size, user,
uv_strerror(ret));
netdata_log_error("uv_exepath(\"%s\", %u) (user: %s) failed (%s).", netdata_exe_file, (unsigned)exepath_size, user,
uv_strerror(ret));
fatal("Cannot start netdata without getting execution path.");
}
exepath[exepath_size] = '\0';
netdata_exe_file[exepath_size] = '\0';
strcpy(netdata_exe_path, netdata_exe_file);
dirname(netdata_exe_path);
}
static void chown_open_file(int fd, uid_t uid, gid_t gid) {
@ -99,7 +103,7 @@ void prepare_required_directories(uid_t uid, gid_t gid) {
change_dir_ownership(netdata_configured_varlib_dir, uid, gid, false);
change_dir_ownership(netdata_configured_lock_dir, uid, gid, false);
change_dir_ownership(netdata_configured_log_dir, uid, gid, false);
change_dir_ownership(claimingdirectory, uid, gid, false);
change_dir_ownership(claiming_directory, uid, gid, false);
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/registry", netdata_configured_varlib_dir);
@ -516,7 +520,7 @@ int become_daemon(int dont_fork, const char *user)
sched_setscheduler_set();
// Set claiming directory based on user config directory with correct ownership
snprintfz(claimingdirectory, FILENAME_MAX, "%s/cloud.d", netdata_configured_varlib_dir);
snprintfz(claiming_directory, FILENAME_MAX, "%s/cloud.d", netdata_configured_varlib_dir);
if(user && *user) {
if(become_user(user, pidfd) != 0) {

View file

@ -13,6 +13,7 @@ void send_statistics(const char *action, const char *action_result, const char *
void get_netdata_execution_path(void);
extern char pidfile[];
extern char exepath[];
extern char netdata_exe_file[];
extern char netdata_exe_path[];
#endif /* NETDATA_DAEMON_H */

View file

@ -199,12 +199,12 @@ int create_spawn_server(uv_loop_t *loop, uv_pipe_t *spawn_channel, uv_process_t
passwd = getpwuid(getuid());
user = (passwd && passwd->pw_name) ? passwd->pw_name : "";
args[0] = exepath;
args[0] = netdata_exe_file;
args[1] = SPAWN_SERVER_COMMAND_LINE_ARGUMENT;
args[2] = NULL;
memset(&options, 0, sizeof(options));
options.file = exepath;
options.file = netdata_exe_file;
options.args = args;
options.exit_cb = NULL; //exit_cb;
options.stdio = stdio;
@ -219,7 +219,7 @@ int create_spawn_server(uv_loop_t *loop, uv_pipe_t *spawn_channel, uv_process_t
ret = uv_spawn(loop, process, &options); /* execute the netdata binary again as the netdata user */
if (0 != ret) {
netdata_log_error("uv_spawn (process: \"%s\") (user: %s) failed (%s).", exepath, user, uv_strerror(ret));
netdata_log_error("uv_spawn (process: \"%s\") (user: %s) failed (%s).", netdata_exe_file, user, uv_strerror(ret));
fatal("Cannot start netdata without the spawn server.");
}