1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! Whether a launchd unit owns the socket an on-demand spawn is about to bind
//! (#6619).
//!
//! Why: a daemon's client bridges spawn it on demand when nothing answers its
//! socket. That contract is correct on a dev machine and wrong on a supervised
//! one: during a `launchctl bootout`/`bootstrap` window the socket is
//! transiently unserved, a bridge read that as "nothing is running", and spawned
//! an unsupervised daemon onto the PRODUCTION socket path — without the plist's
//! `EnvironmentVariables`. launchd's own instance then found the path already
//! held and exited 0 ("another instance is already running"), so launchd
//! reported success while a misconfigured orphan owned the socket.
//!
//! The single-flight `flock` those bridges share (#5267/#6286) cannot see this:
//! it coordinates bridges with each other, not with launchd.
//!
//! 🔴 **`launchctl print` alone cannot answer this question.** In the exact
//! window that loses the race the unit is BOOTED OUT, so launchd reports nothing
//! and a check that trusted it would permit the spawn it exists to stop. The
//! installed plist is what persists across the window, so its presence counts as
//! registration — see [`socket_owner`](crate::launchd_claim::socket_owner).
//!
//! What: [`socket_owner`](crate::launchd_claim::socket_owner) is the pure
//! decision over the two registration signals;
//! [`launchd_socket_owner`](crate::launchd_claim::launchd_socket_owner) reads
//! them off the real launchd. A caller
//! that is not on its canonical production path (a test socket, a
//! `TRUSTY_DATA_DIR_OVERRIDE` sandbox) passes `is_supervised_path: false` and is
//! never affected.
//!
//! Test: `cargo test -p trusty-common --features unconditional-only
//! launchd_claim`.
/// Who owns the socket an on-demand spawn is about to bind.
///
/// Why: the caller's two options are opposite — wait for launchd, or spawn —
/// and the waiting branch has to name the unit it is waiting for, so the label
/// travels with the verdict rather than being re-derived at the message site.
/// What: [`Launchd`](Self::Launchd) carries the owning label;
/// [`OnDemand`](Self::OnDemand) is the unsupervised case the bridge contract was
/// written for.
/// Test: `socket_owner_defers_to_a_loaded_unit`,
/// `socket_owner_defers_while_the_unit_is_booted_out`,
/// `socket_owner_leaves_an_unmanaged_host_alone`,
/// `socket_owner_ignores_a_socket_launchd_does_not_serve`.
/// Decide whether launchd owns this socket, from the two registration signals.
///
/// Why: kept pure and separate from the `launchctl` read, because the decision
/// is what governs whether a daemon gets spawned onto a production socket — the
/// thing that went wrong — and it must be testable without a registered unit.
///
/// What: launchd owns the path when the caller is on its canonical production
/// socket AND either signal says a unit exists.
///
/// - `is_supervised_path` — the caller compares the socket it is about to serve
/// against its own canonical production path. A socket under a `TempDir` or a
/// `TRUSTY_DATA_DIR_OVERRIDE` sandbox is never launchd's, whatever is
/// registered, so tests and dev sandboxes keep the old behaviour.
/// - `unit_loaded` — `launchctl print gui/<uid>/<label>` answered.
/// - `plist_present` — `~/Library/LaunchAgents/<label>.plist` exists.
///
/// 🔴 `plist_present` is not redundant with `unit_loaded`; it is the whole fix.
/// The #6619 window is precisely the one where the unit is booted out and
/// `unit_loaded` is false, and the plist file is what survives it. A host that
/// has genuinely uninstalled the service has no plist, so it still reads
/// [`SocketOwner::OnDemand`].
///
/// Test: `socket_owner_defers_to_a_loaded_unit`,
/// `socket_owner_defers_while_the_unit_is_booted_out`,
/// `socket_owner_leaves_an_unmanaged_host_alone`,
/// `socket_owner_ignores_a_socket_launchd_does_not_serve`.
/// [`socket_owner`] over the launchd registration this host actually has.
///
/// Why/What: binds the two signals to `launchctl print` and the installed plist.
/// On every non-macOS platform launchd does not exist, which is a positive
/// negative rather than an unanswered question, so the answer is
/// [`SocketOwner::OnDemand`] and nothing changes for those callers.
///
/// Test: the decision is covered by `socket_owner_*`; the `launchctl` and
/// filesystem reads are side-effecting and are exercised by trusty-memory's
/// stdio bridge.