2009년 11월 06일
process name으로 동작중인지 확인하기~
최근 작업중인 부분중에 필요로 해서 만든 function... -_-;;
argument로 process_name을 받아서
/proc의 하위 디렉토리를 하나씩 읽어가면서 /proc/pid/cmdline을 읽어서 입력 받은 process name이 있는지 확인하는 func.
특이점이라면... /proc/pid/cmdline에 process 기동시 주었던 argument 관련된 내용들이 중간에 NULL 문자로 채워져 있어서...
cmdline open시 'b' 옵션 (binary 옵션)을 주어서 읽어야 argument까지 처리 가능하다는... -_-;;
cat /proc/pid/cmdline시 정상적으로 보여도 read 하게 되는 경우 잘린다는 문제점이 있더랍니다..ㅋ
ex)
cat -> /usr/java/j2sdk1.4.2_12/bin/javacom.xxx.xxx.service
vi -> /usr/java/j2sdk1.4.2_12/bin/java^@com.xxx.xxx.service^@
위와 같기 때문에 feof로 화일의 끝까지 읽어줘야 처리가 가능합니다... ㅠㅠ
{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//#include <linux/unistd.h>
int fndPsRunning(char *process)
{
char pname[64], buf[512], *ptr, *peek;
struct dirent *dInfo;
DIR *dp;
int fd;
FILE *fp;
int len;
if( (dp = opendir("/proc")) == (DIR *)NULL)
{
printf("opendir failure: %s\n", strerror(errno));
return -1;
}
while(1)
{
/* read all dirs.. /proc */
dInfo = readdir(dp);
if(dInfo == (struct dirent *)NULL) break;
if(atoi(dInfo->d_name) == 0) continue;
/* open /proc/dir_name/stat */
memset(pname, 0x00, sizeof(pname));
sprintf(pname, "/proc/%s/cmdline", dInfo->d_name);
fp = fopen(pname, "rb");
len = 0;
while(!feof(fp))
buf[len++] = fgetc(fp);
if(strstr(buf, process) != (char *)NULL) return 1;
if(len != strlen(buf))
{
char *mptr;
mptr = &buf[strlen(buf) + 1];
if(strstr(mptr, process) != (char *)NULL) return 1;
}
}
close(fd);
closedir(dp);
return 0;
}
# by | 2009/11/06 14:56 | IT Life | 트랙백 | 덧글(0)








☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]