是否可以从以root身份运行的进程中运行pluginkit?

时间:2016-08-01 19:33:07

标签: macos unix launchd osx-extensions

我正在尝试从以root身份运行的启动守护程序运行pluginkit(在OS X上管理扩展的可执行文件)。

/usr/bin/pluginkit -m -i "<identifier>"失败,输出为match: Connection invalid。这并不是非常意外,因为扩展设置是基于每个用户处理的。

但是,我尝试使用su作为普通用户运行pluginkit,但仍然无效。

su <username> -l -c "/usr/bin/pluginkit -m -i "<identifier>"也失败,输出为match: Connection invalid

以某种方式运行pluginkit的环境仍然与普通用户不同,因为它无法正常工作。反正以root身份运行pluginkit吗?或者有没有其他方法可以作为另一个可能提供更完整环境的用户启动流程?

我正在使用Swift编写的命令行工具对此进行测试:

main.swift

import Foundation

let task = NSTask()

// Option 1: Run pluginkit directly
task.launchPath="/usr/bin/pluginkit"
task.arguments = ["-m", "-i", "com.example.findersyncext"]

// Option 2: Run pluginkit as <username> using 'su'
//task.launchPath="/usr/bin/su"
//task.arguments = ["<username>", "-l", "-c", "/usr/bin/pluginkit -m -i \"com.example.findersyncext\""]

// Option 3: Run pluginkit as <username> using 'sudo'
//task.launchPath="/usr/bin/sudo"
//task.arguments = ["-u", "<username>", "/usr/bin/pluginkit", "-m", "-i", "com.example.findersyncext"]

task.standardOutput = NSPipe()
task.standardError = NSPipe()
task.launch()
task.waitUntilExit()

NSLog("Exit code: \(task.terminationStatus)")
let output = NSString(data: (task.standardOutput!.fileHandleForReading.readDataToEndOfFile()), encoding: NSUTF8StringEncoding)
NSLog("Output: \(output)")

let error = NSString(data: (task.standardError!.fileHandleForReading.readDataToEndOfFile()), encoding: NSUTF8StringEncoding)
NSLog("Error: \(error)")

/Library/LaunchDaemons/com.example.PluginKitTest.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.example.PluginKitTest</string>
    <key>Program</key>
    <string>/path/to/PluginKitTest</string>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/Users/<username>/Desktop/pluginkit-error.log</string>
    <key>StandardOutPath</key>
    <string>/Users/<username>/Desktop/pluginkit-out.log</string>
</dict>
</plist>

0 个答案:

没有答案