arc: Move platform2/arc/network/ to platform2/patchpanel
Next step in the arc-networkd -> patchpanel rename, this patch moves the
location of the code.
BUG=b:151879931
TEST=units,flashed image to atlas
TEST=tasts arc.PlayStore, crostini.LaunchTerminal.download
Change-Id: I1b5cf8d670e1631d46f6449b725395157bf88dde
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2115863
Tested-by: Garrick Evans <garrick@chromium.org>
Commit-Queue: Garrick Evans <garrick@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: Eric Caruso <ejcaruso@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Hugo Benichi <hugobenichi@google.com>
diff --git a/patchpanel/scoped_ns.cc b/patchpanel/scoped_ns.cc
new file mode 100644
index 0000000..3fae72c
--- /dev/null
+++ b/patchpanel/scoped_ns.cc
@@ -0,0 +1,43 @@
+// Copyright 2019 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "patchpanel/scoped_ns.h"
+
+#include <fcntl.h>
+#include <sched.h>
+
+#include <string>
+
+#include <base/strings/stringprintf.h>
+
+namespace patchpanel {
+
+ScopedNS::ScopedNS(pid_t pid) : valid_(false) {
+ const std::string filename =
+ base::StringPrintf("/proc/%d/ns/net", static_cast<int>(pid));
+ ns_fd_.reset(open(filename.c_str(), O_RDONLY));
+ if (!ns_fd_.is_valid()) {
+ PLOG(ERROR) << "Could not open " << filename;
+ return;
+ }
+ self_fd_.reset(open("/proc/self/ns/net", O_RDONLY));
+ if (!self_fd_.is_valid()) {
+ PLOG(ERROR) << "Could not open host netns";
+ return;
+ }
+ if (setns(ns_fd_.get(), CLONE_NEWNET) != 0) {
+ PLOG(ERROR) << "Could not enter netns for " << pid;
+ return;
+ }
+ valid_ = true;
+}
+
+ScopedNS::~ScopedNS() {
+ if (valid_) {
+ if (setns(self_fd_.get(), CLONE_NEWNET) != 0)
+ PLOG(FATAL) << "Could not enter host ns";
+ }
+}
+
+} // namespace patchpanel