diff options
| author | 2016-10-13 14:26:07 +0000 | |
|---|---|---|
| committer | 2016-10-13 14:26:07 +0000 | |
| commit | c68597e6719abe92c349b03372578fcd9bba7e6c (patch) | |
| tree | f136f773afec1a1eb99a7095e0212d03be0bdc6a /cmds/idmap/scan.cpp | |
| parent | 4e7407a80ec46ec4c132af088f779d519b33041a (diff) | |
| parent | 1b9303e8f09af3081223df5ed8a90259d339ebc0 (diff) | |
Merge "RRO: Synchronize access to overlays.list" am: 78382db820
am: 1b9303e8f0
Change-Id: I3d6b143ffc5c08249e9a0be35c39b0e89b364c20
Diffstat (limited to 'cmds/idmap/scan.cpp')
| -rw-r--r-- | cmds/idmap/scan.cpp | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/cmds/idmap/scan.cpp b/cmds/idmap/scan.cpp index 6d30f0d0ce34..ab6adfb9475f 100644 --- a/cmds/idmap/scan.cpp +++ b/cmds/idmap/scan.cpp @@ -1,5 +1,6 @@ #include <dirent.h> #include <inttypes.h> +#include <sys/file.h> #include <sys/stat.h> #include "idmap.h" @@ -35,16 +36,31 @@ namespace { bool writePackagesList(const char *filename, const SortedVector<Overlay>& overlayVector) { - FILE* fout = fopen(filename, "w"); + // the file is opened for appending so that it doesn't get truncated + // before we can guarantee mutual exclusion via the flock + FILE* fout = fopen(filename, "a"); if (fout == NULL) { return false; } + if (TEMP_FAILURE_RETRY(flock(fileno(fout), LOCK_EX)) != 0) { + fclose(fout); + return false; + } + + if (TEMP_FAILURE_RETRY(ftruncate(fileno(fout), 0)) != 0) { + TEMP_FAILURE_RETRY(flock(fileno(fout), LOCK_UN)); + fclose(fout); + return false; + } + for (size_t i = 0; i < overlayVector.size(); ++i) { const Overlay& overlay = overlayVector[i]; fprintf(fout, "%s %s\n", overlay.apk_path.string(), overlay.idmap_path.string()); } + TEMP_FAILURE_RETRY(fflush(fout)); + TEMP_FAILURE_RETRY(flock(fileno(fout), LOCK_UN)); fclose(fout); // Make file world readable since Zygote (running as root) will read @@ -171,9 +187,6 @@ int idmap_scan(const char *target_package_name, const char *target_apk_path, { String8 filename = String8(idmap_dir); filename.appendPath("overlays.list"); - if (unlink(filename.string()) != 0 && errno != ENOENT) { - return EXIT_FAILURE; - } SortedVector<Overlay> overlayVector; const size_t N = overlay_dirs->size(); |