From 5cfd3ae4e65e8f12887de7374dd6591449d9454c Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Sun, 11 Oct 2020 01:27:20 -0400 Subject: hosted: Use O_CLOEXEC for all open() and "e" for fopen() calls This way we'll automatically close the files upon exec() Change-Id: Ic0daca8fb56432830de4a2f4a86a77337121ecc7 --- firmware/target/hosted/agptek/button-agptek.c | 2 +- firmware/target/hosted/cpufreq-linux.c | 4 ++-- firmware/target/hosted/cpuinfo-linux.c | 4 ++-- firmware/target/hosted/fiio/button-fiio.c | 2 +- firmware/target/hosted/fiio/system-fiio.c | 2 +- firmware/target/hosted/filesystem-app.c | 1 + firmware/target/hosted/filesystem-unix.c | 6 +++--- firmware/target/hosted/ibasso/sysfs-ibasso.c | 4 ++-- firmware/target/hosted/lcd-linuxfb.c | 7 +------ firmware/target/hosted/rtc.c | 12 ++++++------ firmware/target/hosted/sdl/pcm-sdl.c | 4 ++-- firmware/target/hosted/sonynwz/system-nwz.c | 4 ++-- firmware/target/hosted/sysfs.c | 4 ++-- firmware/target/hosted/xduoo/button-xduoo.c | 2 +- 14 files changed, 27 insertions(+), 31 deletions(-) (limited to 'firmware/target/hosted') diff --git a/firmware/target/hosted/agptek/button-agptek.c b/firmware/target/hosted/agptek/button-agptek.c index 2d2deda6f6..83953e0dab 100644 --- a/firmware/target/hosted/agptek/button-agptek.c +++ b/firmware/target/hosted/agptek/button-agptek.c @@ -77,7 +77,7 @@ void button_init_device(void) for(int i = 0; i < NR_POLL_DESC; i++) { - int fd = open(input_devs[i], O_RDWR); + int fd = open(input_devs[i], O_RDWR | O_CLOEXEC); if(fd < 0) { diff --git a/firmware/target/hosted/cpufreq-linux.c b/firmware/target/hosted/cpufreq-linux.c index d622cf1d74..2083f00f26 100644 --- a/firmware/target/hosted/cpufreq-linux.c +++ b/firmware/target/hosted/cpufreq-linux.c @@ -31,7 +31,7 @@ static FILE* open_read(const char* file_name) { - FILE *f = fopen(file_name, "r"); + FILE *f = fopen(file_name, "re"); if(f == NULL) { DEBUGF("ERROR %s: Can not open %s for reading.", __func__, file_name); @@ -83,7 +83,7 @@ void cpufreq_available_governors(char* governors, int governors_size, int cpu) static FILE* open_write(const char* file_name) { - FILE *f = fopen(file_name, "w"); + FILE *f = fopen(file_name, "we"); if(f == NULL) { DEBUGF("ERROR %s: Can not open %s for writing.", __func__, file_name); diff --git a/firmware/target/hosted/cpuinfo-linux.c b/firmware/target/hosted/cpuinfo-linux.c index c3669a9f5c..8165a0961b 100644 --- a/firmware/target/hosted/cpuinfo-linux.c +++ b/firmware/target/hosted/cpuinfo-linux.c @@ -176,7 +176,7 @@ bool current_scaling_governor(int cpu, char* governor, int governor_size) sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpu); - FILE *f = fopen(path, "r"); + FILE *f = fopen(path, "re"); if(f == NULL) { DEBUGF("ERROR %s: Can not open %s for reading.", __func__, path); @@ -252,7 +252,7 @@ static int read_cpu_frequency(int cpu, enum cpu_frequency_options freqOpt) } } - FILE *f = fopen(path, "r"); + FILE *f = fopen(path, "re"); if(f == NULL) { DEBUGF("ERROR %s: Can not open %s for reading.", __func__, path); diff --git a/firmware/target/hosted/fiio/button-fiio.c b/firmware/target/hosted/fiio/button-fiio.c index fcc7480e11..79ed702668 100644 --- a/firmware/target/hosted/fiio/button-fiio.c +++ b/firmware/target/hosted/fiio/button-fiio.c @@ -226,7 +226,7 @@ void button_init_device(void) for(int i = 0; i < NR_POLL_DESC; i++) { - int fd = open(input_devs[i], O_RDWR); + int fd = open(input_devs[i], O_RDWR | O_CLOEXEC); if(fd < 0) { diff --git a/firmware/target/hosted/fiio/system-fiio.c b/firmware/target/hosted/fiio/system-fiio.c index 5e638989a1..f011ceea75 100644 --- a/firmware/target/hosted/fiio/system-fiio.c +++ b/firmware/target/hosted/fiio/system-fiio.c @@ -94,7 +94,7 @@ void power_off(void) { backlight_hw_off(); - axp_hw = open("/dev/axp173", O_RDWR); + axp_hw = open("/dev/axp173", O_RDWR | O_CLOEXEC); if(axp_hw < 0) panicf("Cannot open '/dev/axp173'"); diff --git a/firmware/target/hosted/filesystem-app.c b/firmware/target/hosted/filesystem-app.c index f291ece06d..4e2fa8db7b 100644 --- a/firmware/target/hosted/filesystem-app.c +++ b/firmware/target/hosted/filesystem-app.c @@ -234,6 +234,7 @@ int app_open(const char *path, int oflag, ...) if (!fpath) FILE_ERROR_RETURN(ENAMETOOLONG, -1); + oflag |= O_CLOEXEC; return os_open(fpath, oflag __OPEN_MODE_ARG); } diff --git a/firmware/target/hosted/filesystem-unix.c b/firmware/target/hosted/filesystem-unix.c index 907d6ab14e..177cb574e0 100644 --- a/firmware/target/hosted/filesystem-unix.c +++ b/firmware/target/hosted/filesystem-unix.c @@ -69,7 +69,7 @@ int os_relate(const char *ospath1, const char *ospath2) } /* First file must stay open for duration so that its stats don't change */ - int fd1 = os_open(ospath1, O_RDONLY); + int fd1 = os_open(ospath1, O_RDONLY | O_CLOEXEC); if (fd1 < 0) return -2; @@ -144,7 +144,7 @@ int os_relate(const char *ospath1, const char *ospath2) bool os_file_exists(const char *ospath) { - int sim_fd = os_open(ospath, O_RDONLY, 0); + int sim_fd = os_open(ospath, O_RDONLY | O_CLOEXEC, 0); if (sim_fd < 0) return false; @@ -157,7 +157,7 @@ bool os_file_exists(const char *ospath) int os_opendirfd(const char *osdirname) { - return os_open(osdirname, O_RDONLY); + return os_open(osdirname, O_RDONLY | O_CLOEXEC); } int os_opendir_and_fd(const char *osdirname, DIR **osdirpp, int *osfdp) diff --git a/firmware/target/hosted/ibasso/sysfs-ibasso.c b/firmware/target/hosted/ibasso/sysfs-ibasso.c index 5269c16704..8f62e3fec2 100644 --- a/firmware/target/hosted/ibasso/sysfs-ibasso.c +++ b/firmware/target/hosted/ibasso/sysfs-ibasso.c @@ -122,7 +122,7 @@ static const char* SYSFS_PATHS[] = static FILE* open_read(const char* file_name) { - FILE *f = fopen(file_name, "r"); + FILE *f = fopen(file_name, "re"); if(f == NULL) { DEBUGF("ERROR %s: Can not open %s for reading.", __func__, file_name); @@ -134,7 +134,7 @@ static FILE* open_read(const char* file_name) static FILE* open_write(const char* file_name) { - FILE *f = fopen(file_name, "w"); + FILE *f = fopen(file_name, "we"); if(f == NULL) { DEBUGF("ERROR %s: Can not open %s for writing.", __func__, file_name); diff --git a/firmware/target/hosted/lcd-linuxfb.c b/firmware/target/hosted/lcd-linuxfb.c index e85b920fef..14c8c30f89 100644 --- a/firmware/target/hosted/lcd-linuxfb.c +++ b/firmware/target/hosted/lcd-linuxfb.c @@ -47,17 +47,12 @@ static void redraw(void) void lcd_init_device(void) { const char * const fb_dev = "/dev/fb0"; - fd = open(fb_dev, O_RDWR /* | O_SYNC */); + fd = open(fb_dev, O_RDWR | O_CLOEXEC); if(fd < 0) { panicf("Cannot open framebuffer: %s\n", fb_dev); } - if (fcntl( fd, F_SETFD, FD_CLOEXEC ) < 0) - { - panicf("Can't set CLOEXEC"); - } - /* get fixed and variable information */ if(ioctl(fd, FBIOGET_FSCREENINFO, &finfo) < 0) { diff --git a/firmware/target/hosted/rtc.c b/firmware/target/hosted/rtc.c index ced298a5c8..e747aece38 100644 --- a/firmware/target/hosted/rtc.c +++ b/firmware/target/hosted/rtc.c @@ -62,7 +62,7 @@ int rtc_write_datetime(const struct tm *tm) tm_time = gmtime(&now); /* Try to write the HW RTC, if present. */ - int rtc = open("/dev/rtc0", O_WRONLY); + int rtc = open("/dev/rtc0", O_WRONLY | O_CLOEXEC); if (rtc > 0) { ioctl(rtc, RTC_SET_TIME, (struct rtc_time *)tm_time); close(rtc); @@ -79,7 +79,7 @@ void rtc_set_alarm(int h, int m) struct rtc_time tm; long sec; - int rtc = open("/dev/rtc0", O_WRONLY); + int rtc = open("/dev/rtc0", O_WRONLY | O_CLOEXEC); if (rtc < 0) return; @@ -124,7 +124,7 @@ void rtc_get_alarm(int *h, int *m) struct rtc_time tm; long sec; - int rtc = open("/dev/rtc0", O_WRONLY); + int rtc = open("/dev/rtc0", O_WRONLY | O_CLOEXEC); if (rtc < 0) return; @@ -157,7 +157,7 @@ void rtc_get_alarm(int *h, int *m) void rtc_enable_alarm(bool enable) { - int rtc = open("/dev/rtc0", O_WRONLY); + int rtc = open("/dev/rtc0", O_WRONLY | O_CLOEXEC); if (rtc < 0) return; @@ -171,7 +171,7 @@ void rtc_enable_alarm(bool enable) /* Returns true if alarm was the reason we started up */ bool rtc_check_alarm_started(bool release_alarm) { - int rtc = open("/dev/rtc0", O_WRONLY); + int rtc = open("/dev/rtc0", O_WRONLY | O_CLOEXEC); if (rtc < 0) return false; @@ -191,7 +191,7 @@ bool rtc_check_alarm_flag(void) { struct rtc_wkalrm alrm; - int rtc = open("/dev/rtc0", O_WRONLY); + int rtc = open("/dev/rtc0", O_WRONLY | O_CLOEXEC); if (rtc < 0) return false; diff --git a/firmware/target/hosted/sdl/pcm-sdl.c b/firmware/target/hosted/sdl/pcm-sdl.c index 9cb35beb48..877ca5a482 100644 --- a/firmware/target/hosted/sdl/pcm-sdl.c +++ b/firmware/target/hosted/sdl/pcm-sdl.c @@ -141,7 +141,7 @@ static void write_to_soundcard(struct pcm_udata *udata) { #ifdef DEBUG if (debug_audio && (udata->debug == NULL)) { - udata->debug = fopen("audiodebug.raw", "ab"); + udata->debug = fopen("audiodebug.raw", "abe"); DEBUGF("Audio debug file open\n"); } #endif @@ -364,7 +364,7 @@ void pcm_play_dma_init(void) #ifdef DEBUG udata.debug = NULL; if (debug_audio) { - udata.debug = fopen("audiodebug.raw", "wb"); + udata.debug = fopen("audiodebug.raw", "wbe"); DEBUGF("Audio debug file open\n"); } #endif diff --git a/firmware/target/hosted/sonynwz/system-nwz.c b/firmware/target/hosted/sonynwz/system-nwz.c index b20ee71774..c10c26250b 100644 --- a/firmware/target/hosted/sonynwz/system-nwz.c +++ b/firmware/target/hosted/sonynwz/system-nwz.c @@ -49,7 +49,7 @@ static void compute_kern_mod_list(void) kern_mod_list = malloc(sizeof(const char **)); kern_mod_list[0] = NULL; /* read from proc file system */ - FILE *f = fopen("/proc/modules", "r"); + FILE *f = fopen("/proc/modules", "re"); if(f == NULL) { printf("Cannot open /proc/modules"); @@ -94,7 +94,7 @@ static void dump_proc_map(void) { const char *file = "/proc/self/maps"; printf("Dumping %s...\n", file); - FILE *f = fopen(file, "r"); + FILE *f = fopen(file, "re"); if(f == NULL) { perror("Cannot open file"); diff --git a/firmware/target/hosted/sysfs.c b/firmware/target/hosted/sysfs.c index 177f338911..1c3fe8c396 100644 --- a/firmware/target/hosted/sysfs.c +++ b/firmware/target/hosted/sysfs.c @@ -32,7 +32,7 @@ static FILE* open_read(const char *file_name) { - FILE *f = fopen(file_name, "r"); + FILE *f = fopen(file_name, "re"); if(f == NULL) { DEBUGF("ERROR %s: Can not open %s for reading.", __func__, file_name); @@ -44,7 +44,7 @@ static FILE* open_read(const char *file_name) static FILE* open_write(const char* file_name) { - FILE *f = fopen(file_name, "w"); + FILE *f = fopen(file_name, "we"); if(f == NULL) { DEBUGF("ERROR %s: Can not open %s for writing.", __func__, file_name); diff --git a/firmware/target/hosted/xduoo/button-xduoo.c b/firmware/target/hosted/xduoo/button-xduoo.c index 503e49e001..3dc1bdc13a 100644 --- a/firmware/target/hosted/xduoo/button-xduoo.c +++ b/firmware/target/hosted/xduoo/button-xduoo.c @@ -84,7 +84,7 @@ void button_init_device(void) for(int i = 0; i < NR_POLL_DESC; i++) { - int fd = open(input_devs[i], O_RDWR); + int fd = open(input_devs[i], O_RDWR | O_CLOEXEC); if(fd < 0) { -- cgit v1.2.3