xref: /freebsd/usr.bin/procstat/procstat_files.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 2007 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
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
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$
27  */
28 
29 #include <sys/param.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>
38 
39 #include <err.h>
40 #include <libprocstat.h>
41 #include <inttypes.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "procstat.h"
47 
48 static const char *
49 protocol_to_string(int domain, int type, int protocol)
50 {
51 
52 	switch (domain) {
53 	case AF_INET:
54 	case AF_INET6:
55 		switch (protocol) {
56 		case IPPROTO_TCP:
57 			return ("TCP");
58 		case IPPROTO_UDP:
59 			return ("UDP");
60 		case IPPROTO_ICMP:
61 			return ("ICM");
62 		case IPPROTO_RAW:
63 			return ("RAW");
64 		case IPPROTO_SCTP:
65 			return ("SCT");
66 		case IPPROTO_DIVERT:
67 			return ("IPD");
68 		default:
69 			return ("IP?");
70 		}
71 
72 	case AF_LOCAL:
73 		switch (type) {
74 		case SOCK_STREAM:
75 			return ("UDS");
76 		case SOCK_DGRAM:
77 			return ("UDD");
78 		default:
79 			return ("UD?");
80 		}
81 	default:
82 		return ("?");
83 	}
84 }
85 
86 static void
87 addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen)
88 {
89 	char buffer2[INET6_ADDRSTRLEN];
90 	struct sockaddr_in6 *sin6;
91 	struct sockaddr_in *sin;
92 	struct sockaddr_un *sun;
93 
94 	switch (ss->ss_family) {
95 	case AF_LOCAL:
96 		sun = (struct sockaddr_un *)ss;
97 		if (strlen(sun->sun_path) == 0)
98 			strlcpy(buffer, "-", buflen);
99 		else
100 			strlcpy(buffer, sun->sun_path, buflen);
101 		break;
102 
103 	case AF_INET:
104 		sin = (struct sockaddr_in *)ss;
105 		snprintf(buffer, buflen, "%s:%d", inet_ntoa(sin->sin_addr),
106 		    ntohs(sin->sin_port));
107 		break;
108 
109 	case AF_INET6:
110 		sin6 = (struct sockaddr_in6 *)ss;
111 		if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2,
112 		    sizeof(buffer2)) != NULL)
113 			snprintf(buffer, buflen, "%s.%d", buffer2,
114 			    ntohs(sin6->sin6_port));
115 		else
116 			strlcpy(buffer, "-", sizeof(buffer));
117 		break;
118 
119 	default:
120 		strlcpy(buffer, "", buflen);
121 		break;
122 	}
123 }
124 
125 static void
126 print_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 
134 void
135 procstat_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;
142 	int error;
143 
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 
149 	head = procstat_getfiles(procstat, kipp, 0);
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)
158 			printf(" cwd ");
159 		else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
160 			printf("jail ");
161 		else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
162 			printf("root ");
163 		else if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
164 			printf("text ");
165 		else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
166 			printf("trace ");
167 		else
168 			printf("%4d ", fst->fs_fd);
169 
170 		switch (fst->fs_type) {
171 		case PS_FST_TYPE_VNODE:
172 			str = "v";
173 			break;
174 
175 		case PS_FST_TYPE_SOCKET:
176 			str = "s";
177 			break;
178 
179 		case PS_FST_TYPE_PIPE:
180 			str = "p";
181 			break;
182 
183 		case PS_FST_TYPE_FIFO:
184 			str = "f";
185 			break;
186 
187 		case PS_FST_TYPE_KQUEUE:
188 			str = "k";
189 			break;
190 
191 		case PS_FST_TYPE_CRYPTO:
192 			str = "c";
193 			break;
194 
195 		case PS_FST_TYPE_MQUEUE:
196 			str = "m";
197 			break;
198 
199 		case PS_FST_TYPE_SHM:
200 			str = "h";
201 			break;
202 
203 		case PS_FST_TYPE_PTS:
204 			str = "t";
205 			break;
206 
207 		case PS_FST_TYPE_SEM:
208 			str = "e";
209 			break;
210 
211 		case PS_FST_TYPE_NONE:
212 		case PS_FST_TYPE_UNKNOWN:
213 		default:
214 			str = "?";
215 			break;
216 		}
217 		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;
225 
226 			case PS_FST_VTYPE_VDIR:
227 				str = "d";
228 				break;
229 
230 			case PS_FST_VTYPE_VBLK:
231 				str = "b";
232 				break;
233 
234 			case PS_FST_VTYPE_VCHR:
235 				str = "c";
236 				break;
237 
238 			case PS_FST_VTYPE_VLNK:
239 				str = "l";
240 				break;
241 
242 			case PS_FST_VTYPE_VSOCK:
243 				str = "s";
244 				break;
245 
246 			case PS_FST_VTYPE_VFIFO:
247 				str = "f";
248 				break;
249 
250 			case PS_FST_VTYPE_VBAD:
251 				str = "x";
252 				break;
253 
254 			case PS_FST_VTYPE_VNON:
255 			case PS_FST_VTYPE_UNKNOWN:
256 			default:
257 				str = "?";
258 				break;
259 			}
260 		}
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" : "-");
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 
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 
287 		case PS_FST_TYPE_SOCKET:
288 			error = procstat_get_socket_info(procstat, fst, &sock, NULL);
289 			if (error != 0)
290 				break;
291 			printf("%-3s ",
292 			    protocol_to_string(sock.dom_family,
293 			    sock.type, sock.proto));
294 			/*
295 			 * While generally we like to print two addresses,
296 			 * local and peer, for sockets, it turns out to be
297 			 * more useful to print the first non-nul address for
298 			 * local sockets, as typically they aren't bound and
299 			 *  connected, and the path strings can get long.
300 			 */
301 			if (sock.dom_family == AF_LOCAL) {
302 				struct sockaddr_un *sun =
303 				    (struct sockaddr_un *)&sock.sa_local;
304 
305 				if (sun->sun_path[0] != 0)
306 					print_address(&sock.sa_local);
307 				else
308 					print_address(&sock.sa_peer);
309 			} else {
310 				print_address(&sock.sa_local);
311 				printf(" ");
312 				print_address(&sock.sa_peer);
313 			}
314 			break;
315 
316 		default:
317 			printf("%-3s ", "-");
318 			printf("%-18s", "-");
319 		}
320 
321 		printf("\n");
322 	}
323 }
324