github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/exec_assert_closed_workload.cc (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  #include <errno.h>
    16  #include <stdlib.h>
    17  #include <sys/stat.h>
    18  #include <unistd.h>
    19  
    20  #include <iostream>
    21  
    22  #include "absl/strings/numbers.h"
    23  
    24  int main(int argc, char** argv) {
    25    if (argc != 2) {
    26      std::cerr << "need two arguments, got " << argc;
    27      exit(1);
    28    }
    29    int fd;
    30    if (!absl::SimpleAtoi(argv[1], &fd)) {
    31      std::cerr << "fd: " << argv[1] << " could not be parsed" << std::endl;
    32      exit(1);
    33    }
    34    struct stat s;
    35    if (fstat(fd, &s) == 0) {
    36      std::cerr << "fd: " << argv[1] << " should not be valid" << std::endl;
    37      exit(2);
    38    }
    39    if (errno != EBADF) {
    40      std::cerr << "fstat fd: " << argv[1] << " got errno: " << errno
    41                << " wanted: " << EBADF << std::endl;
    42      exit(1);
    43    }
    44    return 0;
    45  }