s3-vfstest: Initialize some more
[Samba/gebeck_regimport.git] / source3 / torture / vfstest.c
blobb0d55f5af546d7a0d132ed495ec1bc1f7018450e
1 /*
2 Unix SMB/CIFS implementation.
3 VFS module tester
5 Copyright (C) Simo Sorce 2002
6 Copyright (C) Eric Lorimer 2002
7 Copyright (C) Jelmer Vernooij 2002,2003
9 Most of this code was ripped off of rpcclient.
10 Copyright (C) Tim Potter 2000-2001
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "includes.h"
27 #include "smbd/smbd.h"
28 #include "smbd/globals.h"
29 #include "popt_common.h"
30 #include "vfstest.h"
31 #include "../libcli/smbreadline/smbreadline.h"
32 #include "auth.h"
33 #include "serverid.h"
34 #include "messages.h"
36 /* List to hold groups of commands */
37 static struct cmd_list {
38 struct cmd_list *prev, *next;
39 struct cmd_set *cmd_set;
40 } *cmd_list;
42 /****************************************************************************
43 handle completion of commands for readline
44 ****************************************************************************/
45 static char **completion_fn(const char *text, int start, int end)
47 #define MAX_COMPLETIONS 100
48 char **matches;
49 int i, count=0;
50 struct cmd_list *commands = cmd_list;
52 if (start)
53 return NULL;
55 /* make sure we have a list of valid commands */
56 if (!commands)
57 return NULL;
59 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
60 if (!matches) return NULL;
62 matches[count++] = SMB_STRDUP(text);
63 if (!matches[0]) return NULL;
65 while (commands && count < MAX_COMPLETIONS-1)
67 if (!commands->cmd_set)
68 break;
70 for (i=0; commands->cmd_set[i].name; i++)
72 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
73 commands->cmd_set[i].fn)
75 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
76 if (!matches[count])
77 return NULL;
78 count++;
82 commands = commands->next;
85 if (count == 2) {
86 SAFE_FREE(matches[0]);
87 matches[0] = SMB_STRDUP(matches[1]);
89 matches[count] = NULL;
90 return matches;
93 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
95 char *command;
96 char *p;
98 if (!cmdstr || !(*cmdstr))
99 return NULL;
101 p = strchr_m(*cmdstr, ';');
102 if (p)
103 *p = '\0';
104 command = talloc_strdup(ctx, *cmdstr);
105 *cmdstr = p;
107 return command;
110 /* Load specified configuration file */
111 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
112 int argc, const char **argv)
114 if (argc != 2) {
115 printf("Usage: %s <smb.conf>\n", argv[0]);
116 return NT_STATUS_OK;
119 if (!lp_load(argv[1], False, True, False, True)) {
120 printf("Error loading \"%s\"\n", argv[1]);
121 return NT_STATUS_OK;
124 printf("\"%s\" successfully loaded\n", argv[1]);
125 return NT_STATUS_OK;
128 /* Display help on commands */
129 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
130 int argc, const char **argv)
132 struct cmd_list *tmp;
133 struct cmd_set *tmp_set;
135 /* Usage */
136 if (argc > 2) {
137 printf("Usage: %s [command]\n", argv[0]);
138 return NT_STATUS_OK;
141 /* Help on one command */
143 if (argc == 2) {
144 for (tmp = cmd_list; tmp; tmp = tmp->next) {
146 tmp_set = tmp->cmd_set;
148 while(tmp_set->name) {
149 if (strequal(argv[1], tmp_set->name)) {
150 if (tmp_set->usage &&
151 tmp_set->usage[0])
152 printf("%s\n", tmp_set->usage);
153 else
154 printf("No help for %s\n", tmp_set->name);
156 return NT_STATUS_OK;
159 tmp_set++;
163 printf("No such command: %s\n", argv[1]);
164 return NT_STATUS_OK;
167 /* List all commands */
169 for (tmp = cmd_list; tmp; tmp = tmp->next) {
171 tmp_set = tmp->cmd_set;
173 while(tmp_set->name) {
175 printf("%15s\t\t%s\n", tmp_set->name,
176 tmp_set->description ? tmp_set->description:
177 "");
179 tmp_set++;
183 return NT_STATUS_OK;
186 /* Change the debug level */
187 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
189 if (argc > 2) {
190 printf("Usage: %s [debuglevel]\n", argv[0]);
191 return NT_STATUS_OK;
194 if (argc == 2) {
195 lp_set_cmdline("log level", argv[1]);
198 printf("debuglevel is %d\n", DEBUGLEVEL);
200 return NT_STATUS_OK;
203 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
205 /* Cleanup */
206 talloc_destroy(mem_ctx);
207 mem_ctx = NULL;
208 vfs->data = NULL;
209 vfs->data_size = 0;
210 return NT_STATUS_OK;
213 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
215 /* Cleanup */
216 talloc_destroy(mem_ctx);
218 exit(0);
219 return NT_STATUS_OK; /* NOTREACHED */
222 static struct cmd_set vfstest_commands[] = {
224 { "GENERAL OPTIONS" },
226 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
227 { "help", cmd_help, "Get help on commands", "" },
228 { "?", cmd_help, "Get help on commands", "" },
229 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
230 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
231 { "exit", cmd_quit, "Exit program", "" },
232 { "quit", cmd_quit, "Exit program", "" },
234 { NULL }
237 static struct cmd_set separator_command[] = {
238 { "---------------", NULL, "----------------------" },
239 { NULL }
243 extern struct cmd_set vfs_commands[];
244 static struct cmd_set *vfstest_command_list[] = {
245 vfstest_commands,
246 vfs_commands,
247 NULL
250 static void add_command_set(struct cmd_set *cmd_set)
252 struct cmd_list *entry;
254 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
255 DEBUG(0, ("out of memory\n"));
256 return;
259 ZERO_STRUCTP(entry);
261 entry->cmd_set = cmd_set;
262 DLIST_ADD(cmd_list, entry);
265 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
267 const char *p = cmd;
268 char **argv = NULL;
269 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
270 char *buf;
271 TALLOC_CTX *mem_ctx = talloc_stackframe();
272 int argc = 0, i;
274 /* Count number of arguments first time through the loop then
275 allocate memory and strdup them. */
277 again:
278 while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
279 if (argv) {
280 argv[argc] = SMB_STRDUP(buf);
282 argc++;
285 if (!argv) {
286 /* Create argument list */
288 argv = SMB_MALLOC_ARRAY(char *, argc);
289 memset(argv, 0, sizeof(char *) * argc);
291 if (!argv) {
292 fprintf(stderr, "out of memory\n");
293 result = NT_STATUS_NO_MEMORY;
294 goto done;
297 p = cmd;
298 argc = 0;
300 goto again;
303 /* Call the function */
305 if (cmd_entry->fn) {
306 /* Run command */
307 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
308 } else {
309 fprintf (stderr, "Invalid command\n");
310 goto done;
313 done:
315 /* Cleanup */
317 if (argv) {
318 for (i = 0; i < argc; i++)
319 SAFE_FREE(argv[i]);
321 SAFE_FREE(argv);
324 TALLOC_FREE(mem_ctx);
325 return result;
328 /* Process a command entered at the prompt or as part of -c */
329 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
331 struct cmd_list *temp_list;
332 bool found = False;
333 char *buf;
334 const char *p = cmd;
335 NTSTATUS result = NT_STATUS_OK;
336 TALLOC_CTX *mem_ctx = talloc_stackframe();
337 int len = 0;
339 if (cmd[strlen(cmd) - 1] == '\n')
340 cmd[strlen(cmd) - 1] = '\0';
342 if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
343 TALLOC_FREE(mem_ctx);
344 return NT_STATUS_OK;
347 /* Strip the trailing \n if it exists */
348 len = strlen(buf);
349 if (buf[len-1] == '\n')
350 buf[len-1] = '\0';
352 /* Search for matching commands */
354 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
355 struct cmd_set *temp_set = temp_list->cmd_set;
357 while(temp_set->name) {
358 if (strequal(buf, temp_set->name)) {
359 found = True;
360 result = do_cmd(vfs, temp_set, cmd);
362 goto done;
364 temp_set++;
368 done:
369 if (!found && buf[0]) {
370 printf("command not found: %s\n", buf);
371 TALLOC_FREE(mem_ctx);
372 return NT_STATUS_OK;
375 if (!NT_STATUS_IS_OK(result)) {
376 printf("result was %s\n", nt_errstr(result));
379 TALLOC_FREE(mem_ctx);
380 return result;
383 static void process_file(struct vfs_state *pvfs, char *filename) {
384 FILE *file;
385 char command[3 * PATH_MAX];
387 if (*filename == '-') {
388 file = stdin;
389 } else {
390 file = fopen(filename, "r");
391 if (file == NULL) {
392 printf("vfstest: error reading file (%s)!", filename);
393 printf("errno n.%d: %s", errno, strerror(errno));
394 exit(-1);
398 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
399 process_cmd(pvfs, command);
402 if (file != stdin) {
403 fclose(file);
407 void exit_server(const char *reason)
409 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
410 exit(0);
413 void exit_server_cleanly(const char *const reason)
415 exit_server("normal exit");
418 int last_message = -1;
420 /* Main function */
422 int main(int argc, char *argv[])
424 char *cmdstr = NULL;
425 struct cmd_set **cmd_set;
426 struct vfs_state vfs = { 0, };
427 int i;
428 char *filename = NULL;
429 char cwd[MAXPATHLEN];
430 TALLOC_CTX *frame = talloc_stackframe();
431 struct tevent_context *ev = tevent_context_init(NULL);
433 /* make sure the vars that get altered (4th field) are in
434 a fixed location or certain compilers complain */
435 poptContext pc;
436 struct poptOption long_options[] = {
437 POPT_AUTOHELP
438 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
439 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
440 POPT_COMMON_SAMBA
441 POPT_TABLEEND
444 load_case_tables();
446 setlinebuf(stdout);
448 pc = poptGetContext("vfstest", argc, (const char **) argv,
449 long_options, 0);
451 while(poptGetNextOpt(pc) != -1);
454 poptFreeContext(pc);
456 lp_load_initial_only(get_dyn_CONFIGFILE());
458 /* TODO: check output */
459 reload_services(NULL, NULL, false);
461 /* the following functions are part of the Samba debugging
462 facilities. See lib/debug.c */
463 setup_logging("vfstest", DEBUG_STDOUT);
465 /* Load command lists */
467 cmd_set = vfstest_command_list;
469 while(*cmd_set) {
470 add_command_set(*cmd_set);
471 add_command_set(separator_command);
472 cmd_set++;
475 /* some basic initialization stuff */
476 sec_init();
477 init_guest_info();
478 locking_init();
479 serverid_parent_init(NULL);
480 vfs.conn = talloc_zero(NULL, connection_struct);
481 vfs.conn->params = talloc_zero(vfs.conn, struct share_params);
482 vfs.conn->sconn = talloc_zero(NULL, struct smbd_server_connection);
483 vfs.conn->sconn->msg_ctx = messaging_init(vfs.conn->sconn, ev);
484 make_session_info_guest(NULL, &vfs.conn->session_info);
485 file_init(vfs.conn->sconn);
486 set_conn_connectpath(vfs.conn, getcwd(cwd, sizeof(cwd)));
487 for (i=0; i < 1024; i++)
488 vfs.files[i] = NULL;
490 /* some advanced initialization stuff */
491 smbd_vfs_init(vfs.conn);
493 if (!posix_locking_init(false)) {
494 return 1;
497 /* Do we have a file input? */
498 if (filename && filename[0]) {
499 process_file(&vfs, filename);
500 return 0;
503 /* Do anything specified with -c */
504 if (cmdstr && cmdstr[0]) {
505 char *cmd;
506 char *p = cmdstr;
508 while((cmd=next_command(frame, &p)) != NULL) {
509 process_cmd(&vfs, cmd);
512 TALLOC_FREE(cmd);
513 return 0;
516 /* Loop around accepting commands */
518 while(1) {
519 char *line = NULL;
521 line = smb_readline("vfstest $> ", NULL, completion_fn);
523 if (line == NULL) {
524 break;
527 if (line[0] != '\n') {
528 process_cmd(&vfs, line);
530 SAFE_FREE(line);
533 TALLOC_FREE(vfs.conn);
534 TALLOC_FREE(frame);
535 return 0;