preparing for release of 3.0alpha23
[Samba.git] / source / torture / vfstest.c
blob04f31656d3d0a92ad1809b7d8034a08c4ae719a0
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
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 2 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, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "includes.h"
28 #include "vfstest.h"
30 /* List to hold groups of commands */
31 static struct cmd_list {
32 struct cmd_list *prev, *next;
33 struct cmd_set *cmd_set;
34 } *cmd_list;
36 extern pstring user_socket_options;
38 /****************************************************************************
39 handle completion of commands for readline
40 ****************************************************************************/
41 static char **completion_fn(char *text, int start, int end)
43 #define MAX_COMPLETIONS 100
44 char **matches;
45 int i, count=0;
46 struct cmd_list *commands = cmd_list;
48 if (start)
49 return NULL;
51 /* make sure we have a list of valid commands */
52 if (!commands)
53 return NULL;
55 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
56 if (!matches) return NULL;
58 matches[count++] = strdup(text);
59 if (!matches[0]) return NULL;
61 while (commands && count < MAX_COMPLETIONS-1)
63 if (!commands->cmd_set)
64 break;
66 for (i=0; commands->cmd_set[i].name; i++)
68 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
69 commands->cmd_set[i].fn)
71 matches[count] = strdup(commands->cmd_set[i].name);
72 if (!matches[count])
73 return NULL;
74 count++;
78 commands = commands->next;
82 if (count == 2) {
83 SAFE_FREE(matches[0]);
84 matches[0] = strdup(matches[1]);
86 matches[count] = NULL;
87 return matches;
90 static char* next_command(char** cmdstr)
92 static pstring command;
93 char *p;
95 if (!cmdstr || !(*cmdstr))
96 return NULL;
98 p = strchr_m(*cmdstr, ';');
99 if (p)
100 *p = '\0';
101 pstrcpy(command, *cmdstr);
102 *cmdstr = p;
104 return command;
107 /* Load specified configuration file */
108 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
109 int argc, char **argv)
111 if (argc != 2) {
112 printf("Usage: %s <smb.conf>\n", argv[0]);
113 return NT_STATUS_OK;
116 if (!lp_load(argv[1], False, True, False)) {
117 printf("Error loading \"%s\"\n", argv[1]);
118 return NT_STATUS_OK;
121 printf("\"%s\" successfully loaded\n", argv[1]);
122 return NT_STATUS_OK;
125 /* Display help on commands */
126 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
127 int argc, const char **argv)
129 struct cmd_list *tmp;
130 struct cmd_set *tmp_set;
132 /* Usage */
133 if (argc > 2) {
134 printf("Usage: %s [command]\n", argv[0]);
135 return NT_STATUS_OK;
138 /* Help on one command */
140 if (argc == 2) {
141 for (tmp = cmd_list; tmp; tmp = tmp->next) {
143 tmp_set = tmp->cmd_set;
145 while(tmp_set->name) {
146 if (strequal(argv[1], tmp_set->name)) {
147 if (tmp_set->usage &&
148 tmp_set->usage[0])
149 printf("%s\n", tmp_set->usage);
150 else
151 printf("No help for %s\n", tmp_set->name);
153 return NT_STATUS_OK;
156 tmp_set++;
160 printf("No such command: %s\n", argv[1]);
161 return NT_STATUS_OK;
164 /* List all commands */
166 for (tmp = cmd_list; tmp; tmp = tmp->next) {
168 tmp_set = tmp->cmd_set;
170 while(tmp_set->name) {
172 printf("%15s\t\t%s\n", tmp_set->name,
173 tmp_set->description ? tmp_set->description:
174 "");
176 tmp_set++;
180 return NT_STATUS_OK;
183 /* Change the debug level */
184 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
186 if (argc > 2) {
187 printf("Usage: %s [debuglevel]\n", argv[0]);
188 return NT_STATUS_OK;
191 if (argc == 2) {
192 DEBUGLEVEL = atoi(argv[1]);
195 printf("debuglevel is %d\n", DEBUGLEVEL);
197 return NT_STATUS_OK;
200 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
202 /* Cleanup */
203 talloc_destroy(mem_ctx);
204 mem_ctx = NULL;
205 vfs->data = NULL;
206 vfs->data_size = 0;
207 return NT_STATUS_OK;
210 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
212 /* Cleanup */
213 talloc_destroy(mem_ctx);
215 exit(0);
216 return NT_STATUS_OK; /* NOTREACHED */
219 static struct cmd_set vfstest_commands[] = {
221 { "GENERAL OPTIONS" },
223 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
224 { "help", cmd_help, "Get help on commands", "" },
225 { "?", cmd_help, "Get help on commands", "" },
226 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
227 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
228 { "exit", cmd_quit, "Exit program", "" },
229 { "quit", cmd_quit, "Exit program", "" },
231 { NULL }
234 static struct cmd_set separator_command[] = {
235 { "---------------", NULL, "----------------------" },
236 { NULL }
240 extern struct cmd_set vfs_commands[];
241 static struct cmd_set *vfstest_command_list[] = {
242 vfstest_commands,
243 vfs_commands,
244 NULL
247 static void add_command_set(struct cmd_set *cmd_set)
249 struct cmd_list *entry;
251 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
252 DEBUG(0, ("out of memory\n"));
253 return;
256 ZERO_STRUCTP(entry);
258 entry->cmd_set = cmd_set;
259 DLIST_ADD(cmd_list, entry);
262 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
264 char *p = cmd, **argv = NULL;
265 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
266 pstring buf;
267 TALLOC_CTX *mem_ctx = NULL;
268 int argc = 0, i;
270 /* Count number of arguments first time through the loop then
271 allocate memory and strdup them. */
273 again:
274 while(next_token(&p, buf, " ", sizeof(buf))) {
275 if (argv) {
276 argv[argc] = strdup(buf);
279 argc++;
282 if (!argv) {
284 /* Create argument list */
286 argv = (char **)malloc(sizeof(char *) * argc);
287 memset(argv, 0, sizeof(char *) * argc);
289 if (!argv) {
290 fprintf(stderr, "out of memory\n");
291 result = NT_STATUS_NO_MEMORY;
292 goto done;
295 p = cmd;
296 argc = 0;
298 goto again;
301 /* Call the function */
303 if (cmd_entry->fn) {
305 if (mem_ctx == NULL) {
306 /* Create mem_ctx */
307 if (!(mem_ctx = talloc_init("do_cmd"))) {
308 DEBUG(0, ("talloc_init() failed\n"));
309 goto done;
313 /* Run command */
314 result = cmd_entry->fn(vfs, mem_ctx, argc, argv);
316 } else {
317 fprintf (stderr, "Invalid command\n");
318 goto done;
321 done:
323 /* Cleanup */
325 if (argv) {
326 for (i = 0; i < argc; i++)
327 SAFE_FREE(argv[i]);
329 SAFE_FREE(argv);
332 return result;
335 /* Process a command entered at the prompt or as part of -c */
336 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
338 struct cmd_list *temp_list;
339 BOOL found = False;
340 pstring buf;
341 char *p = cmd;
342 NTSTATUS result = NT_STATUS_OK;
343 int len = 0;
345 if (cmd[strlen(cmd) - 1] == '\n')
346 cmd[strlen(cmd) - 1] = '\0';
348 if (!next_token(&p, buf, " ", sizeof(buf))) {
349 return NT_STATUS_OK;
352 /* strip the trainly \n if it exsists */
353 len = strlen(buf);
354 if (buf[len-1] == '\n')
355 buf[len-1] = '\0';
357 /* Search for matching commands */
359 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
360 struct cmd_set *temp_set = temp_list->cmd_set;
362 while(temp_set->name) {
363 if (strequal(buf, temp_set->name)) {
364 found = True;
365 result = do_cmd(vfs, temp_set, cmd);
367 goto done;
369 temp_set++;
373 done:
374 if (!found && buf[0]) {
375 printf("command not found: %s\n", buf);
376 return NT_STATUS_OK;
379 if (!NT_STATUS_IS_OK(result)) {
380 printf("result was %s\n", nt_errstr(result));
383 return result;
386 static void process_file(struct vfs_state *pvfs, char *filename) {
387 FILE *file;
388 char command[3 * PATH_MAX];
390 if (*filename == '-') {
391 file = stdin;
392 } else {
393 file = fopen(filename, "r");
394 if (file == NULL) {
395 printf("vfstest: error reading file (%s)!", filename);
396 printf("errno n.%d: %s", errno, strerror(errno));
397 exit(-1);
401 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
402 process_cmd(pvfs, command);
406 void exit_server(const char *reason)
408 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
409 exit(0);
412 static int server_fd = -1;
413 int last_message = -1;
415 int smbd_server_fd(void)
417 return server_fd;
420 /****************************************************************************
421 Reload the services file.
422 **************************************************************************/
424 BOOL reload_services(BOOL test)
426 BOOL ret;
428 if (lp_loaded()) {
429 pstring fname;
430 pstrcpy(fname,lp_configfile());
431 if (file_exist(fname, NULL) &&
432 !strcsequal(fname, dyn_CONFIGFILE)) {
433 pstrcpy(dyn_CONFIGFILE, fname);
434 test = False;
438 reopen_logs();
440 if (test && !lp_file_list_changed())
441 return(True);
443 lp_killunused(conn_snum_used);
445 ret = lp_load(dyn_CONFIGFILE, False, False, True);
447 load_printers();
449 /* perhaps the config filename is now set */
450 if (!test)
451 reload_services(True);
453 reopen_logs();
455 load_interfaces();
458 if (smbd_server_fd() != -1) {
459 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
460 set_socket_options(smbd_server_fd(), user_socket_options);
464 mangle_reset_cache();
465 reset_stat_cache();
467 /* this forces service parameters to be flushed */
468 set_current_service(NULL,True);
470 return (ret);
473 /* Main function */
475 int main(int argc, char *argv[])
477 BOOL interactive = True;
478 int opt;
479 static char *cmdstr = "";
480 static char *opt_logfile=NULL;
481 static int opt_debuglevel;
482 pstring logfile;
483 struct cmd_set **cmd_set;
484 extern BOOL AllowDebugChange;
485 static struct vfs_state vfs;
486 int i;
487 static const char *filename = "";
489 /* make sure the vars that get altered (4th field) are in
490 a fixed location or certain compilers complain */
491 poptContext pc;
492 struct poptOption long_options[] = {
493 POPT_AUTOHELP
494 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
495 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
496 {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l', "Write output to specified logfile" },
497 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
498 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version},
499 { 0, 0, 0, 0}
503 setlinebuf(stdout);
505 DEBUGLEVEL = 1;
506 AllowDebugChange = False;
508 pc = poptGetContext("vfstest", argc, (const char **) argv,
509 long_options, 0);
511 while((opt = poptGetNextOpt(pc)) != -1) {
512 switch (opt) {
513 case 'l':
514 slprintf(logfile, sizeof(logfile) - 1, "%s.client",
515 opt_logfile);
516 lp_set_logfile(logfile);
517 interactive = False;
518 break;
520 case 'd':
521 DEBUGLEVEL = opt_debuglevel;
522 break;
527 poptFreeContext(pc);
529 /* TODO: check output */
530 reload_services(False);
532 /* the following functions are part of the Samba debugging
533 facilities. See lib/debug.c */
534 setup_logging("vfstest", interactive);
535 if (!interactive)
536 reopen_logs();
538 /* Load command lists */
540 cmd_set = vfstest_command_list;
542 while(*cmd_set) {
543 add_command_set(*cmd_set);
544 add_command_set(separator_command);
545 cmd_set++;
548 /* some basic initialization stuff */
549 conn_init();
550 vfs.conn = conn_new();
551 vfs.conn->user = "vfstest";
552 for (i=0; i < 1024; i++)
553 vfs.files[i] = NULL;
555 /* some advanced initiliazation stuff */
556 smbd_vfs_init(vfs.conn);
558 /* Do we have a file input? */
559 if (filename[0]) {
560 process_file(&vfs, filename);
561 return 0;
564 /* Do anything specified with -c */
565 if (cmdstr[0]) {
566 char *cmd;
567 char *p = cmdstr;
569 while((cmd=next_command(&p)) != NULL) {
570 process_cmd(&vfs, cmd);
573 return 0;
576 /* Loop around accepting commands */
578 while(1) {
579 pstring prompt;
580 char *line;
582 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
584 line = smb_readline(prompt, NULL, completion_fn);
586 if (line == NULL)
587 break;
589 if (line[0] != '\n')
590 process_cmd(&vfs, line);
593 free(vfs.conn);
594 return 0;