Open sockets, files etc with FD_CLOEXEC

This commit is contained in:
Daniel Eklöf 2024-10-23 09:36:59 +02:00
parent 650d1f13f9
commit a367895dc6
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
12 changed files with 32 additions and 32 deletions

2
main.c
View file

@ -87,7 +87,7 @@ get_config_path(void)
static struct bar *
load_bar(const char *config_path, enum bar_backend backend)
{
FILE *conf_file = fopen(config_path, "r");
FILE *conf_file = fopen(config_path, "re");
if (conf_file == NULL) {
LOG_ERRNO("%s: failed to open", config_path);
return NULL;

View file

@ -112,13 +112,13 @@ readint_from_fd(int fd)
static int
initialize(struct private *m)
{
int backlight_fd = open("/sys/class/backlight", O_RDONLY);
int backlight_fd = open("/sys/class/backlight", O_RDONLY | O_CLOEXEC);
if (backlight_fd == -1) {
LOG_ERRNO("/sys/class/backlight");
return -1;
}
int base_dir_fd = openat(backlight_fd, m->device, O_RDONLY);
int base_dir_fd = openat(backlight_fd, m->device, O_RDONLY | O_CLOEXEC);
close(backlight_fd);
if (base_dir_fd == -1) {
@ -126,7 +126,7 @@ initialize(struct private *m)
return -1;
}
int max_fd = openat(base_dir_fd, "max_brightness", O_RDONLY);
int max_fd = openat(base_dir_fd, "max_brightness", O_RDONLY | O_CLOEXEC);
if (max_fd == -1) {
LOG_ERRNO("/sys/class/backlight/%s/max_brightness", m->device);
close(base_dir_fd);
@ -136,7 +136,7 @@ initialize(struct private *m)
m->max_brightness = readint_from_fd(max_fd);
close(max_fd);
int current_fd = openat(base_dir_fd, "brightness", O_RDONLY);
int current_fd = openat(base_dir_fd, "brightness", O_RDONLY | O_CLOEXEC);
close(base_dir_fd);
if (current_fd == -1) {

View file

@ -259,13 +259,13 @@ initialize(struct private *m)
{
char line_buf[512];
int pw_fd = open("/sys/class/power_supply", O_RDONLY);
int pw_fd = open("/sys/class/power_supply", O_RDONLY | O_CLOEXEC);
if (pw_fd < 0) {
LOG_ERRNO("/sys/class/power_supply");
return false;
}
int base_dir_fd = openat(pw_fd, m->battery, O_RDONLY);
int base_dir_fd = openat(pw_fd, m->battery, O_RDONLY | O_CLOEXEC);
close(pw_fd);
if (base_dir_fd < 0) {
@ -274,7 +274,7 @@ initialize(struct private *m)
}
{
int fd = openat(base_dir_fd, "manufacturer", O_RDONLY);
int fd = openat(base_dir_fd, "manufacturer", O_RDONLY | O_CLOEXEC);
if (fd == -1) {
LOG_WARN("/sys/class/power_supply/%s/manufacturer: %s", m->battery, strerror(errno));
m->manufacturer = NULL;
@ -285,7 +285,7 @@ initialize(struct private *m)
}
{
int fd = openat(base_dir_fd, "model_name", O_RDONLY);
int fd = openat(base_dir_fd, "model_name", O_RDONLY | O_CLOEXEC);
if (fd == -1) {
LOG_WARN("/sys/class/power_supply/%s/model_name: %s", m->battery, strerror(errno));
m->model = NULL;
@ -298,7 +298,7 @@ initialize(struct private *m)
if (faccessat(base_dir_fd, "energy_full_design", O_RDONLY, 0) == 0
&& faccessat(base_dir_fd, "energy_full", O_RDONLY, 0) == 0) {
{
int fd = openat(base_dir_fd, "energy_full_design", O_RDONLY);
int fd = openat(base_dir_fd, "energy_full_design", O_RDONLY | O_CLOEXEC);
if (fd == -1) {
LOG_ERRNO("/sys/class/power_supply/%s/energy_full_design", m->battery);
goto err;
@ -309,7 +309,7 @@ initialize(struct private *m)
}
{
int fd = openat(base_dir_fd, "energy_full", O_RDONLY);
int fd = openat(base_dir_fd, "energy_full", O_RDONLY | O_CLOEXEC);
if (fd == -1) {
LOG_ERRNO("/sys/class/power_supply/%s/energy_full", m->battery);
goto err;
@ -325,7 +325,7 @@ initialize(struct private *m)
if (faccessat(base_dir_fd, "charge_full_design", O_RDONLY, 0) == 0
&& faccessat(base_dir_fd, "charge_full", O_RDONLY, 0) == 0) {
{
int fd = openat(base_dir_fd, "charge_full_design", O_RDONLY);
int fd = openat(base_dir_fd, "charge_full_design", O_RDONLY | O_CLOEXEC);
if (fd == -1) {
LOG_ERRNO("/sys/class/power_supply/%s/charge_full_design", m->battery);
goto err;
@ -336,7 +336,7 @@ initialize(struct private *m)
}
{
int fd = openat(base_dir_fd, "charge_full", O_RDONLY);
int fd = openat(base_dir_fd, "charge_full", O_RDONLY | O_CLOEXEC);
if (fd == -1) {
LOG_ERRNO("/sys/class/power_supply/%s/charge_full", m->battery);
goto err;
@ -362,13 +362,13 @@ update_status(struct module *mod)
{
struct private *m = mod->private;
int pw_fd = open("/sys/class/power_supply", O_RDONLY);
int pw_fd = open("/sys/class/power_supply", O_RDONLY | O_CLOEXEC);
if (pw_fd < 0) {
LOG_ERRNO("/sys/class/power_supply");
return false;
}
int base_dir_fd = openat(pw_fd, m->battery, O_RDONLY);
int base_dir_fd = openat(pw_fd, m->battery, O_RDONLY | O_CLOEXEC);
close(pw_fd);
if (base_dir_fd < 0) {
@ -376,14 +376,14 @@ update_status(struct module *mod)
return false;
}
int status_fd = openat(base_dir_fd, "status", O_RDONLY);
int status_fd = openat(base_dir_fd, "status", O_RDONLY | O_CLOEXEC);
if (status_fd < 0) {
LOG_ERRNO("/sys/class/power_supply/%s/status", m->battery);
close(base_dir_fd);
return false;
}
int capacity_fd = openat(base_dir_fd, "capacity", O_RDONLY);
int capacity_fd = openat(base_dir_fd, "capacity", O_RDONLY | O_CLOEXEC);
if (capacity_fd < 0) {
LOG_ERRNO("/sys/class/power_supply/%s/capacity", m->battery);
close(status_fd);
@ -391,12 +391,12 @@ update_status(struct module *mod)
return false;
}
int energy_fd = openat(base_dir_fd, "energy_now", O_RDONLY);
int power_fd = openat(base_dir_fd, "power_now", O_RDONLY);
int charge_fd = openat(base_dir_fd, "charge_now", O_RDONLY);
int current_fd = openat(base_dir_fd, "current_now", O_RDONLY);
int time_to_empty_fd = openat(base_dir_fd, "time_to_empty_now", O_RDONLY);
int time_to_full_fd = openat(base_dir_fd, "time_to_full_now", O_RDONLY);
int energy_fd = openat(base_dir_fd, "energy_now", O_RDONLY | O_CLOEXEC);
int power_fd = openat(base_dir_fd, "power_now", O_RDONLY | O_CLOEXEC);
int charge_fd = openat(base_dir_fd, "charge_now", O_RDONLY | O_CLOEXEC);
int current_fd = openat(base_dir_fd, "current_now", O_RDONLY | O_CLOEXEC);
int time_to_empty_fd = openat(base_dir_fd, "time_to_empty_now", O_RDONLY | O_CLOEXEC);
int time_to_full_fd = openat(base_dir_fd, "time_to_full_now", O_RDONLY | O_CLOEXEC);
long capacity = readint_from_fd(capacity_fd);
long energy = energy_fd >= 0 ? readint_from_fd(energy_fd) : -1;

View file

@ -124,7 +124,7 @@ refresh_cpu_stats(struct cpu_stats *cpu_stats, size_t core_count)
size_t len = 0;
ssize_t read;
fp = fopen("/proc/stat", "r");
fp = fopen("/proc/stat", "re");
if (NULL == fp) {
LOG_ERRNO("unable to open /proc/stat");
return;

View file

@ -105,7 +105,7 @@ refresh_device_stats(struct private *m)
size_t len = 0;
ssize_t read;
fp = fopen("/proc/diskstats", "r");
fp = fopen("/proc/diskstats", "re");
if (NULL == fp) {
LOG_ERRNO("unable to open /proc/diskstats");
return;

View file

@ -330,7 +330,7 @@ run_init(int *inotify_fd, int *inotify_wd, FILE **file, char *dwl_info_filename)
return 1;
}
*file = fopen(dwl_info_filename, "r");
*file = fopen(dwl_info_filename, "re");
if (*file == NULL) {
inotify_rm_watch(*inotify_fd, *inotify_wd);
close(*inotify_fd);

View file

@ -664,7 +664,7 @@ handle_window_event(int sock, int type, const struct json_object *json, void *_m
char path[64];
snprintf(path, sizeof(path), "/proc/%u/comm", ws->window.pid);
int fd = open(path, O_RDONLY);
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd == -1) {
/* Application may simply have terminated */
free(ws->window.application);

View file

@ -54,7 +54,7 @@ get_mem_stats(uint64_t *mem_free, uint64_t *mem_total)
size_t len = 0;
ssize_t read = 0;
fp = fopen("/proc/meminfo", "r");
fp = fopen("/proc/meminfo", "re");
if (NULL == fp) {
LOG_ERRNO("unable to open /proc/meminfo");
return false;

View file

@ -1576,7 +1576,7 @@ out:
static struct module *
network_new(struct particle *label, int poll_interval, int left_spacing, int right_spacing)
{
int urandom_fd = open("/dev/urandom", O_RDONLY);
int urandom_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
if (urandom_fd < 0) {
LOG_ERRNO("failed to open /dev/urandom");
return NULL;

View file

@ -438,7 +438,7 @@ run(struct module *mod)
}
// Create refresh timer.
priv->refresh_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
priv->refresh_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
if (priv->refresh_timer_fd < 0) {
LOG_ERRNO("failed to create timerfd");
pa_mainloop_free(priv->mainloop);

View file

@ -162,7 +162,7 @@ static void
find_mount_points(const char *dev_path, mount_point_list_t *mount_points)
{
int fd = open("/proc/self/mountinfo", O_RDONLY | O_CLOEXEC);
FILE *f = fd >= 0 ? fdopen(fd, "r") : NULL;
FILE *f = fd >= 0 ? fdopen(fd, "re") : NULL;
if (fd < 0 || f == NULL) {
LOG_ERRNO("failed to open /proc/self/mountinfo");

View file

@ -130,7 +130,7 @@ update_application(struct module *mod)
char path[1024];
snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
int fd = open(path, O_RDONLY);
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd == -1)
return;