Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从swift呼叫nettop:“;无法分配内核控制套接字“;_Swift_Macos_Process_Permissions_Macos Big Sur - Fatal编程技术网

从swift呼叫nettop:“;无法分配内核控制套接字“;

从swift呼叫nettop:“;无法分配内核控制套接字“;,swift,macos,process,permissions,macos-big-sur,Swift,Macos,Process,Permissions,Macos Big Sur,我正在为命令行nettop实用程序编写macOS GUI包装 当我通过zsh从终端应用程序调用nettop-p-L 1时,它会显示正确的输出,但当我从我的Swift应用程序中启动流程时,它会在我的应用程序中显示以下内容: nettop[19213:2351456][NetworkStatistics]无法分配内核控制套接字nettop:nstatmanager创建失败 这是某种权限或沙箱问题吗?如果是,我如何让我的应用程序请求适当的权限 代码段: func shell(launchPath: S

我正在为命令行
nettop
实用程序编写macOS GUI包装

当我通过zsh从终端应用程序调用
nettop-p-L 1
时,它会显示正确的输出,但当我从我的Swift应用程序中启动流程时,它会在我的应用程序中显示以下内容:

nettop[19213:2351456][NetworkStatistics]无法分配内核控制套接字nettop:nstatmanager创建失败

这是某种权限或沙箱问题吗?如果是,我如何让我的应用程序请求适当的权限

代码段:

func shell(launchPath: String, arguments: [String] = []) -> (String? , Int32) {
    let task = Process()
    task.launchPath = launchPath
    task.arguments = arguments

    let pipe = Pipe()
    task.standardOutput = pipe
    task.standardError = pipe
    task.launch()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = String(data: data, encoding: .utf8)
    task.waitUntilExit()
    return (output, task.terminationStatus)
}

...


struct ContentView: View {
    var body: some View {
            Text(shell(launchPath: "/usr/bin/nettop", arguments: ["-P", "-L", "1"]).0 ?? "No Output")
    }
}

这确实是一个沙盒问题:nettop通过创建com.apple.network.statistics PF_SYSTEM/SYSPROTO_控制套接字(源代码示例见)来运行,该套接字可以沙盒化

访问此套接字受两层限制:

  • 当net.statistics\u privcheck sysctl设置为1时,将强制执行com.apple.private.network.statistics权限。这不是你的情况,因为你执行的是nettop,而nettop有权利这样做

  • 沙盒配置文件阻止创建系统套接字,除非

     (allow network-outbound
             (control-name "com.apple.network.statistics")
             (control-name "com.apple.netsrc"))
    
规则

似乎后一种情况就是你的情况,尽管从你的细节来看,不清楚故障是在你自己的应用程序的沙箱配置文件中还是在执行的nettop中。要确定这一点,请尝试自己创建套接字-如果您可以这样做,那么这就是exec的问题。如果你不能,那是你自己的个人资料。创建自己的沙盒配置文件是可能的,但这是苹果永远不会公开允许的,可能会让你被赶出应用商店


另一个测试-直接从命令行运行应用程序。(即,在终端中,然后是/Applications/path/to/your.app/Contents/MacOS/yourApp)。这样,它就不会由xpcproxy启动,并且会受到较少的沙箱约束。

为了明确起见,我通过转到“项目设置”、“签名和功能”选项卡,并在“应用程序沙箱”下选中“网络:传出连接(客户端)”框来修复此问题。你的回答帮助我找到了答案。好。这相当于将授权添加到二进制代码签名中,然后禁用沙箱配置文件。很高兴能帮忙。