everywhere: always use O_CLOEXEC where it makes sense

This commit is contained in:
Lennart Poettering 2014-02-13 14:59:56 +01:00
parent 955d98c9c1
commit c8a202b7d4
14 changed files with 19 additions and 20 deletions

View file

@ -259,7 +259,7 @@ static void do_journal_append(char *file) {
memcpy(p, "BOOTCHART=", 10); memcpy(p, "BOOTCHART=", 10);
f = open(file, O_RDONLY); f = open(file, O_RDONLY|O_CLOEXEC);
if (f < 0) { if (f < 0) {
log_error("Failed to read bootchart data: %m"); log_error("Failed to read bootchart data: %m");
return; return;
@ -356,11 +356,11 @@ int main(int argc, char *argv[]) {
t = time(NULL); t = time(NULL);
strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t)); strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", arg_output_path, datestr); snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", arg_output_path, datestr);
of = fopen(output_file, "w"); of = fopen(output_file, "we");
} }
if (sysfd < 0) if (sysfd < 0)
sysfd = open("/sys", O_RDONLY); sysfd = open("/sys", O_RDONLY|O_CLOEXEC);
if (!build) if (!build)
parse_env_file("/etc/os-release", NEWLINE, parse_env_file("/etc/os-release", NEWLINE,
@ -424,7 +424,7 @@ int main(int argc, char *argv[]) {
t = time(NULL); t = time(NULL);
strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t)); strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", arg_output_path, datestr); snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", arg_output_path, datestr);
of = fopen(output_file, "w"); of = fopen(output_file, "we");
} }
if (!of) { if (!of) {

View file

@ -61,7 +61,7 @@ void log_uptime(void) {
char str[32]; char str[32];
double uptime; double uptime;
f = fopen("/proc/uptime", "r"); f = fopen("/proc/uptime", "re");
if (!f) if (!f)
return; return;

View file

@ -155,7 +155,7 @@ static int process_progress(int fd) {
return -errno; return -errno;
} }
console = fopen("/dev/console", "w"); console = fopen("/dev/console", "we");
if (!console) if (!console)
return -ENOMEM; return -ENOMEM;

View file

@ -130,7 +130,7 @@ ssize_t sendfile_full(int out_fd, const char *fn) {
assert(out_fd > 0); assert(out_fd > 0);
assert(fn); assert(fn);
f = fopen(fn, "r"); f = fopen(fn, "re");
if (!f) if (!f)
return -errno; return -errno;

View file

@ -174,7 +174,7 @@ static int hibernation_partition_size(size_t *size, size_t *used) {
assert(size); assert(size);
assert(used); assert(used);
f = fopen("/proc/swaps", "r"); f = fopen("/proc/swaps", "re");
if (!f) { if (!f) {
log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
"Failed to retrieve open /proc/swaps: %m"); "Failed to retrieve open /proc/swaps: %m");

View file

@ -197,7 +197,7 @@ static void test_orientation(struct udev *udev,
old = get_prev_orientation(dev); old = get_prev_orientation(dev);
fd = open(devpath, O_RDONLY); fd = open(devpath, O_RDONLY|O_CLOEXEC);
if (fd < 0) if (fd < 0)
return; return;

View file

@ -468,7 +468,7 @@ int main(int argc, char *argv[])
goto exit; goto exit;
} }
fd = open(node, O_RDONLY|O_NONBLOCK); fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
if (fd < 0) { if (fd < 0) {
log_error("unable to open '%s'", node); log_error("unable to open '%s'", node);
rc = 1; rc = 1;

View file

@ -930,7 +930,7 @@ int main(int argc, char *argv[])
for (cnt = 20; cnt > 0; cnt--) { for (cnt = 20; cnt > 0; cnt--) {
struct timespec duration; struct timespec duration;
fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL)); fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|(is_mounted(node) ? 0 : O_EXCL));
if (fd >= 0 || errno != EBUSY) if (fd >= 0 || errno != EBUSY)
break; break;
duration.tv_sec = 0; duration.tv_sec = 0;

View file

@ -95,7 +95,7 @@ static int prepare(char *dir, char *filename)
snprintf(buf, sizeof(buf), "%s/%s", dir, filename); snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
fd = open(buf,O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); fd = open(buf,O_RDWR|O_CREAT|O_CLOEXEC, S_IRUSR|S_IWUSR);
if (fd < 0) if (fd < 0)
fprintf(stderr, "Cannot open %s: %m\n", buf); fprintf(stderr, "Cannot open %s: %m\n", buf);

View file

@ -37,7 +37,7 @@ int main(int argc, char** argv)
return 1; return 1;
} }
mtd_fd = open(argv[1], O_RDONLY); mtd_fd = open(argv[1], O_RDONLY|O_CLOEXEC);
if (mtd_fd == -1) { if (mtd_fd == -1) {
perror("open"); perror("open");
exit(-1); exit(-1);

View file

@ -818,7 +818,7 @@ int scsi_std_inquiry(struct udev *udev,
struct stat statbuf; struct stat statbuf;
int err = 0; int err = 0;
fd = open(devname, O_RDONLY | O_NONBLOCK); fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (fd < 0) { if (fd < 0) {
log_debug("scsi_id: cannot open %s: %m", devname); log_debug("scsi_id: cannot open %s: %m", devname);
return 1; return 1;
@ -866,7 +866,7 @@ int scsi_get_serial(struct udev *udev,
for (cnt = 20; cnt > 0; cnt--) { for (cnt = 20; cnt > 0; cnt--) {
struct timespec duration; struct timespec duration;
fd = open(devname, O_RDONLY | O_NONBLOCK); fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (fd >= 0 || errno != EBUSY) if (fd >= 0 || errno != EBUSY)
break; break;
duration.tv_sec = 0; duration.tv_sec = 0;

View file

@ -38,19 +38,18 @@ struct btrfs_ioctl_vol_args {
static int builtin_btrfs(struct udev_device *dev, int argc, char *argv[], bool test) static int builtin_btrfs(struct udev_device *dev, int argc, char *argv[], bool test)
{ {
struct btrfs_ioctl_vol_args args; struct btrfs_ioctl_vol_args args;
int fd; _cleanup_close_ int fd = -1;
int err; int err;
if (argc != 3 || !streq(argv[1], "ready")) if (argc != 3 || !streq(argv[1], "ready"))
return EXIT_FAILURE; return EXIT_FAILURE;
fd = open("/dev/btrfs-control", O_RDWR); fd = open("/dev/btrfs-control", O_RDWR|O_CLOEXEC);
if (fd < 0) if (fd < 0)
return EXIT_FAILURE; return EXIT_FAILURE;
strscpy(args.name, sizeof(args.name), argv[2]); strscpy(args.name, sizeof(args.name), argv[2]);
err = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args); err = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
close(fd);
if (err < 0) if (err < 0)
return EXIT_FAILURE; return EXIT_FAILURE;

View file

@ -51,7 +51,7 @@ static void exec_list(struct udev_enumerate *udev_enumerate, const char *action)
if (dry_run) if (dry_run)
continue; continue;
strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL); strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
fd = open(filename, O_WRONLY); fd = open(filename, O_WRONLY|O_CLOEXEC);
if (fd < 0) if (fd < 0)
continue; continue;
if (write(fd, action, strlen(action)) < 0) if (write(fd, action, strlen(action)) < 0)

View file

@ -734,7 +734,7 @@ static int handle_inotify(struct udev *udev)
log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev)); log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL); strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
fd = open(filename, O_WRONLY); fd = open(filename, O_WRONLY|O_CLOEXEC);
if (fd >= 0) { if (fd >= 0) {
if (write(fd, "change", 6) < 0) if (write(fd, "change", 6) < 0)
log_debug("error writing uevent: %m"); log_debug("error writing uevent: %m");