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 {