1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <inttypes.h>
#include <grp.h>
#include <pwd.h>
#include <signal.h>
#include <stdint.h>
#define MAX_PID 32767
struct Process_Info {
char status ;
char *name ;
pid_t pid ;
pid_t ppid ;
uid_t uid ;
pid_t pgrp ;
pid_t sid ;
gid_t gid ;
int8_t cpu_use_count ;
int8_t cpu_1 ;
int8_t cpu_2 ;
int8_t cpu_3 ;
int8_t cpu_4 ;
int8_t priority ;
int scheduling ;
struct group *grp ;
struct passwd *password ;
} process_infos[]={} ;
void fill_process_information(pid_t pid,struct Process_Info *process_infos,int index) ;
int main() {
/* creation of one seperator line (use the fullscreen terminal size for displaying)
* and start the programm as root.
*/
char *sep_string=malloc(88) ;
int c ;
strcpy(sep_string,"-") ;
for (c=1; c < 87 ; c++) {
strcat(sep_string,"-") ;
}
sep_string[c]='\0' ;
/* process search */
pid_t list_of_process[MAX_PID] ;
int counter ;
int process_counter=0 ;
for (counter=1 ; counter < MAX_PID ; counter++) {
pid_t exist=counter;
int8_t ok ;
if ( (ok=kill(exist,0) == 0) ) {
list_of_process[process_counter]=exist ;
process_counter++ ;
ok = -1 ;
}
}
for (counter=0 ; counter < process_counter ; counter++) {
fill_process_information(list_of_process[counter],process_infos,counter) ;
/* affichage des résultat */
fprintf(stdout,"%s\n",sep_string) ;
fprintf(stdout,"Process...: %-20s PID %05d PGRP %05d PPID %05d SID %05d STATUS: %c \n",process_infos[counter].name,process_infos[counter].pid,process_infos[counter].pgrp,process_infos[counter].ppid,process_infos[counter].sid,process_infos[counter].status) ;
fprintf(stdout,"User......: %-20s UID %05d GID %05d GECOS %s\n",process_infos[counter].password->pw_name,process_infos[counter].password->pw_uid,process_infos[counter].password->pw_gid,process_infos[counter].password->pw_gecos) ;
fprintf(stdout,"Group.....: %-20s GID %05d MEMBERS ",process_infos[counter].grp->gr_name,process_infos[counter].grp->gr_gid) ;
/* show the information: members of the group */
int c=0 ;
char *tmp_ptr ;
tmp_ptr=process_infos[counter].grp->gr_mem[c] ;
while ( tmp_ptr != NULL) {
fprintf(stdout,"%s ",tmp_ptr) ;
c++ ;
tmp_ptr=process_infos[counter].grp->gr_mem[c] ;
}
fprintf(stdout,"\n") ;
/* How many processors|core and wich the processus use.
* 4 processors available on my system
*/
fprintf(stdout,"Processor.: \t\t\t COUNT %d CPU1 %d CPU2 %d CPU3 %d CPU4 %d\n",process_infos[counter].cpu_use_count,process_infos[counter].cpu_1,process_infos[counter].cpu_2,process_infos[counter].cpu_3,process_infos[counter].cpu_4) ;
}
exit(EXIT_SUCCESS) ;
}
void fill_process_information(pid_t pid,struct Process_Info *process_infos,int index) {
FILE *fp ;
char buffer[255] ;
if (sprintf(buffer,"cat /proc/%d/status",pid) == 0) {
char *err_msg ;
err_msg=strerror(errno) ;
fprintf(stderr,"%s\n",err_msg) ;
exit(EXIT_FAILURE) ;
}
if ((fp=popen(buffer,"r")) == NULL) {
char *err_msg ;
err_msg=strerror(errno) ;
fprintf(stderr,"%s\n",err_msg) ;
exit(EXIT_FAILURE) ;
}
char *get_status_file_content=malloc(1024) ; /* wc -c /proc/<pid>/status == 820 */
if (fread(get_status_file_content, (size_t) 1024, (size_t) 1, fp) < 0) {
/* print SUCCCES
char *err_msg ;
err_msg=strerror(errno) ;
fprintf(stderr,"%s\n",err_msg) ;
*/
}
else {
pclose(fp) ;
/* getting pid */
process_infos[index].pid= pid ;
/* Getting the status of the process */
char *get_state ;
get_state=strstr(get_status_file_content,"State:") ;
process_infos[index].status=*get_state ;
/* Getting process Name from /proc/<pid>/status */
char *get_name=strstr(get_status_file_content,"Name:") ;
int counter=0 ;
get_name += 6 ; /* offset to get the process name */
char name[64] ;
name[0]=*get_name ;
while (*get_name != '\n') {
counter++ ;
get_name++ ;
name[counter]=*get_name ;
}
name[counter]='\0' ;
process_infos[index].name=name ;
/* Getting process Gid from /proc/<pid>/status */
uid_t get_gid[4] ;
sscanf(strstr(get_status_file_content,"Gid:"),"Gid: %d %d %d %d",&get_gid[0],&get_gid[1],&get_gid[2],&get_gid[3]) ;
if ( get_gid[0] == get_gid[1] && get_gid[1] == get_gid[2] && get_gid[2] == get_gid[3]) {
process_infos[index].gid = get_gid[0] ;
}
else {
process_infos[index].gid = -1 ;
}
/* Getting process Uid from /proc/<pid>/status */
uid_t get_uid[4] ;
sscanf(strstr(get_status_file_content,"Uid:"),"Uid: %d %d %d %d",&get_uid[0],&get_uid[1],&get_uid[2],&get_uid[3]) ;
if ( get_uid[0] == get_uid[1] && get_uid[1] == get_uid[2] && get_uid[2] == get_uid[3]) {
process_infos[index].uid = get_uid[0] ;
}
else {
process_infos[index].uid = -1 ;
}
/* Getting process PPID from /proc/<pid>/status */
pid_t get_ppid ;
sscanf(strstr(get_status_file_content,"PPid:"),"PPid: %d",&get_ppid) ;
process_infos[index].ppid = get_ppid ;
/* Getting the process group */
process_infos[index].pgrp = getpgid(pid) ;
process_infos[index].scheduling=sched_getscheduler(pid) ;
process_infos[index].priority=getpriority(PRIO_PROCESS, pid);
/* popen /proc/cpuinfo to parse it to get the number of processors */
char buffer_cpu[255] ;
if (sprintf(buffer_cpu,"cat /proc/%d/cpuinfo",pid) == 0) {
char *err_msg ;
err_msg=strerror(errno) ;
fprintf(stderr,"%s\n",err_msg) ;
exit(EXIT_FAILURE) ;
}
if ((fp=popen(buffer,"r")) == NULL) {
char *err_msg ;
err_msg=strerror(errno) ;
fprintf(stderr,"%s\n",err_msg) ;
exit(EXIT_FAILURE) ;
}
char *get_cpuinfo_file_content=malloc(4096) ; /* wc -c /proc/cpuinfo == 3376 */
if (fread(get_cpuinfo_file_content, (size_t) 4096, (size_t) 1, fp) < 0) {
/* print SUCCCES
char *err_msg ;
err_msg=strerror(errno) ;
fprintf(stderr,"%s\n",err_msg) ;
*/
}
pclose(fp) ;
free(get_status_file_content) ;
free(get_cpuinfo_file_content) ;
/* Getting the processors informations */
char *cpu_counter_ptr ;
int cpu_counter ;
cpu_set_t set ;
CPU_ZERO(&set) ;
sched_getaffinity(pid, sizeof(cpu_set_t),&set);
process_infos[index].cpu_use_count=CPU_COUNT(&set);
process_infos[index].cpu_1=CPU_ISSET(0,&set) ;
process_infos[index].cpu_2=CPU_ISSET(1,&set) ;
process_infos[index].cpu_3=CPU_ISSET(2,&set) ;
process_infos[index].cpu_4=CPU_ISSET(3,&set) ;
/* Getting struct group */
struct group *grp ;
grp=getgrgid(get_gid[0]);
process_infos[index].grp=grp ;
/* Getting struct passwd */
struct passwd *password ;
password=getpwuid(get_uid[0]);
process_infos[index].password=password ;
}
return ;
} |
Partager