improve loggerd error handling (#1332)

* call BZ2_bzWriteClose after fail

* set closed file handles to NULL

* move fclose after BZ2_bzWriteClose
This commit is contained in:
Dean Lee 2020-04-09 05:05:19 +08:00 committed by GitHub
parent c22f1267cd
commit 434a2b0658
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -132,8 +132,22 @@ static LoggerHandle* logger_open(LoggerState *s, const char* root_path) {
return h;
fail:
LOGE("logger failed to open files");
if (h->qlog_file) fclose(h->qlog_file);
if (h->log_file) fclose(h->log_file);
if (h->bz_file) {
BZ2_bzWriteClose(&bzerror, h->bz_file, 0, NULL, NULL);
h->bz_file = NULL;
}
if (h->bz_qlog) {
BZ2_bzWriteClose(&bzerror, h->bz_qlog, 0, NULL, NULL);
h->bz_qlog = NULL;
}
if (h->qlog_file) {
fclose(h->qlog_file);
h->qlog_file = NULL;
}
if (h->log_file) {
fclose(h->log_file);
h->log_file = NULL;
}
return NULL;
}
@ -228,8 +242,12 @@ void lh_close(LoggerHandle* h) {
BZ2_bzWriteClose(&bzerror, h->bz_qlog, 0, NULL, NULL);
h->bz_qlog = NULL;
}
if (h->qlog_file) fclose(h->qlog_file);
if (h->qlog_file) {
fclose(h->qlog_file);
h->qlog_file = NULL;
}
fclose(h->log_file);
h->log_file = NULL;
unlink(h->lock_path);
pthread_mutex_unlock(&h->lock);
pthread_mutex_destroy(&h->lock);