Remove the silly "user_socket_options" global variable
[Samba.git] / source3 / torture / vfstest.c
blobdaecf3c2d1dffa5ff75a58eae68d0e4ca1fd0762
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 "vfstest.h"
29 /* List to hold groups of commands */
30 static struct cmd_list {
31 struct cmd_list *prev, *next;
32 struct cmd_set *cmd_set;
33 } *cmd_list;
35 int get_client_fd(void)
37 return -1;
40 /****************************************************************************
41 handle completion of commands for readline
42 ****************************************************************************/
43 static char **completion_fn(const char *text, int start, int end)
45 #define MAX_COMPLETIONS 100
46 char **matches;
47 int i, count=0;
48 struct cmd_list *commands = cmd_list;
50 if (start)
51 return NULL;
53 /* make sure we have a list of valid commands */
54 if (!commands)
55 return NULL;
57 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
58 if (!matches) return NULL;
60 matches[count++] = SMB_STRDUP(text);
61 if (!matches[0]) return NULL;
63 while (commands && count < MAX_COMPLETIONS-1)
65 if (!commands->cmd_set)
66 break;
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] = SMB_STRDUP(commands->cmd_set[i].name);
74 if (!matches[count])
75 return NULL;
76 count++;
80 commands = commands->next;
84 if (count == 2) {
85 SAFE_FREE(matches[0]);
86 matches[0] = SMB_STRDUP(matches[1]);
88 matches[count] = NULL;
89 return matches;
92 static char* next_command(char** cmdstr)
94 static pstring command;
95 char *p;
97 if (!cmdstr || !(*cmdstr))
98 return NULL;
100 p = strchr_m(*cmdstr, ';');
101 if (p)
102 *p = '\0';
103 pstrcpy(command, *cmdstr);
104 *cmdstr = p;
106 return command;
109 /* Load specified configuration file */
110 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
111 int argc, const char **argv)
113 if (argc != 2) {
114 printf("Usage: %s <smb.conf>\n", argv[0]);
115 return NT_STATUS_OK;
118 if (!lp_load(argv[1], False, True, False, True)) {
119 printf("Error loading \"%s\"\n", argv[1]);
120 return NT_STATUS_OK;
123 printf("\"%s\" successfully loaded\n", argv[1]);
124 return NT_STATUS_OK;
127 /* Display help on commands */
128 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
129 int argc, const char **argv)
131 struct cmd_list *tmp;
132 struct cmd_set *tmp_set;
134 /* Usage */
135 if (argc > 2) {
136 printf("Usage: %s [command]\n", argv[0]);
137 return NT_STATUS_OK;
140 /* Help on one command */
142 if (argc == 2) {
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 &&
150 tmp_set->usage[0])
151 printf("%s\n", tmp_set->usage);
152 else
153 printf("No help for %s\n", tmp_set->name);
155 return NT_STATUS_OK;
158 tmp_set++;
162 printf("No such command: %s\n", argv[1]);
163 return NT_STATUS_OK;
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:
176 "");
178 tmp_set++;
182 return NT_STATUS_OK;
185 /* Change the debug level */
186 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
188 if (argc > 2) {
189 printf("Usage: %s [debuglevel]\n", argv[0]);
190 return NT_STATUS_OK;
193 if (argc == 2) {
194 DEBUGLEVEL = atoi(argv[1]);
197 printf("debuglevel is %d\n", DEBUGLEVEL);
199 return NT_STATUS_OK;
202 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
204 /* Cleanup */
205 talloc_destroy(mem_ctx);
206 mem_ctx = NULL;
207 vfs->data = NULL;
208 vfs->data_size = 0;
209 return NT_STATUS_OK;
212 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
214 /* Cleanup */
215 talloc_destroy(mem_ctx);
217 exit(0);
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", "" },
233 { NULL }
236 static struct cmd_set separator_command[] = {
237 { "---------------", NULL, "----------------------" },
238 { NULL }
242 extern struct cmd_set vfs_commands[];
243 static struct cmd_set *vfstest_command_list[] = {
244 vfstest_commands,
245 vfs_commands,
246 NULL
249 static void add_command_set(struct cmd_set *cmd_set)
251 struct cmd_list *entry;
253 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
254 DEBUG(0, ("out of memory\n"));
255 return;
258 ZERO_STRUCTP(entry);
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 const char *p = cmd;
267 char **argv = NULL;
268 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
269 pstring buf;
270 TALLOC_CTX *mem_ctx = NULL;
271 int argc = 0, i;
273 /* Count number of arguments first time through the loop then
274 allocate memory and strdup them. */
276 again:
277 while(next_token(&p, buf, " ", sizeof(buf))) {
278 if (argv) {
279 argv[argc] = SMB_STRDUP(buf);
282 argc++;
285 if (!argv) {
287 /* Create argument list */
289 argv = SMB_MALLOC_ARRAY(char *, argc);
290 memset(argv, 0, sizeof(char *) * argc);
292 if (!argv) {
293 fprintf(stderr, "out of memory\n");
294 result = NT_STATUS_NO_MEMORY;
295 goto done;
298 p = cmd;
299 argc = 0;
301 goto again;
304 /* Call the function */
306 if (cmd_entry->fn) {
308 if (mem_ctx == NULL) {
309 /* Create mem_ctx */
310 if (!(mem_ctx = talloc_init("do_cmd"))) {
311 DEBUG(0, ("talloc_init() failed\n"));
312 goto done;
316 /* Run command */
317 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
319 } else {
320 fprintf (stderr, "Invalid command\n");
321 goto done;
324 done:
326 /* Cleanup */
328 if (argv) {
329 for (i = 0; i < argc; i++)
330 SAFE_FREE(argv[i]);
332 SAFE_FREE(argv);
335 return result;
338 /* Process a command entered at the prompt or as part of -c */
339 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
341 struct cmd_list *temp_list;
342 bool found = False;
343 pstring buf;
344 const char *p = cmd;
345 NTSTATUS result = NT_STATUS_OK;
346 int len = 0;
348 if (cmd[strlen(cmd) - 1] == '\n')
349 cmd[strlen(cmd) - 1] = '\0';
351 if (!next_token(&p, buf, " ", sizeof(buf))) {
352 return NT_STATUS_OK;
355 /* strip the trainly \n if it exsists */
356 len = strlen(buf);
357 if (buf[len-1] == '\n')
358 buf[len-1] = '\0';
360 /* Search for matching commands */
362 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
363 struct cmd_set *temp_set = temp_list->cmd_set;
365 while(temp_set->name) {
366 if (strequal(buf, temp_set->name)) {
367 found = True;
368 result = do_cmd(vfs, temp_set, cmd);
370 goto done;
372 temp_set++;
376 done:
377 if (!found && buf[0]) {
378 printf("command not found: %s\n", buf);
379 return NT_STATUS_OK;
382 if (!NT_STATUS_IS_OK(result)) {
383 printf("result was %s\n", nt_errstr(result));
386 return result;
389 static void process_file(struct vfs_state *pvfs, char *filename) {
390 FILE *file;
391 char command[3 * PATH_MAX];
393 if (*filename == '-') {
394 file = stdin;
395 } else {
396 file = fopen(filename, "r");
397 if (file == NULL) {
398 printf("vfstest: error reading file (%s)!", filename);
399 printf("errno n.%d: %s", errno, strerror(errno));
400 exit(-1);
404 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
405 process_cmd(pvfs, command);
409 void exit_server(const char *reason)
411 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
412 exit(0);
415 void exit_server_cleanly(const char *const reason)
417 exit_server("normal exit");
420 static int server_fd = -1;
421 int last_message = -1;
423 int smbd_server_fd(void)
425 return server_fd;
428 void reload_printers(void)
430 return;
433 /****************************************************************************
434 Reload the services file.
435 **************************************************************************/
437 bool reload_services(bool test)
439 bool ret;
441 if (lp_loaded()) {
442 pstring fname;
443 pstrcpy(fname,lp_configfile());
444 if (file_exist(fname, NULL) &&
445 !strcsequal(fname, dyn_CONFIGFILE)) {
446 pstrcpy(dyn_CONFIGFILE, fname);
447 test = False;
451 reopen_logs();
453 if (test && !lp_file_list_changed())
454 return(True);
456 lp_killunused(conn_snum_used);
458 ret = lp_load(dyn_CONFIGFILE, False, False, True, True);
460 /* perhaps the config filename is now set */
461 if (!test)
462 reload_services(True);
464 reopen_logs();
466 load_interfaces();
469 if (smbd_server_fd() != -1) {
470 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
471 set_socket_options(smbd_server_fd(),
472 lp_socket_options());
476 mangle_reset_cache();
477 reset_stat_cache();
479 /* this forces service parameters to be flushed */
480 set_current_service(NULL,0,True);
482 return (ret);
485 struct event_context *smbd_event_context(void)
487 static struct event_context *ctx;
489 if (!ctx && !(ctx = event_context_init(NULL))) {
490 smb_panic("Could not init smbd event context\n");
492 return ctx;
495 struct messaging_context *smbd_messaging_context(void)
497 static struct messaging_context *ctx;
499 if (!ctx && !(ctx = messaging_init(NULL, server_id_self(),
500 smbd_event_context()))) {
501 smb_panic("Could not init smbd messaging context\n");
503 return ctx;
506 /* Main function */
508 int main(int argc, char *argv[])
510 static char *cmdstr = NULL;
511 struct cmd_set **cmd_set;
512 static struct vfs_state vfs;
513 int i;
514 static char *filename = NULL;
516 /* make sure the vars that get altered (4th field) are in
517 a fixed location or certain compilers complain */
518 poptContext pc;
519 struct poptOption long_options[] = {
520 POPT_AUTOHELP
521 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
522 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
523 POPT_COMMON_SAMBA
524 POPT_TABLEEND
527 load_case_tables();
529 setlinebuf(stdout);
531 pc = poptGetContext("vfstest", argc, (const char **) argv,
532 long_options, 0);
534 while(poptGetNextOpt(pc) != -1);
537 poptFreeContext(pc);
539 /* TODO: check output */
540 reload_services(False);
542 /* the following functions are part of the Samba debugging
543 facilities. See lib/debug.c */
544 setup_logging("vfstest", True);
546 /* Load command lists */
548 cmd_set = vfstest_command_list;
550 while(*cmd_set) {
551 add_command_set(*cmd_set);
552 add_command_set(separator_command);
553 cmd_set++;
556 /* some basic initialization stuff */
557 sec_init();
558 conn_init();
559 vfs.conn = conn_new();
560 string_set(&vfs.conn->user,"vfstest");
561 for (i=0; i < 1024; i++)
562 vfs.files[i] = NULL;
564 /* some advanced initiliazation stuff */
565 smbd_vfs_init(vfs.conn);
567 /* Do we have a file input? */
568 if (filename && filename[0]) {
569 process_file(&vfs, filename);
570 return 0;
573 /* Do anything specified with -c */
574 if (cmdstr && cmdstr[0]) {
575 char *cmd;
576 char *p = cmdstr;
578 while((cmd=next_command(&p)) != NULL) {
579 process_cmd(&vfs, cmd);
582 return 0;
585 /* Loop around accepting commands */
587 while(1) {
588 pstring prompt;
589 char *line;
591 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
593 line = smb_readline(prompt, NULL, completion_fn);
595 if (line == NULL)
596 break;
598 if (line[0] != '\n')
599 process_cmd(&vfs, line);
602 conn_free(vfs.conn);
603 return 0;