~ubuntu-branches/ubuntu/natty/lighttpd/natty

« back to all changes in this revision

Viewing changes to src/network_linux_sendfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2006-12-08 14:40:42 UTC
  • mto: (6.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20061208144042-ehr7h8c6xmijqipw
Tags: upstream-1.4.13
ImportĀ upstreamĀ versionĀ 1.4.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd, chunkqueue *cq) {
30
30
        chunk *c;
31
31
        size_t chunks_written = 0;
32
 
        
 
32
 
33
33
        for(c = cq->first; c; c = c->next, chunks_written++) {
34
34
                int chunk_finished = 0;
35
 
                
 
35
 
36
36
                switch(c->type) {
37
37
                case MEM_CHUNK: {
38
38
                        char * offset;
39
39
                        size_t toSend;
40
40
                        ssize_t r;
41
 
                        
 
41
 
42
42
                        size_t num_chunks, i;
43
43
                        struct iovec chunks[UIO_MAXIOV];
44
44
                        chunk *tc;
45
45
                        size_t num_bytes = 0;
46
 
                        
 
46
 
47
47
                        /* we can't send more then SSIZE_MAX bytes in one chunk */
48
 
                        
49
 
                        /* build writev list 
50
 
                         * 
 
48
 
 
49
                        /* build writev list
 
50
                         *
51
51
                         * 1. limit: num_chunks < UIO_MAXIOV
52
52
                         * 2. limit: num_bytes < SSIZE_MAX
53
53
                         */
54
 
                        for (num_chunks = 0, tc = c; 
55
 
                             tc && tc->type == MEM_CHUNK && num_chunks < UIO_MAXIOV; 
 
54
                        for (num_chunks = 0, tc = c;
 
55
                             tc && tc->type == MEM_CHUNK && num_chunks < UIO_MAXIOV;
56
56
                             tc = tc->next, num_chunks++);
57
 
                        
 
57
 
58
58
                        for (tc = c, i = 0; i < num_chunks; tc = tc->next, i++) {
59
59
                                if (tc->mem->used == 0) {
60
60
                                        chunks[i].iov_base = tc->mem->ptr;
62
62
                                } else {
63
63
                                        offset = tc->mem->ptr + tc->offset;
64
64
                                        toSend = tc->mem->used - 1 - tc->offset;
65
 
                                
 
65
 
66
66
                                        chunks[i].iov_base = offset;
67
 
                                        
 
67
 
68
68
                                        /* protect the return value of writev() */
69
69
                                        if (toSend > SSIZE_MAX ||
70
70
                                            num_bytes + toSend > SSIZE_MAX) {
71
71
                                                chunks[i].iov_len = SSIZE_MAX - num_bytes;
72
 
                                                
 
72
 
73
73
                                                num_chunks = i + 1;
74
74
                                                break;
75
75
                                        } else {
76
76
                                                chunks[i].iov_len = toSend;
77
77
                                        }
78
 
                                 
 
78
 
79
79
                                        num_bytes += toSend;
80
80
                                }
81
81
                        }
82
 
                        
 
82
 
83
83
                        if ((r = writev(fd, chunks, num_chunks)) < 0) {
84
84
                                switch (errno) {
85
85
                                case EAGAIN:
90
90
                                case ECONNRESET:
91
91
                                        return -2;
92
92
                                default:
93
 
                                        log_error_write(srv, __FILE__, __LINE__, "ssd", 
 
93
                                        log_error_write(srv, __FILE__, __LINE__, "ssd",
94
94
                                                        "writev failed:", strerror(errno), fd);
95
 
                                
 
95
 
96
96
                                        return -1;
97
97
                                }
98
98
                        }
99
 
                        
 
99
 
100
100
                        /* check which chunks have been written */
101
101
                        cq->bytes_out += r;
102
102
 
105
105
                                        /* written */
106
106
                                        r -= chunks[i].iov_len;
107
107
                                        tc->offset += chunks[i].iov_len;
108
 
                                        
 
108
 
109
109
                                        if (chunk_finished) {
110
110
                                                /* skip the chunks from further touches */
111
111
                                                chunks_written++;
116
116
                                        }
117
117
                                } else {
118
118
                                        /* partially written */
119
 
                                        
 
119
 
120
120
                                        tc->offset += r;
121
121
                                        chunk_finished = 0;
122
 
                                        
 
122
 
123
123
                                        break;
124
124
                                }
125
125
                        }
126
 
                        
 
126
 
127
127
                        break;
128
128
                }
129
129
                case FILE_CHUNK: {
131
131
                        off_t offset;
132
132
                        size_t toSend;
133
133
                        stat_cache_entry *sce = NULL;
134
 
                        
 
134
 
135
135
                        offset = c->file.start + c->offset;
136
136
                        /* limit the toSend to 2^31-1 bytes in a chunk */
137
 
                        toSend = c->file.length - c->offset > ((1 << 30) - 1) ? 
 
137
                        toSend = c->file.length - c->offset > ((1 << 30) - 1) ?
138
138
                                ((1 << 30) - 1) : c->file.length - c->offset;
139
 
                                
140
 
                        /* open file if not already opened */   
 
139
 
 
140
                        /* open file if not already opened */
141
141
                        if (-1 == c->file.fd) {
142
142
                                if (-1 == (c->file.fd = open(c->file.name->ptr, O_RDONLY))) {
143
143
                                        log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
144
 
                                
 
144
 
145
145
                                        return -1;
146
146
                                }
147
147
#ifdef FD_CLOEXEC
151
151
                                /* tell the kernel that we want to stream the file */
152
152
                                if (-1 == posix_fadvise(c->file.fd, 0, 0, POSIX_FADV_SEQUENTIAL)) {
153
153
                                        if (ENOSYS != errno) {
154
 
                                                log_error_write(srv, __FILE__, __LINE__, "ssd", 
 
154
                                                log_error_write(srv, __FILE__, __LINE__, "ssd",
155
155
                                                        "posix_fadvise failed:", strerror(errno), c->file.fd);
156
156
                                        }
157
157
                                }
168
168
                                case ECONNRESET:
169
169
                                        return -2;
170
170
                                default:
171
 
                                        log_error_write(srv, __FILE__, __LINE__, "ssd", 
 
171
                                        log_error_write(srv, __FILE__, __LINE__, "ssd",
172
172
                                                        "sendfile failed:", strerror(errno), fd);
173
173
                                        return -1;
174
174
                                }
175
175
                        }
176
176
 
177
177
                        if (r == 0) {
 
178
                                int oerrno = errno;
178
179
                                /* We got an event to write but we wrote nothing
179
180
                                 *
180
181
                                 * - the file shrinked -> error
181
182
                                 * - the remote side closed inbetween -> remote-close */
182
 
        
 
183
 
183
184
                                if (HANDLER_ERROR == stat_cache_get_entry(srv, con, c->file.name, &sce)) {
184
185
                                        /* file is gone ? */
185
186
                                        return -1;
187
188
 
188
189
                                if (offset > sce->st.st_size) {
189
190
                                        /* file shrinked, close the connection */
 
191
                                        errno = oerrno;
 
192
 
190
193
                                        return -1;
191
194
                                }
192
195
 
 
196
                                errno = oerrno;
193
197
                                return -2;
194
198
                        }
195
199
 
196
200
#ifdef HAVE_POSIX_FADVISE
197
201
#if 0
198
202
#define K * 1024
199
 
#define M * 1024 K      
 
203
#define M * 1024 K
200
204
#define READ_AHEAD 4 M
201
205
                        /* check if we need a new chunk */
202
206
                        if ((c->offset & ~(READ_AHEAD - 1)) != ((c->offset + r) & ~(READ_AHEAD - 1))) {
203
207
                                /* tell the kernel that we want to stream the file */
204
208
                                if (-1 == posix_fadvise(c->file.fd, (c->offset + r) & ~(READ_AHEAD - 1), READ_AHEAD, POSIX_FADV_NOREUSE)) {
205
 
                                        log_error_write(srv, __FILE__, __LINE__, "ssd", 
 
209
                                        log_error_write(srv, __FILE__, __LINE__, "ssd",
206
210
                                                "posix_fadvise failed:", strerror(errno), c->file.fd);
207
211
                                }
208
212
                        }
209
213
#endif
210
214
#endif
211
 
                        
 
215
 
212
216
                        c->offset += r;
213
217
                        cq->bytes_out += r;
214
 
                        
 
218
 
215
219
                        if (c->offset == c->file.length) {
216
220
                                chunk_finished = 1;
217
221
 
222
226
                                        c->file.fd = -1;
223
227
                                }
224
228
                        }
225
 
                        
 
229
 
226
230
                        break;
227
231
                }
228
232
                default:
229
 
                        
 
233
 
230
234
                        log_error_write(srv, __FILE__, __LINE__, "ds", c, "type not known");
231
 
                        
 
235
 
232
236
                        return -1;
233
237
                }
234
 
                
 
238
 
235
239
                if (!chunk_finished) {
236
240
                        /* not finished yet */
237
 
                        
 
241
 
238
242
                        break;
239
243
                }
240
244
        }