forked from lsof-org/lsof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdprint.c
More file actions
209 lines (197 loc) · 5.6 KB
/
dprint.c
File metadata and controls
209 lines (197 loc) · 5.6 KB
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
/*
* dprint.c - Linux printing functions for /proc-based lsof
*/
/*
* Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana
* 47907. All rights reserved.
*
* Written by Victor A. Abell
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company or the Regents of the University of California.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. Neither the authors nor Purdue University are responsible for any
* consequences of the use of this software.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Credit to the authors and Purdue
* University must appear in documentation and sources.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 4. This notice may not be removed or altered.
*/
#include "common.h"
#include "cli.h"
#if defined(HASSOSTATE)
# include <linux/net.h> /* for SS_* */
#endif /* defined(HASSOSTATE) */
#if defined(HASSOSTATE)
static char *socket_state_to_str(struct lsof_context *ctx, unsigned int ss);
#endif /* defined(HASSOSTATE) */
/*
* print_unix() - print state of UNIX domain socket e.g. UNCONNECTED
*/
static void print_unix(struct lsof_context *ctx, int nl) {
if (Ftcptpi & TCPTPI_STATE) {
#if defined(HASSOSTATE) && defined(HASSOOPT)
char *cp = (Lf->lts.opt == __SO_ACCEPTCON)
? "LISTEN"
: socket_state_to_str(ctx, Lf->lts.ss);
if (Ffield)
(void)printf("%cST=%s%c", LSOF_FID_TCPTPI, cp, Terminator);
else {
putchar('(');
(void)fputs(cp, stdout);
putchar(')');
}
#endif /* defined(HASSOSTATE) && defined(HASSOOPT) */
}
if (nl)
putchar('\n');
}
/*
* print_tcptpi() - print TCP/TPI state e.g. ESTBALISHED
*/
void print_tcptpi(struct lsof_context *ctx, /* context */
int nl) /* 1 == '\n' required */
{
char buf[128];
char *cp = (char *)NULL;
int ps = 0;
int s;
if (Lf->type == LSOF_FILE_UNIX) {
print_unix(ctx, nl);
return;
}
if ((Ftcptpi & TCPTPI_STATE) && Lf->lts.type >= 0) {
if (!TcpSt)
(void)build_IPstates(ctx);
switch (Lf->lts.type) {
case 0: /* TCP */
if ((s = Lf->lts.state.i + TcpStOff) < 0 || s >= TcpNstates) {
(void)snpf(buf, sizeof(buf), "UNKNOWN_TCP_STATE_%d",
Lf->lts.state.i);
cp = buf;
} else
cp = TcpSt[s];
break;
case 1: /* UDP */
if (!UdpSt)
(void)build_IPstates(ctx);
if ((s = Lf->lts.state.i + UdpStOff) >= 0 && s < UdpNstates)
cp = UdpSt[s];
break;
}
if (cp) {
if (Ffield)
(void)printf("%cST=%s%c", LSOF_FID_TCPTPI, cp, Terminator);
else {
putchar('(');
(void)fputs(cp, stdout);
}
ps++;
}
}
#if defined(HASTCPTPIQ)
if (Ftcptpi & TCPTPI_QUEUES) {
if (Lf->lts.rqs) {
if (Ffield)
putchar(LSOF_FID_TCPTPI);
else {
if (ps)
putchar(' ');
else
putchar('(');
}
(void)printf("QR=%lu", Lf->lts.rq);
if (Ffield)
putchar(Terminator);
ps++;
}
if (Lf->lts.sqs) {
if (Ffield)
putchar(LSOF_FID_TCPTPI);
else {
if (ps)
putchar(' ');
else
putchar('(');
}
(void)printf("QS=%lu", Lf->lts.sq);
if (Ffield)
putchar(Terminator);
ps++;
}
}
#endif /* defined(HASTCPTPIQ) */
#if defined(HASTCPTPIW)
if (Ftcptpi & TCPTPI_WINDOWS) {
if (Lf->lts.rws) {
if (Ffield)
putchar(LSOF_FID_TCPTPI);
else {
if (ps)
putchar(' ');
else
putchar('(');
}
(void)printf("WR=%lu", Lf->lts.rw);
if (Ffield)
putchar(Terminator);
ps++;
}
if (Lf->lts.wws) {
if (Ffield)
putchar(LSOF_FID_TCPTPI);
else {
if (ps)
putchar(' ');
else
putchar('(');
}
(void)printf("WW=%lu", Lf->lts.ww);
if (Ffield)
putchar(Terminator);
ps++;
}
}
#endif /* defined(HASTCPTPIW) */
if (!Ffield && ps)
putchar(')');
if (nl)
putchar('\n');
}
#if defined(HASSOSTATE)
/*
* socket_state_to_str() -- convert socket state number to a string
*
* returns "UNKNOWN" for unknown state.
*/
static char *socket_state_to_str(struct lsof_context *ctx, unsigned int ss) {
char *sr;
switch (Lf->lts.ss) {
case SS_UNCONNECTED:
sr = "UNCONNECTED";
break;
case SS_CONNECTING:
sr = "CONNECTING";
break;
case SS_CONNECTED:
sr = "CONNECTED";
break;
case SS_DISCONNECTING:
sr = "DISCONNECTING";
break;
default:
sr = "UNKNOWN";
break;
}
return sr;
}
#endif /* defined(HASSOSTATE) */