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#include "core.h"
38#include "config.h"
39#include "log.h"
40
41
42
43
44
45
46
47
48
49
50
51static struct print_buf null_buf = { NULL, 0, NULL, 0 };
52struct print_buf *const TIPC_NULL = &null_buf;
53
54static struct print_buf cons_buf = { NULL, 0, NULL, 1 };
55struct print_buf *const TIPC_CONS = &cons_buf;
56
57static struct print_buf log_buf = { NULL, 0, NULL, 1 };
58struct print_buf *const TIPC_LOG = &log_buf;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76static char print_string[TIPC_PB_MAX_STR];
77static DEFINE_SPINLOCK(print_lock);
78
79static void tipc_printbuf_move(struct print_buf *pb_to,
80 struct print_buf *pb_from);
81
82#define FORMAT(PTR, LEN, FMT) \
83{\
84 va_list args;\
85 va_start(args, FMT);\
86 LEN = vsprintf(PTR, FMT, args);\
87 va_end(args);\
88 *(PTR + LEN) = '\0';\
89}
90
91
92
93
94
95
96
97
98
99
100
101void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size)
102{
103 pb->buf = raw;
104 pb->crs = raw;
105 pb->size = size;
106 pb->echo = 0;
107
108 if (size < TIPC_PB_MIN_SIZE) {
109 pb->buf = NULL;
110 } else if (raw) {
111 pb->buf[0] = 0;
112 pb->buf[size - 1] = ~0;
113 }
114}
115
116
117
118
119
120
121static void tipc_printbuf_reset(struct print_buf *pb)
122{
123 if (pb->buf) {
124 pb->crs = pb->buf;
125 pb->buf[0] = 0;
126 pb->buf[pb->size - 1] = ~0;
127 }
128}
129
130
131
132
133
134
135
136
137static int tipc_printbuf_empty(struct print_buf *pb)
138{
139 return !pb->buf || (pb->crs == pb->buf);
140}
141
142
143
144
145
146
147
148
149
150
151
152int tipc_printbuf_validate(struct print_buf *pb)
153{
154 char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n";
155 char *cp_buf;
156 struct print_buf cb;
157
158 if (!pb->buf)
159 return 0;
160
161 if (pb->buf[pb->size - 1] == 0) {
162 cp_buf = kmalloc(pb->size, GFP_ATOMIC);
163 if (cp_buf) {
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 }
173 }
174 return pb->crs - pb->buf + 1;
175}
176
177
178
179
180
181
182
183
184
185
186static void tipc_printbuf_move(struct print_buf *pb_to,
187 struct print_buf *pb_from)
188{
189 int len;
190
191
192
193 if (!pb_to->buf)
194 return;
195
196 if (!pb_from->buf) {
197 tipc_printbuf_reset(pb_to);
198 return;
199 }
200
201 if (pb_to->size < pb_from->size) {
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);
205 return;
206 }
207
208
209
210 len = pb_from->buf + pb_from->size - pb_from->crs - 2;
211 if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) {
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
218
219 len = pb_from->crs - pb_from->buf;
220 strcpy(pb_to->crs, pb_from->buf);
221 pb_to->crs += len;
222
223 tipc_printbuf_reset(pb_from);
224}
225
226
227
228
229
230
231
232void tipc_printf(struct print_buf *pb, const char *fmt, ...)
233{
234 int chars_to_add;
235 int chars_left;
236 char save_char;
237
238 spin_lock_bh(&print_lock);
239
240 FORMAT(print_string, chars_to_add, fmt);
241 if (chars_to_add >= TIPC_PB_MAX_STR)
242 strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***");
243
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;
260 }
261 }
262
263 if (pb->echo)
264 printk("%s", print_string);
265
266 spin_unlock_bh(&print_lock);
267}
268
269
270
271
272
273
274int tipc_log_resize(int log_size)
275{
276 int res = 0;
277
278 spin_lock_bh(&print_lock);
279 kfree(TIPC_LOG->buf);
280 TIPC_LOG->buf = NULL;
281 if (log_size) {
282 if (log_size < TIPC_PB_MIN_SIZE)
283 log_size = TIPC_PB_MIN_SIZE;
284 res = TIPC_LOG->echo;
285 tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC),
286 log_size);
287 TIPC_LOG->echo = res;
288 res = !TIPC_LOG->buf;
289 }
290 spin_unlock_bh(&print_lock);
291
292 return res;
293}
294
295
296
297
298
299struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space)
300{
301 u32 value;
302
303 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
304 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
305
306 value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
307 if (value != delimit(value, 0, 32768))
308 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
309 " (log size must be 0-32768)");
310 if (tipc_log_resize(value))
311 return tipc_cfg_reply_error_string(
312 "unable to create specified log (log size is now 0)");
313 return tipc_cfg_reply_none();
314}
315
316
317
318
319
320struct sk_buff *tipc_log_dump(void)
321{
322 struct sk_buff *reply;
323
324 spin_lock_bh(&print_lock);
325 if (!TIPC_LOG->buf) {
326 spin_unlock_bh(&print_lock);
327 reply = tipc_cfg_reply_ultra_string("log not activated\n");
328 } else if (tipc_printbuf_empty(TIPC_LOG)) {
329 spin_unlock_bh(&print_lock);
330 reply = tipc_cfg_reply_ultra_string("log is empty\n");
331 } else {
332 struct tlv_desc *rep_tlv;
333 struct print_buf pb;
334 int str_len;
335
336 str_len = min(TIPC_LOG->size, 32768u);
337 spin_unlock_bh(&print_lock);
338 reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
339 if (reply) {
340 rep_tlv = (struct tlv_desc *)reply->data;
341 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
342 spin_lock_bh(&print_lock);
343 tipc_printbuf_move(&pb, TIPC_LOG);
344 spin_unlock_bh(&print_lock);
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 }
350 return reply;
351}
352