sandboxed-api/sandboxed_api/sandbox2/testcases/close_fds.cc
Wiktor Garbacz 127176d72f Bulk IWYU and build_cleaner fixes
PiperOrigin-RevId: 559733768
Change-Id: Ia38f4c176e9f0abbfdb3a8f1109f482d8870eb0f
2023-08-24 06:23:36 -07:00

33 lines
763 B
C++

#include <fcntl.h>
#include <linux/fs.h>
#include <cerrno>
#include "absl/container/flat_hash_set.h"
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/strings/numbers.h"
#include "sandboxed_api/sandbox2/sanitizer.h"
bool IsFdOpen(int fd) {
int ret = fcntl(fd, F_GETFD);
if (ret == -1) {
CHECK(errno == EBADF);
return false;
}
return true;
}
int main(int argc, char* argv[]) {
absl::flat_hash_set<int> exceptions;
for (int i = 0; i < argc; ++i) {
int fd;
CHECK(absl::SimpleAtoi(argv[i], &fd));
exceptions.insert(fd);
}
CHECK(sandbox2::sanitizer::CloseAllFDsExcept(exceptions).ok());
for (int i = 0; i < INR_OPEN_MAX; i++) {
CHECK_EQ(IsFdOpen(i), exceptions.find(i) != exceptions.end());
}
}