summaryrefslogtreecommitdiffstats
path: root/base/mac
diff options
context:
space:
mode:
authorlambroslambrou@chromium.org <lambroslambrou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-29 16:57:53 +0000
committerlambroslambrou@chromium.org <lambroslambrou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-29 16:57:53 +0000
commitfd1c931540c431023b09927b717ba848daefc369 (patch)
tree4030d49336ea3d3074a231f2645888c301182138 /base/mac
parentc849fab653f06cbb9246eeafa5b7d1f5281cc251 (diff)
downloadchromium_src-fd1c931540c431023b09927b717ba848daefc369.zip
chromium_src-fd1c931540c431023b09927b717ba848daefc369.tar.gz
chromium_src-fd1c931540c431023b09927b717ba848daefc369.tar.bz2
Move launchd code from chrome/browser/mac to base/mac.
This is to allow the code to be used by the Chromoting host plugin. Also expand the interface of PIDForJob() to report if a job is loaded but not running. BUG=None TEST=Compiles, unit-tests pass Review URL: https://chromiumcodereview.appspot.com/9837098 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@129636 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/mac')
-rw-r--r--base/mac/launchd.cc75
-rw-r--r--base/mac/launchd.h31
-rw-r--r--base/mac/scoped_launch_data.h76
3 files changed, 182 insertions, 0 deletions
diff --git a/base/mac/launchd.cc b/base/mac/launchd.cc
new file mode 100644
index 0000000..1d384c9
--- /dev/null
+++ b/base/mac/launchd.cc
@@ -0,0 +1,75 @@
+// 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.
+
+#include "base/mac/launchd.h"
+
+#include "base/logging.h"
+#include "base/mac/scoped_launch_data.h"
+
+namespace base {
+namespace mac {
+
+// MessageForJob sends a single message to launchd with a simple dictionary
+// mapping |operation| to |job_label|, and returns the result of calling
+// launch_msg to send that message. On failure, returns NULL. The caller
+// assumes ownership of the returned launch_data_t object.
+launch_data_t MessageForJob(const std::string& job_label,
+ const char* operation) {
+ // launch_data_alloc returns something that needs to be freed.
+ ScopedLaunchData message(launch_data_alloc(LAUNCH_DATA_DICTIONARY));
+ if (!message) {
+ LOG(ERROR) << "launch_data_alloc";
+ return NULL;
+ }
+
+ // launch_data_new_string returns something that needs to be freed, but
+ // the dictionary will assume ownership when launch_data_dict_insert is
+ // called, so put it in a scoper and .release() it when given to the
+ // dictionary.
+ ScopedLaunchData job_label_launchd(launch_data_new_string(job_label.c_str()));
+ if (!job_label_launchd) {
+ LOG(ERROR) << "launch_data_new_string";
+ return NULL;
+ }
+
+ if (!launch_data_dict_insert(message,
+ job_label_launchd.release(),
+ operation)) {
+ return NULL;
+ }
+
+ return launch_msg(message);
+}
+
+pid_t PIDForJob(const std::string& job_label) {
+ ScopedLaunchData response(MessageForJob(job_label, LAUNCH_KEY_GETJOB));
+ if (!response) {
+ return -1;
+ }
+
+ launch_data_type_t response_type = launch_data_get_type(response);
+ if (response_type != LAUNCH_DATA_DICTIONARY) {
+ if (response_type == LAUNCH_DATA_ERRNO) {
+ LOG(ERROR) << "PIDForJob: error " << launch_data_get_errno(response);
+ } else {
+ LOG(ERROR) << "PIDForJob: expected dictionary, got " << response_type;
+ }
+ return -1;
+ }
+
+ launch_data_t pid_data = launch_data_dict_lookup(response,
+ LAUNCH_JOBKEY_PID);
+ if (!pid_data)
+ return 0;
+
+ if (launch_data_get_type(pid_data) != LAUNCH_DATA_INTEGER) {
+ LOG(ERROR) << "PIDForJob: expected integer";
+ return -1;
+ }
+
+ return launch_data_get_integer(pid_data);
+}
+
+} // namespace mac
+} // namespace base
diff --git a/base/mac/launchd.h b/base/mac/launchd.h
new file mode 100644
index 0000000..b693687
--- /dev/null
+++ b/base/mac/launchd.h
@@ -0,0 +1,31 @@
+// 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.
+
+#ifndef BASE_MAC_LAUNCHD_H_
+#define BASE_MAC_LAUNCHD_H_
+#pragma once
+
+#include <launch.h>
+#include <sys/types.h>
+
+#include <string>
+
+namespace base {
+namespace mac {
+
+// MessageForJob sends a single message to launchd with a simple dictionary
+// mapping |operation| to |job_label|, and returns the result of calling
+// launch_msg to send that message. On failure, returns NULL. The caller
+// assumes ownership of the returned launch_data_t object.
+launch_data_t MessageForJob(const std::string& job_label,
+ const char* operation);
+
+// Returns the process ID for |job_label| if the job is running, 0 if the job
+// is loaded but not running, or -1 on error.
+pid_t PIDForJob(const std::string& job_label);
+
+} // namespace mac
+} // namespace base
+
+#endif // BASE_MAC_LAUNCHD_H_
diff --git a/base/mac/scoped_launch_data.h b/base/mac/scoped_launch_data.h
new file mode 100644
index 0000000..955c3e7
--- /dev/null
+++ b/base/mac/scoped_launch_data.h
@@ -0,0 +1,76 @@
+// 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.
+
+#ifndef BASE_MAC_SCOPED_LAUNCH_DATA_H_
+#define BASE_MAC_SCOPED_LAUNCH_DATA_H_
+#pragma once
+
+#include <launch.h>
+
+#include <algorithm>
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+
+namespace base {
+namespace mac {
+
+// Just like scoped_ptr<> but for launch_data_t.
+class ScopedLaunchData {
+ public:
+ typedef launch_data_t element_type;
+
+ explicit ScopedLaunchData(launch_data_t object = NULL)
+ : object_(object) {
+ }
+
+ ~ScopedLaunchData() {
+ if (object_)
+ launch_data_free(object_);
+ }
+
+ void reset(launch_data_t object = NULL) {
+ if (object != object_) {
+ if (object_)
+ launch_data_free(object_);
+ object_ = object;
+ }
+ }
+
+ bool operator==(launch_data_t that) const {
+ return object_ == that;
+ }
+
+ bool operator!=(launch_data_t that) const {
+ return object_ != that;
+ }
+
+ operator launch_data_t() const {
+ return object_;
+ }
+
+ launch_data_t get() const {
+ return object_;
+ }
+
+ void swap(ScopedLaunchData& that) {
+ std::swap(object_, that.object_);
+ }
+
+ launch_data_t release() WARN_UNUSED_RESULT {
+ launch_data_t temp = object_;
+ object_ = NULL;
+ return temp;
+ }
+
+ private:
+ launch_data_t object_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedLaunchData);
+};
+
+} // namespace mac
+} // namespace base
+
+#endif // BASE_MAC_SCOPED_LAUNCH_DATA_H_