gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/util/signal_util.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 "test/util/signal_util.h"
    16  
    17  #include <signal.h>
    18  #include <sys/signalfd.h>
    19  
    20  #include <ostream>
    21  
    22  #include "gtest/gtest.h"
    23  #include "test/util/cleanup.h"
    24  #include "test/util/posix_error.h"
    25  #include "test/util/test_util.h"
    26  
    27  namespace {
    28  
    29  struct Range {
    30    int start;
    31    int end;
    32  };
    33  
    34  // Format a Range as "start-end" or "start" for single value Ranges.
    35  static ::std::ostream& operator<<(::std::ostream& os, const Range& range) {
    36    if (range.end > range.start) {
    37      return os << range.start << '-' << range.end;
    38    }
    39  
    40    return os << range.start;
    41  }
    42  
    43  }  // namespace
    44  
    45  // Format a sigset_t as a comma separated list of numeric ranges.
    46  // Empty sigset: []
    47  // Full  sigset: [1-31,34-64]
    48  ::std::ostream& operator<<(::std::ostream& os, const sigset_t& sigset) {
    49    const char* delim = "";
    50    Range range = {0, 0};
    51  
    52    os << '[';
    53  
    54    for (int sig = 1; sig <= gvisor::testing::kMaxSignal; ++sig) {
    55      if (sigismember(&sigset, sig)) {
    56        if (range.start) {
    57          range.end = sig;
    58        } else {
    59          range.start = sig;
    60          range.end = sig;
    61        }
    62      } else if (range.start) {
    63        os << delim << range;
    64        delim = ",";
    65        range.start = 0;
    66        range.end = 0;
    67      }
    68    }
    69  
    70    if (range.start) {
    71      os << delim << range;
    72    }
    73  
    74    return os << ']';
    75  }
    76  
    77  namespace gvisor {
    78  namespace testing {
    79  
    80  PosixErrorOr<Cleanup> ScopedSigaction(int sig, struct sigaction const& sa) {
    81    struct sigaction old_sa;
    82    int rc = sigaction(sig, &sa, &old_sa);
    83    MaybeSave();
    84    if (rc < 0) {
    85      return PosixError(errno, "sigaction failed");
    86    }
    87    return Cleanup([sig, old_sa] {
    88      EXPECT_THAT(sigaction(sig, &old_sa, nullptr), SyscallSucceeds());
    89    });
    90  }
    91  
    92  PosixErrorOr<Cleanup> ScopedSignalMask(int how, sigset_t const& set) {
    93    sigset_t old;
    94    int rc = sigprocmask(how, &set, &old);
    95    MaybeSave();
    96    if (rc < 0) {
    97      return PosixError(errno, "sigprocmask failed");
    98    }
    99    return Cleanup([old] {
   100      EXPECT_THAT(sigprocmask(SIG_SETMASK, &old, nullptr), SyscallSucceeds());
   101    });
   102  }
   103  
   104  // Returns a new signalfd.
   105  PosixErrorOr<FileDescriptor> NewSignalFD(sigset_t* mask, int flags) {
   106    int fd = signalfd(-1, mask, flags);
   107    MaybeSave();
   108    if (fd < 0) {
   109      return PosixError(errno, "signalfd");
   110    }
   111    return FileDescriptor(fd);
   112  }
   113  
   114  }  // namespace testing
   115  }  // namespace gvisor