Exemplo n.º 1
0
int find_fd_for_pid(pid_t pid, int *fd_list, int max_fd)
{
DIR *proc;
struct dirent *direntp;
char path_dir[MAXPATHLEN + 1];
char fullpath[MAXPATHLEN + 1];
char link_dest[MAXPATHLEN + 1];
struct stat stat_buf;
int count = 0;
ssize_t len;

snprintf(path_dir, MAXPATHLEN, "%s/%d/fd", PROC_PATH, pid);

proc = opendir(path_dir);
if (!proc) {
    nperror("opendir");
    nfprintf(stderr,"Can't open %s\n",path_dir);
    return 0;
}

while ((direntp = readdir(proc)) != NULL) {
    snprintf(fullpath, MAXPATHLEN, "%s/%s", path_dir, direntp->d_name);
    if (stat(fullpath, &stat_buf) == -1) {
        if (flag_debug)
            nperror("stat (find_fd_for_pid)");
        continue;
    }

    // if not a regular file or a block device
    if(!S_ISREG(stat_buf.st_mode) && !S_ISBLK(stat_buf.st_mode))
        continue;

    // try to read link ...
    len = readlink(fullpath, link_dest, MAXPATHLEN);
    if (len != -1)
        link_dest[len] = 0;
    else
        continue;

    // try to stat link target (invalid link ?)
    if (stat(link_dest, &stat_buf) == -1)
        continue;

    if (is_ignored_file(fullpath) || is_ignored_file(link_dest))
        continue;

    // OK, we've found a potential interesting file.

    fd_list[count++] = atoi(direntp->d_name);
    //~ printf("[debug] %s\n",fullpath);
    if (count == max_fd)
        break;
}

closedir(proc);
return count;
}
Exemplo n.º 2
0
static char * test_is_extension_good_ko()
{
    struct search_t *search;
    search = create_search();
    add_element(&search->extension, ".cpp");
    mu_assert("test_is_extension_good_ko failed", is_ignored_file(search, "file.c") == 0);
    free_search(search);
    return 0;
}
Exemplo n.º 3
0
static char * test_is_ignored_file_ko()
{
    struct search_t *search;
    search = create_search();
    add_element(&search->ignore, "rules");
    mu_assert("test_is_ignored_file_ko failed", is_ignored_file(search, "Rules") == 0);
    free_search(search);
    return 0;
}
Exemplo n.º 4
0
int find_fd_for_pid(pid_t pid, int *fd_list, int max_fd)
{
int count = 0;
int bufferSize = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, 0, 0);
struct stat stat_buf;

if (bufferSize < 0) {
    printf("Error :/, cannot proc_pidinfo\n");
    return 0;
}
struct proc_fdinfo *procFDInfo = (struct proc_fdinfo *)malloc(bufferSize);
proc_pidinfo(pid, PROC_PIDLISTFDS, 0, procFDInfo, bufferSize);
int numberOfProcFDs = bufferSize / PROC_PIDLISTFD_SIZE;
int i;

for(i = 0; i < numberOfProcFDs; i++) {
    if(procFDInfo[i].proc_fdtype == PROX_FDTYPE_VNODE) {
        struct vnode_fdinfowithpath vnodeInfo;
        proc_pidfdinfo(pid, procFDInfo[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE);
        if (stat(vnodeInfo.pvip.vip_path, &stat_buf) < 0) {
            if (flag_debug)
                perror("sstat");
            continue;
        }
        if (!S_ISREG(stat_buf.st_mode) && !S_ISBLK(stat_buf.st_mode))
            continue;

        if (is_ignored_file(vnodeInfo.pvip.vip_path))
            continue;

        // OK, we've found a potential interesting file.

        fd_list[count++] = procFDInfo[i].proc_fd;
        //~ printf("[debug] %s\n",vnodeInfo.pvip.vip_path);
        if(count == max_fd)
            break;
    }
}
return count;
}
Exemplo n.º 5
0
int
filter_chinese_in_file(const char* pcszFileName){
	char		szBuffer[MAX_LINE_LEN];
	char		szFileName[MAX_PATH];
	int		nStrLen, nPos, nType;
	FILE*		pOriginalFile  = NULL;
	FILE*		pTranslateFile = NULL;
	int		nIsANewLineEnd = 0;
	int		nFiltrate = 1;
	int		nRet, i;
	char		szTemp[64];
	char		szTemp2[8];

	g_ignoreIdx = -1;
	nRet = is_ignored_file(pcszFileName);
	if ( nRet >= 0 ){
		if ( g_ignoreCol[nRet][0] > 0 ){
			g_ignoreIdx = nRet;
			memset(szTemp, 0, sizeof(szTemp));
			for (i = 0; i < MAX_IGNORE_COL_NUM; i ++)
			{
				if ( g_ignoreCol[nRet][i] == 0 )
					break;
				sprintf(szTemp2, "%d ", g_ignoreCol[nRet][i]);
				strcat(szTemp, szTemp2);
			}
			write_log(LT_FILE, "Info: File %s's %scolumn is ignored!\n",
				pcszFileName, szTemp);
		}
		else if ( g_ignoreCol[nRet][0] == -1 ){
			write_log(LT_FILE, "Info: File %s is ignored!\n",
				pcszFileName);
			return 1;
		}
		else{
			write_log(LT_FILE, "Info: File %s will not filtrate!\n",
				pcszFileName);
			nFiltrate = 0;
		}
	}

	if ( g_nNotFiltrateDir == 1 )
		nFiltrate = 0;

	nType = get_file_type( pcszFileName );
	if ( nType == FILE_UN_KNOWN ){
		write_log(LT_FILE, "Error: Unknown file type at file %s\n", 
			pcszFileName);
		nFiltrate = 0;
	}

	if ( g_nTranslate == 0 && nFiltrate == 0 )
		return 1;

	pOriginalFile = fopen( pcszFileName, "r" );
	if ( pOriginalFile == NULL ){
		write_log(LT_BOTH, "Error: Can't open file: %s\n", 
			pcszFileName);
		return 0;
	}

	/* if need translate, then open the translate file waiting for write */
	if ( g_nTranslate == 1 ){
		sprintf(szFileName, "%s%s", g_szTranslateRoot,
		        pcszFileName+strlen(g_szOrginalRoot));

		pTranslateFile = fopen(szFileName, "w");
		if ( pTranslateFile == NULL ){
			SAFE_CLOSE_FILE(pOriginalFile);
			write_log(LT_BOTH, "Error: Can't open file: %s\n",
				szFileName);
			return 0;
		}
	}
	
	g_nCurLine = 0;
	szBuffer[0] = '\0';
	/* read and process the file line by line */
	do{
		/* if it is translating file now, 
		   then write the translated line to the corresponding file*/
		if ( g_nTranslate == 1 && g_nCurLine > 0 ){
			if ( szBuffer[0] != '\0' )
				fwrite(szBuffer, 1, strlen(szBuffer), 
				pTranslateFile);
			if ( nIsANewLineEnd == 1 )
				fwrite("\n", 1, 1, pTranslateFile);			
		}

		nIsANewLineEnd = 0;
		if ( fgets(szBuffer, MAX_LINE_LEN, pOriginalFile) == NULL )
			break;
		
		g_nCurLine ++;
		szBuffer[MAX_LINE_LEN - 1] = '\0';

		nStrLen = (int)strlen( szBuffer );
		/* clear the newline at the tail of the line */
		if ( szBuffer[nStrLen-1] == '\n' ||
		     szBuffer[nStrLen-1] == '\r' ){
			szBuffer[nStrLen-1] = '\0';
			nIsANewLineEnd = 1;
		}

		if ( nFiltrate == 0 )
		     continue;

		/* it is impossible a string which its length is 1 contain chinese string */
		if ( nStrLen <= 1 )
			continue;

		/* search the first character which is not space and tab */
		for( nPos = 0; nPos < nStrLen; nPos++){
			if ( is_tab(szBuffer[nPos]) || is_space(szBuffer[nPos]) )
				continue;
			if ( is_gbk_chinese_space((unsigned char)szBuffer[nPos],
				(unsigned char)szBuffer[nPos+1])){
				nPos++;
				continue;
			}
			break;
		}

		/* ignore empty line and single character line */
		if ( szBuffer[nPos] == '\0' || szBuffer[nPos+1] == '\0')
			continue;

		/* record the current filtrating file and its position */
		g_nCurPos = 0;
		strcpy( g_szCurFilterFile, pcszFileName );
		
		g_nCurFilterType = nType;
		/* filtrate the current line' chinese string and process it */
		g_FilterFunc[nType]( &szBuffer[nPos], 
			(int)strlen(&szBuffer[nPos]) );
	}while(1);

	SAFE_CLOSE_FILE(pOriginalFile);
	SAFE_CLOSE_FILE(pTranslateFile);
	
	g_nProcessFileNum ++;
	
	if ( (g_nProcessFileNum % VIEW_A_NUM) == 0 ){
		printf("+ ");
		fflush(stdout);
	}
	if ( (g_nProcessFileNum % VIEW_LINE_NUM) == 0  )
		printf("\n");

	return 1;
}
Exemplo n.º 6
0
int
load_ignore_file(){
	FILE*		pFile = NULL;
	char		szBuffer[MAX_PATH];
	int		nStrLen,nColCount;
	char		*pStart, *pEnd;
	char		*pPrevTab, *pNextTab;
	int		nFileCount;
	int		nColNum;
	
	pFile = fopen(IGNORE_FILE_CFG, "r");
	if ( pFile == NULL ){
		write_log(LT_BOTH, "Error: can't open file %s\n", IGNORE_FILE_CFG);
		return 0;
	}

	nFileCount = 0;
	memset(g_ignoreFile, 0, sizeof(g_ignoreFile));
	memset(g_ignoreCol,  0, sizeof(g_ignoreCol));

	while ( fgets(szBuffer, MAX_PATH, pFile) != NULL ){
		szBuffer[MAX_PATH - 1] = '\0';
		nStrLen = (int)strlen(szBuffer);
		
		if ( szBuffer[0] == ';' )
			continue;

		if ( szBuffer[nStrLen-1] == '\n' ||
		     szBuffer[nStrLen-1] == '\r'){
			szBuffer[nStrLen-1] = '\0';
			nStrLen --;
		}

		if ( nStrLen == 0 )
			continue;

		pStart = szBuffer;
		while ( *pStart != '\0' ){
			if ( !is_tab(*pStart) && !is_space(*pStart) )
				break;
			pStart ++;
		}
		
		pEnd = &szBuffer[nStrLen - 1];
		while ( pEnd != pStart ){
			if ( !is_tab(*pEnd) && !is_space(*pEnd) )
				break;
			pEnd--;
		}
		*(pEnd+1) = '\0';
		

		pPrevTab = strchr(szBuffer, '\t');
		if ( pPrevTab == NULL ){
			if ( is_ignored_file(pStart) > -1 ){
				write_log(LT_BOTH, "Error: file %s"
					"have existed in before line!\n",
				       pStart);
				continue;
			}
			strncpy(g_ignoreFile[nFileCount++], pStart,
				(int)(pEnd-pStart)+1);
		}
		else {
			*pPrevTab = '\0';
			if ( is_ignored_file(pStart) > -1 ){
				write_log(LT_BOTH, "Error: file %s have "
					"existed in before line!\n",
				       pStart);
				continue;
			}

			strncpy(g_ignoreFile[nFileCount++], pStart,
				(int)(pPrevTab-pStart));

			nColCount = 0;
			pNextTab = pPrevTab;
			
			do{
				pPrevTab = pNextTab+1;
				pNextTab = strchr(pPrevTab, '\t');

				if ( pNextTab == NULL )
					pNextTab = pEnd+1;
				*pNextTab = '\0';
				nColNum = atoi( pPrevTab );
				g_ignoreCol[nFileCount-1][nColCount++] = nColNum;
				
				if ( nColCount == MAX_IGNORE_NUM ){
					write_log(LT_BOTH, 
						"Error: too much ignore column number for %s!\n",
						g_ignoreFile[nFileCount-1]);
					break;
				}
			}while (pNextTab != pEnd+1);
		}

		if ( nFileCount >= MAX_IGNORE_NUM ){
			write_log(LT_BOTH, "Error: exceed the max setting"
				"number when load %s", IGNORE_FILE_CFG);
			break;
		}
	}

	SAFE_CLOSE_FILE(pFile);

	return 1;
}