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 <signal.h>
6 #include <unistd.h>
7 
8 #include <memory>
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 "build/branding_buildflags.h"
17 #include "chrome/common/auto_start_linux.h"
18 #include "chrome/common/multi_process_lock.h"
19 #include "chrome/common/service_process_util_posix.h"
20 
21 namespace {
22 
GetBaseDesktopName()23 std::string GetBaseDesktopName() {
24 #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
25   return "google-chrome-service.desktop";
26 #else  // BUILDFLAG(CHROMIUM_BRANDING)
27   return "chromium-service.desktop";
28 #endif
29 }
30 }  // namespace
31 
TakeServiceRunningLock()32 std::unique_ptr<MultiProcessLock> TakeServiceRunningLock() {
33   std::string lock_name =
34       GetServiceProcessScopedName("_service_running");
35   return TakeNamedLock(lock_name);
36 }
37 
ForceServiceProcessShutdown(const std::string & version,base::ProcessId process_id)38 bool ForceServiceProcessShutdown(const std::string& version,
39                                  base::ProcessId process_id) {
40   if (kill(process_id, SIGTERM) < 0) {
41     DPLOG(ERROR) << "kill";
42     return false;
43   }
44   return true;
45 }
46 
47 // Gets the name of the service process IPC channel.
48 // Returns an absolute path as required.
GetServiceProcessServerName()49 mojo::NamedPlatformChannel::ServerName GetServiceProcessServerName() {
50   base::FilePath temp_dir;
51   base::PathService::Get(base::DIR_TEMP, &temp_dir);
52   std::string pipe_name = GetServiceProcessScopedVersionedName("_service_ipc");
53   return temp_dir.Append(pipe_name).value();
54 }
55 
CheckServiceProcessReady()56 bool CheckServiceProcessReady() {
57   std::unique_ptr<MultiProcessLock> running_lock(TakeServiceRunningLock());
58   return !running_lock.get();
59 }
60 
TakeSingletonLock()61 bool ServiceProcessState::TakeSingletonLock() {
62   state_->initializing_lock =
63       TakeNamedLock(GetServiceProcessScopedName("_service_initializing"));
64   return state_->initializing_lock.get();
65 }
66 
AddToAutoRun()67 bool ServiceProcessState::AddToAutoRun() {
68   DCHECK(autorun_command_line_.get());
69 #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
70   std::string app_name = "Google Chrome Service";
71 #else  // BUILDFLAG(CHROMIUM_BRANDING)
72   std::string app_name = "Chromium Service";
73 #endif
74   return AutoStart::AddApplication(
75       GetServiceProcessScopedName(GetBaseDesktopName()),
76       app_name,
77       autorun_command_line_->GetCommandLineString(),
78       false);
79 }
80 
RemoveFromAutoRun()81 bool ServiceProcessState::RemoveFromAutoRun() {
82   return AutoStart::Remove(
83       GetServiceProcessScopedName(GetBaseDesktopName()));
84 }
85