launchd.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/mac/launchd.h"
  5. #include "base/logging.h"
  6. #include "base/mac/scoped_launch_data.h"
  7. #include "base/numerics/safe_conversions.h"
  8. // This file is written in terms of launch_data_t, which is deprecated but has
  9. // no replacement. Ignore the deprecation warnings for now.
  10. #pragma clang diagnostic push
  11. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  12. namespace base::mac {
  13. // MessageForJob sends a single message to launchd with a simple dictionary
  14. // mapping |operation| to |job_label|, and returns the result of calling
  15. // launch_msg to send that message. On failure, returns NULL. The caller
  16. // assumes ownership of the returned launch_data_t object.
  17. launch_data_t MessageForJob(const std::string& job_label,
  18. const char* operation) {
  19. // launch_data_alloc returns something that needs to be freed.
  20. ScopedLaunchData message(launch_data_alloc(LAUNCH_DATA_DICTIONARY));
  21. if (!message.is_valid()) {
  22. LOG(ERROR) << "launch_data_alloc";
  23. return NULL;
  24. }
  25. // launch_data_new_string returns something that needs to be freed, but
  26. // the dictionary will assume ownership when launch_data_dict_insert is
  27. // called, so put it in a scoper and .release() it when given to the
  28. // dictionary.
  29. ScopedLaunchData job_label_launchd(launch_data_new_string(job_label.c_str()));
  30. if (!job_label_launchd.is_valid()) {
  31. LOG(ERROR) << "launch_data_new_string";
  32. return NULL;
  33. }
  34. if (!launch_data_dict_insert(message.get(), job_label_launchd.release(),
  35. operation)) {
  36. return NULL;
  37. }
  38. return launch_msg(message.get());
  39. }
  40. pid_t PIDForJob(const std::string& job_label) {
  41. ScopedLaunchData response(MessageForJob(job_label, LAUNCH_KEY_GETJOB));
  42. if (!response.is_valid()) {
  43. return -1;
  44. }
  45. launch_data_type_t response_type = launch_data_get_type(response.get());
  46. if (response_type != LAUNCH_DATA_DICTIONARY) {
  47. if (response_type == LAUNCH_DATA_ERRNO) {
  48. LOG(ERROR) << "PIDForJob: error "
  49. << launch_data_get_errno(response.get());
  50. } else {
  51. LOG(ERROR) << "PIDForJob: expected dictionary, got " << response_type;
  52. }
  53. return -1;
  54. }
  55. launch_data_t pid_data =
  56. launch_data_dict_lookup(response.get(), LAUNCH_JOBKEY_PID);
  57. if (!pid_data)
  58. return 0;
  59. if (launch_data_get_type(pid_data) != LAUNCH_DATA_INTEGER) {
  60. LOG(ERROR) << "PIDForJob: expected integer";
  61. return -1;
  62. }
  63. return checked_cast<pid_t>(launch_data_get_integer(pid_data));
  64. }
  65. } // namespace base::mac
  66. #pragma clang diagnostic pop