#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <mdb/mdb_shell.h>
#include <mdb/mdb_lex.h>
#include <mdb/mdb_err.h>
#include <mdb/mdb_debug.h>
#include <mdb/mdb_string.h>
#include <mdb/mdb_frame.h>
#include <mdb/mdb_io_impl.h>
#include <mdb/mdb.h>
#define E_BADEXEC 127
static int
closefd_walk(void *unused, int fd)
{
if (fd > 2)
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
return (0);
}
void
mdb_shell_exec(char *cmd)
{
int status;
pid_t pid;
if (access(mdb.m_shell, X_OK) == -1)
yyperror("cannot access %s", mdb.m_shell);
if ((pid = vfork()) == -1)
yyperror("failed to fork");
if (pid == 0) {
(void) fdwalk(closefd_walk, NULL);
(void) execlp(mdb.m_shell, strbasename(mdb.m_shell),
"-c", cmd, NULL);
warn("failed to exec %s", mdb.m_shell);
_exit(E_BADEXEC);
}
do {
mdb_dprintf(MDB_DBG_SHELL, "waiting for PID %d\n", (int)pid);
} while (waitpid(pid, &status, 0) == -1 && errno == EINTR);
mdb_dprintf(MDB_DBG_SHELL, "waitpid %d -> 0x%x\n", (int)pid, status);
strfree(cmd);
}
static void
shellio_unlink(mdb_io_t *io, mdb_iob_t *iob)
{
mdb_io_t *fdio = io->io_next;
ASSERT(iob->iob_iop == io);
ASSERT(fdio != NULL);
io->io_next = fdio->io_next;
fdio->io_next = NULL;
mdb_io_rele(fdio);
}
static void
shellio_close(mdb_io_t *io)
{
pid_t pid = (pid_t)(intptr_t)io->io_data;
int status;
do {
mdb_dprintf(MDB_DBG_SHELL, "waiting for PID %d\n", (int)pid);
} while (waitpid(pid, &status, 0) == -1 && errno == EINTR);
mdb_dprintf(MDB_DBG_SHELL, "waitpid %d -> 0x%x\n", (int)pid, status);
}
static const mdb_io_ops_t shellio_ops = {
.io_read = no_io_read,
.io_write = no_io_write,
.io_seek = no_io_seek,
.io_ctl = no_io_ctl,
.io_close = shellio_close,
.io_name = no_io_name,
.io_link = no_io_link,
.io_unlink = shellio_unlink,
.io_setattr = no_io_setattr,
.io_suspend = no_io_suspend,
.io_resume = no_io_resume
};
void
mdb_shell_pipe(char *cmd)
{
uint_t iflag = mdb_iob_getflags(mdb.m_out) & MDB_IOB_INDENT;
mdb_iob_t *iob;
mdb_io_t *io;
int pfds[2];
pid_t pid;
if (access(mdb.m_shell, X_OK) == -1)
yyperror("cannot access %s", mdb.m_shell);
if (pipe(pfds) == -1)
yyperror("failed to open pipe");
iob = mdb_iob_create(mdb_fdio_create(pfds[1]), MDB_IOB_WRONLY | iflag);
mdb_iob_clrflags(iob, MDB_IOB_AUTOWRAP | MDB_IOB_INDENT);
mdb_iob_resize(iob, BUFSIZ, BUFSIZ);
if ((pid = vfork()) == -1) {
(void) close(pfds[0]);
(void) close(pfds[1]);
mdb_iob_destroy(iob);
yyperror("failed to fork");
}
if (pid == 0) {
(void) close(pfds[1]);
(void) close(STDIN_FILENO);
(void) dup2(pfds[0], STDIN_FILENO);
(void) fdwalk(closefd_walk, NULL);
(void) execlp(mdb.m_shell, strbasename(mdb.m_shell),
"-c", cmd, NULL);
warn("failed to exec %s", mdb.m_shell);
_exit(E_BADEXEC);
}
(void) close(pfds[0]);
strfree(cmd);
io = mdb_alloc(sizeof (mdb_io_t), UM_SLEEP);
io->io_ops = &shellio_ops;
io->io_data = (void *)(intptr_t)pid;
io->io_next = NULL;
io->io_refcnt = 0;
mdb_iob_stack_push(&mdb.m_frame->f_ostk, mdb.m_out, yylineno);
mdb_iob_push_io(iob, io);
mdb.m_out = iob;
}