Debian Codegraph @

src/dialects/linux/dprint.c

raw
1 /*
2 * dprint.c - Linux printing functions for /proc-based lsof
3 */
4
5 /*
6 * Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana
7 * 47907. All rights reserved.
8 *
9 * Written by Victor A. Abell
10 *
11 * This software is not subject to any license of the American Telephone
12 * and Telegraph Company or the Regents of the University of California.
13 *
14 * Permission is granted to anyone to use this software for any purpose on
15 * any computer system, and to alter it and redistribute it freely, subject
16 * to the following restrictions:
17 *
18 * 1. Neither the authors nor Purdue University are responsible for any
19 * consequences of the use of this software.
20 *
21 * 2. The origin of this software must not be misrepresented, either by
22 * explicit claim or by omission. Credit to the authors and Purdue
23 * University must appear in documentation and sources.
24 *
25 * 3. Altered versions must be plainly marked as such, and must not be
26 * misrepresented as being the original software.
27 *
28 * 4. This notice may not be removed or altered.
29 */
30
31 #include "common.h"
32 #include "cli.h"
33
34 #if defined(HASSOSTATE)
35 # include <linux/net.h> /* for SS_* */
36 #endif /* defined(HASSOSTATE) */
37
38 #if defined(HASSOSTATE)
39 static char *socket_state_to_str(struct lsof_context *ctx, unsigned int ss);
40 #endif /* defined(HASSOSTATE) */
41
42 /*
43 * print_unix() - print state of UNIX domain socket e.g. UNCONNECTED
44 */
45 static void print_unix(struct lsof_context *ctx, int nl) {
46 if (Ftcptpi & TCPTPI_STATE) {
47 #if defined(HASSOSTATE) && defined(HASSOOPT)
48 char *cp = (Lf->lts.opt == __SO_ACCEPTCON)
49 ? "LISTEN"
50 : socket_state_to_str(ctx, Lf->lts.ss);
51
52 if (Ffield)
53 (void)printf("%cST=%s%c", LSOF_FID_TCPTPI, cp, Terminator);
54 else {
55 putchar('(');
56 (void)fputs(cp, stdout);
57 putchar(')');
58 }
59 #endif /* defined(HASSOSTATE) && defined(HASSOOPT) */
60 }
61 if (nl)
62 putchar('\n');
63 }
64
65 /*
66 * print_tcptpi() - print TCP/TPI state e.g. ESTBALISHED
67 */
68 void print_tcptpi(struct lsof_context *ctx, /* context */
69 int nl) /* 1 == '\n' required */
70 {
71 char buf[128];
72 char *cp = (char *)NULL;
73 int ps = 0;
74 int s;
75
76 if (Lf->type == LSOF_FILE_UNIX) {
77 print_unix(ctx, nl);
78 return;
79 }
80 if ((Ftcptpi & TCPTPI_STATE) && Lf->lts.type == 0) {
81 if (!TcpSt)
82 (void)build_IPstates(ctx);
83 if ((s = Lf->lts.state.i + TcpStOff) < 0 || s >= TcpNstates) {
84 (void)snpf(buf, sizeof(buf), "UNKNOWN_TCP_STATE_%d",
85 Lf->lts.state.i);
86 cp = buf;
87 } else
88 cp = TcpSt[s];
89 if (cp) {
90 if (Ffield)
91 (void)printf("%cST=%s%c", LSOF_FID_TCPTPI, cp, Terminator);
92 else {
93 putchar('(');
94 (void)fputs(cp, stdout);
95 }
96 ps++;
97 }
98 }
99
100 #if defined(HASTCPTPIQ)
101 if (Ftcptpi & TCPTPI_QUEUES) {
102 if (Lf->lts.rqs) {
103 if (Ffield)
104 putchar(LSOF_FID_TCPTPI);
105 else {
106 if (ps)
107 putchar(' ');
108 else
109 putchar('(');
110 }
111 (void)printf("QR=%lu", Lf->lts.rq);
112 if (Ffield)
113 putchar(Terminator);
114 ps++;
115 }
116 if (Lf->lts.sqs) {
117 if (Ffield)
118 putchar(LSOF_FID_TCPTPI);
119 else {
120 if (ps)
121 putchar(' ');
122 else
123 putchar('(');
124 }
125 (void)printf("QS=%lu", Lf->lts.sq);
126 if (Ffield)
127 putchar(Terminator);
128 ps++;
129 }
130 }
131 #endif /* defined(HASTCPTPIQ) */
132
133 #if defined(HASTCPTPIW)
134 if (Ftcptpi & TCPTPI_WINDOWS) {
135 if (Lf->lts.rws) {
136 if (Ffield)
137 putchar(LSOF_FID_TCPTPI);
138 else {
139 if (ps)
140 putchar(' ');
141 else
142 putchar('(');
143 }
144 (void)printf("WR=%lu", Lf->lts.rw);
145 if (Ffield)
146 putchar(Terminator);
147 ps++;
148 }
149 if (Lf->lts.wws) {
150 if (Ffield)
151 putchar(LSOF_FID_TCPTPI);
152 else {
153 if (ps)
154 putchar(' ');
155 else
156 putchar('(');
157 }
158 (void)printf("WW=%lu", Lf->lts.ww);
159 if (Ffield)
160 putchar(Terminator);
161 ps++;
162 }
163 }
164 #endif /* defined(HASTCPTPIW) */
165
166 if (!Ffield && ps)
167 putchar(')');
168 if (nl)
169 putchar('\n');
170 }
171
172 #if defined(HASSOSTATE)
173 /*
174 * socket_state_to_str() -- convert socket state number to a string
175 *
176 * returns "UNKNOWN" for unknown state.
177 */
178 static char *socket_state_to_str(struct lsof_context *ctx, unsigned int ss) {
179 char *sr;
180 switch (Lf->lts.ss) {
181 case SS_UNCONNECTED:
182 sr = "UNCONNECTED";
183 break;
184 case SS_CONNECTING:
185 sr = "CONNECTING";
186 break;
187 case SS_CONNECTED:
188 sr = "CONNECTED";
189 break;
190 case SS_DISCONNECTING:
191 sr = "DISCONNECTING";
192 break;
193 default:
194 sr = "UNKNOWN";
195 break;
196 }
197 return sr;
198 }
199 #endif /* defined(HASSOSTATE) */