2016-10-24 103 views
0

我想在運行時獲取一些進程信息,尤其是父進程名稱。 雖然我能夠獲取當前進程名稱,但似乎無法對其父進行相同操作。
下面是我在做什麼:在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是相同的。
我錯過了什麼嗎?

回答

2

不能獲取自iOS 9以來的其他進程的信息。現在sysctl已被沙盒化。您只能在iDevice 之前的iOS 9或模擬器中執行此操作。

的sysctl()檢索用於處理具有適當特權

iOS應用不允許看到什麼其他應用正在運行

在iOS系統9中,沙箱現在防止進程訪問所述系統信息kern.proc, kern.procargs和kern.procargs2值其他進程

見:

相關問題