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