log.c 9.8 KB

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