1 /* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */
2 
3 #include "lib.h"
4 #include "fdpass.h"
5 #include "istream-file-private.h"
6 #include "istream-unix.h"
7 
8 struct unix_istream {
9 	struct file_istream fstream;
10 	bool next_read_fd;
11 	int read_fd;
12 };
13 
14 static void
i_stream_unix_close(struct iostream_private * stream,bool close_parent)15 i_stream_unix_close(struct iostream_private *stream, bool close_parent)
16 {
17 	struct unix_istream *ustream =
18 		container_of(stream, struct unix_istream,
19 			     fstream.istream.iostream);
20 
21 	i_close_fd(&ustream->read_fd);
22 	i_stream_file_close(stream, close_parent);
23 }
24 
i_stream_unix_read(struct istream_private * stream)25 static ssize_t i_stream_unix_read(struct istream_private *stream)
26 {
27 	struct unix_istream *ustream =
28 		container_of(stream, struct unix_istream, fstream.istream);
29 	size_t size;
30 	ssize_t ret;
31 
32 	if (!ustream->next_read_fd)
33 		return i_stream_file_read(stream);
34 
35 	i_assert(ustream->read_fd == -1);
36 	i_assert(ustream->fstream.skip_left == 0); /* not supported here.. */
37 	if (!i_stream_try_alloc(stream, 1, &size))
38 		return -2;
39 
40 	ret = fd_read(stream->fd, stream->w_buffer + stream->pos, size,
41 		      &ustream->read_fd);
42 	if (ustream->read_fd != -1)
43 		ustream->next_read_fd = FALSE;
44 
45 	if (ret == 0) {
46 		/* EOF */
47 		stream->istream.eof = TRUE;
48 		ustream->fstream.seen_eof = TRUE;
49 		return -1;
50 	}
51 
52 	if (unlikely(ret < 0)) {
53 		if ((errno == EINTR || errno == EAGAIN) &&
54 		    !stream->istream.blocking) {
55 			return 0;
56 		} else {
57 			i_assert(errno != 0);
58 			/* if we get EBADF for a valid fd, it means something's
59 			   really wrong and we'd better just crash. */
60 			i_assert(errno != EBADF);
61 			stream->istream.stream_errno = errno;
62 			return -1;
63 		}
64 	}
65 	stream->pos += ret;
66 	return ret;
67 }
68 
i_stream_create_unix(int fd,size_t max_buffer_size)69 struct istream *i_stream_create_unix(int fd, size_t max_buffer_size)
70 {
71 	struct unix_istream *ustream;
72 	struct istream *input;
73 
74 	i_assert(fd != -1);
75 
76 	ustream = i_new(struct unix_istream, 1);
77 	ustream->read_fd = -1;
78 	input = i_stream_create_file_common(&ustream->fstream, fd, NULL,
79 					    max_buffer_size, FALSE);
80 	input->real_stream->iostream.close = i_stream_unix_close;
81 	input->real_stream->read = i_stream_unix_read;
82 	return input;
83 }
84 
i_stream_unix_set_read_fd(struct istream * input)85 void i_stream_unix_set_read_fd(struct istream *input)
86 {
87 	struct unix_istream *ustream =
88 		container_of(input->real_stream, struct unix_istream,
89 			     fstream.istream);
90 
91 	ustream->next_read_fd = TRUE;
92 }
93 
i_stream_unix_unset_read_fd(struct istream * input)94 void i_stream_unix_unset_read_fd(struct istream *input)
95 {
96 	struct unix_istream *ustream =
97 		container_of(input->real_stream, struct unix_istream,
98 			     fstream.istream);
99 
100 	ustream->next_read_fd = FALSE;
101 }
102 
i_stream_unix_get_read_fd(struct istream * input)103 int i_stream_unix_get_read_fd(struct istream *input)
104 {
105 	struct unix_istream *ustream =
106 		container_of(input->real_stream, struct unix_istream,
107 			     fstream.istream);
108 	int fd;
109 
110 	fd = ustream->read_fd;
111 	ustream->read_fd = -1;
112 	return fd;
113 }
114