divert_procfs.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* $Id: divert_procfs.c,v 1.11.6.2 2001/09/23 22:24:36 kai Exp $
  2. *
  3. * Filesystem handling for the diversion supplementary services.
  4. *
  5. * Copyright 1998 by Werner Cornelius (werner@isdn4linux.de)
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/poll.h>
  13. #include <linux/slab.h>
  14. #ifdef CONFIG_PROC_FS
  15. #include <linux/proc_fs.h>
  16. #else
  17. #include <linux/fs.h>
  18. #endif
  19. #include <linux/sched.h>
  20. #include <linux/isdnif.h>
  21. #include <net/net_namespace.h>
  22. #include <linux/mutex.h>
  23. #include "isdn_divert.h"
  24. /*********************************/
  25. /* Variables for interface queue */
  26. /*********************************/
  27. ulong if_used = 0; /* number of interface users */
  28. static DEFINE_MUTEX(isdn_divert_mutex);
  29. static struct divert_info *divert_info_head = NULL; /* head of queue */
  30. static struct divert_info *divert_info_tail = NULL; /* pointer to last entry */
  31. static DEFINE_SPINLOCK(divert_info_lock);/* lock for queue */
  32. static wait_queue_head_t rd_queue;
  33. /*********************************/
  34. /* put an info buffer into queue */
  35. /*********************************/
  36. void
  37. put_info_buffer(char *cp)
  38. {
  39. struct divert_info *ib;
  40. unsigned long flags;
  41. if (if_used <= 0)
  42. return;
  43. if (!cp)
  44. return;
  45. if (!*cp)
  46. return;
  47. if (!(ib = kmalloc(sizeof(struct divert_info) + strlen(cp), GFP_ATOMIC)))
  48. return; /* no memory */
  49. strcpy(ib->info_start, cp); /* set output string */
  50. ib->next = NULL;
  51. spin_lock_irqsave(&divert_info_lock, flags);
  52. ib->usage_cnt = if_used;
  53. if (!divert_info_head)
  54. divert_info_head = ib; /* new head */
  55. else
  56. divert_info_tail->next = ib; /* follows existing messages */
  57. divert_info_tail = ib; /* new tail */
  58. /* delete old entrys */
  59. while (divert_info_head->next) {
  60. if ((divert_info_head->usage_cnt <= 0) &&
  61. (divert_info_head->next->usage_cnt <= 0)) {
  62. ib = divert_info_head;
  63. divert_info_head = divert_info_head->next;
  64. kfree(ib);
  65. } else
  66. break;
  67. } /* divert_info_head->next */
  68. spin_unlock_irqrestore(&divert_info_lock, flags);
  69. wake_up_interruptible(&(rd_queue));
  70. } /* put_info_buffer */
  71. #ifdef CONFIG_PROC_FS
  72. /**********************************/
  73. /* deflection device read routine */
  74. /**********************************/
  75. static ssize_t
  76. isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off)
  77. {
  78. struct divert_info *inf;
  79. int len;
  80. if (!(inf = *((struct divert_info **) file->private_data))) {
  81. if (file->f_flags & O_NONBLOCK)
  82. return -EAGAIN;
  83. wait_event_interruptible(rd_queue, (inf =
  84. *((struct divert_info **) file->private_data)));
  85. }
  86. if (!inf)
  87. return (0);
  88. inf->usage_cnt--; /* new usage count */
  89. file->private_data = &inf->next; /* next structure */
  90. if ((len = strlen(inf->info_start)) <= count) {
  91. if (copy_to_user(buf, inf->info_start, len))
  92. return -EFAULT;
  93. *off += len;
  94. return (len);
  95. }
  96. return (0);
  97. } /* isdn_divert_read */
  98. /**********************************/
  99. /* deflection device write routine */
  100. /**********************************/
  101. static ssize_t
  102. isdn_divert_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
  103. {
  104. return (-ENODEV);
  105. } /* isdn_divert_write */
  106. /***************************************/
  107. /* select routines for various kernels */
  108. /***************************************/
  109. static unsigned int
  110. isdn_divert_poll(struct file *file, poll_table *wait)
  111. {
  112. unsigned int mask = 0;
  113. poll_wait(file, &(rd_queue), wait);
  114. /* mask = POLLOUT | POLLWRNORM; */
  115. if (*((struct divert_info **) file->private_data)) {
  116. mask |= POLLIN | POLLRDNORM;
  117. }
  118. return mask;
  119. } /* isdn_divert_poll */
  120. /****************/
  121. /* Open routine */
  122. /****************/
  123. static int
  124. isdn_divert_open(struct inode *ino, struct file *filep)
  125. {
  126. unsigned long flags;
  127. spin_lock_irqsave(&divert_info_lock, flags);
  128. if_used++;
  129. if (divert_info_head)
  130. filep->private_data = &(divert_info_tail->next);
  131. else
  132. filep->private_data = &divert_info_head;
  133. spin_unlock_irqrestore(&divert_info_lock, flags);
  134. /* start_divert(); */
  135. return nonseekable_open(ino, filep);
  136. } /* isdn_divert_open */
  137. /*******************/
  138. /* close routine */
  139. /*******************/
  140. static int
  141. isdn_divert_close(struct inode *ino, struct file *filep)
  142. {
  143. struct divert_info *inf;
  144. unsigned long flags;
  145. spin_lock_irqsave(&divert_info_lock, flags);
  146. if_used--;
  147. inf = *((struct divert_info **) filep->private_data);
  148. while (inf) {
  149. inf->usage_cnt--;
  150. inf = inf->next;
  151. }
  152. if (if_used <= 0)
  153. while (divert_info_head) {
  154. inf = divert_info_head;
  155. divert_info_head = divert_info_head->next;
  156. kfree(inf);
  157. }
  158. spin_unlock_irqrestore(&divert_info_lock, flags);
  159. return (0);
  160. } /* isdn_divert_close */
  161. /*********/
  162. /* IOCTL */
  163. /*********/
  164. static int isdn_divert_ioctl_unlocked(struct file *file, uint cmd, ulong arg)
  165. {
  166. divert_ioctl dioctl;
  167. int i;
  168. unsigned long flags;
  169. divert_rule *rulep;
  170. char *cp;
  171. if (copy_from_user(&dioctl, (void __user *) arg, sizeof(dioctl)))
  172. return -EFAULT;
  173. switch (cmd) {
  174. case IIOCGETVER:
  175. dioctl.drv_version = DIVERT_IIOC_VERSION; /* set version */
  176. break;
  177. case IIOCGETDRV:
  178. if ((dioctl.getid.drvid = divert_if.name_to_drv(dioctl.getid.drvnam)) < 0)
  179. return (-EINVAL);
  180. break;
  181. case IIOCGETNAM:
  182. cp = divert_if.drv_to_name(dioctl.getid.drvid);
  183. if (!cp)
  184. return (-EINVAL);
  185. if (!*cp)
  186. return (-EINVAL);
  187. strcpy(dioctl.getid.drvnam, cp);
  188. break;
  189. case IIOCGETRULE:
  190. if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
  191. return (-EINVAL);
  192. dioctl.getsetrule.rule = *rulep; /* copy data */
  193. break;
  194. case IIOCMODRULE:
  195. if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
  196. return (-EINVAL);
  197. spin_lock_irqsave(&divert_lock, flags);
  198. *rulep = dioctl.getsetrule.rule; /* copy data */
  199. spin_unlock_irqrestore(&divert_lock, flags);
  200. return (0); /* no copy required */
  201. break;
  202. case IIOCINSRULE:
  203. return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule));
  204. break;
  205. case IIOCDELRULE:
  206. return (deleterule(dioctl.getsetrule.ruleidx));
  207. break;
  208. case IIOCDODFACT:
  209. return (deflect_extern_action(dioctl.fwd_ctrl.subcmd,
  210. dioctl.fwd_ctrl.callid,
  211. dioctl.fwd_ctrl.to_nr));
  212. case IIOCDOCFACT:
  213. case IIOCDOCFDIS:
  214. case IIOCDOCFINT:
  215. if (!divert_if.drv_to_name(dioctl.cf_ctrl.drvid))
  216. return (-EINVAL); /* invalid driver */
  217. if (strnlen(dioctl.cf_ctrl.msn, sizeof(dioctl.cf_ctrl.msn)) ==
  218. sizeof(dioctl.cf_ctrl.msn))
  219. return -EINVAL;
  220. if (strnlen(dioctl.cf_ctrl.fwd_nr, sizeof(dioctl.cf_ctrl.fwd_nr)) ==
  221. sizeof(dioctl.cf_ctrl.fwd_nr))
  222. return -EINVAL;
  223. if ((i = cf_command(dioctl.cf_ctrl.drvid,
  224. (cmd == IIOCDOCFACT) ? 1 : (cmd == IIOCDOCFDIS) ? 0 : 2,
  225. dioctl.cf_ctrl.cfproc,
  226. dioctl.cf_ctrl.msn,
  227. dioctl.cf_ctrl.service,
  228. dioctl.cf_ctrl.fwd_nr,
  229. &dioctl.cf_ctrl.procid)))
  230. return (i);
  231. break;
  232. default:
  233. return (-EINVAL);
  234. } /* switch cmd */
  235. return copy_to_user((void __user *)arg, &dioctl, sizeof(dioctl)) ? -EFAULT : 0;
  236. } /* isdn_divert_ioctl */
  237. static long isdn_divert_ioctl(struct file *file, uint cmd, ulong arg)
  238. {
  239. long ret;
  240. mutex_lock(&isdn_divert_mutex);
  241. ret = isdn_divert_ioctl_unlocked(file, cmd, arg);
  242. mutex_unlock(&isdn_divert_mutex);
  243. return ret;
  244. }
  245. static const struct file_operations isdn_fops =
  246. {
  247. .owner = THIS_MODULE,
  248. .llseek = no_llseek,
  249. .read = isdn_divert_read,
  250. .write = isdn_divert_write,
  251. .poll = isdn_divert_poll,
  252. .unlocked_ioctl = isdn_divert_ioctl,
  253. .open = isdn_divert_open,
  254. .release = isdn_divert_close,
  255. };
  256. /****************************/
  257. /* isdn subdir in /proc/net */
  258. /****************************/
  259. static struct proc_dir_entry *isdn_proc_entry = NULL;
  260. static struct proc_dir_entry *isdn_divert_entry = NULL;
  261. #endif /* CONFIG_PROC_FS */
  262. /***************************************************************************/
  263. /* divert_dev_init must be called before the proc filesystem may be used */
  264. /***************************************************************************/
  265. int
  266. divert_dev_init(void)
  267. {
  268. init_waitqueue_head(&rd_queue);
  269. #ifdef CONFIG_PROC_FS
  270. isdn_proc_entry = proc_mkdir("isdn", init_net.proc_net);
  271. if (!isdn_proc_entry)
  272. return (-1);
  273. isdn_divert_entry = proc_create("divert", S_IFREG | S_IRUGO,
  274. isdn_proc_entry, &isdn_fops);
  275. if (!isdn_divert_entry) {
  276. remove_proc_entry("isdn", init_net.proc_net);
  277. return (-1);
  278. }
  279. #endif /* CONFIG_PROC_FS */
  280. return (0);
  281. } /* divert_dev_init */
  282. /***************************************************************************/
  283. /* divert_dev_deinit must be called before leaving isdn when included as */
  284. /* a module. */
  285. /***************************************************************************/
  286. int
  287. divert_dev_deinit(void)
  288. {
  289. #ifdef CONFIG_PROC_FS
  290. remove_proc_entry("divert", isdn_proc_entry);
  291. remove_proc_entry("isdn", init_net.proc_net);
  292. #endif /* CONFIG_PROC_FS */
  293. return (0);
  294. } /* divert_dev_deinit */