 Chromium Code Reviews
 Chromium Code Reviews Issue 849893004:
  Move a couple of utility functions to a new namespace_utils class.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 849893004:
  Move a couple of utility functions to a new namespace_utils class.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: sandbox/linux/services/namespace_utils.cc | 
| diff --git a/sandbox/linux/services/namespace_utils.cc b/sandbox/linux/services/namespace_utils.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..b6c910aba00a6479b4f24f91683a3850fcb52b19 | 
| --- /dev/null | 
| +++ b/sandbox/linux/services/namespace_utils.cc | 
| @@ -0,0 +1,82 @@ | 
| +// Copyright (c) 2014 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. | 
| + | 
| +#include "sandbox/linux/services/namespace_utils.h" | 
| + | 
| +#include <fcntl.h> | 
| +#include <sched.h> | 
| +#include <sys/types.h> | 
| +#include <sys/stat.h> | 
| +#include <unistd.h> | 
| + | 
| +#include <string> | 
| + | 
| +#include "base/files/file_path.h" | 
| +#include "base/files/file_util.h" | 
| +#include "base/files/scoped_file.h" | 
| +#include "base/logging.h" | 
| +#include "base/posix/eintr_wrapper.h" | 
| +#include "base/strings/stringprintf.h" | 
| +#include "base/third_party/valgrind/valgrind.h" | 
| + | 
| +namespace sandbox { | 
| + | 
| +namespace { | 
| +bool IsRunningOnValgrind() { | 
| + return RUNNING_ON_VALGRIND; | 
| +} | 
| +} // namespace | 
| 
jln (very slow on Chromium)
2015/01/14 00:46:38
Nit: vertical space
 
rickyz (no longer on Chrome)
2015/01/23 01:20:19
Sorry, where should vertical space be added/remove
 | 
| + | 
| +// Write a uid or gid mapping from |id| to |id| in |map_file|. | 
| +bool NamespaceUtils::WriteToIdMapFile(const char* map_file, generic_id_t id) { | 
| + base::ScopedFD fd(HANDLE_EINTR(open(map_file, O_WRONLY))); | 
| + if (!fd.is_valid()) { | 
| + return false; | 
| + } | 
| + | 
| + const generic_id_t inside_id = id; | 
| + const generic_id_t outside_id = id; | 
| + const std::string mapping = | 
| + base::StringPrintf("%d %d 1\n", inside_id, outside_id); | 
| + const size_t len = mapping.size(); | 
| + const ssize_t rc = HANDLE_EINTR(write(fd.get(), mapping.c_str(), len)); | 
| + return rc == static_cast<ssize_t>(len); | 
| +} | 
| + | 
| +bool NamespaceUtils::SupportsLinuxNamespace(int type) { | 
| 
jln (very slow on Chromium)
2015/01/14 00:46:38
This implementation will have some false negatives
 
rickyz (no longer on Chrome)
2015/01/23 00:23:38
Darn, looks like this was added in 3.8. In view of
 
jln (very slow on Chromium)
2015/01/23 00:27:09
As you prefer, but it's also ok to keep as long as
 
rickyz (no longer on Chrome)
2015/01/23 01:20:19
Yeah - on second thought, I prefer your suggestion
 | 
| + // Valgrind will let clone(2) pass-through, but doesn't support unshare(), | 
| + // so always consider namespaces unsupported there. | 
| + if (IsRunningOnValgrind()) { | 
| + return false; | 
| + } | 
| + | 
| + const char* path; | 
| + switch (type) { | 
| + case CLONE_NEWIPC: | 
| + path = "/proc/self/ns/ipc"; | 
| + break; | 
| + case CLONE_NEWNET: | 
| + path = "/proc/self/ns/net"; | 
| + break; | 
| + case CLONE_NEWNS: | 
| + path = "/proc/self/ns/mnt"; | 
| + break; | 
| + case CLONE_NEWPID: | 
| + path = "/proc/self/ns/pid"; | 
| + break; | 
| + case CLONE_NEWUSER: | 
| + path = "/proc/self/ns/user"; | 
| + break; | 
| + case CLONE_NEWUTS: | 
| + path = "/proc/self/ns/uts"; | 
| + break; | 
| + default: | 
| + NOTREACHED(); | 
| + return false; | 
| + } | 
| + | 
| + return base::PathExists(base::FilePath(path)); | 
| +} | 
| + | 
| +} // namespace sandbox |