summaryrefslogtreecommitdiff
path: root/chromium/chrome/common/service_process_util_mac.mm
blob: edef9967a03b52186849d764a617f1b422b5fb99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Foundation/Foundation.h>
#include <launch.h>
#include <sys/un.h>

#include <memory>
#include <vector>

#include "base/bind.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/mac/bundle_locations.h"
#include "base/mac/foundation_util.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/metrics/histogram_macros.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/version.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/mac/launchd.h"
#include "chrome/common/service_process_util_posix.h"
#include "components/version_info/version_info.h"

using ::base::FilePathWatcher;

namespace {

#define kServiceProcessSessionType "Aqua"

CFStringRef CopyServiceProcessLaunchDName() {
  @autoreleasepool {
    NSBundle* bundle = base::mac::FrameworkBundle();
    return CFStringCreateCopy(kCFAllocatorDefault,
                              base::mac::NSToCFCast([bundle bundleIdentifier]));
  }
}

NSString* GetServiceProcessLaunchDLabel() {
  base::scoped_nsobject<NSString> name(
      base::mac::CFToNSCast(CopyServiceProcessLaunchDName()));
  NSString* label = [name stringByAppendingString:@".service_process"];
  base::FilePath user_data_dir;
  base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
  std::string user_data_dir_path = user_data_dir.value();
  NSString* ns_path = base::SysUTF8ToNSString(user_data_dir_path);
  ns_path = [ns_path stringByReplacingOccurrencesOfString:@" "
                                               withString:@"_"];
  label = [label stringByAppendingString:ns_path];
  return label;
}

bool RemoveFromLaunchd() {
  // We're killing a file.
  base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
                                                base::BlockingType::MAY_BLOCK);
  base::ScopedCFTypeRef<CFStringRef> name(CopyServiceProcessLaunchDName());
  return Launchd::GetInstance()->DeletePlist(Launchd::User,
                                             Launchd::Agent,
                                             name);
}

class ExecFilePathWatcherCallback {
 public:
  ExecFilePathWatcherCallback() {}
  ~ExecFilePathWatcherCallback() {}

  bool Init(const base::FilePath& path);
  void NotifyPathChanged(const base::FilePath& path, bool error);

 private:
  base::scoped_nsobject<NSURL> executable_fsref_;
};

NSString* GetServiceProcessMachName() {
  base::scoped_nsobject<NSString> name(
      base::mac::CFToNSCast(CopyServiceProcessLaunchDName()));
  return [name stringByAppendingFormat:@".service_process.%lu",
                                       [GetServiceProcessLaunchDLabel() hash]];
}

}  // namespace

mojo::NamedPlatformChannel::ServerName GetServiceProcessServerName() {
  return base::SysNSStringToUTF8(GetServiceProcessMachName());
}

bool ForceServiceProcessShutdown(const std::string& /* version */,
                                 base::ProcessId /* process_id */) {
  const std::string& label =
      base::SysNSStringToUTF8(GetServiceProcessLaunchDLabel());
  bool ret = Launchd::GetInstance()->RemoveJob(label);
  if (!ret) {
    DLOG(ERROR) << "ForceServiceProcessShutdown: " << label;
  }
  return ret;
}

bool ServiceProcessState::GetServiceProcessData(std::string* version,
                                                base::ProcessId* pid) {
  @autoreleasepool {
    std::string label =
        base::SysNSStringToUTF8(GetServiceProcessLaunchDLabel());
    mac::services::JobInfo info;
    if (!Launchd::GetInstance()->GetJobInfo(label, &info))
      return false;
    // Anything past here will return true in that there does appear
    // to be a service process of some sort registered with launchd.
    if (version) {
      *version = "0";
      NSString* exe_path = base::SysUTF8ToNSString(info.program);
      if (exe_path) {
        NSString* bundle_path = [[[exe_path stringByDeletingLastPathComponent]
            stringByDeletingLastPathComponent]
            stringByDeletingLastPathComponent];
        NSBundle* bundle = [NSBundle bundleWithPath:bundle_path];
        if (bundle) {
          NSString* ns_version =
              [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
          if (ns_version) {
            *version = base::SysNSStringToUTF8(ns_version);
          } else {
            DLOG(ERROR) << "Unable to get version at: "
                        << reinterpret_cast<CFStringRef>(bundle_path);
          }
        } else {
          // The bundle has been deleted out from underneath the registered
          // job.
          DLOG(ERROR) << "Unable to get bundle at: "
                      << reinterpret_cast<CFStringRef>(bundle_path);
        }
      } else {
        DLOG(ERROR) << "Unable to get executable path for service process";
      }
    }
    if (pid) {
      *pid = info.pid ? *info.pid : -1;
    }
    return true;
  }
}

bool ServiceProcessState::Initialize() {
  mac::services::JobInfo info;
  // The Mach service will be checked when GetServiceProcessServerEndpoint()
  // is called.
  bool ok = Launchd::GetInstance()->GetJobInfo(
      base::SysNSStringToUTF8(GetServiceProcessLaunchDLabel()), &info);
  if (!ok) {
    DLOG(ERROR) << "Failed to look up job info.";
    return false;
  }
  state_->job_info.program = info.program;
  return true;
}

mojo::PlatformChannelServerEndpoint
ServiceProcessState::GetServiceProcessServerEndpoint() {
  mojo::NamedPlatformChannel::Options options;
  options.server_name = base::SysNSStringToUTF8(GetServiceProcessMachName());
  return mojo::NamedPlatformChannel(options).TakeServerEndpoint();
}

bool CheckServiceProcessReady() {
  std::string version;
  pid_t pid;
  if (!ServiceProcessState::GetServiceProcessData(&version, &pid)) {
    return false;
  }
  base::Version service_version(version);
  bool ready = true;
  if (!service_version.IsValid()) {
    ready = false;
  } else {
    const base::Version& running_version = version_info::GetVersion();
    if (!running_version.IsValid()) {
      // Our own version is invalid. This is an error case. Pretend that we
      // are out of date.
      NOTREACHED();
      ready = true;
    } else {
      ready = running_version.CompareTo(service_version) <= 0;
    }
  }
  if (!ready) {
    ForceServiceProcessShutdown(version, pid);
  }
  return ready;
}

mac::services::JobOptions GetServiceProcessJobOptions(
    base::CommandLine* cmd_line,
    bool for_auto_launch) {
  mac::services::JobOptions options;

  options.label = base::SysNSStringToUTF8(GetServiceProcessLaunchDLabel());
  options.executable_path = cmd_line->GetProgram().value();
  options.arguments = cmd_line->argv();
  options.mach_service_name =
      base::SysNSStringToUTF8(GetServiceProcessMachName());
  options.run_at_load = for_auto_launch;
  options.auto_launch = for_auto_launch;

  return options;
}

CFDictionaryRef CreateServiceProcessLaunchdPlist(base::CommandLine* cmd_line,
                                                 bool for_auto_launch) {
  @autoreleasepool {
    NSString* program = base::SysUTF8ToNSString(cmd_line->GetProgram().value());

    std::vector<std::string> args = cmd_line->argv();
    NSMutableArray* ns_args = [NSMutableArray arrayWithCapacity:args.size()];

    for (std::vector<std::string>::iterator iter = args.begin();
         iter < args.end(); ++iter) {
      [ns_args addObject:base::SysUTF8ToNSString(*iter)];
    }

    // See the man page for launchd.plist.
    NSMutableDictionary* launchd_plist = [@{
      @LAUNCH_JOBKEY_LABEL : GetServiceProcessLaunchDLabel(),
      @LAUNCH_JOBKEY_PROGRAM : program,
      @LAUNCH_JOBKEY_PROGRAMARGUMENTS : ns_args,
      @LAUNCH_JOBKEY_MACHSERVICES : GetServiceProcessMachName(),
    } mutableCopy];

    if (for_auto_launch) {
      // We want the service process to be able to exit if there are no services
      // enabled. With a value of NO in the SuccessfulExit key, launchd will
      // relaunch the service automatically in any other case than exiting
      // cleanly with a 0 return code.
      NSDictionary* keep_alive =
          @{@LAUNCH_JOBKEY_KEEPALIVE_SUCCESSFULEXIT : @NO};
      NSDictionary* auto_launchd_plist = @{
        @LAUNCH_JOBKEY_RUNATLOAD : @YES,
        @LAUNCH_JOBKEY_KEEPALIVE : keep_alive,
        @LAUNCH_JOBKEY_LIMITLOADTOSESSIONTYPE : @kServiceProcessSessionType
      };
      [launchd_plist addEntriesFromDictionary:auto_launchd_plist];
    }
    return reinterpret_cast<CFDictionaryRef>(launchd_plist);
  }
}

// Writes the launchd property list into the user's LaunchAgents directory,
// creating that directory if needed. This will cause the service process to be
// auto launched on the next user login.
bool ServiceProcessState::AddToAutoRun() {
  // We're creating directories and writing a file.
  base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
                                                base::BlockingType::MAY_BLOCK);
  DCHECK(autorun_command_line_.get());
  base::ScopedCFTypeRef<CFStringRef> name(CopyServiceProcessLaunchDName());
  base::ScopedCFTypeRef<CFDictionaryRef> plist(
      CreateServiceProcessLaunchdPlist(autorun_command_line_.get(), true));
  return Launchd::GetInstance()->WritePlistToFile(Launchd::User,
                                                  Launchd::Agent,
                                                  name,
                                                  plist);
}

bool ServiceProcessState::RemoveFromAutoRun() {
  return RemoveFromLaunchd();
}

bool ServiceProcessState::StateData::WatchExecutable() {
  @autoreleasepool {
    base::FilePath executable_path = base::FilePath(job_info.program);
    std::unique_ptr<ExecFilePathWatcherCallback> callback(
        new ExecFilePathWatcherCallback);
    if (!callback->Init(executable_path)) {
      DLOG(ERROR) << "executable_watcher.Init " << executable_path.value();
      return false;
    }
    if (!executable_watcher.Watch(
            executable_path, false,
            base::Bind(&ExecFilePathWatcherCallback::NotifyPathChanged,
                       base::Owned(callback.release())))) {
      DLOG(ERROR) << "executable_watcher.watch " << executable_path.value();
      return false;
    }
    return true;
  }
}

bool ExecFilePathWatcherCallback::Init(const base::FilePath& path) {
  NSString* path_string = base::mac::FilePathToNSString(path);
  NSURL* path_url = [NSURL fileURLWithPath:path_string isDirectory:NO];
  executable_fsref_.reset([[path_url fileReferenceURL] retain]);
  return executable_fsref_.get() != nil;
}

void ExecFilePathWatcherCallback::NotifyPathChanged(const base::FilePath& path,
                                                    bool error) {
  if (error) {
    NOTREACHED();  // TODO(darin): Do something smarter?
    return;
  }

  @autoreleasepool {
    bool needs_shutdown = false;
    bool needs_restart = false;
    bool good_bundle = false;

    // Go from bundle/Contents/MacOS/executable to bundle.
    NSURL* bundle_url = [[[executable_fsref_ URLByDeletingLastPathComponent]
        URLByDeletingLastPathComponent] URLByDeletingLastPathComponent];
    if (bundle_url) {
      base::ScopedCFTypeRef<CFBundleRef> bundle(CFBundleCreate(
          kCFAllocatorDefault, base::mac::NSToCFCast(bundle_url)));
      good_bundle = CFBundleGetIdentifier(bundle) != NULL;
    }

    if (!good_bundle) {
      needs_shutdown = true;
    } else {
      bool in_trash = false;
      NSFileManager* file_manager = [NSFileManager defaultManager];
      NSURLRelationship relationship;
      if ([file_manager getRelationship:&relationship
                            ofDirectory:NSTrashDirectory
                               inDomain:0
                            toItemAtURL:executable_fsref_
                                  error:nil]) {
        in_trash = relationship == NSURLRelationshipContains;
      }
      if (in_trash) {
        needs_shutdown = true;
      } else {
        bool was_moved = true;
        NSString* path_string = base::mac::FilePathToNSString(path);
        NSURL* path_url = [NSURL fileURLWithPath:path_string isDirectory:NO];
        NSURL* path_ref = [path_url fileReferenceURL];
        if (path_ref != nil) {
          if ([path_ref isEqual:executable_fsref_]) {
            was_moved = false;
          }
        }
        if (was_moved) {
          needs_restart = true;
        }
      }
    }
    if (needs_shutdown || needs_restart) {
      // First deal with the plist.
      base::ScopedCFTypeRef<CFStringRef> name(CopyServiceProcessLaunchDName());
      if (needs_restart) {
        base::ScopedCFTypeRef<CFMutableDictionaryRef> plist(
            Launchd::GetInstance()->CreatePlistFromFile(Launchd::User,
                                                        Launchd::Agent, name));
        if (plist.get()) {
          NSMutableDictionary* ns_plist = base::mac::CFToNSCast(plist);
          NSURL* new_path = [executable_fsref_ filePathURL];
          DCHECK([new_path isFileURL]);
          NSString* ns_new_path = [new_path path];
          ns_plist[@LAUNCH_JOBKEY_PROGRAM] = ns_new_path;
          base::scoped_nsobject<NSMutableArray> args(
              [ns_plist[@LAUNCH_JOBKEY_PROGRAMARGUMENTS] mutableCopy]);
          args[0] = ns_new_path;
          ns_plist[@LAUNCH_JOBKEY_PROGRAMARGUMENTS] = args;
          if (!Launchd::GetInstance()->WritePlistToFile(
                  Launchd::User, Launchd::Agent, name, plist)) {
            DLOG(ERROR) << "Unable to rewrite plist.";
            needs_shutdown = true;
          }
        } else {
          DLOG(ERROR) << "Unable to read plist.";
          needs_shutdown = true;
        }
      }
      if (needs_shutdown) {
        if (!RemoveFromLaunchd()) {
          DLOG(ERROR) << "Unable to RemoveFromLaunchd.";
        }
      }

      // Then deal with the process.
      CFStringRef session_type = CFSTR(kServiceProcessSessionType);
      if (needs_restart) {
        if (!Launchd::GetInstance()->RestartJob(Launchd::User, Launchd::Agent,
                                                name, session_type)) {
          DLOG(ERROR) << "RestartLaunchdJob";
          needs_shutdown = true;
        }
      }
      if (needs_shutdown) {
        const std::string& label =
            base::SysNSStringToUTF8(GetServiceProcessLaunchDLabel());
        if (!Launchd::GetInstance()->RemoveJob(label)) {
          DLOG(ERROR) << "RemoveJob " << label;
          // Exiting with zero, so launchd doesn't restart the process.
          exit(0);
        }
      }
    }
  }
}