detect the path the netdata-claim.sh script is in (#15556)
Costa Tsaousis committed
Jul 26, 2023 at 19:55 UTC
cca0d2649c628a9fc4b9ee4502b501c821feac8b
5 files changed
+50
-31
claim/claim.c
+26
-12
@@ -57,7 +57,8 @@ CLAIM_AGENT_RESPONSE claim_agent(const char *claiming_arguments, bool force, con
57
#ifndef DISABLE_CLOUD
58
int exit_code;
59
pid_t command_pid;
60
- char command_buffer[CLAIMING_COMMAND_LENGTH + 1];
60
+ char command_exec_buffer[CLAIMING_COMMAND_LENGTH + 1];
61
+ char command_line_buffer[CLAIMING_COMMAND_LENGTH + 1];
62
FILE *fp_child_output, *fp_child_input;
63
64
// 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
77
if (proxy_type == PROXY_TYPE_SOCKS5 || proxy_type == PROXY_TYPE_HTTP)
78
snprintf(proxy_flag, CLAIMING_PROXY_LENGTH, "-proxy=\"%s\"", proxy_str);
79
79
- snprintfz(command_buffer,
80
+ snprintfz(command_exec_buffer, CLAIMING_COMMAND_LENGTH,
81
+ "exec \"%s%snetdata-claim.sh\"",
82
+ netdata_exe_path[0] ? netdata_exe_path : "",
83
+ netdata_exe_path[0] ? "/" : ""
84
+ );
85
+
86
+ snprintfz(command_line_buffer,
87
CLAIMING_COMMAND_LENGTH,
81
- "exec netdata-claim.sh %s -hostname=%s -id=%s -url=%s -noreload %s",
88
+ "%s %s -hostname=%s -id=%s -url=%s -noreload %s",
89
+ command_exec_buffer,
90
proxy_flag,
91
netdata_configured_hostname,
92
rrdb.localhost->machine_guid,
93
cloud_base_url,
94
claiming_arguments);
95
88
- netdata_log_info("Executing agent claiming command 'netdata-claim.sh'");
89
- fp_child_output = netdata_popen(command_buffer, &command_pid, &fp_child_input);
96
+ netdata_log_info("Executing agent claiming command: %s", command_exec_buffer);
97
+ fp_child_output = netdata_popen(command_line_buffer, &command_pid, &fp_child_input);
98
if(!fp_child_output) {
91
- netdata_log_error("Cannot popen(\"%s\").", command_buffer);
99
+ netdata_log_error("Cannot popen(\"%s\").", command_exec_buffer);
100
return CLAIM_AGENT_CANNOT_EXECUTE_CLAIM_SCRIPT;
101
}
94
- netdata_log_info("Waiting for claiming command to finish.");
95
- while (fgets(command_buffer, CLAIMING_COMMAND_LENGTH, fp_child_output) != NULL) {;}
102
+
103
+ netdata_log_info("Waiting for claiming command '%s' to finish.", command_exec_buffer);
104
+ char read_buffer[100 + 1];
105
+ while (fgets(read_buffer, 100, fp_child_output) != NULL) {;}
106
+
107
exit_code = netdata_pclose(fp_child_input, fp_child_output, command_pid);
97
- netdata_log_info("Agent claiming command returned with code %d", exit_code);
108
+
109
+ netdata_log_info("Agent claiming command '%s' returned with code %d", command_exec_buffer, exit_code);
110
if (0 == exit_code) {
111
load_claiming_state();
112
return CLAIM_AGENT_OK;
113
}
114
if (exit_code < 0) {
103
- netdata_log_error("Agent claiming command failed to complete its run.");
115
+ netdata_log_error("Agent claiming command '%s' failed to complete its run", command_exec_buffer);
116
return CLAIM_AGENT_CLAIM_SCRIPT_FAILED;
117
}
118
errno = 0;
119
unsigned maximum_known_exit_code = sizeof(claiming_errors) / sizeof(claiming_errors[0]) - 1;
120
121
if ((unsigned)exit_code > maximum_known_exit_code) {
110
- netdata_log_error("Agent failed to be claimed with an unknown error.");
122
+ netdata_log_error("Agent failed to be claimed with an unknown error. Cmd: '%s'", command_exec_buffer);
123
return CLAIM_AGENT_CLAIM_SCRIPT_RETURNED_INVALID_CODE;
124
}
125
114
- netdata_log_error("Agent failed to be claimed with the following error message:");
126
+ netdata_log_error("Agent failed to be claimed using the command '%s' with the following error message:",
127
+ command_exec_buffer);
128
+
129
netdata_log_error("\"%s\"", claiming_errors[exit_code]);
130
131
if(msg) *msg = claiming_errors[exit_code];
daemon/commands.c
+4
-4
@@ -407,7 +407,7 @@ static void pipe_write_cb(uv_write_t* req, int status)
407
uv_close((uv_handle_t *)client, pipe_close_cb);
408
--clients;
409
buffer_free(client->data);
410
- netdata_log_info("Command Clients = %u\n", clients);
410
+ // netdata_log_info("Command Clients = %u", clients);
411
}
412
413
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
557
if (nread < 0 && UV_EOF != nread) {
558
uv_close((uv_handle_t *)client, pipe_close_cb);
559
--clients;
560
- netdata_log_info("Command Clients = %u\n", clients);
560
+ // netdata_log_info("Command Clients = %u", clients);
561
}
562
}
563
@@ -593,7 +593,7 @@ static void connection_cb(uv_stream_t *server, int status)
593
}
594
595
++clients;
596
- netdata_log_info("Command Clients = %u\n", clients);
596
+ // netdata_log_info("Command Clients = %u", clients);
597
/* Start parsing a new command */
598
cmd_ctx->command_string_size = 0;
599
cmd_ctx->command_string[0] = '\0';
@@ -603,7 +603,7 @@ static void connection_cb(uv_stream_t *server, int status)
603
netdata_log_error("uv_read_start(): %s", uv_strerror(ret));
604
uv_close((uv_handle_t *)client, pipe_close_cb);
605
--clients;
606
- netdata_log_info("Command Clients = %u\n", clients);
606
+ // netdata_log_info("Command Clients = %u", clients);
607
return;
608
}
609
}
daemon/daemon.c
+15
-11
@@ -4,11 +4,11 @@
4
#include <sched.h>
5
6
char pidfile[FILENAME_MAX + 1] = "";
7
-char claimingdirectory[FILENAME_MAX + 1];
8
-char exepath[FILENAME_MAX + 1];
7
+char claiming_directory[FILENAME_MAX + 1];
8
+char netdata_exe_path[FILENAME_MAX + 1];
9
+char netdata_exe_file[FILENAME_MAX + 1];
10
10
-void get_netdata_execution_path(void)
11
-{
11
+void get_netdata_execution_path(void) {
12
int ret;
13
size_t exepath_size = 0;
14
struct passwd *passwd = NULL;
@@ -17,14 +17,18 @@ void get_netdata_execution_path(void)
17
passwd = getpwuid(getuid());
18
user = (passwd && passwd->pw_name) ? passwd->pw_name : "";
19
20
- exepath_size = sizeof(exepath) - 1;
21
- ret = uv_exepath(exepath, &exepath_size);
20
+ exepath_size = sizeof(netdata_exe_file) - 1;
21
+ ret = uv_exepath(netdata_exe_file, &exepath_size);
22
if (0 != ret) {
23
- netdata_log_error("uv_exepath(\"%s\", %u) (user: %s) failed (%s).", exepath, (unsigned)exepath_size, user,
24
- uv_strerror(ret));
23
+ netdata_log_error("uv_exepath(\"%s\", %u) (user: %s) failed (%s).", netdata_exe_file, (unsigned)exepath_size, user,
24
+ uv_strerror(ret));
25
fatal("Cannot start netdata without getting execution path.");
26
}
27
- exepath[exepath_size] = '\0';
27
+
28
+ netdata_exe_file[exepath_size] = '\0';
29
+
30
+ strcpy(netdata_exe_path, netdata_exe_file);
31
+ dirname(netdata_exe_path);
32
}
33
34
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) {
103
change_dir_ownership(netdata_configured_varlib_dir, uid, gid, false);
104
change_dir_ownership(netdata_configured_lock_dir, uid, gid, false);
105
change_dir_ownership(netdata_configured_log_dir, uid, gid, false);
102
- change_dir_ownership(claimingdirectory, uid, gid, false);
106
+ change_dir_ownership(claiming_directory, uid, gid, false);
107
108
char filename[FILENAME_MAX + 1];
109
snprintfz(filename, FILENAME_MAX, "%s/registry", netdata_configured_varlib_dir);
@@ -516,7 +520,7 @@ int become_daemon(int dont_fork, const char *user)
520
sched_setscheduler_set();
521
522
// Set claiming directory based on user config directory with correct ownership
519
- snprintfz(claimingdirectory, FILENAME_MAX, "%s/cloud.d", netdata_configured_varlib_dir);
523
+ snprintfz(claiming_directory, FILENAME_MAX, "%s/cloud.d", netdata_configured_varlib_dir);
524
525
if(user && *user) {
526
if(become_user(user, pidfd) != 0) {
daemon/daemon.h
+2
-1
@@ -13,6 +13,7 @@ void send_statistics(const char *action, const char *action_result, const char *
13
void get_netdata_execution_path(void);
14
15
extern char pidfile[];
16
-extern char exepath[];
16
+extern char netdata_exe_file[];
17
+extern char netdata_exe_path[];
18
19
#endif /* NETDATA_DAEMON_H */
spawn/spawn.c
+3
-3
@@ -199,12 +199,12 @@ int create_spawn_server(uv_loop_t *loop, uv_pipe_t *spawn_channel, uv_process_t
199
passwd = getpwuid(getuid());
200
user = (passwd && passwd->pw_name) ? passwd->pw_name : "";
201
202
- args[0] = exepath;
202
+ args[0] = netdata_exe_file;
203
args[1] = SPAWN_SERVER_COMMAND_LINE_ARGUMENT;
204
args[2] = NULL;
205
206
memset(&options, 0, sizeof(options));
207
- options.file = exepath;
207
+ options.file = netdata_exe_file;
208
options.args = args;
209
options.exit_cb = NULL; //exit_cb;
210
options.stdio = stdio;
@@ -219,7 +219,7 @@ int create_spawn_server(uv_loop_t *loop, uv_pipe_t *spawn_channel, uv_process_t
219
220
ret = uv_spawn(loop, process, &options); /* execute the netdata binary again as the netdata user */
221
if (0 != ret) {
222
- netdata_log_error("uv_spawn (process: \"%s\") (user: %s) failed (%s).", exepath, user, uv_strerror(ret));
222
+ netdata_log_error("uv_spawn (process: \"%s\") (user: %s) failed (%s).", netdata_exe_file, user, uv_strerror(ret));
223
fatal("Cannot start netdata without the spawn server.");
224
}
225