divert_procfs.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:8k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* $Id: divert_procfs.c,v 1.1.4.1 2001/11/20 14:19:35 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/config.h>
  12. #define __NO_VERSION__
  13. #include <linux/module.h>
  14. #include <linux/version.h>
  15. #include <linux/poll.h>
  16. #include <linux/smp_lock.h>
  17. #ifdef CONFIG_PROC_FS
  18. #include <linux/proc_fs.h>
  19. #else
  20. #include <linux/fs.h>
  21. #endif
  22. #include <linux/isdnif.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 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 = (struct divert_info *) 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. save_flags(flags);
  50. cli();
  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. restore_flags(flags);
  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. wake_up_interruptible(&(rd_queue));
  69. } /* put_info_buffer */
  70. /**********************************/
  71. /* deflection device read routine */
  72. /**********************************/
  73. static ssize_t
  74. isdn_divert_read(struct file *file, char *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. (struct divert_info **) 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. file->f_pos += 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 *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. lock_kernel();
  125. save_flags(flags);
  126. cli();
  127. if_used++;
  128. if (divert_info_head)
  129. (struct divert_info **) filep->private_data = &(divert_info_tail->next);
  130. else
  131. (struct divert_info **) filep->private_data = &divert_info_head;
  132. restore_flags(flags);
  133. /*  start_divert(); */
  134. unlock_kernel();
  135. return (0);
  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. lock_kernel();
  146. save_flags(flags);
  147. cli();
  148. if_used--;
  149. inf = *((struct divert_info **) filep->private_data);
  150. while (inf) {
  151. inf->usage_cnt--;
  152. inf = inf->next;
  153. }
  154. restore_flags(flags);
  155. if (if_used <= 0)
  156. while (divert_info_head) {
  157. inf = divert_info_head;
  158. divert_info_head = divert_info_head->next;
  159. kfree(inf);
  160. }
  161. unlock_kernel();
  162. return (0);
  163. } /* isdn_divert_close */
  164. /*********/
  165. /* IOCTL */
  166. /*********/
  167. static int
  168. isdn_divert_ioctl(struct inode *inode, struct file *file,
  169.   uint cmd, ulong arg)
  170. {
  171. divert_ioctl dioctl;
  172. int i;
  173. unsigned long flags;
  174. divert_rule *rulep;
  175. char *cp;
  176. if ((i = copy_from_user(&dioctl, (char *) arg, sizeof(dioctl))))
  177. return (i);
  178. switch (cmd) {
  179. case IIOCGETVER:
  180. dioctl.drv_version = DIVERT_IIOC_VERSION; /* set version */
  181. break;
  182. case IIOCGETDRV:
  183. if ((dioctl.getid.drvid = divert_if.name_to_drv(dioctl.getid.drvnam)) < 0)
  184. return (-EINVAL);
  185. break;
  186. case IIOCGETNAM:
  187. cp = divert_if.drv_to_name(dioctl.getid.drvid);
  188. if (!cp)
  189. return (-EINVAL);
  190. if (!*cp)
  191. return (-EINVAL);
  192. strcpy(dioctl.getid.drvnam, cp);
  193. break;
  194. case IIOCGETRULE:
  195. if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
  196. return (-EINVAL);
  197. dioctl.getsetrule.rule = *rulep; /* copy data */
  198. break;
  199. case IIOCMODRULE:
  200. if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
  201. return (-EINVAL);
  202. save_flags(flags);
  203. cli();
  204. *rulep = dioctl.getsetrule.rule; /* copy data */
  205. restore_flags(flags);
  206. return (0); /* no copy required */
  207. break;
  208. case IIOCINSRULE:
  209. return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule));
  210. break;
  211. case IIOCDELRULE:
  212. return (deleterule(dioctl.getsetrule.ruleidx));
  213. break;
  214. case IIOCDODFACT:
  215. return (deflect_extern_action(dioctl.fwd_ctrl.subcmd,
  216.   dioctl.fwd_ctrl.callid,
  217.  dioctl.fwd_ctrl.to_nr));
  218. case IIOCDOCFACT:
  219. case IIOCDOCFDIS:
  220. case IIOCDOCFINT:
  221. if (!divert_if.drv_to_name(dioctl.cf_ctrl.drvid))
  222. return (-EINVAL); /* invalid driver */
  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((char *) arg, &dioctl, sizeof(dioctl))); /* success */
  236. } /* isdn_divert_ioctl */
  237. #ifdef CONFIG_PROC_FS
  238. static struct file_operations isdn_fops =
  239. {
  240. llseek:         no_llseek,
  241. read:           isdn_divert_read,
  242. write:          isdn_divert_write,
  243. poll:           isdn_divert_poll,
  244. 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 = create_proc_entry("isdn", S_IFDIR | S_IRUGO | S_IXUGO, proc_net);
  263. if (!isdn_proc_entry)
  264. return (-1);
  265. isdn_divert_entry = create_proc_entry("divert", S_IFREG | S_IRUGO, isdn_proc_entry);
  266. if (!isdn_divert_entry) {
  267. remove_proc_entry("isdn", proc_net);
  268. return (-1);
  269. }
  270. isdn_divert_entry->proc_fops = &isdn_fops; 
  271. isdn_divert_entry->owner = THIS_MODULE; 
  272. #endif /* CONFIG_PROC_FS */
  273. return (0);
  274. } /* divert_dev_init */
  275. /***************************************************************************/
  276. /* divert_dev_deinit must be called before leaving isdn when included as   */
  277. /* a module.                                                               */
  278. /***************************************************************************/
  279. int
  280. divert_dev_deinit(void)
  281. {
  282. #ifdef CONFIG_PROC_FS
  283. remove_proc_entry("divert", isdn_proc_entry);
  284. remove_proc_entry("isdn", proc_net);
  285. #endif /* CONFIG_PROC_FS */
  286. return (0);
  287. } /* divert_dev_deinit */