]>
Commit | Line | Data |
---|---|---|
0b52b749 BP |
1 | /***************************************************************************** |
2 | * | |
3 | * Copyright 1999 Digi International (www.digi.com) | |
4 | * James Puzzo <jamesp at digi dot com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2, or (at your option) | |
9 | * any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the | |
13 | * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
14 | * PURPOSE. See the GNU General Public License for more details. | |
15 | * | |
16 | */ | |
17 | ||
18 | /* | |
19 | * | |
20 | * Filename: | |
21 | * | |
22 | * dgrp_mon_ops.c | |
23 | * | |
24 | * Description: | |
25 | * | |
26 | * Handle the file operations required for the "monitor" devices. | |
27 | * Includes those functions required to register the "mon" devices | |
28 | * in "/proc". | |
29 | * | |
30 | * Author: | |
31 | * | |
32 | * James A. Puzzo | |
33 | * | |
34 | */ | |
35 | ||
36 | #include <linux/module.h> | |
37 | #include <linux/tty.h> | |
38 | #include <linux/sched.h> | |
39 | #include <asm/unaligned.h> | |
40 | #include <linux/proc_fs.h> | |
ee98581f | 41 | #include <linux/uaccess.h> |
0b52b749 BP |
42 | |
43 | #include "dgrp_common.h" | |
44 | ||
45 | /* File operation declarations */ | |
46 | static int dgrp_mon_open(struct inode *, struct file *); | |
47 | static int dgrp_mon_release(struct inode *, struct file *); | |
48 | static ssize_t dgrp_mon_read(struct file *, char __user *, size_t, loff_t *); | |
49 | static long dgrp_mon_ioctl(struct file *file, unsigned int cmd, | |
50 | unsigned long arg); | |
51 | ||
af064cdd | 52 | const struct file_operations dgrp_mon_ops = { |
0b52b749 BP |
53 | .owner = THIS_MODULE, |
54 | .read = dgrp_mon_read, | |
55 | .unlocked_ioctl = dgrp_mon_ioctl, | |
56 | .open = dgrp_mon_open, | |
57 | .release = dgrp_mon_release, | |
58 | }; | |
59 | ||
0b52b749 BP |
60 | /** |
61 | * dgrp_mon_open() -- open /proc/dgrp/ports device for a PortServer | |
62 | * @inode: struct inode * | |
63 | * @file: struct file * | |
64 | * | |
65 | * Open function to open the /proc/dgrp/ports device for a PortServer. | |
66 | */ | |
67 | static int dgrp_mon_open(struct inode *inode, struct file *file) | |
68 | { | |
69 | struct nd_struct *nd; | |
0b52b749 BP |
70 | struct timeval tv; |
71 | uint32_t time; | |
72 | u8 *buf; | |
73 | int rtn; | |
74 | ||
75 | rtn = try_module_get(THIS_MODULE); | |
76 | if (!rtn) | |
77 | return -ENXIO; | |
78 | ||
79 | rtn = 0; | |
80 | ||
81 | if (!capable(CAP_SYS_ADMIN)) { | |
82 | rtn = -EPERM; | |
83 | goto done; | |
84 | } | |
85 | ||
86 | /* | |
87 | * Make sure that the "private_data" field hasn't already been used. | |
88 | */ | |
89 | if (file->private_data) { | |
90 | rtn = -EINVAL; | |
91 | goto done; | |
92 | } | |
93 | ||
94 | /* | |
95 | * Get the node pointer, and fail if it doesn't exist. | |
96 | */ | |
d9dda78b | 97 | nd = PDE_DATA(inode); |
0b52b749 BP |
98 | if (!nd) { |
99 | rtn = -ENXIO; | |
100 | goto done; | |
101 | } | |
102 | ||
103 | file->private_data = (void *) nd; | |
104 | ||
105 | /* | |
106 | * Allocate the monitor buffer. | |
107 | */ | |
108 | ||
109 | /* | |
110 | * Grab the MON lock. | |
111 | */ | |
112 | down(&nd->nd_mon_semaphore); | |
113 | ||
114 | if (nd->nd_mon_buf) { | |
115 | rtn = -EBUSY; | |
116 | goto done_up; | |
117 | } | |
118 | ||
119 | nd->nd_mon_buf = kmalloc(MON_MAX, GFP_KERNEL); | |
120 | ||
121 | if (!nd->nd_mon_buf) { | |
122 | rtn = -ENOMEM; | |
123 | goto done_up; | |
124 | } | |
125 | ||
126 | /* | |
127 | * Enter an RPDUMP file header into the buffer. | |
128 | */ | |
129 | ||
130 | buf = nd->nd_mon_buf; | |
131 | ||
132 | strcpy(buf, RPDUMP_MAGIC); | |
133 | buf += strlen(buf) + 1; | |
134 | ||
135 | do_gettimeofday(&tv); | |
136 | ||
137 | /* | |
138 | * tv.tv_sec might be a 64 bit quantity. Pare | |
139 | * it down to 32 bits before attempting to encode | |
140 | * it. | |
141 | */ | |
142 | time = (uint32_t) (tv.tv_sec & 0xffffffff); | |
143 | ||
144 | put_unaligned_be32(time, buf); | |
145 | put_unaligned_be16(0, buf + 4); | |
146 | buf += 6; | |
147 | ||
148 | if (nd->nd_tx_module) { | |
149 | buf[0] = RPDUMP_CLIENT; | |
150 | put_unaligned_be32(0, buf + 1); | |
151 | put_unaligned_be16(1, buf + 5); | |
152 | buf[7] = 0xf0 + nd->nd_tx_module; | |
153 | buf += 8; | |
154 | } | |
155 | ||
156 | if (nd->nd_rx_module) { | |
157 | buf[0] = RPDUMP_SERVER; | |
158 | put_unaligned_be32(0, buf + 1); | |
159 | put_unaligned_be16(1, buf + 5); | |
160 | buf[7] = 0xf0 + nd->nd_rx_module; | |
161 | buf += 8; | |
162 | } | |
163 | ||
164 | nd->nd_mon_out = 0; | |
165 | nd->nd_mon_in = buf - nd->nd_mon_buf; | |
166 | nd->nd_mon_lbolt = jiffies; | |
167 | ||
168 | done_up: | |
169 | up(&nd->nd_mon_semaphore); | |
170 | ||
171 | done: | |
172 | if (rtn) | |
173 | module_put(THIS_MODULE); | |
174 | return rtn; | |
175 | } | |
176 | ||
177 | ||
178 | /** | |
179 | * dgrp_mon_release() - Close the MON device for a particular PortServer | |
180 | * @inode: struct inode * | |
181 | * @file: struct file * | |
182 | */ | |
183 | static int dgrp_mon_release(struct inode *inode, struct file *file) | |
184 | { | |
185 | struct nd_struct *nd; | |
186 | ||
187 | /* | |
188 | * Get the node pointer, and quit if it doesn't exist. | |
189 | */ | |
190 | nd = (struct nd_struct *)(file->private_data); | |
191 | if (!nd) | |
192 | goto done; | |
193 | ||
194 | /* | |
195 | * Free the monitor buffer. | |
196 | */ | |
197 | ||
198 | down(&nd->nd_mon_semaphore); | |
199 | ||
200 | kfree(nd->nd_mon_buf); | |
201 | nd->nd_mon_buf = NULL; | |
202 | nd->nd_mon_out = nd->nd_mon_in; | |
203 | ||
204 | /* | |
205 | * Wakeup any thread waiting for buffer space. | |
206 | */ | |
207 | ||
208 | if (nd->nd_mon_flag & MON_WAIT_SPACE) { | |
209 | nd->nd_mon_flag &= ~MON_WAIT_SPACE; | |
210 | wake_up_interruptible(&nd->nd_mon_wqueue); | |
211 | } | |
212 | ||
213 | up(&nd->nd_mon_semaphore); | |
214 | ||
215 | /* | |
216 | * Make sure there is no thread in the middle of writing a packet. | |
217 | */ | |
218 | down(&nd->nd_net_semaphore); | |
219 | up(&nd->nd_net_semaphore); | |
220 | ||
221 | done: | |
222 | module_put(THIS_MODULE); | |
223 | file->private_data = NULL; | |
224 | return 0; | |
225 | } | |
226 | ||
227 | /** | |
228 | * dgrp_mon_read() -- Copy data from the monitoring buffer to the user | |
229 | */ | |
230 | static ssize_t dgrp_mon_read(struct file *file, char __user *buf, size_t count, | |
231 | loff_t *ppos) | |
232 | { | |
233 | struct nd_struct *nd; | |
234 | int r; | |
235 | int offset = 0; | |
236 | int res = 0; | |
237 | ssize_t rtn; | |
238 | ||
239 | /* | |
240 | * Get the node pointer, and quit if it doesn't exist. | |
241 | */ | |
242 | nd = (struct nd_struct *)(file->private_data); | |
243 | if (!nd) | |
244 | return -ENXIO; | |
245 | ||
246 | /* | |
247 | * Wait for some data to appear in the buffer. | |
248 | */ | |
249 | ||
250 | down(&nd->nd_mon_semaphore); | |
251 | ||
252 | for (;;) { | |
253 | res = (nd->nd_mon_in - nd->nd_mon_out) & MON_MASK; | |
254 | ||
255 | if (res) | |
256 | break; | |
257 | ||
258 | nd->nd_mon_flag |= MON_WAIT_DATA; | |
259 | ||
260 | up(&nd->nd_mon_semaphore); | |
261 | ||
262 | /* | |
263 | * Go to sleep waiting until the condition becomes true. | |
264 | */ | |
265 | rtn = wait_event_interruptible(nd->nd_mon_wqueue, | |
266 | ((nd->nd_mon_flag & MON_WAIT_DATA) == 0)); | |
267 | ||
268 | if (rtn) | |
269 | return rtn; | |
270 | ||
271 | down(&nd->nd_mon_semaphore); | |
272 | } | |
273 | ||
274 | /* | |
275 | * Read whatever is there. | |
276 | */ | |
277 | ||
278 | if (res > count) | |
279 | res = count; | |
280 | ||
281 | r = MON_MAX - nd->nd_mon_out; | |
282 | ||
283 | if (r <= res) { | |
284 | rtn = copy_to_user((void __user *)buf, | |
285 | nd->nd_mon_buf + nd->nd_mon_out, r); | |
286 | if (rtn) { | |
287 | up(&nd->nd_mon_semaphore); | |
288 | return -EFAULT; | |
289 | } | |
290 | ||
291 | nd->nd_mon_out = 0; | |
292 | res -= r; | |
293 | offset = r; | |
294 | } | |
295 | ||
296 | rtn = copy_to_user((void __user *) buf + offset, | |
297 | nd->nd_mon_buf + nd->nd_mon_out, res); | |
298 | if (rtn) { | |
299 | up(&nd->nd_mon_semaphore); | |
300 | return -EFAULT; | |
301 | } | |
302 | ||
303 | nd->nd_mon_out += res; | |
304 | ||
305 | *ppos += res; | |
306 | ||
307 | up(&nd->nd_mon_semaphore); | |
308 | ||
309 | /* | |
310 | * Wakeup any thread waiting for buffer space. | |
311 | */ | |
312 | ||
313 | if (nd->nd_mon_flag & MON_WAIT_SPACE) { | |
314 | nd->nd_mon_flag &= ~MON_WAIT_SPACE; | |
315 | wake_up_interruptible(&nd->nd_mon_wqueue); | |
316 | } | |
317 | ||
318 | return res; | |
319 | } | |
320 | ||
321 | /* ioctl is not valid on monitor device */ | |
322 | static long dgrp_mon_ioctl(struct file *file, unsigned int cmd, | |
323 | unsigned long arg) | |
324 | { | |
325 | return -EINVAL; | |
326 | } |