log.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/stddef.h>
  33. #include <linux/types.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/wait.h>
  36. #include <stdarg.h>
  37. #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
  38. static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
  39. static char *batadv_log_char_addr(struct batadv_priv_debug_log *debug_log,
  40. size_t idx)
  41. {
  42. return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
  43. }
  44. static void batadv_emit_log_char(struct batadv_priv_debug_log *debug_log,
  45. char c)
  46. {
  47. char *char_addr;
  48. char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
  49. *char_addr = c;
  50. debug_log->log_end++;
  51. if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
  52. debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
  53. }
  54. __printf(2, 3)
  55. static int batadv_fdebug_log(struct batadv_priv_debug_log *debug_log,
  56. const char *fmt, ...)
  57. {
  58. va_list args;
  59. static char debug_log_buf[256];
  60. char *p;
  61. if (!debug_log)
  62. return 0;
  63. spin_lock_bh(&debug_log->lock);
  64. va_start(args, fmt);
  65. vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
  66. va_end(args);
  67. for (p = debug_log_buf; *p != 0; p++)
  68. batadv_emit_log_char(debug_log, *p);
  69. spin_unlock_bh(&debug_log->lock);
  70. wake_up(&debug_log->queue_wait);
  71. return 0;
  72. }
  73. int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
  74. {
  75. va_list args;
  76. char tmp_log_buf[256];
  77. va_start(args, fmt);
  78. vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
  79. batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
  80. jiffies_to_msecs(jiffies), tmp_log_buf);
  81. va_end(args);
  82. return 0;
  83. }
  84. static int batadv_log_open(struct inode *inode, struct file *file)
  85. {
  86. if (!try_module_get(THIS_MODULE))
  87. return -EBUSY;
  88. nonseekable_open(inode, file);
  89. file->private_data = inode->i_private;
  90. return 0;
  91. }
  92. static int batadv_log_release(struct inode *inode, struct file *file)
  93. {
  94. module_put(THIS_MODULE);
  95. return 0;
  96. }
  97. static bool batadv_log_empty(struct batadv_priv_debug_log *debug_log)
  98. {
  99. return !(debug_log->log_start - debug_log->log_end);
  100. }
  101. static ssize_t batadv_log_read(struct file *file, char __user *buf,
  102. size_t count, loff_t *ppos)
  103. {
  104. struct batadv_priv *bat_priv = file->private_data;
  105. struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
  106. int error, i = 0;
  107. char *char_addr;
  108. char c;
  109. if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
  110. return -EAGAIN;
  111. if (!buf)
  112. return -EINVAL;
  113. if (count == 0)
  114. return 0;
  115. if (!access_ok(VERIFY_WRITE, buf, count))
  116. return -EFAULT;
  117. error = wait_event_interruptible(debug_log->queue_wait,
  118. (!batadv_log_empty(debug_log)));
  119. if (error)
  120. return error;
  121. spin_lock_bh(&debug_log->lock);
  122. while ((!error) && (i < count) &&
  123. (debug_log->log_start != debug_log->log_end)) {
  124. char_addr = batadv_log_char_addr(debug_log,
  125. debug_log->log_start);
  126. c = *char_addr;
  127. debug_log->log_start++;
  128. spin_unlock_bh(&debug_log->lock);
  129. error = __put_user(c, buf);
  130. spin_lock_bh(&debug_log->lock);
  131. buf++;
  132. i++;
  133. }
  134. spin_unlock_bh(&debug_log->lock);
  135. if (!error)
  136. return i;
  137. return error;
  138. }
  139. static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
  140. {
  141. struct batadv_priv *bat_priv = file->private_data;
  142. struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
  143. poll_wait(file, &debug_log->queue_wait, wait);
  144. if (!batadv_log_empty(debug_log))
  145. return POLLIN | POLLRDNORM;
  146. return 0;
  147. }
  148. static const struct file_operations batadv_log_fops = {
  149. .open = batadv_log_open,
  150. .release = batadv_log_release,
  151. .read = batadv_log_read,
  152. .poll = batadv_log_poll,
  153. .llseek = no_llseek,
  154. };
  155. int batadv_debug_log_setup(struct batadv_priv *bat_priv)
  156. {
  157. struct dentry *d;
  158. if (!bat_priv->debug_dir)
  159. goto err;
  160. bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
  161. if (!bat_priv->debug_log)
  162. goto err;
  163. spin_lock_init(&bat_priv->debug_log->lock);
  164. init_waitqueue_head(&bat_priv->debug_log->queue_wait);
  165. d = debugfs_create_file("log", 0400, bat_priv->debug_dir, bat_priv,
  166. &batadv_log_fops);
  167. if (!d)
  168. goto err;
  169. return 0;
  170. err:
  171. return -ENOMEM;
  172. }
  173. void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
  174. {
  175. kfree(bat_priv->debug_log);
  176. bat_priv->debug_log = NULL;
  177. }