1 /* @(#) $Id: unix-process.c,v 1.5 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
11 * License details at the LICENSE file included with OSSEC or
12 * online at: http://www.ossec.net/main/license/ .
17 #include "rootcheck.h"
21 char *_os_get_runps(char *ps, int mpid)
24 char buf[OS_SIZE_2048 +1];
25 char command[OS_SIZE_1024 +1];
31 command[OS_SIZE_1024] = '\0';
34 snprintf(command, OS_SIZE_1024, "%s -p %d 2> /dev/null", ps, mpid);
36 fp = popen(command, "r");
39 while(fgets(buf, OS_SIZE_2048, fp) != NULL)
41 tmp_str = strchr(buf, ':');
49 tmp_str = strchr(nbuf, ' ');
57 /* Removing white spaces. */
58 while(*tmp_str == ' ')
65 tmp_str = strchr(nbuf, '\n');
83 /* os_get_unix_process_list: Get list of Unix processes */
84 void *os_get_process_list()
87 pid_t max_pid = MAX_PID;
88 OSList *p_list = NULL;
90 char ps[OS_SIZE_1024 +1];
93 /* Checking where ps is */
94 memset(ps, '\0', OS_SIZE_1024 +1);
95 strncpy(ps, "/bin/ps", OS_SIZE_1024);
98 strncpy(ps, "/usr/bin/ps", OS_SIZE_1024);
101 merror("%s: ERROR: 'ps' not found.", ARGV0);
107 /* Creating process list */
108 p_list = OSList_Create();
111 merror(LIST_ERROR, ARGV0);
117 for(i = 1; i<= max_pid; i++)
119 /* Checking if the pid is present. */
120 if((!((getsid(i) == -1)&&(errno == ESRCH))) &&
121 (!((getpgid(i) == -1)&&(errno == ESRCH))))
126 p_name = _os_get_runps(ps, (int)i);
132 os_calloc(1, sizeof(Proc_Info), p_info);
133 p_info->p_path = p_name;
134 p_info->p_name = NULL;
135 OSList_AddData(p_list, p_info);
139 return((void *)p_list);