ios - 在 iOS 应用程序运行时获取父进程信息

标签 ios pid bsd sysctl

我试图在 iOS 运行时获取一些进程信息,尤其是父进程名称。 虽然我能够获取当前进程名称,但似乎无法为其父进程做同样的事情。
这是我正在做的:

static inline bool is_debugserver_present() {
    int                 err;
    int                 mib[4];
    struct kinfo_proc   info;
    size_t              size;

    // Initialize the flags so that, if sysctl fails for some bizarre
    // reason, we get a predictable result.

    info.kp_proc.p_flag = 0;

    // Initialize mib, which tells sysctl the info we want, in this case
    // we're looking for information about a the parent process ID.

    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PID;
    mib[3] = getppid();

    // Call sysctl.

    size = sizeof(info);
    int n = sizeof(mib) / sizeof(*mib);
    err = sysctl(mib, n, &info, &size, NULL, 0);

    return (strncmp(info.kp_proc.p_comm, "launchd", sizeof("launchd") - 1) != 0);
}

问题是对 sysctl 的调用总是返回 -1,因此是一个错误。 如果我向当前进程询问其kp_eproc.e_ppid,则通过getppid()获得的父进程id是相同的。
我错过了什么吗?

最佳答案

自 iOS 9 以来,您无法获取其他进程的信息。sysctl 现在已被沙盒化。您只能在 以前的 iOS 9 或模拟器中执行此操作。

sysctl() retrieves system information for processes with appropriate privileges

iOS apps are not permitted to see what other apps are running

In iOS 9, the sandbox now prevents a process from accessing the kern.proc, kern.procargs, and kern.procargs2 values for other processes

见:

关于ios - 在 iOS 应用程序运行时获取父进程信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40218676/

相关文章:

java - 如何找到在 java 中启动的进程的进程 ID (pid)?

linux - 如何使用 shell 计算单词在文件中出现的次数?

android - 你能在 Ionic 1 上使用 Native Storage 吗?

ios - Facebook 用户信息以及联系电话

ios - 更改iframe中视频加载的方向

linux - BSD 套接字头

c - 很好的 C 库集合?

ios - 按下按钮时更改为不同的 VIewController

unix - 子进程在 fork() 之后始终为零且始终唯一?

bash - 如何终止后台/分离的 ssh session ?