xref: /freebsd/usr.bin/procstat/procstat_files.c (revision 39beb93c)
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 <inttypes.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <libutil.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(pid_t pid, struct kinfo_proc *kipp)
136 {
137 	struct kinfo_file *freep, *kif;
138 	int i, cnt;
139 	const char *str;
140 
141 	if (!hflag)
142 		printf("%5s %-16s %4s %1s %1s %-8s %3s %7s %-3s %-12s\n",
143 		    "PID", "COMM", "FD", "T", "V", "FLAGS", "REF", "OFFSET",
144 		    "PRO", "NAME");
145 
146 	freep = kinfo_getfile(pid, &cnt);
147 	if (freep == NULL)
148 		return;
149 	for (i = 0; i < cnt; i++) {
150 		kif = &freep[i];
151 
152 		printf("%5d ", pid);
153 		printf("%-16s ", kipp->ki_comm);
154 		switch (kif->kf_fd) {
155 		case KF_FD_TYPE_CWD:
156 			printf(" cwd ");
157 			break;
158 
159 		case KF_FD_TYPE_ROOT:
160 			printf("root ");
161 			break;
162 
163 		case KF_FD_TYPE_JAIL:
164 			printf("jail ");
165 			break;
166 
167 		default:
168 			printf("%4d ", kif->kf_fd);
169 			break;
170 		}
171 		switch (kif->kf_type) {
172 		case KF_TYPE_VNODE:
173 			str = "v";
174 			break;
175 
176 		case KF_TYPE_SOCKET:
177 			str = "s";
178 			break;
179 
180 		case KF_TYPE_PIPE:
181 			str = "p";
182 			break;
183 
184 		case KF_TYPE_FIFO:
185 			str = "f";
186 			break;
187 
188 		case KF_TYPE_KQUEUE:
189 			str = "k";
190 			break;
191 
192 		case KF_TYPE_CRYPTO:
193 			str = "c";
194 			break;
195 
196 		case KF_TYPE_MQUEUE:
197 			str = "m";
198 			break;
199 
200 		case KF_TYPE_SHM:
201 			str = "h";
202 			break;
203 
204 		case KF_TYPE_PTS:
205 			str = "t";
206 			break;
207 
208 		case KF_TYPE_SEM:
209 			str = "e";
210 			break;
211 
212 		case KF_TYPE_NONE:
213 		case KF_TYPE_UNKNOWN:
214 		default:
215 			str = "?";
216 			break;
217 		}
218 		printf("%1s ", str);
219 		str = "-";
220 		if (kif->kf_type == KF_TYPE_VNODE) {
221 			switch (kif->kf_vnode_type) {
222 			case KF_VTYPE_VREG:
223 				str = "r";
224 				break;
225 
226 			case KF_VTYPE_VDIR:
227 				str = "d";
228 				break;
229 
230 			case KF_VTYPE_VBLK:
231 				str = "b";
232 				break;
233 
234 			case KF_VTYPE_VCHR:
235 				str = "c";
236 				break;
237 
238 			case KF_VTYPE_VLNK:
239 				str = "l";
240 				break;
241 
242 			case KF_VTYPE_VSOCK:
243 				str = "s";
244 				break;
245 
246 			case KF_VTYPE_VFIFO:
247 				str = "f";
248 				break;
249 
250 			case KF_VTYPE_VBAD:
251 				str = "x";
252 				break;
253 
254 			case KF_VTYPE_VNON:
255 			case KF_VTYPE_UNKNOWN:
256 			default:
257 				str = "?";
258 				break;
259 			}
260 		}
261 		printf("%1s ", str);
262 		printf("%s", kif->kf_flags & KF_FLAG_READ ? "r" : "-");
263 		printf("%s", kif->kf_flags & KF_FLAG_WRITE ? "w" : "-");
264 		printf("%s", kif->kf_flags & KF_FLAG_APPEND ? "a" : "-");
265 		printf("%s", kif->kf_flags & KF_FLAG_ASYNC ? "s" : "-");
266 		printf("%s", kif->kf_flags & KF_FLAG_FSYNC ? "f" : "-");
267 		printf("%s", kif->kf_flags & KF_FLAG_NONBLOCK ? "n" : "-");
268 		printf("%s", kif->kf_flags & KF_FLAG_DIRECT ? "d" : "-");
269 		printf("%s ", kif->kf_flags & KF_FLAG_HASLOCK ? "l" : "-");
270 		if (kif->kf_ref_count > -1)
271 			printf("%3d ", kif->kf_ref_count);
272 		else
273 			printf("%3c ", '-');
274 		if (kif->kf_offset > -1)
275 			printf("%7jd ", (intmax_t)kif->kf_offset);
276 		else
277 			printf("%7c ", '-');
278 
279 		switch (kif->kf_type) {
280 		case KF_TYPE_VNODE:
281 		case KF_TYPE_FIFO:
282 		case KF_TYPE_PTS:
283 			printf("%-3s ", "-");
284 			printf("%-18s", kif->kf_path);
285 			break;
286 
287 		case KF_TYPE_SOCKET:
288 			printf("%-3s ",
289 			    protocol_to_string(kif->kf_sock_domain,
290 			    kif->kf_sock_type, kif->kf_sock_protocol));
291 			/*
292 			 * While generally we like to print two addresses,
293 			 * local and peer, for sockets, it turns out to be
294 			 * more useful to print the first non-nul address for
295 			 * local sockets, as typically they aren't bound and
296 			 *  connected, and the path strings can get long.
297 			 */
298 			if (kif->kf_sock_domain == AF_LOCAL) {
299 				struct sockaddr_un *sun =
300 				    (struct sockaddr_un *)&kif->kf_sa_local;
301 
302 				if (sun->sun_path[0] != 0)
303 					print_address(&kif->kf_sa_local);
304 				else
305 					print_address(&kif->kf_sa_peer);
306 			} else {
307 				print_address(&kif->kf_sa_local);
308 				printf(" ");
309 				print_address(&kif->kf_sa_peer);
310 			}
311 			break;
312 
313 		default:
314 			printf("%-3s ", "-");
315 			printf("%-18s", "-");
316 		}
317 
318 		printf("\n");
319 	}
320 	free(freep);
321 }
322