divert_procfs.c 8.4 KB

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