log.c 5.2 KB

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