fixes identified by coverity
Costa Tsaousis (ktsaou) committed
Mar 1, 2017 at 02:45 UTC
a4be7b4e24233a66f47a428b0cd256993328352b
5 files changed
+62
-38
src/plugin_tc.c
+1
-1
@@ -1003,7 +1003,7 @@ void *tc_main(void *ptr) {
1003
rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
1004
rrdset_done(stcpu);
1005
1006
- if(unlikely(!sttime)) stcpu = rrdset_find_localhost("netdata.plugin_tc_time");
1006
+ if(unlikely(!sttime)) sttime = rrdset_find_localhost("netdata.plugin_tc_time");
1007
if(unlikely(!sttime)) {
1008
sttime = rrdset_create_localhost("netdata", "plugin_tc_time", NULL, "tc.helper", NULL
1009
, "NetData TC script execution", "milliseconds/run", 135001
src/rrdcalctemplate.c
+2
@@ -23,6 +23,8 @@ void rrdcalctemplate_link_matching(RRDSET *st) {
23
}
24
25
inline void rrdcalctemplate_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
26
+ if(unlikely(!rt)) return;
27
+
28
debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
29
30
if(host->templates == rt) {
src/rrdhost.c
+4
-2
@@ -409,6 +409,10 @@ void rrdhost_free(RRDHOST *host) {
409
info("Freeing all memory for host '%s'...", host->hostname);
410
411
rrd_check_wrlock(); // make sure the RRDs are write locked
412
+
413
+ // stop a possibly running thread
414
+ rrdpush_sender_thread_stop(host);
415
+
416
rrdhost_wrlock(host); // lock this RRDHOST
417
418
// ------------------------------------------------------------------------
@@ -447,8 +451,6 @@ void rrdhost_free(RRDHOST *host) {
451
// ------------------------------------------------------------------------
452
// free it
453
450
- rrdpush_sender_thread_stop(host);
451
-
454
freez(host->os);
455
freez(host->cache_dir);
456
freez(host->varlib_dir);
src/rrdpush.c
+52
-32
@@ -185,31 +185,18 @@ static void rrdpush_sender_thread_reset_all_charts(RRDHOST *host) {
185
186
static inline void rrdpush_sender_thread_data_flush(RRDHOST *host) {
187
rrdpush_lock(host);
188
+
189
if(buffer_strlen(host->rrdpush_buffer))
190
error("STREAM %s [send]: discarding %zu bytes of metrics already in the buffer.", host->hostname, buffer_strlen(host->rrdpush_buffer));
191
192
buffer_flush(host->rrdpush_buffer);
192
- rrdpush_sender_thread_reset_all_charts(host);
193
- rrdpush_unlock(host);
194
-}
195
-
196
-static inline void rrdpush_sender_thread_lock(RRDHOST *host) {
197
- if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
198
- error("STREAM %s [send]: cannot set pthread cancel state to DISABLE.", host->hostname);
193
200
- rrdpush_lock(host);
201
-}
202
-
203
-static inline void rrdpush_sender_thread_unlock(RRDHOST *host) {
204
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
205
- error("STREAM %s [send]: cannot set pthread cancel state to DISABLE.", host->hostname);
194
+ rrdpush_sender_thread_reset_all_charts(host);
195
196
rrdpush_unlock(host);
197
}
198
210
-static void rrdpush_sender_thread_cleanup(RRDHOST *host) {
211
- rrdpush_lock(host);
212
-
199
+static void rrdpush_sender_thread_cleanup_locked_all(RRDHOST *host) {
200
host->rrdpush_connected = 0;
201
202
if(host->rrdpush_socket != -1) {
@@ -232,18 +219,20 @@ static void rrdpush_sender_thread_cleanup(RRDHOST *host) {
219
host->rrdpush_buffer = NULL;
220
221
host->rrdpush_spawn = 0;
235
-
236
- rrdpush_unlock(host);
222
}
223
224
void rrdpush_sender_thread_stop(RRDHOST *host) {
240
- rrdhost_check_wrlock(host);
225
+ rrdpush_lock(host);
226
+ rrdhost_wrlock(host);
227
228
if(host->rrdpush_spawn) {
229
info("STREAM %s [send]: stopping sending thread...", host->hostname);
230
pthread_cancel(host->rrdpush_thread);
245
- rrdpush_sender_thread_cleanup(host);
231
+ rrdpush_sender_thread_cleanup_locked_all(host);
232
}
233
+
234
+ rrdhost_unlock(host);
235
+ rrdpush_unlock(host);
236
}
237
238
void *rrdpush_sender_thread(void *ptr) {
@@ -398,7 +387,19 @@ void *rrdpush_sender_thread(void *ptr) {
387
}
388
389
if(ofd->revents & POLLOUT && begin < buffer_strlen(host->rrdpush_buffer)) {
401
- rrdpush_sender_thread_lock(host);
390
+
391
+ // BEGIN RRDPUSH LOCKED SESSION
392
+
393
+ // during this session, data collectors
394
+ // will not be able to append data to our buffer
395
+ // but the socket is in non-blocking mode
396
+ // so, we will not block at send()
397
+
398
+ if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
399
+ error("STREAM %s [send]: cannot set pthread cancel state to DISABLE.", host->hostname);
400
+
401
+ rrdpush_lock(host);
402
+
403
ssize_t ret = send(host->rrdpush_socket, &host->rrdpush_buffer->buffer[begin], buffer_strlen(host->rrdpush_buffer) - begin, MSG_DONTWAIT);
404
if(ret == -1) {
405
if(errno != EAGAIN && errno != EINTR) {
@@ -412,11 +413,19 @@ void *rrdpush_sender_thread(void *ptr) {
413
sent_bytes += ret;
414
begin += ret;
415
if(begin == buffer_strlen(host->rrdpush_buffer)) {
416
+ // we send it all
417
+
418
buffer_flush(host->rrdpush_buffer);
419
begin = 0;
420
}
421
}
419
- rrdpush_sender_thread_unlock(host);
422
+
423
+ rrdpush_unlock(host);
424
+
425
+ if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
426
+ error("STREAM %s [send]: cannot set pthread cancel state to ENABLE.", host->hostname);
427
+
428
+ // END RRDPUSH LOCKED SESSION
429
}
430
431
// protection from overflow
@@ -433,7 +442,11 @@ void *rrdpush_sender_thread(void *ptr) {
442
cleanup:
443
debug(D_WEB_CLIENT, "STREAM %s [send]: sending thread exits.", host->hostname);
444
436
- rrdpush_sender_thread_cleanup(host);
445
+ rrdpush_lock(host);
446
+ rrdhost_wrlock(host);
447
+ rrdpush_sender_thread_cleanup_locked_all(host);
448
+ rrdhost_unlock(host);
449
+ rrdpush_unlock(host);
450
451
pthread_exit(NULL);
452
return NULL;
@@ -534,6 +547,7 @@ int rrdpush_receive(int fd, const char *key, const char *hostname, const char *m
547
info("STREAM %s [receive from [%s]:%s]: initializing communication...", host->hostname, client_ip, client_port);
548
if(send_timeout(fd, START_STREAMING_PROMPT, strlen(START_STREAMING_PROMPT), 0, 60) != strlen(START_STREAMING_PROMPT)) {
549
error("STREAM %s [receive from [%s]:%s]: cannot send ready command.", host->hostname, client_ip, client_port);
550
+ close(fd);
551
return 0;
552
}
553
@@ -545,6 +559,7 @@ int rrdpush_receive(int fd, const char *key, const char *hostname, const char *m
559
FILE *fp = fdopen(fd, "r");
560
if(!fp) {
561
error("STREAM %s [receive from [%s]:%s]: failed to get a FILE for FD %d.", host->hostname, client_ip, client_port, fd);
562
+ close(fd);
563
return 0;
564
}
565
@@ -566,11 +581,11 @@ int rrdpush_receive(int fd, const char *key, const char *hostname, const char *m
581
host->health_enabled = 0;
582
583
host->senders_disconnected_time = now_realtime_sec();
569
-
570
- rrdpush_sender_thread_stop(host);
584
}
585
rrdhost_unlock(host);
586
587
+ rrdpush_sender_thread_stop(host);
588
+
589
// cleanup
590
fclose(fp);
591
@@ -602,7 +617,6 @@ void *rrdpush_receiver_thread(void *ptr) {
617
rrdpush_receive(rpt->fd, rpt->key, rpt->hostname, rpt->machine_guid, rpt->os, rpt->update_every, rpt->client_ip, rpt->client_port);
618
info("STREAM %s [receive from [%s]:%s]: receive thread ended (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
619
605
- close(rpt->fd);
620
freez(rpt->key);
621
freez(rpt->hostname);
622
freez(rpt->machine_guid);
@@ -616,13 +630,19 @@ void *rrdpush_receiver_thread(void *ptr) {
630
}
631
632
void rrdpush_sender_thread_spawn(RRDHOST *host) {
619
- if(pthread_create(&host->rrdpush_thread, NULL, rrdpush_sender_thread, (void *)host))
620
- error("STREAM %s [send]: failed to create new thread for client.", host->hostname);
633
+ rrdhost_wrlock(host);
634
+
635
+ if(!host->rrdpush_spawn) {
636
+ if(pthread_create(&host->rrdpush_thread, NULL, rrdpush_sender_thread, (void *) host))
637
+ error("STREAM %s [send]: failed to create new thread for client.", host->hostname);
638
622
- else if(pthread_detach(host->rrdpush_thread))
623
- error("STREAM %s [send]: cannot request detach newly created thread.", host->hostname);
639
+ else if(pthread_detach(host->rrdpush_thread))
640
+ error("STREAM %s [send]: cannot request detach newly created thread.", host->hostname);
641
625
- host->rrdpush_spawn = 1;
642
+ host->rrdpush_spawn = 1;
643
+ }
644
+
645
+ rrdhost_unlock(host);
646
}
647
648
int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url) {
@@ -630,7 +650,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
650
651
info("STREAM [receive from [%s]:%s]: new client connection.", w->client_ip, w->client_port);
652
633
- char *key = NULL, *hostname = NULL, *machine_guid = NULL, *os = NULL;
653
+ char *key = NULL, *hostname = NULL, *machine_guid = NULL, *os = "unknown";
654
int update_every = default_rrd_update_every;
655
char buf[GUID_LEN + 1];
656
src/sys_fs_cgroup.c
+3
-3
@@ -466,8 +466,8 @@ static inline void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
466
467
unsigned long i = procfile_linewords(ff, 0);
468
if(unlikely(i == 0)) {
469
- return;
469
ca->updated = 0;
470
+ return;
471
}
472
473
// we may have 1 more CPU reported
@@ -2113,7 +2113,7 @@ void update_cgroup_charts(int update_every) {
2113
);
2114
2115
for(i = 0; i < cg->cpuacct_usage.cpus; i++) {
2116
- snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
2116
+ snprintfz(id, RRD_ID_LENGTH_MAX, "cpu%u", i);
2117
rrddim_add(cg->st_cpu_per_core, id, NULL, 100, 1000000000, RRD_ALGORITHM_INCREMENTAL);
2118
}
2119
}
@@ -2121,7 +2121,7 @@ void update_cgroup_charts(int update_every) {
2121
rrdset_next(cg->st_cpu_per_core);
2122
2123
for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
2124
- snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
2124
+ snprintfz(id, RRD_ID_LENGTH_MAX, "cpu%u", i);
2125
rrddim_set(cg->st_cpu_per_core, id, cg->cpuacct_usage.cpu_percpu[i]);
2126
}
2127
rrdset_done(cg->st_cpu_per_core);