Пример #1
0
static void
sma_init(struct stevedore *parent, int ac, char * const *av)
{
	const char *e;
	uintmax_t u;
	struct sma_sc *sc;

	ASSERT_MGT();
	ALLOC_OBJ(sc, SMA_SC_MAGIC);
	AN(sc);
	sc->sma_max = SIZE_MAX;
	assert(sc->sma_max == SIZE_MAX);
	parent->priv = sc;

	AZ(av[ac]);
	if (ac > 1)
		ARGV_ERR("(-smalloc) too many arguments\n");

	if (ac == 0 || *av[0] == '\0')
		 return;

	e = str2bytes(av[0], &u, 0);
	if (e != NULL)
		ARGV_ERR("(-smalloc) size \"%s\": %s\n", av[0], e);
	if ((u != (uintmax_t)(size_t)u))
		ARGV_ERR("(-smalloc) size \"%s\": too big\n", av[0]);
	if (u < 1024*1024)
		ARGV_ERR("(-smalloc) size \"%s\": too small, "
			 "did you forget to specify M or G?\n", av[0]);

	sc->sma_max = u;
}
Пример #2
0
static int token_to_addr( struct line_token *token, struct sockaddr_storage *addr_p )
{
        char *ptr = strchr( token->token, ':' );
        char *port; 
        uint8_t buffer[4];
        int len;
        struct sockaddr_in *addrin_p;
        uint32_t *addr;
        int rv = 0;

        if ( ptr == NULL ) {
                WARN( "Not valid ip address and port \n" );
                return -1;
        }

        port = ptr + 1;
        *ptr = '\0';

        addrin_p = (struct sockaddr_in *)addr_p;

        /* XXX len!! */
        str2bytes( token->token, buffer, &len );
        addr = (uint32_t *)buffer;

        addrin_p->sin_addr.s_addr = htonl(*addr);
        addrin_p->sin_family = AF_INET;

        addrin_p->sin_port = htons( (uint16_t)strtol( port, NULL, 16 ) );

        TRACE( "The ip |%s| and port 0x%x/%d \n", inet_ntoa( addrin_p->sin_addr ), addrin_p->sin_port, addrin_p->sin_port );

        return rv;
}
Пример #3
0
int
main(int argc, char *argv[])
{
	struct test_case *tc;
	uintmax_t val;
	int ec;
	const char *e;

	(void)argc;
	for (ec = 0, tc = test_cases; tc->str; ++tc) {
		e = str2bytes(tc->str, &val, tc->rel);
		if (e != tc->err) {
			printf("%s: str2bytes(\"%s\", %ju) (%s) != (%s)\n",
			    *argv, tc->str, tc->rel, tc->err, e);
			++ec;
		} else if (e == NULL && val != tc->val) {
			printf("%s: str2bytes(\"%s\", %ju) %ju != %ju (%s)\n",
			    *argv, tc->str, tc->rel, val, tc->val, e);
			++ec;
		}
	}
	/* TODO: test invalid strings */
	if (!ec)
		printf("OK\n");
	return (ec > 0);
}
Пример #4
0
/**
 * Convert a IPv6 address on token read from proc/net/tcp6 (of format
 * <code>addr:port</code> ) to struct sockaddr.
 *
 * @param token Pointer to read token.
 * @param addr_p Pointer to struct sockaddr (really a sockaddr_in6) that should
 * receive the address.
 * @return -1 on error, 0 on success.
 */
static int token_to_addr6( struct line_token *token, struct sockaddr_storage *addr_p )
{
        char *ptr = strchr( token->token, ':' );
        char *port;
        uint8_t buffer[16];
        int len;
        struct sockaddr_in6 *addrin_p;
        int rv = 0;
        int i;
        uint32_t *bufp;

        if ( ptr == NULL ) {
                WARN( "Not valid IPv6 address and port on token\n" );
                return -1;
        }

        port = ptr + 1;
        *ptr = '\0';

        str2bytes( token->token, buffer, &len );
        if ( len != 16 ) {
                WARN("Malformed IPv6 address in tcp6 stats\n");
                return -1;
        }

        /* The IPv6 address in /proc/net/tcp6 is printed as 4 32bit parts, each
         * part being in host byte order. Hence (for little endian platforms,
         * anyway) we have to walk through the address in 32bit chunks and
         * switch their byte order. 
         */
        bufp = (uint32_t *)buffer;
        for ( i = 0; i < 4; i++ ) {
                *bufp = htonl(*bufp);
                bufp++;
        }

        addrin_p = (struct sockaddr_in6 *)addr_p;
        memcpy( &(addrin_p->sin6_addr.s6_addr), buffer, 16 );
        addrin_p->sin6_family = AF_INET6;
        addrin_p->sin6_port = htons( (uint16_t)strtol( port, NULL, 16));

        return rv;
}
Пример #5
0
static void
smu_init(struct stevedore *parent, int ac, char * const *av)
{
	const char *e;
	uintmax_t u;

	(void)parent;

	AZ(av[ac]);
	if (ac > 1)
		ARGV_ERR("(-sumem) too many arguments\n");

	if (ac == 0 || *av[0] == '\0')
		 return;

	e = str2bytes(av[0], &u, 0);
	if (e != NULL)
		ARGV_ERR("(-sumem) size \"%s\": %s\n", av[0], e);
	if ((u != (uintmax_t)(size_t)u))
		ARGV_ERR("(-sumem) size \"%s\": too big\n", av[0]);
	smu_max = u;
}
Пример #6
0
uintmax_t
STV_FileSize(int fd, const char *size, unsigned *granularity, const char *ctx)
{
	uintmax_t l, fssize;
	unsigned bs;
	const char *q;
	int i;
	off_t o;
	struct stat st;

	AZ(fstat(fd, &st));
	xxxassert(S_ISREG(st.st_mode));

	bs = *granularity;
	fssize = stv_fsspace(fd, &bs);
	xxxassert(bs % *granularity == 0);

	if ((size == NULL || *size == '\0') && st.st_size != 0) {
		/*
		 * We have no size specification, but an existing file,
		 * use its existing size.
		 */
		l = st.st_size;
	} else {
		AN(size);
		q = str2bytes(size, &l, fssize);

		if (q != NULL)
			ARGV_ERR("(%s) size \"%s\": %s\n", size, ctx, q);

		if (l < 1024*1024)
			ARGV_ERR("(-spersistent) size \"%s\": too small, "
				 "did you forget to specify M or G?\n", size);
	}

	/*
	 * This trickery wouldn't be necessary if X/Open would
	 * just add OFF_MAX to <limits.h>...
	 */
	i = 0;
	while(1) {
		o = l;
		if (o == l && o > 0)
			break;
		l >>= 1;
		i++;
	}
	if (i)
		fprintf(stderr, "WARNING: (%s) file size reduced"
		    " to %ju due to system \"off_t\" limitations\n", ctx, l);
	else if (l - st.st_size > fssize) {
		l = fssize * 80 / 100;
		fprintf(stderr, "WARNING: (%s) file size reduced"
		    " to %ju (80%% of available disk space)\n", ctx, l);
	}

	if (sizeof(void *) == 4 && l > INT32_MAX) { /*lint !e506 !e774 !e845 */
		fprintf(stderr,
		    "NB: Storage size limited to 2GB on 32 bit architecture,\n"
		    "NB: otherwise we could run out of address space.\n"
		);
		l = INT32_MAX;
	}

	/* round down to multiple of filesystem blocksize or pagesize */
	l -= (l % bs);

	*granularity = bs;
	return(l);
}