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 | */ | |
c8903985 | 50 | static struct print_buf null_buf = { NULL, 0, NULL, 0 }; |
7d3aa712 | 51 | struct print_buf *const TIPC_NULL = &null_buf; |
29ede244 | 52 | |
c8903985 | 53 | static struct print_buf cons_buf = { NULL, 0, NULL, 1 }; |
6e7e309c | 54 | struct print_buf *const TIPC_CONS = &cons_buf; |
b97bf3fd | 55 | |
c8903985 | 56 | static struct print_buf log_buf = { NULL, 0, NULL, 1 }; |
7d3aa712 | 57 | struct print_buf *const TIPC_LOG = &log_buf; |
b97bf3fd | 58 | |
c8903985 AS |
59 | /* |
60 | * Locking policy when using print buffers. | |
61 | * | |
62 | * 1) tipc_printf() uses 'print_lock' to protect against concurrent access to | |
63 | * 'print_string' when writing to a print buffer. This also protects against | |
64 | * concurrent writes to the print buffer being written to. | |
65 | * | |
7ced6890 AS |
66 | * 2) tipc_log_XXX() leverages the aforementioned use of 'print_lock' to |
67 | * protect against all types of concurrent operations on their associated | |
68 | * print buffer (not just write operations). | |
c8903985 AS |
69 | * |
70 | * Note: All routines of the form tipc_printbuf_XXX() are lock-free, and rely | |
71 | * on the caller to prevent simultaneous use of the print buffer(s) being | |
72 | * manipulated. | |
73 | */ | |
c8903985 AS |
74 | static DEFINE_SPINLOCK(print_lock); |
75 | ||
31e3c3f6 | 76 | static void tipc_printbuf_move(struct print_buf *pb_to, |
77 | struct print_buf *pb_from); | |
b97bf3fd | 78 | |
b97bf3fd | 79 | /** |
4323add6 | 80 | * tipc_printbuf_init - initialize print buffer to empty |
29ede244 AS |
81 | * @pb: pointer to print buffer structure |
82 | * @raw: pointer to character array used by print buffer | |
83 | * @size: size of character array | |
84 | * | |
c8903985 AS |
85 | * Note: If the character array is too small (or absent), the print buffer |
86 | * becomes a null device that discards anything written to it. | |
b97bf3fd | 87 | */ |
29ede244 | 88 | void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size) |
b97bf3fd | 89 | { |
29ede244 AS |
90 | pb->buf = raw; |
91 | pb->crs = raw; | |
92 | pb->size = size; | |
c8903985 | 93 | pb->echo = 0; |
29ede244 AS |
94 | |
95 | if (size < TIPC_PB_MIN_SIZE) { | |
96 | pb->buf = NULL; | |
97 | } else if (raw) { | |
98 | pb->buf[0] = 0; | |
7d3aa712 | 99 | pb->buf[size - 1] = ~0; |
29ede244 | 100 | } |
b97bf3fd PL |
101 | } |
102 | ||
103 | /** | |
4323add6 | 104 | * tipc_printbuf_reset - reinitialize print buffer to empty state |
29ede244 | 105 | * @pb: pointer to print buffer structure |
b97bf3fd | 106 | */ |
31e3c3f6 | 107 | static void tipc_printbuf_reset(struct print_buf *pb) |
b97bf3fd | 108 | { |
7d3aa712 | 109 | if (pb->buf) { |
c8903985 AS |
110 | pb->crs = pb->buf; |
111 | pb->buf[0] = 0; | |
112 | pb->buf[pb->size - 1] = ~0; | |
113 | } | |
b97bf3fd PL |
114 | } |
115 | ||
116 | /** | |
4323add6 | 117 | * tipc_printbuf_empty - test if print buffer is in empty state |
29ede244 AS |
118 | * @pb: pointer to print buffer structure |
119 | * | |
120 | * Returns non-zero if print buffer is empty. | |
b97bf3fd | 121 | */ |
31e3c3f6 | 122 | static int tipc_printbuf_empty(struct print_buf *pb) |
b97bf3fd | 123 | { |
a02cec21 | 124 | return !pb->buf || (pb->crs == pb->buf); |
b97bf3fd PL |
125 | } |
126 | ||
127 | /** | |
4323add6 | 128 | * tipc_printbuf_validate - check for print buffer overflow |
29ede244 | 129 | * @pb: pointer to print buffer structure |
c4307285 YH |
130 | * |
131 | * Verifies that a print buffer has captured all data written to it. | |
b97bf3fd | 132 | * If data has been lost, linearize buffer and prepend an error message |
c4307285 | 133 | * |
29ede244 | 134 | * Returns length of print buffer data string (including trailing NUL) |
b97bf3fd | 135 | */ |
4323add6 | 136 | int tipc_printbuf_validate(struct print_buf *pb) |
b97bf3fd | 137 | { |
c4307285 YH |
138 | char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n"; |
139 | char *cp_buf; | |
140 | struct print_buf cb; | |
b97bf3fd | 141 | |
29ede244 | 142 | if (!pb->buf) |
b97bf3fd PL |
143 | return 0; |
144 | ||
29ede244 | 145 | if (pb->buf[pb->size - 1] == 0) { |
c4307285 | 146 | cp_buf = kmalloc(pb->size, GFP_ATOMIC); |
7d3aa712 | 147 | if (cp_buf) { |
c4307285 YH |
148 | tipc_printbuf_init(&cb, cp_buf, pb->size); |
149 | tipc_printbuf_move(&cb, pb); | |
150 | tipc_printbuf_move(pb, &cb); | |
151 | kfree(cp_buf); | |
152 | memcpy(pb->buf, err, strlen(err)); | |
153 | } else { | |
154 | tipc_printbuf_reset(pb); | |
155 | tipc_printf(pb, err); | |
156 | } | |
b97bf3fd | 157 | } |
a02cec21 | 158 | return pb->crs - pb->buf + 1; |
b97bf3fd PL |
159 | } |
160 | ||
161 | /** | |
4323add6 | 162 | * tipc_printbuf_move - move print buffer contents to another print buffer |
29ede244 AS |
163 | * @pb_to: pointer to destination print buffer structure |
164 | * @pb_from: pointer to source print buffer structure | |
c4307285 | 165 | * |
b97bf3fd PL |
166 | * Current contents of destination print buffer (if any) are discarded. |
167 | * Source print buffer becomes empty if a successful move occurs. | |
168 | */ | |
31e3c3f6 | 169 | static void tipc_printbuf_move(struct print_buf *pb_to, |
170 | struct print_buf *pb_from) | |
b97bf3fd PL |
171 | { |
172 | int len; | |
173 | ||
174 | /* Handle the cases where contents can't be moved */ | |
29ede244 | 175 | if (!pb_to->buf) |
b97bf3fd PL |
176 | return; |
177 | ||
29ede244 | 178 | if (!pb_from->buf) { |
4323add6 | 179 | tipc_printbuf_reset(pb_to); |
b97bf3fd PL |
180 | return; |
181 | } | |
182 | ||
183 | if (pb_to->size < pb_from->size) { | |
93758c3d AS |
184 | strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***"); |
185 | pb_to->buf[pb_to->size - 1] = ~0; | |
186 | pb_to->crs = strchr(pb_to->buf, 0); | |
b97bf3fd PL |
187 | return; |
188 | } | |
189 | ||
190 | /* Copy data from char after cursor to end (if used) */ | |
191 | len = pb_from->buf + pb_from->size - pb_from->crs - 2; | |
7d3aa712 | 192 | if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) { |
b97bf3fd PL |
193 | strcpy(pb_to->buf, pb_from->crs + 1); |
194 | pb_to->crs = pb_to->buf + len; | |
195 | } else | |
196 | pb_to->crs = pb_to->buf; | |
197 | ||
198 | /* Copy data from start to cursor (always) */ | |
199 | len = pb_from->crs - pb_from->buf; | |
200 | strcpy(pb_to->crs, pb_from->buf); | |
201 | pb_to->crs += len; | |
202 | ||
4323add6 | 203 | tipc_printbuf_reset(pb_from); |
b97bf3fd PL |
204 | } |
205 | ||
206 | /** | |
c8903985 AS |
207 | * tipc_printf - append formatted output to print buffer |
208 | * @pb: pointer to print buffer | |
29ede244 | 209 | * @fmt: formatted info to be printed |
b97bf3fd | 210 | */ |
b97bf3fd PL |
211 | void tipc_printf(struct print_buf *pb, const char *fmt, ...) |
212 | { | |
e2dbd601 EH |
213 | int i; |
214 | va_list args; | |
215 | char *buf; | |
216 | int len; | |
b97bf3fd | 217 | |
e2dbd601 EH |
218 | buf = pb->crs; |
219 | len = pb->buf + pb->size - pb->crs; | |
220 | va_start(args, fmt); | |
221 | i = vscnprintf(buf, len, fmt, args); | |
222 | va_end(args); | |
223 | pb->crs += i; | |
b97bf3fd PL |
224 | } |
225 | ||
b97bf3fd | 226 | /** |
025adbe8 AS |
227 | * tipc_log_resize - change the size of the TIPC log buffer |
228 | * @log_size: print buffer size to use | |
b97bf3fd | 229 | */ |
fb98ec71 | 230 | int tipc_log_resize(int log_size) |
b97bf3fd | 231 | { |
fb98ec71 AS |
232 | int res = 0; |
233 | ||
b97bf3fd | 234 | spin_lock_bh(&print_lock); |
f5e75269 AS |
235 | kfree(TIPC_LOG->buf); |
236 | TIPC_LOG->buf = NULL; | |
b97bf3fd | 237 | if (log_size) { |
29ede244 AS |
238 | if (log_size < TIPC_PB_MIN_SIZE) |
239 | log_size = TIPC_PB_MIN_SIZE; | |
c8903985 | 240 | res = TIPC_LOG->echo; |
29ede244 AS |
241 | tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC), |
242 | log_size); | |
c8903985 | 243 | TIPC_LOG->echo = res; |
fb98ec71 | 244 | res = !TIPC_LOG->buf; |
b97bf3fd | 245 | } |
025adbe8 | 246 | spin_unlock_bh(&print_lock); |
fb98ec71 AS |
247 | |
248 | return res; | |
b97bf3fd PL |
249 | } |
250 | ||
251 | /** | |
025adbe8 | 252 | * tipc_log_resize_cmd - reconfigure size of TIPC log buffer |
b97bf3fd | 253 | */ |
025adbe8 | 254 | struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space) |
b97bf3fd PL |
255 | { |
256 | u32 value; | |
257 | ||
258 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED)) | |
4323add6 | 259 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd | 260 | |
3e6c8cd5 | 261 | value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); |
732efba4 | 262 | if (value > 32768) |
4323add6 PL |
263 | return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE |
264 | " (log size must be 0-32768)"); | |
fb98ec71 AS |
265 | if (tipc_log_resize(value)) |
266 | return tipc_cfg_reply_error_string( | |
267 | "unable to create specified log (log size is now 0)"); | |
4323add6 | 268 | return tipc_cfg_reply_none(); |
b97bf3fd PL |
269 | } |
270 | ||
271 | /** | |
4323add6 | 272 | * tipc_log_dump - capture TIPC log buffer contents in configuration message |
b97bf3fd | 273 | */ |
4323add6 | 274 | struct sk_buff *tipc_log_dump(void) |
b97bf3fd PL |
275 | { |
276 | struct sk_buff *reply; | |
277 | ||
278 | spin_lock_bh(&print_lock); | |
93758c3d AS |
279 | if (!TIPC_LOG->buf) { |
280 | spin_unlock_bh(&print_lock); | |
4323add6 | 281 | reply = tipc_cfg_reply_ultra_string("log not activated\n"); |
93758c3d AS |
282 | } else if (tipc_printbuf_empty(TIPC_LOG)) { |
283 | spin_unlock_bh(&print_lock); | |
4323add6 | 284 | reply = tipc_cfg_reply_ultra_string("log is empty\n"); |
f5e75269 | 285 | } else { |
b97bf3fd PL |
286 | struct tlv_desc *rep_tlv; |
287 | struct print_buf pb; | |
288 | int str_len; | |
289 | ||
4323add6 | 290 | str_len = min(TIPC_LOG->size, 32768u); |
93758c3d | 291 | spin_unlock_bh(&print_lock); |
4323add6 | 292 | reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len)); |
b97bf3fd PL |
293 | if (reply) { |
294 | rep_tlv = (struct tlv_desc *)reply->data; | |
4323add6 | 295 | tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len); |
93758c3d | 296 | spin_lock_bh(&print_lock); |
4323add6 | 297 | tipc_printbuf_move(&pb, TIPC_LOG); |
93758c3d | 298 | spin_unlock_bh(&print_lock); |
b97bf3fd PL |
299 | str_len = strlen(TLV_DATA(rep_tlv)) + 1; |
300 | skb_put(reply, TLV_SPACE(str_len)); | |
301 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); | |
302 | } | |
303 | } | |
b97bf3fd PL |
304 | return reply; |
305 | } |