mirror of https://gerrit.osmocom.org/libosmocore
libctrl: Add DLCTRL as logging context for the control interface
... and make libctrl code use it
This commit is contained in:
parent
f3c7e85d05
commit
7fd0c830d9
|
@ -67,7 +67,8 @@ void logp(int subsys, const char *file, int line, int cont, const char *format,
|
||||||
#define DLMI -5
|
#define DLMI -5
|
||||||
#define DLMIB -6
|
#define DLMIB -6
|
||||||
#define DLSMS -7
|
#define DLSMS -7
|
||||||
#define OSMO_NUM_DLIB 7
|
#define DLCTRL -8
|
||||||
|
#define OSMO_NUM_DLIB 8
|
||||||
|
|
||||||
struct log_category {
|
struct log_category {
|
||||||
uint8_t loglevel;
|
uint8_t loglevel;
|
||||||
|
|
|
@ -188,7 +188,7 @@ static void create_cmd_struct(struct ctrl_cmd_struct *cmd, const char *name)
|
||||||
for (cur = name, word = NULL; cur[0] != '\0'; ++cur) {
|
for (cur = name, word = NULL; cur[0] != '\0'; ++cur) {
|
||||||
/* warn about optionals */
|
/* warn about optionals */
|
||||||
if (cur[0] == '(' || cur[0] == ')' || cur[0] == '|') {
|
if (cur[0] == '(' || cur[0] == ')' || cur[0] == '|') {
|
||||||
LOGP(DCTRL, LOGL_ERROR,
|
LOGP(DLCTRL, LOGL_ERROR,
|
||||||
"Optionals are not supported in '%s'\n", name);
|
"Optionals are not supported in '%s'\n", name);
|
||||||
goto failure;
|
goto failure;
|
||||||
}
|
}
|
||||||
|
@ -223,7 +223,7 @@ int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd)
|
||||||
if (!cmds_vec) {
|
if (!cmds_vec) {
|
||||||
cmds_vec = vector_init(5);
|
cmds_vec = vector_init(5);
|
||||||
if (!cmds_vec) {
|
if (!cmds_vec) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "vector_init failed.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "vector_init failed.\n");
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
vector_set_index(ctrl_node_vec, node, cmds_vec);
|
vector_set_index(ctrl_node_vec, node, cmds_vec);
|
||||||
|
@ -291,7 +291,7 @@ struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
|
||||||
|
|
||||||
cmd = talloc_zero(ctx, struct ctrl_cmd);
|
cmd = talloc_zero(ctx, struct ctrl_cmd);
|
||||||
if (!cmd) {
|
if (!cmd) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Failed to allocate.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,11 +333,11 @@ struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
|
||||||
if (!var) {
|
if (!var) {
|
||||||
cmd->type = CTRL_TYPE_ERROR;
|
cmd->type = CTRL_TYPE_ERROR;
|
||||||
cmd->reply = "GET incomplete";
|
cmd->reply = "GET incomplete";
|
||||||
LOGP(DCTRL, LOGL_NOTICE, "GET Command incomplete\n");
|
LOGP(DLCTRL, LOGL_NOTICE, "GET Command incomplete\n");
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
cmd->variable = talloc_strdup(cmd, var);
|
cmd->variable = talloc_strdup(cmd, var);
|
||||||
LOGP(DCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
|
LOGP(DLCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
|
||||||
break;
|
break;
|
||||||
case CTRL_TYPE_SET:
|
case CTRL_TYPE_SET:
|
||||||
var = strtok_r(NULL, " ", &saveptr);
|
var = strtok_r(NULL, " ", &saveptr);
|
||||||
|
@ -345,14 +345,14 @@ struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
|
||||||
if (!var || !val) {
|
if (!var || !val) {
|
||||||
cmd->type = CTRL_TYPE_ERROR;
|
cmd->type = CTRL_TYPE_ERROR;
|
||||||
cmd->reply = "SET incomplete";
|
cmd->reply = "SET incomplete";
|
||||||
LOGP(DCTRL, LOGL_NOTICE, "SET Command incomplete\n");
|
LOGP(DLCTRL, LOGL_NOTICE, "SET Command incomplete\n");
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
cmd->variable = talloc_strdup(cmd, var);
|
cmd->variable = talloc_strdup(cmd, var);
|
||||||
cmd->value = talloc_strdup(cmd, val);
|
cmd->value = talloc_strdup(cmd, val);
|
||||||
if (!cmd->variable || !cmd->value)
|
if (!cmd->variable || !cmd->value)
|
||||||
goto oom;
|
goto oom;
|
||||||
LOGP(DCTRL, LOGL_DEBUG, "Command: SET %s = %s\n", cmd->variable, cmd->value);
|
LOGP(DLCTRL, LOGL_DEBUG, "Command: SET %s = %s\n", cmd->variable, cmd->value);
|
||||||
break;
|
break;
|
||||||
case CTRL_TYPE_GET_REPLY:
|
case CTRL_TYPE_GET_REPLY:
|
||||||
case CTRL_TYPE_SET_REPLY:
|
case CTRL_TYPE_SET_REPLY:
|
||||||
|
@ -362,14 +362,14 @@ struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
|
||||||
if (!var || !val) {
|
if (!var || !val) {
|
||||||
cmd->type = CTRL_TYPE_ERROR;
|
cmd->type = CTRL_TYPE_ERROR;
|
||||||
cmd->reply = "Trap/Reply incomplete";
|
cmd->reply = "Trap/Reply incomplete";
|
||||||
LOGP(DCTRL, LOGL_NOTICE, "Trap/Reply incomplete\n");
|
LOGP(DLCTRL, LOGL_NOTICE, "Trap/Reply incomplete\n");
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
cmd->variable = talloc_strdup(cmd, var);
|
cmd->variable = talloc_strdup(cmd, var);
|
||||||
cmd->reply = talloc_strdup(cmd, val);
|
cmd->reply = talloc_strdup(cmd, val);
|
||||||
if (!cmd->variable || !cmd->reply)
|
if (!cmd->variable || !cmd->reply)
|
||||||
goto oom;
|
goto oom;
|
||||||
LOGP(DCTRL, LOGL_DEBUG, "Command: TRAP/REPLY %s: %s\n", cmd->variable, cmd->reply);
|
LOGP(DLCTRL, LOGL_DEBUG, "Command: TRAP/REPLY %s: %s\n", cmd->variable, cmd->reply);
|
||||||
break;
|
break;
|
||||||
case CTRL_TYPE_ERROR:
|
case CTRL_TYPE_ERROR:
|
||||||
var = strtok_r(NULL, "\0", &saveptr);
|
var = strtok_r(NULL, "\0", &saveptr);
|
||||||
|
@ -380,7 +380,7 @@ struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
|
||||||
cmd->reply = talloc_strdup(cmd, var);
|
cmd->reply = talloc_strdup(cmd, var);
|
||||||
if (!cmd->reply)
|
if (!cmd->reply)
|
||||||
goto oom;
|
goto oom;
|
||||||
LOGP(DCTRL, LOGL_DEBUG, "Command: ERROR %s\n", cmd->reply);
|
LOGP(DLCTRL, LOGL_DEBUG, "Command: ERROR %s\n", cmd->reply);
|
||||||
break;
|
break;
|
||||||
case CTRL_TYPE_UNKNOWN:
|
case CTRL_TYPE_UNKNOWN:
|
||||||
default:
|
default:
|
||||||
|
@ -420,7 +420,7 @@ struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
|
||||||
|
|
||||||
tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
|
tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -435,7 +435,7 @@ struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
|
||||||
tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
|
tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
|
||||||
cmd->value);
|
cmd->value);
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -452,7 +452,7 @@ struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
|
||||||
tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
|
tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
|
||||||
cmd->reply);
|
cmd->reply);
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -467,7 +467,7 @@ struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
|
||||||
tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id,
|
tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id,
|
||||||
cmd->reply);
|
cmd->reply);
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -476,7 +476,7 @@ struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
|
||||||
talloc_free(tmp);
|
talloc_free(tmp);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
LOGP(DCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
|
LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
|
||||||
goto err;
|
goto err;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,7 +77,7 @@ int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd)
|
||||||
|
|
||||||
msg = ctrl_cmd_make(cmd);
|
msg = ctrl_cmd_make(cmd);
|
||||||
if (!msg) {
|
if (!msg) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Could not generate msg\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Could not generate msg\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd)
|
||||||
|
|
||||||
ret = osmo_wqueue_enqueue(queue, msg);
|
ret = osmo_wqueue_enqueue(queue, msg);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Failed to enqueue the command.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to enqueue the command.\n");
|
||||||
msgb_free(msg);
|
msgb_free(msg);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -136,27 +136,27 @@ static int handle_control_read(struct osmo_fd * bfd)
|
||||||
if (ret == -EAGAIN)
|
if (ret == -EAGAIN)
|
||||||
return 0;
|
return 0;
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
LOGP(DCTRL, LOGL_INFO, "The control connection was closed\n");
|
LOGP(DLCTRL, LOGL_INFO, "The control connection was closed\n");
|
||||||
else
|
else
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Failed to parse ip access message: %d\n", ret);
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to parse ip access message: %d\n", ret);
|
||||||
|
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg->len < sizeof(*iph) + sizeof(*iph_ext)) {
|
if (msg->len < sizeof(*iph) + sizeof(*iph_ext)) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "The message is too short.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "The message is too short.\n");
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
iph = (struct ipaccess_head *) msg->data;
|
iph = (struct ipaccess_head *) msg->data;
|
||||||
if (iph->proto != IPAC_PROTO_OSMO) {
|
if (iph->proto != IPAC_PROTO_OSMO) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Protocol mismatch. We got 0x%x\n", iph->proto);
|
LOGP(DLCTRL, LOGL_ERROR, "Protocol mismatch. We got 0x%x\n", iph->proto);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
iph_ext = (struct ipaccess_head_ext *) iph->data;
|
iph_ext = (struct ipaccess_head_ext *) iph->data;
|
||||||
if (iph_ext->proto != IPAC_PROTO_EXT_CTRL) {
|
if (iph_ext->proto != IPAC_PROTO_EXT_CTRL) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Extended protocol mismatch. We got 0x%x\n", iph_ext->proto);
|
LOGP(DLCTRL, LOGL_ERROR, "Extended protocol mismatch. We got 0x%x\n", iph_ext->proto);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +174,7 @@ static int handle_control_read(struct osmo_fd * bfd)
|
||||||
cmd = talloc_zero(ccon, struct ctrl_cmd);
|
cmd = talloc_zero(ccon, struct ctrl_cmd);
|
||||||
if (!cmd)
|
if (!cmd)
|
||||||
goto err;
|
goto err;
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Command parser error.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Command parser error.\n");
|
||||||
cmd->type = CTRL_TYPE_ERROR;
|
cmd->type = CTRL_TYPE_ERROR;
|
||||||
cmd->id = "err";
|
cmd->id = "err";
|
||||||
cmd->reply = "Command parser error.";
|
cmd->reply = "Command parser error.";
|
||||||
|
@ -197,7 +197,7 @@ static int control_write_cb(struct osmo_fd *bfd, struct msgb *msg)
|
||||||
|
|
||||||
rc = write(bfd->fd, msg->data, msg->len);
|
rc = write(bfd->fd, msg->data, msg->len);
|
||||||
if (rc != msg->len)
|
if (rc != msg->len)
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Failed to write message to the control connection.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to write message to the control connection.\n");
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
@ -232,19 +232,19 @@ static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
|
||||||
perror("accept");
|
perror("accept");
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
LOGP(DCTRL, LOGL_INFO, "accept()ed new control connection from %s\n",
|
LOGP(DLCTRL, LOGL_INFO, "accept()ed new control connection from %s\n",
|
||||||
inet_ntoa(sa.sin_addr));
|
inet_ntoa(sa.sin_addr));
|
||||||
|
|
||||||
on = 1;
|
on = 1;
|
||||||
ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
|
ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
|
||||||
close(fd);
|
close(fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
ccon = ctrl_connection_alloc(listen_bfd->data);
|
ccon = ctrl_connection_alloc(listen_bfd->data);
|
||||||
if (!ccon) {
|
if (!ccon) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Failed to allocate.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -258,7 +258,7 @@ static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
|
||||||
|
|
||||||
ret = osmo_fd_register(&ccon->write_queue.bfd);
|
ret = osmo_fd_register(&ccon->write_queue.bfd);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOGP(DCTRL, LOGL_ERROR, "Could not register FD.\n");
|
LOGP(DLCTRL, LOGL_ERROR, "Could not register FD.\n");
|
||||||
close(ccon->write_queue.bfd.fd);
|
close(ccon->write_queue.bfd.fd);
|
||||||
talloc_free(ccon);
|
talloc_free(ccon);
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,6 +106,11 @@ static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
|
||||||
.enabled = 1, .loglevel = LOGL_NOTICE,
|
.enabled = 1, .loglevel = LOGL_NOTICE,
|
||||||
.color = "\033[1;38m",
|
.color = "\033[1;38m",
|
||||||
},
|
},
|
||||||
|
[INT2IDX(DLCTRL)] = {
|
||||||
|
.name = "DLCTRL",
|
||||||
|
.description = "Control Interface",
|
||||||
|
.enabled = 1, .loglevel = LOGL_NOTICE,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/*! \brief descriptive string for each log level */
|
/*! \brief descriptive string for each log level */
|
||||||
|
|
Loading…
Reference in New Issue