Annotation of src/usr.bin/aucat/pipe.c, Revision 1.2
1.1 ratchov 1: #include <sys/time.h>
2: #include <sys/types.h>
3: #include <err.h>
4: #include <errno.h>
5: #include <fcntl.h>
6: #include <poll.h>
7: #include <stdio.h>
8: #include <stdlib.h>
9: #include <unistd.h>
10:
11: #include "conf.h"
12: #include "pipe.h"
13:
14: struct fileops pipe_ops = {
15: "pipe",
16: sizeof(struct pipe),
17: pipe_close,
18: pipe_read,
19: pipe_write,
20: NULL, /* start */
21: NULL, /* stop */
22: pipe_nfds,
23: pipe_pollfd,
24: pipe_revents
25: };
26:
27: struct pipe *
28: pipe_new(struct fileops *ops, int fd, char *name)
29: {
30: struct pipe *f;
31:
32: f = (struct pipe *)file_new(ops, name, 1);
33: f->fd = fd;
34: return f;
35: }
36:
37: unsigned
38: pipe_read(struct file *file, unsigned char *data, unsigned count)
39: {
40: struct pipe *f = (struct pipe *)file;
41: int n;
42: #ifdef DEBUG
43: struct timeval tv0, tv1, dtv;
44: unsigned us;
45:
46: if (!(f->file.state & FILE_ROK)) {
47: DPRINTF("pipe_read: %s: bad state\n", f->file.name);
48: abort();
49: }
50: gettimeofday(&tv0, NULL);
51: #endif
52: while ((n = read(f->fd, data, count)) < 0) {
53: f->file.state &= ~FILE_ROK;
54: if (errno == EAGAIN) {
55: DPRINTFN(3, "pipe_read: %s: blocking...\n",
56: f->file.name);
57: } else {
58: warn("%s", f->file.name);
59: file_eof(&f->file);
60: }
61: return 0;
62: }
63: if (n == 0) {
64: DPRINTFN(2, "pipe_read: %s: eof\n", f->file.name);
65: f->file.state &= ~FILE_ROK;
66: file_eof(&f->file);
67: return 0;
68: }
69: #ifdef DEBUG
70: gettimeofday(&tv1, NULL);
71: timersub(&tv1, &tv0, &dtv);
72: us = dtv.tv_sec * 1000000 + dtv.tv_usec;
73: DPRINTFN(us < 5000 ? 4 : 1,
74: "pipe_read: %s: got %d bytes in %uus\n",
75: f->file.name, n, us);
76: #endif
77: return n;
78: }
79:
80:
81: unsigned
82: pipe_write(struct file *file, unsigned char *data, unsigned count)
83: {
84: struct pipe *f = (struct pipe *)file;
85: int n;
86: #ifdef DEBUG
87: struct timeval tv0, tv1, dtv;
88: unsigned us;
89:
90: if (!(f->file.state & FILE_WOK)) {
91: DPRINTF("pipe_write: %s: bad state\n", f->file.name);
92: abort();
93: }
94: gettimeofday(&tv0, NULL);
95: #endif
96: while ((n = write(f->fd, data, count)) < 0) {
97: f->file.state &= ~FILE_WOK;
98: if (errno == EAGAIN) {
99: DPRINTFN(3, "pipe_write: %s: blocking...\n",
100: f->file.name);
101: } else {
102: if (errno != EPIPE)
103: warn("%s", f->file.name);
104: file_hup(&f->file);
105: }
106: return 0;
107: }
108: #ifdef DEBUG
109: gettimeofday(&tv1, NULL);
110: timersub(&tv1, &tv0, &dtv);
111: us = dtv.tv_sec * 1000000 + dtv.tv_usec;
112: DPRINTFN(us < 5000 ? 4 : 1,
113: "pipe_write: %s: wrote %d bytes in %uus\n",
114: f->file.name, n, us);
115: #endif
116: return n;
117: }
118:
119: int
120: pipe_nfds(struct file *file) {
121: return 1;
122: }
123:
124: int
125: pipe_pollfd(struct file *file, struct pollfd *pfd, int events)
126: {
127: struct pipe *f = (struct pipe *)file;
128:
129: pfd->fd = f->fd;
130: pfd->events = events;
1.2 ! ratchov 131: return (events != 0) ? 1 : 0;
1.1 ratchov 132: }
133:
134: int
135: pipe_revents(struct file *f, struct pollfd *pfd)
136: {
137: return pfd->revents;
138: }
139:
140: void
141: pipe_close(struct file *file)
142: {
143: struct pipe *f = (struct pipe *)file;
144:
145: close(f->fd);
146: }