source: rtems/testsuites/samples/fileio/init.c @ a645637

4.104.114.95
Last change on this file since a645637 was a645637, checked in by Joel Sherrill <joel.sherrill@…>, on Apr 17, 2008 at 10:46:15 PM

2008-04-17 Joel Sherrill <joel.sherrill@…>

  • fileio/init.c, hello/system.h, minimum/init.c, unlimited/system.h: Use CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER where needed.
  • Property mode set to 100644
File size: 16.2 KB
Line 
1/*  Init
2 *
3 *  This routine is the initialization task for this test program.
4 *  It is called from init_exec and has the responsibility for creating
5 *  and starting the tasks that make up the test.  If the time of day
6 *  clock is required for the test, it should also be set to a known
7 *  value by this function.
8 *
9 *  Input parameters:  NONE
10 *
11 *  Output parameters:  NONE
12 *
13 *  COPYRIGHT (c) 1989-2007.
14 *  On-Line Applications Research Corporation (OAR).
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.com/license/LICENSE.
19 *
20 *  init.c,v 1.11 2000/06/12 15:00:12 joel Exp
21 */
22
23#define CONFIGURE_INIT
24#include "system.h"
25#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <errno.h>
30#include <rtems.h>
31#include <fcntl.h>
32#include <rtems/error.h>
33#include <rtems/dosfs.h>
34#include <ctype.h>
35#include <rtems/ide_part_table.h>
36#include <rtems/libcsupport.h>
37#include <rtems/fsmount.h>
38
39/*
40 * Table of FAT file systems that will be mounted
41 * with the "fsmount" function.
42 * See cpukit/libmisc/fsmount for definition of fields
43 */
44fstab_t fs_table[] = {
45  {
46    "/dev/hda1","/mnt/hda1",
47    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
48    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
49    0
50  },
51  {
52    "/dev/hda2","/mnt/hda2",
53    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
54    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
55    0
56  },
57  {
58    "/dev/hda3","/mnt/hda3",
59    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
60    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
61    0
62  },
63  {
64    "/dev/hda4","/mnt/hda4",
65    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
66    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
67    0
68  },
69  {
70    "/dev/hdc1","/mnt/hdc1",
71    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
72    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
73    0
74  },
75  {
76    "/dev/hdc2","/mnt/hdc2",
77    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
78    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
79    0
80  },
81  {
82    "/dev/hdc3","/mnt/hdc3",
83    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
84    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
85    0
86  },
87  {
88    "/dev/hdc4","/mnt/hdc4",
89    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
90    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
91    0
92  }
93};
94
95#define MIN(a,b) (((a) > (b)) ? (b) : (a))
96
97#define USE_SHELL
98
99#ifdef USE_SHELL
100#include <rtems/shell.h>
101
102void writeScript(
103  const char *name,
104  const char *contents
105)
106{
107  int sc;
108  sc = setuid(0);
109  if ( sc ) {
110    printf( "setuid failed: %s:\n", name, strerror(errno) );
111  }
112
113  rtems_shell_write_file( name, contents );
114  sc = chmod ( name, 0777 );
115  if ( sc ) {
116    printf( "chmod %s: %s:\n", name, strerror(errno) );
117  }
118}
119
120void fileio_start_shell(void)
121{
122  int sc;
123  sc = mkdir("/scripts", 0777);
124  if ( sc ) {
125    printf( "mkdir /scripts: %s:\n", strerror(errno) );
126  }
127
128  writeScript(
129    "/scripts/js", 
130    "#! joel\n"
131    "\n"
132    "date\n"
133    "echo Script successfully ran\n"
134    "date\n"
135    "stackuse\n"
136  );
137
138  writeScript(
139    "/scripts/j1", 
140    "#! joel -s 20480 -t JESS\n"
141    "stackuse\n"
142  );
143
144  rtems_shell_write_file(
145    "/scripts/j2", 
146    "echo j2 TEST FILE\n"
147    "echo j2   SHOULD BE non-executable AND\n"
148    "echo j2   DOES NOT have the magic first line\n"
149  );
150
151  printf(" =========================\n");
152  printf(" starting shell\n");
153  printf(" =========================\n");
154  rtems_shell_init("SHLL",RTEMS_MINIMUM_STACK_SIZE * 4,100,"/dev/console",
155                   0, 1);
156  rtems_task_suspend(RTEMS_SELF);
157}
158
159#endif /* USE_SHELL */
160
161void fileio_print_free_heap(void)
162{
163  printf("--- unused dynamic memory: %lu bytes ---\n",
164         (unsigned long) malloc_free_space());
165}
166
167
168void fileio_part_table_initialize(void)
169{
170  char devname[64];
171  rtems_status_code rc;
172
173  printf(" =========================\n");
174  printf(" Initialize partition table\n");
175  printf(" =========================\n");
176  fileio_print_free_heap();
177  printf(" Enter device to initialize ==>");
178  fflush(stdout);
179  fgets(devname,sizeof(devname)-1,stdin);
180  while (devname[strlen(devname)-1] == '\n') {
181    devname[strlen(devname)-1] = '\0';
182  }
183  /*
184   * call function
185   */
186  rc = rtems_ide_part_table_initialize(devname);
187  printf("result = %d\n",rc);
188  fileio_print_free_heap();
189}
190
191void fileio_fsmount(void)
192{
193  rtems_status_code rc;
194
195  printf(" =========================\n");
196  printf(" Process fsmount table\n");
197  printf(" =========================\n");
198  fileio_print_free_heap();
199  /*
200   * call function
201   */
202  rc = rtems_fsmount( fs_table,
203                      sizeof(fs_table)/sizeof(fs_table[0]),
204                      NULL);
205  printf("result = %d\n",rc);
206  fileio_print_free_heap();
207}
208
209void fileio_list_file(void)
210{
211  char fname[1024];
212  char *buf_ptr = NULL;
213  ssize_t   flen = 0;
214  int fd = -1;
215  ssize_t n;
216  size_t buf_size = 100;
217
218  rtems_interval start_tick,curr_tick,ticks_per_sec;
219
220  printf(" =========================\n");
221  printf(" LIST FILE ...            \n");
222  printf(" =========================\n");
223  fileio_print_free_heap();
224  printf(" Enter filename to list ==>");
225  fflush(stdout);
226  fgets(fname,sizeof(fname)-1,stdin);
227  while (fname[strlen(fname)-1] == '\n') {
228    fname[strlen(fname)-1] = '\0';
229  }
230  /*
231   * allocate buffer of given size
232   */
233  if (buf_size > 0) {
234    buf_ptr = malloc(buf_size);
235  }
236
237  if (buf_ptr != NULL) {
238    printf("\n Trying to open file \"%s\" for read\n",fname);
239    fd = open(fname,O_RDONLY);
240    if (fd < 0) {
241      printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno));
242    }
243  }
244
245  if (fd >= 0) {
246    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
247    do {
248      n = read(fd,buf_ptr,buf_size);
249      if (n > 0) {
250        write(1,buf_ptr,n);
251        flen += n;
252      }
253    } while (n > 0);
254
255    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
256
257    printf("\n ******** End of file reached, flen = %d\n",flen);
258    close(fd);
259
260    rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
261    printf("time elapsed for read:  %g seconds\n",
262           ((double)curr_tick-start_tick)/ticks_per_sec);
263  }
264  /*
265   * free buffer
266   */
267  if (buf_ptr != NULL) {
268    free(buf_ptr);
269  }
270  fileio_print_free_heap();
271}
272
273/*
274 * convert a size string (like 34K or 12M) to actual byte count
275 */
276boolean fileio_str2size(const char *str,uint32_t   *res_ptr)
277{
278  boolean failed = FALSE;
279  unsigned long size;
280  char suffix = ' ';
281
282  if (1 > sscanf(str,"%lu%c",&size,&suffix)) {
283    failed = TRUE;
284  }
285  else if (toupper(suffix) == 'K') {
286    size *= 1024;
287  }
288  else if (toupper(suffix) == 'M') {
289    size *= 1024UL*1024UL;
290  }
291  else if (isalpha(suffix)) {
292    failed = TRUE;
293  }
294
295  if (!failed) {
296    *res_ptr = size;
297  }
298  return failed;
299}
300
301void fileio_write_file(void)
302{
303  char fname[1024];
304  char tmp_str[32];
305  uint32_t   file_size = 0;
306  uint32_t   buf_size  = 0;
307  size_t curr_pos,bytes_to_copy;
308  int fd = -1;
309  ssize_t n;
310  rtems_interval start_tick,curr_tick,ticks_per_sec;
311  char *bufptr = NULL;
312  boolean failed = FALSE;
313  static const char write_test_string[] =
314    "The quick brown fox jumps over the lazy dog\n";
315  static const char write_block_string[] =
316    "\n----- end of write buffer ------\n";
317
318  printf(" =========================\n");
319  printf(" WRITE FILE ...           \n");
320  printf(" =========================\n");
321  fileio_print_free_heap();
322  /*
323   * get number of ticks per second
324   */
325  rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
326
327  /*
328   * get path to file to write
329   */
330  if (!failed) {
331    printf("Enter path/filename ==>");
332    fflush(stdout);
333    fgets(fname,sizeof(fname)-1,stdin);
334    while (fname[strlen(fname)-1] == '\n') {
335      fname[strlen(fname)-1] = '\0';
336    }
337    if (0 == strlen(fname)) {
338      printf("*** no filename entered, aborted\n");
339      failed = TRUE;
340    }
341  }
342  /*
343   * get total file size to write
344   */
345  if (!failed) {
346    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
347           "Enter filesize to write ==>");
348    fflush(stdout);
349    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
350    failed = fileio_str2size(tmp_str,&file_size);
351    if (failed) {
352      printf("*** illegal file size, aborted\n");
353    }
354  }
355  /*
356   * get block size to write
357   */
358  if (!failed) {
359    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
360           "Enter block size to use for write calls ==>");
361    fflush(stdout);
362    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
363    failed = fileio_str2size(tmp_str,&buf_size);
364    if (failed) {
365      printf("*** illegal block size, aborted\n");
366    }
367  }
368
369  /*
370   * allocate buffer
371   */
372  if (!failed) {
373    printf("... allocating %lu bytes of buffer for write data\n",
374           (unsigned long)buf_size);
375    bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */
376    if (bufptr == NULL) {
377      printf("*** malloc failed, aborted\n");
378      failed = TRUE;
379    }
380  }
381  /*
382   * fill buffer with test pattern
383   */
384  if (!failed) {
385    printf("... filling buffer with write data\n");
386    curr_pos = 0;
387    /*
388     * fill buffer with test string
389     */
390    while (curr_pos < buf_size) {
391      bytes_to_copy = MIN(buf_size-curr_pos,
392                          sizeof(write_test_string)-1);
393      memcpy(bufptr+curr_pos,write_test_string,bytes_to_copy);
394      curr_pos += bytes_to_copy;
395    }
396    /*
397     * put "end" mark at end of buffer
398     */
399    bytes_to_copy = sizeof(write_block_string)-1;
400    if (buf_size >= bytes_to_copy) {
401      memcpy(bufptr+buf_size-bytes_to_copy,
402             write_block_string,
403             bytes_to_copy);
404    }
405  }
406  /*
407   * create file
408   */
409  if (!failed) {
410    printf("... creating file \"%s\"\n",fname);
411    fd = open(fname,O_WRONLY | O_CREAT | O_TRUNC,S_IREAD|S_IWRITE);
412    if (fd < 0) {
413      printf("*** file create failed, errno = %d(%s)\n",errno,strerror(errno));
414      failed = TRUE;
415    }
416  }
417  /*
418   * write file
419   */
420  if (!failed) {
421    printf("... writing to file\n");
422    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
423    curr_pos = 0;
424    do {
425      bytes_to_copy = buf_size;
426      do {
427        n = write(fd,
428          bufptr + (buf_size-bytes_to_copy),
429                  MIN(bytes_to_copy,file_size-curr_pos));
430        if (n > 0) {
431          bytes_to_copy -= n;
432          curr_pos      += n;
433        }
434      } while ((bytes_to_copy > 0)  && (n > 0));
435    } while ((file_size > curr_pos) && (n > 0));
436    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
437    if (n < 0) {
438      failed = TRUE;
439      printf("*** file write failed, "
440             "%lu bytes written, "
441             "errno = %d(%s)\n",
442             (unsigned long)curr_pos,errno,strerror(errno));
443    }
444    else {
445      printf("time elapsed for write:  %g seconds\n",
446             ((double)curr_tick-start_tick)/ticks_per_sec);
447      printf("write data rate: %g KBytes/second\n",
448             (((double)file_size) / 1024.0 /
449              (((double)curr_tick-start_tick)/ticks_per_sec)));
450    }
451  }
452  if (fd >= 0) {
453    printf("... closing file\n");
454    close(fd);
455  }
456  if (bufptr != NULL) {
457    printf("... deallocating buffer\n");
458    free(bufptr);
459    bufptr = NULL;
460  }
461  printf("\n ******** End of file write\n");
462  fileio_print_free_heap();
463}
464
465void fileio_read_file(void)
466{
467  char fname[1024];
468  char tmp_str[32];
469  uint32_t   buf_size  = 0;
470  size_t curr_pos;
471  int fd = -1;
472  ssize_t n;
473  rtems_interval start_tick,curr_tick,ticks_per_sec;
474  char *bufptr = NULL;
475  boolean failed = FALSE;
476
477  printf(" =========================\n");
478  printf(" READ FILE ...            \n");
479  printf(" =========================\n");
480  fileio_print_free_heap();
481  /*
482   * get number of ticks per second
483   */
484  rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
485
486  /*
487   * get path to file to read
488   */
489  if (!failed) {
490    printf("Enter path/filename ==>");
491    fflush(stdout);
492    fgets(fname,sizeof(fname)-1,stdin);
493    while (fname[strlen(fname)-1] == '\n') {
494      fname[strlen(fname)-1] = '\0';
495    }
496    if (0 == strlen(fname)) {
497      printf("*** no filename entered, aborted\n");
498      failed = TRUE;
499    }
500  }
501  /*
502   * get block size to read
503   */
504  if (!failed) {
505    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
506           "Enter block size to use for read calls ==>");
507    fflush(stdout);
508    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
509    failed = fileio_str2size(tmp_str,&buf_size);
510    if (failed) {
511      printf("*** illegal block size, aborted\n");
512    }
513  }
514
515  /*
516   * allocate buffer
517   */
518  if (!failed) {
519    printf("... allocating %lu bytes of buffer for write data\n",
520           (unsigned long)buf_size);
521    bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */
522    if (bufptr == NULL) {
523      printf("*** malloc failed, aborted\n");
524      failed = TRUE;
525    }
526  }
527  /*
528   * open file
529   */
530  if (!failed) {
531    printf("... opening file \"%s\"\n",fname);
532    fd = open(fname,O_RDONLY);
533    if (fd < 0) {
534      printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno));
535      failed = TRUE;
536    }
537  }
538  /*
539   * read file
540   */
541  if (!failed) {
542    printf("... reading from file\n");
543    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
544    curr_pos = 0;
545    do {
546      n = read(fd,
547               bufptr,
548               buf_size);
549      if (n > 0) {
550        curr_pos      += n;
551      }
552    } while (n > 0);
553    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
554    if (n < 0) {
555      failed = TRUE;
556      printf("*** file read failed, "
557             "%lu bytes read, "
558             "errno = %d(%s)\n",
559             (unsigned long)curr_pos,errno,strerror(errno));
560    }
561    else {
562      printf("%lu bytes read\n",
563             (unsigned long)curr_pos);
564      printf("time elapsed for read:  %g seconds\n",
565             ((double)curr_tick-start_tick)/ticks_per_sec);
566      printf("read data rate: %g KBytes/second\n",
567             (((double)curr_pos) / 1024.0 /
568              (((double)curr_tick-start_tick)/ticks_per_sec)));
569    }
570  }
571  if (fd >= 0) {
572    printf("... closing file\n");
573    close(fd);
574  }
575  if (bufptr != NULL) {
576    printf("... deallocating buffer\n");
577    free(bufptr);
578    bufptr = NULL;
579  }
580  printf("\n ******** End of file read\n");
581  fileio_print_free_heap();
582
583}
584
585void fileio_menu (void)
586{
587  char inbuf[10];
588
589  /*
590   * Wait for characters from console terminal
591   */
592  for (;;) {
593    printf(" =========================\n");
594    printf(" RTEMS FILE I/O Test Menu \n");
595    printf(" =========================\n");
596    printf("   p -> part_table_initialize\n");
597    printf("   f -> mount all disks in fs_table\n");
598    printf("   l -> list  file\n");
599    printf("   r -> read  file\n");
600    printf("   w -> write file\n");
601#ifdef USE_SHELL
602    printf("   s -> start shell\n");
603#endif
604    printf("   Enter your selection ==>");
605    fflush(stdout);
606
607    inbuf[0] = '\0';
608    fgets(inbuf,sizeof(inbuf),stdin);
609    switch (inbuf[0]) {
610    case 'l':
611      fileio_list_file ();
612      break;
613    case 'r':
614      fileio_read_file ();
615      break;
616    case 'w':
617      fileio_write_file ();
618      break;
619    case 'p':
620      fileio_part_table_initialize ();
621      break;
622    case 'f':
623      fileio_fsmount ();
624      break;
625#ifdef USE_SHELL
626    case 's':
627      fileio_start_shell ();
628      break;
629#endif
630    default:
631      printf("Selection `%c` not implemented\n",inbuf[0]);
632      break;
633    }
634
635  }
636  exit (0);
637}
638
639int menu_tid;
640
641/*
642 * RTEMS Startup Task
643 */
644rtems_task
645Init (rtems_task_argument ignored)
646{
647  puts( "\n\n*** FILE I/O SAMPLE AND TEST ***" );
648
649  fileio_menu();
650}
651
652/*
653 *  RTEMS Shell Configuration -- Add a command and an alias for it
654 */
655
656int main_usercmd(int argc, char **argv)
657{
658  int i;
659  printf( "UserCommand: argc=%d\n", argc );
660  for (i=0 ; i<argc ; i++ )
661    printf( "argv[%d]= %s\n", i, argv[i] );
662  return 0;
663}
664
665rtems_shell_cmd_t Shell_USERCMD_Command = {
666  "usercmd",                                       /* name */
667  "usercmd n1 [n2 [n3...]]     # echo arguments",  /* usage */
668  "user",                                          /* topic */
669  main_usercmd,                                    /* command */
670  NULL,                                            /* alias */
671  NULL                                             /* next */
672};
673
674rtems_shell_alias_t Shell_USERECHO_Alias = {
675  "usercmd",                 /* command */
676  "userecho"                 /* alias */
677};
678 
679
680#define CONFIGURE_SHELL_USER_COMMANDS &Shell_USERCMD_Command
681#define CONFIGURE_SHELL_USER_ALIASES &Shell_USERECHO_Alias
682#define CONFIGURE_SHELL_COMMANDS_INIT
683#define CONFIGURE_SHELL_COMMANDS_ALL
684#define CONFIGURE_SHELL_MOUNT_MSDOS
685
686#include <rtems/shellconfig.h>
687
Note: See TracBrowser for help on using the repository browser.