From d44301d8fa5f3c6748ff22bc001231632387a7b5 Mon Sep 17 00:00:00 2001 From: remoof Date: Sat, 17 Jul 2021 15:31:18 +0200 Subject: [PATCH] Close file descriptors earlier --- sessions.h | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/sessions.h b/sessions.h index de18583..13c4fc3 100644 --- a/sessions.h +++ b/sessions.h @@ -20,14 +20,14 @@ int getpstartts(int pid, unsigned long long* startts) { } int bytes_read = read(fd, fc, sizeof(fc)); - if (bytes_read < 0) { - close(fd); - err(1, "Could not read %s", path); - } - fc[bytes_read] = '\0'; close(fd); + if (bytes_read < 0) + err(1, "Could not read %s", path); + + fc[bytes_read] = '\0'; + if (memchr(ptr, '\0', bytes_read) != NULL) return -1; @@ -57,17 +57,14 @@ int ensuredir(int recur) { int fd = open("/run/rdo", O_RDONLY, O_DIRECTORY | O_NOFOLLOW); if (fd < 0) { + close(fd); if (errno == ENOENT) { - if (mkdir("/run/rdo", 0700) < 0) { - close(fd); + if (mkdir("/run/rdo", 0700) < 0) err(1, "Could not create /run/rdo"); - } - close(fd); return ensuredir(++recur); - } else { - close(fd); - err(1, "Could not open /run/rdo"); } + else + err(1, "Could not open /run/rdo"); } if (fstat(fd, &st) < 0) { @@ -97,11 +94,9 @@ void setsession(int pid, unsigned int ts_ttl) { int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0700); if (fd < 0) { - if (errno == EEXIST) { - close(fd); - return; - } close(fd); + if (errno == EEXIST) + return; err(1, "Could not open %s", path); } @@ -131,11 +126,9 @@ int getsession(int pid, unsigned int ts_ttl) { int fd = open(path, O_RDONLY); if (fd < 0) { - if (errno == ENOENT) { - close(fd); - return -1; - } close(fd); + if (errno == ENOENT) + return -1; err(1, "Could not open %s", path); }