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 #define _CRT_SECURE_NO_WARNINGS | 5 #define _CRT_SECURE_NO_WARNINGS |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 #include <sys/socket.h> | 43 #include <sys/socket.h> |
44 #include <sys/wait.h> | 44 #include <sys/wait.h> |
45 #endif | 45 #endif |
46 #if defined(OS_WIN) | 46 #if defined(OS_WIN) |
47 #include <windows.h> | 47 #include <windows.h> |
48 #include "base/win/windows_version.h" | 48 #include "base/win/windows_version.h" |
49 #endif | 49 #endif |
50 #if defined(OS_MACOSX) | 50 #if defined(OS_MACOSX) |
51 #include <mach/vm_param.h> | 51 #include <mach/vm_param.h> |
52 #include <malloc/malloc.h> | 52 #include <malloc/malloc.h> |
53 #include "base/mac/mac_util.h" | |
53 #endif | 54 #endif |
54 | 55 |
55 using base::FilePath; | 56 using base::FilePath; |
56 | 57 |
57 namespace { | 58 namespace { |
58 | 59 |
59 #if defined(OS_ANDROID) | 60 #if defined(OS_ANDROID) |
60 const char kShellPath[] = "/system/bin/sh"; | 61 const char kShellPath[] = "/system/bin/sh"; |
61 const char kPosixShell[] = "sh"; | 62 const char kPosixShell[] = "sh"; |
62 #else | 63 #else |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 rlim_t max_int = static_cast<rlim_t>(std::numeric_limits<int32>::max()); | 463 rlim_t max_int = static_cast<rlim_t>(std::numeric_limits<int32>::max()); |
463 if (rlim.rlim_cur > max_int) { | 464 if (rlim.rlim_cur > max_int) { |
464 return max_int; | 465 return max_int; |
465 } | 466 } |
466 | 467 |
467 return rlim.rlim_cur; | 468 return rlim.rlim_cur; |
468 } | 469 } |
469 | 470 |
470 const int kChildPipe = 20; // FD # for write end of pipe in child process. | 471 const int kChildPipe = 20; // FD # for write end of pipe in child process. |
471 | 472 |
473 #if defined(OS_MACOSX) | |
474 | |
475 // <http://opensource.apple.com/source/xnu/xnu-2422.1.72/bsd/sys/guarded.h> | |
476 #if !defined(_GUARDID_T) | |
477 #define _GUARDID_T | |
478 typedef __uint64_t guardid_t; | |
479 #endif // _GUARDID_T | |
480 | |
481 // From .../MacOSX10.9.sdk/usr/include/sys/syscall.h | |
482 #if !defined(SYS_change_fdguard_np) | |
483 #define SYS_change_fdguard_np 444 | |
484 #endif | |
485 | |
486 // <http://opensource.apple.com/source/xnu/xnu-2422.1.72/bsd/sys/guarded.h> | |
487 #if !defined(GUARD_DUP) | |
488 #define GUARD_DUP (1u << 1) | |
489 #endif | |
490 | |
491 // <http://opensource.apple.com/source/xnu/xnu-2422.1.72/bsd/kern/kern_guarded.c ?txt> | |
492 // | |
493 // Atomically replaces |guard|/|guardflags| with |nguard|/|nguardflags| on |fd|. | |
494 int my_change_fdguard_np(int fd, | |
495 const guardid_t *guard, u_int guardflags, | |
496 const guardid_t *nguard, u_int nguardflags, | |
497 int *fdflagsp) { | |
498 // TODO(shess): Citation needed. | |
499 // TODO(shess): What about older kernels? | |
500 // TODO(shess): Insanity much? | |
501 return syscall(SYS_change_fdguard_np, fd, guard, guardflags, | |
502 nguard, nguardflags, fdflagsp); | |
503 } | |
504 | |
505 // Attempt to set a file-descriptor guard on |fd|. In case of success, remove | |
506 // it and return |true| to indicate that it is now unguarded. Returning |false| | |
507 // means either that |fd| is guarded, or more likely EBADF. | |
508 // | |
509 // Starting with 10.9, libdispatch began setting GUARD_DUP on a file descriptor. | |
510 // Unfortunately, it is spun up as part of +[NSApplication initialize], which is | |
511 // not really something that Chromium can avoid using on OSX. See | |
512 // <http://crbug.com/338157>. This function allows querying whether the file | |
513 // descriptor is guarded before attempting to close it. | |
514 bool is_fd_unguarded(int fd) { | |
Mark Mentovai
2014/07/25 13:23:43
Naming.
Scott Hess - ex-Googler
2014/07/28 19:58:03
Done.
I also changed my_guard_fdguard_np -> guard
| |
515 // The syscall is first provided in 10.9/Mavericks. | |
516 if (!base::mac::IsOSMavericksOrLater()) | |
517 return true; | |
518 | |
519 // Saves the original flags to reset later. | |
520 int original_fdflags = 0; | |
Mark Mentovai
2014/07/25 13:23:43
The kernel calls these unsigned ints, might as wel
Scott Hess - ex-Googler
2014/07/28 19:58:03
The guard flags are unsigned, but the fdflags are
| |
521 | |
522 // This can be any value at all, it just has to match up between the two | |
523 // calls. | |
524 const guardid_t kGuard = 15; | |
525 | |
526 // Attempt to change the guard. This can fail with EBADF if the file | |
527 // descriptor is bad, or EINVAL if the fd already has a guard set. | |
528 int ret = HANDLE_EINTR(my_change_fdguard_np( | |
Mark Mentovai
2014/07/25 13:23:43
I don’t think HANDLE_EINTR is really needed around
Scott Hess - ex-Googler
2014/07/28 19:58:03
Done.
| |
529 fd, NULL, 0, &kGuard, GUARD_DUP, &original_fdflags)); | |
530 if (ret == -1) | |
531 return false; | |
532 | |
533 // Remove the guard. It should not be possible to fail in removing the guard | |
534 // just added. | |
535 ret = HANDLE_EINTR(my_change_fdguard_np( | |
536 fd, &kGuard, GUARD_DUP, NULL, 0, &original_fdflags)); | |
537 DPCHECK(ret == 0); | |
538 | |
539 return true; | |
540 } | |
541 #endif // OS_MACOSX | |
542 | |
472 } // namespace | 543 } // namespace |
473 | 544 |
474 MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) { | 545 MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) { |
475 // This child process counts the number of open FDs, it then writes that | 546 // This child process counts the number of open FDs, it then writes that |
476 // number out to a pipe connected to the parent. | 547 // number out to a pipe connected to the parent. |
477 int num_open_files = 0; | 548 int num_open_files = 0; |
478 int write_pipe = kChildPipe; | 549 int write_pipe = kChildPipe; |
479 int max_files = GetMaxFilesOpenInProcess(); | 550 int max_files = GetMaxFilesOpenInProcess(); |
480 for (int i = STDERR_FILENO + 1; i < max_files; i++) { | 551 for (int i = STDERR_FILENO + 1; i < max_files; i++) { |
552 #if defined(OS_MACOSX) | |
553 // Ignore guarded file descriptors. | |
554 if (!is_fd_unguarded(i)) | |
Mark Mentovai
2014/07/25 13:23:43
This is kind of a double negative, and considering
Scott Hess - ex-Googler
2014/07/28 19:58:03
Changed to CanGuardFd(). The reason for this sens
| |
555 continue; | |
556 #endif | |
557 | |
481 if (i != kChildPipe) { | 558 if (i != kChildPipe) { |
482 int fd; | 559 int fd; |
483 if ((fd = HANDLE_EINTR(dup(i))) != -1) { | 560 if ((fd = HANDLE_EINTR(dup(i))) != -1) { |
484 close(fd); | 561 close(fd); |
485 num_open_files += 1; | 562 num_open_files += 1; |
486 } | 563 } |
487 } | 564 } |
488 } | 565 } |
489 | 566 |
490 int written = HANDLE_EINTR(write(write_pipe, &num_open_files, | 567 int written = HANDLE_EINTR(write(write_pipe, &num_open_files, |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
875 // Check that process was really killed. | 952 // Check that process was really killed. |
876 EXPECT_TRUE(IsProcessDead(child_process)); | 953 EXPECT_TRUE(IsProcessDead(child_process)); |
877 base::CloseProcessHandle(child_process); | 954 base::CloseProcessHandle(child_process); |
878 } | 955 } |
879 | 956 |
880 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { | 957 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { |
881 return 0; | 958 return 0; |
882 } | 959 } |
883 | 960 |
884 #endif // defined(OS_POSIX) | 961 #endif // defined(OS_POSIX) |
OLD | NEW |