Line 0
Link Here
|
|
|
1 |
--- Process.c.orig 2018-01-17 01:54:30.580537000 +0800 |
2 |
+++ Process.c 2018-01-17 03:18:28.502348000 +0800 |
3 |
@@ -18,6 +18,7 @@ |
4 |
#include <sys/resource.h> |
5 |
#include <sys/param.h> |
6 |
#include <sys/stat.h> |
7 |
+#include <sys/types.h> |
8 |
#include <unistd.h> |
9 |
#include <stdlib.h> |
10 |
#include <signal.h> |
11 |
@@ -171,6 +172,7 @@ |
12 |
|
13 |
#define As_Process(this_) ((ProcessClass*)((this_)->super.klass)) |
14 |
|
15 |
+#define Process_isChildOf(process_, pid_) (process_->tgid == pid_ || (process_->tgid == process_->pid && process_->ppid == pid_)) |
16 |
}*/ |
17 |
|
18 |
static int Process_getuid = -1; |
19 |
@@ -194,10 +196,10 @@ |
20 |
assert(digits < 20); |
21 |
for (int i = 0; Process_pidColumns[i].label; i++) { |
22 |
assert(i < 20); |
23 |
- snprintf(Process_titleBuffer[i], 20, "%*s ", digits, Process_pidColumns[i].label); |
24 |
+ xSnprintf(Process_titleBuffer[i], 20, "%*s ", digits, Process_pidColumns[i].label); |
25 |
Process_fields[Process_pidColumns[i].id].title = Process_titleBuffer[i]; |
26 |
} |
27 |
- sprintf(Process_pidFormat, "%%%dd ", digits); |
28 |
+ xSnprintf(Process_pidFormat, sizeof(Process_pidFormat), "%%%dd ", digits); |
29 |
} |
30 |
|
31 |
void Process_humanNumber(RichString* str, unsigned long number, bool coloring) { |
32 |
@@ -261,13 +263,16 @@ |
33 |
processShadowColor = CRT_colors[PROCESS]; |
34 |
} |
35 |
|
36 |
- if (number > 10000000000) { |
37 |
- snprintf(buffer, 13, "%11lld ", number / 1000); |
38 |
+ if ((long long) number == -1LL) { |
39 |
+ int len = snprintf(buffer, 13, " no perm "); |
40 |
+ RichString_appendn(str, CRT_colors[PROCESS_SHADOW], buffer, len); |
41 |
+ } else if (number > 10000000000) { |
42 |
+ xSnprintf(buffer, 13, "%11lld ", number / 1000); |
43 |
RichString_appendn(str, largeNumberColor, buffer, 5); |
44 |
RichString_appendn(str, processMegabytesColor, buffer+5, 3); |
45 |
RichString_appendn(str, processColor, buffer+8, 4); |
46 |
} else { |
47 |
- snprintf(buffer, 13, "%11llu ", number); |
48 |
+ xSnprintf(buffer, 13, "%11llu ", number); |
49 |
RichString_appendn(str, largeNumberColor, buffer, 2); |
50 |
RichString_appendn(str, processMegabytesColor, buffer+2, 3); |
51 |
RichString_appendn(str, processColor, buffer+5, 3); |
52 |
@@ -284,15 +289,15 @@ |
53 |
int hundredths = totalHundredths - (totalSeconds * 100); |
54 |
char buffer[11]; |
55 |
if (hours >= 100) { |
56 |
- snprintf(buffer, 10, "%7lluh ", hours); |
57 |
+ xSnprintf(buffer, 10, "%7lluh ", hours); |
58 |
RichString_append(str, CRT_colors[LARGE_NUMBER], buffer); |
59 |
} else { |
60 |
if (hours) { |
61 |
- snprintf(buffer, 10, "%2lluh", hours); |
62 |
+ xSnprintf(buffer, 10, "%2lluh", hours); |
63 |
RichString_append(str, CRT_colors[LARGE_NUMBER], buffer); |
64 |
- snprintf(buffer, 10, "%02d:%02d ", minutes, seconds); |
65 |
+ xSnprintf(buffer, 10, "%02d:%02d ", minutes, seconds); |
66 |
} else { |
67 |
- snprintf(buffer, 10, "%2d:%02d.%02d ", minutes, seconds, hundredths); |
68 |
+ xSnprintf(buffer, 10, "%2d:%02d.%02d ", minutes, seconds, hundredths); |
69 |
} |
70 |
RichString_append(str, CRT_colors[DEFAULT_COLOR], buffer); |
71 |
} |
72 |
@@ -364,19 +369,19 @@ |
73 |
switch (field) { |
74 |
case PERCENT_CPU: { |
75 |
if (this->percent_cpu > 999.9) { |
76 |
- snprintf(buffer, n, "%4d ", (unsigned int)this->percent_cpu); |
77 |
+ xSnprintf(buffer, n, "%4d ", (unsigned int)this->percent_cpu); |
78 |
} else if (this->percent_cpu > 99.9) { |
79 |
- snprintf(buffer, n, "%3d. ", (unsigned int)this->percent_cpu); |
80 |
+ xSnprintf(buffer, n, "%3d. ", (unsigned int)this->percent_cpu); |
81 |
} else { |
82 |
- snprintf(buffer, n, "%4.1f ", this->percent_cpu); |
83 |
+ xSnprintf(buffer, n, "%4.1f ", this->percent_cpu); |
84 |
} |
85 |
break; |
86 |
} |
87 |
case PERCENT_MEM: { |
88 |
if (this->percent_mem > 99.9) { |
89 |
- snprintf(buffer, n, "100. "); |
90 |
+ xSnprintf(buffer, n, "100. "); |
91 |
} else { |
92 |
- snprintf(buffer, n, "%4.1f ", this->percent_mem); |
93 |
+ xSnprintf(buffer, n, "%4.1f ", this->percent_mem); |
94 |
} |
95 |
break; |
96 |
} |
97 |
@@ -407,7 +412,7 @@ |
98 |
n -= written; |
99 |
} |
100 |
const char* draw = CRT_treeStr[lastItem ? (this->settings->direction == 1 ? TREE_STR_BEND : TREE_STR_TEND) : TREE_STR_RTEE]; |
101 |
- snprintf(buf, n, "%s%s ", draw, this->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] ); |
102 |
+ xSnprintf(buf, n, "%s%s ", draw, this->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] ); |
103 |
RichString_append(str, CRT_colors[PROCESS_TREE], buffer); |
104 |
Process_writeCommand(this, attr, baseattr, str); |
105 |
return; |
106 |
@@ -418,28 +423,28 @@ |
107 |
case M_RESIDENT: Process_humanNumber(str, this->m_resident * PAGE_SIZE_KB, coloring); return; |
108 |
case M_SIZE: Process_humanNumber(str, this->m_size * PAGE_SIZE_KB, coloring); return; |
109 |
case NICE: { |
110 |
- snprintf(buffer, n, "%3ld ", this->nice); |
111 |
+ xSnprintf(buffer, n, "%3ld ", this->nice); |
112 |
attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY] |
113 |
: this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY] |
114 |
: attr; |
115 |
break; |
116 |
} |
117 |
- case NLWP: snprintf(buffer, n, "%4ld ", this->nlwp); break; |
118 |
- case PGRP: snprintf(buffer, n, Process_pidFormat, this->pgrp); break; |
119 |
- case PID: snprintf(buffer, n, Process_pidFormat, this->pid); break; |
120 |
- case PPID: snprintf(buffer, n, Process_pidFormat, this->ppid); break; |
121 |
+ case NLWP: xSnprintf(buffer, n, "%4ld ", this->nlwp); break; |
122 |
+ case PGRP: xSnprintf(buffer, n, Process_pidFormat, this->pgrp); break; |
123 |
+ case PID: xSnprintf(buffer, n, Process_pidFormat, this->pid); break; |
124 |
+ case PPID: xSnprintf(buffer, n, Process_pidFormat, this->ppid); break; |
125 |
case PRIORITY: { |
126 |
- if(this->priority == -100) |
127 |
- snprintf(buffer, n, " RT "); |
128 |
+ if(this->priority <= -100) |
129 |
+ xSnprintf(buffer, n, " RT "); |
130 |
else |
131 |
- snprintf(buffer, n, "%3ld ", this->priority); |
132 |
+ xSnprintf(buffer, n, "%3ld ", this->priority); |
133 |
break; |
134 |
} |
135 |
- case PROCESSOR: snprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, this->processor)); break; |
136 |
- case SESSION: snprintf(buffer, n, Process_pidFormat, this->session); break; |
137 |
- case STARTTIME: snprintf(buffer, n, "%s", this->starttime_show); break; |
138 |
+ case PROCESSOR: xSnprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, this->processor)); break; |
139 |
+ case SESSION: xSnprintf(buffer, n, Process_pidFormat, this->session); break; |
140 |
+ case STARTTIME: xSnprintf(buffer, n, "%s", this->starttime_show); break; |
141 |
case STATE: { |
142 |
- snprintf(buffer, n, "%c ", this->state); |
143 |
+ xSnprintf(buffer, n, "%c ", this->state); |
144 |
switch(this->state) { |
145 |
case 'R': |
146 |
attr = CRT_colors[PROCESS_R_STATE]; |
147 |
@@ -450,18 +455,18 @@ |
148 |
} |
149 |
break; |
150 |
} |
151 |
- case ST_UID: snprintf(buffer, n, "%4d ", this->st_uid); break; |
152 |
+ case ST_UID: xSnprintf(buffer, n, "%4d ", this->st_uid); break; |
153 |
case TIME: Process_printTime(str, this->time); return; |
154 |
- case TGID: snprintf(buffer, n, Process_pidFormat, this->tgid); break; |
155 |
- case TPGID: snprintf(buffer, n, Process_pidFormat, this->tpgid); break; |
156 |
- case TTY_NR: snprintf(buffer, n, "%5u ", this->tty_nr); break; |
157 |
+ case TGID: xSnprintf(buffer, n, Process_pidFormat, this->tgid); break; |
158 |
+ case TPGID: xSnprintf(buffer, n, Process_pidFormat, this->tpgid); break; |
159 |
+ case TTY_NR: xSnprintf(buffer, n, "%3u:%3u ", major(this->tty_nr), minor(this->tty_nr)); break; |
160 |
case USER: { |
161 |
if (Process_getuid != (int) this->st_uid) |
162 |
attr = CRT_colors[PROCESS_SHADOW]; |
163 |
if (this->user) { |
164 |
- snprintf(buffer, n, "%-9s ", this->user); |
165 |
+ xSnprintf(buffer, n, "%-9s ", this->user); |
166 |
} else { |
167 |
- snprintf(buffer, n, "%-9d ", this->st_uid); |
168 |
+ xSnprintf(buffer, n, "%-9d ", this->st_uid); |
169 |
} |
170 |
if (buffer[9] != '\0') { |
171 |
buffer[9] = ' '; |
172 |
@@ -470,7 +475,7 @@ |
173 |
break; |
174 |
} |
175 |
default: |
176 |
- snprintf(buffer, n, "- "); |
177 |
+ xSnprintf(buffer, n, "- "); |
178 |
} |
179 |
RichString_append(str, attr, buffer); |
180 |
} |
181 |
@@ -518,11 +523,10 @@ |
182 |
} |
183 |
|
184 |
bool Process_setPriority(Process* this, int priority) { |
185 |
- uid_t euid = geteuid(); |
186 |
- (void) seteuid(getuid()); |
187 |
+ CRT_dropPrivileges(); |
188 |
int old_prio = getpriority(PRIO_PROCESS, this->pid); |
189 |
int err = setpriority(PRIO_PROCESS, this->pid, priority); |
190 |
- (void) seteuid(euid); |
191 |
+ CRT_restorePrivileges(); |
192 |
if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) { |
193 |
this->nice = priority; |
194 |
} |
195 |
@@ -534,10 +538,9 @@ |
196 |
} |
197 |
|
198 |
void Process_sendSignal(Process* this, size_t sgn) { |
199 |
- uid_t euid = geteuid(); |
200 |
- (void) seteuid(getuid()); |
201 |
+ CRT_dropPrivileges(); |
202 |
kill(this->pid, (int) sgn); |
203 |
- (void) seteuid(euid); |
204 |
+ CRT_restorePrivileges(); |
205 |
} |
206 |
|
207 |
long Process_pidCompare(const void* v1, const void* v2) { |