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"
|
|
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
#include <launch.h>
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "base/mac/foundation_util.h"
|
|
|
|
|
#include "base/mac/scoped_cftyperef.h"
|
|
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
2014-09-24 14:08:48 -04:00
|
|
|
|
#include "util/misc/clock.h"
|
2015-11-16 13:39:01 -05:00
|
|
|
|
#include "util/misc/random_string.h"
|
Move some parts of ProcessReader (in snapshot) to ProcessInfo (in util).
Also, move ProcessArgumentsForPID() into ProcessInfo.
This change prepares for a TaskForPID() implementation that’s capable of
operating correctly in a setuid root executable. TaskForPID() belongs in
util/mach, but for its permission checks, it must access some process
properties that were previously fetched by ProcessReader in snapshot.
util can’t depend on snapshot. The generic util-safe process information
bits (Is64Bit(), ProcessID(), ParentProcessID(), and StartTime()) are
moved from ProcessReader to ProcessInfo (in util), where the current
ProcessReader can use it (as it’s OK for snapshot to depend on util),
and the future TaskForPID() in util can also use it. ProcessInfo also
contains other methods that TaskForPID() will use, providing access to
the credentials that the target process holds. ProcessArgumentsForPID()
is related, and is also now a part of ProcessInfo.
TEST=snapshot_test, util_test
R=rsesek@chromium.org
Review URL: https://codereview.chromium.org/727973002
2014-11-14 17:54:42 -05:00
|
|
|
|
#include "util/posix/process_info.h"
|
2014-08-15 22:30:24 -07:00
|
|
|
|
#include "util/stdlib/objc.h"
|
|
|
|
|
|
2014-10-07 17:28:50 -04:00
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace test {
|
2014-08-15 22:30:24 -07:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// Ensures that the process with the specified PID is running, identifying it by
|
|
|
|
|
// requiring that its argv[argc - 1] compare equal to last_arg.
|
|
|
|
|
void ExpectProcessIsRunning(pid_t pid, std::string& last_arg) {
|
Move some parts of ProcessReader (in snapshot) to ProcessInfo (in util).
Also, move ProcessArgumentsForPID() into ProcessInfo.
This change prepares for a TaskForPID() implementation that’s capable of
operating correctly in a setuid root executable. TaskForPID() belongs in
util/mach, but for its permission checks, it must access some process
properties that were previously fetched by ProcessReader in snapshot.
util can’t depend on snapshot. The generic util-safe process information
bits (Is64Bit(), ProcessID(), ParentProcessID(), and StartTime()) are
moved from ProcessReader to ProcessInfo (in util), where the current
ProcessReader can use it (as it’s OK for snapshot to depend on util),
and the future TaskForPID() in util can also use it. ProcessInfo also
contains other methods that TaskForPID() will use, providing access to
the credentials that the target process holds. ProcessArgumentsForPID()
is related, and is also now a part of ProcessInfo.
TEST=snapshot_test, util_test
R=rsesek@chromium.org
Review URL: https://codereview.chromium.org/727973002
2014-11-14 17:54:42 -05:00
|
|
|
|
ProcessInfo process_info;
|
|
|
|
|
ASSERT_TRUE(process_info.Initialize(pid));
|
|
|
|
|
|
2014-08-15 22:30:24 -07:00
|
|
|
|
// The process may not have called exec yet, so loop with a small delay while
|
|
|
|
|
// looking for the cookie.
|
|
|
|
|
int outer_tries = 10;
|
|
|
|
|
std::vector<std::string> job_argv;
|
|
|
|
|
while (outer_tries--) {
|
Move some parts of ProcessReader (in snapshot) to ProcessInfo (in util).
Also, move ProcessArgumentsForPID() into ProcessInfo.
This change prepares for a TaskForPID() implementation that’s capable of
operating correctly in a setuid root executable. TaskForPID() belongs in
util/mach, but for its permission checks, it must access some process
properties that were previously fetched by ProcessReader in snapshot.
util can’t depend on snapshot. The generic util-safe process information
bits (Is64Bit(), ProcessID(), ParentProcessID(), and StartTime()) are
moved from ProcessReader to ProcessInfo (in util), where the current
ProcessReader can use it (as it’s OK for snapshot to depend on util),
and the future TaskForPID() in util can also use it. ProcessInfo also
contains other methods that TaskForPID() will use, providing access to
the credentials that the target process holds. ProcessArgumentsForPID()
is related, and is also now a part of ProcessInfo.
TEST=snapshot_test, util_test
R=rsesek@chromium.org
Review URL: https://codereview.chromium.org/727973002
2014-11-14 17:54:42 -05:00
|
|
|
|
// If the process is in the middle of calling exec, process_info.Arguments()
|
2014-08-15 22:30:24 -07:00
|
|
|
|
// may fail. Loop with a small retry delay while waiting for the expected
|
|
|
|
|
// successful call.
|
|
|
|
|
int inner_tries = 10;
|
|
|
|
|
bool success;
|
|
|
|
|
do {
|
Move some parts of ProcessReader (in snapshot) to ProcessInfo (in util).
Also, move ProcessArgumentsForPID() into ProcessInfo.
This change prepares for a TaskForPID() implementation that’s capable of
operating correctly in a setuid root executable. TaskForPID() belongs in
util/mach, but for its permission checks, it must access some process
properties that were previously fetched by ProcessReader in snapshot.
util can’t depend on snapshot. The generic util-safe process information
bits (Is64Bit(), ProcessID(), ParentProcessID(), and StartTime()) are
moved from ProcessReader to ProcessInfo (in util), where the current
ProcessReader can use it (as it’s OK for snapshot to depend on util),
and the future TaskForPID() in util can also use it. ProcessInfo also
contains other methods that TaskForPID() will use, providing access to
the credentials that the target process holds. ProcessArgumentsForPID()
is related, and is also now a part of ProcessInfo.
TEST=snapshot_test, util_test
R=rsesek@chromium.org
Review URL: https://codereview.chromium.org/727973002
2014-11-14 17:54:42 -05:00
|
|
|
|
success = process_info.Arguments(&job_argv);
|
2014-08-15 22:30:24 -07:00
|
|
|
|
if (success) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (inner_tries > 0) {
|
2014-09-24 14:08:48 -04:00
|
|
|
|
SleepNanoseconds(1E6); // 1 millisecond
|
2014-08-15 22:30:24 -07:00
|
|
|
|
}
|
|
|
|
|
} while (inner_tries--);
|
|
|
|
|
ASSERT_TRUE(success);
|
|
|
|
|
|
|
|
|
|
ASSERT_FALSE(job_argv.empty());
|
|
|
|
|
if (job_argv.back() == last_arg) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (outer_tries > 0) {
|
2014-09-24 14:08:48 -04:00
|
|
|
|
SleepNanoseconds(1E6); // 1 millisecond
|
2014-08-15 22:30:24 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT_FALSE(job_argv.empty());
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
|
EXPECT_EQ(job_argv.back(), last_arg);
|
2014-08-15 22:30:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensures that the process with the specified PID is not running. Because the
|
|
|
|
|
// PID may be reused for another process, a process is only treated as running
|
|
|
|
|
// if its argv[argc - 1] compares equal to last_arg.
|
|
|
|
|
void ExpectProcessIsNotRunning(pid_t pid, std::string& last_arg) {
|
|
|
|
|
// The process may not have exited yet, so loop with a small delay while
|
|
|
|
|
// checking that it has exited.
|
|
|
|
|
int tries = 10;
|
|
|
|
|
std::vector<std::string> job_argv;
|
|
|
|
|
while (tries--) {
|
Move some parts of ProcessReader (in snapshot) to ProcessInfo (in util).
Also, move ProcessArgumentsForPID() into ProcessInfo.
This change prepares for a TaskForPID() implementation that’s capable of
operating correctly in a setuid root executable. TaskForPID() belongs in
util/mach, but for its permission checks, it must access some process
properties that were previously fetched by ProcessReader in snapshot.
util can’t depend on snapshot. The generic util-safe process information
bits (Is64Bit(), ProcessID(), ParentProcessID(), and StartTime()) are
moved from ProcessReader to ProcessInfo (in util), where the current
ProcessReader can use it (as it’s OK for snapshot to depend on util),
and the future TaskForPID() in util can also use it. ProcessInfo also
contains other methods that TaskForPID() will use, providing access to
the credentials that the target process holds. ProcessArgumentsForPID()
is related, and is also now a part of ProcessInfo.
TEST=snapshot_test, util_test
R=rsesek@chromium.org
Review URL: https://codereview.chromium.org/727973002
2014-11-14 17:54:42 -05:00
|
|
|
|
ProcessInfo process_info;
|
|
|
|
|
if (!process_info.Initialize(pid) || !process_info.Arguments(&job_argv)) {
|
2014-08-15 22:30:24 -07:00
|
|
|
|
// The PID was not found.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The PID was found. It may have been recycled for another process. Make
|
|
|
|
|
// sure that the cookie isn’t found.
|
|
|
|
|
ASSERT_FALSE(job_argv.empty());
|
|
|
|
|
if (job_argv.back() != last_arg) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tries > 0) {
|
2014-09-24 14:08:48 -04:00
|
|
|
|
SleepNanoseconds(1E6); // 1 millisecond
|
2014-08-15 22:30:24 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT_FALSE(job_argv.empty());
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
|
EXPECT_NE(job_argv.back(), last_arg);
|
2014-08-15 22:30:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(ServiceManagement, SubmitRemoveJob) {
|
|
|
|
|
@autoreleasepool {
|
2015-11-16 13:39:01 -05:00
|
|
|
|
const std::string cookie = RandomString();
|
2014-08-15 22:30:24 -07:00
|
|
|
|
|
|
|
|
|
std::string shell_script =
|
|
|
|
|
base::StringPrintf("sleep 10; echo %s", cookie.c_str());
|
|
|
|
|
NSString* shell_script_ns = base::SysUTF8ToNSString(shell_script);
|
|
|
|
|
|
2017-07-25 13:34:04 -04:00
|
|
|
|
static constexpr char kJobLabel[] =
|
|
|
|
|
"org.chromium.crashpad.test.service_management";
|
2014-08-15 22:30:24 -07:00
|
|
|
|
NSDictionary* job_dictionary_ns = @{
|
2015-10-29 18:31:20 -04:00
|
|
|
|
@LAUNCH_JOBKEY_LABEL : @"org.chromium.crashpad.test.service_management",
|
2014-08-15 22:30:24 -07:00
|
|
|
|
@LAUNCH_JOBKEY_RUNATLOAD : @YES,
|
|
|
|
|
@LAUNCH_JOBKEY_PROGRAMARGUMENTS :
|
|
|
|
|
@[ @"/bin/sh", @"-c", shell_script_ns, ],
|
|
|
|
|
};
|
|
|
|
|
CFDictionaryRef job_dictionary_cf =
|
|
|
|
|
base::mac::NSToCFCast(job_dictionary_ns);
|
|
|
|
|
|
|
|
|
|
// The job may be left over from a failed previous run.
|
|
|
|
|
if (ServiceManagementIsJobLoaded(kJobLabel)) {
|
|
|
|
|
EXPECT_TRUE(ServiceManagementRemoveJob(kJobLabel, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT_FALSE(ServiceManagementIsJobLoaded(kJobLabel));
|
|
|
|
|
ASSERT_FALSE(ServiceManagementIsJobRunning(kJobLabel));
|
|
|
|
|
|
|
|
|
|
// Submit the job.
|
|
|
|
|
ASSERT_TRUE(ServiceManagementSubmitJob(job_dictionary_cf));
|
|
|
|
|
EXPECT_TRUE(ServiceManagementIsJobLoaded(kJobLabel));
|
|
|
|
|
|
|
|
|
|
// launchd started the job because RunAtLoad is true.
|
|
|
|
|
pid_t job_pid = ServiceManagementIsJobRunning(kJobLabel);
|
|
|
|
|
ASSERT_GT(job_pid, 0);
|
|
|
|
|
|
|
|
|
|
ExpectProcessIsRunning(job_pid, shell_script);
|
|
|
|
|
|
|
|
|
|
// Remove the job.
|
|
|
|
|
ASSERT_TRUE(ServiceManagementRemoveJob(kJobLabel, true));
|
|
|
|
|
EXPECT_FALSE(ServiceManagementIsJobLoaded(kJobLabel));
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
|
EXPECT_EQ(ServiceManagementIsJobRunning(kJobLabel), 0);
|
2014-08-15 22:30:24 -07:00
|
|
|
|
|
|
|
|
|
// Now that the job is unloaded, a subsequent attempt to unload it should be
|
2014-09-22 13:11:40 -04:00
|
|
|
|
// an error.
|
|
|
|
|
EXPECT_FALSE(ServiceManagementRemoveJob(kJobLabel, false));
|
2014-08-15 22:30:24 -07:00
|
|
|
|
|
|
|
|
|
ExpectProcessIsNotRunning(job_pid, shell_script);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
2014-10-07 17:28:50 -04:00
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace crashpad
|