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 "sandbox/linux/seccomp-bpf/port.h" 9 10 11 namespace playground2 { 12 13 class Die { 14 public: 15 // This is the main API for using this file. Prints a error message and 16 // exits with a fatal error. 17 #define SANDBOX_DIE(m) playground2::Die::SandboxDie(m, __FILE__, __LINE__) 18 19 // Adds an informational message to the log file or stderr as appropriate. 20 #define SANDBOX_INFO(m) playground2::Die::SandboxInfo(m, __FILE__, __LINE__) 21 22 // Terminate the program, even if the current sandbox policy prevents some 23 // of the more commonly used functions used for exiting. 24 // Most users would want to call SANDBOX_DIE() instead, as it logs extra 25 // information. But calling ExitGroup() is correct and in some rare cases 26 // preferable. So, we make it part of the public API. 27 static void ExitGroup() __attribute__((noreturn)); 28 29 // This method gets called by SANDBOX_DIE(). There is normally no reason 30 // to call it directly unless you are defining your own exiting macro. 31 static void SandboxDie(const char *msg, const char *file, int line) 32 __attribute__((noreturn)); 33 34 // This method gets called by SANDBOX_INFO(). There is normally no reason 35 // to call it directly unless you are defining your own logging macro. 36 static void SandboxInfo(const char *msg, const char *file, int line); 37 38 // Writes a message to stderr. Used as a fall-back choice, if we don't have 39 // any other way to report an error. 40 static void LogToStderr(const char *msg, const char *file, int line); 41 42 // We generally want to run all exit handlers. This means, on SANDBOX_DIE() 43 // we should be calling LOG(FATAL). But there are some situations where 44 // we just need to print a message and then terminate. This would typically 45 // happen in cases where we consume the error message internally (e.g. in 46 // unit tests or in the supportsSeccompSandbox() method). 47 static void EnableSimpleExit() { simple_exit_ = true; } 48 49 // Sometimes we need to disable all informational messages (e.g. from within 50 // unittests). 51 static void SuppressInfoMessages(bool flag) { suppress_info_ = flag; } 52 53 private: 54 static bool simple_exit_; 55 static bool suppress_info_; 56 57 DISALLOW_IMPLICIT_CONSTRUCTORS(Die); 58 }; 59 60 } // namespace 61 62 #endif // SANDBOX_LINUX_SECCOMP_BPF_DIE_H__ 63