gensec: recv_handler can't be NULL at that point.
[Samba.git] / source3 / modules / vfs_preopen.c
blob612b0252e220e5aa097fdf27d6c7c6e0ea751784
1 /*
2 * Force a readahead of files by opening them and reading the first bytes
4 * Copyright (C) Volker Lendecke 2008
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.
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.
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.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
25 struct preopen_state;
27 struct preopen_helper {
28 struct preopen_state *state;
29 struct tevent_fd *fde;
30 pid_t pid;
31 int fd;
32 bool busy;
35 struct preopen_state {
36 int num_helpers;
37 struct preopen_helper *helpers;
39 size_t to_read; /* How many bytes to read in children? */
40 int queue_max;
42 char *template_fname; /* Filename to be sent to children */
43 size_t number_start; /* start offset into "template_fname" */
44 int num_digits; /* How many digits is the number long? */
46 int fnum_sent; /* last fname sent to children */
48 int fnum_queue_end; /* last fname to be sent, based on
49 * last open call + preopen:queuelen
52 name_compare_entry *preopen_names;
55 static void preopen_helper_destroy(struct preopen_helper *c)
57 int status;
58 close(c->fd);
59 c->fd = -1;
60 kill(c->pid, SIGKILL);
61 waitpid(c->pid, &status, 0);
62 c->busy = true;
65 static void preopen_queue_run(struct preopen_state *state)
67 char *pdelimiter;
68 char delimiter;
70 pdelimiter = state->template_fname + state->number_start
71 + state->num_digits;
72 delimiter = *pdelimiter;
74 while (state->fnum_sent < state->fnum_queue_end) {
76 ssize_t written;
77 size_t to_write;
78 int helper;
80 for (helper=0; helper<state->num_helpers; helper++) {
81 if (state->helpers[helper].busy) {
82 continue;
84 break;
86 if (helper == state->num_helpers) {
87 /* everyone is busy */
88 return;
91 snprintf(state->template_fname + state->number_start,
92 state->num_digits + 1,
93 "%.*lu", state->num_digits,
94 (long unsigned int)(state->fnum_sent + 1));
95 *pdelimiter = delimiter;
97 to_write = talloc_get_size(state->template_fname);
98 written = write_data(state->helpers[helper].fd,
99 state->template_fname, to_write);
100 state->helpers[helper].busy = true;
102 if (written != to_write) {
103 preopen_helper_destroy(&state->helpers[helper]);
105 state->fnum_sent += 1;
109 static void preopen_helper_readable(struct tevent_context *ev,
110 struct tevent_fd *fde, uint16_t flags,
111 void *priv)
113 struct preopen_helper *helper = (struct preopen_helper *)priv;
114 struct preopen_state *state = helper->state;
115 ssize_t nread;
116 char c;
118 if ((flags & TEVENT_FD_READ) == 0) {
119 return;
122 nread = read(helper->fd, &c, 1);
123 if (nread <= 0) {
124 preopen_helper_destroy(helper);
125 return;
128 helper->busy = false;
130 preopen_queue_run(state);
133 static int preopen_helpers_destructor(struct preopen_state *c)
135 int i;
137 for (i=0; i<c->num_helpers; i++) {
138 if (c->helpers[i].fd == -1) {
139 continue;
141 preopen_helper_destroy(&c->helpers[i]);
144 return 0;
147 static bool preopen_helper_open_one(int sock_fd, char **pnamebuf,
148 size_t to_read, void *filebuf)
150 char *namebuf = *pnamebuf;
151 ssize_t nwritten, nread;
152 char c = 0;
153 int fd;
155 nread = 0;
157 while ((nread == 0) || (namebuf[nread-1] != '\0')) {
158 ssize_t thistime;
160 thistime = read(sock_fd, namebuf + nread,
161 talloc_get_size(namebuf) - nread);
162 if (thistime <= 0) {
163 return false;
166 nread += thistime;
168 if (nread == talloc_get_size(namebuf)) {
169 namebuf = talloc_realloc(
170 NULL, namebuf, char,
171 talloc_get_size(namebuf) * 2);
172 if (namebuf == NULL) {
173 return false;
175 *pnamebuf = namebuf;
179 fd = open(namebuf, O_RDONLY);
180 if (fd == -1) {
181 goto done;
183 nread = read(fd, filebuf, to_read);
184 close(fd);
186 done:
187 nwritten = write(sock_fd, &c, 1);
188 return true;
191 static bool preopen_helper(int fd, size_t to_read)
193 char *namebuf;
194 void *readbuf;
196 namebuf = talloc_array(NULL, char, 1024);
197 if (namebuf == NULL) {
198 return false;
201 readbuf = talloc_size(NULL, to_read);
202 if (readbuf == NULL) {
203 TALLOC_FREE(namebuf);
204 return false;
207 while (preopen_helper_open_one(fd, &namebuf, to_read, readbuf)) {
211 TALLOC_FREE(readbuf);
212 TALLOC_FREE(namebuf);
213 return false;
216 static NTSTATUS preopen_init_helper(struct preopen_helper *h)
218 int fdpair[2];
219 NTSTATUS status;
221 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
222 status = map_nt_error_from_unix(errno);
223 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
224 return status;
227 h->pid = fork();
229 if (h->pid == -1) {
230 return map_nt_error_from_unix(errno);
233 if (h->pid == 0) {
234 close(fdpair[0]);
235 preopen_helper(fdpair[1], h->state->to_read);
236 exit(0);
238 close(fdpair[1]);
239 h->fd = fdpair[0];
240 h->fde = tevent_add_fd(server_event_context(), h->state, h->fd,
241 TEVENT_FD_READ, preopen_helper_readable, h);
242 if (h->fde == NULL) {
243 close(h->fd);
244 h->fd = -1;
245 return NT_STATUS_NO_MEMORY;
247 h->busy = false;
248 return NT_STATUS_OK;
251 static NTSTATUS preopen_init_helpers(TALLOC_CTX *mem_ctx, size_t to_read,
252 int num_helpers, int queue_max,
253 struct preopen_state **presult)
255 struct preopen_state *result;
256 int i;
258 result = talloc(mem_ctx, struct preopen_state);
259 if (result == NULL) {
260 return NT_STATUS_NO_MEMORY;
263 result->num_helpers = num_helpers;
264 result->helpers = talloc_array(result, struct preopen_helper,
265 num_helpers);
266 if (result->helpers == NULL) {
267 TALLOC_FREE(result);
268 return NT_STATUS_NO_MEMORY;
271 result->to_read = to_read;
272 result->queue_max = queue_max;
273 result->template_fname = NULL;
274 result->fnum_sent = 0;
276 for (i=0; i<num_helpers; i++) {
277 result->helpers[i].state = result;
278 result->helpers[i].fd = -1;
281 talloc_set_destructor(result, preopen_helpers_destructor);
283 for (i=0; i<num_helpers; i++) {
284 preopen_init_helper(&result->helpers[i]);
287 *presult = result;
288 return NT_STATUS_OK;
291 static void preopen_free_helpers(void **ptr)
293 TALLOC_FREE(*ptr);
296 static struct preopen_state *preopen_state_get(vfs_handle_struct *handle)
298 struct preopen_state *state;
299 NTSTATUS status;
300 const char *namelist;
302 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
303 SMB_VFS_HANDLE_GET_DATA(handle, state, struct preopen_state,
304 return NULL);
305 return state;
308 namelist = lp_parm_const_string(SNUM(handle->conn), "preopen", "names",
309 NULL);
311 if (namelist == NULL) {
312 return NULL;
315 status = preopen_init_helpers(
316 NULL,
317 lp_parm_int(SNUM(handle->conn), "preopen", "num_bytes", 1),
318 lp_parm_int(SNUM(handle->conn), "preopen", "helpers", 1),
319 lp_parm_int(SNUM(handle->conn), "preopen", "queuelen", 10),
320 &state);
321 if (!NT_STATUS_IS_OK(status)) {
322 return NULL;
325 set_namearray(&state->preopen_names, namelist);
327 if (state->preopen_names == NULL) {
328 TALLOC_FREE(state);
329 return NULL;
332 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
333 SMB_VFS_HANDLE_SET_DATA(handle, state, preopen_free_helpers,
334 struct preopen_state, return NULL);
337 return state;
340 static bool preopen_parse_fname(const char *fname, unsigned long *pnum,
341 size_t *pstart_idx, int *pnum_digits)
343 const char *p, *q;
344 unsigned long num;
346 p = strrchr_m(fname, '/');
347 if (p == NULL) {
348 p = fname;
351 p += 1;
352 while (p[0] != '\0') {
353 if (isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2])) {
354 break;
356 p += 1;
358 if (*p == '\0') {
359 /* no digits around */
360 return false;
363 num = strtoul(p, (char **)&q, 10);
365 if (num+1 < num) {
366 /* overflow */
367 return false;
370 *pnum = num;
371 *pstart_idx = (p - fname);
372 *pnum_digits = (q - p);
373 return true;
376 static int preopen_open(vfs_handle_struct *handle,
377 struct smb_filename *smb_fname, files_struct *fsp,
378 int flags, mode_t mode)
380 struct preopen_state *state;
381 int res;
382 unsigned long num;
384 DEBUG(10, ("preopen_open called on %s\n", smb_fname_str_dbg(smb_fname)));
386 state = preopen_state_get(handle);
387 if (state == NULL) {
388 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
391 res = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
392 if (res == -1) {
393 return -1;
396 if (flags != O_RDONLY) {
397 return res;
400 if (!is_in_path(smb_fname->base_name, state->preopen_names, true)) {
401 DEBUG(10, ("%s does not match the preopen:names list\n",
402 smb_fname_str_dbg(smb_fname)));
403 return res;
406 TALLOC_FREE(state->template_fname);
407 state->template_fname = talloc_asprintf(
408 state, "%s/%s", fsp->conn->cwd, smb_fname->base_name);
410 if (state->template_fname == NULL) {
411 return res;
414 if (!preopen_parse_fname(state->template_fname, &num,
415 &state->number_start, &state->num_digits)) {
416 TALLOC_FREE(state->template_fname);
417 return res;
420 if (num > state->fnum_sent) {
422 * Helpers were too slow, there's no point in reading
423 * files in helpers that we already read in the
424 * parent.
426 state->fnum_sent = num;
429 if ((state->fnum_queue_end != 0) /* Something was started earlier */
430 && (num < (state->fnum_queue_end - state->queue_max))) {
432 * "num" is before the queue we announced. This means
433 * a new run is started.
435 state->fnum_sent = num;
438 state->fnum_queue_end = num + state->queue_max;
440 preopen_queue_run(state);
442 return res;
445 static struct vfs_fn_pointers vfs_preopen_fns = {
446 .open_fn = preopen_open
449 NTSTATUS vfs_preopen_init(void);
450 NTSTATUS vfs_preopen_init(void)
452 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
453 "preopen", &vfs_preopen_fns);