 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| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 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 #include "sandbox/linux/services/namespace_utils.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 #include <sched.h> | |
| 9 #include <sys/types.h> | |
| 10 #include <sys/stat.h> | |
| 11 #include <unistd.h> | |
| 12 | |
| 13 #include <string> | |
| 14 | |
| 15 #include "base/files/file_path.h" | |
| 16 #include "base/files/file_util.h" | |
| 17 #include "base/files/scoped_file.h" | |
| 18 #include "base/logging.h" | |
| 19 #include "base/posix/eintr_wrapper.h" | |
| 20 #include "base/strings/stringprintf.h" | |
| 21 #include "base/third_party/valgrind/valgrind.h" | |
| 22 | |
| 23 namespace sandbox { | |
| 24 | |
| 25 namespace { | |
| 26 bool IsRunningOnValgrind() { | |
| 27 return RUNNING_ON_VALGRIND; | |
| 28 } | |
| 29 } // 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
 | |
| 30 | |
| 31 // Write a uid or gid mapping from |id| to |id| in |map_file|. | |
| 32 bool NamespaceUtils::WriteToIdMapFile(const char* map_file, generic_id_t id) { | |
| 33 base::ScopedFD fd(HANDLE_EINTR(open(map_file, O_WRONLY))); | |
| 34 if (!fd.is_valid()) { | |
| 35 return false; | |
| 36 } | |
| 37 | |
| 38 const generic_id_t inside_id = id; | |
| 39 const generic_id_t outside_id = id; | |
| 40 const std::string mapping = | |
| 41 base::StringPrintf("%d %d 1\n", inside_id, outside_id); | |
| 42 const size_t len = mapping.size(); | |
| 43 const ssize_t rc = HANDLE_EINTR(write(fd.get(), mapping.c_str(), len)); | |
| 44 return rc == static_cast<ssize_t>(len); | |
| 45 } | |
| 46 | |
| 47 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
 | |
| 48 // Valgrind will let clone(2) pass-through, but doesn't support unshare(), | |
| 49 // so always consider namespaces unsupported there. | |
| 50 if (IsRunningOnValgrind()) { | |
| 51 return false; | |
| 52 } | |
| 53 | |
| 54 const char* path; | |
| 55 switch (type) { | |
| 56 case CLONE_NEWIPC: | |
| 57 path = "/proc/self/ns/ipc"; | |
| 58 break; | |
| 59 case CLONE_NEWNET: | |
| 60 path = "/proc/self/ns/net"; | |
| 61 break; | |
| 62 case CLONE_NEWNS: | |
| 63 path = "/proc/self/ns/mnt"; | |
| 64 break; | |
| 65 case CLONE_NEWPID: | |
| 66 path = "/proc/self/ns/pid"; | |
| 67 break; | |
| 68 case CLONE_NEWUSER: | |
| 69 path = "/proc/self/ns/user"; | |
| 70 break; | |
| 71 case CLONE_NEWUTS: | |
| 72 path = "/proc/self/ns/uts"; | |
| 73 break; | |
| 74 default: | |
| 75 NOTREACHED(); | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 return base::PathExists(base::FilePath(path)); | |
| 80 } | |
| 81 | |
| 82 } // namespace sandbox | |
| OLD | NEW |