如何从 iPhone 中后台进程的名称访问(复制/修改)特定的 appfile 目录?

Posted

技术标签:

【中文标题】如何从 iPhone 中后台进程的名称访问(复制/修改)特定的 appfile 目录?【英文标题】:How to access (copy/modify) particular appfiledirectory from the name of its background process in iPhone? 【发布时间】:2011-11-19 05:56:32 【问题描述】:

我有后台进程列表及其在 iphone 后台运行的 pid 来自以下代码。我的项目要求是- (就像杀毒软件)

    获取每个进程的信息

一个。名称

b.尺寸

c。上次修改日期/时间

d。相关文件

e。进程从所有接口(存储、USB、蓝牙、Wi-Fi 等)访问的内容

f。任何其他可用信息

提前致谢。

#import <mach/mach_host.h>
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "sys/sysctl.h"    
#include <CoreFoundation/CoreFoundation.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>



- (void)viewDidLoad

 [super viewDidLoad];
 [self printProcessInfo];


-(int) printProcessInfo 

 int mib[5];
 struct kinfo_proc *procs = NULL, *newprocs;
 int i, st, nprocs;
 size_t miblen, size;

 /* Set up sysctl MIB */
 mib[0] = CTL_KERN;
 mib[1] = KERN_PROC;
 mib[2] = KERN_PROC_ALL;
 mib[3] = 0;
 miblen = 4;

 /* Get initial sizing */
 st = sysctl(mib, miblen, NULL, &size, NULL, 0);

 /* Repeat until we get them all ... */
 do 
      /* Room to grow */
      size += size / 10;
      newprocs = realloc(procs, size);
      if (!newprocs) 
           if (procs) 
                free(procs);
           
           perror("Error: realloc failed.");
           return (0);
      
      procs = newprocs;
      st = sysctl(mib, miblen, procs, &size, NULL, 0);
  while (st == -1 && errno == ENOMEM);

 if (st != 0) 
      perror("Error: sysctl(KERN_PROC) failed.");
      return (0);
 

 /* Do we match the kernel? */
 assert(size % sizeof(struct kinfo_proc) == 0);

 nprocs = size / sizeof(struct kinfo_proc);

 if (!nprocs) 
      perror("Error: printProcessInfo.");
      return(0);
 
 printf("  PID\tName\n");
 printf("-----\t--------------\n");
 self.lists = [[NSMutableString alloc] init];
 for (i = nprocs-1; i >=0;  i--) 
       printf("%5d\t%s\n",(int)procs[i].kp_proc.p_pid, procs[i].kp_proc.p_comm);



 
 NSLog(@"%@",lists);
 listsText.text = lists;
 free(procs);
 return (0);



【问题讨论】:

这段代码可以在 iPhone 上运行吗?? 哇...它确实可以在 iPhone 上编译和工作。不过,我不确定您是否可以在应用沙箱之外获得问题的答案。 是的迈克尔,这些都可能无法在应用程序级别访问。我想我需要像防病毒应用程序一样进入内核/系统级别。此类程序的任何参考?跨度> 【参考方案1】:

回答 a) 您在上述代码中获得的进程名称。

回答d)获取关联文件,将进程的pid传递给这个函数(我们在问题代码中得到了pid)-

 void print_argv_of_pid(int pid) 
  printf("%d\n", pid);
  int    mib[3], argmax, nargs, c = 0;
 size_t    size;
 char    *procargs, *sp, *np, *cp;
 extern int  eflg;
 int show_args = 1;

 mib[0] = CTL_KERN;
 mib[1] = KERN_ARGMAX;

 size = sizeof(argmax);
 if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) 
 goto ERROR_A;


   /* Allocate space for the arguments. */
   procargs = (char *)malloc(argmax);
   if (procargs == NULL) 
     goto ERROR_A;
   



   mib[0] = CTL_KERN;
   mib[1] = KERN_PROCARGS2;
   mib[2] = pid;


   size = (size_t)argmax;
   if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) 
     goto ERROR_B;
   

   memcpy(&nargs, procargs, sizeof(nargs));
   cp = procargs + sizeof(nargs);

   /* Skip the saved exec_path. */
   for (; cp < &procargs[size]; cp++) 
     if (*cp == '\0') 
       /* End of exec_path reached. */
       break;
     
   
   if (cp == &procargs[size]) 
     goto ERROR_B;
   

   /* Skip trailing '\0' characters. */
   for (; cp < &procargs[size]; cp++) 
     if (*cp != '\0') 
       /* Beginning of first argument reached. */
       break;
     
   
   if (cp == &procargs[size]) 
     goto ERROR_B;
   
   /* Save where the argv[0] string starts. */
   sp = cp;


   for (np = NULL; c < nargs && cp < &procargs[size]; cp++) 
     if (*cp == '\0') 
       c++;
       if (np != NULL) 
           /* Convert previous '\0'. */
           *np = ' ';
        else 
           /* *argv0len = cp - sp; */
       
       /* Note location of current '\0'. */
       np = cp;

       if (!show_args) 
      /*
       * Don't convert '\0' characters to ' '.
       * However, we needed to know that the
       * command name was terminated, which we
       * now know.
       */
      break;
       
     
   


   if (np == NULL || np == sp) 
     /* Empty or unterminated string. */
     goto ERROR_B;
   

   /* Make a copy of the string. */
   printf("%s\n", sp);

   /* Clean up. */
   free(procargs);
   return;

   ERROR_B:
   free(procargs);
   ERROR_A:
   printf("error");

 

答案 b),c)-大小和访问时间-

    struct stat st;
    //pass filepath upto /.app/ to stat function (use 'componentsseparatedby' of nsstring apply on full path which we got in answer d's code above) 
if (stat(filename, &st)) 
 perror(filename);
 else 
 printf("%s: mtime = %lld.%.9ld\n", filename, (long long)st.st_mtimespec.tv_sec, st.st_mtimespec.tv_nsec);

  printf("File size:                %lld bytes\n",
        (long long) st.st_size);


 printf("Last status change:       %s", ctime(&st.st_ctime));
 printf("Last file access:         %s", ctime(&st.st_atime));
 printf("Last file modification:   %s", ctime(&st.st_mtime));

其他信息- 杀死进程—— 只需传递要被杀死的进程的 pid-

int pid_exists(long pid)

 int kill_ret;

 // save some time if it's an invalid PID
 if (pid < 0) 
      return 0;
 

 // if kill returns success of permission denied we know it's a valid PID
 kill_ret = kill(pid , 0); 
 if ( (0 == kill_ret) || (EPERM == errno) ) 
      return 1;
 

 // otherwise return 0 for PID not found
 return 0;

【讨论】:

以上是关于如何从 iPhone 中后台进程的名称访问(复制/修改)特定的 appfile 目录?的主要内容,如果未能解决你的问题,请参考以下文章

如何每小时在后台运行约 30 秒的进程(iphone 应用程序)

Iphone,如何在后台保存坐标gps? [复制]

如何在 iPhone 中刷新 NSLocale?

进入后台、复制新文本并返回应用程序后如何从 UIPasteboard 中检索文本?

如何通过 JavaScript 访问一些具有相同名称的变量? [复制]

如何在 iPhone 游戏中播放背景音乐? [复制]