]>
Commit | Line | Data |
---|---|---|
b97bf3fd | 1 | /* |
f5e75269 | 2 | * net/tipc/log.c: TIPC print buffer routines for debugging |
c4307285 | 3 | * |
593a5f22 | 4 | * Copyright (c) 1996-2006, Ericsson AB |
fb98ec71 | 5 | * Copyright (c) 2005-2007, Wind River Systems |
b97bf3fd PL |
6 | * All rights reserved. |
7 | * | |
9ea1fd3c | 8 | * Redistribution and use in source and binary forms, with or without |
b97bf3fd PL |
9 | * modification, are permitted provided that the following conditions are met: |
10 | * | |
9ea1fd3c PL |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the names of the copyright holders nor the names of its | |
17 | * contributors may be used to endorse or promote products derived from | |
18 | * this software without specific prior written permission. | |
b97bf3fd | 19 | * |
9ea1fd3c PL |
20 | * Alternatively, this software may be distributed under the terms of the |
21 | * GNU General Public License ("GPL") version 2 as published by the Free | |
22 | * Software Foundation. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
b97bf3fd PL |
34 | * POSSIBILITY OF SUCH DAMAGE. |
35 | */ | |
36 | ||
37 | #include "core.h" | |
38 | #include "config.h" | |
f5e75269 | 39 | #include "log.h" |
b97bf3fd | 40 | |
c8903985 AS |
41 | /* |
42 | * TIPC pre-defines the following print buffers: | |
43 | * | |
44 | * TIPC_NULL : null buffer (i.e. print nowhere) | |
45 | * TIPC_CONS : system console | |
46 | * TIPC_LOG : TIPC log buffer | |
47 | * | |
48 | * Additional user-defined print buffers are also permitted. | |
49 | */ | |
b97bf3fd | 50 | |
c8903985 | 51 | static struct print_buf null_buf = { NULL, 0, NULL, 0 }; |
7d3aa712 | 52 | struct print_buf *const TIPC_NULL = &null_buf; |
29ede244 | 53 | |
c8903985 | 54 | static struct print_buf cons_buf = { NULL, 0, NULL, 1 }; |
6e7e309c | 55 | struct print_buf *const TIPC_CONS = &cons_buf; |
b97bf3fd | 56 | |
c8903985 | 57 | static struct print_buf log_buf = { NULL, 0, NULL, 1 }; |
7d3aa712 | 58 | struct print_buf *const TIPC_LOG = &log_buf; |
b97bf3fd | 59 | |
c8903985 AS |
60 | /* |
61 | * Locking policy when using print buffers. | |
62 | * | |
63 | * 1) tipc_printf() uses 'print_lock' to protect against concurrent access to | |
64 | * 'print_string' when writing to a print buffer. This also protects against | |
65 | * concurrent writes to the print buffer being written to. | |
66 | * | |
7ced6890 AS |
67 | * 2) tipc_log_XXX() leverages the aforementioned use of 'print_lock' to |
68 | * protect against all types of concurrent operations on their associated | |
69 | * print buffer (not just write operations). | |
c8903985 AS |
70 | * |
71 | * Note: All routines of the form tipc_printbuf_XXX() are lock-free, and rely | |
72 | * on the caller to prevent simultaneous use of the print buffer(s) being | |
73 | * manipulated. | |
74 | */ | |
75 | ||
76 | static char print_string[TIPC_PB_MAX_STR]; | |
77 | static DEFINE_SPINLOCK(print_lock); | |
78 | ||
31e3c3f6 | 79 | static void tipc_printbuf_move(struct print_buf *pb_to, |
80 | struct print_buf *pb_from); | |
b97bf3fd | 81 | |
f5e75269 | 82 | #define FORMAT(PTR, LEN, FMT) \ |
b97bf3fd | 83 | {\ |
f5e75269 AS |
84 | va_list args;\ |
85 | va_start(args, FMT);\ | |
86 | LEN = vsprintf(PTR, FMT, args);\ | |
87 | va_end(args);\ | |
88 | *(PTR + LEN) = '\0';\ | |
b97bf3fd PL |
89 | } |
90 | ||
b97bf3fd | 91 | /** |
4323add6 | 92 | * tipc_printbuf_init - initialize print buffer to empty |
29ede244 AS |
93 | * @pb: pointer to print buffer structure |
94 | * @raw: pointer to character array used by print buffer | |
95 | * @size: size of character array | |
96 | * | |
c8903985 AS |
97 | * Note: If the character array is too small (or absent), the print buffer |
98 | * becomes a null device that discards anything written to it. | |
b97bf3fd PL |
99 | */ |
100 | ||
29ede244 | 101 | void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size) |
b97bf3fd | 102 | { |
29ede244 AS |
103 | pb->buf = raw; |
104 | pb->crs = raw; | |
105 | pb->size = size; | |
c8903985 | 106 | pb->echo = 0; |
29ede244 AS |
107 | |
108 | if (size < TIPC_PB_MIN_SIZE) { | |
109 | pb->buf = NULL; | |
110 | } else if (raw) { | |
111 | pb->buf[0] = 0; | |
7d3aa712 | 112 | pb->buf[size - 1] = ~0; |
29ede244 | 113 | } |
b97bf3fd PL |
114 | } |
115 | ||
116 | /** | |
4323add6 | 117 | * tipc_printbuf_reset - reinitialize print buffer to empty state |
29ede244 | 118 | * @pb: pointer to print buffer structure |
b97bf3fd PL |
119 | */ |
120 | ||
31e3c3f6 | 121 | static void tipc_printbuf_reset(struct print_buf *pb) |
b97bf3fd | 122 | { |
7d3aa712 | 123 | if (pb->buf) { |
c8903985 AS |
124 | pb->crs = pb->buf; |
125 | pb->buf[0] = 0; | |
126 | pb->buf[pb->size - 1] = ~0; | |
127 | } | |
b97bf3fd PL |
128 | } |
129 | ||
130 | /** | |
4323add6 | 131 | * tipc_printbuf_empty - test if print buffer is in empty state |
29ede244 AS |
132 | * @pb: pointer to print buffer structure |
133 | * | |
134 | * Returns non-zero if print buffer is empty. | |
b97bf3fd PL |
135 | */ |
136 | ||
31e3c3f6 | 137 | static int tipc_printbuf_empty(struct print_buf *pb) |
b97bf3fd | 138 | { |
a02cec21 | 139 | return !pb->buf || (pb->crs == pb->buf); |
b97bf3fd PL |
140 | } |
141 | ||
142 | /** | |
4323add6 | 143 | * tipc_printbuf_validate - check for print buffer overflow |
29ede244 | 144 | * @pb: pointer to print buffer structure |
c4307285 YH |
145 | * |
146 | * Verifies that a print buffer has captured all data written to it. | |
b97bf3fd | 147 | * If data has been lost, linearize buffer and prepend an error message |
c4307285 | 148 | * |
29ede244 | 149 | * Returns length of print buffer data string (including trailing NUL) |
b97bf3fd PL |
150 | */ |
151 | ||
4323add6 | 152 | int tipc_printbuf_validate(struct print_buf *pb) |
b97bf3fd | 153 | { |
c4307285 YH |
154 | char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n"; |
155 | char *cp_buf; | |
156 | struct print_buf cb; | |
b97bf3fd | 157 | |
29ede244 | 158 | if (!pb->buf) |
b97bf3fd PL |
159 | return 0; |
160 | ||
29ede244 | 161 | if (pb->buf[pb->size - 1] == 0) { |
c4307285 | 162 | cp_buf = kmalloc(pb->size, GFP_ATOMIC); |
7d3aa712 | 163 | if (cp_buf) { |
c4307285 YH |
164 | tipc_printbuf_init(&cb, cp_buf, pb->size); |
165 | tipc_printbuf_move(&cb, pb); | |
166 | tipc_printbuf_move(pb, &cb); | |
167 | kfree(cp_buf); | |
168 | memcpy(pb->buf, err, strlen(err)); | |
169 | } else { | |
170 | tipc_printbuf_reset(pb); | |
171 | tipc_printf(pb, err); | |
172 | } | |
b97bf3fd | 173 | } |
a02cec21 | 174 | return pb->crs - pb->buf + 1; |
b97bf3fd PL |
175 | } |
176 | ||
177 | /** | |
4323add6 | 178 | * tipc_printbuf_move - move print buffer contents to another print buffer |
29ede244 AS |
179 | * @pb_to: pointer to destination print buffer structure |
180 | * @pb_from: pointer to source print buffer structure | |
c4307285 | 181 | * |
b97bf3fd PL |
182 | * Current contents of destination print buffer (if any) are discarded. |
183 | * Source print buffer becomes empty if a successful move occurs. | |
184 | */ | |
185 | ||
31e3c3f6 | 186 | static void tipc_printbuf_move(struct print_buf *pb_to, |
187 | struct print_buf *pb_from) | |
b97bf3fd PL |
188 | { |
189 | int len; | |
190 | ||
191 | /* Handle the cases where contents can't be moved */ | |
192 | ||
29ede244 | 193 | if (!pb_to->buf) |
b97bf3fd PL |
194 | return; |
195 | ||
29ede244 | 196 | if (!pb_from->buf) { |
4323add6 | 197 | tipc_printbuf_reset(pb_to); |
b97bf3fd PL |
198 | return; |
199 | } | |
200 | ||
201 | if (pb_to->size < pb_from->size) { | |
93758c3d AS |
202 | strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***"); |
203 | pb_to->buf[pb_to->size - 1] = ~0; | |
204 | pb_to->crs = strchr(pb_to->buf, 0); | |
b97bf3fd PL |
205 | return; |
206 | } | |
207 | ||
208 | /* Copy data from char after cursor to end (if used) */ | |
29ede244 | 209 | |
b97bf3fd | 210 | len = pb_from->buf + pb_from->size - pb_from->crs - 2; |
7d3aa712 | 211 | if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) { |
b97bf3fd PL |
212 | strcpy(pb_to->buf, pb_from->crs + 1); |
213 | pb_to->crs = pb_to->buf + len; | |
214 | } else | |
215 | pb_to->crs = pb_to->buf; | |
216 | ||
217 | /* Copy data from start to cursor (always) */ | |
29ede244 | 218 | |
b97bf3fd PL |
219 | len = pb_from->crs - pb_from->buf; |
220 | strcpy(pb_to->crs, pb_from->buf); | |
221 | pb_to->crs += len; | |
222 | ||
4323add6 | 223 | tipc_printbuf_reset(pb_from); |
b97bf3fd PL |
224 | } |
225 | ||
226 | /** | |
c8903985 AS |
227 | * tipc_printf - append formatted output to print buffer |
228 | * @pb: pointer to print buffer | |
29ede244 | 229 | * @fmt: formatted info to be printed |
b97bf3fd PL |
230 | */ |
231 | ||
232 | void tipc_printf(struct print_buf *pb, const char *fmt, ...) | |
233 | { | |
234 | int chars_to_add; | |
235 | int chars_left; | |
236 | char save_char; | |
b97bf3fd PL |
237 | |
238 | spin_lock_bh(&print_lock); | |
c8903985 | 239 | |
b97bf3fd | 240 | FORMAT(print_string, chars_to_add, fmt); |
29ede244 AS |
241 | if (chars_to_add >= TIPC_PB_MAX_STR) |
242 | strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***"); | |
b97bf3fd | 243 | |
c8903985 AS |
244 | if (pb->buf) { |
245 | chars_left = pb->buf + pb->size - pb->crs - 1; | |
246 | if (chars_to_add <= chars_left) { | |
247 | strcpy(pb->crs, print_string); | |
248 | pb->crs += chars_to_add; | |
249 | } else if (chars_to_add >= (pb->size - 1)) { | |
250 | strcpy(pb->buf, print_string + chars_to_add + 1 | |
251 | - pb->size); | |
252 | pb->crs = pb->buf + pb->size - 1; | |
253 | } else { | |
254 | strcpy(pb->buf, print_string + chars_left); | |
255 | save_char = print_string[chars_left]; | |
256 | print_string[chars_left] = 0; | |
257 | strcpy(pb->crs, print_string); | |
258 | print_string[chars_left] = save_char; | |
259 | pb->crs = pb->buf + chars_to_add - chars_left; | |
c4307285 | 260 | } |
b97bf3fd | 261 | } |
b97bf3fd | 262 | |
c8903985 | 263 | if (pb->echo) |
4b704d59 | 264 | printk("%s", print_string); |
b97bf3fd | 265 | |
b97bf3fd | 266 | spin_unlock_bh(&print_lock); |
b97bf3fd PL |
267 | } |
268 | ||
b97bf3fd | 269 | /** |
025adbe8 AS |
270 | * tipc_log_resize - change the size of the TIPC log buffer |
271 | * @log_size: print buffer size to use | |
b97bf3fd PL |
272 | */ |
273 | ||
fb98ec71 | 274 | int tipc_log_resize(int log_size) |
b97bf3fd | 275 | { |
fb98ec71 AS |
276 | int res = 0; |
277 | ||
b97bf3fd | 278 | spin_lock_bh(&print_lock); |
f5e75269 AS |
279 | kfree(TIPC_LOG->buf); |
280 | TIPC_LOG->buf = NULL; | |
b97bf3fd | 281 | if (log_size) { |
29ede244 AS |
282 | if (log_size < TIPC_PB_MIN_SIZE) |
283 | log_size = TIPC_PB_MIN_SIZE; | |
c8903985 | 284 | res = TIPC_LOG->echo; |
29ede244 AS |
285 | tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC), |
286 | log_size); | |
c8903985 | 287 | TIPC_LOG->echo = res; |
fb98ec71 | 288 | res = !TIPC_LOG->buf; |
b97bf3fd | 289 | } |
025adbe8 | 290 | spin_unlock_bh(&print_lock); |
fb98ec71 AS |
291 | |
292 | return res; | |
b97bf3fd PL |
293 | } |
294 | ||
295 | /** | |
025adbe8 | 296 | * tipc_log_resize_cmd - reconfigure size of TIPC log buffer |
b97bf3fd PL |
297 | */ |
298 | ||
025adbe8 | 299 | struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space) |
b97bf3fd PL |
300 | { |
301 | u32 value; | |
302 | ||
303 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED)) | |
4323add6 | 304 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd | 305 | |
3e6c8cd5 | 306 | value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); |
b97bf3fd | 307 | if (value != delimit(value, 0, 32768)) |
4323add6 PL |
308 | return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE |
309 | " (log size must be 0-32768)"); | |
fb98ec71 AS |
310 | if (tipc_log_resize(value)) |
311 | return tipc_cfg_reply_error_string( | |
312 | "unable to create specified log (log size is now 0)"); | |
4323add6 | 313 | return tipc_cfg_reply_none(); |
b97bf3fd PL |
314 | } |
315 | ||
316 | /** | |
4323add6 | 317 | * tipc_log_dump - capture TIPC log buffer contents in configuration message |
b97bf3fd PL |
318 | */ |
319 | ||
4323add6 | 320 | struct sk_buff *tipc_log_dump(void) |
b97bf3fd PL |
321 | { |
322 | struct sk_buff *reply; | |
323 | ||
324 | spin_lock_bh(&print_lock); | |
93758c3d AS |
325 | if (!TIPC_LOG->buf) { |
326 | spin_unlock_bh(&print_lock); | |
4323add6 | 327 | reply = tipc_cfg_reply_ultra_string("log not activated\n"); |
93758c3d AS |
328 | } else if (tipc_printbuf_empty(TIPC_LOG)) { |
329 | spin_unlock_bh(&print_lock); | |
4323add6 | 330 | reply = tipc_cfg_reply_ultra_string("log is empty\n"); |
f5e75269 | 331 | } else { |
b97bf3fd PL |
332 | struct tlv_desc *rep_tlv; |
333 | struct print_buf pb; | |
334 | int str_len; | |
335 | ||
4323add6 | 336 | str_len = min(TIPC_LOG->size, 32768u); |
93758c3d | 337 | spin_unlock_bh(&print_lock); |
4323add6 | 338 | reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len)); |
b97bf3fd PL |
339 | if (reply) { |
340 | rep_tlv = (struct tlv_desc *)reply->data; | |
4323add6 | 341 | tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len); |
93758c3d | 342 | spin_lock_bh(&print_lock); |
4323add6 | 343 | tipc_printbuf_move(&pb, TIPC_LOG); |
93758c3d | 344 | spin_unlock_bh(&print_lock); |
b97bf3fd PL |
345 | str_len = strlen(TLV_DATA(rep_tlv)) + 1; |
346 | skb_put(reply, TLV_SPACE(str_len)); | |
347 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); | |
348 | } | |
349 | } | |
b97bf3fd PL |
350 | return reply; |
351 | } |