This source file includes following definitions.
- pipe_read
- pipe_write
- pipe_lseek
- bad_pipe_r
- bad_pipe_w
- pipe_ioctl
- pipe_select
- fifo_select
- connect_read
- connect_select
- pipe_read_release
- pipe_write_release
- pipe_rdwr_release
- pipe_read_open
- pipe_write_open
- pipe_rdwr_open
- do_pipe
1
2
3
4
5
6
7 #include <asm/segment.h>
8
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/signal.h>
13 #include <linux/fcntl.h>
14 #include <linux/termios.h>
15 #include <linux/mm.h>
16
17
18
19
20
21
22
23
24
25 static int pipe_read(struct inode * inode, struct file * filp, char * buf, int count)
26 {
27 int chars = 0, size = 0, read = 0;
28 char *pipebuf;
29
30 if (filp->f_flags & O_NONBLOCK) {
31 if (PIPE_LOCK(*inode))
32 return -EAGAIN;
33 if (PIPE_EMPTY(*inode))
34 if (PIPE_WRITERS(*inode))
35 return -EAGAIN;
36 else
37 return 0;
38 } else while (PIPE_EMPTY(*inode) || PIPE_LOCK(*inode)) {
39 if (PIPE_EMPTY(*inode)) {
40 if (!PIPE_WRITERS(*inode))
41 return 0;
42 }
43 if (current->signal & ~current->blocked)
44 return -ERESTARTSYS;
45 interruptible_sleep_on(&PIPE_WAIT(*inode));
46 }
47 PIPE_LOCK(*inode)++;
48 while (count>0 && (size = PIPE_SIZE(*inode))) {
49 chars = PIPE_MAX_RCHUNK(*inode);
50 if (chars > count)
51 chars = count;
52 if (chars > size)
53 chars = size;
54 read += chars;
55 pipebuf = PIPE_BASE(*inode)+PIPE_START(*inode);
56 PIPE_START(*inode) += chars;
57 PIPE_START(*inode) &= (PIPE_BUF-1);
58 PIPE_LEN(*inode) -= chars;
59 count -= chars;
60 memcpy_tofs(buf, pipebuf, chars );
61 buf += chars;
62 }
63 PIPE_LOCK(*inode)--;
64 wake_up_interruptible(&PIPE_WAIT(*inode));
65 if (read)
66 return read;
67 if (PIPE_WRITERS(*inode))
68 return -EAGAIN;
69 return 0;
70 }
71
72 static int pipe_write(struct inode * inode, struct file * filp, const char * buf, int count)
73 {
74 int chars = 0, free = 0, written = 0;
75 char *pipebuf;
76
77 if (!PIPE_READERS(*inode)) {
78 send_sig(SIGPIPE,current,0);
79 return -EPIPE;
80 }
81
82 if (count <= PIPE_BUF)
83 free = count;
84 else
85 free = 1;
86 while (count>0) {
87 while ((PIPE_FREE(*inode) < free) || PIPE_LOCK(*inode)) {
88 if (!PIPE_READERS(*inode)) {
89 send_sig(SIGPIPE,current,0);
90 return written? :-EPIPE;
91 }
92 if (current->signal & ~current->blocked)
93 return written? :-ERESTARTSYS;
94 if (filp->f_flags & O_NONBLOCK)
95 return written? :-EAGAIN;
96 interruptible_sleep_on(&PIPE_WAIT(*inode));
97 }
98 PIPE_LOCK(*inode)++;
99 while (count>0 && (free = PIPE_FREE(*inode))) {
100 chars = PIPE_MAX_WCHUNK(*inode);
101 if (chars > count)
102 chars = count;
103 if (chars > free)
104 chars = free;
105 pipebuf = PIPE_BASE(*inode)+PIPE_END(*inode);
106 written += chars;
107 PIPE_LEN(*inode) += chars;
108 count -= chars;
109 memcpy_fromfs(pipebuf, buf, chars );
110 buf += chars;
111 }
112 PIPE_LOCK(*inode)--;
113 wake_up_interruptible(&PIPE_WAIT(*inode));
114 free = 1;
115 }
116 return written;
117 }
118
119 static int pipe_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
120 {
121 return -ESPIPE;
122 }
123
124 static int bad_pipe_r(struct inode * inode, struct file * filp, char * buf, int count)
125 {
126 return -EBADF;
127 }
128
129 static int bad_pipe_w(struct inode * inode, struct file * filp, const char * buf, int count)
130 {
131 return -EBADF;
132 }
133
134 static int pipe_ioctl(struct inode *pino, struct file * filp,
135 unsigned int cmd, unsigned long arg)
136 {
137 int error;
138
139 switch (cmd) {
140 case FIONREAD:
141 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(int));
142 if (!error)
143 put_user(PIPE_SIZE(*pino),(int *) arg);
144 return error;
145 default:
146 return -EINVAL;
147 }
148 }
149
150 static int pipe_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
151 {
152 switch (sel_type) {
153 case SEL_IN:
154 if (!PIPE_EMPTY(*inode) || !PIPE_WRITERS(*inode))
155 return 1;
156 select_wait(&PIPE_WAIT(*inode), wait);
157 return 0;
158 case SEL_OUT:
159 if (!PIPE_FULL(*inode) || !PIPE_READERS(*inode))
160 return 1;
161 select_wait(&PIPE_WAIT(*inode), wait);
162 return 0;
163 case SEL_EX:
164 if (!PIPE_READERS(*inode) || !PIPE_WRITERS(*inode))
165 return 1;
166 select_wait(&inode->i_wait,wait);
167 return 0;
168 }
169 return 0;
170 }
171
172
173
174
175
176 static int fifo_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
177 {
178 switch (sel_type) {
179 case SEL_IN:
180 if (!PIPE_EMPTY(*inode))
181 return 1;
182 select_wait(&PIPE_WAIT(*inode), wait);
183 return 0;
184 case SEL_OUT:
185 if (!PIPE_FULL(*inode) || !PIPE_READERS(*inode))
186 return 1;
187 select_wait(&PIPE_WAIT(*inode), wait);
188 return 0;
189 case SEL_EX:
190 if (!PIPE_READERS(*inode) || !PIPE_WRITERS(*inode))
191 return 1;
192 select_wait(&inode->i_wait,wait);
193 return 0;
194 }
195 return 0;
196 }
197
198
199
200
201
202
203 static int connect_read(struct inode * inode, struct file * filp, char * buf, int count)
204 {
205 while (!PIPE_SIZE(*inode)) {
206 if (PIPE_WRITERS(*inode))
207 break;
208 if (filp->f_flags & O_NONBLOCK)
209 return -EAGAIN;
210 wake_up_interruptible(& PIPE_WAIT(*inode));
211 if (current->signal & ~current->blocked)
212 return -ERESTARTSYS;
213 interruptible_sleep_on(& PIPE_WAIT(*inode));
214 }
215 filp->f_op = &read_fifo_fops;
216 return pipe_read(inode,filp,buf,count);
217 }
218
219 static int connect_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
220 {
221 switch (sel_type) {
222 case SEL_IN:
223 if (!PIPE_EMPTY(*inode)) {
224 filp->f_op = &read_fifo_fops;
225 return 1;
226 }
227 select_wait(&PIPE_WAIT(*inode), wait);
228 return 0;
229 case SEL_OUT:
230 if (!PIPE_FULL(*inode))
231 return 1;
232 select_wait(&PIPE_WAIT(*inode), wait);
233 return 0;
234 case SEL_EX:
235 if (!PIPE_READERS(*inode) || !PIPE_WRITERS(*inode))
236 return 1;
237 select_wait(&inode->i_wait,wait);
238 return 0;
239 }
240 return 0;
241 }
242
243 static void pipe_read_release(struct inode * inode, struct file * filp)
244 {
245 PIPE_READERS(*inode)--;
246 wake_up_interruptible(&PIPE_WAIT(*inode));
247 }
248
249 static void pipe_write_release(struct inode * inode, struct file * filp)
250 {
251 PIPE_WRITERS(*inode)--;
252 wake_up_interruptible(&PIPE_WAIT(*inode));
253 }
254
255 static void pipe_rdwr_release(struct inode * inode, struct file * filp)
256 {
257 if (filp->f_mode & FMODE_READ)
258 PIPE_READERS(*inode)--;
259 if (filp->f_mode & FMODE_WRITE)
260 PIPE_WRITERS(*inode)--;
261 wake_up_interruptible(&PIPE_WAIT(*inode));
262 }
263
264 static int pipe_read_open(struct inode * inode, struct file * filp)
265 {
266 PIPE_READERS(*inode)++;
267 return 0;
268 }
269
270 static int pipe_write_open(struct inode * inode, struct file * filp)
271 {
272 PIPE_WRITERS(*inode)++;
273 return 0;
274 }
275
276 static int pipe_rdwr_open(struct inode * inode, struct file * filp)
277 {
278 if (filp->f_mode & FMODE_READ)
279 PIPE_READERS(*inode)++;
280 if (filp->f_mode & FMODE_WRITE)
281 PIPE_WRITERS(*inode)++;
282 return 0;
283 }
284
285
286
287
288
289 struct file_operations connecting_fifo_fops = {
290 pipe_lseek,
291 connect_read,
292 bad_pipe_w,
293 NULL,
294 connect_select,
295 pipe_ioctl,
296 NULL,
297 pipe_read_open,
298 pipe_read_release,
299 NULL
300 };
301
302 struct file_operations read_fifo_fops = {
303 pipe_lseek,
304 pipe_read,
305 bad_pipe_w,
306 NULL,
307 fifo_select,
308 pipe_ioctl,
309 NULL,
310 pipe_read_open,
311 pipe_read_release,
312 NULL
313 };
314
315 struct file_operations write_fifo_fops = {
316 pipe_lseek,
317 bad_pipe_r,
318 pipe_write,
319 NULL,
320 fifo_select,
321 pipe_ioctl,
322 NULL,
323 pipe_write_open,
324 pipe_write_release,
325 NULL
326 };
327
328 struct file_operations rdwr_fifo_fops = {
329 pipe_lseek,
330 pipe_read,
331 pipe_write,
332 NULL,
333 fifo_select,
334 pipe_ioctl,
335 NULL,
336 pipe_rdwr_open,
337 pipe_rdwr_release,
338 NULL
339 };
340
341 struct file_operations read_pipe_fops = {
342 pipe_lseek,
343 pipe_read,
344 bad_pipe_w,
345 NULL,
346 pipe_select,
347 pipe_ioctl,
348 NULL,
349 pipe_read_open,
350 pipe_read_release,
351 NULL
352 };
353
354 struct file_operations write_pipe_fops = {
355 pipe_lseek,
356 bad_pipe_r,
357 pipe_write,
358 NULL,
359 pipe_select,
360 pipe_ioctl,
361 NULL,
362 pipe_write_open,
363 pipe_write_release,
364 NULL
365 };
366
367 struct file_operations rdwr_pipe_fops = {
368 pipe_lseek,
369 pipe_read,
370 pipe_write,
371 NULL,
372 pipe_select,
373 pipe_ioctl,
374 NULL,
375 pipe_rdwr_open,
376 pipe_rdwr_release,
377 NULL
378 };
379
380 struct inode_operations pipe_inode_operations = {
381 &rdwr_pipe_fops,
382 NULL,
383 NULL,
384 NULL,
385 NULL,
386 NULL,
387 NULL,
388 NULL,
389 NULL,
390 NULL,
391 NULL,
392 NULL,
393 NULL,
394 NULL,
395 NULL,
396 NULL,
397 NULL
398 };
399
400 int do_pipe(int *fd)
401 {
402 struct inode * inode;
403 struct file *f[2];
404 int i,j;
405
406 inode = get_pipe_inode();
407 if (!inode)
408 return -ENFILE;
409
410 for(j=0 ; j<2 ; j++)
411 if (!(f[j] = get_empty_filp()))
412 break;
413 if (j < 2) {
414 iput(inode);
415 if (j)
416 f[0]->f_count--;
417 return -ENFILE;
418 }
419 j=0;
420 for(i=0;j<2 && i<NR_OPEN && i<current->rlim[RLIMIT_NOFILE].rlim_cur;i++)
421 if (!current->files->fd[i]) {
422 current->files->fd[ fd[j]=i ] = f[j];
423 j++;
424 }
425 if (j<2) {
426 iput(inode);
427 f[0]->f_count--;
428 f[1]->f_count--;
429 if (j)
430 current->files->fd[fd[0]] = NULL;
431 return -EMFILE;
432 }
433 f[0]->f_inode = f[1]->f_inode = inode;
434 f[0]->f_pos = f[1]->f_pos = 0;
435 f[0]->f_flags = O_RDONLY;
436 f[0]->f_op = &read_pipe_fops;
437 f[0]->f_mode = 1;
438 f[1]->f_flags = O_WRONLY;
439 f[1]->f_op = &write_pipe_fops;
440 f[1]->f_mode = 2;
441 return 0;
442 }