1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
13 #include "rootcheck.h"
20 static int read_proc_file(const char *file_name, const char *pid, int position);
21 static int read_proc_dir(const char *dir_name, const char *pid, int position);
23 /* Global variables */
24 static int proc_pid_found;
27 static int read_proc_file(const char *file_name, const char *pid, int position)
31 if (lstat(file_name, &statbuf) < 0) {
35 /* If directory, read the directory */
36 if (S_ISDIR(statbuf.st_mode)) {
37 return (read_proc_dir(file_name, pid, position));
43 int read_proc_dir(const char *dir_name, const char *pid, int position)
48 if ((dir_name == NULL) || (strlen(dir_name) > PATH_MAX)) {
49 merror("%s: Invalid directory given", ARGV0);
53 /* Open the directory */
54 dp = opendir(dir_name);
59 while ((entry = readdir(dp)) != NULL) {
60 char f_name[PATH_MAX + 2];
63 if (strcmp(entry->d_name, ".") == 0 ||
64 strcmp(entry->d_name, "..") == 0) {
68 if (position == PROC) {
71 tmp_str = entry->d_name;
72 while (*tmp_str != '\0') {
73 if (!isdigit((int)*tmp_str)) {
79 if (*tmp_str != '\0') {
83 snprintf(f_name, PATH_MAX + 1, "%s/%s", dir_name, entry->d_name);
84 read_proc_file(f_name, pid, position + 1);
85 } else if (position == PID) {
86 if (strcmp(entry->d_name, "task") == 0) {
87 snprintf(f_name, PATH_MAX + 1, "%s/%s", dir_name, entry->d_name);
88 read_proc_file(f_name, pid, position + 1);
90 } else if (position == TASK) {
91 /* Check under proc/pid/task/lwp */
92 if (strcmp(entry->d_name, pid) == 0) {
106 /* Read the /proc directory (if present) and check if it can find
107 * the given pid (as a pid or as a thread)
109 int check_rc_readproc(int pid)
116 snprintf(char_pid, 31, "/proc/.%d", pid);
117 if (is_file(char_pid)) {
121 snprintf(char_pid, 31, "%d", pid);
122 read_proc_dir("/proc", char_pid, PROC);
124 return (proc_pid_found);