| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <dirent.h> | 5 #include <dirent.h> |
| 6 #include <errno.h> | 6 #include <errno.h> |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stdarg.h> | 8 #include <stdarg.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 return false; | 108 return false; |
| 109 } | 109 } |
| 110 va_start(ap, len); | 110 va_start(ap, len); |
| 111 for (int *fd, i = 0; (fd = va_arg(ap, int *)) != NULL; ++i) { | 111 for (int *fd, i = 0; (fd = va_arg(ap, int *)) != NULL; ++i) { |
| 112 *fd = (reinterpret_cast<int *>(CMSG_DATA(cmsg)))[i]; | 112 *fd = (reinterpret_cast<int *>(CMSG_DATA(cmsg)))[i]; |
| 113 } | 113 } |
| 114 va_end(ap); | 114 va_end(ap); |
| 115 return true; | 115 return true; |
| 116 } | 116 } |
| 117 | 117 |
| 118 void Util::CloseAllBut(int fd, ...) { | 118 void Util::CloseAllBut(int proc_fd, int fd, ...) { |
| 119 int proc_fd; | |
| 120 int fdir; | 119 int fdir; |
| 121 if ((proc_fd = Sandbox::proc_fd()) < 0 || | 120 if (proc_fd < 0) { |
| 121 proc_fd = open("/proc/", O_RDONLY|O_DIRECTORY); |
| 122 } |
| 123 if (proc_fd < 0 || |
| 122 (fdir = openat(proc_fd, "self/fd", O_RDONLY|O_DIRECTORY)) < 0) { | 124 (fdir = openat(proc_fd, "self/fd", O_RDONLY|O_DIRECTORY)) < 0) { |
| 123 SANDBOX_DIE("Cannot access \"/proc/self/fd\""); | 125 SANDBOX_DIE("Cannot access \"/proc/self/fd\""); |
| 124 } | 126 } |
| 125 int dev_null = open("/dev/null", O_RDWR); | 127 int dev_null = open("/dev/null", O_RDWR); |
| 126 DIR *dir = fdopendir(fdir); | 128 DIR *dir = fdopendir(fdir); |
| 127 struct dirent de, *res; | 129 struct dirent de, *res; |
| 128 while (!readdir_r(dir, &de, &res) && res) { | 130 while (!readdir_r(dir, &de, &res) && res) { |
| 129 if (res->d_name[0] < '0') { | 131 if (res->d_name[0] < '0') { |
| 130 continue; | 132 continue; |
| 131 } | 133 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 155 } | 157 } |
| 156 } | 158 } |
| 157 closedir(dir); | 159 closedir(dir); |
| 158 if (dev_null >= 0) { | 160 if (dev_null >= 0) { |
| 159 if (HANDLE_EINTR(close(dev_null))) { } | 161 if (HANDLE_EINTR(close(dev_null))) { } |
| 160 } | 162 } |
| 161 return; | 163 return; |
| 162 } | 164 } |
| 163 | 165 |
| 164 } // namespace | 166 } // namespace |
| OLD | NEW |