From bb4f274ae0e8869dcd51e0dea231751f3726441c Mon Sep 17 00:00:00 2001 From: Demi Marie Obenour Date: Fri, 23 Sep 2022 10:40:55 -0400 Subject: [PATCH] Make all fopen() calls use O_CLOEXEC by adding "e" to the mode strings. --- spa/plugins/alsa/alsa-udev.c | 4 ++-- spa/plugins/bluez5/bluez5-dbus.c | 2 +- spa/plugins/support/logger.c | 2 +- src/modules/module-protocol-pulse/utils.c | 2 +- src/tools/pw-dot.c | 2 +- src/tools/pw-profiler.c | 16 ++++++++-------- test/pwtest.c | 4 ++-- test/test-config.c | 2 +- test/test-logger.c | 10 +++++----- test/test-properties.c | 2 +- 10 files changed, 23 insertions(+), 23 deletions(-) diff --git a/spa/plugins/alsa/alsa-udev.c b/spa/plugins/alsa/alsa-udev.c index 8ee217d9d..30975523f 100644 --- a/spa/plugins/alsa/alsa-udev.c +++ b/spa/plugins/alsa/alsa-udev.c @@ -256,7 +256,7 @@ static int check_device_pcm_class(const char *devname) /* Check device class */ spa_scnprintf(path, sizeof(path), "/sys/class/sound/%s/pcm_class", devname); - f = fopen(path, "r"); + f = fopen(path, "re"); if (f == NULL) return -errno; sz = fread(buf, 1, sizeof(buf) - 1, f); @@ -361,7 +361,7 @@ static int check_device_available(struct impl *this, struct device *device, int spa_scnprintf(path, sizeof(path), "/proc/asound/card%u/%s/%s/status", (unsigned int)device->id, entry->d_name, entry_pcm->d_name); - f = fopen(path, "r"); + f = fopen(path, "re"); if (f == NULL) goto done; sz = fread(buf, 1, 6, f); diff --git a/spa/plugins/bluez5/bluez5-dbus.c b/spa/plugins/bluez5/bluez5-dbus.c index 220d401ff..c7ed335fc 100644 --- a/spa/plugins/bluez5/bluez5-dbus.c +++ b/spa/plugins/bluez5/bluez5-dbus.c @@ -949,7 +949,7 @@ static int adapter_init_modalias(struct spa_bt_monitor *monitor, struct spa_bt_a if (str == NULL) goto fail; snprintf(path, sizeof(path), "/sys/class/bluetooth/%s/device/modalias", str); - if ((f = fopen(path, "rb")) == NULL) { + if ((f = fopen(path, "rbe")) == NULL) { res = -errno; goto fail; } diff --git a/spa/plugins/support/logger.c b/spa/plugins/support/logger.c index 3b854c65e..615a49bee 100644 --- a/spa/plugins/support/logger.c +++ b/spa/plugins/support/logger.c @@ -357,7 +357,7 @@ impl_init(const struct spa_handle_factory *factory, if ((str = spa_dict_lookup(info, SPA_KEY_LOG_LEVEL)) != NULL) this->log.level = atoi(str); if ((str = spa_dict_lookup(info, SPA_KEY_LOG_FILE)) != NULL) { - this->file = fopen(str, "w"); + this->file = fopen(str, "we"); if (this->file == NULL) fprintf(stderr, "Warning: failed to open file %s: (%m)", str); else diff --git a/src/modules/module-protocol-pulse/utils.c b/src/modules/module-protocol-pulse/utils.c index 1c9bc1c5f..617ca0524 100644 --- a/src/modules/module-protocol-pulse/utils.c +++ b/src/modules/module-protocol-pulse/utils.c @@ -195,7 +195,7 @@ int create_pid_file(void) { strcat(pid_file, "/pid"); - if ((f = fopen(pid_file, "w")) == NULL) { + if ((f = fopen(pid_file, "we")) == NULL) { res = -errno; pw_log_error("failed to open pid file: %m"); return res; diff --git a/src/tools/pw-dot.c b/src/tools/pw-dot.c index 68abc3b95..eed63512a 100644 --- a/src/tools/pw-dot.c +++ b/src/tools/pw-dot.c @@ -514,7 +514,7 @@ static int draw_graph(struct data *d, const char *path) fputs(d->dot_str, stdout); } else { /* open the file */ - fp = fopen(path, "w"); + fp = fopen(path, "we"); if (fp == NULL) { printf("open error: could not open %s for writing\n", path); return -1; diff --git a/src/tools/pw-profiler.c b/src/tools/pw-profiler.c index f2000a986..00dc4104a 100644 --- a/src/tools/pw-profiler.c +++ b/src/tools/pw-profiler.c @@ -262,7 +262,7 @@ static void dump_scripts(struct data *d) printf("\ndumping scripts for %d followers\n", d->n_followers); - out = fopen("Timing1.plot", "w"); + out = fopen("Timing1.plot", "we"); if (out == NULL) { pw_log_error("Can't open Timing1.plot: %m"); } else { @@ -282,7 +282,7 @@ static void dump_scripts(struct data *d) fclose(out); } - out = fopen("Timing2.plot", "w"); + out = fopen("Timing2.plot", "we"); if (out == NULL) { pw_log_error("Can't open Timing2.plot: %m"); } else { @@ -298,7 +298,7 @@ static void dump_scripts(struct data *d) fclose(out); } - out = fopen("Timing3.plot", "w"); + out = fopen("Timing3.plot", "we"); if (out == NULL) { pw_log_error("Can't open Timing3.plot: %m"); } else { @@ -328,7 +328,7 @@ static void dump_scripts(struct data *d) fclose(out); } - out = fopen("Timing4.plot", "w"); + out = fopen("Timing4.plot", "we"); if (out == NULL) { pw_log_error("Can't open Timing4.plot: %m"); } else { @@ -355,7 +355,7 @@ static void dump_scripts(struct data *d) fclose(out); } - out = fopen("Timing5.plot", "w"); + out = fopen("Timing5.plot", "we"); if (out == NULL) { pw_log_error("Can't open Timing5.plot: %m"); } else { @@ -381,7 +381,7 @@ static void dump_scripts(struct data *d) "unset output\n"); fclose(out); } - out = fopen("Timings.html", "w"); + out = fopen("Timings.html", "we"); if (out == NULL) { pw_log_error("Can't open Timings.html: %m"); } else { @@ -409,7 +409,7 @@ static void dump_scripts(struct data *d) fclose(out); } - out = fopen("generate_timings.sh", "w"); + out = fopen("generate_timings.sh", "we"); if (out == NULL) { pw_log_error("Can't open generate_timings.sh: %m"); } else { @@ -624,7 +624,7 @@ int main(int argc, char *argv[]) data.filename = opt_output; - data.output = fopen(data.filename, "w"); + data.output = fopen(data.filename, "we"); if (data.output == NULL) { fprintf(stderr, "Can't open file %s: %m\n", data.filename); return -1; diff --git a/test/pwtest.c b/test/pwtest.c index 18be5c6ad..2d668184b 100644 --- a/test/pwtest.c +++ b/test/pwtest.c @@ -835,7 +835,7 @@ static int init_pipes(int read_fds[_FD_LAST], int write_fds[_FD_LAST]) #ifdef __linux__ { FILE *f; - f = fopen("/proc/sys/fs/pipe-max-size", "r"); + f = fopen("/proc/sys/fs/pipe-max-size", "re"); if (f) { if (fscanf(f, "%d", &r) == 1) pipe_max_size = SPA_MIN(r, pipe_max_size); @@ -1244,7 +1244,7 @@ static char* make_xdg_runtime_dir(void) /* Marker file to avoid removing a random directory during cleanup */ r = spa_scnprintf(path, sizeof(path), "%s/pwtest.dir", dir); spa_assert_se((size_t)r == strlen(dir) + 11); - fp = fopen(path, "w"); + fp = fopen(path, "we"); spa_assert_se(fp); fprintf(fp, "pwtest\n"); fclose(fp); diff --git a/test/test-config.c b/test/test-config.c index d1397bafb..9ebbc26d1 100644 --- a/test/test-config.c +++ b/test/test-config.c @@ -35,7 +35,7 @@ PWTEST(config_load_abspath) char *basename; pwtest_mkstemp(path); - fp = fopen(path, "w"); + fp = fopen(path, "we"); fputs("data = x", fp); fclose(fp); diff --git a/test/test-logger.c b/test/test-logger.c index a1b0ff01d..5aca7d199 100644 --- a/test/test-logger.c +++ b/test/test-logger.c @@ -63,7 +63,7 @@ PWTEST(logger_truncate_long_lines) /* Print a line expected to be truncated */ spa_log_error(iface, "MARK: %1100s", "foo"); - fp = fopen(fname, "r"); + fp = fopen(fname, "re"); while (fgets(buffer, sizeof(buffer), fp) != NULL) { if (strstr(buffer, "MARK:")) { const char *suffix = ".. (truncated)\n"; @@ -110,7 +110,7 @@ PWTEST(logger_no_ansi) * tty so expect none despite colors being enabled */ spa_log_error(iface, "MARK\n"); - fp = fopen(fname, "r"); + fp = fopen(fname, "re"); while (fgets(buffer, sizeof(buffer), fp) != NULL) { if (strstr(buffer, "MARK")) { mark_line_found = true; @@ -157,7 +157,7 @@ test_log_levels(enum spa_log_level level) if (level < SPA_LOG_LEVEL_TRACE) pw_log(level + 1, "ABOVE"); - fp = fopen(fname, "r"); + fp = fopen(fname, "re"); while (fgets(buffer, sizeof(buffer), fp) != NULL) { if (strstr(buffer, "CURRENT")) current_level_found = true; @@ -427,7 +427,7 @@ PWTEST(logger_topics) spa_logt_info(iface, &topic, "MARK\n"); - fp = fopen(fname, "r"); + fp = fopen(fname, "re"); while (fgets(buffer, sizeof(buffer), fp) != NULL) { if (strstr(buffer, "MARK")) { mark_line_found = true; @@ -602,7 +602,7 @@ PWTEST(logger_journal_chain) /* Now check that the line is in the chained file logger too */ spa_memzero(buffer, sizeof(buffer)); mark_line_found = false; - fp = fopen(fname, "r"); + fp = fopen(fname, "re"); while (fgets(buffer, sizeof(buffer), fp) != NULL) { if (strstr(buffer, token)) { mark_line_found = true; diff --git a/test/test-properties.c b/test/test-properties.c index 0a4d38754..89e66bb37 100644 --- a/test/test-properties.c +++ b/test/test-properties.c @@ -484,7 +484,7 @@ PWTEST(properties_serialize_dict_stack_overflow) dict = SPA_DICT_INIT(items, 2); pwtest_mkstemp(tmpfile); - fp = fopen(tmpfile, "w"); + fp = fopen(tmpfile, "we"); pwtest_ptr_notnull(fp); r = pw_properties_serialize_dict(fp, &dict, 0); pwtest_int_eq(r, 1);