summaryrefslogtreecommitdiff
path: root/usr/src/lib/libcmd/common/fds.c
blob: 0eb22bb1fd5eb4eb3e29b3058d83de96ea8a1e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1992-2010 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                                                                      *
***********************************************************************/
#pragma prototyped

static const char usage[] =
"[-?\n@(#)$Id: fds (AT&T Research) 2009-09-09 $\n]"
USAGE_LICENSE
"[+NAME?fds - list open file descriptor status]"
"[+DESCRIPTION?\bfds\b lists the status for each open file descriptor. "
    "When invoked as a shell builtin it accesses the file descriptors of the "
    "calling shell, otherwise it lists the file descriptors passed across "
    "\bexec\b(2).]"
"[l:long?List file descriptor details.]"
"[u:unit?Write output to \afd\a.]#[fd]"
"[+SEE ALSO?\blogname\b(1), \bwho\b(1), \bgetgroups\b(2), \bgetsockname\b(2), \bgetsockopts\b(2)]"
;

#include <cmd.h>
#include <ls.h>

#include "FEATURE/sockets"

#if defined(S_IFSOCK) && _sys_socket && _hdr_arpa_inet && _hdr_netinet_in && _lib_getsockname && _lib_getsockopt && _lib_inet_ntoa
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#undef	S_IFSOCK
#endif

#ifndef minor
#define minor(x)	(int)((x)&0xff)
#endif
#ifndef major
#define major(x)	(int)(((unsigned int)(x)>>8)&0xff)
#endif

#undef	getconf
#define getconf(x)	strtol(astconf(x,NiL,NiL),NiL,0)

#ifdef S_IFSOCK

typedef struct NV_s
{
	const char*	name;
	int		value;
} NV_t;

static const NV_t	family[] =
{
#ifdef AF_LOCAL
	"pipe",		AF_LOCAL,
#endif
#ifdef AF_UNIX
	"pipe",		AF_UNIX,
#endif
#ifdef AF_FILE
	"FILE",		AF_FILE,
#endif
#ifdef AF_INET
	"INET",		AF_INET,
#endif
#ifdef AF_AX25
	"AX25",		AF_AX25,
#endif
#ifdef AF_IPX
	"IPX",		AF_IPX,
#endif
#ifdef AF_APPLETALK
	"APPLETALK",	AF_APPLETALK,
#endif
#ifdef AF_NETROM
	"NETROM",	AF_NETROM,
#endif
#ifdef AF_BRIDGE
	"BRIDGE",	AF_BRIDGE,
#endif
#ifdef AF_ATMPVC
	"ATMPVC",	AF_ATMPVC,
#endif
#ifdef AF_X25
	"X25",		AF_X25,
#endif
#ifdef AF_INET6
	"INET6",	AF_INET6,
#endif
#ifdef AF_ROSE
	"ROSE",		AF_ROSE,
#endif
#ifdef AF_DECnet
	"DECnet",	AF_DECnet,
#endif
#ifdef AF_NETBEUI
	"NETBEUI",	AF_NETBEUI,
#endif
#ifdef AF_SECURITY
	"SECURITY",	AF_SECURITY,
#endif
#ifdef AF_KEY
	"KEY",		AF_KEY,
#endif
#ifdef AF_NETLINK
	"NETLINK",	AF_NETLINK,
#endif
#ifdef AF_ROUTE
	"ROUTE",	AF_ROUTE,
#endif
#ifdef AF_PACKET
	"PACKET",	AF_PACKET,
#endif
#ifdef AF_ASH
	"ASH",		AF_ASH,
#endif
#ifdef AF_ECONET
	"ECONET",	AF_ECONET,
#endif
#ifdef AF_ATMSVC
	"ATMSVC",	AF_ATMSVC,
#endif
#ifdef AF_SNA
	"SNA",		AF_SNA,
#endif
#ifdef AF_IRDA
	"IRDA",		AF_IRDA,
#endif
#ifdef AF_PPPOX
	"PPPOX",	AF_PPPOX,
#endif
#ifdef AF_WANPIPE
	"WANPIPE",	AF_WANPIPE,
#endif
#ifdef AF_BLUETOOTH
	"BLUETOOTH",	AF_BLUETOOTH,
#endif
	0
};

#endif

int
b_fds(int argc, char** argv, void* context)
{
	register char*		s;
	register int		i;
	register char*		m;
	register char*		x;
	int			flags;
	int			details;
	int			open_max;
	int			unit;
	Sfio_t*			sp;
	struct stat		st;
#ifdef S_IFSOCK
	struct sockaddr_in	addr;
	char*			a;
	unsigned char*		b;
	unsigned char*		e;
	socklen_t		addrlen;
	socklen_t		len;
	int			type;
	int			port;
	int			prot;
	char			num[64];
	char			fam[64];
#ifdef INET6_ADDRSTRLEN
	char			nam[256];
#endif
#endif

	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
	details = 0;
	unit = 1;
	for (;;)
	{
		switch (optget(argv, usage))
		{
		case 'l':
			details = opt_info.num;
			continue;
		case 'u':
			unit = opt_info.num;
			continue;
		case '?':
			error(ERROR_USAGE|4, "%s", opt_info.arg);
			continue;
		case ':':
			error(2, "%s", opt_info.arg);
			continue;
		}
		break;
	}
	argv += opt_info.index;
	if (error_info.errors || *argv)
		error(ERROR_USAGE|4, "%s", optusage(NiL));
	if ((open_max = getconf("OPEN_MAX")) <= 0)
		open_max = OPEN_MAX;
	if (unit == 1)
		sp = sfstdout;
	else if (fstat(unit, &st) || !(sp = sfnew(NiL, NiL, SF_UNBOUND, unit, SF_WRITE)))
		error(ERROR_SYSTEM|3, "%d: cannot write to file descriptor");
	for (i = 0; i <= open_max; i++)
	{
		if (fstat(i, &st))
		{
			/* not open */
			continue;
		}
		if (!details)
		{
			sfprintf(sp, "%d\n", i);
			continue;
		}
		if ((flags = fcntl(i, F_GETFL, (char*)0)) == -1)
			m = "--";
		else
			switch (flags & (O_RDONLY|O_WRONLY|O_RDWR))
			{
			case O_RDONLY:
				m = "r-";
				break;
			case O_WRONLY:
				m = "-w";
				break;
			case O_RDWR:
				m = "rw";
				break;
			default:
				m = "??";
				break;
			}
		x = (fcntl(i, F_GETFD, (char*)0) > 0) ? "x" : "-";
		if (isatty(i) && (s = ttyname(i)))
		{
			sfprintf(sp, "%02d %s%s %s %s\n", i, m, x, fmtmode(st.st_mode, 0), s);
			continue;
		}
#ifdef S_IFSOCK
		addrlen = sizeof(addr);
		memset(&addr, 0, addrlen);
		if (!getsockname(i, (struct sockaddr*)&addr, (void*)&addrlen))
		{
			type = 0;
			prot = 0;
#ifdef SO_TYPE
			len = sizeof(type);
			if (getsockopt(i, SOL_SOCKET, SO_TYPE, (void*)&type, (void*)&len))
				type = -1;
#endif
#ifdef SO_PROTOTYPE
			len = sizeof(prot);
			if (getsockopt(i, SOL_SOCKET, SO_PROTOTYPE, (void*)&prot, (void*)&len))
				prot = -1;
#endif
			if (!st.st_mode)
				st.st_mode = S_IFSOCK|S_IRUSR|S_IWUSR;
			s = 0;
			switch (type)
			{
			case SOCK_DGRAM:
				switch (addr.sin_family)
				{
				case AF_INET:
#ifdef AF_INET6
				case AF_INET6:
#endif
					s = "udp";
					break;
				}
				break;
			case SOCK_STREAM:
				switch (addr.sin_family)
				{
				case AF_INET:
#ifdef AF_INET6
				case AF_INET6:
#endif
#ifdef IPPROTO_SCTP
					if (prot == IPPROTO_SCTP)
						s = "sctp";
					else
#endif
						s = "tcp";
					break;
				}
				break;
#ifdef SOCK_RAW
			case SOCK_RAW:
				s = "raw";
				break;
#endif
#ifdef SOCK_RDM
			case SOCK_RDM:
				s = "rdm";
				break;
#endif
#ifdef SOCK_SEQPACKET
			case SOCK_SEQPACKET:
				s = "seqpacket";
				break;
#endif
			}
			if (!s)
			{
				for (type = 0; family[type].name && family[type].value != addr.sin_family; type++);
				if (!(s = (char*)family[type].name))
					sfsprintf(s = num, sizeof(num), "family.%d", addr.sin_family);
			}
			port = 0;
#ifdef INET6_ADDRSTRLEN
			if (a = (char*)inet_ntop(addr.sin_family, &addr.sin_addr, nam, sizeof(nam)))
				port = ntohs(addr.sin_port);
			else
#endif
			if (addr.sin_family == AF_INET)
			{
				a = inet_ntoa(addr.sin_addr);
				port = ntohs(addr.sin_port);
			}
			else
			{
				a = fam;
				e = (b = (unsigned char*)&addr) + addrlen;
				while (b < e && a < &fam[sizeof(fam)-1])
					a += sfsprintf(a, &fam[sizeof(fam)] - a - 1, ".%d", *b++);
				a = a == fam ? "0" : fam + 1;
			}
			if (port)
				sfprintf(sp, "%02d %s%s %s /dev/%s/%s/%d\n", i, m, x, fmtmode(st.st_mode, 0), s, a, port);
			else
				sfprintf(sp, "%02d %s%s %s /dev/%s/%s\n", i, m, x, fmtmode(st.st_mode, 0), s, a);
			continue;
		}
#endif
		sfprintf(sp, "%02d %s%s %s /dev/inode/%u/%u\n", i, m, x, fmtmode(st.st_mode, 0), st.st_dev, st.st_ino);
	}
	if (sp != sfstdout)
	{
		sfsetfd(sp, -1);
		sfclose(sp);
	}
	return 0;
}