Impose log length restrictions
For stm_log() (strings) logging impose a maximum of 1024 bytes of
user data (forced truncation and NULL termination). For stm_logbin()
(binary) logging split logs larger than 2048 bytes of user data
into individual log packets of up to 2KB of user data per log.
Change-Id: I0be9a1788e54f41a3d675552cb2f81b79a0e5ab8
diff --git a/stm-log.c b/stm-log.c
index 90e93ea..2427d40 100644
--- a/stm-log.c
+++ b/stm-log.c
@@ -45,6 +45,9 @@
#define STM_LOG_MAGIC_0 0xf0
#define STM_LOG_MAGIC_1 0xf1
+#define STM_LOG_STR_MAX 1024
+#define STM_LOG_BIN_MAX 2048
+
static uint8_t dflt_stm_entity_id = OST_ENTITY_NONE;
static uint8_t dflt_stm_proto_id = 0;
static uint32_t dflt_stm_options = STM_OPTION_TIMESTAMPED;
@@ -80,28 +83,35 @@
#endif
}
-int STMLOG_WRITE(int len, void *data) {
+static int STMLOG_WRITE(int len, void *data, int *pfd) {
int fd;
int rc = -1;
- fd = open(STM_LOG_DEV, O_WRONLY);
- if (fd >= 0) {
+
+ if (!pfd || (pfd && *pfd == -1))
+ fd = open(STM_LOG_DEV, O_WRONLY);
+ else
+ fd = *pfd;
+
+ if (fd != -1) {
do {
rc = write(fd, data, len);
if (rc == -1)
break;
len -= rc;
} while (len > 0);
- close(fd);
+
+ if (pfd)
+ *pfd = fd;
+ else
+ close(fd);
}
-#ifdef DEBUG
- ALOGE("Failed to write to stm log");
-#endif
return rc;
}
void stm_log(const char *format, ...)
{
va_list ap;
+
va_start(ap, format);
stm_log_ex(dflt_stm_entity_id,
dflt_stm_proto_id,
@@ -125,15 +135,18 @@
uint32_t options,
const char *format, ...)
{
- int length;
- char *buf;
+ int length;
+ char *buf;
stmlog_t *log;
- va_list ap;
+ va_list ap;
va_start(ap, format);
length = vsnprintf(NULL, 0, format, ap);
if (length >= 0) {
length++;
+ if (length > STM_LOG_STR_MAX)
+ length = STM_LOG_STR_MAX;
+
buf = (char *)malloc(sizeof(stmlog_t) + length);
if (buf) {
log = (stmlog_t *)buf;
@@ -143,7 +156,7 @@
log->proto = proto_id;
log->options = options;
vsnprintf(buf + sizeof(stmlog_t), length, format, ap);
- STMLOG_WRITE(sizeof(stmlog_t) + length, buf);
+ STMLOG_WRITE(sizeof(stmlog_t) + length, buf, NULL);
free(buf);
}
#ifdef DEBUG
@@ -161,9 +174,20 @@
int length,
void *data)
{
+ uint8_t *usrdata = (uint8_t *)data;
uint8_t *buf;
stmlog_t *stm;
- buf = (uint8_t *)malloc(sizeof(stmlog_t) + length);
+ int pktlen;
+ int fd = -1;
+
+ if (length < 0)
+ return;
+ else if (length > STM_LOG_BIN_MAX)
+ pktlen = STM_LOG_BIN_MAX;
+ else
+ pktlen = length;
+
+ buf = (uint8_t *)malloc(sizeof(stmlog_t) + pktlen);
if (buf) {
stm = (stmlog_t *)buf;
stm->magic[0] = STM_LOG_MAGIC_0;
@@ -171,9 +195,19 @@
stm->entity = entity_id;
stm->proto = proto_id;
stm->options = options;
- memcpy(buf + sizeof(stmlog_t), data, length);
- STMLOG_WRITE(sizeof(stmlog_t) + length, buf);
+
+ do {
+ memcpy(buf + sizeof(stmlog_t), usrdata, pktlen);
+ STMLOG_WRITE(sizeof(stmlog_t) + pktlen, buf, &fd);
+ usrdata += pktlen;
+ length -= pktlen;
+ if (length < pktlen)
+ pktlen = length;
+ } while (length > 0);
+
free(buf);
+ if (fd != -1)
+ close(fd);
}
#ifdef DEBUG
else {
diff --git a/stm-log.txt b/stm-log.txt
index 78a51f4..c229aa7 100644
--- a/stm-log.txt
+++ b/stm-log.txt
@@ -73,7 +73,7 @@
parameters are valid.
Parameters:
- Refer to man pages for printf() for details.
+ Same parameters as printf(). Refer to man pages for printf() for details.
Examples:
stm_log("event X");
@@ -81,7 +81,12 @@
3.2) stm_logbin(int length, void *data)
Description:
- Generate a binary via STM using the default entity/protocol ID and options.
+ Generate a binary log via STM using the default entity/protocol ID and
+ options.
+
+ Parameters:
+ length: Length of binary log data in bytes
+ data: Binary data
3.3) stm_log_ex(uint8_t entity_id,
uint8_t proto_id,
@@ -95,10 +100,33 @@
entity_id: Specify the default entity ID
proto_id: Specify the default protocol ID
options: Specify the default options
- ...: Same as stmlog()
+ ...: Same as stm_log()
3.4) stm_logbin_ex(uint8_t entity_id,
uint8_t proto_id,
uint32_t options,
int length,
void *data)
+ Description:
+ Generate a binary log via STM using the specified entity/protocol ID and
+ options.
+
+ Parameters:
+ entity_id: Specify the default entity ID
+ proto_id: Specify the default protocol ID
+ options: Specify the default options
+ ...: Same as stm_logbin()
+
+
+4) Limitations:
+~~~~~~~~~~~~~~~
+
+4.1) stm_log() and stm_log_ex()
+ Log strings via stm_log() or stm_log_ex() are automatically truncated to a
+ maximum of 1024 bytes including the terminating NULL character.
+
+4.2) stm_logbin() and stm_logbin_ex()
+ A single log via stm_logbin() or stm_logbin_ex() that is greater than 2048
+ bytes are broken down into individual packets up to 2048 bytes before
+ sending over the underlying STM transport. The header is replicated for
+ each log.