aboutsummaryrefslogtreecommitdiffstats
path: root/src/firemon/top.c
diff options
context:
space:
mode:
authorLibravatar Fred Barclay <Fred-Barclay@users.noreply.github.com>2017-05-24 14:13:52 -0500
committerLibravatar Fred Barclay <Fred-Barclay@users.noreply.github.com>2017-05-24 14:13:52 -0500
commit96c920e166b40bbe50f216e294f2efac154a1cb2 (patch)
treefa80a34e81863ab897f2f2b8ec4124b10d023516 /src/firemon/top.c
parentremove trailing whitespace from etc/ (diff)
downloadfirejail-96c920e166b40bbe50f216e294f2efac154a1cb2.tar.gz
firejail-96c920e166b40bbe50f216e294f2efac154a1cb2.tar.zst
firejail-96c920e166b40bbe50f216e294f2efac154a1cb2.zip
Remove trailing whitespace from src/
Diffstat (limited to 'src/firemon/top.c')
-rw-r--r--src/firemon/top.c55
1 files changed, 27 insertions, 28 deletions
diff --git a/src/firemon/top.c b/src/firemon/top.c
index 081f04eba..fc6e6289e 100644
--- a/src/firemon/top.c
+++ b/src/firemon/top.c
@@ -23,7 +23,7 @@
23#include <sys/types.h> 23#include <sys/types.h>
24#include <sys/stat.h> 24#include <sys/stat.h>
25#include <unistd.h> 25#include <unistd.h>
26 26
27static unsigned pgs_rss = 0; 27static unsigned pgs_rss = 0;
28static unsigned pgs_shared = 0; 28static unsigned pgs_shared = 0;
29static unsigned clocktick = 0; 29static unsigned clocktick = 0;
@@ -40,7 +40,7 @@ static char *get_user_name(uid_t uid) {
40 } 40 }
41 else if (uid == cached_uid) 41 else if (uid == cached_uid)
42 return strdup(cached_user_name); 42 return strdup(cached_user_name);
43 else 43 else
44 return pid_get_user_name(uid); 44 return pid_get_user_name(uid);
45} 45}
46 46
@@ -49,7 +49,7 @@ static char *get_header(void) {
49 if (asprintf(&rv, "%-5.5s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s", 49 if (asprintf(&rv, "%-5.5s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s",
50 "PID", "User", "RES(KiB)", "SHR(KiB)", "CPU%", "Prcs", "Uptime", "Command") == -1) 50 "PID", "User", "RES(KiB)", "SHR(KiB)", "CPU%", "Prcs", "Uptime", "Command") == -1)
51 errExit("asprintf"); 51 errExit("asprintf");
52 52
53 return rv; 53 return rv;
54} 54}
55 55
@@ -66,7 +66,7 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne
66 struct stat s; 66 struct stat s;
67 if (stat(procdir, &s) == -1) 67 if (stat(procdir, &s) == -1)
68 return NULL; 68 return NULL;
69 69
70 if (pids[index].level == 1) { 70 if (pids[index].level == 1) {
71 pgs_rss = 0; 71 pgs_rss = 0;
72 pgs_shared = 0; 72 pgs_shared = 0;
@@ -74,7 +74,7 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne
74 *stime = 0; 74 *stime = 0;
75 *cnt = 0; 75 *cnt = 0;
76 } 76 }
77 77
78 (*cnt)++; 78 (*cnt)++;
79 pid_getmem(index, &pgs_rss, &pgs_shared); 79 pid_getmem(index, &pgs_rss, &pgs_shared);
80 unsigned utmp; 80 unsigned utmp;
@@ -82,8 +82,8 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne
82 pid_get_cpu_time(index, &utmp, &stmp); 82 pid_get_cpu_time(index, &utmp, &stmp);
83 *utime += utmp; 83 *utime += utmp;
84 *stime += stmp; 84 *stime += stmp;
85 85
86 86
87 int i; 87 int i;
88 for (i = index + 1; i < max_pids; i++) { 88 for (i = index + 1; i < max_pids; i++) {
89 if (pids[i].parent == (pid_t)index) 89 if (pids[i].parent == (pid_t)index)
@@ -108,7 +108,7 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne
108 ptrcmd = cmd + 9; 108 ptrcmd = cmd + 9;
109 else 109 else
110 ptrcmd = cmd; 110 ptrcmd = cmd;
111 111
112 // user 112 // user
113 char *user = get_user_name(pids[index].uid); 113 char *user = get_user_name(pids[index].uid);
114 char *ptruser; 114 char *ptruser;
@@ -116,7 +116,7 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne
116 ptruser = user; 116 ptruser = user;
117 else 117 else
118 ptruser = ""; 118 ptruser = "";
119 119
120 // memory 120 // memory
121 if (pgsz == 0) 121 if (pgsz == 0)
122 pgsz = getpagesize(); 122 pgsz = getpagesize();
@@ -124,7 +124,7 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne
124 snprintf(rss, 10, "%u", pgs_rss * pgsz / 1024); 124 snprintf(rss, 10, "%u", pgs_rss * pgsz / 1024);
125 char shared[10]; 125 char shared[10];
126 snprintf(shared, 10, "%u", pgs_shared * pgsz / 1024); 126 snprintf(shared, 10, "%u", pgs_shared * pgsz / 1024);
127 127
128 // uptime 128 // uptime
129 unsigned long long uptime = pid_get_start_time(index); 129 unsigned long long uptime = pid_get_start_time(index);
130 if (clocktick == 0) 130 if (clocktick == 0)
@@ -140,7 +140,7 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne
140 unsigned hour = uptime; 140 unsigned hour = uptime;
141 char uptime_str[50]; 141 char uptime_str[50];
142 snprintf(uptime_str, 50, "%02u:%02u:%02u", hour, min, sec); 142 snprintf(uptime_str, 50, "%02u:%02u:%02u", hour, min, sec);
143 143
144 // cpu 144 // cpu
145 itv *= clocktick; 145 itv *= clocktick;
146 float ud = (float) (*utime - pids[index].utime) / itv * 100; 146 float ud = (float) (*utime - pids[index].utime) / itv * 100;
@@ -153,18 +153,18 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne
153 // process count 153 // process count
154 char prcs_str[10]; 154 char prcs_str[10];
155 snprintf(prcs_str, 10, "%d", *cnt); 155 snprintf(prcs_str, 10, "%d", *cnt);
156 156
157 if (asprintf(&rv, "%-5.5s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s", 157 if (asprintf(&rv, "%-5.5s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s",
158 pidstr, ptruser, rss, shared, cpu_str, prcs_str, uptime_str, ptrcmd) == -1) 158 pidstr, ptruser, rss, shared, cpu_str, prcs_str, uptime_str, ptrcmd) == -1)
159 errExit("asprintf"); 159 errExit("asprintf");
160 160
161 if (cmd) 161 if (cmd)
162 free(cmd); 162 free(cmd);
163 if (user) 163 if (user)
164 free(user); 164 free(user);
165 165
166 } 166 }
167 167
168 return rv; 168 return rv;
169} 169}
170 170
@@ -174,7 +174,7 @@ typedef struct node_t {
174 char *line; 174 char *line;
175 float cpu; 175 float cpu;
176} Node; 176} Node;
177 177
178static Node *head = NULL; 178static Node *head = NULL;
179 179
180static void head_clear(void) { 180static void head_clear(void) {
@@ -186,7 +186,7 @@ static void head_clear(void) {
186 free(ptr); 186 free(ptr);
187 ptr = next; 187 ptr = next;
188 } 188 }
189 189
190 head = NULL; 190 head = NULL;
191} 191}
192 192
@@ -198,14 +198,14 @@ static void head_add(float cpu, char *line) {
198 node->line = line; 198 node->line = line;
199 node->cpu = cpu; 199 node->cpu = cpu;
200 node->next = NULL; 200 node->next = NULL;
201 201
202 // insert in first list position 202 // insert in first list position
203 if (head == NULL || head->cpu < cpu) { 203 if (head == NULL || head->cpu < cpu) {
204 node->next = head; 204 node->next = head;
205 head = node; 205 head = node;
206 return; 206 return;
207 } 207 }
208 208
209 // insert in the right place 209 // insert in the right place
210 Node *ptr = head; 210 Node *ptr = head;
211 while (1) { 211 while (1) {
@@ -215,14 +215,14 @@ static void head_add(float cpu, char *line) {
215 ptr->next = node; 215 ptr->next = node;
216 return; 216 return;
217 } 217 }
218 218
219 // current position 219 // current position
220 if (current->cpu < cpu) { 220 if (current->cpu < cpu) {
221 ptr->next = node; 221 ptr->next = node;
222 node->next = current; 222 node->next = current;
223 return; 223 return;
224 } 224 }
225 225
226 ptr = current; 226 ptr = current;
227 } 227 }
228} 228}
@@ -233,10 +233,10 @@ void head_print(int col, int row) {
233 while (ptr) { 233 while (ptr) {
234 if (current >= row) 234 if (current >= row)
235 break; 235 break;
236 236
237 if (strlen(ptr->line) > (size_t)col) 237 if (strlen(ptr->line) > (size_t)col)
238 ptr->line[col] = '\0'; 238 ptr->line[col] = '\0';
239 239
240 if (ptr->next == NULL || current == (row - 1)) { 240 if (ptr->next == NULL || current == (row - 1)) {
241 printf("%s", ptr->line); 241 printf("%s", ptr->line);
242 fflush(0); 242 fflush(0);
@@ -253,7 +253,7 @@ void top(void) {
253 while (1) { 253 while (1) {
254 // clear linked list 254 // clear linked list
255 head_clear(); 255 head_clear();
256 256
257 // set pid table 257 // set pid table
258 int i; 258 int i;
259 int itv = 1; // 1 second interval 259 int itv = 1; // 1 second interval
@@ -266,10 +266,10 @@ void top(void) {
266 if (pids[i].level == 1) 266 if (pids[i].level == 1)
267 pid_store_cpu(i, 0, &utime, &stime); 267 pid_store_cpu(i, 0, &utime, &stime);
268 } 268 }
269 269
270 // wait 1 second 270 // wait 1 second
271 firemon_sleep(itv); 271 firemon_sleep(itv);
272 272
273 // grab screen size 273 // grab screen size
274 struct winsize sz; 274 struct winsize sz;
275 int row = 24; 275 int row = 24;
@@ -288,7 +288,7 @@ void top(void) {
288 if (row > 0) 288 if (row > 0)
289 row--; 289 row--;
290 free(header); 290 free(header);
291 291
292 // find system uptime 292 // find system uptime
293 FILE *fp = fopen("/proc/uptime", "r"); 293 FILE *fp = fopen("/proc/uptime", "r");
294 if (fp) { 294 if (fp) {
@@ -315,4 +315,3 @@ void top(void) {
315#endif 315#endif
316 } 316 }
317} 317}
318