1 /* @(#) $Id: ./src/rootcheck/unix-process.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
12 * License details at the LICENSE file included with OSSEC or
13 * online at: http://www.ossec.net/main/license/ .
18 #include "rootcheck.h"
22 char *_os_get_runps(char *ps, int mpid)
25 char buf[OS_SIZE_2048 +1];
26 char command[OS_SIZE_1024 +1];
32 command[OS_SIZE_1024] = '\0';
35 snprintf(command, OS_SIZE_1024, "%s -p %d 2> /dev/null", ps, mpid);
37 fp = popen(command, "r");
40 while(fgets(buf, OS_SIZE_2048, fp) != NULL)
42 tmp_str = strchr(buf, ':');
50 tmp_str = strchr(nbuf, ' ');
58 /* Removing white spaces. */
59 while(*tmp_str == ' ')
66 tmp_str = strchr(nbuf, '\n');
84 /* os_get_unix_process_list: Get list of Unix processes */
85 void *os_get_process_list()
88 pid_t max_pid = MAX_PID;
89 OSList *p_list = NULL;
91 char ps[OS_SIZE_1024 +1];
94 /* Checking where ps is */
95 memset(ps, '\0', OS_SIZE_1024 +1);
96 strncpy(ps, "/bin/ps", OS_SIZE_1024);
99 strncpy(ps, "/usr/bin/ps", OS_SIZE_1024);
102 merror("%s: ERROR: 'ps' not found.", ARGV0);
108 /* Creating process list */
109 p_list = OSList_Create();
112 merror(LIST_ERROR, ARGV0);
118 for(i = 1; i<= max_pid; i++)
120 /* Checking if the pid is present. */
121 if((!((getsid(i) == -1)&&(errno == ESRCH))) &&
122 (!((getpgid(i) == -1)&&(errno == ESRCH))))
127 p_name = _os_get_runps(ps, (int)i);
133 os_calloc(1, sizeof(Proc_Info), p_info);
134 p_info->p_path = p_name;
135 p_info->p_name = NULL;
136 OSList_AddData(p_list, p_info);
140 return((void *)p_list);