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