1 /* @(#) $Id: check_rc_readproc.c,v 1.8 2009/06/24 18:53:07 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
15 #include "rootcheck.h"
25 int read_proc_dir(char *dir_name, char *pid, int position);
28 int read_proc_file(char *file_name, char *pid, int position)
32 if(lstat(file_name, &statbuf) < 0)
37 /* If directory, read the directory */
38 else if(S_ISDIR(statbuf.st_mode))
40 return(read_proc_dir(file_name, pid, position));
49 int read_proc_dir(char *dir_name, char *pid, int position)
56 if((dir_name == NULL)||(strlen(dir_name) > PATH_MAX))
58 merror("%s: Invalid directory given",ARGV0);
62 /* Opening the directory given */
63 dp = opendir(dir_name);
69 while((entry = readdir(dp)) != NULL)
71 char f_name[PATH_MAX +2];
73 /* Just ignore . and .. */
74 if((strcmp(entry->d_name,".") == 0) ||
75 (strcmp(entry->d_name,"..") == 0))
82 tmp_str = entry->d_name;
84 while(*tmp_str != '\0')
86 if(!isdigit((int)*tmp_str))
95 snprintf(f_name, PATH_MAX +1, "%s/%s",dir_name, entry->d_name);
97 read_proc_file(f_name, pid, position+1);
100 else if(position == PID)
102 if(strcmp(entry->d_name, "task") == 0)
104 snprintf(f_name, PATH_MAX +1, "%s/%s",dir_name, entry->d_name);
105 read_proc_file(f_name, pid, position+1);
109 else if(position == TASK)
111 /* checking under proc/pid/task/lwp */
112 if(strcmp(entry->d_name, pid) == 0)
130 /* int check_rc_readproc(int pid): v0.1
131 * Reads the /proc directory (if present) and checks
132 * if the given pid is there (or as a PID or as a thread).
134 int check_rc_readproc(int pid)
141 snprintf(char_pid, 31, "/proc/.%d", pid);
142 if(is_file(char_pid))
146 snprintf(char_pid, 31, "%d", pid);
148 read_proc_dir("/proc", char_pid, PROC);
150 return(proc_pid_found);