crashpad/util/mac/service_management.cc
Mark Mentovai 88690cece3 util/mac: Provide wrappers for <launch.h> functions deprecated in 10.10.
A few stragglers were missing from bc401f6aac22. I really don’t know how
that’s possible, since I thought I was testing it with the correct SDK.
I guess I wasn’t.

BUG=crashpad:11
R=rsesek@chromium.org

Review URL: https://codereview.chromium.org/978883007
2015-03-08 15:12:40 -04:00

137 lines
3.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2014 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "util/mac/service_management.h"
#include <errno.h>
#include <launch.h>
#include "base/mac/scoped_launch_data.h"
#include "util/mac/launchd.h"
#include "util/misc/clock.h"
namespace crashpad {
namespace {
launch_data_t LaunchDataDictionaryForJob(const std::string& label) {
base::mac::ScopedLaunchData request(LaunchDataAlloc(LAUNCH_DATA_DICTIONARY));
LaunchDataDictInsert(
request, LaunchDataNewString(label.c_str()), LAUNCH_KEY_GETJOB);
base::mac::ScopedLaunchData response(LaunchMsg(request));
if (LaunchDataGetType(response) != LAUNCH_DATA_DICTIONARY) {
return nullptr;
}
return response.release();
}
} // namespace
bool ServiceManagementSubmitJob(CFDictionaryRef job_cf) {
base::mac::ScopedLaunchData job_launch(CFPropertyToLaunchData(job_cf));
if (!job_launch.get()) {
return false;
}
base::mac::ScopedLaunchData jobs(LaunchDataAlloc(LAUNCH_DATA_ARRAY));
LaunchDataArraySetIndex(jobs, job_launch.release(), 0);
base::mac::ScopedLaunchData request(LaunchDataAlloc(LAUNCH_DATA_DICTIONARY));
LaunchDataDictInsert(request, jobs.release(), LAUNCH_KEY_SUBMITJOB);
base::mac::ScopedLaunchData response(LaunchMsg(request));
if (LaunchDataGetType(response) != LAUNCH_DATA_ARRAY) {
return false;
}
if (LaunchDataArrayGetCount(response) != 1) {
return false;
}
launch_data_t response_element = LaunchDataArrayGetIndex(response, 0);
if (LaunchDataGetType(response_element) != LAUNCH_DATA_ERRNO) {
return false;
}
int err = LaunchDataGetErrno(response_element);
if (err != 0) {
return false;
}
return true;
}
bool ServiceManagementRemoveJob(const std::string& label, bool wait) {
base::mac::ScopedLaunchData request(LaunchDataAlloc(LAUNCH_DATA_DICTIONARY));
LaunchDataDictInsert(
request, LaunchDataNewString(label.c_str()), LAUNCH_KEY_REMOVEJOB);
base::mac::ScopedLaunchData response(LaunchMsg(request));
if (LaunchDataGetType(response) != LAUNCH_DATA_ERRNO) {
return false;
}
int err = LaunchDataGetErrno(response);
if (err == EINPROGRESS) {
if (wait) {
// TODO(mark): Use a kqueue to wait for the process to exit. To avoid a
// race, the kqueue would need to be set up prior to asking launchd to
// remove the job. Even so, the jobs PID may change between the time its
// obtained and the time the kqueue is set up, so this is nontrivial.
do {
SleepNanoseconds(1E5); // 100 microseconds
} while (ServiceManagementIsJobLoaded(label));
}
return true;
}
if (err != 0) {
return false;
}
return true;
}
bool ServiceManagementIsJobLoaded(const std::string& label) {
base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label));
if (!dictionary) {
return false;
}
return true;
}
pid_t ServiceManagementIsJobRunning(const std::string& label) {
base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label));
if (!dictionary) {
return 0;
}
launch_data_t pid = LaunchDataDictLookup(dictionary, LAUNCH_JOBKEY_PID);
if (!pid) {
return 0;
}
if (LaunchDataGetType(pid) != LAUNCH_DATA_INTEGER) {
return 0;
}
return LaunchDataGetInteger(pid);
}
} // namespace crashpad