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:
commit
7df91d5f0b
1 changed files with 23 additions and 14 deletions
37
sessions.h
37
sessions.h
|
@ -1,4 +1,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
@ -18,12 +18,14 @@ int getpstartts(int pid, unsigned long long* startts) {
|
||||||
err(1, "Could not open %s", path);
|
err(1, "Could not open %s", path);
|
||||||
|
|
||||||
int bytes_read = read(fd, fc, sizeof(fc));
|
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);
|
close(fd);
|
||||||
|
|
||||||
|
if (bytes_read < 0)
|
||||||
|
err(1, "Could not read %s", path);
|
||||||
|
|
||||||
|
fc[bytes_read] = '\0';
|
||||||
|
|
||||||
if (memchr(ptr, '\0', bytes_read) != NULL)
|
if (memchr(ptr, '\0', bytes_read) != NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
@ -31,7 +33,7 @@ int getpstartts(int pid, unsigned long long* startts) {
|
||||||
|
|
||||||
char* token = strtok(ptr, " ");
|
char* token = strtok(ptr, " ");
|
||||||
|
|
||||||
for (short i = 0; i<20 && token; i++)
|
for (int i = 0; i<20 && token; i++)
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
|
|
||||||
if (!token)
|
if (!token)
|
||||||
|
@ -56,20 +58,22 @@ int ensuredir(int recur) {
|
||||||
if (errno == ENOENT) {
|
if (errno == ENOENT) {
|
||||||
if (mkdir("/run/rdo", 0700) < 0)
|
if (mkdir("/run/rdo", 0700) < 0)
|
||||||
err(1, "Could not create /run/rdo");
|
err(1, "Could not create /run/rdo");
|
||||||
close(fd);
|
|
||||||
return ensuredir(++recur);
|
return ensuredir(++recur);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
err(1, "Could not open /run/rdo");
|
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;
|
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));
|
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);
|
err(1, "Could not write to %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
|
@ -122,6 +128,9 @@ int getsession(int pid, unsigned int ts_ttl) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int bytes_read = read(fd, ts_str, sizeof(ts_str));
|
int bytes_read = read(fd, ts_str, sizeof(ts_str));
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
if (bytes_read < 0)
|
if (bytes_read < 0)
|
||||||
err(1, "Could not read %s", path);
|
err(1, "Could not read %s", path);
|
||||||
ts_str[bytes_read] = '\0';
|
ts_str[bytes_read] = '\0';
|
||||||
|
|
Loading…
Add table
Reference in a new issue