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. 4 5 #include "chrome/common/service_process_util_posix.h" 6 7 #include <signal.h> 8 #include <unistd.h> 9 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" 18 19 namespace { 20 TakeServiceInitializingLock(bool waiting)21MultiProcessLock* TakeServiceInitializingLock(bool waiting) { 22 std::string lock_name = 23 GetServiceProcessScopedName("_service_initializing"); 24 return TakeNamedLock(lock_name, waiting); 25 } 26 GetBaseDesktopName()27std::string GetBaseDesktopName() { 28 #if defined(GOOGLE_CHROME_BUILD) 29 return "google-chrome-service.desktop"; 30 #else // CHROMIUM_BUILD 31 return "chromium-service.desktop"; 32 #endif 33 } 34 } // namespace 35 TakeServiceRunningLock(bool waiting)36MultiProcessLock* TakeServiceRunningLock(bool waiting) { 37 std::string lock_name = 38 GetServiceProcessScopedName("_service_running"); 39 return TakeNamedLock(lock_name, waiting); 40 } 41 ForceServiceProcessShutdown(const std::string & version,base::ProcessId process_id)42bool ForceServiceProcessShutdown(const std::string& version, 43 base::ProcessId process_id) { 44 if (kill(process_id, SIGTERM) < 0) { 45 DPLOG(ERROR) << "kill"; 46 return false; 47 } 48 return true; 49 } 50 51 // Gets the name of the service process IPC channel. 52 // Returns an absolute path as required. GetServiceProcessChannel()53IPC::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(); 58 return pipe_path; 59 } 60 61 62 CheckServiceProcessReady()63bool CheckServiceProcessReady() { 64 scoped_ptr<MultiProcessLock> running_lock(TakeServiceRunningLock(false)); 65 return running_lock.get() == NULL; 66 } 67 TakeSingletonLock()68bool ServiceProcessState::TakeSingletonLock() { 69 state_->initializing_lock_.reset(TakeServiceInitializingLock(true)); 70 return state_->initializing_lock_.get(); 71 } 72 AddToAutoRun()73bool 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"; 79 #endif 80 return AutoStart::AddApplication( 81 GetServiceProcessScopedName(GetBaseDesktopName()), 82 app_name, 83 autorun_command_line_->GetCommandLineString(), 84 false); 85 } 86 RemoveFromAutoRun()87bool ServiceProcessState::RemoveFromAutoRun() { 88 return AutoStart::Remove( 89 GetServiceProcessScopedName(GetBaseDesktopName())); 90 } 91