blob: 3e4585ed187109b906102236e46b0a497067d64b [file] [log] [blame]
Garrick Evanscf036f32018-12-21 12:56:59 +09001// Copyright 2019 The Chromium OS 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
Garrick Evans3388a032020-03-24 11:25:55 +09005#include "patchpanel/scoped_ns.h"
Garrick Evanscf036f32018-12-21 12:56:59 +09006
7#include <fcntl.h>
8#include <sched.h>
Hugo Benichi0781d402021-02-22 13:43:11 +09009#include <sys/stat.h>
Jie Jiangf6799312021-05-14 16:27:03 +090010#include <sys/types.h>
Garrick Evanscf036f32018-12-21 12:56:59 +090011
12#include <string>
Jie Jiangf6799312021-05-14 16:27:03 +090013#include <utility>
14
15#include <base/memory/ptr_util.h>
Garrick Evanscf036f32018-12-21 12:56:59 +090016
Garrick Evans3388a032020-03-24 11:25:55 +090017namespace patchpanel {
Garrick Evanscf036f32018-12-21 12:56:59 +090018
Jie Jiangf6799312021-05-14 16:27:03 +090019std::unique_ptr<ScopedNS> ScopedNS::EnterMountNS(pid_t pid) {
20 int nstype = CLONE_NEWNS;
21 const std::string current_path = "/proc/self/ns/mnt";
22 const std::string target_path = "/proc/" + std::to_string(pid) + "/ns/mnt";
23 auto ns = base::WrapUnique(new ScopedNS(nstype, current_path, target_path));
24 return ns->valid_ ? std::move(ns) : nullptr;
25}
Hugo Benichi0781d402021-02-22 13:43:11 +090026
Jie Jiangf6799312021-05-14 16:27:03 +090027std::unique_ptr<ScopedNS> ScopedNS::EnterNetworkNS(pid_t pid) {
28 int nstype = CLONE_NEWNET;
29 const std::string current_path = "/proc/self/ns/net";
30 const std::string target_path = "/proc/" + std::to_string(pid) + "/ns/net";
31 auto ns = base::WrapUnique(new ScopedNS(nstype, current_path, target_path));
32 return ns->valid_ ? std::move(ns) : nullptr;
33}
34
35std::unique_ptr<ScopedNS> ScopedNS::EnterNetworkNS(
36 const std::string& netns_name) {
37 int nstype = CLONE_NEWNET;
38 const std::string current_path = "/proc/self/ns/net";
39 const std::string target_path = "/run/netns/" + netns_name;
40 auto ns = base::WrapUnique(new ScopedNS(nstype, current_path, target_path));
41 return ns->valid_ ? std::move(ns) : nullptr;
42}
43
44ScopedNS::ScopedNS(int nstype,
45 const std::string& current_ns_path,
46 const std::string& target_ns_path)
47 : nstype_(nstype), valid_(false) {
Hugo Benichi0781d402021-02-22 13:43:11 +090048 ns_fd_.reset(open(target_ns_path.c_str(), O_RDONLY | O_CLOEXEC));
Garrick Evanscf036f32018-12-21 12:56:59 +090049 if (!ns_fd_.is_valid()) {
Hugo Benichi0781d402021-02-22 13:43:11 +090050 PLOG(ERROR) << "Could not open namespace " << target_ns_path;
Garrick Evanscf036f32018-12-21 12:56:59 +090051 return;
52 }
Hugo Benichi0781d402021-02-22 13:43:11 +090053 self_fd_.reset(open(current_ns_path.c_str(), O_RDONLY | O_CLOEXEC));
Garrick Evanscf036f32018-12-21 12:56:59 +090054 if (!self_fd_.is_valid()) {
Hugo Benichi0781d402021-02-22 13:43:11 +090055 PLOG(ERROR) << "Could not open host namespace " << current_ns_path;
Garrick Evanscf036f32018-12-21 12:56:59 +090056 return;
57 }
Hugo Benichi0781d402021-02-22 13:43:11 +090058 if (setns(ns_fd_.get(), nstype_) != 0) {
59 PLOG(ERROR) << "Could not enter namespace " << target_ns_path;
Garrick Evanscf036f32018-12-21 12:56:59 +090060 return;
61 }
62 valid_ = true;
63}
64
65ScopedNS::~ScopedNS() {
66 if (valid_) {
Hugo Benichi0781d402021-02-22 13:43:11 +090067 if (setns(self_fd_.get(), nstype_) != 0)
68 PLOG(FATAL) << "Could not re-enter host namespace type " << nstype_;
Garrick Evanscf036f32018-12-21 12:56:59 +090069 }
70}
71
Garrick Evans3388a032020-03-24 11:25:55 +090072} // namespace patchpanel