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

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: addressed review 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
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 // Add |mode_char| immediately before the comma or at the end of the string.
Mark Mentovai 2017/02/11 00:52:29 This comment says basically the same thing as the
grt (UTC plus 2) 2017/02/13 07:53:28 Done.
194 std::string result(mode.as_string());
195 size_t comma_pos = result.find(',');
196 result.insert(comma_pos == std::string::npos ? result.length() : comma_pos, 1,
197 mode_char);
198 return result;
199 }
200 #endif
201
188 } // namespace 202 } // namespace
189 203
190 #if !defined(OS_NACL_NONSFI) 204 #if !defined(OS_NACL_NONSFI)
191 FilePath MakeAbsoluteFilePath(const FilePath& input) { 205 FilePath MakeAbsoluteFilePath(const FilePath& input) {
192 ThreadRestrictions::AssertIOAllowed(); 206 ThreadRestrictions::AssertIOAllowed();
193 char full_path[PATH_MAX]; 207 char full_path[PATH_MAX];
194 if (realpath(input.value().c_str(), full_path) == NULL) 208 if (realpath(input.value().c_str(), full_path) == NULL)
195 return FilePath(); 209 return FilePath();
196 return FilePath(full_path); 210 return FilePath(full_path);
197 } 211 }
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 #if defined(OS_ANDROID) 717 #if defined(OS_ANDROID)
704 } 718 }
705 #endif // defined(OS_ANDROID) 719 #endif // defined(OS_ANDROID)
706 720
707 results->FromStat(file_info); 721 results->FromStat(file_info);
708 return true; 722 return true;
709 } 723 }
710 #endif // !defined(OS_NACL_NONSFI) 724 #endif // !defined(OS_NACL_NONSFI)
711 725
712 FILE* OpenFile(const FilePath& filename, const char* mode) { 726 FILE* OpenFile(const FilePath& filename, const char* mode) {
727 // 'e' is unconditionally added below, so be sure there is not one already
728 // present before a comma in |mode|.
729 DCHECK(
730 strchr(mode, 'e') == nullptr ||
731 (strchr(mode, ',') != nullptr && strchr(mode, 'e') > strchr(mode, ',')));
713 ThreadRestrictions::AssertIOAllowed(); 732 ThreadRestrictions::AssertIOAllowed();
714 FILE* result = NULL; 733 FILE* result = NULL;
734 #if defined(OS_MACOSX)
735 // macOS does not provide a mode character to set O_CLOEXEC; see
736 // https://developer.apple.com/legacy/library/documentation/Darwin/Reference/M anPages/man3/fopen.3.html.
737 const char* the_mode = mode;
738 #else
739 std::string mode_with_e(AppendModeCharacter(mode, 'e'));
740 const char* the_mode = mode_with_e.c_str();
741 #endif
715 do { 742 do {
716 result = fopen(filename.value().c_str(), mode); 743 result = fopen(filename.value().c_str(), the_mode);
717 } while (!result && errno == EINTR); 744 } while (!result && errno == EINTR);
745 #if defined(OS_MACOSX)
746 // Mark the descriptor as close-on-exec.
747 if (result)
748 SetCloseOnExec(fileno(result));
749 #endif
718 return result; 750 return result;
719 } 751 }
720 752
721 // NaCl doesn't implement system calls to open files directly. 753 // NaCl doesn't implement system calls to open files directly.
722 #if !defined(OS_NACL) 754 #if !defined(OS_NACL)
723 FILE* FileToFILE(File file, const char* mode) { 755 FILE* FileToFILE(File file, const char* mode) {
724 FILE* stream = fdopen(file.GetPlatformFile(), mode); 756 FILE* stream = fdopen(file.GetPlatformFile(), mode);
725 if (stream) 757 if (stream)
726 file.TakePlatformFile(); 758 file.TakePlatformFile();
727 return stream; 759 return stream;
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 return false; 1014 return false;
983 1015
984 DeleteFile(from_path, true); 1016 DeleteFile(from_path, true);
985 return true; 1017 return true;
986 } 1018 }
987 1019
988 } // namespace internal 1020 } // namespace internal
989 1021
990 #endif // !defined(OS_NACL_NONSFI) 1022 #endif // !defined(OS_NACL_NONSFI)
991 } // namespace base 1023 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698