Deleted Added
full compact
procstat_files.c (221807) procstat_files.c (224859)
1/*-
1/*-
2 * Copyright (c) 2007 Robert N. M. Watson
2 * Copyright (c) 2007-2011 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright

--- 7 unchanged lines hidden (view full) ---

18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright

--- 7 unchanged lines hidden (view full) ---

18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/usr.bin/procstat/procstat_files.c 221807 2011-05-12 10:11:39Z stas $
26 * $FreeBSD: head/usr.bin/procstat/procstat_files.c 224859 2011-08-14 00:42:09Z rwatson $
27 */
28
29#include <sys/param.h>
27 */
28
29#include <sys/param.h>
30#include <sys/capability.h>
30#include <sys/socket.h>
31#include <sys/sysctl.h>
32#include <sys/un.h>
33#include <sys/user.h>
34
35#include <netinet/in.h>
36
37#include <arpa/inet.h>

--- 88 unchanged lines hidden (view full) ---

126print_address(struct sockaddr_storage *ss)
127{
128 char addr[PATH_MAX];
129
130 addr_to_string(ss, addr, sizeof(addr));
131 printf("%s", addr);
132}
133
31#include <sys/socket.h>
32#include <sys/sysctl.h>
33#include <sys/un.h>
34#include <sys/user.h>
35
36#include <netinet/in.h>
37
38#include <arpa/inet.h>

--- 88 unchanged lines hidden (view full) ---

127print_address(struct sockaddr_storage *ss)
128{
129 char addr[PATH_MAX];
130
131 addr_to_string(ss, addr, sizeof(addr));
132 printf("%s", addr);
133}
134
135static struct cap_desc {
136 cap_rights_t cd_right;
137 const char *cd_desc;
138} cap_desc[] = {
139 /* General file I/O. */
140 { CAP_READ, "rd" },
141 { CAP_WRITE, "wr" },
142 { CAP_MMAP, "mm" },
143 { CAP_MAPEXEC, "me" },
144 { CAP_FEXECVE, "fe" },
145 { CAP_FSYNC, "fy" },
146 { CAP_FTRUNCATE, "ft" },
147 { CAP_SEEK, "se" },
148
149 /* VFS methods. */
150 { CAP_FCHFLAGS, "cf" },
151 { CAP_FCHDIR, "cd" },
152 { CAP_FCHMOD, "cm" },
153 { CAP_FCHOWN, "cn" },
154 { CAP_FCNTL, "fc" },
155 { CAP_FPATHCONF, "fp" },
156 { CAP_FLOCK, "fl" },
157 { CAP_FSCK, "fk" },
158 { CAP_FSTAT, "fs" },
159 { CAP_FSTATFS, "sf" },
160 { CAP_FUTIMES, "fu" },
161 { CAP_CREATE, "cr" },
162 { CAP_DELETE, "de" },
163 { CAP_MKDIR, "md" },
164 { CAP_RMDIR, "rm" },
165 { CAP_MKFIFO, "mf" },
166
167 /* Lookups - used to constraint *at() calls. */
168 { CAP_LOOKUP, "lo" },
169
170 /* Extended attributes. */
171 { CAP_EXTATTR_GET, "eg" },
172 { CAP_EXTATTR_SET, "es" },
173 { CAP_EXTATTR_DELETE, "ed" },
174 { CAP_EXTATTR_LIST, "el" },
175
176 /* Access Control Lists. */
177 { CAP_ACL_GET, "ag" },
178 { CAP_ACL_SET, "as" },
179 { CAP_ACL_DELETE, "ad" },
180 { CAP_ACL_CHECK, "ac" },
181
182 /* Socket operations. */
183 { CAP_ACCEPT, "at" },
184 { CAP_BIND, "bd" },
185 { CAP_CONNECT, "co" },
186 { CAP_GETPEERNAME, "pn" },
187 { CAP_GETSOCKNAME, "sn" },
188 { CAP_GETSOCKOPT, "gs" },
189 { CAP_LISTEN, "ln" },
190 { CAP_PEELOFF, "pf" },
191 { CAP_SETSOCKOPT, "ss" },
192 { CAP_SHUTDOWN, "sh" },
193
194 /* Mandatory Access Control. */
195 { CAP_MAC_GET, "mg" },
196 { CAP_MAC_SET, "ms" },
197
198 /* Methods on semaphores. */
199 { CAP_SEM_GETVALUE, "sg" },
200 { CAP_SEM_POST, "sp" },
201 { CAP_SEM_WAIT, "sw" },
202
203 /* Event monitoring and posting. */
204 { CAP_POLL_EVENT, "po" },
205 { CAP_POST_EVENT, "ev" },
206
207 /* Strange and powerful rights that should not be given lightly. */
208 { CAP_IOCTL, "io" },
209 { CAP_TTYHOOK, "ty" },
210
211#ifdef NOTYET
212 { CAP_PDGETPID, "pg" },
213 { CAP_PDWAIT4, "pw" },
214 { CAP_PDKILL, "pk" },
215#endif
216};
217static const u_int cap_desc_count = sizeof(cap_desc) /
218 sizeof(cap_desc[0]);
219
220static u_int
221width_capability(cap_rights_t rights)
222{
223 u_int count, i, width;
224
225 count = 0;
226 width = 0;
227 for (i = 0; i < cap_desc_count; i++) {
228 if (rights & cap_desc[i].cd_right) {
229 width += strlen(cap_desc[i].cd_desc);
230 if (count)
231 width++;
232 count++;
233 }
234 }
235 return (width);
236}
237
238static void
239print_capability(cap_rights_t rights, u_int capwidth)
240{
241 u_int count, i, width;
242
243 count = 0;
244 width = 0;
245 for (i = width_capability(rights); i < capwidth; i++) {
246 if (rights || i != 0)
247 printf(" ");
248 else
249 printf("-");
250 }
251 for (i = 0; i < cap_desc_count; i++) {
252 if (rights & cap_desc[i].cd_right) {
253 printf("%s%s", count ? "," : "", cap_desc[i].cd_desc);
254 width += strlen(cap_desc[i].cd_desc);
255 if (count)
256 width++;
257 count++;
258 }
259 }
260}
261
134void
135procstat_files(struct procstat *procstat, struct kinfo_proc *kipp)
136{
137 struct sockstat sock;
138 struct filestat_list *head;
139 struct filestat *fst;
140 const char *str;
141 struct vnstat vn;
262void
263procstat_files(struct procstat *procstat, struct kinfo_proc *kipp)
264{
265 struct sockstat sock;
266 struct filestat_list *head;
267 struct filestat *fst;
268 const char *str;
269 struct vnstat vn;
270 u_int capwidth, width;
142 int error;
143
271 int error;
272
144 if (!hflag)
145 printf("%5s %-16s %4s %1s %1s %-8s %3s %7s %-3s %-12s\n",
146 "PID", "COMM", "FD", "T", "V", "FLAGS", "REF", "OFFSET",
147 "PRO", "NAME");
148
273 /*
274 * To print the header in capability mode, we need to know the width
275 * of the widest capability string. Even if we get no processes
276 * back, we will print the header, so we defer aborting due to a lack
277 * of processes until after the header logic.
278 */
279 capwidth = 0;
149 head = procstat_getfiles(procstat, kipp, 0);
280 head = procstat_getfiles(procstat, kipp, 0);
281 if (head != NULL && Cflag) {
282 STAILQ_FOREACH(fst, head, next) {
283 width = width_capability(fst->fs_cap_rights);
284 if (width > capwidth)
285 capwidth = width;
286 }
287 if (capwidth < strlen("CAPABILITIES"))
288 capwidth = strlen("CAPABILITIES");
289 }
290
291 if (!hflag) {
292 if (Cflag)
293 printf("%5s %-16s %4s %1s %-9s %-*s "
294 "%-3s %-12s\n", "PID", "COMM", "FD", "T",
295 "FLAGS", capwidth, "CAPABILITIES", "PRO",
296 "NAME");
297 else
298 printf("%5s %-16s %4s %1s %1s %-9s "
299 "%3s %7s %-3s %-12s\n", "PID", "COMM", "FD", "T",
300 "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME");
301 }
302
150 if (head == NULL)
151 return;
152 STAILQ_FOREACH(fst, head, next) {
153 printf("%5d ", kipp->ki_pid);
154 printf("%-16s ", kipp->ki_comm);
155 if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
156 printf("ctty ");
157 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)

--- 52 unchanged lines hidden (view full) ---

210
211 case PS_FST_TYPE_NONE:
212 case PS_FST_TYPE_UNKNOWN:
213 default:
214 str = "?";
215 break;
216 }
217 printf("%1s ", str);
303 if (head == NULL)
304 return;
305 STAILQ_FOREACH(fst, head, next) {
306 printf("%5d ", kipp->ki_pid);
307 printf("%-16s ", kipp->ki_comm);
308 if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
309 printf("ctty ");
310 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)

--- 52 unchanged lines hidden (view full) ---

363
364 case PS_FST_TYPE_NONE:
365 case PS_FST_TYPE_UNKNOWN:
366 default:
367 str = "?";
368 break;
369 }
370 printf("%1s ", str);
218 str = "-";
219 if (fst->fs_type == PS_FST_TYPE_VNODE) {
220 error = procstat_get_vnode_info(procstat, fst, &vn, NULL);
221 switch (vn.vn_type) {
222 case PS_FST_VTYPE_VREG:
223 str = "r";
224 break;
371 if (!Cflag) {
372 str = "-";
373 if (fst->fs_type == PS_FST_TYPE_VNODE) {
374 error = procstat_get_vnode_info(procstat, fst,
375 &vn, NULL);
376 switch (vn.vn_type) {
377 case PS_FST_VTYPE_VREG:
378 str = "r";
379 break;
225
380
226 case PS_FST_VTYPE_VDIR:
227 str = "d";
228 break;
381 case PS_FST_VTYPE_VDIR:
382 str = "d";
383 break;
229
384
230 case PS_FST_VTYPE_VBLK:
231 str = "b";
232 break;
385 case PS_FST_VTYPE_VBLK:
386 str = "b";
387 break;
233
388
234 case PS_FST_VTYPE_VCHR:
235 str = "c";
236 break;
389 case PS_FST_VTYPE_VCHR:
390 str = "c";
391 break;
237
392
238 case PS_FST_VTYPE_VLNK:
239 str = "l";
240 break;
393 case PS_FST_VTYPE_VLNK:
394 str = "l";
395 break;
241
396
242 case PS_FST_VTYPE_VSOCK:
243 str = "s";
244 break;
397 case PS_FST_VTYPE_VSOCK:
398 str = "s";
399 break;
245
400
246 case PS_FST_VTYPE_VFIFO:
247 str = "f";
248 break;
401 case PS_FST_VTYPE_VFIFO:
402 str = "f";
403 break;
249
404
250 case PS_FST_VTYPE_VBAD:
251 str = "x";
252 break;
405 case PS_FST_VTYPE_VBAD:
406 str = "x";
407 break;
253
408
254 case PS_FST_VTYPE_VNON:
255 case PS_FST_VTYPE_UNKNOWN:
256 default:
257 str = "?";
258 break;
409 case PS_FST_VTYPE_VNON:
410 case PS_FST_VTYPE_UNKNOWN:
411 default:
412 str = "?";
413 break;
414 }
259 }
415 }
416 printf("%1s ", str);
260 }
417 }
261 printf("%1s ", str);
262 printf("%s", fst->fs_fflags & PS_FST_FFLAG_READ ? "r" : "-");
263 printf("%s", fst->fs_fflags & PS_FST_FFLAG_WRITE ? "w" : "-");
264 printf("%s", fst->fs_fflags & PS_FST_FFLAG_APPEND ? "a" : "-");
265 printf("%s", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? "s" : "-");
266 printf("%s", fst->fs_fflags & PS_FST_FFLAG_SYNC ? "f" : "-");
267 printf("%s", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? "n" : "-");
268 printf("%s", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? "d" : "-");
418 printf("%s", fst->fs_fflags & PS_FST_FFLAG_READ ? "r" : "-");
419 printf("%s", fst->fs_fflags & PS_FST_FFLAG_WRITE ? "w" : "-");
420 printf("%s", fst->fs_fflags & PS_FST_FFLAG_APPEND ? "a" : "-");
421 printf("%s", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? "s" : "-");
422 printf("%s", fst->fs_fflags & PS_FST_FFLAG_SYNC ? "f" : "-");
423 printf("%s", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? "n" : "-");
424 printf("%s", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? "d" : "-");
269 printf("%s ", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? "l" : "-");
270 if (fst->fs_ref_count > -1)
271 printf("%3d ", fst->fs_ref_count);
272 else
273 printf("%3c ", '-');
274 if (fst->fs_offset > -1)
275 printf("%7jd ", (intmax_t)fst->fs_offset);
276 else
277 printf("%7c ", '-');
278
425 printf("%s", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? "l" : "-");
426 printf("%s ", fst->fs_fflags & PS_FST_FFLAG_CAPABILITY ?
427 "c" : "-");
428 if (!Cflag) {
429 if (fst->fs_ref_count > -1)
430 printf("%3d ", fst->fs_ref_count);
431 else
432 printf("%3c ", '-');
433 if (fst->fs_offset > -1)
434 printf("%7jd ", (intmax_t)fst->fs_offset);
435 else
436 printf("%7c ", '-');
437 }
438 if (Cflag) {
439 print_capability(fst->fs_cap_rights, capwidth);
440 printf(" ");
441 }
279 switch (fst->fs_type) {
280 case PS_FST_TYPE_VNODE:
281 case PS_FST_TYPE_FIFO:
282 case PS_FST_TYPE_PTS:
283 printf("%-3s ", "-");
284 printf("%-18s", fst->fs_path != NULL ? fst->fs_path : "-");
285 break;
286

--- 22 unchanged lines hidden (view full) ---

309 } else {
310 print_address(&sock.sa_local);
311 printf(" ");
312 print_address(&sock.sa_peer);
313 }
314 break;
315
316 default:
442 switch (fst->fs_type) {
443 case PS_FST_TYPE_VNODE:
444 case PS_FST_TYPE_FIFO:
445 case PS_FST_TYPE_PTS:
446 printf("%-3s ", "-");
447 printf("%-18s", fst->fs_path != NULL ? fst->fs_path : "-");
448 break;
449

--- 22 unchanged lines hidden (view full) ---

472 } else {
473 print_address(&sock.sa_local);
474 printf(" ");
475 print_address(&sock.sa_peer);
476 }
477 break;
478
479 default:
317 printf("%-3s ", "-");
318 printf("%-18s", "-");
319 }
320
321 printf("\n");
322 }
323}
480 printf("%-18s", "-");
481 }
482
483 printf("\n");
484 }
485}