1 /* @(#) $Id: ./src/rootcheck/check_rc_readproc.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
16 #include "rootcheck.h"
26 int read_proc_dir(char *dir_name, char *pid, int position);
29 int read_proc_file(char *file_name, char *pid, int position)
33 if(lstat(file_name, &statbuf) < 0)
38 /* If directory, read the directory */
39 else if(S_ISDIR(statbuf.st_mode))
41 return(read_proc_dir(file_name, pid, position));
50 int read_proc_dir(char *dir_name, char *pid, int position)
57 if((dir_name == NULL)||(strlen(dir_name) > PATH_MAX))
59 merror("%s: Invalid directory given",ARGV0);
63 /* Opening the directory given */
64 dp = opendir(dir_name);
70 while((entry = readdir(dp)) != NULL)
72 char f_name[PATH_MAX +2];
74 /* Just ignore . and .. */
75 if((strcmp(entry->d_name,".") == 0) ||
76 (strcmp(entry->d_name,"..") == 0))
83 tmp_str = entry->d_name;
85 while(*tmp_str != '\0')
87 if(!isdigit((int)*tmp_str))
96 snprintf(f_name, PATH_MAX +1, "%s/%s",dir_name, entry->d_name);
98 read_proc_file(f_name, pid, position+1);
101 else if(position == PID)
103 if(strcmp(entry->d_name, "task") == 0)
105 snprintf(f_name, PATH_MAX +1, "%s/%s",dir_name, entry->d_name);
106 read_proc_file(f_name, pid, position+1);
110 else if(position == TASK)
112 /* checking under proc/pid/task/lwp */
113 if(strcmp(entry->d_name, pid) == 0)
131 /* int check_rc_readproc(int pid): v0.1
132 * Reads the /proc directory (if present) and checks
133 * if the given pid is there (or as a PID or as a thread).
135 int check_rc_readproc(int pid)
142 snprintf(char_pid, 31, "/proc/.%d", pid);
143 if(is_file(char_pid))
147 snprintf(char_pid, 31, "%d", pid);
149 read_proc_dir("/proc", char_pid, PROC);
151 return(proc_pid_found);