implemented lock and mknod
[samba.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
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 */
31 static struct cmd_list {
32         struct cmd_list *prev, *next;
33         struct cmd_set *cmd_set;
34 } *cmd_list;
35
36 TALLOC_CTX *global_ctx;
37
38 extern pstring user_socket_options;
39
40 /****************************************************************************
41 handle completion of commands for readline
42 ****************************************************************************/
43 static char **completion_fn(char *text, int start, int end)
44 {
45 #define MAX_COMPLETIONS 100
46         char **matches;
47         int i, count=0;
48         struct cmd_list *commands = cmd_list;
49
50         if (start) 
51                 return NULL;
52
53         /* make sure we have a list of valid commands */
54         if (!commands) 
55                 return NULL;
56
57         matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
58         if (!matches) return NULL;
59
60         matches[count++] = strdup(text);
61         if (!matches[0]) return NULL;
62
63         while (commands && count < MAX_COMPLETIONS-1) 
64         {
65                 if (!commands->cmd_set)
66                         break;
67                 
68                 for (i=0; commands->cmd_set[i].name; i++)
69                 {
70                         if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
71                                 commands->cmd_set[i].fn) 
72                         {
73                                 matches[count] = strdup(commands->cmd_set[i].name);
74                                 if (!matches[count]) 
75                                         return NULL;
76                                 count++;
77                         }
78                 }
79                 
80                 commands = commands->next;
81                 
82         }
83
84         if (count == 2) {
85                 SAFE_FREE(matches[0]);
86                 matches[0] = strdup(matches[1]);
87         }
88         matches[count] = NULL;
89         return matches;
90 }
91
92 static char* next_command(char** cmdstr)
93 {
94         static pstring          command;
95         char                    *p;
96         
97         if (!cmdstr || !(*cmdstr))
98                 return NULL;
99         
100         p = strchr_m(*cmdstr, ';');
101         if (p)
102                 *p = '\0';
103         pstrcpy(command, *cmdstr);
104         *cmdstr = p;
105         
106         return command;
107 }
108
109 /* Load specified configuration file */
110 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
111                         int argc, char **argv)
112 {
113         if (argc != 2) {
114                 printf("Usage: %s <smb.conf>\n", argv[0]);
115                 return NT_STATUS_OK;
116         }
117
118         if (!lp_load(argv[1], False, True, False)) {
119                 printf("Error loading \"%s\"\n", argv[1]);
120                 return NT_STATUS_OK;
121         }
122
123         printf("\"%s\" successfully loaded\n", argv[1]);
124         return NT_STATUS_OK;
125 }
126         
127 /* Display help on commands */
128 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
129                          int argc, char **argv)
130 {
131         struct cmd_list *tmp;
132         struct cmd_set *tmp_set;
133
134         /* Usage */
135         if (argc > 2) {
136                 printf("Usage: %s [command]\n", argv[0]);
137                 return NT_STATUS_OK;
138         }
139
140         /* Help on one command */
141
142         if (argc == 2) {
143                 for (tmp = cmd_list; tmp; tmp = tmp->next) {
144                         
145                         tmp_set = tmp->cmd_set;
146
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);
154
155                                         return NT_STATUS_OK;
156                                 }
157
158                                 tmp_set++;
159                         }
160                 }
161
162                 printf("No such command: %s\n", argv[1]);
163                 return NT_STATUS_OK;
164         }
165
166         /* List all commands */
167
168         for (tmp = cmd_list; tmp; tmp = tmp->next) {
169
170                 tmp_set = tmp->cmd_set;
171
172                 while(tmp_set->name) {
173
174                         printf("%15s\t\t%s\n", tmp_set->name,
175                                tmp_set->description ? tmp_set->description:
176                                "");
177
178                         tmp_set++;
179                 }
180         }
181
182         return NT_STATUS_OK;
183 }
184
185 /* Change the debug level */
186 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
187 {
188         if (argc > 2) {
189                 printf("Usage: %s [debuglevel]\n", argv[0]);
190                 return NT_STATUS_OK;
191         }
192
193         if (argc == 2) {
194                 DEBUGLEVEL = atoi(argv[1]);
195         }
196
197         printf("debuglevel is %d\n", DEBUGLEVEL);
198
199         return NT_STATUS_OK;
200 }
201
202 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
203 {
204         /* Cleanup */
205         talloc_destroy(global_ctx);
206         global_ctx = NULL;
207         vfs->data = NULL;
208         vfs->data_size = 0;
209         return NT_STATUS_OK;
210 }
211
212 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
213 {
214         /* Cleanup */
215         talloc_destroy(global_ctx);
216
217         exit(0);
218         return NT_STATUS_OK; /* NOTREACHED */
219 }
220
221 static struct cmd_set vfstest_commands[] = {
222
223         { "GENERAL OPTIONS" },
224
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", "" },
232
233         { NULL }
234 };
235
236 static struct cmd_set separator_command[] = {
237         { "---------------", NULL,      "----------------------" },
238         { NULL }
239 };
240
241
242 extern struct cmd_set vfs_commands[];
243 static struct cmd_set *vfstest_command_list[] = {
244         vfstest_commands,
245         vfs_commands,
246         NULL
247 };
248
249 static void add_command_set(struct cmd_set *cmd_set)
250 {
251         struct cmd_list *entry;
252
253         if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
254                 DEBUG(0, ("out of memory\n"));
255                 return;
256         }
257
258         ZERO_STRUCTP(entry);
259
260         entry->cmd_set = cmd_set;
261         DLIST_ADD(cmd_list, entry);
262 }
263
264 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
265 {
266         char *p = cmd, **argv = NULL;
267         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
268         pstring buf;
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] = strdup(buf);
278                 }
279                 
280                 argc++;
281         }
282                                 
283         if (!argv) {
284
285                 /* Create argument list */
286
287                 argv = (char **)malloc(sizeof(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 (global_ctx == NULL) {
307                         /* Create mem_ctx */
308                         if (!(global_ctx = talloc_init())) {
309                                 DEBUG(0, ("talloc_init() failed\n"));
310                                 goto done;
311                         }
312                 }
313
314                 /* Run command */
315                 result = cmd_entry->fn(vfs, global_ctx, argc, 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 */
337 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
338 {
339         struct cmd_list *temp_list;
340         BOOL found = False;
341         pstring buf;
342         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
387 static 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
407 void exit_server(char *reason)
408 {
409         DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
410         exit(0);
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 /****************************************************************************
422  Reload the services file.
423 **************************************************************************/
424
425 BOOL reload_services(BOOL test)
426 {
427         BOOL ret;
428         
429         if (lp_loaded()) {
430                 pstring fname;
431                 pstrcpy(fname,lp_configfile());
432                 if (file_exist(fname, NULL) &&
433                     !strcsequal(fname, dyn_CONFIGFILE)) {
434                         pstrcpy(dyn_CONFIGFILE, fname);
435                         test = False;
436                 }
437         }
438
439         reopen_logs();
440
441         if (test && !lp_file_list_changed())
442                 return(True);
443
444         lp_killunused(conn_snum_used);
445         
446         ret = lp_load(dyn_CONFIGFILE, False, False, True);
447
448         load_printers();
449
450         /* perhaps the config filename is now set */
451         if (!test)
452                 reload_services(True);
453
454         reopen_logs();
455
456         load_interfaces();
457
458         {
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);
462                 }
463         }
464
465         mangle_reset_cache();
466         reset_stat_cache();
467
468         /* this forces service parameters to be flushed */
469         set_current_service(NULL,True);
470
471         return (ret);
472 }
473
474 /* Print usage information */
475 static void usage(void)
476 {
477         printf("Usage: vfstest [options]\n");
478
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");
484         printf("\n");
485 }
486
487 /* Main function */
488
489 int main(int argc, char *argv[])
490 {
491         BOOL                    interactive = True;
492         int                     opt;
493         static char             *cmdstr = "";
494         static char             *opt_logfile=NULL;
495         static int              opt_debuglevel;
496         pstring                 logfile;
497         struct cmd_set          **cmd_set;
498         extern BOOL             AllowDebugChange;
499         static struct vfs_state vfs;
500         int i;
501         static char             *filename = "";
502
503         /* make sure the vars that get altered (4th field) are in
504            a fixed location or certain compilers complain */
505         poptContext pc;
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'},
513                 { 0, 0, 0, 0}
514         };
515
516
517         setlinebuf(stdout);
518
519         DEBUGLEVEL = 1;
520         AllowDebugChange = False;
521
522         pc = poptGetContext("vfstest", argc, (const char **) argv,
523                             long_options, 0);
524         
525         while((opt = poptGetNextOpt(pc)) != -1) {
526                 switch (opt) {
527                 case 'l':
528                         slprintf(logfile, sizeof(logfile) - 1, "%s.client", 
529                                  opt_logfile);
530                         lp_set_logfile(logfile);
531                         interactive = False;
532                         break;
533                         
534                 case 'd':
535                         DEBUGLEVEL = opt_debuglevel;
536                         break;
537                         
538                 case 'h':
539                 default:
540                         usage();
541                         exit(1);
542                 }
543         }
544
545
546         poptFreeContext(pc);
547
548         /* TODO: check output */
549         reload_services(False);
550
551         /* the following functions are part of the Samba debugging
552            facilities.  See lib/debug.c */
553         setup_logging("vfstest", interactive);
554         if (!interactive) 
555                 reopen_logs();
556         
557         /* Load command lists */
558
559         cmd_set = vfstest_command_list;
560
561         while(*cmd_set) {
562                 add_command_set(*cmd_set);
563                 add_command_set(separator_command);
564                 cmd_set++;
565         }
566
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++)
571                 vfs.files[i] = NULL;
572
573         /* some advanced initiliazation stuff */
574         smbd_vfs_init(vfs.conn);
575
576         /* Do we have a file input? */
577         if (filename[0]) {
578                 process_file(&vfs, filename);
579                 return 0;
580         }
581
582         /* Do anything specified with -c */
583         if (cmdstr[0]) {
584                 char    *cmd;
585                 char    *p = cmdstr;
586  
587                 while((cmd=next_command(&p)) != NULL) {
588                         process_cmd(&vfs, cmd);
589                 }
590                 
591                 return 0;
592         }
593
594         /* Loop around accepting commands */
595
596         while(1) {
597                 pstring prompt;
598                 char *line;
599
600                 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
601
602                 line = smb_readline(prompt, NULL, completion_fn);
603
604                 if (line == NULL)
605                         break;
606
607                 if (line[0] != '\n')
608                         process_cmd(&vfs, line);
609         }
610         
611         free(vfs.conn);
612         return 0;
613 }