blob: 942dbab9ef6c55598db224d84f2f699244a6cd2f [file] [log] [blame]
[email protected]9c73c772008-10-27 20:49:551// Copyright (c) 2008 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
6#include "base/process_util.h"
7
8#import <Cocoa/Cocoa.h>
9#include <spawn.h>
10#include <string>
11#include <sys/types.h>
12#include <sys/wait.h>
13
14namespace process_util {
15
16bool LaunchApp(const std::vector<std::string>& argv,
17 bool wait, ProcessHandle* process_handle) {
18 bool retval = true;
19
20 char* argv_copy[argv.size() + 1];
21 for (size_t i = 0; i < argv.size(); i++) {
22 argv_copy[i] = const_cast<char*>(argv[i].c_str());
23 }
24 argv_copy[argv.size()] = NULL;
25
26 int pid = 0;
27 int spawn_succeeded = (posix_spawn(&pid,
28 argv_copy[0],
29 NULL,
30 NULL,
31 argv_copy,
32 NULL) == 0);
33
34 bool process_handle_valid = pid > 0;
35 if (!spawn_succeeded || !process_handle_valid) {
36 retval = false;
37 } else {
38 if (wait)
39 waitpid(pid, 0, 0);
40
41 if(process_handle)
42 *process_handle = pid;
43 }
44
45 return retval;
46}
47
48bool LaunchApp(const CommandLine& cl,
49 bool wait, bool start_hidden, ProcessHandle* process_handle) {
50 // TODO(playmobil): Do we need to respect the start_hidden flag?
51 return LaunchApp(cl.argv(), wait, process_handle);
52}
53
54bool WaitForSingleProcess(ProcessHandle handle, int wait_milliseconds) {
55 // TODO(playmobil): Do we need to support wait_milliseconds?
56 int status;
57 waitpid(handle, &status, 0);
58 return WIFEXITED(status);
59}
60
61} // namespace process_util