#include <sys/zone.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <libintl.h>
#include <locale.h>
#include <string.h>
#include <priv.h>
#include <errno.h>
#include <zone.h>
#include <pool.h>
#include <unistd.h>
#include "utils.h"
#ifndef TEXT_DOMAIN
#define TEXT_DOMAIN "SYS_TEST"
#endif
static int Cflag;
static int Sflag;
static int Dflag;
static int Eflag;
static int Nflag;
static int Xflag;
static void
usage(void)
{
(void) fprintf(stderr,
gettext("Usage:\tpooladm [-n] [-s] [-c] [filename]\n"));
(void) fprintf(stderr,
gettext("Usage:\tpooladm [-n] -x\n"));
(void) fprintf(stderr,
gettext("Usage:\tpooladm -d | -e\n"));
exit(E_USAGE);
}
static void
config_print(pool_conf_t *conf)
{
char *buf;
pool_value_t *pv;
const char *tgt;
if (pool_conf_open(conf, pool_dynamic_location(), PO_RDONLY)
!= PO_SUCCESS)
die(gettext(ERR_OPEN_DYNAMIC), get_errstr());
if ((pv = pool_value_alloc()) == NULL ||
pool_get_property(conf, pool_conf_to_elem(conf), "system.name",
pv) == POC_INVAL ||
pool_value_get_string(pv, &tgt) != PO_SUCCESS)
die(gettext(ERR_GET_ELEMENT_DETAILS),
gettext(CONFIGURATION), "unknown", get_errstr());
if ((buf = pool_conf_info(conf, PO_TRUE)) == NULL)
die(gettext(ERR_GET_ELEMENT_DETAILS), gettext(CONFIGURATION),
tgt, get_errstr());
pool_value_free(pv);
(void) printf("%s", buf);
free(buf);
(void) pool_conf_close(conf);
}
static void
config_destroy(pool_conf_t *conf)
{
if (pool_conf_open(conf, pool_dynamic_location(), PO_RDWR)
!= PO_SUCCESS)
die(gettext(ERR_OPEN_DYNAMIC), get_errstr());
if (pool_conf_remove(conf) != PO_SUCCESS)
die(gettext(ERR_REMOVE_DYNAMIC), get_errstr());
}
static void
config_commit(pool_conf_t *conf, const char *static_conf_name)
{
if (pool_conf_open(conf, static_conf_name, Nflag || !Sflag ?
PO_RDONLY : PO_RDWR) != PO_SUCCESS)
die(gettext(ERR_OPEN_STATIC), static_conf_name, get_errstr());
if (pool_conf_validate(conf, POV_RUNTIME) != PO_SUCCESS)
die(gettext(ERR_VALIDATE_RUNTIME), static_conf_name);
if (!Nflag) {
if (pool_conf_commit(conf, PO_TRUE) != PO_SUCCESS)
die(gettext(ERR_COMMIT_DYNAMIC), static_conf_name,
get_errstr());
if (Sflag) {
if (pool_conf_commit(conf, PO_FALSE) != PO_SUCCESS)
die(gettext(ERR_COMMIT_STATIC),
static_conf_name, get_errstr());
}
}
(void) pool_conf_close(conf);
}
int
main(int argc, char *argv[])
{
int c;
pool_conf_t *conf = NULL;
const char *static_conf_loc;
(void) getpname(argv[0]);
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "cdensx")) != EOF) {
switch (c) {
case 'c':
Cflag++;
break;
case 'd':
Dflag++;
break;
case 'e':
Eflag++;
break;
case 'n':
Nflag++;
break;
case 's':
Sflag++;
break;
case 'x':
Xflag++;
break;
case '?':
default:
usage();
}
}
if ((Cflag || Sflag || Dflag || Eflag) && Xflag)
usage();
if ((Dflag || Eflag) && (Cflag || Sflag || Xflag))
usage();
if (Dflag && Eflag)
usage();
argc -= optind;
argv += optind;
if (! (Cflag || Sflag)) {
if (argc != 0)
usage();
} else {
if (argc == 0)
static_conf_loc = pool_static_location();
else if (argc == 1)
static_conf_loc = argv[0];
else
usage();
}
if (!Nflag && (Cflag + Dflag + Eflag + Xflag != 0) &&
!priv_ineffect(PRIV_SYS_RES_CONFIG))
die(gettext(ERR_PERMISSIONS));
if (Dflag) {
if (pool_set_status(POOL_DISABLED) != PO_SUCCESS)
die(gettext(ERR_DISABLE));
} else if (Eflag) {
if (pool_set_status(POOL_ENABLED) != PO_SUCCESS) {
if (errno == EEXIST)
die(gettext(ERR_ENABLE
": System has active processor sets\n"));
else
die(gettext(ERR_ENABLE));
}
} else {
if ((conf = pool_conf_alloc()) == NULL)
die(gettext(ERR_NOMEM));
if (Cflag + Sflag + Xflag == 0) {
config_print(conf);
} else if (!Nflag && Xflag) {
config_destroy(conf);
} else {
if (Cflag)
config_commit(conf, static_conf_loc);
else {
if (!Nflag && Sflag) {
if (pool_conf_open(conf,
pool_dynamic_location(), PO_RDONLY)
!= PO_SUCCESS)
die(gettext(ERR_OPEN_DYNAMIC),
get_errstr());
if (pool_conf_export(conf,
static_conf_loc, POX_NATIVE) !=
PO_SUCCESS)
die(gettext(ERR_EXPORT_DYNAMIC),
static_conf_loc, get_errstr());
(void) pool_conf_close(conf);
}
}
}
pool_conf_free(conf);
}
return (E_PO_SUCCESS);
}