]> git.saurik.com Git - apple/securityd.git/blob - src/child.cpp
securityd-36489.tar.gz
[apple/securityd.git] / src / child.cpp
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // child - track a single child process and its belongings
27 //
28 #include "child.h"
29 #include <security_utilities/debugging.h>
30
31
32 //
33 // We use a static Mutex to coordinate checkin
34 //
35 Mutex ServerChild::mCheckinLock;
36
37
38 //
39 // Make and break ServerChildren
40 //
41 ServerChild::ServerChild()
42 : mCheckinCond(mCheckinLock)
43 {
44 }
45
46
47 //
48 // If the ServerChild is destroyed, kill its process, nice or hard.
49 //
50 // In case you wonder about the tango below, it's making sure we
51 // get to "It's dead, Jim" with the minimum number of checkChildren()
52 // calls while still working correctly if this is the only thread alive.
53 //
54 //@@@ We *could* define a "soft shutdown" MIG message to send to all
55 //@@@ ServerChildren in this situation.
56 //
57 ServerChild::~ServerChild()
58 {
59 mServicePort.destroy();
60
61 if (state() == alive) {
62 this->kill(SIGTERM); // shoot it once
63 checkChildren(); // check for quick death
64 if (state() == alive) {
65 usleep(300000); // give it some grace
66 if (state() == alive) { // could have been reaped by another thread
67 checkChildren(); // check again
68 if (state() == alive) { // it... just... won't... die...
69 this->kill(SIGKILL); // take THAT!
70 checkChildren();
71 if (state() == alive) // stuck zombie
72 abandon(); // leave the body behind
73 }
74 }
75 }
76 }
77 }
78
79
80 //
81 // Parent action during fork: wait until ready or dead, then return
82 //
83 void ServerChild::parentAction()
84 {
85 // wait for either checkin or (premature) death
86 secdebug("serverchild", "%p (pid %d) waiting for checkin", this, pid());
87 StLock<Mutex> _(mCheckinLock);
88 while (!ready() && state() == alive)
89 mCheckinCond.wait();
90
91 // so what happened?
92 if (state() == dead) {
93 // our child died
94 secdebug("serverchild", "%p (pid %d) died before checking in", this, pid());
95 } else if (ready()) {
96 // child has checked in and is ready for service
97 secdebug("serverchild", "%p (pid %d) ready for service on port %d",
98 this, pid(), mServicePort.port());
99 } else
100 assert(false); // how did we ever get here?!
101 }
102
103
104 //
105 // Death action during fork: release the waiting creator thread, if any
106 //
107 void ServerChild::dying()
108 {
109 secdebug("serverchild", "%p is dead; resuming parent thread (if any)", this);
110 mCheckinCond.signal();
111 }
112
113
114 void ServerChild::checkIn(Port servicePort, pid_t pid)
115 {
116 if (ServerChild *child = Child::find<ServerChild>(pid)) {
117 // Child was alive when last seen. Store service port and signal parent thread
118 {
119 StLock<Mutex> _(mCheckinLock);
120 child->mServicePort = servicePort;
121 servicePort.modRefs(MACH_PORT_RIGHT_SEND, +1); // retain send right
122 secdebug("serverchild", "%p (pid %d) checking in; resuming parent thread",
123 child, pid);
124 }
125 child->mCheckinCond.signal();
126 } else {
127 // Child has died; is wrong kind; or spurious checkin.
128 // If it was a proper child, death notifications will wake up the parent thread
129 secdebug("serverchild", "pid %d not in child set; checkin ignored", pid);
130 }
131 }