log.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2010-2020 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner
  5. */
  6. #include "log.h"
  7. #include "main.h"
  8. #include <linux/compiler.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/errno.h>
  11. #include <linux/eventpoll.h>
  12. #include <linux/export.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/fs.h>
  15. #include <linux/gfp.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/poll.h>
  20. #include <linux/sched.h> /* for linux/wait.h */
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/stddef.h>
  24. #include <linux/types.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/wait.h>
  27. #include <stdarg.h>
  28. #include "debugfs.h"
  29. #include "trace.h"
  30. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  31. #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
  32. static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
  33. static char *batadv_log_char_addr(struct batadv_priv_debug_log *debug_log,
  34. size_t idx)
  35. {
  36. return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
  37. }
  38. static void batadv_emit_log_char(struct batadv_priv_debug_log *debug_log,
  39. char c)
  40. {
  41. char *char_addr;
  42. char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
  43. *char_addr = c;
  44. debug_log->log_end++;
  45. if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
  46. debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
  47. }
  48. __printf(2, 3)
  49. static int batadv_fdebug_log(struct batadv_priv_debug_log *debug_log,
  50. const char *fmt, ...)
  51. {
  52. va_list args;
  53. static char debug_log_buf[256];
  54. char *p;
  55. if (!debug_log)
  56. return 0;
  57. spin_lock_bh(&debug_log->lock);
  58. va_start(args, fmt);
  59. vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
  60. va_end(args);
  61. for (p = debug_log_buf; *p != 0; p++)
  62. batadv_emit_log_char(debug_log, *p);
  63. spin_unlock_bh(&debug_log->lock);
  64. wake_up(&debug_log->queue_wait);
  65. return 0;
  66. }
  67. static int batadv_log_open(struct inode *inode, struct file *file)
  68. {
  69. if (!try_module_get(THIS_MODULE))
  70. return -EBUSY;
  71. batadv_debugfs_deprecated(file,
  72. "Use tracepoint batadv:batadv_dbg instead\n");
  73. stream_open(inode, file);
  74. file->private_data = inode->i_private;
  75. return 0;
  76. }
  77. static int batadv_log_release(struct inode *inode, struct file *file)
  78. {
  79. module_put(THIS_MODULE);
  80. return 0;
  81. }
  82. static bool batadv_log_empty(struct batadv_priv_debug_log *debug_log)
  83. {
  84. return !(debug_log->log_start - debug_log->log_end);
  85. }
  86. static ssize_t batadv_log_read(struct file *file, char __user *buf,
  87. size_t count, loff_t *ppos)
  88. {
  89. struct batadv_priv *bat_priv = file->private_data;
  90. struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
  91. int error, i = 0;
  92. char *char_addr;
  93. char c;
  94. if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
  95. return -EAGAIN;
  96. if (!buf)
  97. return -EINVAL;
  98. if (count == 0)
  99. return 0;
  100. if (!access_ok(buf, count))
  101. return -EFAULT;
  102. error = wait_event_interruptible(debug_log->queue_wait,
  103. (!batadv_log_empty(debug_log)));
  104. if (error)
  105. return error;
  106. spin_lock_bh(&debug_log->lock);
  107. while ((!error) && (i < count) &&
  108. (debug_log->log_start != debug_log->log_end)) {
  109. char_addr = batadv_log_char_addr(debug_log,
  110. debug_log->log_start);
  111. c = *char_addr;
  112. debug_log->log_start++;
  113. spin_unlock_bh(&debug_log->lock);
  114. error = __put_user(c, buf);
  115. spin_lock_bh(&debug_log->lock);
  116. buf++;
  117. i++;
  118. }
  119. spin_unlock_bh(&debug_log->lock);
  120. if (!error)
  121. return i;
  122. return error;
  123. }
  124. static __poll_t batadv_log_poll(struct file *file, poll_table *wait)
  125. {
  126. struct batadv_priv *bat_priv = file->private_data;
  127. struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
  128. poll_wait(file, &debug_log->queue_wait, wait);
  129. if (!batadv_log_empty(debug_log))
  130. return EPOLLIN | EPOLLRDNORM;
  131. return 0;
  132. }
  133. static const struct file_operations batadv_log_fops = {
  134. .open = batadv_log_open,
  135. .release = batadv_log_release,
  136. .read = batadv_log_read,
  137. .poll = batadv_log_poll,
  138. .llseek = no_llseek,
  139. .owner = THIS_MODULE,
  140. };
  141. /**
  142. * batadv_debug_log_setup() - Initialize debug log
  143. * @bat_priv: the bat priv with all the soft interface information
  144. *
  145. * Return: 0 on success or negative error number in case of failure
  146. */
  147. int batadv_debug_log_setup(struct batadv_priv *bat_priv)
  148. {
  149. bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
  150. if (!bat_priv->debug_log)
  151. return -ENOMEM;
  152. spin_lock_init(&bat_priv->debug_log->lock);
  153. init_waitqueue_head(&bat_priv->debug_log->queue_wait);
  154. debugfs_create_file("log", 0400, bat_priv->debug_dir, bat_priv,
  155. &batadv_log_fops);
  156. return 0;
  157. }
  158. /**
  159. * batadv_debug_log_cleanup() - Destroy debug log
  160. * @bat_priv: the bat priv with all the soft interface information
  161. */
  162. void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
  163. {
  164. kfree(bat_priv->debug_log);
  165. bat_priv->debug_log = NULL;
  166. }
  167. #endif /* CONFIG_BATMAN_ADV_DEBUGFS */
  168. /**
  169. * batadv_debug_log() - Add debug log entry
  170. * @bat_priv: the bat priv with all the soft interface information
  171. * @fmt: format string
  172. *
  173. * Return: 0 on success or negative error number in case of failure
  174. */
  175. int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
  176. {
  177. struct va_format vaf;
  178. va_list args;
  179. va_start(args, fmt);
  180. vaf.fmt = fmt;
  181. vaf.va = &args;
  182. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  183. batadv_fdebug_log(bat_priv->debug_log, "[%10u] %pV",
  184. jiffies_to_msecs(jiffies), &vaf);
  185. #endif
  186. trace_batadv_dbg(bat_priv, &vaf);
  187. va_end(args);
  188. return 0;
  189. }