summaryrefslogtreecommitdiffstats
path: root/uncrypt/uncrypt.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'uncrypt/uncrypt.cpp')
-rw-r--r--uncrypt/uncrypt.cpp82
1 files changed, 40 insertions, 42 deletions
diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp
index d7105a01f..5697712aa 100644
--- a/uncrypt/uncrypt.cpp
+++ b/uncrypt/uncrypt.cpp
@@ -109,6 +109,7 @@
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
#include <bootloader_message_writer.h>
#include <cutils/android_reboot.h>
#include <cutils/properties.h>
@@ -118,8 +119,6 @@
#define LOG_TAG "uncrypt"
#include <log/log.h>
-#include "unique_fd.h"
-
#define WINDOW_SIZE 5
// uncrypt provides three services: SETUP_BCB, CLEAR_BCB and UNCRYPT.
@@ -237,8 +236,9 @@ static int produce_block_map(const char* path, const char* map_file, const char*
return -1;
}
std::string tmp_map_file = std::string(map_file) + ".tmp";
- unique_fd mapfd(open(tmp_map_file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR));
- if (!mapfd) {
+ android::base::unique_fd mapfd(open(tmp_map_file.c_str(),
+ O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR));
+ if (mapfd == -1) {
ALOGE("failed to open %s: %s\n", tmp_map_file.c_str(), strerror(errno));
return -1;
}
@@ -262,9 +262,10 @@ static int produce_block_map(const char* path, const char* map_file, const char*
std::vector<int> ranges;
- std::string s = android::base::StringPrintf("%s\n%" PRId64 " %ld\n",
- blk_dev, sb.st_size, static_cast<long>(sb.st_blksize));
- if (!android::base::WriteStringToFd(s, mapfd.get())) {
+ std::string s = android::base::StringPrintf("%s\n%" PRId64 " %" PRId64 "\n",
+ blk_dev, static_cast<int64_t>(sb.st_size),
+ static_cast<int64_t>(sb.st_blksize));
+ if (!android::base::WriteStringToFd(s, mapfd)) {
ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno));
return -1;
}
@@ -276,16 +277,16 @@ static int produce_block_map(const char* path, const char* map_file, const char*
int head_block = 0;
int head = 0, tail = 0;
- unique_fd fd(open(path, O_RDONLY));
- if (!fd) {
+ android::base::unique_fd fd(open(path, O_RDONLY));
+ if (fd == -1) {
ALOGE("failed to open %s for reading: %s", path, strerror(errno));
return -1;
}
- unique_fd wfd(-1);
+ android::base::unique_fd wfd;
if (encrypted) {
- wfd = open(blk_dev, O_WRONLY);
- if (!wfd) {
+ wfd.reset(open(blk_dev, O_WRONLY));
+ if (wfd == -1) {
ALOGE("failed to open fd for writing: %s", strerror(errno));
return -1;
}
@@ -304,14 +305,14 @@ static int produce_block_map(const char* path, const char* map_file, const char*
if ((tail+1) % WINDOW_SIZE == head) {
// write out head buffer
int block = head_block;
- if (ioctl(fd.get(), FIBMAP, &block) != 0) {
+ if (ioctl(fd, FIBMAP, &block) != 0) {
ALOGE("failed to find block %d", head_block);
return -1;
}
add_block_to_ranges(ranges, block);
if (encrypted) {
- if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd.get(),
- static_cast<off64_t>(sb.st_blksize) * block) != 0) {
+ if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd,
+ static_cast<off64_t>(sb.st_blksize) * block) != 0) {
return -1;
}
}
@@ -323,7 +324,7 @@ static int produce_block_map(const char* path, const char* map_file, const char*
if (encrypted) {
size_t to_read = static_cast<size_t>(
std::min(static_cast<off64_t>(sb.st_blksize), sb.st_size - pos));
- if (!android::base::ReadFully(fd.get(), buffers[tail].data(), to_read)) {
+ if (!android::base::ReadFully(fd, buffers[tail].data(), to_read)) {
ALOGE("failed to read: %s", strerror(errno));
return -1;
}
@@ -340,14 +341,14 @@ static int produce_block_map(const char* path, const char* map_file, const char*
while (head != tail) {
// write out head buffer
int block = head_block;
- if (ioctl(fd.get(), FIBMAP, &block) != 0) {
+ if (ioctl(fd, FIBMAP, &block) != 0) {
ALOGE("failed to find block %d", head_block);
return -1;
}
add_block_to_ranges(ranges, block);
if (encrypted) {
- if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd.get(),
- static_cast<off64_t>(sb.st_blksize) * block) != 0) {
+ if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd,
+ static_cast<off64_t>(sb.st_blksize) * block) != 0) {
return -1;
}
}
@@ -356,38 +357,36 @@ static int produce_block_map(const char* path, const char* map_file, const char*
}
if (!android::base::WriteStringToFd(
- android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd.get())) {
+ android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd)) {
ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno));
return -1;
}
for (size_t i = 0; i < ranges.size(); i += 2) {
if (!android::base::WriteStringToFd(
- android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd.get())) {
+ android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd)) {
ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno));
return -1;
}
}
- if (fsync(mapfd.get()) == -1) {
+ if (fsync(mapfd) == -1) {
ALOGE("failed to fsync \"%s\": %s", tmp_map_file.c_str(), strerror(errno));
return -1;
}
- if (close(mapfd.get()) == -1) {
+ if (close(mapfd.release()) == -1) {
ALOGE("failed to close %s: %s", tmp_map_file.c_str(), strerror(errno));
return -1;
}
- mapfd = -1;
if (encrypted) {
- if (fsync(wfd.get()) == -1) {
+ if (fsync(wfd) == -1) {
ALOGE("failed to fsync \"%s\": %s", blk_dev, strerror(errno));
return -1;
}
- if (close(wfd.get()) == -1) {
+ if (close(wfd.release()) == -1) {
ALOGE("failed to close %s: %s", blk_dev, strerror(errno));
return -1;
}
- wfd = -1;
}
if (rename(tmp_map_file.c_str(), map_file) == -1) {
@@ -397,20 +396,19 @@ static int produce_block_map(const char* path, const char* map_file, const char*
// Sync dir to make rename() result written to disk.
std::string file_name = map_file;
std::string dir_name = dirname(&file_name[0]);
- unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY));
- if (!dfd) {
+ android::base::unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY));
+ if (dfd == -1) {
ALOGE("failed to open dir %s: %s", dir_name.c_str(), strerror(errno));
return -1;
}
- if (fsync(dfd.get()) == -1) {
+ if (fsync(dfd) == -1) {
ALOGE("failed to fsync %s: %s", dir_name.c_str(), strerror(errno));
return -1;
}
- if (close(dfd.get()) == -1) {
+ if (close(dfd.release()) == -1) {
ALOGE("failed to close %s: %s", dir_name.c_str(), strerror(errno));
return -1;
}
- dfd = -1;
return 0;
}
@@ -549,20 +547,20 @@ int main(int argc, char** argv) {
// c3. The socket is created by init when starting the service. uncrypt
// will use the socket to communicate with its caller.
- unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str()));
- if (!service_socket) {
+ android::base::unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str()));
+ if (service_socket == -1) {
ALOGE("failed to open socket \"%s\": %s", UNCRYPT_SOCKET.c_str(), strerror(errno));
return 1;
}
- fcntl(service_socket.get(), F_SETFD, FD_CLOEXEC);
+ fcntl(service_socket, F_SETFD, FD_CLOEXEC);
- if (listen(service_socket.get(), 1) == -1) {
+ if (listen(service_socket, 1) == -1) {
ALOGE("failed to listen on socket %d: %s", service_socket.get(), strerror(errno));
return 1;
}
- unique_fd socket_fd(accept4(service_socket.get(), nullptr, nullptr, SOCK_CLOEXEC));
- if (!socket_fd) {
+ android::base::unique_fd socket_fd(accept4(service_socket, nullptr, nullptr, SOCK_CLOEXEC));
+ if (socket_fd == -1) {
ALOGE("failed to accept on socket %d: %s", service_socket.get(), strerror(errno));
return 1;
}
@@ -570,13 +568,13 @@ int main(int argc, char** argv) {
bool success = false;
switch (action) {
case UNCRYPT:
- success = uncrypt_wrapper(input_path, map_file, socket_fd.get());
+ success = uncrypt_wrapper(input_path, map_file, socket_fd);
break;
case SETUP_BCB:
- success = setup_bcb(socket_fd.get());
+ success = setup_bcb(socket_fd);
break;
case CLEAR_BCB:
- success = clear_bcb(socket_fd.get());
+ success = clear_bcb(socket_fd);
break;
default: // Should never happen.
ALOGE("Invalid uncrypt action code: %d", action);
@@ -587,7 +585,7 @@ int main(int argc, char** argv) {
// ensure the client to receive the last status code before the socket gets
// destroyed.
int code;
- if (android::base::ReadFully(socket_fd.get(), &code, 4)) {
+ if (android::base::ReadFully(socket_fd, &code, 4)) {
ALOGI(" received %d, exiting now", code);
} else {
ALOGE("failed to read the code: %s", strerror(errno));