| OLD | NEW |
| (Empty) | |
| 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/sandbox_bpf.h" |
| 9 |
| 10 |
| 11 namespace playground2 { |
| 12 |
| 13 class Die { |
| 14 public: |
| 15 // We use macros so that the pre-processor can insert appropriate file names |
| 16 // and line numbers, when outputting error messages. |
| 17 #if defined(SECCOMP_BPF_STANDALONE) |
| 18 #define SECCOMP_BPF_FAIL_MACRO(m) \ |
| 19 do { \ |
| 20 Die::LogToStderr(m, __FILE__, __LINE__); \ |
| 21 } while (false) |
| 22 #else |
| 23 #define SECCOMP_BPF_FAIL_MACRO(m) LOG(FATAL) << (m) |
| 24 #endif |
| 25 |
| 26 // This is the main API for using this file. Prints a error message and |
| 27 // exits with a fatal error. |
| 28 #define SANDBOX_DIE(m) \ |
| 29 do { \ |
| 30 const char *msg = (m); \ |
| 31 if (!Die::CallHandler(msg, __FILE__, __LINE__)) { \ |
| 32 SECCOMP_BPF_FAIL_MACRO(msg); \ |
| 33 } \ |
| 34 for (;;) { \ |
| 35 /* exit_group() should exit our program. After all, it is */ \ |
| 36 /* defined as a function that doesn't return. But things can */ \ |
| 37 /* theoretically go wrong. Especially, since we are dealing */ \ |
| 38 /* with system call filters. Continuing execution would be */ \ |
| 39 /* very bad in most cases where die() gets called. So, if */ \ |
| 40 /* there is no way for us to ask for the program to exit, the */ \ |
| 41 /* next best thing we can do is to loop indefinitely. Maybe, */ \ |
| 42 /* somebody will notice and file a bug... */ \ |
| 43 syscall(__NR_exit_group, 1); \ |
| 44 _exit(1); \ |
| 45 } \ |
| 46 } while (false) |
| 47 |
| 48 // Call back function for custom error handlers. Custom error handlers can be |
| 49 // used when we don't want the default behavior of writing a detailed error |
| 50 // log (possibly including a stack trace). It is normally used, when the |
| 51 // error is handled internally and will never surface to the user. |
| 52 typedef void (*Handler)(const char *msg, const char *file, int line); |
| 53 |
| 54 // Use the constructor to push a custom error handler onto the stack. This |
| 55 // simulates dynamic-scoping in C++. |
| 56 Die(Handler handler) { handlers_.push_back(handler); } |
| 57 ~Die() { handlers_.pop_back(); } |
| 58 |
| 59 // Calls the top-most error handler function or returns false if there is |
| 60 // no such function. |
| 61 static bool CallHandler(const char *msg, const char *file, int line); |
| 62 |
| 63 // Writes a message to stderr. Used as a fall-back choice, if we don't have |
| 64 // any other way to report an error. |
| 65 static void LogToStderr(const char *msg, const char *file, int line); |
| 66 |
| 67 static bool HasCustomHandler() { return !handlers_.empty(); } |
| 68 |
| 69 private: |
| 70 static std::vector<Handler> handlers_; |
| 71 }; |
| 72 |
| 73 } // namespace |
| 74 |
| 75 #endif // SANDBOX_LINUX_SECCOMP_BPF_DIE_H__ |
| OLD | NEW |