コード例 #1
0
ファイル: main_df.c プロジェクト: AoLaD/rtems
static int rtems_shell_main_df(int argc, char **argv)
{
  int c;
  struct getopt_data optdata;
  struct df_context context;
  char buf[32];

  memset(&optdata, 0, sizeof(optdata));
  context.block_size = 1024;

  while ((c = getopt_r(argc, (char**) argv, ":hB:", &optdata)) != -1)
  {
    switch (c)
    {
    case 'h':
      context.block_size = 0;
      break;
    case 'B':
      context.block_size = rtems_shell_df_parse_size(optdata.optarg);
      break;
    default:
      return -1;
    }
  }

  if (context.block_size == 0)
    printf(
        "Filesystem     Size             Used   Available       Use%%     Mounted on\n");
  else
    printf(
        "Filesystem     %s-blocks        Used   Available       Use%%     Mounted on\n",
        rtems_shell_df_humanize_size(context.block_size, buf, sizeof(buf)));

  rtems_filesystem_mount_iterate(rtems_shell_df_print_entry, &context);

  return 0;
}
コード例 #2
0
int rtems_shell_main_joel(
  int    argc,
  char **argv
)
{
  unsigned long        tmp;
  int                  option;
  int                  sc;
  int                  verbose = 0;
  char                *taskName = "JOEL";
  uint32_t             stackSize = RTEMS_MINIMUM_STACK_SIZE * 10;
  rtems_task_priority  taskPriority = 20;
  char                *outputFile = "stdout";
  rtems_status_code    result;
  char                 scriptFile[PATH_MAX];
  struct getopt_data   getopt_reent;

  memset(&getopt_reent, 0, sizeof(getopt_data));
  while ( (option = getopt_r( argc, argv, "o:p:s:t:v", &getopt_reent)) != -1 ) {
    switch ((char)option) {
      case 'o':
        outputFile = getopt_reent.optarg;
        break;
      case 'p': {
        const char *s = getopt_reent.optarg;

	if ( rtems_string_to_unsigned_long( s, &tmp, NULL, 0) ) {
	  printf( "Task Priority argument (%s) is not a number\n", s );
	  return -1;
	}
	taskPriority = (rtems_task_priority) tmp;
        break;
      }
      case 's': {
        const char *s = getopt_reent.optarg;

	if ( rtems_string_to_unsigned_long( s, &tmp, NULL, 0) ) {
	  printf( "Stack size argument (%s) is not a number\n", s );
	  return -1;
	}
        stackSize = (uint32_t) tmp;
        break;
      }
      case 't':
        taskName = getopt_reent.optarg;
        break;
      case 'v':
        verbose = 1;
        break;
      case '?':
      default:
        rtems_shell_joel_usage();
        return -1;
    }
  }

  if ( verbose ) {
    fprintf( stderr,
      "outputFile: %s\n"
      "taskPriority: %" PRId32 "\n"
      "stackSize: %" PRId32 "\n"
      "taskName: %s\n",
      outputFile,
      taskPriority,
      stackSize,
      taskName
   );
  }

  /*
   *  Verify there is a script name past the end of the arguments.
   *  Preincrement to skip program name.
   */
  if ( getopt_reent.optind >= argc ) {
    fprintf( stderr, "Shell: No script to execute\n" );
    return -1;
  }

  /*
   *  Find script on the path.
   *
   *  NOTE: It is terrible that this is done twice but it
   *        seems to be the most expedient thing.
   */
  sc = findOnPATH( argv[getopt_reent.optind], scriptFile );
  if ( sc ) {
    fprintf( stderr, "%s: command not found\n", argv[0] );
    return -1;
  }

  /* fprintf( stderr, "SCRIPT: -%s-\n", scriptFile ); */

  /*
   *  I assume that argv[optind...] will have the arguments to
   *  the shell script.  But that remains to be implemented.
   */

  /*
   * Run the script
   */
  result = rtems_shell_script(
    taskName,        /* the name of the task */
    stackSize,       /* stack size */
    taskPriority,    /* task priority */
    scriptFile,      /* the script file */
    outputFile,      /* where to redirect the script */
    0,               /* run once and exit */
    1,               /* we will wait */
    verbose          /* do we echo */
  );
  if (result)
    return -1;
  return 0;
}
コード例 #3
0
ファイル: ifconfig.c プロジェクト: BlueFireworks/rtems-libbsd
int
main(int argc, char *argv[])
#endif
{
	int c, all, namesonly, downonly, uponly;
	const struct afswtch *afp = NULL;
	int ifindex;
	struct ifaddrs *ifap, *ifa;
	struct ifreq paifr;
	const struct sockaddr_dl *sdl;
	char options[1024], *cp;
	const char *ifname;
#ifdef __rtems__
	struct ifconfig_option *p;
#else
	struct option *p;
#endif
	size_t iflen;
#ifdef __rtems__
	struct getopt_data getopt_reent;
#define optind getopt_reent.optind
#define optarg getopt_reent.optarg
#define opterr getopt_reent.opterr
#define optopt getopt_reent.optopt
#endif

	all = downonly = uponly = namesonly = noload = verbose = 0;

	/* Parse leading line options */
	strlcpy(options, "adklmnuv", sizeof(options));
	for (p = opts; p != NULL; p = p->next)
		strlcat(options, p->opt, sizeof(options));
#ifdef __rtems__
	memset(&getopt_reent, 0, sizeof(getopt_data));
	while ((c = getopt_r(argc, argv, options, &getopt_reent)) != -1) {
#else
	while ((c = getopt(argc, argv, options)) != -1) {
#endif
		switch (c) {
		case 'a':	/* scan all interfaces */
			all++;
			break;
		case 'd':	/* restrict scan to "down" interfaces */
			downonly++;
			break;
		case 'k':
			printkeys++;
			break;
		case 'l':	/* scan interface names only */
			namesonly++;
			break;
		case 'm':	/* show media choices in status */
			supmedia = 1;
			break;
		case 'n':	/* suppress module loading */
			noload++;
			break;
		case 'u':	/* restrict scan to "up" interfaces */
			uponly++;
			break;
		case 'v':
			verbose++;
			break;
		default:
			for (p = opts; p != NULL; p = p->next)
				if (p->opt[0] == c) {
					p->cb(optarg);
					break;
				}
			if (p == NULL)
				usage();
			break;
		}
	}
	argc -= optind;
	argv += optind;

	/* -l cannot be used with -a or -m */
	if (namesonly && (all || supmedia))
		usage();

	/* nonsense.. */
	if (uponly && downonly)
		usage();

	/* no arguments is equivalent to '-a' */
	if (!namesonly && argc < 1)
		all = 1;

	/* -a and -l allow an address family arg to limit the output */
	if (all || namesonly) {
		if (argc > 1)
			usage();

		ifname = NULL;
		ifindex = 0;
		if (argc == 1) {
			afp = af_getbyname(*argv);
			if (afp == NULL)
				usage();
			if (afp->af_name != NULL)
				argc--, argv++;
			/* leave with afp non-zero */
		}
	} else {
		/* not listing, need an argument */
		if (argc < 1)
			usage();

		ifname = *argv;
		argc--, argv++;

		/* check and maybe load support for this interface */
		ifmaybeload(ifname);

		ifindex = if_nametoindex(ifname);
		if (ifindex == 0) {
			/*
			 * NOTE:  We must special-case the `create' command
			 * right here as we would otherwise fail when trying
			 * to find the interface.
			 */
			if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
			    strcmp(argv[0], "plumb") == 0)) {
				iflen = strlcpy(name, ifname, sizeof(name));
				if (iflen >= sizeof(name))
					errx(1, "%s: cloning name too long",
					    ifname);
				ifconfig(argc, argv, 1, NULL);
				exit(0);
			}
			/*
			 * NOTE:  We have to special-case the `-vnet' command
			 * right here as we would otherwise fail when trying
			 * to find the interface as it lives in another vnet.
			 */
			if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
				iflen = strlcpy(name, ifname, sizeof(name));
				if (iflen >= sizeof(name))
					errx(1, "%s: interface name too long",
					    ifname);
				ifconfig(argc, argv, 0, NULL);
				exit(0);
			}
			errx(1, "interface %s does not exist", ifname);
		}
	}

	/* Check for address family */
	if (argc > 0) {
		afp = af_getbyname(*argv);
		if (afp != NULL)
			argc--, argv++;
	}

	if (getifaddrs(&ifap) != 0)
		err(EXIT_FAILURE, "getifaddrs");
	cp = NULL;
	ifindex = 0;
	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
		memset(&paifr, 0, sizeof(paifr));
		strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
		if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
			memcpy(&paifr.ifr_addr, ifa->ifa_addr,
			    ifa->ifa_addr->sa_len);
		}

		if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
			continue;
		if (ifa->ifa_addr->sa_family == AF_LINK)
			sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
		else
			sdl = NULL;
		if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0)
			continue;
		iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
		if (iflen >= sizeof(name)) {
			warnx("%s: interface name too long, skipping",
			    ifa->ifa_name);
			continue;
		}
		cp = ifa->ifa_name;

		if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
			continue;
		if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
			continue;
		ifindex++;
		/*
		 * Are we just listing the interfaces?
		 */
		if (namesonly) {
			if (ifindex > 1)
				printf(" ");
			fputs(name, stdout);
			continue;
		}

		if (argc > 0)
			ifconfig(argc, argv, 0, afp);
		else
			status(afp, sdl, ifa);
	}
	if (namesonly)
		printf("\n");
	freeifaddrs(ifap);

	exit(0);
}

static struct afswtch *afs = NULL;

void
af_register(struct afswtch *p)
{
	p->af_next = afs;
	afs = p;
}
コード例 #4
0
void
newsyntax(rtems_shell_hexdump_globals* globals, int argc, char ***argvp)
{
	int ch;
	char *p, **argv;

  struct getopt_data getopt_reent;
  memset(&getopt_reent, 0, sizeof(getopt_data));

	argv = *argvp;
	if ((p = rindex(argv[0], 'h')) != NULL &&
	    strcmp(p, "hd") == 0) {
		/* "Canonical" format, implies -C. */
		add(globals, "\"%08.8_Ax\n\"");
		add(globals, "\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
		add(globals, "\"  |\" 16/1 \"%_p\" \"|\\n\"");
	}
	while ((ch = getopt_r(argc, argv, "bcCde:f:n:os:vx", &getopt_reent)) != -1)
		switch (ch) {
		case 'b':
			add(globals, "\"%07.7_Ax\n\"");
			add(globals, "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
			break;
		case 'c':
			add(globals, "\"%07.7_Ax\n\"");
			add(globals, "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
			break;
		case 'C':
			add(globals, "\"%08.8_Ax\n\"");
			add(globals, "\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
			add(globals, "\"  |\" 16/1 \"%_p\" \"|\\n\"");
			break;
		case 'd':
			add(globals, "\"%07.7_Ax\n\"");
			add(globals, "\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
			break;
		case 'e':
			add(globals, getopt_reent.optarg);
			break;
		case 'f':
			addfile(globals, getopt_reent.optarg);
			break;
		case 'n':
			if ((length = atoi(getopt_reent.optarg)) < 0)
				errx(exit_jump, 1, "%s: bad length value", getopt_reent.optarg);
			break;
		case 'o':
			add(globals, "\"%07.7_Ax\n\"");
			add(globals, "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
			break;
		case 's':
			if ((skip = strtoll(getopt_reent.optarg, &p, 0)) < 0)
				errx(exit_jump, 1, "%s: bad skip value", getopt_reent.optarg);
			switch(*p) {
			case 'b':
				skip *= 512;
				break;
			case 'k':
				skip *= 1024;
				break;
			case 'm':
				skip *= 1048576;
				break;
			}
			break;
		case 'v':
			vflag = ALL;
			break;
		case 'x':
			add(globals, "\"%07.7_Ax\n\"");
			add(globals, "\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
			break;
		case '?':
			usage(globals);
		}

	if (!fshead) {
		add(globals, "\"%07.7_Ax\n\"");
		add(globals, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
	}

	*argvp += getopt_reent.optind;
}
コード例 #5
0
ファイル: main_ln.c プロジェクト: 0871087123/rtems
static int
main_ln(rtems_shell_ln_globals* globals, int argc, char *argv[])
{
	struct stat sb;
	int ch, exitval;
	char *sourcedir;

	struct getopt_data getopt_reent;
	memset(&getopt_reent, 0, sizeof(getopt_data));

#if RTEMS_REMOVED
	setprogname(argv[0]);
	(void)setlocale(LC_ALL, "");
#endif

	while ((ch = getopt_r(argc, argv, "fhinsv", &getopt_reent)) != -1)
		switch (ch) {
		case 'f':
			fflag = 1;
			iflag = 0;
			break;
		case 'h':
		case 'n':
			hflag = 1;
			break;
		case 'i':
			iflag = 1;
			fflag = 0;
			break;
		case 's':
			sflag = 1;
			break;
		case 'v':
			vflag = 1;
			break;
		case '?':
		default:
			usage(globals);
			/* NOTREACHED */
		}

	argv += getopt_reent.optind;
	argc -= getopt_reent.optind;

	if (sflag) {
		linkf  = symlink;
		linkch = '-';
	} else {
		linkf  = link;
		linkch = '=';
	}

	switch(argc) {
	case 0:
		usage(globals);
		/* NOTREACHED */
	case 1:				/* ln target */
		exit(linkit(globals, argv[0], ".", 1));
		/* NOTREACHED */
	case 2:				/* ln target source */
		exit(linkit(globals, argv[0], argv[1], 0));
		/* NOTREACHED */
	}

					/* ln target1 target2 directory */
	sourcedir = argv[argc - 1];
	if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
		/* we were asked not to follow symlinks, but found one at
		   the target--simulate "not a directory" error */
		errno = ENOTDIR;
		err(exit_jump, EXIT_FAILURE, "%s", sourcedir);
		/* NOTREACHED */
	}
	if (stat(sourcedir, &sb)) {
		err(exit_jump, EXIT_FAILURE, "%s", sourcedir);
		/* NOTREACHED */
	}
	if (!S_ISDIR(sb.st_mode)) {
		usage(globals);
		/* NOTREACHED */
	}
	for (exitval = 0; *argv != sourcedir; ++argv)
		exitval |= linkit(globals, *argv, sourcedir, 1);
	exit(exitval);
	/* NOTREACHED */
  return 0;
}
コード例 #6
0
int rtems_shell_main_netstats(                       /* command */
  int   argc,
  char *argv[]
)
{
  int   option;
  int   doAll = 0;
  int   doInetRoutes = 0;
  int   doMBUFStats = 0;
  int   doIFStats = 0;
  int   doIPStats = 0;
  int   doICMPStats = 0;
  int   doUDPStats = 0;
  int   doTCPStats = 0;
  int   verbose = 0;
  struct getopt_data getopt_reent;

  memset(&getopt_reent, 0, sizeof(getopt_data));
  while ( (option = getopt_r( argc, argv, "Aimfpcutv", &getopt_reent)) != -1 ) {

    switch ((char)option) {
      case 'A': doAll = 1;        break;
      case 'i': doInetRoutes = 1; break;
      case 'm': doMBUFStats = 1;  break;
      case 'f': doIFStats = 1;    break;
      case 'p': doIPStats = 1;    break;
      case 'c': doICMPStats = 1;  break;
      case 'u': doUDPStats = 1;   break;
      case 't': doTCPStats = 1;   break;
      case 'v': verbose = 1;      break;
      case '?':
      default:
        netstats_usage();
        return -1;
    }
  }

  if ( verbose ) {
    printf(
      "doAll=%d\n"
      "doInetRoutes=%d\n"
      "doMBUFStats=%d\n"
      "doIFStats=%d\n"
      "doIPStats=%d\n"
      "doICMPStats=%d\n"
      "doUDPStats=%d\n"
      "doTCPStats=%d\n",
      doAll,
      doInetRoutes,
      doMBUFStats,
      doIFStats,
      doIPStats,
      doICMPStats,
      doUDPStats,
      doTCPStats
    );
  }

  if ( doInetRoutes == 1 || doAll == 1 ) {
    rtems_bsdnet_show_inet_routes();
  }

  if ( doMBUFStats == 1 || doAll == 1 ) {
    rtems_bsdnet_show_mbuf_stats();
  }

  if ( doIFStats == 1 || doAll == 1 ) {
    rtems_bsdnet_show_if_stats();
  }

  if ( doIPStats == 1 || doAll == 1 ) {
    rtems_bsdnet_show_ip_stats();
  }

  if ( doICMPStats == 1 || doAll == 1 ) {
    rtems_bsdnet_show_icmp_stats();
  }

  if ( doUDPStats == 1 || doAll == 1 ) {
    rtems_bsdnet_show_udp_stats();
  }

  if ( doTCPStats == 1 || doAll == 1 ) {
    rtems_bsdnet_show_tcp_stats();
  }

  return 0;
}
コード例 #7
0
ファイル: main_cp.c プロジェクト: 0871087123/rtems
int
main_cp(rtems_shell_cp_globals* cp_globals, int argc, char *argv[])
{
	struct stat to_stat, tmp_stat;
	enum op type;
	int Hflag, Lflag, Pflag, ch, fts_options, r, have_trailing_slash;
	char *target;
  struct getopt_data getopt_reent;

	Hflag = Lflag = Pflag = 0;
  memset(&getopt_reent, 0, sizeof(getopt_data));

	while ((ch = getopt_r(argc, argv, "HLPRafilnprv", &getopt_reent)) != -1)
		switch (ch) {
		case 'H':
			Hflag = 1;
			Lflag = Pflag = 0;
			break;
		case 'L':
			Lflag = 1;
			Hflag = Pflag = 0;
			break;
		case 'P':
			Pflag = 1;
			Hflag = Lflag = 0;
			break;
		case 'R':
			Rflag = 1;
			break;
		case 'a':
			Pflag = 1;
			pflag = 1;
			Rflag = 1;
			Hflag = Lflag = 0;
			break;
		case 'f':
			fflag = 1;
			iflag = nflag = 0;
			break;
		case 'i':
			iflag = 1;
			fflag = nflag = 0;
			break;
		case 'l':
			lflag = 1;
			break;
		case 'n':
			nflag = 1;
			fflag = iflag = 0;
			break;
		case 'p':
			pflag = 1;
			break;
		case 'r':
			rflag = Lflag = 1;
			Hflag = Pflag = 0;
			break;
		case 'v':
			vflag = 1;
			break;
		default:
			usage(cp_globals);
			break;
		}
	argc -= getopt_reent.optind;
	argv += getopt_reent.optind;

	if (argc < 2)
		usage(cp_globals);

	fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
	if (Rflag && rflag)
		errx(exit_jump, 1, "the -R and -r options may not be specified together");
	if (rflag)
		Rflag = 1;
	if (Rflag) {
		if (Hflag)
			fts_options |= FTS_COMFOLLOW;
		if (Lflag) {
			fts_options &= ~FTS_PHYSICAL;
			fts_options |= FTS_LOGICAL;
		}
	} else {
		fts_options &= ~FTS_PHYSICAL;
		fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
	}
#if 0
	(void)signal(SIGINFO, siginfo);
#endif

	/* Save the target base in "to". */
	target = argv[--argc];
	if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
		errx(exit_jump, 1, "%s: name too long", target);
	to.p_end = to.p_path + strlen(to.p_path);
        if (to.p_path == to.p_end) {
		*to.p_end++ = '.';
		*to.p_end = 0;
	}
	have_trailing_slash = (to.p_end[-1] == '/');
	if (have_trailing_slash)
		STRIP_TRAILING_SLASH(to);
	to.target_end = to.p_end;

	/* Set end of argument list for fts(3). */
	argv[argc] = NULL;

	/*
	 * Cp has two distinct cases:
	 *
	 * cp [-R] source target
	 * cp [-R] source1 ... sourceN directory
	 *
	 * In both cases, source can be either a file or a directory.
	 *
	 * In (1), the target becomes a copy of the source. That is, if the
	 * source is a file, the target will be a file, and likewise for
	 * directories.
	 *
	 * In (2), the real target is not directory, but "directory/source".
	 */
	r = stat(to.p_path, &to_stat);
	if (r == -1 && errno != ENOENT)
		err(exit_jump, 1, "%s", to.p_path);
	if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
		/*
		 * Case (1).  Target is not a directory.
		 */
		if (argc > 1)
			errx(exit_jump, 1, "%s is not a directory", to.p_path);

		/*
		 * Need to detect the case:
		 *	cp -R dir foo
		 * Where dir is a directory and foo does not exist, where
		 * we want pathname concatenations turned on but not for
		 * the initial mkdir().
		 */
		if (r == -1) {
			if (Rflag && (Lflag || Hflag))
				stat(*argv, &tmp_stat);
			else
				lstat(*argv, &tmp_stat);

			if (S_ISDIR(tmp_stat.st_mode) && Rflag)
				type = DIR_TO_DNE;
			else
				type = FILE_TO_FILE;
		} else
			type = FILE_TO_FILE;

		if (have_trailing_slash && type == FILE_TO_FILE) {
			if (r == -1)
				errx(exit_jump, 1, "directory %s does not exist",
				     to.p_path);
			else
				errx(exit_jump, 1, "%s is not a directory", to.p_path);
		}
	} else
		/*
		 * Case (2).  Target is a directory.
		 */
		type = FILE_TO_DIR;

  return copy(cp_globals, argv, type, fts_options);
}
コード例 #8
0
int main_pcmmio_benchmark(int argc, char **argv)
{
  int                 maximum;
  int                 sc;
  char                ch;
  bool                verbose;
  struct getopt_data  getopt_reent;
  const  char        *s;
  int                 interrupts;
  uint64_t            timestamp;
  uint64_t            now;
  uint32_t            min_cycles;
  uint32_t            max_cycles;
  uint64_t            total_cycles;
  uint64_t            cycles;

  /*
   * Parse arguments here
   */
  maximum = 1;
  verbose = false;

  memset(&getopt_reent, 0, sizeof(getopt_data));
  while ((ch = getopt_r(argc, argv, "i:v", &getopt_reent)) != -1) {
    switch (ch) {
      case 'i': /* maximum interrupts */
        s = getopt_reent.optarg;
        if ( rtems_string_to_int( s, &maximum, NULL, 0 ) ) {
          printf( "Maximum interrupts (%s) is not a number\n", s );
          PRINT_USAGE();
          return -1;
        }
	if ( maximum <= 0 ) {
	  printf( "Maximum interrupts (%d) is invalid\n", maximum );
	  PRINT_USAGE();
          return -1;
	}
        break;
      case 'v': /* verbose */
        verbose = true;
        break;
      default:
        printf( pcmmio_benchmark_usage, argv[0] );
        return -1;
    }
  }

  printf( "Benchmarking for DIN IRQ for %d interrupts\n", maximum );

  /*
   *  Now catch interrupts in the loop
   */
  interrupts   = 0;
  min_cycles   = 0xffffffff;
  max_cycles   = 0;
  total_cycles = 0;

  flush_buffered_ints();
  while (1) {
    sc = 0;
   
    sc = wait_dio_int_with_timestamp(0, &timestamp);

    if ( sc == -1 )
      continue;

    now = rdtsc();

    cycles = now - timestamp;
    total_cycles += cycles;

    if ( cycles < min_cycles ) min_cycles = cycles;
    if ( cycles > max_cycles ) max_cycles = cycles;

    interrupts++;

    if (interrupts >= maximum )
      break;
  }

  printf(
    "Number of Interrupts:     %d\n"
    "Total Cycles:             %lld\n"
    "min/max/avg cycles:       %ld/%ld/%lld\n"
    "min/max/avg microseconds: %lld/%lld/%lld\n",
    maximum,
    total_cycles,
    min_cycles, max_cycles, total_cycles / maximum,
    to_usecs( min_cycles ),
    to_usecs( max_cycles ),
    to_usecs( total_cycles / maximum )
  );
  return 0;
}
コード例 #9
0
ファイル: ttcp.c プロジェクト: ariavie/bcm
void
ttcp(char *line)
{
	int ac;
	char *av[32];
	char *buf;
        char *host;
	int c;
	int fd;
	int newfd;
	int cnt;
	int nbytes;
	int die;
	int err;
        int udp;
        int trans;
        int num;
        int len;
        int sinkmode;
        short port;
        struct sockaddr_in sinhim, sinme;
        struct sockaddr_in frominet;
        int fromlen;
        GOPT optstruct;
        GOPT *g = &optstruct;

printf("ttcp!");
#ifdef DEBUG_SEM
        ttcpDebugSem = semBCreate (0, SEM_EMPTY);
        semTake (ttcpDebugSem, WAIT_FOREVER);
#endif
	/* init globals with default values */
	udp = 0;
	trans = 1;
	num = 1024;
	len = 1024;
	sinkmode = 1;
	port = 2000;
	host = NULL;
	g->place = EMSG;
	g->opterr = 1;
	g->optind = 0; 

	/* chop the command line into whitespace-delimited strings */
	for (ac = 0; ac < 32; ) {
		av[ac] = line;
		while ((*line != ' ') && (*line != '\t') && (*line != '\0'))
			line++;
		ac++;
		if (*line == '\0')
			break;
		*line++ = '\0';
		while ((*line == ' ') || (*line == '\t'))
			line++;
		if (*line == '\0')
			break;
	}

	while ((c = getopt_r (g, ac, av, "rrtsun:l:p:")) != -1) {
		switch (c) {
		case 'l':
			len = atoi(g->optarg);
			break;

		case 'n':
			num = atoi(g->optarg);
			break;

		case 'p':
			port = atoi(g->optarg);
			break;

		case 'r':
			trans = 0;
			break;

		case 's':	/* nop */
			break;

		case 't':
			trans = 1;
			break;

		case 'u':
			udp = 1;
			break;
		}
	}

	bzero((char*)&sinme, sizeof (sinme));
	bzero((char*)&sinhim, sizeof (sinhim));

	sinme.sin_family = AF_INET;

	if (trans) {
		host = av[g->optind];

		if ((*host < '0') || (*host > '9')) {
			printf("only dot notation internet addresses currently supported\n");
			return;
		}
		sinhim.sin_family = AF_INET;
		sinhim.sin_addr.s_addr = inet_addr(host);
		sinhim.sin_port = htons(port);
printf("inet_addr, port = 0x%x, %d\n", inet_addr(host), port);
		sinme.sin_port = INADDR_ANY;
	}
	else
		sinme.sin_port = htons(port);

	if ((fd = socket(AF_INET, udp? SOCK_DGRAM: SOCK_STREAM, 0)) == ERROR) {
		syserr("socket");
		return;
	}

	if (!trans) {
		int val;

		val = 1;
		if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*) &val, sizeof (val)) == ERROR) {
			syserr("setsockopt: REUSEADDR");
			goto done;
		}
	}

	if (trans) {
printf(" connect! \n");
		if (connect(fd, (struct sockaddr*) &sinhim, sizeof (sinhim)) == ERROR) {
			syserr("connect failed");
			goto done;
		}
		if (!udp)
			printf("connected\n");
	}
	else {	/* rcvr */


		if (bind(fd, (struct sockaddr*) &sinme, sizeof (sinme)) == ERROR) {
			syserr("bind");
			goto done;
		}

		if (!udp && (listen(fd, 2) == ERROR)) {
			syserr("listen");
			goto done;
		}

		fromlen = sizeof (frominet);

		if (!udp && (newfd = accept(fd, (struct sockaddr*) &frominet, &fromlen)) == ERROR) {
			syserr("accept");
			goto done;
		}

		if (!udp)
			printf("ttcp-r: accept from %s\n", inet_ntoa(frominet.sin_addr));

		if (!udp) {
			close(fd);
			fd = newfd;
		}
	}

        buf = malloc (len);
        if (buf == NULL)
            {
            printf ("malloc failed, len = %d\n", len);
            return;
            }

	pattern(buf, len);

	nbytes = 0;

	if (trans) {

		if (udp)
			write(fd, buf, 4);	/* rcvr start */
		
		die = 0;
		while (num > 0) {
			cnt = write(fd, buf, len);
			if (cnt == ERROR) {
				err = errnoGet();
#if 0  /* Jimmy test this */
				if ((die < 100) && ((err == EMSGSIZE) || (err == ENOBUFS))) {
					die++;
					taskDelay(10);
					continue;
				}
#endif
				syserr("write");
				goto done;
			}
			else if (cnt != len) {
				syserr("short write");
				goto done;
			}
			nbytes += len;
			num--;
			die = 0;
		}

		if (udp)
			write(fd, buf, 4);	/* rcvr stop */
	}
	else {
		if (udp) {
			die = 0;
			while ((cnt = read(fd, buf, len)) > 0) {
				if (cnt == ERROR) {
					syserr("read");
					goto done;
				}
				if (cnt <= 4) {
					if (die)
						goto done;
					die = 1;
				}
				nbytes += cnt;
			}
		}
		else {
			while ((cnt = read(fd, buf, len)) > 0)
				nbytes += cnt;
			if (cnt == ERROR) {
				syserr("read");
				goto done;
			}
		}
	}

done:
	if (trans)
		printf("wrote %d bytes\n", nbytes);
	else
		printf("read %d bytes\n", nbytes);

	close(fd);
	return;
}
コード例 #10
0
ファイル: rtems-debugger-cmd.c プロジェクト: gedare/rtems
static int rtems_shell_main_debugger(int argc, char *argv[])
{
  if (argc == 1) {
    printf("RTEMS debugger is %srunning\n", rtems_debugger_running() ? "" : "not ");
    return 0;
  }

  if (strcasecmp(argv[1], "start") == 0) {
    rtems_printer       printer;
    const char*         remote = "tcp";
    const char*         device = "1122";
    int                 timeout = RTEMS_DEBUGGER_TIMEOUT;
    rtems_task_priority priority = 1;
    bool                verbose = false;
    struct getopt_data  data;
    char*               end;
    int                 r;

    if (rtems_debugger_running()) {
      printf("error: debugger already running.\n");
      return 1;
    }

    memset(&data, 0, sizeof(data));

    rtems_print_printer_fprintf(&printer, stdout);

    argv += 1;
    argc -= 1;

    while (true) {
      int c;

      c = getopt_r(argc, argv, "vR:d:t:P:l:", &data);
      if (c == -1)
        break;

      switch (c) {
      case 'v':
        verbose = true;
        break;
      case 'R':
        remote = data.optarg;
        break;
      case 'd':
        device = data.optarg;
        break;
      case 't':
        timeout = strtoul(data.optarg, &end, 10);
        if (timeout == 0 || *end != '\0') {
          printf("error: invalid timeout: %s\n", data.optarg);
          return 1;
        }
        break;
      case 'P':
        priority = strtoul(data.optarg, &end, 10);
        if (priority == 0 || *end != '\0') {
          printf("error: invalid priority: %s\n", data.optarg);
          return 1;
        }
        break;
      case 'l':
        if (strcasecmp(data.optarg, "stdout") == 0)
          rtems_print_printer_fprintf(&printer, stdout);
        else if (strcasecmp(data.optarg, "stderr") == 0)
          rtems_print_printer_fprintf(&printer, stderr);
        else if (strcasecmp(data.optarg, "kernel") == 0)
          rtems_print_printer_printk(&printer);
        else {
          printf("error: unknown printer (stdout, stderr, kernel): %s\n", data.optarg);
          return 1;
        }
        break;
      default:
      case '?':
        if (data.optarg != NULL)
          printf("error: unknown option: %s\n", data.optarg);
        else
          printf("error: invalid start command\n");
        return 1;
      }
    }

    printf("RTEMS Debugger start: remote=%s device=%s priority=%" PRIu32 "\n",
           remote, device, priority);

    r = rtems_debugger_start(remote, device, timeout, priority, &printer);
    if (r < 0) {
      printf("debugger start failed\n");
      return 1;
    }

    rtems_debugger_set_verbose(verbose);
  }
  else if (strcasecmp(argv[1], "stop") == 0) {
    int r;

    if (!rtems_debugger_running()) {
      printf("error: debugger not running.\n");
      return 1;
    }

    r = rtems_debugger_stop();
    if (r < 0) {
      printf("debugger stop failed\n");
      return 1;
    }
  }
  else if (strcasecmp(argv[1], "remote-debug") == 0) {
    int r;

    if (!rtems_debugger_running()) {
      printf("error: debugger not running.\n");
      return 1;
    }

    if (argc == 3 && strcasecmp(argv[2], "on") == 0) {
      r = rtems_debugger_remote_debug(true);
      if (r < 0) {
        printf("debugger remote-debug on failed\n");
        return 1;
      }
    }
    else if (argc == 3 && strcasecmp(argv[2], "off") == 0) {
      r = rtems_debugger_remote_debug(false);
      if (r < 0) {
        printf("debugger remote-debug off failed\n");
        return 1;
      }
    }
    else {
      printf("debugger remote-debug: not on or off\n");
      return 1;
    }
  }
  else if (strcasecmp(argv[1], "verbose") == 0) {
    if (!rtems_debugger_running()) {
      printf("error: debugger not running.\n");
      return 1;
    }

    if (argc == 3 && strcasecmp(argv[2], "on") == 0) {
      rtems_debugger_set_verbose(true);
    }
    else if (argc == 3 && strcasecmp(argv[2], "off") == 0) {
      rtems_debugger_set_verbose(false);
    }
    else {
      printf("debugger verbose: not on or off\n");
      return 1;
    }
  }
  else if (strcasecmp(argv[1], "help") == 0) {
    printf("debugger [start/stop/help] ...\n" \
           "  start -v -R remote -d device -t secs -P priority -l [stdout,stderr,kernel]\n" \
           "  stop\n" \
           "  remote-debug <on/off>\n" \
           "  verbose <on/off>\n" \
           "  help\n");
  }
  else {
    printf("error: unknown command: %s\n", argv[1]);
    return 1;
  }

  return 0;
}
コード例 #11
0
ファイル: main_mknod.c プロジェクト: 0871087123/rtems
int
main_mknod(rtems_shell_mknod_globals* globals, int argc, char **argv)
{
	char	*name, *p;
	mode_t	 mode;
	portdev_t	 dev;
	pack_t	*pack;
	u_long	 numbers[MAXARGS];
	int	 n, ch, fifo, hasformat;
	int	 r_flag = 0;		/* force: delete existing entry */
#ifdef KERN_DRIVERS
	int	 l_flag = 0;		/* list device names and numbers */
	int	 major;
#endif
#if RTEMS_REMOVED
	void	*modes = 0;
#endif
	uid_t	 uid = -1;
	gid_t	 gid = -1;
	int	 rval;

	struct getopt_data getopt_reent;
	memset(&getopt_reent, 0, sizeof(getopt_data));

	dev = 0;
	fifo = hasformat = 0;
	pack = pack_native;

#ifdef KERN_DRIVERS
	while ((ch = getopt(argc, argv, "lrRF:g:m:u:")) != -1) {
#else
	while ((ch = getopt_r(argc, argv, "rRF:g:m:u:", &getopt_reent)) != -1) {
#endif
		switch (ch) {

#ifdef KERN_DRIVERS
		case 'l':
			l_flag = 1;
			break;
#endif

		case 'r':
			r_flag = 1;
			break;

		case 'R':
			r_flag = 2;
			break;

		case 'F':
			pack = pack_find(getopt_reent.optarg);
			if (pack == NULL)
				errx(exit_jump, 1, "invalid format: %s", getopt_reent.optarg);
			hasformat++;
			break;

		case 'g':
			if (getopt_reent.optarg[0] == '#') {
				gid = strtol(getopt_reent.optarg + 1, &p, 10);
				if (*p == 0)
					break;
			}
			if (gid_name(getopt_reent.optarg, &gid) == 0)
				break;
			gid = strtol(getopt_reent.optarg, &p, 10);
			if (*p == 0)
				break;
			errx(exit_jump, 1, "%s: invalid group name", getopt_reent.optarg);

		case 'm':
#if RTEMS_REMOVED
			modes = setmode(getopt_reent.optarg);
			if (modes == NULL)
#endif
				err(exit_jump, 1, "Cannot set file mode `%s'", getopt_reent.optarg);
			break;

		case 'u':
			if (getopt_reent.optarg[0] == '#') {
				uid = strtol(getopt_reent.optarg + 1, &p, 10);
				if (*p == 0)
					break;
			}
#if RTEMS_REMOVED
			if (uid_from_user(getopt_reent.optarg, &uid) == 0)
				break;
#endif
			uid = strtol(getopt_reent.optarg, &p, 10);
			if (*p == 0)
				break;
			errx(exit_jump, 1, "%s: invalid user name", getopt_reent.optarg);

		default:
		case '?':
			usage(globals);
		}
	}
	argc -= getopt_reent.optind;
	argv += getopt_reent.optind;

#ifdef KERN_DRIVERS
	if (l_flag) {
		print_device_info(argv);
		return 0;
	}
#endif

	if (argc < 2 || argc > 10)
		usage(globals);

	name = *argv;
	argc--;
	argv++;

	umask(mode = umask(0));
	mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mode;

	if (argv[0][1] != '\0')
		goto badtype;
	switch (*argv[0]) {
	case 'c':
		mode |= S_IFCHR;
		break;

	case 'b':
		mode |= S_IFBLK;
		break;

	case 'p':
		if (hasformat)
			errx(exit_jump, 1, "format is meaningless for fifos");
		mode |= S_IFIFO;
		fifo = 1;
		break;

	default:
 badtype:
		errx(exit_jump, 1, "node type must be 'b', 'c' or 'p'.");
	}
	argc--;
	argv++;

	if (fifo) {
		if (argc != 0)
			usage(globals);
	} else {
		if (argc < 1 || argc > MAXARGS)
			usage(globals);
	}

	for (n = 0; n < argc; n++) {
		errno = 0;
		numbers[n] = strtoul(argv[n], &p, 0);
		if (*p == 0 && errno == 0)
			continue;
#ifdef KERN_DRIVERS
		if (n == 0) {
			major = major_from_name(argv[0], mode);
			if (major != -1) {
				numbers[0] = major;
				continue;
			}
			if (!isdigit(*(unsigned char *)argv[0]))
				errx(1, "unknown driver: %s", argv[0]);
		}
#endif
		errx(exit_jump, 1, "invalid number: %s", argv[n]);
	}

	switch (argc) {
	case 0:
		dev = 0;
		break;

	case 1:
		dev = numbers[0];
		break;

	default:
		dev = callPack(globals, pack, argc, numbers);
		break;
	}

#if RTEMS_REMOVED
	if (modes != NULL)
		mode = getmode(modes, mode);
#endif
	umask(0);
	rval = fifo ? mkfifo(name, mode) : mknod(name, mode, dev);
	if (rval < 0 && errno == EEXIST && r_flag) {
		struct stat sb;
		if (lstat(name, &sb) != 0 || (!fifo && sb.st_rdev != dev))
			sb.st_mode = 0;

		if ((sb.st_mode & S_IFMT) == (mode & S_IFMT)) {
			if (r_flag == 1)
				/* Ignore permissions and user/group */
				return 0;
			if (sb.st_mode != mode)
				rval = chmod(name, mode);
			else
				rval = 0;
		} else {
			unlink(name);
			rval = fifo ? mkfifo(name, mode)
				    : mknod(name, mode, dev);
		}
	}
	if (rval < 0)
		err(exit_jump, 1, "%s", name);
	if ((uid != (uid_t)-1 || gid != (uid_t)-1) && chown(name, uid, gid) == -1)
		/* XXX Should we unlink the files here? */
		warn("%s: uid/gid not changed", name);

	return 0;
}

static void
usage(rtems_shell_mknod_globals* globals)
{
	const char *progname = getprogname();

	(void)fprintf(stderr,
	    "usage: %s [-rR] [-F format] [-m mode] [-u user] [-g group]\n",
	    progname);
	(void)fprintf(stderr,
#ifdef KERN_DRIVERS
	    "                   [ name [b | c] [major | driver] minor\n"
#else
	    "                   [ name [b | c] major minor\n"
#endif
	    "                   | name [b | c] major unit subunit\n"
	    "                   | name [b | c] number\n"
	    "                   | name p ]\n");
#ifdef KERN_DRIVERS
	(void)fprintf(stderr, "       %s -l [driver] ...\n", progname);
#endif
	exit(1);
}

static int
gid_name(const char *name, gid_t *gid)
{
	struct group *g;

	g = getgrnam(name);
	if (!g)
		return -1;
	*gid = g->gr_gid;
	return 0;
}

static portdev_t
callPack(rtems_shell_mknod_globals* globals, pack_t *f, int n, u_long *numbers)
{
	portdev_t d;
	const char *error = NULL;

	d = (*f)(n, numbers, &error);
	if (error != NULL)
		errx(exit_jump, 1, "%s", error);
	return d;
}