OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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/launchd_mac.h" | |
6 | |
7 #import <Foundation/Foundation.h> | |
8 #include <launch.h> | |
9 | |
10 #include "base/mac/mac_util.h" | |
11 #include "base/mac/scoped_cftyperef.h" | |
12 #include "base/mac/scoped_nsautorelease_pool.h" | |
13 #include "base/process_util.h" | |
14 #include "base/stringprintf.h" | |
15 #include "base/sys_string_conversions.h" | |
16 #include "third_party/GTM/Foundation/GTMServiceManagement.h" | |
17 | |
18 namespace { | |
19 | |
20 std::string FileSystemPathFromCFURL(CFURLRef url) { | |
21 base::mac::ScopedCFTypeRef<CFStringRef> path( | |
22 CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle)); | |
Mark Mentovai
2011/03/21 16:50:49
Might path be NULL? If url is bad in some way, lik
dmac
2011/03/21 22:59:19
Obsolete.
| |
23 | |
24 // TODO(dmaclach): Turn this into a more general utility | |
Mark Mentovai
2011/03/21 16:50:49
This comment applies to the entire function, right
dmac
2011/03/21 22:59:19
Obsolete.
| |
25 // http://crbug.com/76928 | |
26 CFIndex max = CFStringGetMaximumSizeOfFileSystemRepresentation(path); | |
Mark Mentovai
2011/03/21 16:50:49
I’d just use -[NSString fileSystemRepresentation]
dmac
2011/03/21 22:59:19
Obsolete.
| |
27 std::vector<char> buffer(max); | |
28 CHECK(CFStringGetFileSystemRepresentation(path, &buffer[0], max)); | |
29 return std::string(&buffer[0]); | |
30 } | |
31 | |
32 CFURLRef CopyPListURL(Launchd::Domain domain, | |
Mark Mentovai
2011/03/21 16:50:49
CopyPlistURL, lowercase L, as you’ve done elsewher
dmac
2011/03/21 22:59:19
Done.
| |
33 Launchd::Type type, | |
34 CFStringRef name) { | |
35 base::mac::ScopedNSAutoreleasePool pool; | |
36 NSArray* library_paths = NSSearchPathForDirectoriesInDomains( | |
37 NSLibraryDirectory, | |
38 domain, | |
Mark Mentovai
2011/03/21 16:50:49
Come up with a classier way to break these lines u
dmac
2011/03/21 22:59:19
Done.
| |
39 true); | |
40 DCHECK_EQ([library_paths count], 1U); | |
41 NSString* library_path = [library_paths objectAtIndex:0]; | |
42 | |
43 NSString *launch_dir_name = (type == Launchd::Daemon) ? @"LaunchDaemons" | |
44 : @"LaunchAgents"; | |
45 NSString* launch_dir = | |
46 [library_path stringByAppendingPathComponent:launch_dir_name]; | |
Mark Mentovai
2011/03/21 16:50:49
4-space the continuation line.
dmac
2011/03/21 22:59:19
Done.
| |
47 | |
48 NSError* err; | |
49 if (![[NSFileManager defaultManager] createDirectoryAtPath:launch_dir | |
50 withIntermediateDirectories:YES | |
51 attributes:nil | |
52 error:&err]) { | |
53 LOG(ERROR) << "CopyPListURL " << base::mac::NSToCFCast(err); | |
54 return NULL; | |
55 } | |
56 | |
57 NSString* plist_file_path | |
Mark Mentovai
2011/03/21 16:50:49
= goes on this line, not the next.
dmac
2011/03/21 22:59:19
I hate that. Done.
| |
58 = [launch_dir stringByAppendingPathComponent:base::mac::CFToNSCast(name)]; | |
59 plist_file_path = [plist_file_path stringByAppendingPathExtension:@"plist"]; | |
60 NSURL *url = [[NSURL alloc] initFileURLWithPath:plist_file_path | |
Mark Mentovai
2011/03/21 16:50:49
You’ve been putting the *s on the type and not the
dmac
2011/03/21 22:59:19
Done.
| |
61 isDirectory:NO]; | |
62 return base::mac::NSToCFCast(url); | |
63 } | |
64 | |
65 } | |
Mark Mentovai
2011/03/21 16:50:49
} // namespace
dmac
2011/03/21 22:59:19
Done.
| |
66 | |
67 Launchd* Launchd::g_instance_ = NULL; | |
68 bool Launchd::g_set_to_singleton_ = false; | |
Mark Mentovai
2011/03/21 16:50:49
I don’t see why you need this.
dmac
2011/03/21 22:59:19
Done.
| |
69 | |
70 Launchd* Launchd::GetInstance() { | |
71 if (!g_instance_) { | |
72 g_instance_ = Singleton<Launchd>::get(); | |
73 g_set_to_singleton_ = true; | |
74 } | |
75 return g_instance_; | |
76 } | |
77 | |
78 void Launchd::SetInstance(Launchd* instance) { | |
79 CHECK(!g_set_to_singleton_); | |
80 g_instance_ = instance; | |
81 } | |
82 | |
83 Launchd::~Launchd() { } | |
84 | |
85 CFDictionaryRef Launchd::CopyLaunchdExports() { | |
86 return GTMCopyLaunchdExports(); | |
Mark Mentovai
2011/03/21 16:50:49
I haven’t read this code or its API documentation,
dmac
2011/03/21 22:59:19
OK.. it's been working for a while now.
| |
87 } | |
88 | |
89 CFDictionaryRef Launchd::CopyLaunchdJobDictionary(CFStringRef label) { | |
90 return GTMSMJobCopyDictionary(label); | |
91 } | |
92 | |
93 CFDictionaryRef Launchd::CopyLaunchdDictionaryByCheckingIn(CFErrorRef* error) { | |
94 return GTMSMJobCheckIn(error); | |
95 } | |
96 | |
97 bool Launchd::RemoveLaunchdJob(CFStringRef label, CFErrorRef* error) { | |
98 return GTMSMJobRemove(label, error); | |
99 } | |
100 | |
101 bool Launchd::ShutdownLaunchdJob(Domain domain, | |
102 Type type, | |
103 CFStringRef name, | |
104 CFStringRef cf_session_type) { | |
105 base::mac::ScopedCFTypeRef<CFURLRef> cf_url(CopyPListURL(domain, type, name)); | |
106 std::string path(FileSystemPathFromCFURL(cf_url)); | |
Mark Mentovai
2011/03/21 16:50:49
Isn’t it kind of obnoxious to use CopyPlistURL, wh
dmac
2011/03/21 22:59:19
Done. It made sense in the earlier CL because some
| |
107 std::string session_type(base::SysCFStringRefToUTF8(cf_session_type)); | |
108 std::vector<std::string> argv; | |
109 argv.push_back("/bin/launchctl"); | |
110 argv.push_back("unload"); | |
111 argv.push_back("-S"); | |
112 argv.push_back(session_type); | |
113 argv.push_back(path); | |
114 base::ProcessHandle handle; | |
115 return base::LaunchAppInNewProcessGroup(argv, | |
116 base::environment_vector(), | |
117 base::file_handle_mapping_vector(), | |
118 NO, | |
119 &handle); | |
120 } | |
121 | |
122 bool Launchd::RestartLaunchdJob(Domain domain, | |
123 Type type, | |
124 CFStringRef name, | |
125 CFStringRef cf_session_type) { | |
126 base::mac::ScopedCFTypeRef<CFURLRef> cf_url(CopyPListURL(domain, type, name)); | |
127 std::string path(FileSystemPathFromCFURL(cf_url)); | |
128 std::string session_type(base::SysCFStringRefToUTF8(cf_session_type)); | |
129 std::vector<std::string> argv; | |
130 argv.push_back("/bin/bash"); | |
131 argv.push_back("--noprofile"); | |
132 argv.push_back("-c"); | |
133 std::string command = base::StringPrintf( | |
134 "/bin/launchctl unload -S %s \"%s\";/bin/launchctl load -S %s \"%s\";", | |
135 session_type.c_str(), path.c_str(), | |
Mark Mentovai
2011/03/21 16:50:49
Didn’t I mention doing proper sanitization and esc
| |
136 session_type.c_str(), path.c_str()); | |
137 argv.push_back(command); | |
138 base::ProcessHandle handle; | |
139 return base::LaunchAppInNewProcessGroup(argv, | |
140 base::environment_vector(), | |
141 base::file_handle_mapping_vector(), | |
142 NO, | |
143 &handle); | |
144 } | |
145 | |
146 CFMutableDictionaryRef Launchd::ReadLaunchdPlist(Domain domain, | |
147 Type type, | |
148 CFStringRef name) { | |
149 base::mac::ScopedNSAutoreleasePool pool; | |
150 base::mac::ScopedCFTypeRef<CFURLRef> cf_url(CopyPListURL(domain, type, name)); | |
151 NSURL* ns_url = base::mac::CFToNSCast(cf_url); | |
152 NSMutableDictionary* plist | |
Mark Mentovai
2011/03/21 16:50:49
= goes on this line.
| |
153 = [[NSMutableDictionary alloc] initWithContentsOfURL:ns_url]; | |
Mark Mentovai
2011/03/21 16:50:49
Continuation line uses a 4-space indent.
Mark Mentovai
2011/03/21 16:50:49
OK, I see, the caller owns the returned dictionary
| |
154 return base::mac::NSToCFCast(plist); | |
155 } | |
156 | |
157 bool Launchd::WriteLaunchdPlist(Domain domain, | |
158 Type type, | |
159 CFStringRef name, | |
160 CFDictionaryRef dict) { | |
161 base::mac::ScopedNSAutoreleasePool pool; | |
162 base::mac::ScopedCFTypeRef<CFURLRef> cf_url(CopyPListURL(domain, type, name)); | |
163 base::mac::ScopedCFTypeRef<CFDataRef> cf_data( | |
164 CFPropertyListCreateXMLData(kCFAllocatorDefault, | |
165 dict)); | |
Mark Mentovai
2011/03/21 16:50:49
dict fits on the previous line for sure.
| |
166 Boolean good = false; | |
Mark Mentovai
2011/03/21 16:50:49
Just bool, not Boolean.
| |
167 if (cf_url.get() && cf_data.get()) { | |
168 SInt32 error = 0; | |
169 good = CFURLWriteDataAndPropertiesToResource(cf_url, cf_data, NULL, &error); | |
170 if (!good) { | |
171 LOG(ERROR) << "WriteLaunchdPlist " << error; | |
172 } | |
173 } | |
174 return good; | |
175 } | |
176 | |
177 bool Launchd::DeleteLaunchDPlist(Domain domain, Type type, CFStringRef name) { | |
178 base::mac::ScopedCFTypeRef<CFURLRef> url(CopyPListURL(domain, type, name)); | |
179 if (!url) { | |
180 return false; | |
181 } | |
182 SInt32 error = 0; | |
183 if (!CFURLDestroyResource(url, &error)) { | |
184 LOG(ERROR) << "DeleteLaunchDPlist: " << error; | |
185 return false; | |
186 } | |
187 return true; | |
188 } | |
189 | |
Mark Mentovai
2011/03/21 16:50:49
Dump the blank line at EOF.
| |
OLD | NEW |