示例#1
0
文件: printf-ext.c 项目: brho/akaros
int main(int argc, char **argv)
{
	int ret;

	if (register_printf_specifier('i', printf_ipaddr, printf_ipaddr_info))
		printf("Failed to register 'i'\n");
	if (register_printf_specifier('M', printf_ipmask, printf_ipmask_info))
		printf("Failed to register 'M'\n");
	if (register_printf_specifier('E', printf_ethaddr, printf_ethaddr_info))
		printf("Failed to register 'E'\n");
	if (register_printf_specifier('H', printf_hexdump, printf_hexdump_info))
		printf("Failed to register 'H'\n");

	printf("IPv4 addr %i\n", v4addr);
	printf("IPv6 addr %i\n", v6addr);
	printf("IPv6 mask %M\n", v6mask);
	printf("IPv4 addr as mask %M\n", v4addr);
	printf("IPv6 addr as mask %M\n", v6addr);
	printf("ethaddr %E\n", ethaddr);
	printf("ethaddr null %E\n", 0);
	printf("IPv6 addr as hex (4 bytes only): %.4H\n", v6addr);
	printf("IPv6 addr as hex: %.*H\n", COUNT_OF(v6addr), v6addr);

	ret = open("/9/proc/no/such/file", 0, 0);
	printf("Open ret %d, errstr: %r\n", ret);
	printf("%s %i %M %d %s %r %E\n", "testing a few,", v6addr, v6mask, 1337,
	       "more cowbell", ethaddr);

	printf("Done\n");
	return 0;
}
示例#2
0
int
main (void)
{
  pa_xmm = register_printf_type (xmm_va);
  mod_v4f = register_printf_modifier (L"v4f");
  mod_v2d = register_printf_modifier (L"v2d");
  mod_v16i = register_printf_modifier (L"v16i");
  mod_v8i = register_printf_modifier (L"v8i");
  mod_v4i = register_printf_modifier (L"v4i");
  mod_v2i = register_printf_modifier (L"v2i");

  register_printf_specifier ('f', xmm_printf_f, xmm_ais);
  register_printf_specifier ('x', xmm_printf_x, xmm_ais);

  __m128 f = _mm_set_ps (1.0, 2.0, 3.0, 4.0);
  __m128d d = _mm_set_pd (1.0, 2.0);
  __m128i i = _mm_set_epi8 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);

  printf ("%f\n", 1.0f);
  printf ("%f\n", 2.0);
  printf ("%v4ff\n", f);
  printf ("%v2df\n", d);
  printf ("%x\n", 1);
  printf ("%v16ix\n", i);
  printf ("%v8ix\n", i);
  printf ("%v4ix\n", i);
  printf ("%v2ix\n", i);

  return 0;
}
示例#3
0
文件: vcore.c 项目: windyuuy/akaros
/* Run libc specific early setup code. */
static void vcore_libc_init(void)
{
    register_printf_specifier('r', printf_errstr, printf_errstr_info);
    /* TODO: register for other kevents/signals and whatnot (can probably reuse
     * the simple ev_q).  Could also do this via explicit functions from the
     * program. */
}
static void vhx_register_hooks(apr_pool_t *p) {
	ap_hook_translate_name(vhx_hook_vhost, NULL, NULL, APR_HOOK_FIRST);
	ap_hook_child_init(vhx_hook_init, NULL, NULL, APR_HOOK_MIDDLE);
	ap_hook_post_read_request(vhx_hook_itk, NULL, NULL, APR_HOOK_REALLY_FIRST);

	// %v tries to locate hostname from request_rec_t
	register_printf_specifier('v', vhx_print_host, vhx_print_host_info);
}
示例#5
0
文件: vcore.c 项目: 7perl/akaros
/* This gets called in glibc before calling the programs 'main'.  Need to set
 * ourselves up so that thread0 is a uthread, and then register basic signals to
 * go to vcore 0. */
void vcore_event_init(void)
{
	register_printf_specifier('r', printf_errstr, printf_errstr_info);
	/* set up our thread0 as a uthread */
	uthread_slim_init();
	/* TODO: register for other kevents/signals and whatnot (can probably reuse
	 * the simple ev_q).  Could also do this via explicit functions from the
	 * program. */
}
示例#6
0
文件: cs.c 项目: brho/akaros
int main(int argc, char *argv[])
{
	int justsetname, ch;
	char ext[Maxpath], servefile[Maxpath];

	/* Make us an SCP with a 2LS */
	parlib_wants_to_be_mcp = FALSE;
	register_printf_specifier('F', printf_fcall, printf_fcall_info);

	argv0 = argv[0];
	justsetname = 0;
	setnetmtpt(mntpt, sizeof(mntpt), NULL);
	ext[0] = 0;
	while ((ch = getopt(argc, argv, "4df:nSx:")) != -1) {
		switch (ch) {
		case '4':
			ipv6lookups = 0;
			break;
		case 'd':
			debug = 1;
			break;
		case 'f':
			dbfile = optarg;
			break;
		case 'n':
			justsetname = 1;
			break;
		case 'S':
			server = 1;
			break;
		case 'x':
			setnetmtpt(mntpt, sizeof(mntpt), optarg);
			setext(ext, sizeof(ext), mntpt);
			break;
		default:
			usage();
			break;
		}
	}
	snprintf(servefile, sizeof(servefile), "#srv/cs%s", ext);
	snprintf(netndb, sizeof(netndb), "%s/ndb", mntpt);
	syscall(SYS_nunmount, (unsigned long)servefile, strlen(servefile),
	        (unsigned long)mntpt, strlen(mntpt));
	remove(servefile);

	ndbinit();
	netinit(0);

	if (!justsetname) {
		mountinit(servefile, mntpt);
		if (server)
			evnotify(0);
		io();
	}

	evexit(0);
}
示例#7
0
void printf_binary_register() {
	if (register_printf_specifier(BINARY_FORMAT_KEY,printf_binary,printf_binary_arginfo_size) != 0) {
		const int error = errno;
		if (error != 0) {
			ERROR_MSG("register_printf_specifier %c for binary print has failed, error %d (%m)",BINARY_FORMAT_KEY,error);
		} else {
			ERROR_MSG("register_printf_specifier %c for binary print has failed",BINARY_FORMAT_KEY);
		}
	}
}
示例#8
0
int __register_printf_dfp (void)
{
  pa_d128 = register_printf_type (d128_va);
  pa_d32 = register_printf_type (d32_va);
  pa_d64 = register_printf_type (d64_va);

  mod_DD = register_printf_modifier (L"DD");
  mod_H = register_printf_modifier (L"H");
  mod_D = register_printf_modifier (L"D");

  register_printf_specifier ('f', printf_dfp, dfp_ais);
  register_printf_specifier ('F', printf_dfp, dfp_ais);
  register_printf_specifier ('e', printf_dfp, dfp_ais);
  register_printf_specifier ('E', printf_dfp, dfp_ais);
  register_printf_specifier ('g', printf_dfp, dfp_ais);
  register_printf_specifier ('G', printf_dfp, dfp_ais);
  register_printf_specifier ('a', printf_dfp, dfp_ais);
  register_printf_specifier ('A', printf_dfp, dfp_ais);

  return 0;
}
示例#9
0
int
main (int argc, char *argv[])
{
  static const char *options = "rv?";
  static const struct option long_options[] = {
    { "help", 0, 0, '?' },
    { "verbose", 0, 0, 'v' },
    { 0, 0, 0, 0 }
  };
  int c;
  char *cmdline;

  ignore_value (chdir ("/"));

  if (winsock_init () == -1)
    error (EXIT_FAILURE, 0, "winsock initialization failed");

#ifdef HAVE_REGISTER_PRINTF_SPECIFIER
  /* http://udrepper.livejournal.com/20948.html */
  register_printf_specifier ('Q', print_shell_quote, print_arginfo);
  register_printf_specifier ('R', print_sysroot_shell_quote, print_arginfo);
#else
#ifdef HAVE_REGISTER_PRINTF_FUNCTION
  register_printf_function ('Q', print_shell_quote, print_arginfo);
  register_printf_function ('R', print_sysroot_shell_quote, print_arginfo);
#else
#error "HAVE_REGISTER_PRINTF_{SPECIFIER|FUNCTION} not defined"
#endif
#endif

  struct stat statbuf;
  if (stat ("/", &statbuf) == 0)
    root_device = statbuf.st_dev;

  for (;;) {
    c = getopt_long (argc, argv, options, long_options, NULL);
    if (c == -1) break;

    switch (c) {
      /* The -r flag is used when running standalone.  It changes
       * several aspects of the daemon.
       */
    case 'r':
      sysroot = "";
      sysroot_len = 0;
      autosync_umount = 0;
      break;

    case 'v':
      verbose = 1;
      break;

    case '?':
      usage ();
      exit (EXIT_SUCCESS);

    default:
      fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c);
      exit (EXIT_FAILURE);
    }
  }

  if (optind < argc) {
    usage ();
    exit (EXIT_FAILURE);
  }

  cmdline = read_cmdline ();

  /* Set the verbose flag. */
  verbose = verbose ||
    (cmdline && strstr (cmdline, "guestfs_verbose=1") != NULL);
  if (verbose)
    printf ("verbose daemon enabled\n");

  if (verbose) {
    if (cmdline)
      printf ("linux commmand line: %s\n", cmdline);
    else
      printf ("could not read linux command line\n");
  }

#ifndef WIN32
  /* Make sure SIGPIPE doesn't kill us. */
  struct sigaction sa;
  memset (&sa, 0, sizeof sa);
  sa.sa_handler = SIG_IGN;
  sa.sa_flags = 0;
  if (sigaction (SIGPIPE, &sa, NULL) == -1)
    perror ("sigaction SIGPIPE"); /* but try to continue anyway ... */
#endif

#ifdef WIN32
# define setenv(n,v,f) _putenv(n "=" v)
#endif
  /* Set up a basic environment.  After we are called by /init the
   * environment is essentially empty.
   * https://bugzilla.redhat.com/show_bug.cgi?id=502074#c5
   *
   * NOTE: if you change $PATH, you must also change 'prog_exists'
   * function below.
   */
  setenv ("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1);
  setenv ("SHELL", "/bin/sh", 1);
  setenv ("LC_ALL", "C", 1);
  setenv ("TERM", "dumb", 1);

#ifndef WIN32
  /* We document that umask defaults to 022 (it should be this anyway). */
  umask (022);
#else
  /* This is the default for Windows anyway.  It's not even clear if
   * Windows ever uses this -- the MSDN documentation for the function
   * contains obvious errors.
   */
  _umask (0);
#endif

  /* Make a private copy of /etc/lvm so we can change the config (see
   * daemon/lvm-filter.c).
   */
  copy_lvm ();

  /* Connect to virtio-serial channel. */
  int sock = open (VIRTIO_SERIAL_CHANNEL, O_RDWR | O_CLOEXEC);
  if (sock == -1) {
    fprintf (stderr,
             "\n"
             "Failed to connect to virtio-serial channel.\n"
             "\n"
             "This is a fatal error and the appliance will now exit.\n"
             "\n"
             "Usually this error is caused by either QEMU or the appliance\n"
             "kernel not supporting the vmchannel method that the\n"
             "libguestfs library chose to use.  Please run\n"
             "'libguestfs-test-tool' and provide the complete, unedited\n"
             "output to the libguestfs developers, either in a bug report\n"
             "or on the libguestfs redhat com mailing list.\n"
             "\n");
    perror (VIRTIO_SERIAL_CHANNEL);
    exit (EXIT_FAILURE);
  }

  /* Send the magic length message which indicates that
   * userspace is up inside the guest.
   */
  char lenbuf[4];
  XDR xdr;
  uint32_t len = GUESTFS_LAUNCH_FLAG;
  xdrmem_create (&xdr, lenbuf, sizeof lenbuf, XDR_ENCODE);
  xdr_u_int (&xdr, &len);

  if (xwrite (sock, lenbuf, sizeof lenbuf) == -1) {
    perror ("xwrite");
    exit (EXIT_FAILURE);
  }

  xdr_destroy (&xdr);

  /* Enter the main loop, reading and performing actions. */
  main_loop (sock);

  exit (EXIT_SUCCESS);
}
示例#10
0
int
main (int argc, char *argv[])
{
  static const char *options = "rv?";
  static const struct option long_options[] = {
    { "help", 0, 0, '?' },
    { "verbose", 0, 0, 'v' },
    { 0, 0, 0, 0 }
  };
  int c;
  char *cmdline;

  ignore_value (chdir ("/"));

  if (winsock_init () == -1)
    error (EXIT_FAILURE, 0, "winsock initialization failed");

#ifdef HAVE_REGISTER_PRINTF_SPECIFIER
  /* http://udrepper.livejournal.com/20948.html */
  register_printf_specifier ('Q', print_shell_quote, print_arginfo);
  register_printf_specifier ('R', print_sysroot_shell_quote, print_arginfo);
#else
#ifdef HAVE_REGISTER_PRINTF_FUNCTION
  register_printf_function ('Q', print_shell_quote, print_arginfo);
  register_printf_function ('R', print_sysroot_shell_quote, print_arginfo);
#else
#error "HAVE_REGISTER_PRINTF_{SPECIFIER|FUNCTION} not defined"
#endif
#endif

  /* XXX The appliance /init script sets LD_PRELOAD=../libSegFault.so.
   * However if we CHROOT_IN to the sysroot that file might not exist,
   * resulting in all commands failing.  What we'd really like to do
   * is to have LD_PRELOAD only set while outside the chroot.  I
   * suspect the proper way to solve this is to remove the
   * CHROOT_IN/_OUT hack and replace it properly (fork), but that is
   * for another day.
   */
  unsetenv ("LD_PRELOAD");

  struct stat statbuf;
  if (stat ("/", &statbuf) == 0)
    root_device = statbuf.st_dev;

  for (;;) {
    c = getopt_long (argc, argv, options, long_options, NULL);
    if (c == -1) break;

    switch (c) {
      /* The -r flag is used when running standalone.  It changes
       * several aspects of the daemon.
       */
    case 'r':
      sysroot = "";
      sysroot_len = 0;
      autosync_umount = 0;
      break;

    case 'v':
      verbose = 1;
      break;

    case '?':
      usage ();
      exit (EXIT_SUCCESS);

    default:
      fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c);
      exit (EXIT_FAILURE);
    }
  }

  if (optind < argc) {
    usage ();
    exit (EXIT_FAILURE);
  }

  cmdline = read_cmdline ();

  /* Set the verbose flag. */
  verbose = verbose ||
    (cmdline && strstr (cmdline, "guestfs_verbose=1") != NULL);
  if (verbose)
    printf ("verbose daemon enabled\n");

  if (verbose) {
    if (cmdline)
      printf ("linux commmand line: %s\n", cmdline);
    else
      printf ("could not read linux command line\n");
  }

  enable_network = cmdline && strstr (cmdline, "guestfs_network=1") != NULL;

#ifndef WIN32
  /* Make sure SIGPIPE doesn't kill us. */
  struct sigaction sa;
  memset (&sa, 0, sizeof sa);
  sa.sa_handler = SIG_IGN;
  sa.sa_flags = 0;
  if (sigaction (SIGPIPE, &sa, NULL) == -1)
    perror ("sigaction SIGPIPE"); /* but try to continue anyway ... */
#endif

#ifdef WIN32
# define setenv(n,v,f) _putenv(n "=" v)
#endif
  /* Set up a basic environment.  After we are called by /init the
   * environment is essentially empty.
   * https://bugzilla.redhat.com/show_bug.cgi?id=502074#c5
   *
   * NOTE: if you change $PATH, you must also change 'prog_exists'
   * function below.
   */
  setenv ("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1);
  setenv ("SHELL", "/bin/sh", 1);
  setenv ("LC_ALL", "C", 1);
  setenv ("TERM", "dumb", 1);

#ifndef WIN32
  /* We document that umask defaults to 022 (it should be this anyway). */
  umask (022);
#else
  /* This is the default for Windows anyway.  It's not even clear if
   * Windows ever uses this -- the MSDN documentation for the function
   * contains obvious errors.
   */
  _umask (0);
#endif

  /* Make a private copy of /etc/lvm so we can change the config (see
   * daemon/lvm-filter.c).
   */
  copy_lvm ();

  /* Connect to virtio-serial channel. */
  char *channel, *p;
  if (cmdline && (p = strstr (cmdline, "guestfs_channel=")) != NULL) {
    p += 16;
    channel = strndup (p, strcspn (p, " \n"));
  }
  else
    channel = strdup (VIRTIO_SERIAL_CHANNEL);
  if (!channel) {
    perror ("strdup");
    exit (EXIT_FAILURE);
  }

  if (verbose)
    printf ("trying to open virtio-serial channel '%s'\n", channel);

  int sock = open (channel, O_RDWR|O_CLOEXEC);
  if (sock == -1) {
    fprintf (stderr,
             "\n"
             "Failed to connect to virtio-serial channel.\n"
             "\n"
             "This is a fatal error and the appliance will now exit.\n"
             "\n"
             "Usually this error is caused by either QEMU or the appliance\n"
             "kernel not supporting the vmchannel method that the\n"
             "libguestfs library chose to use.  Please run\n"
             "'libguestfs-test-tool' and provide the complete, unedited\n"
             "output to the libguestfs developers, either in a bug report\n"
             "or on the libguestfs redhat com mailing list.\n"
             "\n");
    perror (channel);
    exit (EXIT_FAILURE);
  }

  /* If it's a serial-port like device then it probably has echoing
   * enabled.  Put it into complete raw mode.
   */
  if (STRPREFIX (channel, "/dev/ttyS"))
    makeraw (channel, sock);

  /* cmdline, channel not used after this point */
  free (cmdline);
  free (channel);

  /* Wait for udev devices to be created.  If you start libguestfs,
   * especially with disks that contain complex (eg. mdadm) data
   * already, then it is possible for the 'mdadm' and LVM commands
   * that the init script runs to have not completed by the time the
   * daemon starts executing library commands.  (This is very rare and
   * hard to test however, but we have seen it in 'brew').  Run
   * udev_settle, but do it as late as possible to minimize the chance
   * that we'll have to do any waiting here.
   */
  udev_settle ();

  /* Send the magic length message which indicates that
   * userspace is up inside the guest.
   */
  char lenbuf[4];
  XDR xdr;
  uint32_t len = GUESTFS_LAUNCH_FLAG;
  xdrmem_create (&xdr, lenbuf, sizeof lenbuf, XDR_ENCODE);
  xdr_u_int (&xdr, &len);

  if (xwrite (sock, lenbuf, sizeof lenbuf) == -1) {
    perror ("xwrite");
    exit (EXIT_FAILURE);
  }

  xdr_destroy (&xdr);

  /* Enter the main loop, reading and performing actions. */
  main_loop (sock);

  exit (EXIT_SUCCESS);
}
示例#11
0
文件: ping.c 项目: rajuvindane/akaros
int main(int argc, char **argv)
{
	char *ds;
	int pid;
	pthread_t rcvr;

	register_printf_specifier('i', printf_ipaddr, printf_ipaddr_info);

	msglen = interval = 0;
	nmsg = NR_MSG;

	argv0 = argv[0];
	if (argc <= 1)
		usage();
	argc--, argv++;
	while (**argv == '-'){
		switch(argv[0][1]){
		case '6':
			proto = &v6pr;
			break;
		case 'a':
			addresses = 1;
			break;
		case 'd':
			debug++;
			break;
		case 'f':
			flood = 1;
			break;
		case 'i':
			argc--,argv++;
			interval = atoi(*argv);
			if(interval < 0)
				usage();
			break;
		case 'l':
			lostonly++;
			break;
		case 'n':
			argc--,argv++;
			nmsg = atoi(*argv);
			if(nmsg < 0)
				usage();
			break;
		case 'q':
			quiet = 1;
			break;
		case 'r':
			pingrint = 1;
			break;
		case 's':
			argc--,argv++;
			msglen = atoi(*argv);
			break;
		case 'w':
			argc--,argv++;
			waittime = atoi(*argv);
			if(waittime < 0)
				usage();
			break;
		default:
			usage();
			break;
		}
		
		argc--,argv++;
	}

	if(msglen < proto->iphdrsz + ICMP_HDRSIZE)
		msglen = proto->iphdrsz + ICMP_HDRSIZE;
	if(msglen < 64)
		msglen = 64;
	if(msglen >= 64*1024)
		msglen = 64*1024-1;
	if(interval <= 0 && !flood)
		interval = SLEEPMS;

	if(argc < 1)
		usage();

	/* TODO: consider catching ctrl-c and other signals. */

	if (!isv4name(argv[0]))
		proto = &v6pr;
	ds = netmkaddr(argv[0], proto->net, "1");
	printf("ping: dial %s\n", ds);
	fd = dial(ds, 0, 0, 0);
	if(fd < 0){
		fprintf(stderr, "%s: couldn't dial %s: %r\n", argv0, ds);
		exit(1);
	}

	if (!quiet)
		printf("sending %d %d byte messages %d ms apart to %s\n",
			nmsg, msglen, interval, ds);

	/* Spawning the receiver on a separate thread, possibly separate core */
	if (pthread_create(&rcvr, NULL, &rcvr_thread, NULL)) {
		perror("Failed to create recevier");
		exit(-1);
	}
	sender(fd, msglen, interval, nmsg);
	/* races with prints from the rcvr.  either lock, or live with it! */
	printd("Sent, now joining\n");
	pthread_join(rcvr, NULL);
	return 0;
}
示例#12
0
int register_printf_b(void)
{
	return register_printf_specifier('b',
		print_b, arginfo_size_b);
}
示例#13
0
文件: vecpf.c 项目: frediz/libvecpf
static int
__register_printf_vec( void )
{
  int i;


  /* Register a new type, and a function to call to copy an element
     of that type when going through the varargs list.

     Register_printf_type doesn't acknowlege failure, so assume the
     the retun code is always good. */

  printf_argtype_vec = register_printf_type (vec_va);

  /* Register our modifiers */

  for (i=0; i<vector_mods_len; ++i)
    {
      vector_mods[i].bits
       = register_printf_modifier (vector_mods[i].modifier_string);
    }


  /* Indicate our interest in integer types */
  register_printf_specifier ('d', vec_printf_d, vec_ais);
  register_printf_specifier ('i', vec_printf_d, vec_ais);
  register_printf_specifier ('o', vec_printf_d, vec_ais);
  register_printf_specifier ('u', vec_printf_d, vec_ais);
  register_printf_specifier ('x', vec_printf_d, vec_ais);
  register_printf_specifier ('X', vec_printf_d, vec_ais);
  register_printf_specifier ('c', vec_printf_d, vec_ais);

  /* Indicate our interest in fp types */
  register_printf_specifier ('f', vec_printf_f, vec_ais);
  register_printf_specifier ('F', vec_printf_f, vec_ais);
  register_printf_specifier ('e', vec_printf_f, vec_ais);
  register_printf_specifier ('E', vec_printf_f, vec_ais);
  register_printf_specifier ('g', vec_printf_f, vec_ais);
  register_printf_specifier ('G', vec_printf_f, vec_ais);
  register_printf_specifier ('a', vec_printf_f, vec_ais);
  register_printf_specifier ('A', vec_printf_f, vec_ais);

  return 0;
}