Annotation of src/usr.bin/aucat/pipe.c, Revision 1.7
1.6 ratchov 1: /*
2: * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
3: *
4: * Permission to use, copy, modify, and distribute this software for any
5: * purpose with or without fee is hereby granted, provided that the above
6: * copyright notice and this permission notice appear in all copies.
7: *
8: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15: */
16:
1.1 ratchov 17: #include <sys/time.h>
18: #include <sys/types.h>
1.6 ratchov 19:
1.1 ratchov 20: #include <err.h>
21: #include <errno.h>
22: #include <fcntl.h>
23: #include <poll.h>
24: #include <stdio.h>
25: #include <stdlib.h>
26: #include <unistd.h>
27:
28: #include "conf.h"
29: #include "pipe.h"
30:
31: struct fileops pipe_ops = {
32: "pipe",
33: sizeof(struct pipe),
34: pipe_close,
35: pipe_read,
36: pipe_write,
37: NULL, /* start */
38: NULL, /* stop */
39: pipe_nfds,
40: pipe_pollfd,
41: pipe_revents
42: };
43:
44: struct pipe *
45: pipe_new(struct fileops *ops, int fd, char *name)
46: {
47: struct pipe *f;
48:
49: f = (struct pipe *)file_new(ops, name, 1);
1.4 ratchov 50: if (f == NULL)
51: return NULL;
1.1 ratchov 52: f->fd = fd;
53: return f;
54: }
55:
56: unsigned
57: pipe_read(struct file *file, unsigned char *data, unsigned count)
58: {
59: struct pipe *f = (struct pipe *)file;
60: int n;
1.7 ! ratchov 61:
1.1 ratchov 62: while ((n = read(f->fd, data, count)) < 0) {
63: f->file.state &= ~FILE_ROK;
64: if (errno == EAGAIN) {
65: } else {
66: warn("%s", f->file.name);
67: file_eof(&f->file);
68: }
69: return 0;
70: }
71: if (n == 0) {
1.7 ! ratchov 72: f->file.state &= ~FILE_ROK; /* XXX: already cleared in file_eof */
1.1 ratchov 73: file_eof(&f->file);
74: return 0;
75: }
76: return n;
77: }
78:
79:
80: unsigned
81: pipe_write(struct file *file, unsigned char *data, unsigned count)
82: {
83: struct pipe *f = (struct pipe *)file;
84: int n;
1.3 ratchov 85:
1.1 ratchov 86: while ((n = write(f->fd, data, count)) < 0) {
87: f->file.state &= ~FILE_WOK;
88: if (errno == EAGAIN) {
89: } else {
90: if (errno != EPIPE)
91: warn("%s", f->file.name);
92: file_hup(&f->file);
93: }
94: return 0;
95: }
96: return n;
97: }
98:
99: int
100: pipe_nfds(struct file *file) {
101: return 1;
102: }
103:
104: int
105: pipe_pollfd(struct file *file, struct pollfd *pfd, int events)
106: {
107: struct pipe *f = (struct pipe *)file;
108:
109: pfd->fd = f->fd;
110: pfd->events = events;
1.2 ratchov 111: return (events != 0) ? 1 : 0;
1.1 ratchov 112: }
113:
114: int
115: pipe_revents(struct file *f, struct pollfd *pfd)
116: {
117: return pfd->revents;
118: }
119:
120: void
121: pipe_close(struct file *file)
122: {
123: struct pipe *f = (struct pipe *)file;
124:
125: close(f->fd);
126: }