diff options
| author | Colin Cross <ccross@android.com> | 2013-09-18 16:09:13 -0700 |
|---|---|---|
| committer | Android Git Automerger <android-git-automerger@android.com> | 2013-09-18 16:09:13 -0700 |
| commit | 7a060eddce1fe51553371b1c499f1dbf54388d0c (patch) | |
| tree | d2976f13c48b19476282ab479d2fb44023759fde /init | |
| parent | 126cdc2c4b135156ffb2a4414ff67af2b22b2283 (diff) | |
| parent | 54aad026616148164ff6ceda7508a4e5a04ec569 (diff) | |
| download | system_core-7a060eddce1fe51553371b1c499f1dbf54388d0c.zip system_core-7a060eddce1fe51553371b1c499f1dbf54388d0c.tar.gz system_core-7a060eddce1fe51553371b1c499f1dbf54388d0c.tar.bz2 | |
am 54aad026: am 8dc82eea: am 36b39a97: Merge "init: Fix get_hardware_name() to cope with long /proc/cpuinfo output"
* commit '54aad026616148164ff6ceda7508a4e5a04ec569':
init: Fix get_hardware_name() to cope with long /proc/cpuinfo output
Diffstat (limited to 'init')
| -rw-r--r-- | init/util.c | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/init/util.c b/init/util.c index 078b99b..9aaa77d 100644 --- a/init/util.c +++ b/init/util.c @@ -405,7 +405,9 @@ void open_devnull_stdio(void) void get_hardware_name(char *hardware, unsigned int *revision) { - char data[1024]; + const char *cpuinfo = "/proc/cpuinfo"; + char *data = NULL; + size_t len = 0, limit = 1024; int fd, n; char *x, *hw, *rev; @@ -413,14 +415,32 @@ void get_hardware_name(char *hardware, unsigned int *revision) if (hardware[0]) return; - fd = open("/proc/cpuinfo", O_RDONLY); + fd = open(cpuinfo, O_RDONLY); if (fd < 0) return; - n = read(fd, data, 1023); - close(fd); - if (n < 0) return; + for (;;) { + x = realloc(data, limit); + if (!x) { + ERROR("Failed to allocate memory to read %s\n", cpuinfo); + goto done; + } + data = x; + + n = read(fd, data + len, limit - len); + if (n < 0) { + ERROR("Failed reading %s: %s (%d)\n", cpuinfo, strerror(errno), errno); + goto done; + } + len += n; + + if (len < limit) + break; + + /* We filled the buffer, so increase size and loop to read more */ + limit *= 2; + } - data[n] = 0; + data[len] = 0; hw = strstr(data, "\nHardware"); rev = strstr(data, "\nRevision"); @@ -445,6 +465,10 @@ void get_hardware_name(char *hardware, unsigned int *revision) *revision = strtoul(x + 2, 0, 16); } } + +done: + close(fd); + free(data); } void import_kernel_cmdline(int in_qemu, |
