• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <fcntl.h>
2 #include <linux/fs.h>
3 
4 #include <cerrno>
5 
6 #include "absl/container/flat_hash_set.h"
7 #include "absl/log/check.h"
8 #include "absl/status/status.h"
9 #include "absl/strings/numbers.h"
10 #include "sandboxed_api/sandbox2/sanitizer.h"
11 
IsFdOpen(int fd)12 bool IsFdOpen(int fd) {
13   int ret = fcntl(fd, F_GETFD);
14   if (ret == -1) {
15     CHECK(errno == EBADF);
16     return false;
17   }
18   return true;
19 }
20 
main(int argc,char * argv[])21 int main(int argc, char* argv[]) {
22   absl::flat_hash_set<int> exceptions;
23   for (int i = 0; i < argc; ++i) {
24     int fd;
25     CHECK(absl::SimpleAtoi(argv[i], &fd));
26     exceptions.insert(fd);
27   }
28   CHECK(sandbox2::sanitizer::CloseAllFDsExcept(exceptions).ok());
29   for (int i = 0; i < INR_OPEN_MAX; i++) {
30     CHECK_EQ(IsFdOpen(i), exceptions.find(i) != exceptions.end());
31   }
32 }
33