diff options
| author | Elliott Hughes <enh@google.com> | 2016-08-31 14:41:51 -0700 |
|---|---|---|
| committer | Elliott Hughes <enh@google.com> | 2016-08-31 14:44:41 -0700 |
| commit | f39f7f14281c1b98524c740cd8f50905da86cdb9 (patch) | |
| tree | 331c44668bdd18e4c442a6f4700a98b825dc1166 /init | |
| parent | 6a5ed849e832aac7da24fca5b7cf01d7c052f56e (diff) | |
| download | system_core-f39f7f14281c1b98524c740cd8f50905da86cdb9.tar.gz system_core-f39f7f14281c1b98524c740cd8f50905da86cdb9.tar.bz2 system_core-f39f7f14281c1b98524c740cd8f50905da86cdb9.zip | |
Use android::base::Readlink in init.
Bug: http://b/30988271
Change-Id: Ia0000e9dd7883c31ccbd54fc01bf585c3f8b3fa7
Diffstat (limited to 'init')
| -rw-r--r-- | init/devices.cpp | 45 | ||||
| -rw-r--r-- | init/util.cpp | 34 | ||||
| -rw-r--r-- | init/util.h | 2 |
3 files changed, 32 insertions, 49 deletions
diff --git a/init/devices.cpp b/init/devices.cpp index 830b74cb2..bad04aec1 100644 --- a/init/devices.cpp +++ b/init/devices.cpp @@ -14,21 +14,23 @@ * limitations under the License. */ +#include <dirent.h> #include <errno.h> +#include <fcntl.h> #include <fnmatch.h> +#include <libgen.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <sys/socket.h> #include <sys/stat.h> +#include <sys/time.h> #include <sys/types.h> - -#include <fcntl.h> -#include <dirent.h> +#include <sys/un.h> +#include <sys/wait.h> #include <unistd.h> -#include <string.h> -#include <sys/socket.h> -#include <sys/un.h> #include <linux/netlink.h> #include <memory> @@ -39,8 +41,6 @@ #include <selinux/avc.h> #include <private/android_filesystem_config.h> -#include <sys/time.h> -#include <sys/wait.h> #include <android-base/file.h> #include <android-base/stringprintf.h> @@ -541,30 +541,49 @@ static char **get_block_device_symlinks(struct uevent *uevent) return links; } +static void make_link_init(const char* oldpath, const char* newpath) { + const char* slash = strrchr(newpath, '/'); + if (!slash) return; + + if (mkdir_recursive(dirname(newpath), 0755)) { + PLOG(ERROR) << "Failed to create directory " << dirname(newpath); + } + + if (symlink(oldpath, newpath) && errno != EEXIST) { + PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath; + } +} + +static void remove_link(const char* oldpath, const char* newpath) { + std::string path; + if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath); +} + static void handle_device(const char *action, const char *devpath, const char *path, int block, int major, int minor, char **links) { - int i; - if(!strcmp(action, "add")) { make_device(devpath, path, block, major, minor, (const char **)links); if (links) { - for (i = 0; links[i]; i++) + for (int i = 0; links[i]; i++) { make_link_init(devpath, links[i]); + } } } if(!strcmp(action, "remove")) { if (links) { - for (i = 0; links[i]; i++) + for (int i = 0; links[i]; i++) { remove_link(devpath, links[i]); + } } unlink(devpath); } if (links) { - for (i = 0; links[i]; i++) + for (int i = 0; links[i]; i++) { free(links[i]); + } free(links); } } diff --git a/init/util.cpp b/init/util.cpp index 10ab1c73c..e451edd72 100644 --- a/init/util.cpp +++ b/init/util.cpp @@ -270,40 +270,6 @@ void sanitize(char *s) } } -void make_link_init(const char *oldpath, const char *newpath) -{ - int ret; - char buf[256]; - const char *slash; - int width; - - slash = strrchr(newpath, '/'); - if (!slash) - return; - width = slash - newpath; - if (width <= 0 || width > (int)sizeof(buf) - 1) - return; - memcpy(buf, newpath, width); - buf[width] = 0; - ret = mkdir_recursive(buf, 0755); - if (ret) PLOG(ERROR) << "Failed to create directory " << buf; - - ret = symlink(oldpath, newpath); - if (ret && errno != EEXIST) PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath; -} - -void remove_link(const char *oldpath, const char *newpath) -{ - char path[256]; - ssize_t ret; - ret = readlink(newpath, path, sizeof(path) - 1); - if (ret <= 0) - return; - path[ret] = 0; - if (!strcmp(path, oldpath)) - unlink(newpath); -} - int wait_for_file(const char *filename, int timeout) { struct stat info; diff --git a/init/util.h b/init/util.h index f020da8c1..5fcbdf070 100644 --- a/init/util.h +++ b/init/util.h @@ -51,8 +51,6 @@ unsigned int decode_uid(const char *s); int mkdir_recursive(const char *pathname, mode_t mode); void sanitize(char *p); -void make_link_init(const char *oldpath, const char *newpath); -void remove_link(const char *oldpath, const char *newpath); int wait_for_file(const char *filename, int timeout); void import_kernel_cmdline(bool in_qemu, const std::function<void(const std::string&, const std::string&, bool)>&); |
