log.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. 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 DEFINE_SPINLOCK(print_lock);
  70. static void tipc_printbuf_move(struct print_buf *pb_to,
  71. struct print_buf *pb_from);
  72. /**
  73. * tipc_printbuf_init - initialize print buffer to empty
  74. * @pb: pointer to print buffer structure
  75. * @raw: pointer to character array used by print buffer
  76. * @size: size of character array
  77. *
  78. * Note: If the character array is too small (or absent), the print buffer
  79. * becomes a null device that discards anything written to it.
  80. */
  81. void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size)
  82. {
  83. pb->buf = raw;
  84. pb->crs = raw;
  85. pb->size = size;
  86. pb->echo = 0;
  87. if (size < TIPC_PB_MIN_SIZE) {
  88. pb->buf = NULL;
  89. } else if (raw) {
  90. pb->buf[0] = 0;
  91. pb->buf[size - 1] = ~0;
  92. }
  93. }
  94. /**
  95. * tipc_printbuf_reset - reinitialize print buffer to empty state
  96. * @pb: pointer to print buffer structure
  97. */
  98. static void tipc_printbuf_reset(struct print_buf *pb)
  99. {
  100. if (pb->buf) {
  101. pb->crs = pb->buf;
  102. pb->buf[0] = 0;
  103. pb->buf[pb->size - 1] = ~0;
  104. }
  105. }
  106. /**
  107. * tipc_printbuf_empty - test if print buffer is in empty state
  108. * @pb: pointer to print buffer structure
  109. *
  110. * Returns non-zero if print buffer is empty.
  111. */
  112. static int tipc_printbuf_empty(struct print_buf *pb)
  113. {
  114. return !pb->buf || (pb->crs == pb->buf);
  115. }
  116. /**
  117. * tipc_printbuf_move - move print buffer contents to another print buffer
  118. * @pb_to: pointer to destination print buffer structure
  119. * @pb_from: pointer to source print buffer structure
  120. *
  121. * Current contents of destination print buffer (if any) are discarded.
  122. * Source print buffer becomes empty if a successful move occurs.
  123. */
  124. static void tipc_printbuf_move(struct print_buf *pb_to,
  125. struct print_buf *pb_from)
  126. {
  127. int len;
  128. /* Handle the cases where contents can't be moved */
  129. if (!pb_to->buf)
  130. return;
  131. if (!pb_from->buf) {
  132. tipc_printbuf_reset(pb_to);
  133. return;
  134. }
  135. if (pb_to->size < pb_from->size) {
  136. strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***");
  137. pb_to->buf[pb_to->size - 1] = ~0;
  138. pb_to->crs = strchr(pb_to->buf, 0);
  139. return;
  140. }
  141. /* Copy data from char after cursor to end (if used) */
  142. len = pb_from->buf + pb_from->size - pb_from->crs - 2;
  143. if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) {
  144. strcpy(pb_to->buf, pb_from->crs + 1);
  145. pb_to->crs = pb_to->buf + len;
  146. } else
  147. pb_to->crs = pb_to->buf;
  148. /* Copy data from start to cursor (always) */
  149. len = pb_from->crs - pb_from->buf;
  150. strcpy(pb_to->crs, pb_from->buf);
  151. pb_to->crs += len;
  152. tipc_printbuf_reset(pb_from);
  153. }
  154. /**
  155. * tipc_snprintf - append formatted output to print buffer
  156. * @buf: pointer to print buffer
  157. * @len: buffer length
  158. * @fmt: formatted info to be printed
  159. */
  160. int tipc_snprintf(char *buf, int len, const char *fmt, ...)
  161. {
  162. int i;
  163. va_list args;
  164. va_start(args, fmt);
  165. i = vscnprintf(buf, len, fmt, args);
  166. va_end(args);
  167. return i;
  168. }
  169. /**
  170. * tipc_log_resize - change the size of the TIPC log buffer
  171. * @log_size: print buffer size to use
  172. */
  173. int tipc_log_resize(int log_size)
  174. {
  175. int res = 0;
  176. spin_lock_bh(&print_lock);
  177. kfree(TIPC_LOG->buf);
  178. TIPC_LOG->buf = NULL;
  179. if (log_size) {
  180. if (log_size < TIPC_PB_MIN_SIZE)
  181. log_size = TIPC_PB_MIN_SIZE;
  182. res = TIPC_LOG->echo;
  183. tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC),
  184. log_size);
  185. TIPC_LOG->echo = res;
  186. res = !TIPC_LOG->buf;
  187. }
  188. spin_unlock_bh(&print_lock);
  189. return res;
  190. }
  191. /**
  192. * tipc_log_resize_cmd - reconfigure size of TIPC log buffer
  193. */
  194. struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space)
  195. {
  196. u32 value;
  197. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
  198. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  199. value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  200. if (value > 32768)
  201. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  202. " (log size must be 0-32768)");
  203. if (tipc_log_resize(value))
  204. return tipc_cfg_reply_error_string(
  205. "unable to create specified log (log size is now 0)");
  206. return tipc_cfg_reply_none();
  207. }
  208. /**
  209. * tipc_log_dump - capture TIPC log buffer contents in configuration message
  210. */
  211. struct sk_buff *tipc_log_dump(void)
  212. {
  213. struct sk_buff *reply;
  214. spin_lock_bh(&print_lock);
  215. if (!TIPC_LOG->buf) {
  216. spin_unlock_bh(&print_lock);
  217. reply = tipc_cfg_reply_ultra_string("log not activated\n");
  218. } else if (tipc_printbuf_empty(TIPC_LOG)) {
  219. spin_unlock_bh(&print_lock);
  220. reply = tipc_cfg_reply_ultra_string("log is empty\n");
  221. } else {
  222. struct tlv_desc *rep_tlv;
  223. struct print_buf pb;
  224. int str_len;
  225. str_len = min(TIPC_LOG->size, 32768u);
  226. spin_unlock_bh(&print_lock);
  227. reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
  228. if (reply) {
  229. rep_tlv = (struct tlv_desc *)reply->data;
  230. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
  231. spin_lock_bh(&print_lock);
  232. tipc_printbuf_move(&pb, TIPC_LOG);
  233. spin_unlock_bh(&print_lock);
  234. str_len = strlen(TLV_DATA(rep_tlv)) + 1;
  235. skb_put(reply, TLV_SPACE(str_len));
  236. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  237. }
  238. }
  239. return reply;
  240. }