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

Side by Side Diff: base/memory/shared_memory_helper.cc

Issue 2555483002: Add POSIX shared memory support for Mac (Closed)
Patch Set: Address @asvitkine nits Created 4 years 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
(Empty)
1 // Copyright 2016 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 "base/memory/shared_memory_helper.h"
6
7 #include "base/threading/thread_restrictions.h"
8
9 namespace base {
10
11 #if !defined(OS_ANDROID)
12 bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options,
13 ScopedFILE* fp,
14 ScopedFD* readonly_fd,
15 FilePath* path) {
16 #if !(defined(OS_MACOSX) && !defined(OS_IOS))
17 // It doesn't make sense to have a open-existing private piece of shmem
18 DCHECK(!options.open_existing_deprecated);
19 #endif // !(defined(OS_MACOSX) && !defined(OS_IOS)
20 // Q: Why not use the shm_open() etc. APIs?
21 // A: Because they're limited to 4mb on OS X. FFFFFFFUUUUUUUUUUU
22 FilePath directory;
23 ScopedPathUnlinker path_unlinker;
24 if (GetShmemTempDir(options.executable, &directory)) {
25 fp->reset(base::CreateAndOpenTemporaryFileInDir(directory, path));
26
27 // Deleting the file prevents anyone else from mapping it in (making it
28 // private), and prevents the need for cleanup (once the last fd is
29 // closed, it is truly freed).
30 if (*fp)
31 path_unlinker.reset(path);
Mark Mentovai 2016/12/09 19:53:52 So you unlink path here…
32 }
33
34 if (*fp && options.share_read_only) {
Mark Mentovai 2016/12/09 21:21:30 OK, I see. A lot of related but distinct objects i
lawrencewu 2016/12/09 22:08:22 OK, I restructured the code so it uses more early
35 // Also open as readonly so that we can ShareReadOnlyToProcess.
36 readonly_fd->reset(HANDLE_EINTR(open(path->value().c_str(), O_RDONLY)));
Mark Mentovai 2016/12/09 19:53:52 …then will you try to reopen it here?
lawrencewu 2016/12/09 20:32:12 I think we have to re-open it as readonly here. Ar
Mark Mentovai 2016/12/09 20:54:13 lawrencewu wrote:
lawrencewu 2016/12/09 21:14:31 So the code is kind of confusing but I don't think
37 if (!readonly_fd->is_valid()) {
38 DPLOG(ERROR) << "open(\"" << path->value() << "\", O_RDONLY) failed";
39 fp->reset();
40 return false;
41 }
42 }
43 return true;
Mark Mentovai 2016/12/09 21:21:30 Another problem is that if (!*fp), we probably don
lawrencewu 2016/12/09 22:08:22 Done.
44 }
45
46 bool PrepareMapFile(ScopedFILE fp, ScopedFD readonly_fd, int* mapped_file,
47 int* readonly_mapped_file) {
48 DCHECK_EQ(-1, *mapped_file);
49 DCHECK_EQ(-1, *readonly_mapped_file);
50 if (fp == NULL)
51 return false;
52
53 // This function theoretically can block on the disk, but realistically
54 // the temporary files we create will just go into the buffer cache
55 // and be deleted before they ever make it out to disk.
56 base::ThreadRestrictions::ScopedAllowIO allow_io;
57
58 struct stat st = {};
59 if (fstat(fileno(fp.get()), &st))
Mark Mentovai 2016/12/09 19:53:52 What’s the point of doing this if readonly_fd.is_v
lawrencewu 2016/12/09 20:32:12 Moved to inside the if statement.
60 NOTREACHED();
61 if (readonly_fd.is_valid()) {
62 struct stat readonly_st = {};
63 if (fstat(readonly_fd.get(), &readonly_st))
64 NOTREACHED();
65 if (st.st_dev != readonly_st.st_dev || st.st_ino != readonly_st.st_ino) {
66 LOG(ERROR) << "writable and read-only inodes don't match; bailing";
67 return false;
68 }
69 }
70
71 *mapped_file = HANDLE_EINTR(dup(fileno(fp.get())));
72 if (*mapped_file == -1) {
73 if (errno == EMFILE) {
Mark Mentovai 2016/12/09 19:53:52 Is it worth distinguishing EMFILE? dup() failing i
lawrencewu 2016/12/09 20:32:12 Removed the check for EMFILE.
74 LOG(WARNING) << "Shared memory creation failed; out of file descriptors";
75 return false;
76 }
77 NOTREACHED() << "Call to dup failed, errno=" << errno;
78 }
79 *readonly_mapped_file = readonly_fd.release();
80
81 return true;
82 }
83 #endif // !defined(OS_ANDROID)
84
85 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698