shiguredo_container 2026.1.0-canary.8

Runtime-agnostic container library for Rust on macOS and Linux
Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! XPC 接続とメッセージ送受信の低レベルラッパ。
//!
//! `xpc_bridge.c` の FFI を包み、`XpcConn::send` で XPC サービスにメッセージを送る。

use std::collections::HashMap;
use std::ffi::{CStr, CString, c_char, c_void};
use std::time::Duration;

use nojson::DisplayJson;

use crate::core::error::{ClientError, Result};

#[expect(non_camel_case_types)]
type xpc_object_t = *mut c_void;
#[expect(non_camel_case_types)]
type xpc_connection_t = *mut c_void;

unsafe extern "C" {
    fn xpc_bridge_create_connection(name: *const c_char) -> xpc_connection_t;
    fn xpc_bridge_connection_cancel(conn: xpc_connection_t);
    fn xpc_bridge_create_dictionary() -> xpc_object_t;
    fn xpc_bridge_dictionary_set_string(d: xpc_object_t, k: *const c_char, v: *const c_char);
    fn xpc_bridge_dictionary_set_data(
        d: xpc_object_t,
        k: *const c_char,
        v: *const c_void,
        l: usize,
    );
    fn xpc_bridge_dictionary_set_int64(d: xpc_object_t, k: *const c_char, v: i64);
    fn xpc_bridge_dictionary_set_uint64(d: xpc_object_t, k: *const c_char, v: u64);
    fn xpc_bridge_dictionary_set_bool(d: xpc_object_t, k: *const c_char, v: bool);
    // true = 設定成功。xpc_fd_create 失敗時は false。
    fn xpc_bridge_dictionary_set_fd(d: xpc_object_t, k: *const c_char, v: i32) -> bool;
    fn xpc_bridge_release(obj: xpc_object_t);
    fn xpc_bridge_send_message_with_reply_sync(
        c: xpc_connection_t,
        m: xpc_object_t,
        timeout_ns: u64,
    ) -> xpc_object_t;
    fn xpc_bridge_copy_data(d: xpc_object_t, k: *const c_char, l: *mut usize) -> *const c_void;
    fn xpc_bridge_get_string(d: xpc_object_t, k: *const c_char) -> *const c_char;
    // キー存在かつ XPC_TYPE_INT64 のときだけ *out に書き出して true。
    fn xpc_bridge_get_int64_checked(d: xpc_object_t, k: *const c_char, out: *mut i64) -> bool;
    fn xpc_bridge_is_error(obj: xpc_object_t) -> bool;
    fn xpc_bridge_get_error_description(obj: xpc_object_t, b: *mut c_char, bl: usize) -> bool;
    fn xpc_bridge_get_log_fds(reply: xpc_object_t, fds: *mut i32, max_fds: usize) -> i32;
}

/// XPC のルートキー・エラーキー・サービス名。
const ROUTE_KEY: &CStr = c"com.apple.container.xpc.route";
const ERROR_KEY: &CStr = c"com.apple.container.xpc.error";
pub(crate) const SERVICE_NAME: &CStr = c"com.apple.container.apiserver";
pub(crate) const IMAGE_SERVICE: &CStr = c"com.apple.container.core.container-core-images";

/// 通常の XPC 呼び出しのタイムアウト。
pub(crate) const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
/// `containerWait` など、プロセス終了を待つ呼び出しのタイムアウト。
pub(crate) const LONG_TIMEOUT: Duration = Duration::from_secs(86400);

/// XPC の応答を包むラッパ。Drop で `xpc_bridge_release` する。
pub(crate) struct RawReply {
    obj: xpc_object_t,
}

// `xpc_object_t` は `*mut c_void` だが、XPC オブジェクトはスレッドセーフに解放できる。
unsafe impl Send for RawReply {}

impl RawReply {
    /// 指定キーのデータを `Vec<u8>` で取り出す。
    pub(crate) fn data(&self, key: &CStr) -> Option<Vec<u8>> {
        unsafe {
            let mut len = 0;
            let p = xpc_bridge_copy_data(self.obj, key.as_ptr(), &mut len);
            if p.is_null() || len == 0 {
                return None;
            }
            let v = std::slice::from_raw_parts(p as *const u8, len).to_vec();
            libc::free(p as *mut c_void);
            Some(v)
        }
    }

    /// 指定キーの文字列を `String` で取り出す。
    pub(crate) fn string(&self, key: &CStr) -> Option<String> {
        unsafe {
            let p = xpc_bridge_get_string(self.obj, key.as_ptr());
            if p.is_null() {
                return None;
            }
            CStr::from_ptr(p).to_str().ok().map(|s| s.to_string())
        }
    }

    /// 指定キーの `int64` を取り出す。キー欠落・型不一致はエラー。
    pub(crate) fn try_int64(&self, key: &CStr) -> Result<i64> {
        let mut out = 0i64;
        let ok = unsafe { xpc_bridge_get_int64_checked(self.obj, key.as_ptr(), &mut out) };
        if ok {
            Ok(out)
        } else {
            Err(ClientError::Xpc(format!(
                "missing or non-int64 key: {}",
                key.to_string_lossy()
            ))
            .into())
        }
    }

    /// `"logs"` 配列から FD を複製して取り出す。
    pub(crate) fn log_fds(&self) -> Vec<std::os::fd::RawFd> {
        let mut fds = [0i32; 2];
        let n = unsafe { xpc_bridge_get_log_fds(self.obj, fds.as_mut_ptr(), fds.len()) };
        if n <= 0 {
            return Vec::new();
        }
        fds[..n as usize].to_vec()
    }

    /// 応答が XPC エラーオブジェクトかどうか。
    pub(crate) fn is_error(&self) -> bool {
        unsafe { xpc_bridge_is_error(self.obj) }
    }

    /// エラー説明を取り出す。
    pub(crate) fn error_desc(&self) -> Option<String> {
        let mut buf = vec![0u8; 1024];
        if unsafe {
            xpc_bridge_get_error_description(self.obj, buf.as_mut_ptr() as *mut c_char, buf.len())
        } {
            let end = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
            Some(String::from_utf8_lossy(&buf[..end]).into_owned())
        } else {
            None
        }
    }

    /// `com.apple.container.xpc.error` キーのデータを JSON としてパースし、
    /// `code` / `message` を取り出して `Error::Other` を作る。
    pub(crate) fn json_error(&self) -> Option<ClientError> {
        let ek = ERROR_KEY;
        self.data(ek).map(|d| {
            let text = std::str::from_utf8(&d).unwrap_or("");
            if let Ok(j) = nojson::RawJson::parse(text) {
                let code = j
                    .value()
                    .to_member("code")
                    .and_then(|m| m.required())
                    .and_then(|v| {
                        let s: String = v.try_into()?;
                        Ok(s)
                    })
                    .unwrap_or_default();
                let msg = j
                    .value()
                    .to_member("message")
                    .and_then(|m| m.required())
                    .and_then(|v| {
                        let s: String = v.try_into()?;
                        Ok(s)
                    })
                    .unwrap_or_default();
                ClientError::Xpc(format!("XPC error {code}: {msg}"))
            } else {
                ClientError::Xpc("XPC error unparseable".into())
            }
        })
    }
}

impl Drop for RawReply {
    fn drop(&mut self) {
        unsafe {
            xpc_bridge_release(self.obj);
        }
    }
}

/// XPC 接続。Drop で `xpc_connection_cancel` する。
pub(crate) struct XpcConn {
    conn: xpc_connection_t,
}

// `xpc_connection_t` は `*mut c_void` だが、XPC 接続はスレッドセーフに cancel できる。
unsafe impl Send for XpcConn {}

impl XpcConn {
    /// 指定サービスに接続する。
    pub(crate) fn connect(service: &CStr) -> Result<XpcConn> {
        let conn = unsafe { xpc_bridge_create_connection(service.as_ptr()) };
        if conn.is_null() {
            return Err(ClientError::XpcConnect.into());
        }
        Ok(XpcConn { conn })
    }

    /// メッセージを送り、同期で応答を受け取る。
    ///
    /// `route` は XPC のルート名(例: `"containerCreate"`)。`entries` はキーと値のリスト。
    /// タイムアウトは `DEFAULT_TIMEOUT` (60 秒)。
    pub(crate) fn send(&self, route: &str, entries: &[(CString, KeyValue)]) -> Result<RawReply> {
        self.send_with_timeout(route, entries, DEFAULT_TIMEOUT)
    }

    /// メッセージを送り、同期で応答を受け取る。タイムアウトを指定できる版。
    pub(crate) fn send_with_timeout(
        &self,
        route: &str,
        entries: &[(CString, KeyValue)],
        timeout: Duration,
    ) -> Result<RawReply> {
        // 失敗しうる CString 変換は XPC 辞書を作る前に全部済ませる。
        // 辞書作成後に `?` で early return すると辞書 (と dup 済み fd) がリークするため。
        let rv = CString::new(route).map_err(|_| ClientError::Xpc("route contains NUL".into()))?;
        let mut cstrings = Vec::new();
        for (_, val) in entries {
            if let KeyValue::String(s) = val {
                cstrings.push(
                    CString::new(s.as_str())
                        .map_err(|_| ClientError::Xpc("string value contains NUL".into()))?,
                );
            }
        }
        let mut cstrings = cstrings.into_iter();

        let msg = unsafe { xpc_bridge_create_dictionary() };
        let rk = ROUTE_KEY;
        unsafe {
            xpc_bridge_dictionary_set_string(msg, rk.as_ptr(), rv.as_ptr());
        }
        for (key, val) in entries {
            match val {
                KeyValue::String(_) => {
                    // cstrings は KeyValue::String と同順で積んであるため必ず存在する。
                    let c = cstrings.next().expect("cstring must exist");
                    unsafe {
                        xpc_bridge_dictionary_set_string(msg, key.as_ptr(), c.as_ptr());
                    }
                }
                KeyValue::Data(d) => unsafe {
                    xpc_bridge_dictionary_set_data(
                        msg,
                        key.as_ptr(),
                        d.as_ptr() as *const c_void,
                        d.len(),
                    );
                },
                KeyValue::Bool(b) => unsafe {
                    xpc_bridge_dictionary_set_bool(msg, key.as_ptr(), *b);
                },
                KeyValue::Int64(v) => unsafe {
                    xpc_bridge_dictionary_set_int64(msg, key.as_ptr(), *v);
                },
                KeyValue::UInt64(v) => unsafe {
                    xpc_bridge_dictionary_set_uint64(msg, key.as_ptr(), *v);
                },
                KeyValue::Fd(v) => {
                    let ok = unsafe { xpc_bridge_dictionary_set_fd(msg, key.as_ptr(), *v) };
                    if !ok {
                        // 途中失敗時も作成済み msg を解放してから Err を返す。
                        unsafe {
                            xpc_bridge_release(msg);
                        }
                        return Err(ClientError::Xpc(
                            "failed to set file descriptor in XPC dictionary".into(),
                        )
                        .into());
                    }
                }
            }
        }
        // 巨大な Duration で切り詰めないよう飽和させる。C 側でも INT64_MAX にクランプする。
        let timeout_ns = u64::try_from(timeout.as_nanos()).unwrap_or(u64::MAX);
        let raw = unsafe { xpc_bridge_send_message_with_reply_sync(self.conn, msg, timeout_ns) };
        unsafe {
            xpc_bridge_release(msg);
        }
        if raw.is_null() {
            return Err(ClientError::XpcTimeout.into());
        }
        let reply = RawReply { obj: raw };
        if reply.is_error() {
            return Err(ClientError::Xpc(reply.error_desc().unwrap_or_default()).into());
        }
        if let Some(e) = reply.json_error() {
            return Err(e.into());
        }
        Ok(reply)
    }
}

impl Drop for XpcConn {
    fn drop(&mut self) {
        unsafe {
            xpc_bridge_connection_cancel(self.conn);
            // `xpc_connection_create_mach_service` は +1 参照を返すため、
            // cancel だけでは接続オブジェクトが解放されずリークする。
            xpc_bridge_release(self.conn);
        }
    }
}

/// XPC メッセージの値。
pub(crate) enum KeyValue {
    String(String),
    Data(Vec<u8>),
    Bool(bool),
    Int64(i64),
    UInt64(u64),
    Fd(std::os::fd::RawFd),
}

/// キー名から `CString` を作る。
pub(crate) fn k(name: &'static str) -> CString {
    CString::new(name).expect("key must not contain NUL")
}

/// `id` キーの `CString`。頻繁に使うので専用ヘルパ。
pub(crate) fn id_key() -> CString {
    k("id")
}

/// 文字列から `KeyValue::String` を作る。
pub(crate) fn s(v: &str) -> KeyValue {
    KeyValue::String(v.to_string())
}

/// `DisplayJson` を JSON バイト列にする。
pub(crate) fn j(v: &impl DisplayJson) -> Vec<u8> {
    nojson::Json(v).to_string().into_bytes()
}

/// `nojson::RawJsonValue` からメンバーを文字列で取り出す(オプション)。
pub(crate) fn member_opt_string(item: &nojson::RawJsonValue, key: &str) -> Option<String> {
    item.to_member(key)
        .ok()
        .and_then(|m| m.required().ok())
        .and_then(|v| TryInto::<String>::try_into(v).ok())
}

/// フィルタ用の JSON を構築するヘルパ(`containerList` / `volumeList` / `networkList` 用)。
pub(crate) struct Filters {
    pub(crate) ids: Vec<String>,
    pub(crate) labels: HashMap<String, String>,
}

impl DisplayJson for Filters {
    fn fmt(&self, f: &mut nojson::JsonFormatter) -> std::fmt::Result {
        f.object(|f| {
            f.member("ids", &self.ids)?;
            f.member("labels", &self.labels)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// テスト用に空の XPC 辞書を RawReply で包む。Drop で release される。
    fn empty_reply() -> RawReply {
        let obj = unsafe { xpc_bridge_create_dictionary() };
        assert!(!obj.is_null(), "辞書の作成に失敗しないこと");
        RawReply { obj }
    }

    #[test]
    fn try_int64_returns_value_when_key_is_int64() {
        // XPC_TYPE_INT64 のキーがあるとき値を返すこと。
        let reply = empty_reply();
        let key = CString::new("exitCode").expect("キーの作成に失敗しないこと");
        unsafe {
            xpc_bridge_dictionary_set_int64(reply.obj, key.as_ptr(), 42);
        }
        let v = reply.try_int64(&key).expect("int64 を取得できること");
        assert_eq!(v, 42, "設定した int64 が返ること");
    }

    #[test]
    fn try_int64_errors_when_key_missing() {
        // キー欠落はエラーになること (旧 int64 は 0 を返していた)。
        let reply = empty_reply();
        let key = CString::new("exitCode").expect("キーの作成に失敗しないこと");
        let err = reply
            .try_int64(&key)
            .expect_err("キー欠落はエラーであること");
        let msg = err.to_string();
        assert!(
            msg.contains("missing or non-int64"),
            "欠落を示すメッセージであること: {msg}"
        );
    }

    #[test]
    fn try_int64_errors_when_key_is_string() {
        // 型が string のときはエラーになること。
        let reply = empty_reply();
        let key = CString::new("exitCode").expect("キーの作成に失敗しないこと");
        let val = CString::new("整数でない値").expect("値の作成に失敗しないこと");
        unsafe {
            xpc_bridge_dictionary_set_string(reply.obj, key.as_ptr(), val.as_ptr());
        }
        let err = reply
            .try_int64(&key)
            .expect_err("型不一致はエラーであること");
        let msg = err.to_string();
        assert!(
            msg.contains("missing or non-int64"),
            "型不一致を示すメッセージであること: {msg}"
        );
    }
}