divert_procfs.c 8.6 KB

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