bill xia(deleted)
|
Re: How to get argument list of running process from a program.
|
bill xia(deleted)
11/24/2011 9:45 PM
post90289
|
Re: How to get argument list of running process from a program.
Hi, you can refrence to following source code, maybe it's usful for you.
pid_t GetProcessPid(const char *process ){
DIR *dirp;
struct dirent *dire;
char buffer[32];
int fd=0, status;
pid_t pid;
struct dinfo_s {
procfs_debuginfo info;
char pathbuffer[PATH_MAX];
} dinfo;
printf("GetProcessId(%s)\r\n", process) ;
if ((dirp = opendir( "/proc" )) == NULL)
{
closedir( dirp );
printf( "Could not open '/proc'\n" );
return -1;
}
while (1)
{
if ((dire = readdir( dirp )) == NULL) break;
if (isdigit( dire->d_name[0] ))
{
pid = strtoul( dire->d_name, NULL, 0 );
sprintf( buffer, "/proc/%d/as", pid );
if (fd>0) close( fd );
fd = open( buffer, O_RDONLY ) ;
if (fd>0)
{
status = devctl( fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo, sizeof(dinfo), NULL ) ;
if (status == EOK)
{
printf("[%s]\r\n", basename(dinfo.info.path)) ;
if (!strcmp(process, basename(dinfo.info.path)))
{
if (fd>0) close( fd );
closedir( dirp );
return pid;
}
}
if (fd>0) close( fd );
}
}
}
if (fd>0) close( fd );
closedir( dirp );
return 0;
}
|
|
|