Merge pull request 'Restructured ensuredir and other small fixes' (#5) from remoof/rdo:main into main

Reviewed-on: https://codeberg.org/sw1tchbl4d3/rdo/pulls/5
This commit is contained in:
sw1tchbl4d3 2021-07-17 15:58:11 +02:00
commit 7df91d5f0b

View file

@ -1,4 +1,4 @@
#pragma once
#pragma once
#include <limits.h>
#include <errno.h>
@ -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';