vfs_preopen: Fix an uninitialized variable read
[obnox/samba-autobuild/.git] / source3 / modules / vfs_preopen.c
1 /*
2  * Force a readahead of files by opening them and reading the first bytes
3  *
4  * Copyright (C) Volker Lendecke 2008
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "lib/util/sys_rw.h"
25 #include "lib/util/sys_rw_data.h"
26
27 struct preopen_state;
28
29 struct preopen_helper {
30         struct preopen_state *state;
31         struct tevent_fd *fde;
32         pid_t pid;
33         int fd;
34         bool busy;
35 };
36
37 struct preopen_state {
38         int num_helpers;
39         struct preopen_helper *helpers;
40
41         size_t to_read;         /* How many bytes to read in children? */
42         int queue_max;
43
44         char *template_fname;   /* Filename to be sent to children */
45         size_t number_start;    /* start offset into "template_fname" */
46         int num_digits;         /* How many digits is the number long? */
47
48         int fnum_sent;          /* last fname sent to children */
49
50         int fnum_queue_end;     /* last fname to be sent, based on
51                                  * last open call + preopen:queuelen
52                                  */
53
54         name_compare_entry *preopen_names;
55 };
56
57 static void preopen_helper_destroy(struct preopen_helper *c)
58 {
59         int status;
60         close(c->fd);
61         c->fd = -1;
62         kill(c->pid, SIGKILL);
63         waitpid(c->pid, &status, 0);
64         c->busy = true;
65 }
66
67 static void preopen_queue_run(struct preopen_state *state)
68 {
69         char *pdelimiter;
70         char delimiter;
71
72         pdelimiter = state->template_fname + state->number_start
73                 + state->num_digits;
74         delimiter = *pdelimiter;
75
76         while (state->fnum_sent < state->fnum_queue_end) {
77
78                 ssize_t written;
79                 size_t to_write;
80                 int helper;
81
82                 for (helper=0; helper<state->num_helpers; helper++) {
83                         if (state->helpers[helper].busy) {
84                                 continue;
85                         }
86                         break;
87                 }
88                 if (helper == state->num_helpers) {
89                         /* everyone is busy */
90                         return;
91                 }
92
93                 snprintf(state->template_fname + state->number_start,
94                          state->num_digits + 1,
95                          "%.*lu", state->num_digits,
96                          (long unsigned int)(state->fnum_sent + 1));
97                 *pdelimiter = delimiter;
98
99                 to_write = talloc_get_size(state->template_fname);
100                 written = write_data(state->helpers[helper].fd,
101                                      state->template_fname, to_write);
102                 state->helpers[helper].busy = true;
103
104                 if (written != to_write) {
105                         preopen_helper_destroy(&state->helpers[helper]);
106                 }
107                 state->fnum_sent += 1;
108         }
109 }
110
111 static void preopen_helper_readable(struct tevent_context *ev,
112                                     struct tevent_fd *fde, uint16_t flags,
113                                     void *priv)
114 {
115         struct preopen_helper *helper = (struct preopen_helper *)priv;
116         struct preopen_state *state = helper->state;
117         ssize_t nread;
118         char c;
119
120         if ((flags & TEVENT_FD_READ) == 0) {
121                 return;
122         }
123
124         nread = read(helper->fd, &c, 1);
125         if (nread <= 0) {
126                 preopen_helper_destroy(helper);
127                 return;
128         }
129
130         helper->busy = false;
131
132         preopen_queue_run(state);
133 }
134
135 static int preopen_helpers_destructor(struct preopen_state *c)
136 {
137         int i;
138
139         for (i=0; i<c->num_helpers; i++) {
140                 if (c->helpers[i].fd == -1) {
141                         continue;
142                 }
143                 preopen_helper_destroy(&c->helpers[i]);
144         }
145
146         return 0;
147 }
148
149 static bool preopen_helper_open_one(int sock_fd, char **pnamebuf,
150                                     size_t to_read, void *filebuf)
151 {
152         char *namebuf = *pnamebuf;
153         ssize_t nread;
154         char c = 0;
155         int fd;
156
157         nread = 0;
158
159         do {
160                 ssize_t thistime;
161
162                 thistime = read(sock_fd, namebuf + nread,
163                                 talloc_get_size(namebuf) - nread);
164                 if (thistime <= 0) {
165                         return false;
166                 }
167
168                 nread += thistime;
169
170                 if (nread == talloc_get_size(namebuf)) {
171                         namebuf = talloc_realloc(
172                                 NULL, namebuf, char,
173                                 talloc_get_size(namebuf) * 2);
174                         if (namebuf == NULL) {
175                                 return false;
176                         }
177                         *pnamebuf = namebuf;
178                 }
179         } while (namebuf[nread - 1] != '\0');
180
181         fd = open(namebuf, O_RDONLY);
182         if (fd == -1) {
183                 goto done;
184         }
185         nread = read(fd, filebuf, to_read);
186         close(fd);
187
188  done:
189         sys_write_v(sock_fd, &c, 1);
190         return true;
191 }
192
193 static bool preopen_helper(int fd, size_t to_read)
194 {
195         char *namebuf;
196         void *readbuf;
197
198         namebuf = talloc_array(NULL, char, 1024);
199         if (namebuf == NULL) {
200                 return false;
201         }
202
203         readbuf = talloc_size(NULL, to_read);
204         if (readbuf == NULL) {
205                 TALLOC_FREE(namebuf);
206                 return false;
207         }
208
209         while (preopen_helper_open_one(fd, &namebuf, to_read, readbuf)) {
210                 ;
211         }
212
213         TALLOC_FREE(readbuf);
214         TALLOC_FREE(namebuf);
215         return false;
216 }
217
218 static NTSTATUS preopen_init_helper(struct preopen_helper *h)
219 {
220         int fdpair[2];
221         NTSTATUS status;
222
223         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
224                 status = map_nt_error_from_unix(errno);
225                 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
226                 return status;
227         }
228
229         h->pid = fork();
230
231         if (h->pid == -1) {
232                 return map_nt_error_from_unix(errno);
233         }
234
235         if (h->pid == 0) {
236                 close(fdpair[0]);
237                 preopen_helper(fdpair[1], h->state->to_read);
238                 exit(0);
239         }
240         close(fdpair[1]);
241         h->fd = fdpair[0];
242         h->fde = tevent_add_fd(global_event_context(), h->state, h->fd,
243                               TEVENT_FD_READ, preopen_helper_readable, h);
244         if (h->fde == NULL) {
245                 close(h->fd);
246                 h->fd = -1;
247                 return NT_STATUS_NO_MEMORY;
248         }
249         h->busy = false;
250         return NT_STATUS_OK;
251 }
252
253 static NTSTATUS preopen_init_helpers(TALLOC_CTX *mem_ctx, size_t to_read,
254                                      int num_helpers, int queue_max,
255                                      struct preopen_state **presult)
256 {
257         struct preopen_state *result;
258         int i;
259
260         result = talloc(mem_ctx, struct preopen_state);
261         if (result == NULL) {
262                 return NT_STATUS_NO_MEMORY;
263         }
264
265         result->num_helpers = num_helpers;
266         result->helpers = talloc_array(result, struct preopen_helper,
267                                        num_helpers);
268         if (result->helpers == NULL) {
269                 TALLOC_FREE(result);
270                 return NT_STATUS_NO_MEMORY;
271         }
272
273         result->to_read = to_read;
274         result->queue_max = queue_max;
275         result->template_fname = NULL;
276         result->fnum_sent = 0;
277         result->fnum_queue_end = 0;
278
279         for (i=0; i<num_helpers; i++) {
280                 result->helpers[i].state = result;
281                 result->helpers[i].fd = -1;
282         }
283
284         talloc_set_destructor(result, preopen_helpers_destructor);
285
286         for (i=0; i<num_helpers; i++) {
287                 preopen_init_helper(&result->helpers[i]);
288         }
289
290         *presult = result;
291         return NT_STATUS_OK;
292 }
293
294 static void preopen_free_helpers(void **ptr)
295 {
296         TALLOC_FREE(*ptr);
297 }
298
299 static struct preopen_state *preopen_state_get(vfs_handle_struct *handle)
300 {
301         struct preopen_state *state;
302         NTSTATUS status;
303         const char *namelist;
304
305         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
306                 SMB_VFS_HANDLE_GET_DATA(handle, state, struct preopen_state,
307                                         return NULL);
308                 return state;
309         }
310
311         namelist = lp_parm_const_string(SNUM(handle->conn), "preopen", "names",
312                                         NULL);
313
314         if (namelist == NULL) {
315                 return NULL;
316         }
317
318         status = preopen_init_helpers(
319                 NULL,
320                 lp_parm_int(SNUM(handle->conn), "preopen", "num_bytes", 1),
321                 lp_parm_int(SNUM(handle->conn), "preopen", "helpers", 1),
322                 lp_parm_int(SNUM(handle->conn), "preopen", "queuelen", 10),
323                 &state);
324         if (!NT_STATUS_IS_OK(status)) {
325                 return NULL;
326         }
327
328         set_namearray(&state->preopen_names, namelist);
329
330         if (state->preopen_names == NULL) {
331                 TALLOC_FREE(state);
332                 return NULL;
333         }
334
335         if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
336                 SMB_VFS_HANDLE_SET_DATA(handle, state, preopen_free_helpers,
337                                         struct preopen_state, return NULL);
338         }
339
340         return state;
341 }
342
343 static bool preopen_parse_fname(const char *fname, unsigned long *pnum,
344                                 size_t *pstart_idx, int *pnum_digits)
345 {
346         const char *p;
347         char *q = NULL;
348         unsigned long num;
349         int error = 0;
350
351         p = strrchr_m(fname, '/');
352         if (p == NULL) {
353                 p = fname;
354         }
355
356         p += 1;
357         while (p[0] != '\0') {
358                 if (isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2])) {
359                         break;
360                 }
361                 p += 1;
362         }
363         if (*p == '\0') {
364                 /* no digits around */
365                 return false;
366         }
367
368         num = strtoul_err(p, (char **)&q, 10, &error);
369         if (error != 0) {
370                 return false;
371         }
372
373         if (num+1 < num) {
374                 /* overflow */
375                 return false;
376         }
377
378         *pnum = num;
379         *pstart_idx = (p - fname);
380         *pnum_digits = (q - p);
381         return true;
382 }
383
384 static int preopen_open(vfs_handle_struct *handle,
385                         struct smb_filename *smb_fname, files_struct *fsp,
386                         int flags, mode_t mode)
387 {
388         struct preopen_state *state;
389         int res;
390         unsigned long num;
391
392         DEBUG(10, ("preopen_open called on %s\n", smb_fname_str_dbg(smb_fname)));
393
394         state = preopen_state_get(handle);
395         if (state == NULL) {
396                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
397         }
398
399         res = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
400         if (res == -1) {
401                 return -1;
402         }
403
404         if ((flags & O_ACCMODE) != O_RDONLY) {
405                 return res;
406         }
407
408         if (!is_in_path(smb_fname->base_name, state->preopen_names, true)) {
409                 DEBUG(10, ("%s does not match the preopen:names list\n",
410                            smb_fname_str_dbg(smb_fname)));
411                 return res;
412         }
413
414         TALLOC_FREE(state->template_fname);
415         state->template_fname = talloc_asprintf(
416                 state, "%s/%s",
417                 fsp->conn->cwd_fname->base_name, smb_fname->base_name);
418
419         if (state->template_fname == NULL) {
420                 return res;
421         }
422
423         if (!preopen_parse_fname(state->template_fname, &num,
424                                  &state->number_start, &state->num_digits)) {
425                 TALLOC_FREE(state->template_fname);
426                 return res;
427         }
428
429         if (num > state->fnum_sent) {
430                 /*
431                  * Helpers were too slow, there's no point in reading
432                  * files in helpers that we already read in the
433                  * parent.
434                  */
435                 state->fnum_sent = num;
436         }
437
438         if ((state->fnum_queue_end != 0) /* Something was started earlier */
439             && (num < (state->fnum_queue_end - state->queue_max))) {
440                 /*
441                  * "num" is before the queue we announced. This means
442                  * a new run is started.
443                  */
444                 state->fnum_sent = num;
445         }
446
447         state->fnum_queue_end = num + state->queue_max;
448
449         preopen_queue_run(state);
450
451         return res;
452 }
453
454 static struct vfs_fn_pointers vfs_preopen_fns = {
455         .open_fn = preopen_open
456 };
457
458 static_decl_vfs;
459 NTSTATUS vfs_preopen_init(TALLOC_CTX *ctx)
460 {
461         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
462                                 "preopen", &vfs_preopen_fns);
463 }