#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/termios.h>
#include <sys/zcons.h>
#include <sys/mkdev.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <stropts.h>
#include <thread.h>
#include <ucred.h>
#include <unistd.h>
#include <zone.h>
#include <libdevinfo.h>
#include <libdevice.h>
#include <libzonecfg.h>
#include <syslog.h>
#include <sys/modctl.h>
#include "zoneadmd.h"
#define ZCONSNEX_DEVTREEPATH "/pseudo/zconsnex@1"
#define ZCONSNEX_FILEPATH "/devices/pseudo/zconsnex@1"
#define CONSOLE_SOCKPATH ZONES_TMPDIR "/%s.console_sock"
static int serverfd = -1;
char boot_args[BOOTARGS_MAX];
char bad_boot_arg[BOOTARGS_MAX];
static int eventstream[2];
int
eventstream_init()
{
if (pipe(eventstream) == -1)
return (-1);
return (0);
}
void
eventstream_write(zone_evt_t evt)
{
(void) write(eventstream[0], &evt, sizeof (evt));
}
static zone_evt_t
eventstream_read(void)
{
zone_evt_t evt = Z_EVT_NULL;
(void) read(eventstream[1], &evt, sizeof (evt));
return (evt);
}
struct cb_data {
zlog_t *zlogp;
int found;
int killed;
};
static int
count_cb(di_node_t node, void *arg)
{
struct cb_data *cb = (struct cb_data *)arg;
char *prop_data;
if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "zonename",
&prop_data) != -1) {
assert(prop_data != NULL);
if (strcmp(prop_data, zone_name) == 0) {
cb->found++;
return (DI_WALK_CONTINUE);
}
}
return (DI_WALK_CONTINUE);
}
static int
count_console_devs(zlog_t *zlogp)
{
di_node_t root;
struct cb_data cb;
bzero(&cb, sizeof (cb));
cb.zlogp = zlogp;
if ((root = di_init(ZCONSNEX_DEVTREEPATH, DINFOCPYALL)) ==
DI_NODE_NIL) {
zerror(zlogp, B_TRUE, "%s failed", "di_init");
return (-1);
}
(void) di_walk_node(root, DI_WALK_CLDFIRST, (void *)&cb, count_cb);
di_fini(root);
return (cb.found);
}
static int
destroy_cb(di_node_t node, void *arg)
{
struct cb_data *cb = (struct cb_data *)arg;
char *prop_data;
char *tmp;
char devpath[MAXPATHLEN];
devctl_hdl_t hdl;
if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "zonename",
&prop_data) == -1)
return (DI_WALK_CONTINUE);
assert(prop_data != NULL);
if (strcmp(prop_data, zone_name) != 0) {
return (DI_WALK_CONTINUE);
}
cb->found++;
tmp = di_devfs_path(node);
(void) snprintf(devpath, sizeof (devpath), "/devices/%s", tmp);
di_devfs_path_free(tmp);
if ((hdl = devctl_device_acquire(devpath, 0)) == NULL) {
zerror(cb->zlogp, B_TRUE, "WARNING: console %s found, "
"but it could not be controlled.", devpath);
return (DI_WALK_CONTINUE);
}
if (devctl_device_remove(hdl) == 0) {
cb->killed++;
} else {
zerror(cb->zlogp, B_TRUE, "WARNING: console %s found, "
"but it could not be removed.", devpath);
}
devctl_release(hdl);
return (DI_WALK_CONTINUE);
}
static int
destroy_console_devs(zlog_t *zlogp)
{
char conspath[MAXPATHLEN];
di_node_t root;
struct cb_data cb;
int managerfd;
int subfd;
(void) snprintf(conspath, sizeof (conspath), "/dev/zcons/%s/%s",
zone_name, ZCONS_MANAGER_NAME);
if ((managerfd = open(conspath, O_RDWR | O_NOCTTY)) != -1) {
(void) snprintf(conspath, sizeof (conspath), "/dev/zcons/%s/%s",
zone_name, ZCONS_SUBSIDIARY_NAME);
if ((subfd = open(conspath, O_RDWR | O_NOCTTY)) != -1) {
if (ioctl(managerfd, ZC_RELEASESUBSID,
(caddr_t)(intptr_t)subfd) != 0)
zerror(zlogp, B_TRUE, "WARNING: error while "
"releasing subsidiary handle of zone "
"console for %s", zone_name);
(void) close(subfd);
} else {
zerror(zlogp, B_TRUE, "WARNING: could not open "
"subsidiary side of zone console for %s to "
"release subsidiary handle", zone_name);
}
(void) close(managerfd);
} else {
zerror(zlogp, B_TRUE, "WARNING: could not open manager side of "
"zone console for %s to release subsidiary handle",
zone_name);
}
bzero(&cb, sizeof (cb));
cb.zlogp = zlogp;
if ((root = di_init(ZCONSNEX_DEVTREEPATH, DINFOCPYALL)) ==
DI_NODE_NIL) {
zerror(zlogp, B_TRUE, "%s failed", "di_init");
return (-1);
}
(void) di_walk_node(root, DI_WALK_CLDFIRST, (void *)&cb, destroy_cb);
if (cb.found > 1) {
zerror(zlogp, B_FALSE, "WARNING: multiple zone console "
"instances detected for zone '%s'; %d of %d "
"successfully removed.",
zone_name, cb.killed, cb.found);
}
di_fini(root);
return (0);
}
static int
init_console_dev(zlog_t *zlogp)
{
char conspath[MAXPATHLEN];
devctl_hdl_t bus_hdl = NULL;
devctl_hdl_t dev_hdl = NULL;
devctl_ddef_t ddef_hdl = NULL;
di_devlink_handle_t dl = NULL;
int rv = -1;
int ndevs;
int managerfd;
int subfd;
int i;
ndevs = count_console_devs(zlogp);
if (ndevs == 1) {
goto devlinks;
} else if (ndevs > 1 || ndevs == -1) {
if (destroy_console_devs(zlogp) == -1) {
goto error;
}
}
if ((bus_hdl = devctl_bus_acquire(ZCONSNEX_FILEPATH, 0)) == NULL) {
zerror(zlogp, B_TRUE, "%s failed", "devctl_bus_acquire");
goto error;
}
if ((ddef_hdl = devctl_ddef_alloc("zcons", 0)) == NULL) {
zerror(zlogp, B_TRUE, "failed to allocate ddef handle");
goto error;
}
if (devctl_ddef_string(ddef_hdl, "zonename", zone_name) == -1) {
zerror(zlogp, B_TRUE, "failed to create zonename property");
goto error;
}
if (devctl_ddef_int(ddef_hdl, "auto-assign-instance", 1) == -1) {
zerror(zlogp, B_TRUE, "failed to create auto-assign-instance "
"property");
goto error;
}
if (devctl_ddef_int(ddef_hdl, "ddi-no-autodetach", 1) == -1) {
zerror(zlogp, B_TRUE, "failed to create ddi-no-auto-detach "
"property");
goto error;
}
if (devctl_bus_dev_create(bus_hdl, ddef_hdl, 0, &dev_hdl) == -1) {
zerror(zlogp, B_TRUE, "failed to create console node");
goto error;
}
devlinks:
if ((dl = di_devlink_init("zcons", DI_MAKE_LINK)) != NULL) {
(void) di_devlink_fini(&dl);
} else {
zerror(zlogp, B_TRUE, "failed to create devlinks");
goto error;
}
(void) snprintf(conspath, sizeof (conspath), "/dev/zcons/%s/%s",
zone_name, ZCONS_MANAGER_NAME);
if ((managerfd = open(conspath, O_RDWR | O_NOCTTY)) == -1) {
zerror(zlogp, B_TRUE, "ERROR: could not open manager side of "
"zone console for %s to acquire subsidiary handle",
zone_name);
goto error;
}
(void) snprintf(conspath, sizeof (conspath), "/dev/zcons/%s/%s",
zone_name, ZCONS_SUBSIDIARY_NAME);
if ((subfd = open(conspath, O_RDWR | O_NOCTTY)) == -1) {
zerror(zlogp, B_TRUE, "ERROR: could not open subsidiary side "
"of zone console for %s to acquire subsidiary handle",
zone_name);
(void) close(managerfd);
goto error;
}
for (i = 0; i < 5; i++) {
if (ioctl(managerfd, ZC_HOLDSUBSID, (caddr_t)(intptr_t)subfd)
== 0) {
rv = 0;
break;
} else if (errno != ENXIO) {
break;
}
(void) sleep(1);
}
if (rv != 0)
zerror(zlogp, B_TRUE, "ERROR: error while acquiring "
"subsidiary handle of zone console for %s", zone_name);
(void) close(subfd);
(void) close(managerfd);
error:
if (ddef_hdl)
devctl_ddef_free(ddef_hdl);
if (bus_hdl)
devctl_release(bus_hdl);
if (dev_hdl)
devctl_release(dev_hdl);
return (rv);
}
static int
init_console_sock(zlog_t *zlogp)
{
int servfd;
struct sockaddr_un servaddr;
bzero(&servaddr, sizeof (servaddr));
servaddr.sun_family = AF_UNIX;
(void) snprintf(servaddr.sun_path, sizeof (servaddr.sun_path),
CONSOLE_SOCKPATH, zone_name);
if ((servfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
zerror(zlogp, B_TRUE, "console setup: could not create socket");
return (-1);
}
(void) unlink(servaddr.sun_path);
if (bind(servfd, (struct sockaddr *)&servaddr,
sizeof (servaddr)) == -1) {
zerror(zlogp, B_TRUE,
"console setup: could not bind to socket");
goto out;
}
if (listen(servfd, 4) == -1) {
zerror(zlogp, B_TRUE,
"console setup: could not listen on socket");
goto out;
}
return (servfd);
out:
(void) unlink(servaddr.sun_path);
(void) close(servfd);
return (-1);
}
static void
destroy_console_sock(int servfd)
{
char path[MAXPATHLEN];
(void) snprintf(path, sizeof (path), CONSOLE_SOCKPATH, zone_name);
(void) unlink(path);
(void) shutdown(servfd, SHUT_RDWR);
(void) close(servfd);
}
static int
get_client_ident(int clifd, pid_t *pid, char *locale, size_t locale_len,
int *disconnect)
{
char buf[BUFSIZ], *bufp;
size_t buflen = sizeof (buf);
char c = '\0';
int i = 0, r;
if (pid == NULL) {
assert(locale == NULL && locale_len == 0);
while (read(clifd, &c, 1) == 1) {
if (c == '\n')
return (0);
}
}
bzero(buf, sizeof (buf));
while ((buflen > 1) && (r = read(clifd, &c, 1)) == 1) {
buflen--;
if (c == '\n')
break;
buf[i] = c;
i++;
}
if (r == -1)
return (-1);
if (c != '\n') {
while ((r = read(clifd, &c, sizeof (c))) > 0)
if (c == '\n')
break;
}
bufp = buf;
if (strncmp(bufp, "IDENT ", 6) != 0)
return (-1);
bufp += 6;
errno = 0;
*pid = strtoll(bufp, &bufp, 10);
if (errno != 0)
return (-1);
while (*bufp != '\0' && isspace(*bufp))
bufp++;
buflen = strlen(bufp) - 1;
*disconnect = atoi(&bufp[buflen]);
bufp[buflen - 1] = '\0';
(void) strlcpy(locale, bufp, locale_len);
return (0);
}
static int
accept_client(int servfd, pid_t *pid, char *locale, size_t locale_len,
int *disconnect)
{
int connfd;
struct sockaddr_un cliaddr;
socklen_t clilen;
clilen = sizeof (cliaddr);
connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen);
if (connfd == -1)
return (-1);
if (get_client_ident(connfd, pid, locale, locale_len,
disconnect) == -1) {
(void) shutdown(connfd, SHUT_RDWR);
(void) close(connfd);
return (-1);
}
(void) write(connfd, "OK\n", 3);
return (connfd);
}
static void
reject_client(int servfd, pid_t clientpid)
{
int connfd;
struct sockaddr_un cliaddr;
socklen_t clilen;
char nak[MAXPATHLEN];
clilen = sizeof (cliaddr);
connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen);
if (get_client_ident(connfd, NULL, NULL, 0, NULL) == 0) {
(void) snprintf(nak, sizeof (nak), "%lu\n",
clientpid);
(void) write(connfd, nak, strlen(nak));
}
(void) shutdown(connfd, SHUT_RDWR);
(void) close(connfd);
}
static void
event_message(int clifd, char *clilocale, zone_evt_t evt, int dflag)
{
char *str, *lstr = NULL;
char lmsg[BUFSIZ];
char outbuf[BUFSIZ];
if (clifd == -1)
return;
switch (evt) {
case Z_EVT_ZONE_BOOTING:
if (*boot_args == '\0') {
str = "NOTICE: Zone booting up";
break;
}
(void) snprintf(lmsg, sizeof (lmsg), localize_msg(clilocale,
"NOTICE: Zone booting up with arguments: %s"), boot_args);
lstr = lmsg;
break;
case Z_EVT_ZONE_READIED:
str = "NOTICE: Zone readied";
break;
case Z_EVT_ZONE_HALTED:
if (dflag)
str = "NOTICE: Zone halted. Disconnecting...";
else
str = "NOTICE: Zone halted";
break;
case Z_EVT_ZONE_REBOOTING:
if (*boot_args == '\0') {
str = "NOTICE: Zone rebooting";
break;
}
(void) snprintf(lmsg, sizeof (lmsg), localize_msg(clilocale,
"NOTICE: Zone rebooting with arguments: %s"), boot_args);
lstr = lmsg;
break;
case Z_EVT_ZONE_UNINSTALLING:
str = "NOTICE: Zone is being uninstalled. Disconnecting...";
break;
case Z_EVT_ZONE_BOOTFAILED:
if (dflag)
str = "NOTICE: Zone boot failed. Disconnecting...";
else
str = "NOTICE: Zone boot failed";
break;
case Z_EVT_ZONE_BADARGS:
(void) snprintf(lmsg, sizeof (lmsg),
localize_msg(clilocale,
"WARNING: Ignoring invalid boot arguments: %s"),
bad_boot_arg);
lstr = lmsg;
break;
default:
return;
}
if (lstr == NULL)
lstr = localize_msg(clilocale, str);
(void) snprintf(outbuf, sizeof (outbuf), "\r\n[%s]\r\n", lstr);
(void) write(clifd, outbuf, strlen(outbuf));
}
static int
test_client(int clifd)
{
if ((write(clifd, "", 0) == -1) && errno == EPIPE)
return (-1);
return (0);
}
static void
do_console_io(zlog_t *zlogp, int consfd, int servfd)
{
struct pollfd pollfds[4];
char ibuf[BUFSIZ];
int cc, ret;
int clifd = -1;
int pollerr = 0;
char clilocale[MAXPATHLEN];
pid_t clipid = 0;
int disconnect = 0;
pollfds[0].fd = consfd;
pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND |
POLLPRI | POLLERR | POLLHUP | POLLNVAL;
pollfds[1].fd = clifd;
pollfds[1].events = pollfds[0].events;
pollfds[2].fd = servfd;
pollfds[2].events = pollfds[0].events;
pollfds[3].fd = eventstream[1];
pollfds[3].events = pollfds[0].events;
for (;;) {
pollfds[0].revents = pollfds[1].revents = 0;
pollfds[2].revents = pollfds[3].revents = 0;
ret = poll(pollfds,
sizeof (pollfds) / sizeof (struct pollfd), -1);
if (ret == -1 && errno != EINTR) {
zerror(zlogp, B_TRUE, "poll failed");
break;
}
if (pollfds[0].revents) {
if (pollfds[0].revents &
(POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
errno = 0;
cc = read(consfd, ibuf, BUFSIZ);
if (cc <= 0 && (errno != EINTR) &&
(errno != EAGAIN))
break;
if (clifd != -1 && cc > 0)
(void) write(clifd, ibuf, cc);
} else {
pollerr = pollfds[0].revents;
zerror(zlogp, B_FALSE,
"closing connection with (console) "
"pollerr %d\n", pollerr);
break;
}
}
if (pollfds[1].revents) {
if (pollfds[1].revents &
(POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
errno = 0;
cc = read(clifd, ibuf, BUFSIZ);
if (cc <= 0 && (errno != EINTR) &&
(errno != EAGAIN))
break;
(void) write(consfd, ibuf, cc);
} else {
pollerr = pollfds[1].revents;
zerror(zlogp, B_FALSE,
"closing connection with (client) "
"pollerr %d\n", pollerr);
break;
}
}
if (pollfds[2].revents &&
(pollfds[2].revents & (POLLIN | POLLRDNORM))) {
if (clifd != -1) {
if (test_client(clifd) == -1) {
break;
}
reject_client(servfd, clipid);
} else if ((clifd = accept_client(servfd, &clipid,
clilocale, sizeof (clilocale),
&disconnect)) != -1) {
pollfds[1].fd = clifd;
} else {
break;
}
}
if (pollfds[3].revents) {
int evt = eventstream_read();
if (clifd == -1) {
break;
}
event_message(clifd, clilocale, evt, disconnect);
if (evt == Z_EVT_ZONE_UNINSTALLING) {
break;
}
if ((evt == Z_EVT_ZONE_HALTED ||
evt == Z_EVT_ZONE_BOOTFAILED) && disconnect) {
break;
}
}
}
if (clifd != -1) {
(void) shutdown(clifd, SHUT_RDWR);
(void) close(clifd);
}
}
int
init_console(zlog_t *zlogp)
{
if (init_console_dev(zlogp) == -1) {
zerror(zlogp, B_FALSE,
"console setup: device initialization failed");
return (-1);
}
if ((serverfd = init_console_sock(zlogp)) == -1) {
zerror(zlogp, B_FALSE,
"console setup: socket initialization failed");
return (-1);
}
return (0);
}
void
serve_console(zlog_t *zlogp)
{
int managerfd;
zone_state_t zstate;
char conspath[MAXPATHLEN];
(void) snprintf(conspath, sizeof (conspath),
"/dev/zcons/%s/%s", zone_name, ZCONS_MANAGER_NAME);
for (;;) {
managerfd = open(conspath, O_RDWR|O_NONBLOCK|O_NOCTTY);
if (managerfd == -1) {
zerror(zlogp, B_TRUE, "failed to open console manager");
(void) mutex_lock(&lock);
goto death;
}
if (ioctl(managerfd, I_SRDOPT, RNORM|RPROTDIS) == -1) {
zerror(zlogp, B_TRUE, "failed to set options on "
"console manager");
(void) mutex_lock(&lock);
goto death;
}
do_console_io(zlogp, managerfd, serverfd);
(void) close(managerfd);
(void) mutex_lock(&lock);
if (zone_get_state(zone_name, &zstate) == Z_OK) {
if (zstate < ZONE_STATE_READY)
goto death;
} else {
zerror(zlogp, B_FALSE,
"unable to determine state of zone");
goto death;
}
(void) mutex_unlock(&lock);
}
death:
assert(MUTEX_HELD(&lock));
in_death_throes = B_TRUE;
(void) mutex_unlock(&lock);
destroy_console_sock(serverfd);
(void) destroy_console_devs(zlogp);
}