source: trunk-3.0/source/torture/vfstest.c@ 101

Last change on this file since 101 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 12.1 KB
Line 
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 2 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, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27#include "includes.h"
28#include "vfstest.h"
29
30/* List to hold groups of commands */
31static struct cmd_list {
32 struct cmd_list *prev, *next;
33 struct cmd_set *cmd_set;
34} *cmd_list;
35
36extern pstring user_socket_options;
37
38/****************************************************************************
39handle completion of commands for readline
40****************************************************************************/
41static 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
90static char* next_command(char** cmdstr)
91{
92 static pstring 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 pstrcpy(command, *cmdstr);
102 *cmdstr = p;
103
104 return command;
105}
106
107/* Load specified configuration file */
108static 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 */
126static 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 */
184static 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 DEBUGLEVEL = atoi(argv[1]);
193 }
194
195 printf("debuglevel is %d\n", DEBUGLEVEL);
196
197 return NT_STATUS_OK;
198}
199
200static 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
210static 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
219static 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
234static struct cmd_set separator_command[] = {
235 { "---------------", NULL, "----------------------" },
236 { NULL }
237};
238
239
240extern struct cmd_set vfs_commands[];
241static struct cmd_set *vfstest_command_list[] = {
242 vfstest_commands,
243 vfs_commands,
244 NULL
245};
246
247static 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
262static 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 pstring buf;
268 TALLOC_CTX *mem_ctx = NULL;
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(&p, buf, " ", sizeof(buf))) {
276 if (argv) {
277 argv[argc] = SMB_STRDUP(buf);
278 }
279
280 argc++;
281 }
282
283 if (!argv) {
284
285 /* Create argument list */
286
287 argv = SMB_MALLOC_ARRAY(char *, argc);
288 memset(argv, 0, sizeof(char *) * argc);
289
290 if (!argv) {
291 fprintf(stderr, "out of memory\n");
292 result = NT_STATUS_NO_MEMORY;
293 goto done;
294 }
295
296 p = cmd;
297 argc = 0;
298
299 goto again;
300 }
301
302 /* Call the function */
303
304 if (cmd_entry->fn) {
305
306 if (mem_ctx == NULL) {
307 /* Create mem_ctx */
308 if (!(mem_ctx = talloc_init("do_cmd"))) {
309 DEBUG(0, ("talloc_init() failed\n"));
310 goto done;
311 }
312 }
313
314 /* Run command */
315 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
316
317 } else {
318 fprintf (stderr, "Invalid command\n");
319 goto done;
320 }
321
322 done:
323
324 /* Cleanup */
325
326 if (argv) {
327 for (i = 0; i < argc; i++)
328 SAFE_FREE(argv[i]);
329
330 SAFE_FREE(argv);
331 }
332
333 return result;
334}
335
336/* Process a command entered at the prompt or as part of -c */
337static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
338{
339 struct cmd_list *temp_list;
340 BOOL found = False;
341 pstring buf;
342 const char *p = cmd;
343 NTSTATUS result = NT_STATUS_OK;
344 int len = 0;
345
346 if (cmd[strlen(cmd) - 1] == '\n')
347 cmd[strlen(cmd) - 1] = '\0';
348
349 if (!next_token(&p, buf, " ", sizeof(buf))) {
350 return NT_STATUS_OK;
351 }
352
353 /* strip the trainly \n if it exsists */
354 len = strlen(buf);
355 if (buf[len-1] == '\n')
356 buf[len-1] = '\0';
357
358 /* Search for matching commands */
359
360 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
361 struct cmd_set *temp_set = temp_list->cmd_set;
362
363 while(temp_set->name) {
364 if (strequal(buf, temp_set->name)) {
365 found = True;
366 result = do_cmd(vfs, temp_set, cmd);
367
368 goto done;
369 }
370 temp_set++;
371 }
372 }
373
374 done:
375 if (!found && buf[0]) {
376 printf("command not found: %s\n", buf);
377 return NT_STATUS_OK;
378 }
379
380 if (!NT_STATUS_IS_OK(result)) {
381 printf("result was %s\n", nt_errstr(result));
382 }
383
384 return result;
385}
386
387static void process_file(struct vfs_state *pvfs, char *filename) {
388 FILE *file;
389 char command[3 * PATH_MAX];
390
391 if (*filename == '-') {
392 file = stdin;
393 } else {
394 file = fopen(filename, "r");
395 if (file == NULL) {
396 printf("vfstest: error reading file (%s)!", filename);
397 printf("errno n.%d: %s", errno, strerror(errno));
398 exit(-1);
399 }
400 }
401
402 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
403 process_cmd(pvfs, command);
404 }
405}
406
407void exit_server(const char *reason)
408{
409 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
410 exit(0);
411}
412
413void exit_server_cleanly(const char *const reason)
414{
415 exit_server("normal exit");
416}
417
418static int server_fd = -1;
419int last_message = -1;
420
421int smbd_server_fd(void)
422{
423 return server_fd;
424}
425
426void reload_printers(void)
427{
428 return;
429}
430
431/****************************************************************************
432 Reload the services file.
433**************************************************************************/
434
435BOOL reload_services(BOOL test)
436{
437 BOOL ret;
438
439 if (lp_loaded()) {
440 pstring fname;
441 pstrcpy(fname,lp_configfile());
442 if (file_exist(fname, NULL) &&
443 !strcsequal(fname, dyn_CONFIGFILE)) {
444 pstrcpy(dyn_CONFIGFILE, fname);
445 test = False;
446 }
447 }
448
449 reopen_logs();
450
451 if (test && !lp_file_list_changed())
452 return(True);
453
454 lp_killunused(conn_snum_used);
455
456 ret = lp_load(dyn_CONFIGFILE, False, False, True, True);
457
458 /* perhaps the config filename is now set */
459 if (!test)
460 reload_services(True);
461
462 reopen_logs();
463
464 load_interfaces();
465
466 {
467 if (smbd_server_fd() != -1) {
468 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
469 set_socket_options(smbd_server_fd(), user_socket_options);
470 }
471 }
472
473 mangle_reset_cache();
474 reset_stat_cache();
475
476 /* this forces service parameters to be flushed */
477 set_current_service(NULL,0,True);
478
479 return (ret);
480}
481
482struct event_context *smbd_event_context(void)
483{
484 static struct event_context *ctx;
485
486 if (!ctx && !(ctx = event_context_init(NULL))) {
487 smb_panic("Could not init smbd event context\n");
488 }
489 return ctx;
490}
491
492struct messaging_context *smbd_messaging_context(void)
493{
494 static struct messaging_context *ctx;
495
496 if (!ctx && !(ctx = messaging_init(NULL, server_id_self(),
497 smbd_event_context()))) {
498 smb_panic("Could not init smbd messaging context\n");
499 }
500 return ctx;
501}
502
503/* Main function */
504
505int main(int argc, char *argv[])
506{
507 static char *cmdstr = NULL;
508 struct cmd_set **cmd_set;
509 static struct vfs_state vfs;
510 int i;
511 static char *filename = NULL;
512
513 /* make sure the vars that get altered (4th field) are in
514 a fixed location or certain compilers complain */
515 poptContext pc;
516 struct poptOption long_options[] = {
517 POPT_AUTOHELP
518 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
519 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
520 POPT_COMMON_SAMBA
521 POPT_TABLEEND
522 };
523
524 load_case_tables();
525
526 setlinebuf(stdout);
527
528 pc = poptGetContext("vfstest", argc, (const char **) argv,
529 long_options, 0);
530
531 while(poptGetNextOpt(pc) != -1);
532
533
534 poptFreeContext(pc);
535
536 /* TODO: check output */
537 reload_services(False);
538
539 /* the following functions are part of the Samba debugging
540 facilities. See lib/debug.c */
541 setup_logging("vfstest", True);
542
543 /* Load command lists */
544
545 cmd_set = vfstest_command_list;
546
547 while(*cmd_set) {
548 add_command_set(*cmd_set);
549 add_command_set(separator_command);
550 cmd_set++;
551 }
552
553 /* some basic initialization stuff */
554 sec_init();
555 conn_init();
556 vfs.conn = conn_new();
557 string_set(&vfs.conn->user,"vfstest");
558 for (i=0; i < 1024; i++)
559 vfs.files[i] = NULL;
560
561 /* some advanced initiliazation stuff */
562 smbd_vfs_init(vfs.conn);
563
564 /* Do we have a file input? */
565 if (filename && filename[0]) {
566 process_file(&vfs, filename);
567 return 0;
568 }
569
570 /* Do anything specified with -c */
571 if (cmdstr && cmdstr[0]) {
572 char *cmd;
573 char *p = cmdstr;
574
575 while((cmd=next_command(&p)) != NULL) {
576 process_cmd(&vfs, cmd);
577 }
578
579 return 0;
580 }
581
582 /* Loop around accepting commands */
583
584 while(1) {
585 pstring prompt;
586 char *line;
587
588 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
589
590 line = smb_readline(prompt, NULL, completion_fn);
591
592 if (line == NULL)
593 break;
594
595 if (line[0] != '\n')
596 process_cmd(&vfs, line);
597 }
598
599 conn_free(vfs.conn);
600 return 0;
601}
Note: See TracBrowser for help on using the repository browser.