/
newps.c
172 lines (159 loc) · 3.82 KB
/
newps.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <dirent.h>
#include <libgen.h>
#include <pwd.h>
#include <unistd.h>
int check_if_number (char *str)
{
int i;
for (i=0; str[i] != '\0'; i++)
{
if (!isdigit (str[i]))
{
return 0;
}
}
return 1;
}
#define MAX_BUF 1024
#define INT_SIZE_BUF 6
#define PID_LIST_BLOCK 32
#define UP_TIME_SIZE 10
const char *getUserName(int uid)
{
struct passwd *pw = getpwuid(uid);
if (pw)
{
return pw->pw_name;
}
return "";
}
void pidaux ()
{
DIR *dirp;
FILE *fp;
struct dirent *entry;
char path[MAX_BUF], read_buf[MAX_BUF],temp_buf[MAX_BUF];
char uid_int_str[INT_SIZE_BUF]={0},*line;
char uptime_str[UP_TIME_SIZE];
char *user,*command;
size_t len=0;
dirp = opendir ("/proc/");
if (dirp == NULL)
{
perror ("Fail");
exit(0);
}
strcpy(path,"/proc/");
strcat(path,"uptime");
fp=fopen(path,"r");
if(fp!=NULL)
{
getline(&line,&len,fp);
sscanf(line,"%s ",uptime_str);
}
long uptime=atof(uptime_str);
long Hertz=sysconf(_SC_CLK_TCK);
strcpy(path,"/proc/");
strcat(path,"meminfo");
fp=fopen(path,"r");
unsigned long long total_memory;
if(fp!=NULL)
{
getline(&line,&len,fp);
sscanf(line,"MemTotal: %llu kB",&total_memory);
}
while ((entry = readdir (dirp)) != NULL)
{
if (check_if_number (entry->d_name))
{
strcpy(path,"/proc/");
strcat(path,entry->d_name);
strcat(path,"/status");
unsigned long long memory_rss;
fp=fopen(path,"r");
unsigned long long vmsize;
if(fp!=NULL)
{
vmsize=0;
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
sscanf(line,"Uid: %s ",uid_int_str);
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
sscanf(line,"VmSize: %llu kB",&vmsize);
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
getline(&line,&len,fp);
sscanf(line,"VmRSS: %llu kB",&memory_rss);
}
else
{
fprintf(stdout,"FP is NULL\n");
}
float memory_usage=100*memory_rss/total_memory;
strcpy(path,"/proc/");
strcat(path,entry->d_name);
strcat(path,"/stat");
fp=fopen(path,"r");
getline(&line,&len,fp);
char comm[10],state;
unsigned int flags;
int pid,ppid,pgrp,session,tty_nr,tpgid;
unsigned long minflt,cminflt,majflt,cmajflt,utime,stime;
unsigned long long starttime;
long cutime,cstime,priority,nice,num_threads,itreavalue;
sscanf(line,"%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu",&pid,comm,&state,&ppid,&pgrp,&session,&tty_nr,&tpgid,&flags,&minflt,&cminflt,&majflt,&cmajflt,&utime,&stime,&cutime,&cstime,&priority,&nice,&num_threads,&itreavalue,&starttime);
unsigned long total_time=utime+stime;
total_time=total_time+(unsigned long)cutime+(unsigned long)cstime;
float seconds=uptime-(starttime/Hertz);
float cpu_usage=100*((total_time/Hertz)/seconds);
if(isnan(cpu_usage))
{
cpu_usage=0.0;
}
if(isnan(memory_usage))
{
memory_usage=0.0;
}
strcpy (path, "/proc/");
strcat (path, entry->d_name);
strcat (path, "/comm");
fp = fopen (path, "r");
if (fp != NULL)
{
fscanf (fp, "%s", read_buf);
fclose(fp);
}
char *userName=getUserName(atoi(uid_int_str));
if(strlen(userName)<9)
{
user=userName;
}
else
{
user=uid_int_str;
}
fprintf(stdout,"%s %s %0.1f %0.1f %llu %llu %c %s\n",user,entry->d_name,cpu_usage,memory_usage,vmsize,memory_rss,state,read_buf);
}
}
closedir (dirp);
}
int main (int argc, char *argv[])
{
pidaux();
return 0;
}