2014-08-15 22:30:24 -07:00
|
|
|
|
// 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"
|
|
|
|
|
|
2014-09-22 13:11:40 -04:00
|
|
|
|
#include <errno.h>
|
2014-08-15 22:30:24 -07:00
|
|
|
|
#include <launch.h>
|
|
|
|
|
|
2014-09-22 13:11:40 -04:00
|
|
|
|
#include "base/mac/scoped_launch_data.h"
|
|
|
|
|
#include "util/mac/launchd.h"
|
2014-09-24 14:08:48 -04:00
|
|
|
|
#include "util/misc/clock.h"
|
2014-08-15 22:30:24 -07:00
|
|
|
|
|
2015-03-06 22:27:25 -05:00
|
|
|
|
namespace crashpad {
|
|
|
|
|
|
2014-08-15 22:30:24 -07:00
|
|
|
|
namespace {
|
|
|
|
|
|
2014-09-22 13:11:40 -04:00
|
|
|
|
launch_data_t LaunchDataDictionaryForJob(const std::string& label) {
|
2015-03-06 22:27:25 -05:00
|
|
|
|
base::mac::ScopedLaunchData request(LaunchDataAlloc(LAUNCH_DATA_DICTIONARY));
|
|
|
|
|
LaunchDataDictInsert(
|
2016-01-06 12:31:59 -05:00
|
|
|
|
request.get(), LaunchDataNewString(label.c_str()), LAUNCH_KEY_GETJOB);
|
2014-08-15 22:30:24 -07:00
|
|
|
|
|
2016-01-06 12:31:59 -05:00
|
|
|
|
base::mac::ScopedLaunchData response(LaunchMsg(request.get()));
|
|
|
|
|
if (LaunchDataGetType(response.get()) != LAUNCH_DATA_DICTIONARY) {
|
2014-10-14 11:10:45 -04:00
|
|
|
|
return nullptr;
|
2014-09-22 13:11:40 -04:00
|
|
|
|
}
|
2014-08-15 22:30:24 -07:00
|
|
|
|
|
2014-09-22 13:11:40 -04:00
|
|
|
|
return response.release();
|
2014-09-16 17:45:12 -04:00
|
|
|
|
}
|
2014-08-15 22:30:24 -07:00
|
|
|
|
|
2014-09-16 17:45:12 -04:00
|
|
|
|
} // namespace
|
2014-08-15 22:30:24 -07:00
|
|
|
|
|
2014-09-16 17:45:12 -04:00
|
|
|
|
bool ServiceManagementSubmitJob(CFDictionaryRef job_cf) {
|
2014-09-22 13:11:40 -04:00
|
|
|
|
base::mac::ScopedLaunchData job_launch(CFPropertyToLaunchData(job_cf));
|
|
|
|
|
if (!job_launch.get()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-06 22:27:25 -05:00
|
|
|
|
base::mac::ScopedLaunchData jobs(LaunchDataAlloc(LAUNCH_DATA_ARRAY));
|
2016-01-06 12:31:59 -05:00
|
|
|
|
LaunchDataArraySetIndex(jobs.get(), job_launch.release(), 0);
|
2014-09-22 13:11:40 -04:00
|
|
|
|
|
2015-03-06 22:27:25 -05:00
|
|
|
|
base::mac::ScopedLaunchData request(LaunchDataAlloc(LAUNCH_DATA_DICTIONARY));
|
2016-01-06 12:31:59 -05:00
|
|
|
|
LaunchDataDictInsert(request.get(), jobs.release(), LAUNCH_KEY_SUBMITJOB);
|
2014-09-22 13:11:40 -04:00
|
|
|
|
|
2016-01-06 12:31:59 -05:00
|
|
|
|
base::mac::ScopedLaunchData response(LaunchMsg(request.get()));
|
|
|
|
|
if (LaunchDataGetType(response.get()) != LAUNCH_DATA_ARRAY) {
|
2014-09-22 13:11:40 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-06 12:31:59 -05:00
|
|
|
|
if (LaunchDataArrayGetCount(response.get()) != 1) {
|
2014-09-22 13:11:40 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-06 12:31:59 -05:00
|
|
|
|
launch_data_t response_element = LaunchDataArrayGetIndex(response.get(), 0);
|
2015-03-06 22:27:25 -05:00
|
|
|
|
if (LaunchDataGetType(response_element) != LAUNCH_DATA_ERRNO) {
|
2014-09-22 13:11:40 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-06 22:27:25 -05:00
|
|
|
|
int err = LaunchDataGetErrno(response_element);
|
2014-09-22 13:11:40 -04:00
|
|
|
|
if (err != 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2014-08-15 22:30:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ServiceManagementRemoveJob(const std::string& label, bool wait) {
|
2015-03-06 22:27:25 -05:00
|
|
|
|
base::mac::ScopedLaunchData request(LaunchDataAlloc(LAUNCH_DATA_DICTIONARY));
|
|
|
|
|
LaunchDataDictInsert(
|
2016-01-06 12:31:59 -05:00
|
|
|
|
request.get(), LaunchDataNewString(label.c_str()), LAUNCH_KEY_REMOVEJOB);
|
2014-09-22 13:11:40 -04:00
|
|
|
|
|
2016-01-06 12:31:59 -05:00
|
|
|
|
base::mac::ScopedLaunchData response(LaunchMsg(request.get()));
|
|
|
|
|
if (LaunchDataGetType(response.get()) != LAUNCH_DATA_ERRNO) {
|
2014-09-22 13:11:40 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-06 12:31:59 -05:00
|
|
|
|
int err = LaunchDataGetErrno(response.get());
|
2014-09-22 13:11:40 -04:00
|
|
|
|
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 job’s PID may change between the time it’s
|
|
|
|
|
// obtained and the time the kqueue is set up, so this is nontrivial.
|
|
|
|
|
do {
|
2014-09-24 14:08:48 -04:00
|
|
|
|
SleepNanoseconds(1E5); // 100 microseconds
|
2014-09-22 13:11:40 -04:00
|
|
|
|
} while (ServiceManagementIsJobLoaded(label));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (err != 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2014-08-15 22:30:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ServiceManagementIsJobLoaded(const std::string& label) {
|
2014-09-22 13:11:40 -04:00
|
|
|
|
base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label));
|
2016-01-06 12:31:59 -05:00
|
|
|
|
if (!dictionary.is_valid()) {
|
2014-09-22 13:11:40 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2014-08-15 22:30:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pid_t ServiceManagementIsJobRunning(const std::string& label) {
|
2014-09-22 13:11:40 -04:00
|
|
|
|
base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label));
|
2016-01-06 12:31:59 -05:00
|
|
|
|
if (!dictionary.is_valid()) {
|
2014-09-22 13:11:40 -04:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-06 12:31:59 -05:00
|
|
|
|
launch_data_t pid = LaunchDataDictLookup(dictionary.get(), LAUNCH_JOBKEY_PID);
|
2014-09-23 14:54:41 -04:00
|
|
|
|
if (!pid) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-06 22:27:25 -05:00
|
|
|
|
if (LaunchDataGetType(pid) != LAUNCH_DATA_INTEGER) {
|
2014-09-22 13:11:40 -04:00
|
|
|
|
return 0;
|
2014-08-15 22:30:24 -07:00
|
|
|
|
}
|
2014-09-22 13:11:40 -04:00
|
|
|
|
|
2015-03-08 15:12:40 -04:00
|
|
|
|
return LaunchDataGetInteger(pid);
|
2014-08-15 22:30:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace crashpad
|