do not get the address of FILE pointer; fixes #4145
Costa Tsaousis (ktsaou) committed
Sep 10, 2018 at 17:49 UTC
89f50682d12459d35ba977a162deb4e8b8731c16
1 file changed
+26
-21
src/log.c
+26
-21
@@ -57,7 +57,7 @@ static inline void log_unlock() {
57
netdata_mutex_unlock(&log_mutex);
58
}
59
60
-int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog) {
60
+static FILE *open_log_file(int fd, FILE *fp, const char *filename, int *enabled_syslog, int is_stdaccess, int *fd_ptr) {
61
int f, devnull = 0;
62
63
if(!filename || !*filename || !strcmp(filename, "none") || !strcmp(filename, "/dev/null")) {
@@ -76,8 +76,10 @@ int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog)
76
// don't do anything if the user is willing
77
// to have the standard one
78
if(!strcmp(filename, "system")) {
79
- if(fd != -1 && fp != &stdaccess)
80
- return fd;
79
+ if(fd != -1 && !is_stdaccess) {
80
+ if(fd_ptr) *fd_ptr = fd;
81
+ return fp;
82
+ }
83
84
filename = "stderr";
85
}
@@ -92,19 +94,20 @@ int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog)
94
f = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0664);
95
if(f == -1) {
96
error("Cannot open file '%s'. Leaving %d to its default.", filename, fd);
95
- return fd;
97
+ if(fd_ptr) *fd_ptr = fd;
98
+ return fp;
99
}
100
}
101
99
- if(devnull && fp == &stdaccess) {
102
+ if(devnull && is_stdaccess) {
103
fd = -1;
101
- *fp = NULL;
104
+ fp = NULL;
105
}
106
107
// if there is a level-2 file pointer
108
// flush it before switching the level-1 fds
106
- if(fp && *fp)
107
- fflush(*fp);
109
+ if(fp)
110
+ fflush(fp);
111
112
if(fd != f && fd != -1) {
113
// it automatically closes
@@ -112,44 +115,46 @@ int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog)
115
if (t == -1) {
116
error("Cannot dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
117
close(f);
115
- return fd;
118
+ if(fd_ptr) *fd_ptr = fd;
119
+ return fp;
120
}
121
// info("dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
122
close(f);
123
}
124
else fd = f;
125
122
- if(fp && !*fp) {
123
- *fp = fdopen(fd, "a");
124
- if (!*fp)
126
+ if(!fp) {
127
+ fp = fdopen(fd, "a");
128
+ if (!fp)
129
error("Cannot fdopen() fd %d ('%s')", fd, filename);
130
else {
127
- if (setvbuf(*fp, NULL, _IOLBF, 0) != 0)
131
+ if (setvbuf(fp, NULL, _IOLBF, 0) != 0)
132
error("Cannot set line buffering on fd %d ('%s')", fd, filename);
133
}
134
}
135
132
- return fd;
136
+ if(fd_ptr) *fd_ptr = fd;
137
+ return fp;
138
}
139
140
void reopen_all_log_files() {
141
if(stdout_filename)
137
- open_log_file(STDOUT_FILENO, (FILE **)&stdout, stdout_filename, &output_log_syslog);
142
+ stdout = open_log_file(STDOUT_FILENO, stdout, stdout_filename, &output_log_syslog, 0, NULL);
143
144
if(stderr_filename)
140
- open_log_file(STDERR_FILENO, (FILE **)&stderr, stderr_filename, &error_log_syslog);
145
+ stderr = open_log_file(STDERR_FILENO, stderr, stderr_filename, &error_log_syslog, 0, NULL);
146
147
if(stdaccess_filename)
143
- stdaccess_fd = open_log_file(stdaccess_fd, (FILE **)&stdaccess, stdaccess_filename, &access_log_syslog);
148
+ stdaccess = open_log_file(stdaccess_fd, stdaccess, stdaccess_filename, &access_log_syslog, 1, &stdaccess_fd);
149
}
150
151
void open_all_log_files() {
152
// disable stdin
148
- open_log_file(STDIN_FILENO, (FILE **)&stdin, "/dev/null", NULL);
153
+ stdin = open_log_file(STDIN_FILENO, stdin, "/dev/null", NULL, 0, NULL);
154
150
- open_log_file(STDOUT_FILENO, (FILE **)&stdout, stdout_filename, &output_log_syslog);
151
- open_log_file(STDERR_FILENO, (FILE **)&stderr, stderr_filename, &error_log_syslog);
152
- stdaccess_fd = open_log_file(stdaccess_fd, (FILE **)&stdaccess, stdaccess_filename, &access_log_syslog);
155
+ stdout = open_log_file(STDOUT_FILENO, stdout, stdout_filename, &output_log_syslog, 0, NULL);
156
+ stderr = open_log_file(STDERR_FILENO, stderr, stderr_filename, &error_log_syslog, 0, NULL);
157
+ stdaccess = open_log_file(stdaccess_fd, stdaccess, stdaccess_filename, &access_log_syslog, 1, &stdaccess_fd);
158
}
159
160
// ----------------------------------------------------------------------------