Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Unified Diff: sandbox/linux/seccomp-bpf/die.h

Issue 10833044: Refactored ErrorCode into it's own class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unittest (and new framework to make this possible) Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sandbox/linux/seccomp-bpf/die.h
diff --git a/sandbox/linux/seccomp-bpf/die.h b/sandbox/linux/seccomp-bpf/die.h
new file mode 100644
index 0000000000000000000000000000000000000000..60a6c6f17042a3ed0c887b5961e6fcefb3dd78c7
--- /dev/null
+++ b/sandbox/linux/seccomp-bpf/die.h
@@ -0,0 +1,75 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SANDBOX_LINUX_SECCOMP_BPF_DIE_H__
+#define SANDBOX_LINUX_SECCOMP_BPF_DIE_H__
+
+#include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
+
+
+namespace playground2 {
+
+class Die {
+ public:
+ // We use macros so that the pre-processor can insert appropriate file names
+ // and line numbers, when outputting error messages.
+ #if defined(SECCOMP_BPF_STANDALONE)
+ #define SECCOMP_BPF_FAIL_MACRO(m) \
+ do { \
+ Die::LogToStderr(m, __FILE__, __LINE__); \
+ } while (false)
+ #else
+ #define SECCOMP_BPF_FAIL_MACRO(m) LOG(FATAL) << (m)
+ #endif
+
+ // This is the main API for using this file. Prints a error message and
+ // exits with a fatal error.
+ #define SANDBOX_DIE(m) \
+ do { \
+ const char *msg = (m); \
+ if (!Die::CallHandler(msg, __FILE__, __LINE__)) { \
+ SECCOMP_BPF_FAIL_MACRO(msg); \
+ } \
+ for (;;) { \
+ /* exit_group() should exit our program. After all, it is */ \
+ /* defined as a function that doesn't return. But things can */ \
+ /* theoretically go wrong. Especially, since we are dealing */ \
+ /* with system call filters. Continuing execution would be */ \
+ /* very bad in most cases where die() gets called. So, if */ \
+ /* there is no way for us to ask for the program to exit, the */ \
+ /* next best thing we can do is to loop indefinitely. Maybe, */ \
+ /* somebody will notice and file a bug... */ \
+ syscall(__NR_exit_group, 1); \
+ _exit(1); \
+ } \
+ } while (false)
+
+ // Call back function for custom error handlers. Custom error handlers can be
+ // used when we don't want the default behavior of writing a detailed error
+ // log (possibly including a stack trace). It is normally used, when the
+ // error is handled internally and will never surface to the user.
+ typedef void (*Handler)(const char *msg, const char *file, int line);
+
+ // Use the constructor to push a custom error handler onto the stack. This
+ // simulates dynamic-scoping in C++.
+ Die(Handler handler) { handlers_.push_back(handler); }
+ ~Die() { handlers_.pop_back(); }
+
+ // Calls the top-most error handler function or returns false if there is
+ // no such function.
+ static bool CallHandler(const char *msg, const char *file, int line);
+
+ // Writes a message to stderr. Used as a fall-back choice, if we don't have
+ // any other way to report an error.
+ static void LogToStderr(const char *msg, const char *file, int line);
+
+ static bool HasCustomHandler() { return !handlers_.empty(); }
+
+ private:
+ static std::vector<Handler> handlers_;
+};
+
+} // namespace
+
+#endif // SANDBOX_LINUX_SECCOMP_BPF_DIE_H__

Powered by Google App Engine
This is Rietveld 408576698