Restructured ensuredir and other small fixes

This commit is contained in:
remoof 2021-07-16 23:55:59 +02:00
parent ea15c5631a
commit 66e2cb0c42

View file

@ -14,12 +14,16 @@ int getpstartts(int pid, unsigned long long* startts) {
snprintf(path, sizeof(path), "/proc/%d/stat", pid); snprintf(path, sizeof(path), "/proc/%d/stat", pid);
int fd = open(path, O_RDONLY); int fd = open(path, O_RDONLY);
if (fd < 0) if (fd < 0) {
close(fd);
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) if (bytes_read < 0) {
close(fd);
err(1, "Could not read %s", path); err(1, "Could not read %s", path);
}
fc[bytes_read] = '\0'; fc[bytes_read] = '\0';
close(fd); close(fd);
@ -31,7 +35,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)
@ -54,22 +58,28 @@ int ensuredir(int recur) {
if (fd < 0) { if (fd < 0) {
if (errno == ENOENT) { if (errno == ENOENT) {
if (mkdir("/run/rdo", 0700) < 0) if (mkdir("/run/rdo", 0700) < 0) {
close(fd);
err(1, "Could not create /run/rdo"); err(1, "Could not create /run/rdo");
}
close(fd); close(fd);
return ensuredir(++recur); return ensuredir(++recur);
} } else {
else close(fd);
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;
} }
@ -87,15 +97,20 @@ void setsession(int pid, unsigned int ts_ttl) {
int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0700); int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0700);
if (fd < 0) { if (fd < 0) {
if (errno == EEXIST) if (errno == EEXIST) {
close(fd);
return; return;
}
close(fd);
err(1, "Could not open %s", path); err(1, "Could not open %s", path);
} }
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);
@ -116,12 +131,18 @@ int getsession(int pid, unsigned int ts_ttl) {
int fd = open(path, O_RDONLY); int fd = open(path, O_RDONLY);
if (fd < 0) { if (fd < 0) {
if (errno == ENOENT) if (errno == ENOENT) {
close(fd);
return -1; return -1;
}
close(fd);
err(1, "Could not open %s", path); err(1, "Could not open %s", path);
} }
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';