add smack rule
[framework/system/dlog.git] / logutil.c
index d4d5dce..03a5276 100755 (executable)
--- a/logutil.c
+++ b/logutil.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2007 The Android Open Source Project
+ * Copyright (c) 2005-2008, The Android Open Source Project
+ * Copyright (c) 2009-2013, Samsung Electronics Co., Ltd. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -180,9 +181,12 @@ static void processBuffer(struct log_device_t* dev, struct logger_entry *buf)
 
        g_out_byte_count += bytes_written;
 
-    if (g_log_rotate_size_kbytes > 0 && (g_out_byte_count / 1024) >= g_log_rotate_size_kbytes)
-       {
-               rotate_logs();
+       if (g_log_rotate_size_kbytes > 0 && (g_out_byte_count / 1024) >= g_log_rotate_size_kbytes) {
+               if (g_nonblock) {
+                       exit(0);
+               } else {
+                       rotate_logs();
+               }
        }
 
 error:
@@ -281,10 +285,16 @@ static void read_log_lines(struct log_device_t* devices)
                         exit(EXIT_FAILURE);
                     }
                     else if (!ret) {
+                        free(entry);
                         fprintf(stderr, "read: Unexpected EOF!\n");
                         exit(EXIT_FAILURE);
                     }
-
+                    else if (entry->entry.len != ret - sizeof(struct logger_entry)) {
+                        free(entry);
+                        fprintf(stderr, "read: unexpected length. Expected %d, got %d\n",
+                                entry->entry.len, ret - sizeof(struct logger_entry));
+                        exit(EXIT_FAILURE);
+                    }
                     entry->entry.msg[entry->entry.len] = '\0';
 
                     enqueue(dev, entry);
@@ -356,23 +366,23 @@ static int get_log_readable_size(int logfd)
 static void setup_output()
 {
 
-    if (g_output_filename == NULL) {
-        g_outfd = STDOUT_FILENO;
-
-    } else {
-        struct stat statbuf;
-
-        g_outfd = open_logfile (g_output_filename);
+       if (g_output_filename == NULL) {
+               g_outfd = STDOUT_FILENO;
 
-        if (g_outfd < 0) {
-            perror ("couldn't open output file");
-            exit(-1);
-        }
+       } else {
+               struct stat statbuf;
 
-        fstat(g_outfd, &statbuf);
+               g_outfd = open_logfile (g_output_filename);
 
-        g_out_byte_count = statbuf.st_size;
-    }
+               if (g_outfd < 0) {
+                       perror ("couldn't open output file");
+                       exit(-1);
+               }
+               if (fstat(g_outfd, &statbuf) == -1)
+                       g_out_byte_count = 0;
+               else
+                       g_out_byte_count = statbuf.st_size;
+       }
 }
 
 static int set_log_format(const char * formatString)
@@ -583,16 +593,20 @@ int main(int argc, char **argv)
                                                  }
 
                                                  dev = log_devices_new(buf);
-                if (devices) {
-                                       if (log_devices_add_to_tail(devices, dev)) {
-                                               fprintf(stderr, "Open log device %s failed\n", buf);
-                                               exit(-1);
-                                       }
-                } else {
-                                       devices = dev;
-                                       g_dev_count = 1;
-                }
-            }
+                                                 if (dev == NULL) {
+                                                         fprintf(stderr,"Can't add log device: %s\n", buf);
+                                                         exit(-1);
+                                                 }
+                                                 if (devices) {
+                                                         if (log_devices_add_to_tail(devices, dev)) {
+                                                                 fprintf(stderr, "Open log device %s failed\n", buf);
+                                                                 exit(-1);
+                                                         }
+                                                 } else {
+                                                         devices = dev;
+                                                         g_dev_count = 1;
+                                                 }
+                                         }
             break;
 
             case 'f':
@@ -673,13 +687,13 @@ int main(int argc, char **argv)
                        exit(-1);
                }
        }
-
        if (0 == access("/dev/"LOGGER_LOG_APPS, accessmode)) {
                if (log_devices_add_to_tail(devices, log_devices_new("/dev/"LOGGER_LOG_APPS))) {
                        fprintf(stderr,"Can't add log device: %s\n", LOGGER_LOG_APPS);
                        exit(-1);
                }
        }
+
 /*
         // only add this if it's available
        int fd;