summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEunji, Lee <eunjieji.lee@samsung.com>2016-05-10 19:43:43 +0900
committerEunji, Lee <eunjieji.lee@samsung.com>2016-05-10 20:04:02 +0900
commite66c90a06cc1820b8562a0f531fa31ad6e64a830 (patch)
tree758a9812a784f6a3e7361c2a3b13b44c2b21f6e4
parentbdf42e18444401bda3a62558503ee899b1619fa7 (diff)
downloadttrace-e66c90a06cc1820b8562a0f531fa31ad6e64a830.tar.gz
ttrace-e66c90a06cc1820b8562a0f531fa31ad6e64a830.tar.bz2
ttrace-e66c90a06cc1820b8562a0f531fa31ad6e64a830.zip
increase default buf size to 1024 and add protection code for changes of _SC_GETGR_R_SIZE_MAX
Change-Id: I6abbe0da54442eb1172b0d611700b51d83c62e00
-rwxr-xr-xsrc/atrace/atrace.cpp38
1 files changed, 34 insertions, 4 deletions
diff --git a/src/atrace/atrace.cpp b/src/atrace/atrace.cpp
index 12f005f..fb2ec1f 100755
--- a/src/atrace/atrace.cpp
+++ b/src/atrace/atrace.cpp
@@ -43,6 +43,7 @@
#define BACKUP_TRACE "/tmp/trace.backup"
#define BOOTUP_TRACE "/etc/ttrace.conf"
+#define DEF_GR_SIZE 1024
#else
#include <binder/IBinder.h>
#include <binder/IServiceManager.h>
@@ -507,11 +508,40 @@ static bool setTagsProperty(uint64_t tags)
//atrace "--init_exec" mode
if(g_init_exec) {
- char buf[256]; //insufficient buffer size can cause "ERANGE" as a result of getgrnam_r
- if(0 != getgrnam_r(TTRACE_GROUP_NAME, &group_dev, buf, sizeof(buf), &group_ptr))
+ size_t bufSize = DEF_GR_SIZE;
+ char buf[DEF_GR_SIZE];
+ int ret = 0;
+ ret = getgrnam_r(TTRACE_GROUP_NAME, &group_dev, buf, bufSize, &group_ptr);
+
+ if (ret != 0 && ret != ERANGE)
+ {
+ fprintf(stderr, "Fail to group[%s] info: %s(%d)\n", TTRACE_GROUP_NAME, strerror_r(errno, str_error, sizeof(str_error)), errno);
+ return false;
+ }
+
+ bool isInvalid = false;
+ while(ret == ERANGE)
{
- fprintf(stderr, "Fail to group[%s] info: %s(%d)\n", TTRACE_GROUP_NAME, strerror_r(errno, str_error, sizeof(str_error)), errno);
- return false;
+ long int tmpSize = -1;
+
+ if(!isInvalid)
+ tmpSize = sysconf(_SC_GETGR_R_SIZE_MAX);
+
+ if (tmpSize == -1)
+ {
+ bufSize *= 2;
+ }
+ else bufSize = (size_t) tmpSize;
+
+ char *dynbuf = (char *) malloc(bufSize);
+ if(dynbuf == NULL)
+ {
+ fprintf(stderr, "Fail to allocate buffer for group[%s]: %s(%d)\n", TTRACE_GROUP_NAME, strerror_r(errno, str_error, sizeof(str_error)), errno);
+ return false;
+ }
+ ret = getgrnam_r(TTRACE_GROUP_NAME, &group_dev, dynbuf, bufSize, &group_ptr);
+ if(ret == ERANGE) isInvalid = true;
+ free(dynbuf);
}
fd = open("/tmp/tmp_tag", O_CREAT | O_RDWR | O_CLOEXEC, 0666);