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
3 // found in the LICENSE file.
5 #include "chrome/common/service_process_util_posix.h"
10 #include "base/base_paths.h"
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/path_service.h"
15 #include "base/threading/platform_thread.h"
16 #include "chrome/common/auto_start_linux.h"
17 #include "chrome/common/multi_process_lock.h"
21 MultiProcessLock
* TakeServiceInitializingLock(bool waiting
) {
22 std::string lock_name
=
23 GetServiceProcessScopedName("_service_initializing");
24 return TakeNamedLock(lock_name
, waiting
);
27 std::string
GetBaseDesktopName() {
28 #if defined(GOOGLE_CHROME_BUILD)
29 return "google-chrome-service.desktop";
30 #else // CHROMIUM_BUILD
31 return "chromium-service.desktop";
36 MultiProcessLock
* TakeServiceRunningLock(bool waiting
) {
37 std::string lock_name
=
38 GetServiceProcessScopedName("_service_running");
39 return TakeNamedLock(lock_name
, waiting
);
42 bool ForceServiceProcessShutdown(const std::string
& version
,
43 base::ProcessId process_id
) {
44 if (kill(process_id
, SIGTERM
) < 0) {
45 DPLOG(ERROR
) << "kill";
51 // Gets the name of the service process IPC channel.
52 // Returns an absolute path as required.
53 IPC::ChannelHandle
GetServiceProcessChannel() {
54 base::FilePath temp_dir
;
55 PathService::Get(base::DIR_TEMP
, &temp_dir
);
56 std::string pipe_name
= GetServiceProcessScopedVersionedName("_service_ipc");
57 std::string pipe_path
= temp_dir
.Append(pipe_name
).value();
63 bool CheckServiceProcessReady() {
64 scoped_ptr
<MultiProcessLock
> running_lock(TakeServiceRunningLock(false));
65 return running_lock
.get() == NULL
;
68 bool ServiceProcessState::TakeSingletonLock() {
69 state_
->initializing_lock_
.reset(TakeServiceInitializingLock(true));
70 return state_
->initializing_lock_
.get();
73 bool ServiceProcessState::AddToAutoRun() {
74 DCHECK(autorun_command_line_
.get());
75 #if defined(GOOGLE_CHROME_BUILD)
76 std::string app_name
= "Google Chrome Service";
77 #else // CHROMIUM_BUILD
78 std::string app_name
= "Chromium Service";
80 return AutoStart::AddApplication(
81 GetServiceProcessScopedName(GetBaseDesktopName()),
83 autorun_command_line_
->GetCommandLineString(),
87 bool ServiceProcessState::RemoveFromAutoRun() {
88 return AutoStart::Remove(
89 GetServiceProcessScopedName(GetBaseDesktopName()));