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 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 return syscall(SYS_change_fdguard_np, fd, guard, guardflags, |
| 499 nguard, nguardflags, fdflagsp); |
| 500 } |
| 501 |
| 502 // Attempt to set a file-descriptor guard on |fd|. In case of success, remove |
| 503 // it and return |true| to indicate that it can be guarded. Returning |false| |
| 504 // means either that |fd| is guarded by some other code, or more likely EBADF. |
| 505 // |
| 506 // Starting with 10.9, libdispatch began setting GUARD_DUP on a file descriptor. |
| 507 // Unfortunately, it is spun up as part of +[NSApplication initialize], which is |
| 508 // not really something that Chromium can avoid using on OSX. See |
| 509 // <http://crbug.com/338157>. This function allows querying whether the file |
| 510 // descriptor is guarded before attempting to close it. |
| 511 bool CanGuardFd(int fd) { |
| 512 // The syscall is first provided in 10.9/Mavericks. |
| 513 if (!base::mac::IsOSMavericksOrLater()) |
| 514 return true; |
| 515 |
| 516 // Saves the original flags to reset later. |
| 517 int original_fdflags = 0; |
| 518 |
| 519 // This can be any value at all, it just has to match up between the two |
| 520 // calls. |
| 521 const guardid_t kGuard = 15; |
| 522 |
| 523 // Attempt to change the guard. This can fail with EBADF if the file |
| 524 // descriptor is bad, or EINVAL if the fd already has a guard set. |
| 525 int ret = |
| 526 change_fdguard_np(fd, NULL, 0, &kGuard, GUARD_DUP, &original_fdflags); |
| 527 if (ret == -1) |
| 528 return false; |
| 529 |
| 530 // Remove the guard. It should not be possible to fail in removing the guard |
| 531 // just added. |
| 532 ret = change_fdguard_np(fd, &kGuard, GUARD_DUP, NULL, 0, &original_fdflags); |
| 533 DPCHECK(ret == 0); |
| 534 |
| 535 return true; |
| 536 } |
| 537 #endif // OS_MACOSX |
| 538 |
472 } // namespace | 539 } // namespace |
473 | 540 |
474 MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) { | 541 MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) { |
475 // This child process counts the number of open FDs, it then writes that | 542 // This child process counts the number of open FDs, it then writes that |
476 // number out to a pipe connected to the parent. | 543 // number out to a pipe connected to the parent. |
477 int num_open_files = 0; | 544 int num_open_files = 0; |
478 int write_pipe = kChildPipe; | 545 int write_pipe = kChildPipe; |
479 int max_files = GetMaxFilesOpenInProcess(); | 546 int max_files = GetMaxFilesOpenInProcess(); |
480 for (int i = STDERR_FILENO + 1; i < max_files; i++) { | 547 for (int i = STDERR_FILENO + 1; i < max_files; i++) { |
| 548 #if defined(OS_MACOSX) |
| 549 // Ignore guarded or invalid file descriptors. |
| 550 if (!CanGuardFd(i)) |
| 551 continue; |
| 552 #endif |
| 553 |
481 if (i != kChildPipe) { | 554 if (i != kChildPipe) { |
482 int fd; | 555 int fd; |
483 if ((fd = HANDLE_EINTR(dup(i))) != -1) { | 556 if ((fd = HANDLE_EINTR(dup(i))) != -1) { |
484 close(fd); | 557 close(fd); |
485 num_open_files += 1; | 558 num_open_files += 1; |
486 } | 559 } |
487 } | 560 } |
488 } | 561 } |
489 | 562 |
490 int written = HANDLE_EINTR(write(write_pipe, &num_open_files, | 563 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. | 948 // Check that process was really killed. |
876 EXPECT_TRUE(IsProcessDead(child_process)); | 949 EXPECT_TRUE(IsProcessDead(child_process)); |
877 base::CloseProcessHandle(child_process); | 950 base::CloseProcessHandle(child_process); |
878 } | 951 } |
879 | 952 |
880 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { | 953 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { |
881 return 0; | 954 return 0; |
882 } | 955 } |
883 | 956 |
884 #endif // defined(OS_POSIX) | 957 #endif // defined(OS_POSIX) |
OLD | NEW |