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