s3:vfstest: reload_services() doesn't need a messaging_context
[nivanova/samba-autobuild/.git] / source3 / torture / vfstest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    VFS module tester
4
5    Copyright (C) Simo Sorce 2002
6    Copyright (C) Eric Lorimer 2002
7    Copyright (C) Jelmer Vernooij 2002,2003
8
9    Most of this code was ripped off of rpcclient.
10    Copyright (C) Tim Potter 2000-2001
11
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.
16    
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.
21    
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/>.
24 */
25
26 #include "includes.h"
27 #include "smbd/smbd.h"
28 #include "popt_common.h"
29 #include "vfstest.h"
30 #include "../libcli/smbreadline/smbreadline.h"
31
32 /* List to hold groups of commands */
33 static struct cmd_list {
34         struct cmd_list *prev, *next;
35         struct cmd_set *cmd_set;
36 } *cmd_list;
37
38 /****************************************************************************
39 handle completion of commands for readline
40 ****************************************************************************/
41 static char **completion_fn(const char *text, int start, int end)
42 {
43 #define MAX_COMPLETIONS 100
44         char **matches;
45         int i, count=0;
46         struct cmd_list *commands = cmd_list;
47
48         if (start) 
49                 return NULL;
50
51         /* make sure we have a list of valid commands */
52         if (!commands) 
53                 return NULL;
54
55         matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
56         if (!matches) return NULL;
57
58         matches[count++] = SMB_STRDUP(text);
59         if (!matches[0]) return NULL;
60
61         while (commands && count < MAX_COMPLETIONS-1) 
62         {
63                 if (!commands->cmd_set)
64                         break;
65                 
66                 for (i=0; commands->cmd_set[i].name; i++)
67                 {
68                         if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
69                                 commands->cmd_set[i].fn) 
70                         {
71                                 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
72                                 if (!matches[count]) 
73                                         return NULL;
74                                 count++;
75                         }
76                 }
77                 
78                 commands = commands->next;
79                 
80         }
81
82         if (count == 2) {
83                 SAFE_FREE(matches[0]);
84                 matches[0] = SMB_STRDUP(matches[1]);
85         }
86         matches[count] = NULL;
87         return matches;
88 }
89
90 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
91 {
92         char *command;
93         char *p;
94
95         if (!cmdstr || !(*cmdstr))
96                 return NULL;
97
98         p = strchr_m(*cmdstr, ';');
99         if (p)
100                 *p = '\0';
101         command = talloc_strdup(ctx, *cmdstr);
102         *cmdstr = p;
103
104         return command;
105 }
106
107 /* Load specified configuration file */
108 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
109                         int argc, const char **argv)
110 {
111         if (argc != 2) {
112                 printf("Usage: %s <smb.conf>\n", argv[0]);
113                 return NT_STATUS_OK;
114         }
115
116         if (!lp_load(argv[1], False, True, False, True)) {
117                 printf("Error loading \"%s\"\n", argv[1]);
118                 return NT_STATUS_OK;
119         }
120
121         printf("\"%s\" successfully loaded\n", argv[1]);
122         return NT_STATUS_OK;
123 }
124         
125 /* Display help on commands */
126 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
127                          int argc, const char **argv)
128 {
129         struct cmd_list *tmp;
130         struct cmd_set *tmp_set;
131
132         /* Usage */
133         if (argc > 2) {
134                 printf("Usage: %s [command]\n", argv[0]);
135                 return NT_STATUS_OK;
136         }
137
138         /* Help on one command */
139
140         if (argc == 2) {
141                 for (tmp = cmd_list; tmp; tmp = tmp->next) {
142                         
143                         tmp_set = tmp->cmd_set;
144
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);
152
153                                         return NT_STATUS_OK;
154                                 }
155
156                                 tmp_set++;
157                         }
158                 }
159
160                 printf("No such command: %s\n", argv[1]);
161                 return NT_STATUS_OK;
162         }
163
164         /* List all commands */
165
166         for (tmp = cmd_list; tmp; tmp = tmp->next) {
167
168                 tmp_set = tmp->cmd_set;
169
170                 while(tmp_set->name) {
171
172                         printf("%15s\t\t%s\n", tmp_set->name,
173                                tmp_set->description ? tmp_set->description:
174                                "");
175
176                         tmp_set++;
177                 }
178         }
179
180         return NT_STATUS_OK;
181 }
182
183 /* Change the debug level */
184 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
185 {
186         if (argc > 2) {
187                 printf("Usage: %s [debuglevel]\n", argv[0]);
188                 return NT_STATUS_OK;
189         }
190
191         if (argc == 2) {
192                 lp_set_cmdline("log level", argv[1]);
193         }
194
195         printf("debuglevel is %d\n", DEBUGLEVEL);
196
197         return NT_STATUS_OK;
198 }
199
200 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
201 {
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;
208 }
209
210 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
211 {
212         /* Cleanup */
213         talloc_destroy(mem_ctx);
214
215         exit(0);
216         return NT_STATUS_OK; /* NOTREACHED */
217 }
218
219 static struct cmd_set vfstest_commands[] = {
220
221         { "GENERAL OPTIONS" },
222
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", "" },
230
231         { NULL }
232 };
233
234 static struct cmd_set separator_command[] = {
235         { "---------------", NULL,      "----------------------" },
236         { NULL }
237 };
238
239
240 extern struct cmd_set vfs_commands[];
241 static struct cmd_set *vfstest_command_list[] = {
242         vfstest_commands,
243         vfs_commands,
244         NULL
245 };
246
247 static void add_command_set(struct cmd_set *cmd_set)
248 {
249         struct cmd_list *entry;
250
251         if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
252                 DEBUG(0, ("out of memory\n"));
253                 return;
254         }
255
256         ZERO_STRUCTP(entry);
257
258         entry->cmd_set = cmd_set;
259         DLIST_ADD(cmd_list, entry);
260 }
261
262 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
263 {
264         const char *p = cmd;
265         char **argv = NULL;
266         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
267         char *buf;
268         TALLOC_CTX *mem_ctx = talloc_stackframe();
269         int argc = 0, i;
270
271         /* Count number of arguments first time through the loop then
272            allocate memory and strdup them. */
273
274  again:
275         while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
276                 if (argv) {
277                         argv[argc] = SMB_STRDUP(buf);
278                 }
279                 argc++;
280         }
281
282         if (!argv) {
283                 /* Create argument list */
284
285                 argv = SMB_MALLOC_ARRAY(char *, argc);
286                 memset(argv, 0, sizeof(char *) * argc);
287
288                 if (!argv) {
289                         fprintf(stderr, "out of memory\n");
290                         result = NT_STATUS_NO_MEMORY;
291                         goto done;
292                 }
293
294                 p = cmd;
295                 argc = 0;
296
297                 goto again;
298         }
299
300         /* Call the function */
301
302         if (cmd_entry->fn) {
303                 /* Run command */
304                 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
305         } else {
306                 fprintf (stderr, "Invalid command\n");
307                 goto done;
308         }
309
310  done:
311
312         /* Cleanup */
313
314         if (argv) {
315                 for (i = 0; i < argc; i++)
316                         SAFE_FREE(argv[i]);
317
318                 SAFE_FREE(argv);
319         }
320
321         TALLOC_FREE(mem_ctx);
322         return result;
323 }
324
325 /* Process a command entered at the prompt or as part of -c */
326 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
327 {
328         struct cmd_list *temp_list;
329         bool found = False;
330         char *buf;
331         const char *p = cmd;
332         NTSTATUS result = NT_STATUS_OK;
333         TALLOC_CTX *mem_ctx = talloc_stackframe();
334         int len = 0;
335
336         if (cmd[strlen(cmd) - 1] == '\n')
337                 cmd[strlen(cmd) - 1] = '\0';
338
339         if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
340                 TALLOC_FREE(mem_ctx);
341                 return NT_STATUS_OK;
342         }
343
344         /* Strip the trailing \n if it exists */
345         len = strlen(buf);
346         if (buf[len-1] == '\n')
347                 buf[len-1] = '\0';
348
349         /* Search for matching commands */
350
351         for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
352                 struct cmd_set *temp_set = temp_list->cmd_set;
353
354                 while(temp_set->name) {
355                         if (strequal(buf, temp_set->name)) {
356                                 found = True;
357                                 result = do_cmd(vfs, temp_set, cmd);
358
359                                 goto done;
360                         }
361                         temp_set++;
362                 }
363         }
364
365  done:
366         if (!found && buf[0]) {
367                 printf("command not found: %s\n", buf);
368                 TALLOC_FREE(mem_ctx);
369                 return NT_STATUS_OK;
370         }
371
372         if (!NT_STATUS_IS_OK(result)) {
373                 printf("result was %s\n", nt_errstr(result));
374         }
375
376         TALLOC_FREE(mem_ctx);
377         return result;
378 }
379
380 static void process_file(struct vfs_state *pvfs, char *filename) {
381         FILE *file;
382         char command[3 * PATH_MAX];
383
384         if (*filename == '-') {
385                 file = stdin;
386         } else {
387                 file = fopen(filename, "r");
388                 if (file == NULL) {
389                         printf("vfstest: error reading file (%s)!", filename);
390                         printf("errno n.%d: %s", errno, strerror(errno));
391                         exit(-1);
392                 }
393         }
394
395         while (fgets(command, 3 * PATH_MAX, file) != NULL) {
396                 process_cmd(pvfs, command);
397         }
398
399         if (file != stdin) {
400                 fclose(file);
401         }
402 }
403
404 void exit_server(const char *reason)
405 {
406         DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
407         exit(0);
408 }
409
410 void exit_server_cleanly(const char *const reason)
411 {
412         exit_server("normal exit");
413 }
414
415 int last_message = -1;
416
417 /* Main function */
418
419 int main(int argc, char *argv[])
420 {
421         static char             *cmdstr = NULL;
422         struct cmd_set          **cmd_set;
423         static struct vfs_state vfs;
424         int i;
425         static char             *filename = NULL;
426         TALLOC_CTX *frame = talloc_stackframe();
427
428         /* make sure the vars that get altered (4th field) are in
429            a fixed location or certain compilers complain */
430         poptContext pc;
431         struct poptOption long_options[] = {
432                 POPT_AUTOHELP
433                 {"file",        'f', POPT_ARG_STRING,   &filename, 0, },
434                 {"command",     'c', POPT_ARG_STRING,   &cmdstr, 0, "Execute specified list of commands" },
435                 POPT_COMMON_SAMBA
436                 POPT_TABLEEND
437         };
438
439         load_case_tables();
440
441         setlinebuf(stdout);
442
443         pc = poptGetContext("vfstest", argc, (const char **) argv,
444                             long_options, 0);
445         
446         while(poptGetNextOpt(pc) != -1);
447
448
449         poptFreeContext(pc);
450
451         lp_load_initial_only(get_dyn_CONFIGFILE());
452
453         /* TODO: check output */
454         reload_services(NULL, -1, False);
455
456         /* the following functions are part of the Samba debugging
457            facilities.  See lib/debug.c */
458         setup_logging("vfstest", DEBUG_STDOUT);
459         
460         /* Load command lists */
461
462         cmd_set = vfstest_command_list;
463
464         while(*cmd_set) {
465                 add_command_set(*cmd_set);
466                 add_command_set(separator_command);
467                 cmd_set++;
468         }
469
470         /* some basic initialization stuff */
471         sec_init();
472         vfs.conn = talloc_zero(NULL, connection_struct);
473         vfs.conn->params = talloc(vfs.conn, struct share_params);
474         for (i=0; i < 1024; i++)
475                 vfs.files[i] = NULL;
476
477         /* some advanced initiliazation stuff */
478         smbd_vfs_init(vfs.conn);
479
480         /* Do we have a file input? */
481         if (filename && filename[0]) {
482                 process_file(&vfs, filename);
483                 return 0;
484         }
485
486         /* Do anything specified with -c */
487         if (cmdstr && cmdstr[0]) {
488                 char    *cmd;
489                 char    *p = cmdstr;
490
491                 while((cmd=next_command(frame, &p)) != NULL) {
492                         process_cmd(&vfs, cmd);
493                 }
494
495                 TALLOC_FREE(cmd);
496                 return 0;
497         }
498
499         /* Loop around accepting commands */
500
501         while(1) {
502                 char *line = NULL;
503
504                 line = smb_readline("vfstest $> ", NULL, completion_fn);
505
506                 if (line == NULL) {
507                         break;
508                 }
509
510                 if (line[0] != '\n') {
511                         process_cmd(&vfs, line);
512                 }
513                 SAFE_FREE(line);
514         }
515
516         TALLOC_FREE(vfs.conn);
517         TALLOC_FREE(frame);
518         return 0;
519 }