PageRenderTime 23ms CodeModel.GetById 18ms app.highlight 3ms 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
  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
 30#import <iostream>
 31#import <mach/mach.h>
 32#import <servers/bootstrap.h>
 33#import <stdio.h>
 34#import <stdlib.h>
 35#import <sys/stat.h>
 36#import <unistd.h>
 37
 38//==============================================================================
 39// class OnDemandServer :
 40//    A basic on-demand server launcher supporting a single named service port
 41//
 42// Example Usage :
 43//
 44//  kern_return_t result;
 45//  OnDemandServer *server = OnDemandServer::Create("/tmp/myserver",
 46//                                                  "com.MyCompany.MyServiceName",
 47//                                                  true,
 48//                                                  &result);
 49//
 50//  if (server) {
 51//    server->LaunchOnDemand();
 52//    mach_port_t service_port = GetServicePort();
 53//
 54//    // Send a mach message to service_port and "myserver" will be launched
 55//  }
 56//
 57//
 58//                  ---- Now in the server code ----
 59//
 60//  // "myserver" should get the service port and read the message which
 61//  // launched it:
 62//  mach_port_t service_rcv_port_;
 63//  kern_return_t kr = bootstrap_check_in(bootstrap_port,
 64//                                      "com.MyCompany.MyServiceName",
 65//                                      &service_rcv_port_);
 66//  // mach_msg() read service_rcv_port_ ....
 67//
 68//  ....
 69//
 70//  // Later "myserver" may want to unregister the service if it doesn't
 71//  // want its bootstrap service to stick around after it exits.
 72//
 73//  // DO NOT use mach_port_deallocate() here -- it will fail and the
 74//  // following bootstrap_register() will also fail leaving our service
 75//  // name hanging around forever (until reboot)
 76//  kern_return_t kr = mach_port_destroy(mach_task_self(), service_rcv_port_);
 77//
 78//  kr = bootstrap_register(bootstrap_port,
 79//                          "com.MyCompany.MyServiceName",
 80//                          MACH_PORT_NULL);
 81
 82class OnDemandServer {
 83 public:
 84  // must call Initialize() to be useful
 85  OnDemandServer()
 86    : server_port_(MACH_PORT_NULL),
 87      service_port_(MACH_PORT_NULL),
 88      unregister_on_cleanup_(true) {
 89  }
 90
 91  // Creates the bootstrap server and service
 92  kern_return_t Initialize(const char *server_command,
 93                           const char *service_name,
 94                           bool unregister_on_cleanup);
 95
 96  // Returns an OnDemandServer object if successful, or NULL if there's
 97  // an error.  The error result will be returned in out_result.
 98  //
 99  //    server_command : the full path name including optional command-line
100  //      arguments to the executable representing the server
101  //
102  //    service_name : represents service name
103  //      something like "com.company.ServiceName"
104  //
105  //    unregister_on_cleanup : if true, unregisters the service name
106  //      when the OnDemandServer is deleted -- unregistering will
107  //      ONLY be possible if LaunchOnDemand() has NOT been called.
108  //      If false, then the service will continue to be registered
109  //      even after the current process quits.
110  //
111  //    out_result : if non-NULL, returns the result
112  //      this value will be KERN_SUCCESS if Create() returns non-NULL
113  //
114  static OnDemandServer *Create(const char *server_command,
115                                const char *service_name,
116                                bool unregister_on_cleanup,
117                                kern_return_t *out_result);
118
119  // Cleans up and if LaunchOnDemand() has not yet been called then
120  // the bootstrap service will be unregistered.
121  ~OnDemandServer();
122
123  // This must be called if we intend to commit to launching the server
124  // by sending a mach message to our service port.  Do not call it otherwise
125  // or it will be difficult (impossible?) to unregister the service name.
126  void LaunchOnDemand();
127
128  // This is the port we need to send a mach message to after calling
129  // LaunchOnDemand().  Sending a message causing an immediate launch
130  // of the server
131  mach_port_t GetServicePort() { return service_port_; };
132
133 private:
134  // Disallow copy constructor
135  OnDemandServer(const OnDemandServer&);
136
137  // Cleans up and if LaunchOnDemand() has not yet been called then
138  // the bootstrap service will be unregistered.
139  void Unregister();
140
141  name_t      service_name_;
142
143  mach_port_t server_port_;
144  mach_port_t service_port_;
145  bool        unregister_on_cleanup_;
146};