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

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: suppress glibc leak 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 // Appends |mode_char| to |mode| before the optional character set encoding for
189 // non-Mac platforms (which do not support this); see
Mark Mentovai 2017/02/09 15:15:16 Why even bother defining this on Mac? Since you ne
grt (UTC plus 2) 2017/02/10 09:37:21 Done.
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.
194 std::string result(mode.as_string());
195 #if !defined(OS_MACOSX)
196 size_t comma_pos = result.find(L',');
Mark Mentovai 2017/02/09 15:15:16 No L
grt (UTC plus 2) 2017/02/10 09:37:21 Done.
197 result.insert(comma_pos == std::string::npos ? result.length() : comma_pos, 1,
198 mode_char);
199 #endif
200 return result;
201 }
202
188 } // namespace 203 } // namespace
189 204
190 #if !defined(OS_NACL_NONSFI) 205 #if !defined(OS_NACL_NONSFI)
191 FilePath MakeAbsoluteFilePath(const FilePath& input) { 206 FilePath MakeAbsoluteFilePath(const FilePath& input) {
192 ThreadRestrictions::AssertIOAllowed(); 207 ThreadRestrictions::AssertIOAllowed();
193 char full_path[PATH_MAX]; 208 char full_path[PATH_MAX];
194 if (realpath(input.value().c_str(), full_path) == NULL) 209 if (realpath(input.value().c_str(), full_path) == NULL)
195 return FilePath(); 210 return FilePath();
196 return FilePath(full_path); 211 return FilePath(full_path);
197 } 212 }
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 #if defined(OS_ANDROID) 718 #if defined(OS_ANDROID)
704 } 719 }
705 #endif // defined(OS_ANDROID) 720 #endif // defined(OS_ANDROID)
706 721
707 results->FromStat(file_info); 722 results->FromStat(file_info);
708 return true; 723 return true;
709 } 724 }
710 #endif // !defined(OS_NACL_NONSFI) 725 #endif // !defined(OS_NACL_NONSFI)
711 726
712 FILE* OpenFile(const FilePath& filename, const char* mode) { 727 FILE* OpenFile(const FilePath& filename, const char* mode) {
728 // 'e' is unconditionally added below, so be sure there is not one already
729 // present before a comma in |mode|.
730 DCHECK(::strchr(mode, 'e') == nullptr ||
Mark Mentovai 2017/02/09 15:15:16 No ::
grt (UTC plus 2) 2017/02/10 09:37:21 Done.
731 (::strchr(mode, ',') != nullptr &&
732 ::strchr(mode, 'e') > ::strchr(mode, ',')));
713 ThreadRestrictions::AssertIOAllowed(); 733 ThreadRestrictions::AssertIOAllowed();
714 FILE* result = NULL; 734 FILE* result = NULL;
735 std::string mode_with_e(AppendModeCharacter(mode, 'e'));
715 do { 736 do {
716 result = fopen(filename.value().c_str(), mode); 737 result = fopen(filename.value().c_str(), mode_with_e.c_str());
717 } while (!result && errno == EINTR); 738 } while (!result && errno == EINTR);
739 #if defined(OS_MACOSX)
740 // Mark the descriptor as close-on-exec.
741 if (result)
742 SetCloseOnExec(fileno(result));
743 #endif
718 return result; 744 return result;
719 } 745 }
720 746
721 // NaCl doesn't implement system calls to open files directly. 747 // NaCl doesn't implement system calls to open files directly.
722 #if !defined(OS_NACL) 748 #if !defined(OS_NACL)
723 FILE* FileToFILE(File file, const char* mode) { 749 FILE* FileToFILE(File file, const char* mode) {
724 FILE* stream = fdopen(file.GetPlatformFile(), mode); 750 FILE* stream = fdopen(file.GetPlatformFile(), mode);
725 if (stream) 751 if (stream)
726 file.TakePlatformFile(); 752 file.TakePlatformFile();
727 return stream; 753 return stream;
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 return false; 1008 return false;
983 1009
984 DeleteFile(from_path, true); 1010 DeleteFile(from_path, true);
985 return true; 1011 return true;
986 } 1012 }
987 1013
988 } // namespace internal 1014 } // namespace internal
989 1015
990 #endif // !defined(OS_NACL_NONSFI) 1016 #endif // !defined(OS_NACL_NONSFI)
991 } // namespace base 1017 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698