PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/client/mac/Framework/OnDemandServer.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 146 lines | 32 code | 12 blank | 102 comment | 0 complexity | f5332d14d809509806acff15ef1fa76a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Copyright (c) 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #import <iostream>
  30. #import <mach/mach.h>
  31. #import <servers/bootstrap.h>
  32. #import <stdio.h>
  33. #import <stdlib.h>
  34. #import <sys/stat.h>
  35. #import <unistd.h>
  36. //==============================================================================
  37. // class OnDemandServer :
  38. // A basic on-demand server launcher supporting a single named service port
  39. //
  40. // Example Usage :
  41. //
  42. // kern_return_t result;
  43. // OnDemandServer *server = OnDemandServer::Create("/tmp/myserver",
  44. // "com.MyCompany.MyServiceName",
  45. // true,
  46. // &result);
  47. //
  48. // if (server) {
  49. // server->LaunchOnDemand();
  50. // mach_port_t service_port = GetServicePort();
  51. //
  52. // // Send a mach message to service_port and "myserver" will be launched
  53. // }
  54. //
  55. //
  56. // ---- Now in the server code ----
  57. //
  58. // // "myserver" should get the service port and read the message which
  59. // // launched it:
  60. // mach_port_t service_rcv_port_;
  61. // kern_return_t kr = bootstrap_check_in(bootstrap_port,
  62. // "com.MyCompany.MyServiceName",
  63. // &service_rcv_port_);
  64. // // mach_msg() read service_rcv_port_ ....
  65. //
  66. // ....
  67. //
  68. // // Later "myserver" may want to unregister the service if it doesn't
  69. // // want its bootstrap service to stick around after it exits.
  70. //
  71. // // DO NOT use mach_port_deallocate() here -- it will fail and the
  72. // // following bootstrap_register() will also fail leaving our service
  73. // // name hanging around forever (until reboot)
  74. // kern_return_t kr = mach_port_destroy(mach_task_self(), service_rcv_port_);
  75. //
  76. // kr = bootstrap_register(bootstrap_port,
  77. // "com.MyCompany.MyServiceName",
  78. // MACH_PORT_NULL);
  79. class OnDemandServer {
  80. public:
  81. // must call Initialize() to be useful
  82. OnDemandServer()
  83. : server_port_(MACH_PORT_NULL),
  84. service_port_(MACH_PORT_NULL),
  85. unregister_on_cleanup_(true) {
  86. }
  87. // Creates the bootstrap server and service
  88. kern_return_t Initialize(const char *server_command,
  89. const char *service_name,
  90. bool unregister_on_cleanup);
  91. // Returns an OnDemandServer object if successful, or NULL if there's
  92. // an error. The error result will be returned in out_result.
  93. //
  94. // server_command : the full path name including optional command-line
  95. // arguments to the executable representing the server
  96. //
  97. // service_name : represents service name
  98. // something like "com.company.ServiceName"
  99. //
  100. // unregister_on_cleanup : if true, unregisters the service name
  101. // when the OnDemandServer is deleted -- unregistering will
  102. // ONLY be possible if LaunchOnDemand() has NOT been called.
  103. // If false, then the service will continue to be registered
  104. // even after the current process quits.
  105. //
  106. // out_result : if non-NULL, returns the result
  107. // this value will be KERN_SUCCESS if Create() returns non-NULL
  108. //
  109. static OnDemandServer *Create(const char *server_command,
  110. const char *service_name,
  111. bool unregister_on_cleanup,
  112. kern_return_t *out_result);
  113. // Cleans up and if LaunchOnDemand() has not yet been called then
  114. // the bootstrap service will be unregistered.
  115. ~OnDemandServer();
  116. // This must be called if we intend to commit to launching the server
  117. // by sending a mach message to our service port. Do not call it otherwise
  118. // or it will be difficult (impossible?) to unregister the service name.
  119. void LaunchOnDemand();
  120. // This is the port we need to send a mach message to after calling
  121. // LaunchOnDemand(). Sending a message causing an immediate launch
  122. // of the server
  123. mach_port_t GetServicePort() { return service_port_; };
  124. private:
  125. // Disallow copy constructor
  126. OnDemandServer(const OnDemandServer&);
  127. // Cleans up and if LaunchOnDemand() has not yet been called then
  128. // the bootstrap service will be unregistered.
  129. void Unregister();
  130. name_t service_name_;
  131. mach_port_t server_port_;
  132. mach_port_t service_port_;
  133. bool unregister_on_cleanup_;
  134. };