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

Side by Side Diff: base/process/launch_posix.cc

Issue 885423003: Add the ability to change directories before execing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to comments. Created 5 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
« no previous file with comments | « base/process/launch.h ('k') | base/process/process_util_unittest.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) 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/process/launch.h" 5 #include "base/process/launch.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 <sched.h> 10 #include <sched.h>
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 size_t fd_shuffle_size = 0; 340 size_t fd_shuffle_size = 0;
341 if (options.fds_to_remap) { 341 if (options.fds_to_remap) {
342 fd_shuffle_size = options.fds_to_remap->size(); 342 fd_shuffle_size = options.fds_to_remap->size();
343 } 343 }
344 344
345 InjectiveMultimap fd_shuffle1; 345 InjectiveMultimap fd_shuffle1;
346 InjectiveMultimap fd_shuffle2; 346 InjectiveMultimap fd_shuffle2;
347 fd_shuffle1.reserve(fd_shuffle_size); 347 fd_shuffle1.reserve(fd_shuffle_size);
348 fd_shuffle2.reserve(fd_shuffle_size); 348 fd_shuffle2.reserve(fd_shuffle_size);
349 349
350 scoped_ptr<char*[]> argv_cstr(new char*[argv.size() + 1]); 350 scoped_ptr<char* []> argv_cstr(new char* [argv.size() + 1]);
351 for (size_t i = 0; i < argv.size(); i++) {
352 argv_cstr[i] = const_cast<char*>(argv[i].c_str());
353 }
354 argv_cstr[argv.size()] = NULL;
355
351 scoped_ptr<char*[]> new_environ; 356 scoped_ptr<char*[]> new_environ;
352 char* const empty_environ = NULL; 357 char* const empty_environ = NULL;
353 char* const* old_environ = GetEnvironment(); 358 char* const* old_environ = GetEnvironment();
354 if (options.clear_environ) 359 if (options.clear_environ)
355 old_environ = &empty_environ; 360 old_environ = &empty_environ;
356 if (!options.environ.empty()) 361 if (!options.environ.empty())
357 new_environ = AlterEnvironment(old_environ, options.environ); 362 new_environ = AlterEnvironment(old_environ, options.environ);
358 363
359 sigset_t full_sigset; 364 sigset_t full_sigset;
360 sigfillset(&full_sigset); 365 sigfillset(&full_sigset);
361 const sigset_t orig_sigmask = SetSignalMask(full_sigset); 366 const sigset_t orig_sigmask = SetSignalMask(full_sigset);
362 367
368 const char* current_directory = nullptr;
369 if (!options.current_directory.empty()) {
370 current_directory = options.current_directory.value().c_str();
371 }
372
363 pid_t pid; 373 pid_t pid;
364 #if defined(OS_LINUX) 374 #if defined(OS_LINUX)
365 if (options.clone_flags) { 375 if (options.clone_flags) {
366 // Signal handling in this function assumes the creation of a new 376 // Signal handling in this function assumes the creation of a new
367 // process, so we check that a thread is not being created by mistake 377 // process, so we check that a thread is not being created by mistake
368 // and that signal handling follows the process-creation rules. 378 // and that signal handling follows the process-creation rules.
369 RAW_CHECK( 379 RAW_CHECK(
370 !(options.clone_flags & (CLONE_SIGHAND | CLONE_THREAD | CLONE_VM))); 380 !(options.clone_flags & (CLONE_SIGHAND | CLONE_THREAD | CLONE_VM)));
371 381
372 // We specify a null ptid and ctid. 382 // We specify a null ptid and ctid.
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 #define PR_SET_NO_NEW_PRIVS 38 518 #define PR_SET_NO_NEW_PRIVS 38
509 #endif 519 #endif
510 if (!options.allow_new_privs) { 520 if (!options.allow_new_privs) {
511 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) && errno != EINVAL) { 521 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) && errno != EINVAL) {
512 // Only log if the error is not EINVAL (i.e. not supported). 522 // Only log if the error is not EINVAL (i.e. not supported).
513 RAW_LOG(FATAL, "prctl(PR_SET_NO_NEW_PRIVS) failed"); 523 RAW_LOG(FATAL, "prctl(PR_SET_NO_NEW_PRIVS) failed");
514 } 524 }
515 } 525 }
516 #endif 526 #endif
517 527
518 #if defined(OS_POSIX) 528 #if defined(OS_POSIX)
rvargas (doing something else) 2015/02/03 03:32:10 isn't this redundant?
529 if (current_directory != nullptr) {
530 RAW_CHECK(chdir(current_directory) == 0);
531 }
532
519 if (options.pre_exec_delegate != nullptr) { 533 if (options.pre_exec_delegate != nullptr) {
520 options.pre_exec_delegate->RunAsyncSafe(); 534 options.pre_exec_delegate->RunAsyncSafe();
521 } 535 }
522 #endif 536 #endif
523 537
524 for (size_t i = 0; i < argv.size(); i++)
525 argv_cstr[i] = const_cast<char*>(argv[i].c_str());
526 argv_cstr[argv.size()] = NULL;
527 execvp(argv_cstr[0], argv_cstr.get()); 538 execvp(argv_cstr[0], argv_cstr.get());
528 539
529 RAW_LOG(ERROR, "LaunchProcess: failed to execvp:"); 540 RAW_LOG(ERROR, "LaunchProcess: failed to execvp:");
530 RAW_LOG(ERROR, argv_cstr[0]); 541 RAW_LOG(ERROR, argv_cstr[0]);
531 _exit(127); 542 _exit(127);
532 } else { 543 } else {
533 // Parent process 544 // Parent process
534 if (options.wait) { 545 if (options.wait) {
535 // While this isn't strictly disk IO, waiting for another process to 546 // While this isn't strictly disk IO, waiting for another process to
536 // finish is the sort of thing ThreadRestrictions is trying to prevent. 547 // finish is the sort of thing ThreadRestrictions is trying to prevent.
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 jmp_buf env; 770 jmp_buf env;
760 if (setjmp(env) == 0) { 771 if (setjmp(env) == 0) {
761 return CloneAndLongjmpInChild(flags, ptid, ctid, &env); 772 return CloneAndLongjmpInChild(flags, ptid, ctid, &env);
762 } 773 }
763 774
764 return 0; 775 return 0;
765 } 776 }
766 #endif // defined(OS_LINUX) 777 #endif // defined(OS_LINUX)
767 778
768 } // namespace base 779 } // namespace base
OLDNEW
« no previous file with comments | « base/process/launch.h ('k') | base/process/process_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698