divert_procfs.c 8.6 KB

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