removed thread unsafe functions
Costa Tsaousis (ktsaou) committed
Dec 8, 2015 at 00:10 UTC
75932745fe1364b875d6a641495e2eb1d0dbd12a
6 files changed
+46
-43
src/apps_plugin.c
+1
-1
@@ -387,7 +387,7 @@ int read_process_groups(const char *name)
387
if(debug) fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
388
FILE *fp = fopen(filename, "r");
389
if(!fp) {
390
- error("Cannot open file '%s' (%s)", filename, strerror(errno));
390
+ error("Cannot open file '%s'", filename);
391
return 1;
392
}
393
src/daemon.c
+18
-18
@@ -37,7 +37,7 @@ void sig_handler(int signo)
37
case SIGTRAP:
38
case SIGXCPU:
39
case SIGXFSZ:
40
- info("Death signaled exit (signal %d). Errno: %d (%s)", signo, errno, strerror(errno));
40
+ infoerr("Death signaled exit (signal %d).", signo);
41
signal(signo, SIG_DFL);
42
break;
43
@@ -48,7 +48,7 @@ void sig_handler(int signo)
48
case SIGHUP:
49
case SIGUSR1:
50
case SIGUSR2:
51
- info("Signaled exit (signal %d). Errno: %d (%s)", signo, errno, strerror(errno));
51
+ infoerr("Signaled exit (signal %d).", signo);
52
signal(SIGPIPE, SIG_IGN);
53
signal(SIGTERM, SIG_IGN);
54
signal(SIGQUIT, SIG_IGN);
@@ -59,10 +59,10 @@ void sig_handler(int signo)
59
break;
60
61
case SIGPIPE:
62
- info("Signaled PIPE (signal %d). Errno: %d (%s)", signo, errno, strerror(errno));
62
+ infoerr("Signaled PIPE (signal %d).", signo);
63
// this is received when web clients send a reset
64
// no need to log it.
65
- // info("Ignoring signal %d. Errno: %d (%s)", signo, errno, strerror(errno));
65
+ // infoerr("Ignoring signal %d.", signo);
66
break;
67
68
default:
@@ -85,7 +85,7 @@ void prepare_rundir() {
85
snprintf(pidfile, FILENAME_MAX, "%s/netdata.pid", rundir);
86
87
if(mkdir(rundir, 0775) != 0) {
88
- if(errno != EEXIST) fprintf(stderr, "Cannot create directory '%s' (%s).", rundir, strerror(errno));
88
+ if(errno != EEXIST) error("Cannot create directory '%s'.", rundir);
89
}
90
}
91
@@ -93,39 +93,39 @@ int become_user(const char *username)
93
{
94
struct passwd *pw = getpwnam(username);
95
if(!pw) {
96
- error("User %s is not present. Error: %s\n", username, strerror(errno));
96
+ error("User %s is not present.", username);
97
return -1;
98
}
99
100
if(chown(rundir, pw->pw_uid, pw->pw_gid) != 0) {
101
- error("Cannot chown directory '%s' to user %s. Error: %s\n", rundir, username, strerror(errno));
101
+ error("Cannot chown directory '%s' to user %s.", rundir, username);
102
return -1;
103
}
104
105
if(setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
106
- error("Cannot switch to user's %s group (gid: %d). Error: %s\n", username, pw->pw_gid, strerror(errno));
106
+ error("Cannot switch to user's %s group (gid: %d).", username, pw->pw_gid);
107
return -1;
108
}
109
110
if(setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
111
- error("Cannot switch to user %s (uid: %d). Error: %s\n", username, pw->pw_uid, strerror(errno));
111
+ error("Cannot switch to user %s (uid: %d).", username, pw->pw_uid);
112
return -1;
113
}
114
115
if(setgid(pw->pw_gid) != 0) {
116
- error("Cannot switch to user's %s group (gid: %d). Error: %s\n", username, pw->pw_gid, strerror(errno));
116
+ error("Cannot switch to user's %s group (gid: %d).", username, pw->pw_gid);
117
return -1;
118
}
119
if(setegid(pw->pw_gid) != 0) {
120
- error("Cannot effectively switch to user's %s group (gid: %d). Error: %s\n", username, pw->pw_gid, strerror(errno));
120
+ error("Cannot effectively switch to user's %s group (gid: %d).", username, pw->pw_gid);
121
return -1;
122
}
123
if(setuid(pw->pw_uid) != 0) {
124
- error("Cannot switch to user %s (uid: %d). Error: %s\n", username, pw->pw_uid, strerror(errno));
124
+ error("Cannot switch to user %s (uid: %d).", username, pw->pw_uid);
125
return -1;
126
}
127
if(seteuid(pw->pw_uid) != 0) {
128
- error("Cannot effectively switch to user %s (uid: %d). Error: %s\n", username, pw->pw_uid, strerror(errno));
128
+ error("Cannot effectively switch to user %s (uid: %d).", username, pw->pw_uid);
129
return -1;
130
}
131
@@ -141,14 +141,14 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
141
142
if(input && *input) {
143
if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
144
- fprintf(stderr, "Cannot open input file '%s' (%s).\n", input, strerror(errno));
144
+ error("Cannot open input file '%s'.", input);
145
return -1;
146
}
147
}
148
149
if(output && *output) {
150
if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
151
- fprintf(stderr, "Cannot open output log file '%s' (%s).\n", output, strerror(errno));
151
+ error("Cannot open output log file '%s'", output);
152
if(input_fd != -1) close(input_fd);
153
return -1;
154
}
@@ -156,7 +156,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
156
157
if(error && *error) {
158
if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
159
- fprintf(stderr, "Cannot open error log file '%s' (%s).\n", error, strerror(errno));
159
+ error("Cannot open error log file '%s'.", error);
160
if(input_fd != -1) close(input_fd);
161
if(output_fd != -1) close(output_fd);
162
return -1;
@@ -165,7 +165,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
165
166
if(access && *access && access_fd) {
167
if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
168
- fprintf(stderr, "Cannot open access log file '%s' (%s).\n", access, strerror(errno));
168
+ error("Cannot open access log file '%s'", access);
169
if(input_fd != -1) close(input_fd);
170
if(output_fd != -1) close(output_fd);
171
if(error_fd != -1) close(error_fd);
@@ -175,7 +175,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
175
if(access_fp) {
176
*access_fp = fdopen(*access_fd, "w");
177
if(!*access_fp) {
178
- fprintf(stderr, "Cannot migrate file's '%s' fd %d (%s).\n", access, *access_fd, strerror(errno));
178
+ error("Cannot migrate file's '%s' fd %d.", access, *access_fd);
179
if(input_fd != -1) close(input_fd);
180
if(output_fd != -1) close(output_fd);
181
if(error_fd != -1) close(error_fd);
src/log.c
+7
-5
@@ -82,21 +82,23 @@ void info_int( const char *file, const char *function, const unsigned long line,
82
}
83
}
84
85
-void error_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
85
+void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
86
{
87
va_list args;
88
89
log_date(stderr);
90
91
va_start( args, fmt );
92
- if(debug_flags) fprintf(stderr, "ERROR (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
93
- else fprintf(stderr, "ERROR: %s: ", program_name);
92
+ if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
93
+ else fprintf(stderr, "%s: %s: ", prefix, program_name);
94
vfprintf( stderr, fmt, args );
95
va_end( args );
96
97
if(errno) {
98
- fprintf(stderr, " (errno %d, %s)\n", errno, strerror(errno));
99
- errno = 0;
98
+ char buf[200];
99
+ char *s = strerror_r(errno, buf, 200);
100
+ fprintf(stderr, " (errno %d, %s)\n", errno, s);
101
+ errno = 0;
102
}
103
else fprintf(stderr, "\n");
104
src/log.h
+5
-4
@@ -43,14 +43,15 @@ extern int error_log_syslog;
43
extern int output_log_syslog;
44
45
#define debug(type, args...) do { if(unlikely(!silent && (debug_flags & type))) debug_int(__FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
46
-#define info(args...) info_int(__FILE__, __FUNCTION__, __LINE__, ##args)
47
-#define error(args...) error_int(__FILE__, __FUNCTION__, __LINE__, ##args)
48
-#define fatal(args...) fatal_int(__FILE__, __FUNCTION__, __LINE__, ##args)
46
+#define info(args...) info_int(__FILE__, __FUNCTION__, __LINE__, ##args)
47
+#define infoerr(args...) error_int("INFO", __FILE__, __FUNCTION__, __LINE__, ##args)
48
+#define error(args...) error_int("ERROR", __FILE__, __FUNCTION__, __LINE__, ##args)
49
+#define fatal(args...) fatal_int(__FILE__, __FUNCTION__, __LINE__, ##args)
50
51
extern void log_date(FILE *out);
52
extern void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... );
53
extern void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... );
53
-extern void error_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... );
54
+extern void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... );
55
extern void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... );
56
extern void log_access( const char *fmt, ... );
57
src/main.c
+4
-4
@@ -198,7 +198,7 @@ int main(int argc, char **argv)
198
for(i = 1; i < argc ; i++) {
199
if(strcmp(argv[i], "-c") == 0 && (i+1) < argc) {
200
if(load_config(argv[i+1], 1) != 1) {
201
- fprintf(stderr, "Cannot load configuration file %s. Reason: %s\n", argv[i+1], strerror(errno));
201
+ error("Cannot load configuration file %s.", argv[i+1]);
202
exit(1);
203
}
204
else {
@@ -262,7 +262,7 @@ int main(int argc, char **argv)
262
setenv("TZ", ":/etc/localtime", 0);
263
264
// cd to /tmp to avoid any plugins writing files at random places
265
- if(chdir("/tmp")) fprintf(stderr, "netdata: ERROR: Cannot cd to /tmp: %s", strerror(errno));
265
+ if(chdir("/tmp")) error("netdata: ERROR: Cannot cd to /tmp");
266
267
char *input_log_file = NULL;
268
char *output_log_file = NULL;
@@ -414,10 +414,10 @@ int main(int argc, char **argv)
414
}
415
416
// never become a problem
417
- if(nice(20) == -1) fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno));
417
+ if(nice(20) == -1) error("Cannot lower my CPU priority.");
418
419
if(become_daemon(dont_fork, 0, user, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
420
- fatal("Cannot demonize myself (%s).", strerror(errno));
420
+ fatal("Cannot demonize myself.");
421
exit(1);
422
}
423
src/web_client.c
+11
-11
@@ -236,7 +236,7 @@ uid_t web_files_uid(void)
236
else {
237
struct passwd *pw = getpwnam(web_owner);
238
if(!pw) {
239
- error("User %s is not present. Ignoring option. Error: %s\n", web_owner, strerror(errno));
239
+ error("User %s is not present. Ignoring option.", web_owner);
240
owner_uid = geteuid();
241
}
242
else {
@@ -1246,8 +1246,8 @@ void web_client_process(struct web_client *w) {
1246
}
1247
1248
char date[100];
1249
- struct tm tm = *gmtime(&w->response.data->date);
1250
- strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %Z", &tm);
1249
+ struct tm tmbuf, *tm = gmtime_r(&w->response.data->date, &tmbuf);
1250
+ strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %Z", tm);
1251
1252
buffer_sprintf(w->response.header_output,
1253
"HTTP/1.1 %d %s\r\n"
@@ -1365,7 +1365,7 @@ long web_client_send_chunk_header(struct web_client *w, int len)
1365
1366
if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk header %d bytes.", w->id, bytes);
1367
else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk header to the client.", w->id);
1368
- else debug(D_DEFLATE, "%llu: Failed to send chunk header to client. Reason: %s", w->id, strerror(errno));
1368
+ else debug(D_DEFLATE, "%llu: Failed to send chunk header to client.", w->id);
1369
1370
return bytes;
1371
}
@@ -1378,7 +1378,7 @@ long web_client_send_chunk_close(struct web_client *w)
1378
1379
if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk suffix %d bytes.", w->id, bytes);
1380
else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk suffix to the client.", w->id);
1381
- else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client. Reason: %s", w->id, strerror(errno));
1381
+ else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client.", w->id);
1382
1383
return bytes;
1384
}
@@ -1391,7 +1391,7 @@ long web_client_send_chunk_finalize(struct web_client *w)
1391
1392
if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk suffix %d bytes.", w->id, bytes);
1393
else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk suffix to the client.", w->id);
1394
- else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client. Reason: %s", w->id, strerror(errno));
1394
+ else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client.", w->id);
1395
1396
return bytes;
1397
}
@@ -1540,7 +1540,7 @@ long web_client_send(struct web_client *w)
1540
debug(D_WEB_CLIENT, "%llu: Sent %d bytes.", w->id, bytes);
1541
}
1542
else if(likely(bytes == 0)) debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client.", w->id);
1543
- else debug(D_WEB_CLIENT, "%llu: Failed to send data to client. Reason: %s", w->id, strerror(errno));
1543
+ else debug(D_WEB_CLIENT, "%llu: Failed to send data to client.", w->id);
1544
1545
return(bytes);
1546
}
@@ -1655,19 +1655,19 @@ void *web_client_main(void *ptr)
1655
}
1656
1657
if(FD_ISSET(w->ifd, &efds)) {
1658
- debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on input socket (%s).", w->id, strerror(errno));
1658
+ debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on input socket.", w->id);
1659
break;
1660
}
1661
1662
if(FD_ISSET(w->ofd, &efds)) {
1663
- debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on output socket (%s).", w->id, strerror(errno));
1663
+ debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on output socket.", w->id);
1664
break;
1665
}
1666
1667
if(w->wait_send && FD_ISSET(w->ofd, &ofds)) {
1668
long bytes;
1669
if((bytes = web_client_send(w)) < 0) {
1670
- debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client (ouput: %s).", w->id, strerror(errno));
1670
+ debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client.", w->id);
1671
errno = 0;
1672
break;
1673
}
@@ -1680,7 +1680,7 @@ void *web_client_main(void *ptr)
1680
if(w->wait_receive && FD_ISSET(w->ifd, &ifds)) {
1681
long bytes;
1682
if((bytes = web_client_receive(w)) < 0) {
1683
- debug(D_WEB_CLIENT, "%llu: Cannot receive data from client. Closing client (input: %s).", w->id, strerror(errno));
1683
+ debug(D_WEB_CLIENT, "%llu: Cannot receive data from client. Closing client.", w->id);
1684
errno = 0;
1685
break;
1686
}