util: introduce safe_fclose() and port everything over to it

Adds a coccinelle script to port things over automatically.
This commit is contained in:
Lennart Poettering 2015-09-09 15:20:10 +02:00
parent 9c00a6adfa
commit 74ca738f6a
14 changed files with 79 additions and 49 deletions

View File

@ -0,0 +1,27 @@
@@
expression p;
@@
- if (p) {
- fclose(p);
- p = NULL;
- }
+ p = safe_fclose(p);
@@
expression p;
@@
- if (p)
- fclose(p);
- p = NULL;
+ p = safe_fclose(p);
@@
expression p;
@@
- fclose(p);
- p = NULL;
+ p = safe_fclose(p);
@@
expression p;
@@
- if (p)
- fclose(p);
+ safe_fclose(p);

View File

@ -327,6 +327,33 @@ void close_many(const int fds[], unsigned n_fd) {
safe_close(fds[i]); safe_close(fds[i]);
} }
int fclose_nointr(FILE *f) {
assert(f);
/* Same as close_nointr(), but for fclose() */
if (fclose(f) == 0)
return 0;
if (errno == EINTR)
return 0;
return -errno;
}
FILE* safe_fclose(FILE *f) {
/* Same as safe_close(), but for fclose() */
if (f) {
PROTECT_ERRNO;
assert_se(fclose_nointr(f) != EBADF);
}
return NULL;
}
int unlink_noerrno(const char *path) { int unlink_noerrno(const char *path) {
PROTECT_ERRNO; PROTECT_ERRNO;
int r; int r;

View File

@ -149,6 +149,9 @@ void safe_close_pair(int p[]);
void close_many(const int fds[], unsigned n_fd); void close_many(const int fds[], unsigned n_fd);
int fclose_nointr(FILE *f);
FILE* safe_fclose(FILE *f);
int parse_size(const char *t, off_t base, off_t *size); int parse_size(const char *t, off_t base, off_t *size);
int parse_boolean(const char *v) _pure_; int parse_boolean(const char *v) _pure_;
@ -514,7 +517,10 @@ static inline void close_pairp(int (*p)[2]) {
safe_close_pair(*p); safe_close_pair(*p);
} }
DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose); static inline void fclosep(FILE **f) {
safe_fclose(*f);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose); DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir); DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent); DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent);

View File

@ -458,10 +458,7 @@ int main(int argc, char *argv[]) {
ps = ps->next_ps; ps = ps->next_ps;
ps->schedstat = safe_close(ps->schedstat); ps->schedstat = safe_close(ps->schedstat);
ps->sched = safe_close(ps->sched); ps->sched = safe_close(ps->sched);
if (ps->smaps) { ps->smaps = safe_fclose(ps->smaps);
fclose(ps->smaps);
ps->smaps = NULL;
}
} }
if (!of) { if (!of) {

View File

@ -918,8 +918,7 @@ static int parse_argv(int argc, char *argv[]) {
if (!f) if (!f)
return log_error_errno(errno, "Failed to open serialization fd: %m"); return log_error_errno(errno, "Failed to open serialization fd: %m");
if (arg_serialization) safe_fclose(arg_serialization);
fclose(arg_serialization);
arg_serialization = f; arg_serialization = f;
@ -1059,8 +1058,7 @@ static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool switching
fail: fail:
fdset_free(fds); fdset_free(fds);
if (f) safe_fclose(f);
fclose(f);
return r; return r;
} }
@ -1678,10 +1676,7 @@ int main(int argc, char *argv[]) {
fdset_free(fds); fdset_free(fds);
fds = NULL; fds = NULL;
if (arg_serialization) { arg_serialization = safe_fclose(arg_serialization);
fclose(arg_serialization);
arg_serialization = NULL;
}
if (queue_default_job) { if (queue_default_job) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
@ -1923,10 +1918,7 @@ finish:
* getopt() in argv[], and some cleanups in envp[], * getopt() in argv[], and some cleanups in envp[],
* but let's hope that doesn't matter.) */ * but let's hope that doesn't matter.) */
if (arg_serialization) { arg_serialization = safe_fclose(arg_serialization);
fclose(arg_serialization);
arg_serialization = NULL;
}
if (fds) { if (fds) {
fdset_free(fds); fdset_free(fds);
@ -1966,10 +1958,7 @@ finish:
log_warning_errno(errno, "Failed to execute /sbin/init, giving up: %m"); log_warning_errno(errno, "Failed to execute /sbin/init, giving up: %m");
} }
if (arg_serialization) { arg_serialization = safe_fclose(arg_serialization);
fclose(arg_serialization);
arg_serialization = NULL;
}
if (fds) { if (fds) {
fdset_free(fds); fdset_free(fds);

View File

@ -1540,10 +1540,7 @@ static void mount_shutdown(Manager *m) {
m->mount_event_source = sd_event_source_unref(m->mount_event_source); m->mount_event_source = sd_event_source_unref(m->mount_event_source);
m->mount_utab_event_source = sd_event_source_unref(m->mount_utab_event_source); m->mount_utab_event_source = sd_event_source_unref(m->mount_utab_event_source);
if (m->proc_self_mountinfo) { m->proc_self_mountinfo = safe_fclose(m->proc_self_mountinfo);
fclose(m->proc_self_mountinfo);
m->proc_self_mountinfo = NULL;
}
m->utab_inotify_fd = safe_close(m->utab_inotify_fd); m->utab_inotify_fd = safe_close(m->utab_inotify_fd);
} }

View File

@ -1252,10 +1252,7 @@ static void swap_shutdown(Manager *m) {
m->swap_event_source = sd_event_source_unref(m->swap_event_source); m->swap_event_source = sd_event_source_unref(m->swap_event_source);
if (m->proc_swaps) { m->proc_swaps = safe_fclose(m->proc_swaps);
fclose(m->proc_swaps);
m->proc_swaps = NULL;
}
hashmap_free(m->swaps_by_devnode); hashmap_free(m->swaps_by_devnode);
m->swaps_by_devnode = NULL; m->swaps_by_devnode = NULL;

View File

@ -103,8 +103,7 @@ static int create_dbus_files(
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to write %s: %m", a); return log_error_errno(r, "Failed to write %s: %m", a);
fclose(f); f = safe_fclose(f);
f = NULL;
service = s; service = s;
} }

View File

@ -105,8 +105,7 @@ static void request_meta_free(
sd_journal_close(m->journal); sd_journal_close(m->journal);
if (m->tmp) safe_fclose(m->tmp);
fclose(m->tmp);
free(m->cursor); free(m->cursor);
free(m); free(m);

View File

@ -512,8 +512,7 @@ static int compose_open_fds(pid_t pid, char **open_fds) {
} }
errno = 0; errno = 0;
fclose(stream); stream = safe_fclose(stream);
stream = NULL;
if (errno != 0) if (errno != 0)
return -errno; return -errno;

View File

@ -177,8 +177,7 @@ int coredump_make_stack_trace(int fd, const char *executable, char **ret) {
goto finish; goto finish;
} }
fclose(c.f); c.f = safe_fclose(c.f);
c.f = NULL;
*ret = buf; *ret = buf;
buf = NULL; buf = NULL;
@ -192,8 +191,7 @@ finish:
if (c.elf) if (c.elf)
elf_end(c.elf); elf_end(c.elf);
if (c.f) safe_fclose(c.f);
fclose(c.f);
free(buf); free(buf);

View File

@ -204,8 +204,7 @@ int introspect_finish(struct introspect *i, sd_bus *bus, sd_bus_message *m, sd_b
void introspect_free(struct introspect *i) { void introspect_free(struct introspect *i) {
assert(i); assert(i);
if (i->f) safe_fclose(i->f);
fclose(i->f);
free(i->introspection); free(i->introspection);
zero(*i); zero(*i);

View File

@ -344,8 +344,7 @@ _public_ sd_hwdb *sd_hwdb_unref(sd_hwdb *hwdb) {
if (hwdb && REFCNT_DEC(hwdb->n_ref) == 0) { if (hwdb && REFCNT_DEC(hwdb->n_ref) == 0) {
if (hwdb->map) if (hwdb->map)
munmap((void *)hwdb->map, hwdb->st.st_size); munmap((void *)hwdb->map, hwdb->st.st_size);
if (hwdb->f) safe_fclose(hwdb->f);
fclose(hwdb->f);
free(hwdb->modalias); free(hwdb->modalias);
ordered_hashmap_free(hwdb->properties); ordered_hashmap_free(hwdb->properties);
free(hwdb); free(hwdb);

View File

@ -151,13 +151,10 @@ void pager_close(void) {
return; return;
/* Inform pager that we are done */ /* Inform pager that we are done */
fclose(stdout); stdout = safe_fclose(stdout);
stdout = NULL; stderr = safe_fclose(stderr);
fclose(stderr); (void) kill(pager_pid, SIGCONT);
stderr = NULL;
kill(pager_pid, SIGCONT);
(void) wait_for_terminate(pager_pid, NULL); (void) wait_for_terminate(pager_pid, NULL);
pager_pid = 0; pager_pid = 0;
} }