http://stackoverflow.com/questions/3018054/retrieve-names-of-running-processes
但是我的机器上没有动态库libproc.dylib,编译环境里也没有对应头文件
#include <sys/proc_info.h>
#include <libproc.h>
也没有用来连接的静态库。于是从ps命令入手:
http://www.opensource.apple.com/tarballs/adv_cmds/
通过sysctl传递KERN_PROC,参数为KERN_PROC_ALL。这样可以获得当前的进程信息列表,每个进程的信息通过结构体kinfo_proc来反映。
这里包含pid:kp->kp_proc.p_pid,然后在调用sysctl传递KERN_PROCARGS2,pid作为参数,就可以获得对应的运行参数:
http://my-sample-code.googlecode.com/svn/trunk/user-set/get_pidbyname.c
int main (int argc, char **argv, char **envp, char **apple);
0: 01 00 00 00 2e 2f 67 65 74 70 69 64 70 61 74 68
10: 00 00 00 00 2e 2f 67 65 74 70 69 64 70 61 74 68
20: 00
argc = 1
exec_path = ./getpidpath
完整的结构是这个样子:
/*
* Make a sysctl() call to get the raw argument space of the process.
* The layout is documented in start.s, which is part of the Csu
* project. In summary, it looks like:
*
* /—————\ 0×00000000
* : :
* : :
* |—————|
* | argc |
* |—————|
* | arg[0] |
* |—————|
* : :
* : :
* |—————|
* | arg[argc - 1] |
* |—————|
* | 0 |
* |—————|
* | env[0] |
* |—————|
* : :
* : :
* |—————|
* | env[n] |
* |—————|
* | 0 |
* |—————| <– Beginning of data returned by sysctl() is here.
* | argc |
* |—————|
* | exec_path |
* |:::::::::::::::|
* | |
* | String area. |
* | |
* |—————| <– Top of stack.
* : :
* : :
* \—————/ 0xffffffff
*/
/* getpidenv.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
void hex_dump(char *str, void *buf, int size)
{
int i;
unsigned char *ubuf = buf;
if(str)
printf(“%s:”, str);
for(i=0; i<size; i++){
if((i%16)==0){
printf(“\n%4x:”, i);
}
printf(” %02x”, ubuf[i]);
}
printf(“\n\n”);
}
char *nextstr(char *p)
{
while(*p<0×20 ||*p>0x7e)
p++;
return p;
}
int analysis_args(char * buf)
{
int argc, i;
char *p = buf;
argc = *(unsigned int *)buf;
hex_dump(“args”, buf-0×100, 0×700);
printf(“argc %d\n”, argc);
for(i=0; i<100/*argc*/; i++){
p = nextstr(p);
printf(“off [%08x] argc[%d]=[%s]\n”, p-buf, i, p);
p+= strlen(p);
}
}
int main(int argc, char *argv[])
{
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
size_t bufSize = 0;
struct kinfo_proc *kprocbuf;
struct kinfo_proc *kp;
int retry_count = 0;
size_t orig_bufSize = 0;
int local_error=0;
int nentries, i;
int argmax;
size_t size;
char *procargs;
if (sysctl(mib, 4, NULL, &bufSize, NULL, 0) < 0) {
printf(“Failure calling sysctl\n”);
return 0;
}
kprocbuf= kp = (struct kinfo_proc *)malloc(bufSize);
retry_count = 0;
orig_bufSize = bufSize;
for(retry_count=0; ; retry_count++){
/* retry for transient errors due to load in the system */
local_error = 0;
bufSize = orig_bufSize;
if ((local_error = sysctl(mib, 4, kp, &bufSize, NULL, 0)) < 0) {
if (retry_count < 1000) {
/* 1 sec back off */
sleep(1);
continue;
}
printf(“Failure calling sysctl\n”);
return 0;
}else if(local_error == 0) {
break;
}
/* 1 sec back off */
sleep(1);
}
/* Get the maximum process arguments size. */
mib[0] = CTL_KERN;
mib[1] = KERN_ARGMAX;
size = sizeof(argmax);
if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) {
printf(“Failure calling sysctl KERN_ARGMAX\n”);
return 0;
}
printf(“get argmax %d\n”, argmax);
procargs = (char *)malloc(argmax);
nentries = bufSize / sizeof(struct kinfo_proc);
for(i=0; i<nentries; i++,kp++){
mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS2;
mib[2] = kp->kp_proc.p_pid;
size = (size_t)argmax;
if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) {
printf(“Failure calling sysctl KERN_PROCARGS\n”);
return 0;
}
analysis_args(procargs);
goto DONE;
#if 0
if(strstr(procargs+4, “39809BB6-96CE-4DE5-8701-44B3AB7738D2″)!=NULL){
printf(“[%d] [%s]\n”, kp->kp_proc.p_pid, procargs+4);
analysis_args(procargs);
goto DONE;
}
#endif
}
DONE:
free(kprocbuf);
free(procargs);
return 0;
}
############################################################################
argc 1
off [00000004] argc[0]=[./genv]
off [0000000c] argc[1]=[./genv]
off [00000013] argc[2]=[TERM=vt100]
off [0000001e] argc[3]=[SHELL=/bin/sh]
off [0000002c] argc[4]=[CLICOLOR=]
off [00000036] argc[5]=[SSH_CLIENT=192.168.2.112 1487 22]
off [00000057] argc[6]=[SSH_TTY=/dev/ttys000]
off [0000006c] argc[7]=[USER=root]
off [00000076] argc[8]=[LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:]
off [0000058a] argc[9]=[SSH_AUTH_SOCK=/tmp/ssh-0x0M2EdWxC/agent.3461]
off [000005b7] argc[10]=[MAIL=/var/mail/root]
off [000005cb] argc[11]=[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games]
off [00000625] argc[12]=[PWD=/home/jerry]
off [00000635] argc[13]=[PS1=\h:\w \u\$ ]
off [00000645] argc[14]=[SHLVL=1]
off [0000064d] argc[15]=[HOME=/var/root]
off [0000065c] argc[16]=[LOGNAME=root]
off [00000669] argc[17]=[SSH_CONNECTION=192.168.2.112 1487 192.168.2.107 22]
off [0000069c] argc[18]=[_=./genv]
off [000006a5] argc[19]=[OLDPWD=/var/root]
############################################################################
argc 1
off [00000004] argc[0]=[/var/mobile/Applications/39809BB6-96CE-4DE5-8701-44B3AB7738D2/tdbeta.app/tdbeta]
off [00000054] argc[1]=[/var/mobile/Applications/39809BB6-96CE-4DE5-8701-44B3AB7738D2/tdbeta.app/tdbeta]
off [000000a4] argc[2]=[PATH=/usr/bin:/bin:/usr/sbin:/sbin]
off [000000c7] argc[3]=[TMPDIR=/private/var/mobile/Applications/39809BB6-96CE-4DE5-8701-44B3AB7738D2/tmp]
off [00000118] argc[4]=[SHELL=/bin/sh]
off [00000126] argc[5]=[HOME=/private/var/mobile/Applications/39809BB6-96CE-4DE5-8701-44B3AB7738D2]
off [00000171] argc[6]=[USER=mobile]
off [0000017d] argc[7]=[LOGNAME=mobile]
off [0000018c] argc[8]=[__CF_USER_TEXT_ENCODING=0x1F5:0:0]
off [000001ae] argc[9]=[_MSSafeMode=0]
off [000001bc] argc[10]=[CFFIXED_USER_HOME=/private/var/mobile/Applications/39809BB6-96CE-4DE5-8701-44B3AB7738D2]
off [00000214] argc[11]=[DYLD_INSERT_LIBRARIES=/Library/MobileSubstrate/MobileSubstrate.dylib]
off [000002c0] argc[12]=[35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:]
off [0000058a] argc[13]=[SSH_AUTH_SOCK=/tmp/ssh-0x0M2EdWxC/agent.3461]
off [000005b7] argc[14]=[MAIL=/var/mail/root]
off [000005cb] argc[15]=[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games]
off [00000625] argc[16]=[PWD=/home/jerry]
off [00000635] argc[17]=[PS1=\h:\w \u\$ ]
off [00000645] argc[18]=[SHLVL=1]
off [0000064d] argc[19]=[HOME=/var/root]
off [0000065c] argc[20]=[LOGNAME=root]
off [00000669] argc[21]=[SSH_CONNECTION=192.168.2.112 1487 192.168.2.107 22]
off [0000069c] argc[22]=[_=./genv]
off [000006a5] argc[23]=[OLDPWD=/var/root]
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!