Home | History | Annotate | Download | only in seccomp-bpf
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_DIE_H__
      6 #define SANDBOX_LINUX_SECCOMP_BPF_DIE_H__
      7 
      8 #include "base/basictypes.h"
      9 
     10 namespace sandbox {
     11 
     12 // This is the main API for using this file. Prints a error message and
     13 // exits with a fatal error. This is not async-signal safe.
     14 #define SANDBOX_DIE(m) sandbox::Die::SandboxDie(m, __FILE__, __LINE__)
     15 
     16 // An async signal safe version of the same API. Won't print the filename
     17 // and line numbers.
     18 #define RAW_SANDBOX_DIE(m) sandbox::Die::RawSandboxDie(m)
     19 
     20 // Adds an informational message to the log file or stderr as appropriate.
     21 #define SANDBOX_INFO(m) sandbox::Die::SandboxInfo(m, __FILE__, __LINE__)
     22 
     23 class Die {
     24  public:
     25   // Terminate the program, even if the current sandbox policy prevents some
     26   // of the more commonly used functions used for exiting.
     27   // Most users would want to call SANDBOX_DIE() instead, as it logs extra
     28   // information. But calling ExitGroup() is correct and in some rare cases
     29   // preferable. So, we make it part of the public API.
     30   static void ExitGroup() __attribute__((noreturn));
     31 
     32   // This method gets called by SANDBOX_DIE(). There is normally no reason
     33   // to call it directly unless you are defining your own exiting macro.
     34   static void SandboxDie(const char* msg, const char* file, int line)
     35       __attribute__((noreturn));
     36 
     37   static void RawSandboxDie(const char* msg) __attribute__((noreturn));
     38 
     39   // This method gets called by SANDBOX_INFO(). There is normally no reason
     40   // to call it directly unless you are defining your own logging macro.
     41   static void SandboxInfo(const char* msg, const char* file, int line);
     42 
     43   // Writes a message to stderr. Used as a fall-back choice, if we don't have
     44   // any other way to report an error.
     45   static void LogToStderr(const char* msg, const char* file, int line);
     46 
     47   // We generally want to run all exit handlers. This means, on SANDBOX_DIE()
     48   // we should be calling LOG(FATAL). But there are some situations where
     49   // we just need to print a message and then terminate. This would typically
     50   // happen in cases where we consume the error message internally (e.g. in
     51   // unit tests or in the supportsSeccompSandbox() method).
     52   static void EnableSimpleExit() { simple_exit_ = true; }
     53 
     54   // Sometimes we need to disable all informational messages (e.g. from within
     55   // unittests).
     56   static void SuppressInfoMessages(bool flag) { suppress_info_ = flag; }
     57 
     58  private:
     59   static bool simple_exit_;
     60   static bool suppress_info_;
     61 
     62   DISALLOW_IMPLICIT_CONSTRUCTORS(Die);
     63 };
     64 
     65 }  // namespace sandbox
     66 
     67 #endif  // SANDBOX_LINUX_SECCOMP_BPF_DIE_H__
     68