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