OLD | NEW |
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 "chrome/common/service_process_util_posix.h" | 5 #include "chrome/common/service_process_util_posix.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 #include <launch.h> | 8 #include <launch.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/mac/bundle_locations.h" | 15 #include "base/mac/bundle_locations.h" |
16 #include "base/mac/foundation_util.h" | 16 #include "base/mac/foundation_util.h" |
17 #include "base/mac/mac_util.h" | 17 #include "base/mac/mac_util.h" |
18 #include "base/mac/scoped_nsautorelease_pool.h" | 18 #include "base/mac/scoped_nsautorelease_pool.h" |
19 #include "base/memory/scoped_nsobject.h" | 19 #include "base/mac/scoped_nsobject.h" |
20 #include "base/path_service.h" | 20 #include "base/path_service.h" |
21 #include "base/process_util.h" | 21 #include "base/process_util.h" |
22 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
23 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
24 #include "base/strings/sys_string_conversions.h" | 24 #include "base/strings/sys_string_conversions.h" |
25 #include "base/threading/thread_restrictions.h" | 25 #include "base/threading/thread_restrictions.h" |
26 #include "base/version.h" | 26 #include "base/version.h" |
27 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
28 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/common/chrome_version_info.h" | 29 #include "chrome/common/chrome_version_info.h" |
30 #include "chrome/common/mac/launchd.h" | 30 #include "chrome/common/mac/launchd.h" |
31 | 31 |
32 using ::base::FilePathWatcher; | 32 using ::base::FilePathWatcher; |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 #define kServiceProcessSessionType "Aqua" | 36 #define kServiceProcessSessionType "Aqua" |
37 | 37 |
38 CFStringRef CopyServiceProcessLaunchDName() { | 38 CFStringRef CopyServiceProcessLaunchDName() { |
39 base::mac::ScopedNSAutoreleasePool pool; | 39 base::mac::ScopedNSAutoreleasePool pool; |
40 NSBundle* bundle = base::mac::FrameworkBundle(); | 40 NSBundle* bundle = base::mac::FrameworkBundle(); |
41 return CFStringCreateCopy(kCFAllocatorDefault, | 41 return CFStringCreateCopy(kCFAllocatorDefault, |
42 base::mac::NSToCFCast([bundle bundleIdentifier])); | 42 base::mac::NSToCFCast([bundle bundleIdentifier])); |
43 } | 43 } |
44 | 44 |
45 NSString* GetServiceProcessLaunchDLabel() { | 45 NSString* GetServiceProcessLaunchDLabel() { |
46 scoped_nsobject<NSString> name( | 46 base::scoped_nsobject<NSString> name( |
47 base::mac::CFToNSCast(CopyServiceProcessLaunchDName())); | 47 base::mac::CFToNSCast(CopyServiceProcessLaunchDName())); |
48 NSString *label = [name stringByAppendingString:@".service_process"]; | 48 NSString *label = [name stringByAppendingString:@".service_process"]; |
49 base::FilePath user_data_dir; | 49 base::FilePath user_data_dir; |
50 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | 50 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
51 std::string user_data_dir_path = user_data_dir.value(); | 51 std::string user_data_dir_path = user_data_dir.value(); |
52 NSString *ns_path = base::SysUTF8ToNSString(user_data_dir_path); | 52 NSString *ns_path = base::SysUTF8ToNSString(user_data_dir_path); |
53 ns_path = [ns_path stringByReplacingOccurrencesOfString:@" " | 53 ns_path = [ns_path stringByReplacingOccurrencesOfString:@" " |
54 withString:@"_"]; | 54 withString:@"_"]; |
55 label = [label stringByAppendingString:ns_path]; | 55 label = [label stringByAppendingString:ns_path]; |
56 return label; | 56 return label; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 withString:@"_"]; | 93 withString:@"_"]; |
94 env_var = [env_var stringByAppendingString:@"_SOCKET"]; | 94 env_var = [env_var stringByAppendingString:@"_SOCKET"]; |
95 env_var = [env_var uppercaseString]; | 95 env_var = [env_var uppercaseString]; |
96 return env_var; | 96 return env_var; |
97 } | 97 } |
98 | 98 |
99 // Gets the name of the service process IPC channel. | 99 // Gets the name of the service process IPC channel. |
100 IPC::ChannelHandle GetServiceProcessChannel() { | 100 IPC::ChannelHandle GetServiceProcessChannel() { |
101 base::mac::ScopedNSAutoreleasePool pool; | 101 base::mac::ScopedNSAutoreleasePool pool; |
102 std::string socket_path; | 102 std::string socket_path; |
103 scoped_nsobject<NSDictionary> dictionary( | 103 base::scoped_nsobject<NSDictionary> dictionary( |
104 base::mac::CFToNSCast(Launchd::GetInstance()->CopyExports())); | 104 base::mac::CFToNSCast(Launchd::GetInstance()->CopyExports())); |
105 NSString *ns_socket_path = | 105 NSString *ns_socket_path = |
106 [dictionary objectForKey:GetServiceProcessLaunchDSocketEnvVar()]; | 106 [dictionary objectForKey:GetServiceProcessLaunchDSocketEnvVar()]; |
107 if (ns_socket_path) { | 107 if (ns_socket_path) { |
108 socket_path = base::SysNSStringToUTF8(ns_socket_path); | 108 socket_path = base::SysNSStringToUTF8(ns_socket_path); |
109 } | 109 } |
110 return IPC::ChannelHandle(socket_path); | 110 return IPC::ChannelHandle(socket_path); |
111 } | 111 } |
112 | 112 |
113 bool ForceServiceProcessShutdown(const std::string& /* version */, | 113 bool ForceServiceProcessShutdown(const std::string& /* version */, |
114 base::ProcessId /* process_id */) { | 114 base::ProcessId /* process_id */) { |
115 base::mac::ScopedNSAutoreleasePool pool; | 115 base::mac::ScopedNSAutoreleasePool pool; |
116 CFStringRef label = base::mac::NSToCFCast(GetServiceProcessLaunchDLabel()); | 116 CFStringRef label = base::mac::NSToCFCast(GetServiceProcessLaunchDLabel()); |
117 CFErrorRef err = NULL; | 117 CFErrorRef err = NULL; |
118 bool ret = Launchd::GetInstance()->RemoveJob(label, &err); | 118 bool ret = Launchd::GetInstance()->RemoveJob(label, &err); |
119 if (!ret) { | 119 if (!ret) { |
120 DLOG(ERROR) << "ForceServiceProcessShutdown: " << err << " " | 120 DLOG(ERROR) << "ForceServiceProcessShutdown: " << err << " " |
121 << base::SysCFStringRefToUTF8(label); | 121 << base::SysCFStringRefToUTF8(label); |
122 CFRelease(err); | 122 CFRelease(err); |
123 } | 123 } |
124 return ret; | 124 return ret; |
125 } | 125 } |
126 | 126 |
127 bool GetServiceProcessData(std::string* version, base::ProcessId* pid) { | 127 bool GetServiceProcessData(std::string* version, base::ProcessId* pid) { |
128 base::mac::ScopedNSAutoreleasePool pool; | 128 base::mac::ScopedNSAutoreleasePool pool; |
129 CFStringRef label = base::mac::NSToCFCast(GetServiceProcessLaunchDLabel()); | 129 CFStringRef label = base::mac::NSToCFCast(GetServiceProcessLaunchDLabel()); |
130 scoped_nsobject<NSDictionary> launchd_conf(base::mac::CFToNSCast( | 130 base::scoped_nsobject<NSDictionary> launchd_conf( |
131 Launchd::GetInstance()->CopyJobDictionary(label))); | 131 base::mac::CFToNSCast(Launchd::GetInstance()->CopyJobDictionary(label))); |
132 if (!launchd_conf.get()) { | 132 if (!launchd_conf.get()) { |
133 return false; | 133 return false; |
134 } | 134 } |
135 // Anything past here will return true in that there does appear | 135 // Anything past here will return true in that there does appear |
136 // to be a service process of some sort registered with launchd. | 136 // to be a service process of some sort registered with launchd. |
137 if (version) { | 137 if (version) { |
138 *version = "0"; | 138 *version = "0"; |
139 NSString *exe_path = [launchd_conf objectForKey:@ LAUNCH_JOBKEY_PROGRAM]; | 139 NSString *exe_path = [launchd_conf objectForKey:@ LAUNCH_JOBKEY_PROGRAM]; |
140 if (exe_path) { | 140 if (exe_path) { |
141 NSString *bundle_path = [[[exe_path stringByDeletingLastPathComponent] | 141 NSString *bundle_path = [[[exe_path stringByDeletingLastPathComponent] |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 base::ScopedCFTypeRef<CFStringRef> name(CopyServiceProcessLaunchDName()); | 397 base::ScopedCFTypeRef<CFStringRef> name(CopyServiceProcessLaunchDName()); |
398 if (needs_restart) { | 398 if (needs_restart) { |
399 base::ScopedCFTypeRef<CFMutableDictionaryRef> plist( | 399 base::ScopedCFTypeRef<CFMutableDictionaryRef> plist( |
400 Launchd::GetInstance()->CreatePlistFromFile( | 400 Launchd::GetInstance()->CreatePlistFromFile( |
401 Launchd::User, Launchd::Agent, name)); | 401 Launchd::User, Launchd::Agent, name)); |
402 if (plist.get()) { | 402 if (plist.get()) { |
403 NSMutableDictionary* ns_plist = base::mac::CFToNSCast(plist); | 403 NSMutableDictionary* ns_plist = base::mac::CFToNSCast(plist); |
404 std::string new_path = base::mac::PathFromFSRef(executable_fsref_); | 404 std::string new_path = base::mac::PathFromFSRef(executable_fsref_); |
405 NSString* ns_new_path = base::SysUTF8ToNSString(new_path); | 405 NSString* ns_new_path = base::SysUTF8ToNSString(new_path); |
406 [ns_plist setObject:ns_new_path forKey:@ LAUNCH_JOBKEY_PROGRAM]; | 406 [ns_plist setObject:ns_new_path forKey:@ LAUNCH_JOBKEY_PROGRAM]; |
407 scoped_nsobject<NSMutableArray> args( | 407 base::scoped_nsobject<NSMutableArray> args([[ns_plist |
408 [[ns_plist objectForKey:@ LAUNCH_JOBKEY_PROGRAMARGUMENTS] | 408 objectForKey:@LAUNCH_JOBKEY_PROGRAMARGUMENTS] mutableCopy]); |
409 mutableCopy]); | |
410 [args replaceObjectAtIndex:0 withObject:ns_new_path]; | 409 [args replaceObjectAtIndex:0 withObject:ns_new_path]; |
411 [ns_plist setObject:args forKey:@ LAUNCH_JOBKEY_PROGRAMARGUMENTS]; | 410 [ns_plist setObject:args forKey:@ LAUNCH_JOBKEY_PROGRAMARGUMENTS]; |
412 if (!Launchd::GetInstance()->WritePlistToFile(Launchd::User, | 411 if (!Launchd::GetInstance()->WritePlistToFile(Launchd::User, |
413 Launchd::Agent, | 412 Launchd::Agent, |
414 name, | 413 name, |
415 plist)) { | 414 plist)) { |
416 DLOG(ERROR) << "Unable to rewrite plist."; | 415 DLOG(ERROR) << "Unable to rewrite plist."; |
417 needs_shutdown = true; | 416 needs_shutdown = true; |
418 } | 417 } |
419 } else { | 418 } else { |
(...skipping 24 matching lines...) Expand all Loading... |
444 CFErrorRef err = NULL; | 443 CFErrorRef err = NULL; |
445 if (!Launchd::GetInstance()->RemoveJob(label, &err)) { | 444 if (!Launchd::GetInstance()->RemoveJob(label, &err)) { |
446 base::ScopedCFTypeRef<CFErrorRef> scoped_err(err); | 445 base::ScopedCFTypeRef<CFErrorRef> scoped_err(err); |
447 DLOG(ERROR) << "RemoveJob " << err; | 446 DLOG(ERROR) << "RemoveJob " << err; |
448 // Exiting with zero, so launchd doesn't restart the process. | 447 // Exiting with zero, so launchd doesn't restart the process. |
449 exit(0); | 448 exit(0); |
450 } | 449 } |
451 } | 450 } |
452 } | 451 } |
453 } | 452 } |
OLD | NEW |