#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <libds.h>
#include <libgen.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <synch.h>
#include <syslog.h>
#include <thread.h>
#include <unistd.h>
#include <sys/debug.h>
#include <sys/ldoms.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "ldma.h"
#define LDMA_MODULE "ldm-agent-daemon"
#define LDMA_CONTROL_DOMAIN_DHDL 0
typedef struct ldma_connexion_t {
ds_hdl_t hdl;
ds_domain_hdl_t dhdl;
ds_ver_t ver;
} ldma_connexion_t;
typedef struct ldma_agent {
ldma_agent_info_t *info;
mutex_t conn_lock;
ldma_connexion_t conn[LDOMS_MAX_DOMAINS];
} ldma_agent_t;
extern ldma_agent_info_t ldma_device_info;
extern ldma_agent_info_t ldma_system_info;
extern ldma_agent_info_t ldma_dio_info;
boolean_t ldma_debug = B_FALSE;
boolean_t ldma_daemon = B_FALSE;
static ldma_agent_info_t *ldma_agent_infos[] = {
&ldma_device_info,
&ldma_system_info,
&ldma_dio_info,
NULL
};
static char *cmdname;
static pid_t daemon_pid = 0;
static ldma_connexion_t *
ldma_connexion_lookup(ldma_agent_t *agent, ds_hdl_t hdl)
{
ldma_connexion_t *connp;
int i;
ASSERT(MUTEX_HELD(&agent->conn_lock));
for (connp = agent->conn, i = 0; i < LDOMS_MAX_DOMAINS; i++, connp++) {
if (connp->hdl == hdl)
return (connp);
}
return (NULL);
}
static int
ldma_connexion_add(ldma_agent_t *agent, ds_hdl_t hdl, ds_domain_hdl_t dhdl,
ds_ver_t *verp)
{
ldma_connexion_t *connp;
ldma_connexion_t *availp = NULL;
int i;
(void) mutex_lock(&agent->conn_lock);
for (connp = agent->conn, i = 0; i < LDOMS_MAX_DOMAINS; i++, connp++) {
if (connp->hdl == hdl)
break;
if (availp == NULL && connp->hdl == DS_INVALID_HDL)
availp = connp;
}
if (i < LDOMS_MAX_DOMAINS) {
(void) mutex_unlock(&agent->conn_lock);
LDMA_INFO("agent %s hdl %llx already exists", agent->info->name,
hdl);
return (0);
}
if (!availp) {
(void) mutex_unlock(&agent->conn_lock);
LDMA_INFO("agent %s too many connections", agent->info->name);
return (0);
}
LDMA_DBG("agent %s adding connection (%x) %llx, %llx, %d.%d",
agent->info->name, availp, hdl, dhdl, verp->major, verp->minor);
availp->hdl = hdl;
availp->dhdl = dhdl;
availp->ver = *verp;
(void) mutex_unlock(&agent->conn_lock);
return (1);
}
static int
ldma_connexion_delete(ldma_agent_t *agent, ds_hdl_t hdl)
{
ldma_connexion_t *connp;
(void) mutex_lock(&agent->conn_lock);
if ((connp = ldma_connexion_lookup(agent, hdl)) == NULL) {
(void) mutex_unlock(&agent->conn_lock);
LDMA_INFO("agent %s connection delete failed to find %llx",
agent->info->name, hdl);
return (0);
}
LDMA_DBG("agent %s deleting connection (%x) %llx", agent->info->name,
connp, hdl);
connp->hdl = DS_INVALID_HDL;
connp->dhdl = 0;
connp->ver.major = 0;
connp->ver.minor = 0;
(void) mutex_unlock(&agent->conn_lock);
return (1);
}
static void
ldma_connexion_init(ldma_agent_t *agent)
{
ldma_connexion_t *connp;
int i;
for (connp = agent->conn, i = 0; i < LDOMS_MAX_DOMAINS; i++, connp++) {
connp->hdl = DS_INVALID_HDL;
}
}
static ldma_message_header_t *
ldma_alloc_msg(uint64_t msg_num, uint32_t msg_type, size_t msg_dlen)
{
ldma_message_header_t *msg;
size_t msg_len;
msg_len = LDMA_MESSAGE_SIZE(msg_dlen);
msg = malloc(msg_len);
if (msg == NULL)
return (NULL);
msg->msg_num = msg_num;
msg->msg_type = msg_type;
msg->msg_info = 0;
return (msg);
}
ldma_message_header_t *
ldma_alloc_result_msg(ldma_message_header_t *request, size_t msg_dlen)
{
uint64_t msg_num;
msg_num = (request == NULL)? 0 : request->msg_num;
return (ldma_alloc_msg(msg_num, LDMA_MSG_RESULT, msg_dlen));
}
static void
ldma_reg_cb(ds_hdl_t hdl, ds_cb_arg_t arg, ds_ver_t *ver,
ds_domain_hdl_t dhdl)
{
ldma_agent_t *agent = (ldma_agent_t *)arg;
char dname[LDOMS_MAX_NAME_LEN];
if (ds_dom_hdl_to_name(dhdl, dname, LDOMS_MAX_NAME_LEN) != 0) {
(void) strcpy(dname, "<unknown>");
}
LDMA_DBG("%s: REGISTER hdl=%llx, dhdl=%llx (%s) ver=%hd.%hd",
agent->info->name, hdl, dhdl, dname, ver->major, ver->minor);
if (!ldma_connexion_add(agent, hdl, dhdl, ver)) {
LDMA_INFO("agent %s failed to add connection from "
"domain %s", agent->info->name, dname);
}
}
static void
ldma_unreg_cb(ds_hdl_t hdl, ds_cb_arg_t arg)
{
ldma_agent_t *agent = (ldma_agent_t *)arg;
LDMA_DBG("%s: UNREGISTER hdl=%llx", agent->info->name, hdl);
if (!ldma_connexion_delete(agent, hdl)) {
LDMA_INFO("agent %s failed to unregister handle %llx",
agent->info->name, hdl);
}
}
static void
ldma_data_cb(ds_hdl_t hdl, ds_cb_arg_t arg, void *buf, size_t len)
{
ldma_agent_t *agent = (ldma_agent_t *)arg;
ldma_msg_handler_t *handler;
ldma_message_header_t *request = buf;
ldma_message_header_t *reply = NULL;
ldma_connexion_t *connp;
ds_ver_t conn_ver;
ds_domain_hdl_t conn_dhdl;
ldma_request_status_t status;
size_t request_dlen, reply_len, reply_dlen = 0;
int i;
if (len < LDMA_MESSAGE_HEADER_SIZE) {
LDMA_INFO("agent %s has ignored message with an invalid "
"size of %d bytes", agent->info->name, len);
return;
}
request_dlen = LDMA_MESSAGE_DLEN(len);
LDMA_DBG("%s: DATA hdl=%llx, request num=%llu type=0x%x info=0x%x "
"dlen=%d", agent->info->name, hdl, request->msg_num,
request->msg_type, request->msg_info, request_dlen);
(void) mutex_lock(&agent->conn_lock);
connp = ldma_connexion_lookup(agent, hdl);
if (connp != NULL) {
conn_dhdl = connp->dhdl;
conn_ver = connp->ver;
}
(void) mutex_unlock(&agent->conn_lock);
if (connp == NULL) {
LDMA_DBG("%s: DATA hdl=%llx, rejecting request from a "
"distrusted domain", agent->info->name, hdl);
status = LDMA_REQ_DENIED;
goto do_reply;
}
handler = NULL;
for (i = 0; i < agent->info->nhandlers; i++) {
if (agent->info->handlers[i].msg_type == request->msg_type) {
handler = &agent->info->handlers[i];
break;
}
}
if (handler == NULL) {
LDMA_DBG("%s: DATA hdl=%llx, unknown message type %x",
agent->info->name, hdl, request->msg_type);
status = LDMA_REQ_NOTSUP;
goto do_reply;
}
if ((conn_dhdl != LDMA_CONTROL_DOMAIN_DHDL) &&
(handler->msg_flags & LDMA_MSGFLG_ACCESS_ANY) == 0) {
LDMA_DBG("%s: DATA hdl=%llx, rejecting request from a "
"distrusted domain", agent->info->name, hdl);
status = LDMA_REQ_DENIED;
goto do_reply;
}
if (handler->msg_handler == NULL) {
LDMA_DBG("%s: DATA hdl=%llx, no handler",
agent->info->name, hdl);
status = LDMA_REQ_COMPLETED;
goto do_reply;
}
status = (*handler->msg_handler)(&conn_ver, request, request_dlen,
&reply, &reply_dlen);
LDMA_DBG("%s: DATA hdl=%llx, handler stat=%d reply=%p rlen=%d",
agent->info->name, hdl, status, (void *)reply, reply_dlen);
do_reply:
if (reply == NULL) {
reply = request;
reply_dlen = 0;
if (status == LDMA_REQ_COMPLETED) {
reply->msg_type = LDMA_MSG_RESULT;
reply->msg_info = 0;
} else {
reply->msg_type = LDMA_MSG_ERROR;
reply->msg_info =
(status == LDMA_REQ_NOTSUP)? LDMA_MSGERR_NOTSUP :
(status == LDMA_REQ_INVALID)? LDMA_MSGERR_INVALID :
(status == LDMA_REQ_DENIED)? LDMA_MSGERR_DENY :
LDMA_MSGERR_FAIL;
}
}
reply_len = LDMA_MESSAGE_SIZE(reply_dlen);
LDMA_DBG("%s: DATA hdl=%llx, reply num=%llu type=0x%x info=0x%x "
"dlen=%d", agent->info->name, hdl, reply->msg_num,
reply->msg_type, reply->msg_info, reply_dlen);
if (ds_send_msg(hdl, reply, reply_len) != 0) {
LDMA_ERR("agent %s has failed to send reply for request %llu",
agent->info->name, request->msg_num);
}
if (reply != request)
free(reply);
}
static int
ldma_register(ldma_agent_info_t *agent_info)
{
ldma_agent_t *agent;
ds_capability_t ds_cap;
ds_ops_t ds_ops;
agent = malloc(sizeof (ldma_agent_t));
if (agent == NULL)
goto register_fail;
agent->info = agent_info;
(void) mutex_init(&agent->conn_lock, USYNC_THREAD, NULL);
ldma_connexion_init(agent);
ds_cap.svc_id = agent_info->name;
ds_cap.vers = agent_info->vers;
ds_cap.nvers = agent_info->nvers;
ds_ops.ds_reg_cb = ldma_reg_cb;
ds_ops.ds_unreg_cb = ldma_unreg_cb;
ds_ops.ds_data_cb = ldma_data_cb;
ds_ops.cb_arg = agent;
if (ds_svc_reg(&ds_cap, &ds_ops) == 0) {
LDMA_INFO("agent %s registered", agent_info->name);
return (0);
}
register_fail:
LDMA_ERR("agent %s has failed to register", agent_info->name);
free(agent);
return (-1);
}
static int
ldma_register_agents()
{
int count = 0;
ldma_agent_info_t **agent_infop;
for (agent_infop = ldma_agent_infos;
*agent_infop != NULL; agent_infop++) {
if (ldma_register(*agent_infop) == 0)
count++;
}
return (count);
}
static void
ldma_sigusr_handler(int sig, siginfo_t *sinfo, void *ucontext)
{
if (sig != SIGUSR1 || sinfo->si_code != SI_USER ||
(daemon_pid > 0 && sinfo->si_pid != daemon_pid))
return;
exit(0);
}
static void
ldma_start(boolean_t standalone)
{
int stat, rv;
struct sigaction action;
if (!standalone) {
action.sa_sigaction = ldma_sigusr_handler;
action.sa_flags = SA_SIGINFO;
if (sigemptyset(&action.sa_mask) == -1) {
LDMA_ERR("sigemptyset error (%d)", errno);
exit(1);
}
if (sigaction(SIGUSR1, &action, NULL) == -1) {
LDMA_ERR("sigaction() error (%d)", errno);
exit(1);
}
if (sigrelse(SIGUSR1) == -1) {
LDMA_ERR("sigrelse() error (%d)", errno);
exit(1);
}
if ((daemon_pid = fork()) == -1) {
LDMA_ERR("fork() error (%d)", errno);
exit(1);
}
if (daemon_pid != 0) {
for (;;) {
rv = waitpid(daemon_pid, &stat, 0);
if ((rv == daemon_pid && WIFEXITED(stat)) ||
(rv == -1 && errno != EINTR)) {
exit(1);
}
}
}
if (sighold(SIGUSR1) == -1) {
LDMA_ERR("sighold error (%d)", errno);
exit(1);
}
if (sigignore(SIGUSR1) == -1) {
LDMA_ERR("sigignore error (%d)", errno);
exit(1);
}
if (setsid() == -1) {
LDMA_ERR("setsid error (%d)", errno);
exit(1);
}
if (chdir("/") == -1) {
LDMA_ERR("chdir error (%d)", errno);
exit(1);
}
(void) umask(0);
(void) close(STDIN_FILENO);
if (open("/dev/null", O_RDWR) == -1) {
LDMA_ERR("open /dev/null error (%d)", errno);
exit(1);
}
if (dup2(STDIN_FILENO, STDOUT_FILENO) == -1) {
LDMA_ERR("dup2 error (%d)", errno);
exit(1);
}
closefrom(STDERR_FILENO + 1);
openlog(cmdname, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
ldma_daemon = B_TRUE;
}
if (ldma_register_agents() == 0) {
LDMA_ERR("Unable to register any agent");
exit(1);
}
if (!standalone) {
if (kill(getppid(), SIGUSR1) == -1)
exit(1);
}
}
static void
ldma_usage()
{
(void) fprintf(stderr, "usage: %s\n", cmdname);
}
int
main(int argc, char *argv[])
{
int opt;
boolean_t standalone = B_FALSE;
cmdname = basename(argv[0]);
opterr = 0;
while ((opt = getopt(argc, argv, "ds")) != EOF) {
switch (opt) {
case 'd':
ldma_debug = B_TRUE;
break;
case 's':
standalone = B_TRUE;
break;
default:
ldma_usage();
exit(1);
}
}
ldma_start(standalone);
for (;;) {
(void) pause();
}
return (0);
}