1 /* $Id: divert_procfs.c,v 1.11.6.2 2001/09/23 22:24:36 kai Exp $
3 * Filesystem handling for the diversion supplementary services.
5 * Copyright 1998 by Werner Cornelius (werner@isdn4linux.de)
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/poll.h>
14 #include <linux/slab.h>
16 #include <linux/proc_fs.h>
20 #include <linux/sched.h>
21 #include <linux/isdnif.h>
22 #include <net/net_namespace.h>
23 #include <linux/mutex.h>
24 #include "isdn_divert.h"
27 /*********************************/
28 /* Variables for interface queue */
29 /*********************************/
30 ulong if_used
= 0; /* number of interface users */
31 static DEFINE_MUTEX(isdn_divert_mutex
);
32 static struct divert_info
*divert_info_head
= NULL
; /* head of queue */
33 static struct divert_info
*divert_info_tail
= NULL
; /* pointer to last entry */
34 static DEFINE_SPINLOCK(divert_info_lock
);/* lock for queue */
35 static wait_queue_head_t rd_queue
;
37 /*********************************/
38 /* put an info buffer into queue */
39 /*********************************/
41 put_info_buffer(char *cp
)
43 struct divert_info
*ib
;
52 if (!(ib
= kmalloc(sizeof(struct divert_info
) + strlen(cp
), GFP_ATOMIC
)))
53 return; /* no memory */
54 strcpy(ib
->info_start
, cp
); /* set output string */
56 spin_lock_irqsave(&divert_info_lock
, flags
);
57 ib
->usage_cnt
= if_used
;
58 if (!divert_info_head
)
59 divert_info_head
= ib
; /* new head */
61 divert_info_tail
->next
= ib
; /* follows existing messages */
62 divert_info_tail
= ib
; /* new tail */
64 /* delete old entrys */
65 while (divert_info_head
->next
) {
66 if ((divert_info_head
->usage_cnt
<= 0) &&
67 (divert_info_head
->next
->usage_cnt
<= 0)) {
68 ib
= divert_info_head
;
69 divert_info_head
= divert_info_head
->next
;
73 } /* divert_info_head->next */
74 spin_unlock_irqrestore(&divert_info_lock
, flags
);
75 wake_up_interruptible(&(rd_queue
));
76 } /* put_info_buffer */
80 /**********************************/
81 /* deflection device read routine */
82 /**********************************/
84 isdn_divert_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*off
)
86 struct divert_info
*inf
;
89 if (!(inf
= *((struct divert_info
**) file
->private_data
))) {
90 if (file
->f_flags
& O_NONBLOCK
)
92 wait_event_interruptible(rd_queue
, (inf
=
93 *((struct divert_info
**) file
->private_data
)));
98 inf
->usage_cnt
--; /* new usage count */
99 file
->private_data
= &inf
->next
; /* next structure */
100 if ((len
= strlen(inf
->info_start
)) <= count
) {
101 if (copy_to_user(buf
, inf
->info_start
, len
))
107 } /* isdn_divert_read */
109 /**********************************/
110 /* deflection device write routine */
111 /**********************************/
113 isdn_divert_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*off
)
116 } /* isdn_divert_write */
119 /***************************************/
120 /* select routines for various kernels */
121 /***************************************/
123 isdn_divert_poll(struct file
*file
, poll_table
*wait
)
127 poll_wait(file
, &(rd_queue
), wait
);
128 /* mask = EPOLLOUT | EPOLLWRNORM; */
129 if (*((struct divert_info
**) file
->private_data
)) {
130 mask
|= EPOLLIN
| EPOLLRDNORM
;
133 } /* isdn_divert_poll */
139 isdn_divert_open(struct inode
*ino
, struct file
*filep
)
143 spin_lock_irqsave(&divert_info_lock
, flags
);
145 if (divert_info_head
)
146 filep
->private_data
= &(divert_info_tail
->next
);
148 filep
->private_data
= &divert_info_head
;
149 spin_unlock_irqrestore(&divert_info_lock
, flags
);
150 /* start_divert(); */
151 return nonseekable_open(ino
, filep
);
152 } /* isdn_divert_open */
154 /*******************/
156 /*******************/
158 isdn_divert_close(struct inode
*ino
, struct file
*filep
)
160 struct divert_info
*inf
;
163 spin_lock_irqsave(&divert_info_lock
, flags
);
165 inf
= *((struct divert_info
**) filep
->private_data
);
171 while (divert_info_head
) {
172 inf
= divert_info_head
;
173 divert_info_head
= divert_info_head
->next
;
176 spin_unlock_irqrestore(&divert_info_lock
, flags
);
178 } /* isdn_divert_close */
183 static int isdn_divert_ioctl_unlocked(struct file
*file
, uint cmd
, ulong arg
)
191 if (copy_from_user(&dioctl
, (void __user
*) arg
, sizeof(dioctl
)))
196 dioctl
.drv_version
= DIVERT_IIOC_VERSION
; /* set version */
200 if ((dioctl
.getid
.drvid
= divert_if
.name_to_drv(dioctl
.getid
.drvnam
)) < 0)
205 cp
= divert_if
.drv_to_name(dioctl
.getid
.drvid
);
210 strcpy(dioctl
.getid
.drvnam
, cp
);
214 if (!(rulep
= getruleptr(dioctl
.getsetrule
.ruleidx
)))
216 dioctl
.getsetrule
.rule
= *rulep
; /* copy data */
220 if (!(rulep
= getruleptr(dioctl
.getsetrule
.ruleidx
)))
222 spin_lock_irqsave(&divert_lock
, flags
);
223 *rulep
= dioctl
.getsetrule
.rule
; /* copy data */
224 spin_unlock_irqrestore(&divert_lock
, flags
);
225 return (0); /* no copy required */
229 return (insertrule(dioctl
.getsetrule
.ruleidx
, &dioctl
.getsetrule
.rule
));
233 return (deleterule(dioctl
.getsetrule
.ruleidx
));
237 return (deflect_extern_action(dioctl
.fwd_ctrl
.subcmd
,
238 dioctl
.fwd_ctrl
.callid
,
239 dioctl
.fwd_ctrl
.to_nr
));
244 if (!divert_if
.drv_to_name(dioctl
.cf_ctrl
.drvid
))
245 return (-EINVAL
); /* invalid driver */
246 if (strnlen(dioctl
.cf_ctrl
.msn
, sizeof(dioctl
.cf_ctrl
.msn
)) ==
247 sizeof(dioctl
.cf_ctrl
.msn
))
249 if (strnlen(dioctl
.cf_ctrl
.fwd_nr
, sizeof(dioctl
.cf_ctrl
.fwd_nr
)) ==
250 sizeof(dioctl
.cf_ctrl
.fwd_nr
))
252 if ((i
= cf_command(dioctl
.cf_ctrl
.drvid
,
253 (cmd
== IIOCDOCFACT
) ? 1 : (cmd
== IIOCDOCFDIS
) ? 0 : 2,
254 dioctl
.cf_ctrl
.cfproc
,
256 dioctl
.cf_ctrl
.service
,
257 dioctl
.cf_ctrl
.fwd_nr
,
258 &dioctl
.cf_ctrl
.procid
)))
265 return copy_to_user((void __user
*)arg
, &dioctl
, sizeof(dioctl
)) ? -EFAULT
: 0;
266 } /* isdn_divert_ioctl */
268 static long isdn_divert_ioctl(struct file
*file
, uint cmd
, ulong arg
)
272 mutex_lock(&isdn_divert_mutex
);
273 ret
= isdn_divert_ioctl_unlocked(file
, cmd
, arg
);
274 mutex_unlock(&isdn_divert_mutex
);
279 static const struct file_operations isdn_fops
=
281 .owner
= THIS_MODULE
,
283 .read
= isdn_divert_read
,
284 .write
= isdn_divert_write
,
285 .poll
= isdn_divert_poll
,
286 .unlocked_ioctl
= isdn_divert_ioctl
,
287 .open
= isdn_divert_open
,
288 .release
= isdn_divert_close
,
291 /****************************/
292 /* isdn subdir in /proc/net */
293 /****************************/
294 static struct proc_dir_entry
*isdn_proc_entry
= NULL
;
295 static struct proc_dir_entry
*isdn_divert_entry
= NULL
;
296 #endif /* CONFIG_PROC_FS */
298 /***************************************************************************/
299 /* divert_dev_init must be called before the proc filesystem may be used */
300 /***************************************************************************/
302 divert_dev_init(void)
305 init_waitqueue_head(&rd_queue
);
307 #ifdef CONFIG_PROC_FS
308 isdn_proc_entry
= proc_mkdir("isdn", init_net
.proc_net
);
309 if (!isdn_proc_entry
)
311 isdn_divert_entry
= proc_create("divert", S_IFREG
| S_IRUGO
,
312 isdn_proc_entry
, &isdn_fops
);
313 if (!isdn_divert_entry
) {
314 remove_proc_entry("isdn", init_net
.proc_net
);
317 #endif /* CONFIG_PROC_FS */
320 } /* divert_dev_init */
322 /***************************************************************************/
323 /* divert_dev_deinit must be called before leaving isdn when included as */
325 /***************************************************************************/
327 divert_dev_deinit(void)
330 #ifdef CONFIG_PROC_FS
331 remove_proc_entry("divert", isdn_proc_entry
);
332 remove_proc_entry("isdn", init_net
.proc_net
);
333 #endif /* CONFIG_PROC_FS */
336 } /* divert_dev_deinit */