2 Unix SMB/CIFS implementation.
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.
30 /* List to hold groups of commands */
31 static struct cmd_list {
32 struct cmd_list *prev, *next;
33 struct cmd_set *cmd_set;
36 TALLOC_CTX *global_ctx;
38 extern pstring user_socket_options;
40 /****************************************************************************
41 handle completion of commands for readline
42 ****************************************************************************/
43 static char **completion_fn(char *text, int start, int end)
45 #define MAX_COMPLETIONS 100
48 struct cmd_list *commands = cmd_list;
53 /* make sure we have a list of valid commands */
57 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
58 if (!matches) return NULL;
60 matches[count++] = strdup(text);
61 if (!matches[0]) return NULL;
63 while (commands && count < MAX_COMPLETIONS-1)
65 if (!commands->cmd_set)
68 for (i=0; commands->cmd_set[i].name; i++)
70 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
71 commands->cmd_set[i].fn)
73 matches[count] = strdup(commands->cmd_set[i].name);
80 commands = commands->next;
85 SAFE_FREE(matches[0]);
86 matches[0] = strdup(matches[1]);
88 matches[count] = NULL;
92 static char* next_command(char** cmdstr)
94 static pstring command;
97 if (!cmdstr || !(*cmdstr))
100 p = strchr_m(*cmdstr, ';');
103 pstrcpy(command, *cmdstr);
109 /* Load specified configuration file */
110 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
111 int argc, char **argv)
114 printf("Usage: %s <smb.conf>\n", argv[0]);
118 if (!lp_load(argv[1], False, True, False)) {
119 printf("Error loading \"%s\"\n", argv[1]);
123 printf("\"%s\" successfully loaded\n", argv[1]);
127 /* Display help on commands */
128 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
129 int argc, char **argv)
131 struct cmd_list *tmp;
132 struct cmd_set *tmp_set;
136 printf("Usage: %s [command]\n", argv[0]);
140 /* Help on one command */
143 for (tmp = cmd_list; tmp; tmp = tmp->next) {
145 tmp_set = tmp->cmd_set;
147 while(tmp_set->name) {
148 if (strequal(argv[1], tmp_set->name)) {
149 if (tmp_set->usage &&
151 printf("%s\n", tmp_set->usage);
153 printf("No help for %s\n", tmp_set->name);
162 printf("No such command: %s\n", argv[1]);
166 /* List all commands */
168 for (tmp = cmd_list; tmp; tmp = tmp->next) {
170 tmp_set = tmp->cmd_set;
172 while(tmp_set->name) {
174 printf("%15s\t\t%s\n", tmp_set->name,
175 tmp_set->description ? tmp_set->description:
185 /* Change the debug level */
186 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
189 printf("Usage: %s [debuglevel]\n", argv[0]);
194 DEBUGLEVEL = atoi(argv[1]);
197 printf("debuglevel is %d\n", DEBUGLEVEL);
202 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
205 talloc_destroy(global_ctx);
212 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
215 talloc_destroy(global_ctx);
218 return NT_STATUS_OK; /* NOTREACHED */
221 static struct cmd_set vfstest_commands[] = {
223 { "GENERAL OPTIONS" },
225 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
226 { "help", cmd_help, "Get help on commands", "" },
227 { "?", cmd_help, "Get help on commands", "" },
228 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
229 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
230 { "exit", cmd_quit, "Exit program", "" },
231 { "quit", cmd_quit, "Exit program", "" },
236 static struct cmd_set separator_command[] = {
237 { "---------------", NULL, "----------------------" },
242 extern struct cmd_set vfs_commands[];
243 static struct cmd_set *vfstest_command_list[] = {
249 static void add_command_set(struct cmd_set *cmd_set)
251 struct cmd_list *entry;
253 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
254 DEBUG(0, ("out of memory\n"));
260 entry->cmd_set = cmd_set;
261 DLIST_ADD(cmd_list, entry);
264 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
266 char *p = cmd, **argv = NULL;
267 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
271 /* Count number of arguments first time through the loop then
272 allocate memory and strdup them. */
275 while(next_token(&p, buf, " ", sizeof(buf))) {
277 argv[argc] = strdup(buf);
285 /* Create argument list */
287 argv = (char **)malloc(sizeof(char *) * argc);
288 memset(argv, 0, sizeof(char *) * argc);
291 fprintf(stderr, "out of memory\n");
292 result = NT_STATUS_NO_MEMORY;
302 /* Call the function */
306 if (global_ctx == NULL) {
308 if (!(global_ctx = talloc_init())) {
309 DEBUG(0, ("talloc_init() failed\n"));
315 result = cmd_entry->fn(vfs, global_ctx, argc, argv);
318 fprintf (stderr, "Invalid command\n");
327 for (i = 0; i < argc; i++)
336 /* Process a command entered at the prompt or as part of -c */
337 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
339 struct cmd_list *temp_list;
343 NTSTATUS result = NT_STATUS_OK;
346 if (cmd[strlen(cmd) - 1] == '\n')
347 cmd[strlen(cmd) - 1] = '\0';
349 if (!next_token(&p, buf, " ", sizeof(buf))) {
353 /* strip the trainly \n if it exsists */
355 if (buf[len-1] == '\n')
358 /* Search for matching commands */
360 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
361 struct cmd_set *temp_set = temp_list->cmd_set;
363 while(temp_set->name) {
364 if (strequal(buf, temp_set->name)) {
366 result = do_cmd(vfs, temp_set, cmd);
375 if (!found && buf[0]) {
376 printf("command not found: %s\n", buf);
380 if (!NT_STATUS_IS_OK(result)) {
381 printf("result was %s\n", nt_errstr(result));
387 static void process_file(struct vfs_state *pvfs, char *filename) {
389 char command[3 * PATH_MAX];
391 if (*filename == '-') {
394 file = fopen(filename, "r");
396 printf("vfstest: error reading file (%s)!", filename);
397 printf("errno n.%d: %s", errno, strerror(errno));
402 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
403 process_cmd(pvfs, command);
407 void exit_server(char *reason)
409 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
413 static int server_fd = -1;
414 int last_message = -1;
416 int smbd_server_fd(void)
421 /****************************************************************************
422 Reload the services file.
423 **************************************************************************/
425 BOOL reload_services(BOOL test)
431 pstrcpy(fname,lp_configfile());
432 if (file_exist(fname, NULL) &&
433 !strcsequal(fname, dyn_CONFIGFILE)) {
434 pstrcpy(dyn_CONFIGFILE, fname);
441 if (test && !lp_file_list_changed())
444 lp_killunused(conn_snum_used);
446 ret = lp_load(dyn_CONFIGFILE, False, False, True);
450 /* perhaps the config filename is now set */
452 reload_services(True);
459 if (smbd_server_fd() != -1) {
460 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
461 set_socket_options(smbd_server_fd(), user_socket_options);
465 mangle_reset_cache();
468 /* this forces service parameters to be flushed */
469 set_current_service(NULL,True);
474 /* Print usage information */
475 static void usage(void)
477 printf("Usage: vfstest [options]\n");
479 printf("\t-c or --command \"command string\" execute semicolon separated cmds\n");
480 printf("\t-f or --file filename execute a set of operations as described in the file\n");
481 printf("\t-d or --debug debuglevel set the debuglevel\n");
482 printf("\t-l or --logfile logfile logfile to use instead of stdout\n");
483 printf("\t-h or --help Print this help message.\n");
489 int main(int argc, char *argv[])
491 BOOL interactive = True;
493 static char *cmdstr = "";
494 static char *opt_logfile=NULL;
495 static int opt_debuglevel;
497 struct cmd_set **cmd_set;
498 extern BOOL AllowDebugChange;
499 static struct vfs_state vfs;
501 static char *filename = "";
503 /* make sure the vars that get altered (4th field) are in
504 a fixed location or certain compilers complain */
506 struct poptOption long_options[] = {
507 {"file", 'f', POPT_ARG_STRING, &filename},
508 {"debug", 'd', POPT_ARG_INT, &opt_debuglevel, 'd'},
509 {"debuglevel", 'd', POPT_ARG_INT, &opt_debuglevel, 'd'},
510 {"command", 'c', POPT_ARG_STRING, &cmdstr},
511 {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l'},
512 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
520 AllowDebugChange = False;
522 pc = poptGetContext("vfstest", argc, (const char **) argv,
525 while((opt = poptGetNextOpt(pc)) != -1) {
528 slprintf(logfile, sizeof(logfile) - 1, "%s.client",
530 lp_set_logfile(logfile);
535 DEBUGLEVEL = opt_debuglevel;
548 /* TODO: check output */
549 reload_services(False);
551 /* the following functions are part of the Samba debugging
552 facilities. See lib/debug.c */
553 setup_logging("vfstest", interactive);
557 /* Load command lists */
559 cmd_set = vfstest_command_list;
562 add_command_set(*cmd_set);
563 add_command_set(separator_command);
567 /* some basic initialization stuff */
568 vfs.conn = (struct connection_struct *)malloc(sizeof(struct connection_struct));
569 vfs.conn->user = "vfstest";
570 for (i=0; i < 1024; i++)
573 /* some advanced initiliazation stuff */
574 smbd_vfs_init(vfs.conn);
576 /* Do we have a file input? */
578 process_file(&vfs, filename);
582 /* Do anything specified with -c */
587 while((cmd=next_command(&p)) != NULL) {
588 process_cmd(&vfs, cmd);
594 /* Loop around accepting commands */
600 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
602 line = smb_readline(prompt, NULL, completion_fn);
608 process_cmd(&vfs, line);