diff --git a/sessions.h b/sessions.h index 6150515..19cf679 100644 --- a/sessions.h +++ b/sessions.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include #include @@ -18,12 +18,14 @@ int getpstartts(int pid, unsigned long long* startts) { err(1, "Could not open %s", path); int bytes_read = read(fd, fc, sizeof(fc)); - if (bytes_read < 0) - 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; @@ -31,7 +33,7 @@ int getpstartts(int pid, unsigned long long* startts) { char* token = strtok(ptr, " "); - for (short i = 0; i<20 && token; i++) + for (int i = 0; i<20 && token; i++) token = strtok(NULL, " "); if (!token) @@ -56,20 +58,22 @@ int ensuredir(int recur) { if (errno == ENOENT) { if (mkdir("/run/rdo", 0700) < 0) err(1, "Could not create /run/rdo"); - close(fd); return ensuredir(++recur); } else err(1, "Could not open /run/rdo"); - } else { - if (fstat(fd, &st) < 0) - err(1, "Could not fstat /run/rdo"); - - close(fd); - if (st.st_uid != 0 || st.st_mode != (0700 | S_IFDIR)) - return -1; } + if (fstat(fd, &st) < 0) { + close(fd); + err(1, "Could not fstat /run/rdo"); + } + + close(fd); + + if (st.st_uid != 0 || st.st_mode != (0700 | S_IFDIR)) + return -1; + return 0; } @@ -94,8 +98,10 @@ void setsession(int pid, unsigned int ts_ttl) { snprintf(ts_str, sizeof(ts_str), "%llu", (unsigned long long)time(NULL)); - if (write(fd, ts_str, strlen(ts_str)) < 0) + if (write(fd, ts_str, strlen(ts_str)) < 0) { + close(fd); err(1, "Could not write to %s", path); + } close(fd); @@ -122,6 +128,9 @@ int getsession(int pid, unsigned int ts_ttl) { } int bytes_read = read(fd, ts_str, sizeof(ts_str)); + + close(fd); + if (bytes_read < 0) err(1, "Could not read %s", path); ts_str[bytes_read] = '\0';