log.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2010-2018 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "log.h"
  19. #include "main.h"
  20. #include <linux/compiler.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/errno.h>
  23. #include <linux/eventpoll.h>
  24. #include <linux/export.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/fs.h>
  27. #include <linux/gfp.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/poll.h>
  32. #include <linux/sched.h> /* for linux/wait.h */
  33. #include <linux/slab.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/stddef.h>
  36. #include <linux/types.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/wait.h>
  39. #include <stdarg.h>
  40. #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
  41. static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
  42. static char *batadv_log_char_addr(struct batadv_priv_debug_log *debug_log,
  43. size_t idx)
  44. {
  45. return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
  46. }
  47. static void batadv_emit_log_char(struct batadv_priv_debug_log *debug_log,
  48. char c)
  49. {
  50. char *char_addr;
  51. char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
  52. *char_addr = c;
  53. debug_log->log_end++;
  54. if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
  55. debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
  56. }
  57. __printf(2, 3)
  58. static int batadv_fdebug_log(struct batadv_priv_debug_log *debug_log,
  59. const char *fmt, ...)
  60. {
  61. va_list args;
  62. static char debug_log_buf[256];
  63. char *p;
  64. if (!debug_log)
  65. return 0;
  66. spin_lock_bh(&debug_log->lock);
  67. va_start(args, fmt);
  68. vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
  69. va_end(args);
  70. for (p = debug_log_buf; *p != 0; p++)
  71. batadv_emit_log_char(debug_log, *p);
  72. spin_unlock_bh(&debug_log->lock);
  73. wake_up(&debug_log->queue_wait);
  74. return 0;
  75. }
  76. /**
  77. * batadv_debug_log() - Add debug log entry
  78. * @bat_priv: the bat priv with all the soft interface information
  79. * @fmt: format string
  80. *
  81. * Return: 0 on success or negative error number in case of failure
  82. */
  83. int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
  84. {
  85. va_list args;
  86. char tmp_log_buf[256];
  87. va_start(args, fmt);
  88. vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
  89. batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
  90. jiffies_to_msecs(jiffies), tmp_log_buf);
  91. va_end(args);
  92. return 0;
  93. }
  94. static int batadv_log_open(struct inode *inode, struct file *file)
  95. {
  96. if (!try_module_get(THIS_MODULE))
  97. return -EBUSY;
  98. nonseekable_open(inode, file);
  99. file->private_data = inode->i_private;
  100. return 0;
  101. }
  102. static int batadv_log_release(struct inode *inode, struct file *file)
  103. {
  104. module_put(THIS_MODULE);
  105. return 0;
  106. }
  107. static bool batadv_log_empty(struct batadv_priv_debug_log *debug_log)
  108. {
  109. return !(debug_log->log_start - debug_log->log_end);
  110. }
  111. static ssize_t batadv_log_read(struct file *file, char __user *buf,
  112. size_t count, loff_t *ppos)
  113. {
  114. struct batadv_priv *bat_priv = file->private_data;
  115. struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
  116. int error, i = 0;
  117. char *char_addr;
  118. char c;
  119. if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
  120. return -EAGAIN;
  121. if (!buf)
  122. return -EINVAL;
  123. if (count == 0)
  124. return 0;
  125. if (!access_ok(VERIFY_WRITE, buf, count))
  126. return -EFAULT;
  127. error = wait_event_interruptible(debug_log->queue_wait,
  128. (!batadv_log_empty(debug_log)));
  129. if (error)
  130. return error;
  131. spin_lock_bh(&debug_log->lock);
  132. while ((!error) && (i < count) &&
  133. (debug_log->log_start != debug_log->log_end)) {
  134. char_addr = batadv_log_char_addr(debug_log,
  135. debug_log->log_start);
  136. c = *char_addr;
  137. debug_log->log_start++;
  138. spin_unlock_bh(&debug_log->lock);
  139. error = __put_user(c, buf);
  140. spin_lock_bh(&debug_log->lock);
  141. buf++;
  142. i++;
  143. }
  144. spin_unlock_bh(&debug_log->lock);
  145. if (!error)
  146. return i;
  147. return error;
  148. }
  149. static __poll_t batadv_log_poll(struct file *file, poll_table *wait)
  150. {
  151. struct batadv_priv *bat_priv = file->private_data;
  152. struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
  153. poll_wait(file, &debug_log->queue_wait, wait);
  154. if (!batadv_log_empty(debug_log))
  155. return EPOLLIN | EPOLLRDNORM;
  156. return 0;
  157. }
  158. static const struct file_operations batadv_log_fops = {
  159. .open = batadv_log_open,
  160. .release = batadv_log_release,
  161. .read = batadv_log_read,
  162. .poll = batadv_log_poll,
  163. .llseek = no_llseek,
  164. .owner = THIS_MODULE,
  165. };
  166. /**
  167. * batadv_debug_log_setup() - Initialize debug log
  168. * @bat_priv: the bat priv with all the soft interface information
  169. *
  170. * Return: 0 on success or negative error number in case of failure
  171. */
  172. int batadv_debug_log_setup(struct batadv_priv *bat_priv)
  173. {
  174. struct dentry *d;
  175. if (!bat_priv->debug_dir)
  176. goto err;
  177. bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
  178. if (!bat_priv->debug_log)
  179. goto err;
  180. spin_lock_init(&bat_priv->debug_log->lock);
  181. init_waitqueue_head(&bat_priv->debug_log->queue_wait);
  182. d = debugfs_create_file("log", 0400, bat_priv->debug_dir, bat_priv,
  183. &batadv_log_fops);
  184. if (!d)
  185. goto err;
  186. return 0;
  187. err:
  188. return -ENOMEM;
  189. }
  190. /**
  191. * batadv_debug_log_cleanup() - Destroy debug log
  192. * @bat_priv: the bat priv with all the soft interface information
  193. */
  194. void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
  195. {
  196. kfree(bat_priv->debug_log);
  197. bat_priv->debug_log = NULL;
  198. }