Exemplo n.º 1
0
static void xsync_handle(struct xsync_ctx_t * ctx)
{
	uint8_t buf[PACKET_DATA_MAX];
	size_t size;

	switch(ctx->packet.command)
	{
	case XSYNC_COMMAND_ALIVE:
		size = sprintf((char *)buf, "%s", machine_uniqueid());
		xsync_put(XSYNC_COMMAND_ALIVE, buf, size);
		break;

	case XSYNC_COMMAND_START:
		buf[0] = xsync_handle_start(ctx);
		xsync_put(XSYNC_COMMAND_START, buf, 1);
		break;

	case XSYNC_COMMAND_TRANSFER:
		vfs_write(ctx->fd, (void *)ctx->packet.data, packet_dsize(&ctx->packet));
		xsync_put(XSYNC_COMMAND_TRANSFER, 0, 0);
		break;

	case XSYNC_COMMAND_STOP:
		if(ctx->fd > 0)
		{
			vfs_close(ctx->fd);
			ctx->fd = -1;
		}
		xsync_put(XSYNC_COMMAND_STOP, 0, 0);
		break;

	case XSYNC_COMMAND_SYSTEM:
		xsync_put(XSYNC_COMMAND_SYSTEM, 0, 0);
		ctx->quit = 1;
		if(ctx->fd > 0)
		{
			vfs_close(ctx->fd);
			ctx->fd = -1;
		}
		memset(buf, 0, sizeof(buf));
		memcpy(buf, &ctx->packet.data[0], packet_dsize(&ctx->packet));
		shell_system((const char *)buf);
		break;

	default:
		xsync_put(XSYNC_COMMAND_UNKOWN, 0, 0);
		break;
	}
}
Exemplo n.º 2
0
int
main(int argc, char *argv[])
{
	size_t clen, l;
	int ch, debug, i, magic, n, nargs, rval;
	char *c, *cmd, *p, *q, *nc;

	(void)setprogname(argv[0]);	/* for portability */

	debug = 0;
	magic = '%';		/* Default magic char is `%'. */
	nargs = -1;
	while ((ch = getopt(argc, argv, "a:d0123456789")) != -1) {
		switch (ch) {
		case 'a':
			if (optarg[1] != '\0')
				errx(EXIT_FAILURE,
				    "illegal magic character specification.");
			magic = optarg[0];
			break;
		case 'd':
			debug = 1;
			break;
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			if (nargs != -1)
				errx(EXIT_FAILURE,
				    "only one -# argument may be specified.");
			nargs = optopt - '0';
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 2)
		usage();

	/*
	 * The command to run is argv[0], and the args are argv[1..].
	 * Look for %digit references in the command, remembering the
	 * largest one.
	 */
	for (n = 0, p = argv[0]; *p != '\0'; ++p) {
		if (p[0] == magic && isdigit((unsigned char)p[1]) &&
		    p[1] != '0') {
			++p;
			if (p[0] - '0' > n)
				n = p[0] - '0';
		}
	}
	/*
	 * If there were any %digit references, then use those, otherwise
	 * build a new command string with sufficient %digit references at
	 * the end to consume (nargs) arguments each time round the loop.
	 * Allocate enough space to hold the maximum command.
	 */
	if ((cmd = malloc(sizeof("exec ") - 1 +
	    strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1)) == NULL)
		err(EXIT_FAILURE, "malloc");

	if (n == 0) {
		/* If nargs not set, default to a single argument. */
		if (nargs == -1)
			nargs = 1;

		p = cmd;
		p += sprintf(cmd, "exec %s", argv[0]);
		for (i = 1; i <= nargs; i++)
			p += sprintf(p, " %c%d", magic, i);

		/*
		 * If nargs set to the special value 0, eat a single
		 * argument for each command execution.
		 */
		if (nargs == 0)
			nargs = 1;
	} else {
		(void)sprintf(cmd, "exec %s", argv[0]);
		nargs = n;
	}

	/*
	 * Grab some space in which to build the command.  Allocate
	 * as necessary later, but no reason to build it up slowly
	 * for the normal case.
	 */
	if ((c = malloc(clen = 1024)) == NULL)
		err(EXIT_FAILURE, "malloc");

	/*
	 * (argc) and (argv) are still offset by one to make it simpler to
	 * expand %digit references.  At the end of the loop check for (argc)
	 * equals 1 means that all the (argv) has been consumed.
	 */
	for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
		/*
		 * Find a max value for the command length, and ensure
		 * there's enough space to build it.
		 */
		for (l = strlen(cmd), i = 0; i < nargs; i++)
			l += strlen(argv[i+1]);
		if (l > clen) {
			nc = realloc(c, l);
			if (nc == NULL)
				err(EXIT_FAILURE, "malloc");
			c = nc;
			clen = l;
		}

		/* Expand command argv references. */
		for (p = cmd, q = c; *p != '\0'; ++p) {
			if (p[0] == magic && isdigit((unsigned char)p[1]) &&
			    p[1] != '0')
				q += sprintf(q, "%s", argv[(++p)[0] - '0']);
			else
				*q++ = *p;
		}

		/* Terminate the command string. */
		*q = '\0';

		/* Run the command. */
		if (debug)
			(void)printf("%s\n", c);
		else if (shell_system(c))
			rval = 1;
	}

	if (argc != 1)
		errx(EXIT_FAILURE,
		    "expecting additional argument%s after \"%s\"",
		    (nargs - argc) ? "s" : "", argv[argc - 1]);
	return rval;
}