FreeBSD/lib/libjail/jail.c
/*
 * Convert a jail parameter's value to external form.
 */
Convert a jail parameter's value to external form.
Convert a jail parameter ' s value to external form .
char *
jailparam_export(struct jailparam *jp)
{
	size_t *valuelens;
	char *value, *tvalue, **values;
	size_t valuelen;
	int i, nval, ival;
	char valbuf[INET6_ADDRSTRLEN];
	if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
		value = strdup(jp->jp_value);
		if (value == NULL)
			strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
		return (value);
	}
	nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1;
	if (nval == 0) {
		value = strdup("");
		if (value == NULL)
			strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
		return (value);
	}
	values = alloca(nval * sizeof(char *));
	valuelens = alloca(nval * sizeof(size_t));
	valuelen = 0;
	for (i = 0; i < nval; i++) {
		switch (jp->jp_ctltype & CTLTYPE) {
		case CTLTYPE_INT:
			ival = ((int *)jp->jp_value)[i];
			if ((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
			    (unsigned)ival < 2) {
				strlcpy(valbuf, bool_values[ival],
				    sizeof(valbuf));
				break;
			}
			if ((jp->jp_flags & JP_JAILSYS) &&
			    (unsigned)ival < sizeof(jailsys_values) /
			    sizeof(jailsys_values[0])) {
				strlcpy(valbuf, jailsys_values[ival],
				    sizeof(valbuf));
				break;
			}
			snprintf(valbuf, sizeof(valbuf), "%d", ival);
			break;
		case CTLTYPE_UINT:
			snprintf(valbuf, sizeof(valbuf), "%u",
			    ((unsigned *)jp->jp_value)[i]);
			break;
		case CTLTYPE_LONG:
			snprintf(valbuf, sizeof(valbuf), "%ld",
			    ((long *)jp->jp_value)[i]);
			break;
		case CTLTYPE_ULONG:
			snprintf(valbuf, sizeof(valbuf), "%lu",
			    ((unsigned long *)jp->jp_value)[i]);
			break;
		case CTLTYPE_S64:
			snprintf(valbuf, sizeof(valbuf), "%jd",
			    (intmax_t)((int64_t *)jp->jp_value)[i]);
			break;
		case CTLTYPE_U64:
			snprintf(valbuf, sizeof(valbuf), "%ju",
			    (uintmax_t)((uint64_t *)jp->jp_value)[i]);
			break;
		case CTLTYPE_STRUCT:
			switch (jp->jp_structtype) {
			case JPS_IN_ADDR:
				if (inet_ntop(AF_INET,
				    &((struct in_addr *)jp->jp_value)[i],
				    valbuf, sizeof(valbuf)) == NULL) {
					strerror_r(errno, jail_errmsg,
					    JAIL_ERRMSGLEN);
					return (NULL);
				}
				break;
			case JPS_IN6_ADDR:
				if (inet_ntop(AF_INET6,
				    &((struct in6_addr *)jp->jp_value)[i],
				    valbuf, sizeof(valbuf)) == NULL) {
					strerror_r(errno, jail_errmsg,
					    JAIL_ERRMSGLEN);
					return (NULL);
				}
				break;
			default:
				goto unknown_type;
			}
			break;
		default:
		unknown_type:
			snprintf(jail_errmsg, JAIL_ERRMSGLEN,
			    "unknown type for %s", jp->jp_name);
			errno = ENOENT;
			return (NULL);
		}
		valuelens[i] = strlen(valbuf) + 1;
		valuelen += valuelens[i];
		values[i] = alloca(valuelens[i]);
		strcpy(values[i], valbuf);
	}
	value = malloc(valuelen);
	if (value == NULL)
		strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
	else {
		tvalue = value;
		for (i = 0; i < nval; i++) {
			strcpy(tvalue, values[i]);
			if (i < nval - 1) {
				tvalue += valuelens[i];
				tvalue[-1] = ',';
			}
		}
	}
	return (value);
}
char * jailparam_export ( struct jailparam * jp ) { size_t * valuelens ; char * value , * tvalue , * * values ; size_t valuelen ; int i , nval , ival ; char valbuf [ INET6_ADDRSTRLEN ] ; if ( ( jp -> jp_ctltype & CTLTYPE ) == CTLTYPE_STRING ) { value = strdup ( jp -> jp_value ) ; if ( value == NULL ) strerror_r ( errno , jail_errmsg , JAIL_ERRMSGLEN ) ; return ( value ) ; } nval = jp -> jp_elemlen ? jp -> jp_valuelen / jp -> jp_elemlen : 1 ; if ( nval == 0 ) { value = strdup ( " " ) ; if ( value == NULL ) strerror_r ( errno , jail_errmsg , JAIL_ERRMSGLEN ) ; return ( value ) ; } values = alloca ( nval * sizeof ( char * ) ) ; valuelens = alloca ( nval * sizeof ( size_t ) ) ; valuelen = 0 ; for ( i = 0 ; i < nval ; i ++ ) { switch ( jp -> jp_ctltype & CTLTYPE ) { case CTLTYPE_INT : ival = ( ( int * ) jp -> jp_value ) [ i ] ; if ( ( jp -> jp_flags & ( JP_BOOL | JP_NOBOOL ) ) && ( unsigned ) ival < 2 ) { strlcpy ( valbuf , bool_values [ ival ] , sizeof ( valbuf ) ) ; break ; } if ( ( jp -> jp_flags & JP_JAILSYS ) && ( unsigned ) ival < sizeof ( jailsys_values ) / sizeof ( jailsys_values [ 0 ] ) ) { strlcpy ( valbuf , jailsys_values [ ival ] , sizeof ( valbuf ) ) ; break ; } snprintf ( valbuf , sizeof ( valbuf ) , " " , ival ) ; break ; case CTLTYPE_UINT : snprintf ( valbuf , sizeof ( valbuf ) , " " , ( ( unsigned * ) jp -> jp_value ) [ i ] ) ; break ; case CTLTYPE_LONG : snprintf ( valbuf , sizeof ( valbuf ) , " " , ( ( long * ) jp -> jp_value ) [ i ] ) ; break ; case CTLTYPE_ULONG : snprintf ( valbuf , sizeof ( valbuf ) , " " , ( ( unsigned long * ) jp -> jp_value ) [ i ] ) ; break ; case CTLTYPE_S64 : snprintf ( valbuf , sizeof ( valbuf ) , " " , ( intmax_t ) ( ( int64_t * ) jp -> jp_value ) [ i ] ) ; break ; case CTLTYPE_U64 : snprintf ( valbuf , sizeof ( valbuf ) , " " , ( uintmax_t ) ( ( uint64_t * ) jp -> jp_value ) [ i ] ) ; break ; case CTLTYPE_STRUCT : switch ( jp -> jp_structtype ) { case JPS_IN_ADDR : if ( inet_ntop ( AF_INET , & ( ( struct in_addr * ) jp -> jp_value ) [ i ] , valbuf , sizeof ( valbuf ) ) == NULL ) { strerror_r ( errno , jail_errmsg , JAIL_ERRMSGLEN ) ; return ( NULL ) ; } break ; case JPS_IN6_ADDR : if ( inet_ntop ( AF_INET6 , & ( ( struct in6_addr * ) jp -> jp_value ) [ i ] , valbuf , sizeof ( valbuf ) ) == NULL ) { strerror_r ( errno , jail_errmsg , JAIL_ERRMSGLEN ) ; return ( NULL ) ; } break ; default : goto unknown_type ; } break ; default : unknown_type : snprintf ( jail_errmsg , JAIL_ERRMSGLEN , " " , jp -> jp_name ) ; errno = ENOENT ; return ( NULL ) ; } valuelens [ i ] = strlen ( valbuf ) + 1 ; valuelen += valuelens [ i ] ; values [ i ] = alloca ( valuelens [ i ] ) ; strcpy ( values [ i ] , valbuf ) ; } value = malloc ( valuelen ) ; if ( value == NULL ) strerror_r ( errno , jail_errmsg , JAIL_ERRMSGLEN ) ; else { tvalue = value ; for ( i = 0 ; i < nval ; i ++ ) { strcpy ( tvalue , values [ i ] ) ; if ( i < nval - 1 ) { tvalue += valuelens [ i ] ; tvalue [ -1 ] = ' ' ; } } } return ( value ) ; }
Convert a jail parameter's value to external form.
Convert a jail parameter ' s value to external form .