divert_procfs.c 8.4 KB

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