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

Side by Side Diff: base/process_util_posix.cc

Issue 3423012: Cleanup orphaned testserver processes on posix. (Closed)
Patch Set: CR feedback. Created 10 years, 3 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
« no previous file with comments | « base/process_util.h ('k') | chrome/test/test_launcher/out_of_proc_test_runner.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 <dirent.h> 5 #include <dirent.h>
6 #include <errno.h> 6 #include <errno.h>
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <signal.h> 8 #include <signal.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <sys/resource.h> 10 #include <sys/resource.h>
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 if (!exited) 188 if (!exited)
189 result = kill(process_id, SIGKILL) == 0; 189 result = kill(process_id, SIGKILL) == 0;
190 } 190 }
191 191
192 if (!result) 192 if (!result)
193 DPLOG(ERROR) << "Unable to terminate process " << process_id; 193 DPLOG(ERROR) << "Unable to terminate process " << process_id;
194 194
195 return result; 195 return result;
196 } 196 }
197 197
198 bool KillProcessGroup(ProcessHandle process_group_id) {
199 bool result = kill(-1 * process_group_id, SIGKILL) == 0;
200 if (!result)
201 PLOG(ERROR) << "Unable to terminate process group " << process_group_id;
202 return result;
203 }
204
198 // A class to handle auto-closing of DIR*'s. 205 // A class to handle auto-closing of DIR*'s.
199 class ScopedDIRClose { 206 class ScopedDIRClose {
200 public: 207 public:
201 inline void operator()(DIR* x) const { 208 inline void operator()(DIR* x) const {
202 if (x) { 209 if (x) {
203 closedir(x); 210 closedir(x);
204 } 211 }
205 } 212 }
206 }; 213 };
207 typedef scoped_ptr_malloc<DIR, ScopedDIRClose> ScopedDIR; 214 typedef scoped_ptr_malloc<DIR, ScopedDIRClose> ScopedDIR;
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 *scratch++ = '='; 420 *scratch++ = '=';
414 memcpy(scratch, j->second.c_str(), j->second.size() + 1); 421 memcpy(scratch, j->second.c_str(), j->second.size() + 1);
415 scratch += j->second.size() + 1; 422 scratch += j->second.size() + 1;
416 } 423 }
417 } 424 }
418 425
419 ret[k] = NULL; 426 ret[k] = NULL;
420 return ret; 427 return ret;
421 } 428 }
422 429
423 bool LaunchApp( 430 bool LaunchAppImpl(
424 const std::vector<std::string>& argv, 431 const std::vector<std::string>& argv,
425 const environment_vector& env_changes, 432 const environment_vector& env_changes,
426 const file_handle_mapping_vector& fds_to_remap, 433 const file_handle_mapping_vector& fds_to_remap,
427 bool wait, 434 bool wait,
428 ProcessHandle* process_handle) { 435 ProcessHandle* process_handle,
436 bool start_new_process_group) {
429 pid_t pid; 437 pid_t pid;
430 InjectiveMultimap fd_shuffle1, fd_shuffle2; 438 InjectiveMultimap fd_shuffle1, fd_shuffle2;
431 fd_shuffle1.reserve(fds_to_remap.size()); 439 fd_shuffle1.reserve(fds_to_remap.size());
432 fd_shuffle2.reserve(fds_to_remap.size()); 440 fd_shuffle2.reserve(fds_to_remap.size());
433 scoped_array<char*> argv_cstr(new char*[argv.size() + 1]); 441 scoped_array<char*> argv_cstr(new char*[argv.size() + 1]);
434 scoped_array<char*> new_environ(AlterEnvironment(env_changes, environ)); 442 scoped_array<char*> new_environ(AlterEnvironment(env_changes, environ));
435 443
436 pid = fork(); 444 pid = fork();
437 if (pid < 0) 445 if (pid < 0)
438 return false; 446 return false;
439 447
440 if (pid == 0) { 448 if (pid == 0) {
441 // Child process 449 // Child process
450
451 if (start_new_process_group) {
452 // Instead of inheriting the process group ID of the parent, the child
453 // starts off a new process group with pgid equal to its process ID.
454 if (setpgid(0, 0) < 0)
455 return false;
456 }
442 #if defined(OS_MACOSX) 457 #if defined(OS_MACOSX)
443 RestoreDefaultExceptionHandler(); 458 RestoreDefaultExceptionHandler();
444 #endif 459 #endif
445 460
446 ResetChildSignalHandlersToDefaults(); 461 ResetChildSignalHandlersToDefaults();
447 462
448 #if 0 463 #if 0
449 // When debugging it can be helpful to check that we really aren't making 464 // When debugging it can be helpful to check that we really aren't making
450 // any hidden calls to malloc. 465 // any hidden calls to malloc.
451 void *malloc_thunk = 466 void *malloc_thunk =
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 DPCHECK(ret > 0); 506 DPCHECK(ret > 0);
492 } 507 }
493 508
494 if (process_handle) 509 if (process_handle)
495 *process_handle = pid; 510 *process_handle = pid;
496 } 511 }
497 512
498 return true; 513 return true;
499 } 514 }
500 515
516 bool LaunchApp(
517 const std::vector<std::string>& argv,
518 const environment_vector& env_changes,
519 const file_handle_mapping_vector& fds_to_remap,
520 bool wait,
521 ProcessHandle* process_handle) {
522 return LaunchAppImpl(argv, env_changes, fds_to_remap,
523 wait, process_handle, false);
524 }
525
526 bool LaunchAppInNewProcessGroup(
527 const std::vector<std::string>& argv,
528 const environment_vector& env_changes,
529 const file_handle_mapping_vector& fds_to_remap,
530 bool wait,
531 ProcessHandle* process_handle) {
532 return LaunchAppImpl(argv, env_changes, fds_to_remap, wait,
533 process_handle, true);
534 }
535
501 bool LaunchApp(const std::vector<std::string>& argv, 536 bool LaunchApp(const std::vector<std::string>& argv,
502 const file_handle_mapping_vector& fds_to_remap, 537 const file_handle_mapping_vector& fds_to_remap,
503 bool wait, ProcessHandle* process_handle) { 538 bool wait, ProcessHandle* process_handle) {
504 base::environment_vector no_env; 539 base::environment_vector no_env;
505 return LaunchApp(argv, no_env, fds_to_remap, wait, process_handle); 540 return LaunchApp(argv, no_env, fds_to_remap, wait, process_handle);
506 } 541 }
507 542
508 bool LaunchApp(const CommandLine& cl, 543 bool LaunchApp(const CommandLine& cl,
509 bool wait, bool start_hidden, 544 bool wait, bool start_hidden,
510 ProcessHandle* process_handle) { 545 ProcessHandle* process_handle) {
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 const ProcessFilter* filter) { 844 const ProcessFilter* filter) {
810 bool exited_cleanly = 845 bool exited_cleanly =
811 WaitForProcessesToExit(executable_name, wait_milliseconds, 846 WaitForProcessesToExit(executable_name, wait_milliseconds,
812 filter); 847 filter);
813 if (!exited_cleanly) 848 if (!exited_cleanly)
814 KillProcesses(executable_name, exit_code, filter); 849 KillProcesses(executable_name, exit_code, filter);
815 return exited_cleanly; 850 return exited_cleanly;
816 } 851 }
817 852
818 } // namespace base 853 } // namespace base
OLDNEW
« no previous file with comments | « base/process_util.h ('k') | chrome/test/test_launcher/out_of_proc_test_runner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698