toolkit/crashreporter/google-breakpad/src/client/mac/Framework/OnDemandServer.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial