fix: 5: Input/output error when install macos helper

This commit is contained in:
ezequielnick 2025-07-28 20:03:38 +08:00
parent 6f3845151d
commit 151726fcce

View File

@ -74,7 +74,9 @@ cat << EOF > "$LAUNCH_DAEMON"
<key>Label</key>
<string>party.mihomo.helper</string>
<key>AssociatedBundleIdentifiers</key>
<string>party.mihomo.app</string>
<array>
<string>party.mihomo.app</string>
</array>
<key>KeepAlive</key>
<true/>
<key>Program</key>
@ -91,18 +93,62 @@ chown root:wheel "$LAUNCH_DAEMON"
chmod 644 "$LAUNCH_DAEMON"
log "LaunchDaemon configured"
# 加载并启动服务
log "Loading and starting service..."
launchctl unload "$LAUNCH_DAEMON" 2>/dev/null || true
if ! launchctl load "$LAUNCH_DAEMON"; then
log "Error: Failed to load helper service"
# 验证关键文件
log "Verifying installation..."
if [ ! -x "$HELPER_PATH" ]; then
log "Error: Helper tool is not executable: $HELPER_PATH"
exit 1
fi
if ! launchctl start party.mihomo.helper; then
log "Error: Failed to start helper service"
# 检查二进制文件有效性
if ! file "$HELPER_PATH" | grep -q "Mach-O"; then
log "Error: Helper tool is not a valid Mach-O binary"
exit 1
fi
# 验证 plist 格式
if ! plutil -lint "$LAUNCH_DAEMON" >/dev/null 2>&1; then
log "Error: Invalid plist format"
exit 1
fi
# 获取 macOS 版本
macos_version=$(sw_vers -productVersion)
macos_major=$(echo "$macos_version" | cut -d. -f1)
log "macOS version: $macos_version"
# 清理现有服务
log "Cleaning up existing services..."
launchctl bootout system "$LAUNCH_DAEMON" 2>/dev/null || true
launchctl unload "$LAUNCH_DAEMON" 2>/dev/null || true
# 加载服务
log "Loading service..."
if [ "$macos_major" -ge 11 ]; then
# macOS Big Sur 及更新版本使用 bootstrap
if ! launchctl bootstrap system "$LAUNCH_DAEMON"; then
log "Bootstrap failed, trying legacy load..."
if ! launchctl load "$LAUNCH_DAEMON"; then
log "Error: Failed to load service with both methods"
exit 1
fi
fi
else
# 旧版本使用 load
if ! launchctl load "$LAUNCH_DAEMON"; then
log "Error: Failed to load service"
exit 1
fi
fi
# 验证服务状态
log "Verifying service status..."
sleep 2
if launchctl list | grep -q "party.mihomo.helper"; then
log "Service loaded successfully"
else
log "Warning: Service may not be running properly"
fi
log "Installation completed successfully"
exit 0