Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: base/files/file_util_posix.cc

Issue 2687713003: Stop base::OpenFile from leaking fds/handles into child procs. (Closed)
Patch Set: gab comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/file_util.h ('k') | base/files/file_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/files/file_util.h" 5 #include "base/files/file_util.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <libgen.h> 10 #include <libgen.h>
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0) 178 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
179 result = true; 179 result = true;
180 munmap(mapping, pagesize); 180 munmap(mapping, pagesize);
181 } 181 }
182 } 182 }
183 return result; 183 return result;
184 } 184 }
185 #endif // defined(OS_LINUX) 185 #endif // defined(OS_LINUX)
186 #endif // !defined(OS_NACL_NONSFI) 186 #endif // !defined(OS_NACL_NONSFI)
187 187
188 #if !defined(OS_MACOSX)
189 // Appends |mode_char| to |mode| before the optional character set encoding; see
190 // https://www.gnu.org/software/libc/manual/html_node/Opening-Streams.html for
191 // details.
192 std::string AppendModeCharacter(StringPiece mode, char mode_char) {
193 std::string result(mode.as_string());
194 size_t comma_pos = result.find(',');
195 result.insert(comma_pos == std::string::npos ? result.length() : comma_pos, 1,
196 mode_char);
197 return result;
198 }
199 #endif
200
188 } // namespace 201 } // namespace
189 202
190 #if !defined(OS_NACL_NONSFI) 203 #if !defined(OS_NACL_NONSFI)
191 FilePath MakeAbsoluteFilePath(const FilePath& input) { 204 FilePath MakeAbsoluteFilePath(const FilePath& input) {
192 ThreadRestrictions::AssertIOAllowed(); 205 ThreadRestrictions::AssertIOAllowed();
193 char full_path[PATH_MAX]; 206 char full_path[PATH_MAX];
194 if (realpath(input.value().c_str(), full_path) == NULL) 207 if (realpath(input.value().c_str(), full_path) == NULL)
195 return FilePath(); 208 return FilePath();
196 return FilePath(full_path); 209 return FilePath(full_path);
197 } 210 }
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 #if defined(OS_ANDROID) 716 #if defined(OS_ANDROID)
704 } 717 }
705 #endif // defined(OS_ANDROID) 718 #endif // defined(OS_ANDROID)
706 719
707 results->FromStat(file_info); 720 results->FromStat(file_info);
708 return true; 721 return true;
709 } 722 }
710 #endif // !defined(OS_NACL_NONSFI) 723 #endif // !defined(OS_NACL_NONSFI)
711 724
712 FILE* OpenFile(const FilePath& filename, const char* mode) { 725 FILE* OpenFile(const FilePath& filename, const char* mode) {
726 // 'e' is unconditionally added below, so be sure there is not one already
727 // present before a comma in |mode|.
728 DCHECK(
729 strchr(mode, 'e') == nullptr ||
730 (strchr(mode, ',') != nullptr && strchr(mode, 'e') > strchr(mode, ',')));
713 ThreadRestrictions::AssertIOAllowed(); 731 ThreadRestrictions::AssertIOAllowed();
714 FILE* result = NULL; 732 FILE* result = NULL;
733 #if defined(OS_MACOSX)
734 // macOS does not provide a mode character to set O_CLOEXEC; see
735 // https://developer.apple.com/legacy/library/documentation/Darwin/Reference/M anPages/man3/fopen.3.html.
736 const char* the_mode = mode;
737 #else
738 std::string mode_with_e(AppendModeCharacter(mode, 'e'));
739 const char* the_mode = mode_with_e.c_str();
740 #endif
715 do { 741 do {
716 result = fopen(filename.value().c_str(), mode); 742 result = fopen(filename.value().c_str(), the_mode);
717 } while (!result && errno == EINTR); 743 } while (!result && errno == EINTR);
744 #if defined(OS_MACOSX)
745 // Mark the descriptor as close-on-exec.
746 if (result)
747 SetCloseOnExec(fileno(result));
748 #endif
718 return result; 749 return result;
719 } 750 }
720 751
721 // NaCl doesn't implement system calls to open files directly. 752 // NaCl doesn't implement system calls to open files directly.
722 #if !defined(OS_NACL) 753 #if !defined(OS_NACL)
723 FILE* FileToFILE(File file, const char* mode) { 754 FILE* FileToFILE(File file, const char* mode) {
724 FILE* stream = fdopen(file.GetPlatformFile(), mode); 755 FILE* stream = fdopen(file.GetPlatformFile(), mode);
725 if (stream) 756 if (stream)
726 file.TakePlatformFile(); 757 file.TakePlatformFile();
727 return stream; 758 return stream;
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 return false; 1013 return false;
983 1014
984 DeleteFile(from_path, true); 1015 DeleteFile(from_path, true);
985 return true; 1016 return true;
986 } 1017 }
987 1018
988 } // namespace internal 1019 } // namespace internal
989 1020
990 #endif // !defined(OS_NACL_NONSFI) 1021 #endif // !defined(OS_NACL_NONSFI)
991 } // namespace base 1022 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_util.h ('k') | base/files/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698