Protect access to buildid dir by flock to prevent race conditions. This patch adds buildid_dir_read/write_lock/unlock functions.
Signed-off-by: Milos Vyletel <mi...@redhat.com> --- tools/perf/util/build-id.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/build-id.h | 5 +++++ 2 files changed, 61 insertions(+) diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index 61867df..46c41e1 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -536,3 +536,59 @@ bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) return ret; } + +static int buildid_dir_lock(int op) +{ + int lockfd, wait = 30; + const size_t size = PATH_MAX; + char *lockfile = zalloc(size); + + scnprintf(lockfile, size, "%s/.lock", buildid_dir); + + lockfd = open(lockfile, O_RDONLY|O_CREAT, S_IRUSR); + if (lockfd == -1) + goto out; + + while (1) { + if (flock(lockfd, op | LOCK_NB) == 0) + goto out; + else if (wait == 0) { + close(lockfd); + lockfd = -1; + goto out; + } + if (--wait % 10 == 0) + pr_info("Waiting for %s lock to be available\n", buildid_dir); + usleep(100); + } + +out: + free(lockfile); + return lockfd; +} + +static void buildid_dir_unlock(int fd) +{ + flock(fd, LOCK_UN); + close(fd); +} + +void buildid_dir_write_lock(int *fd) +{ + *fd = buildid_dir_lock(LOCK_EX); +} + +void buildid_dir_read_lock(int *fd) +{ + *fd = buildid_dir_lock(LOCK_SH); +} + +void buildid_dir_write_unlock(int fd) +{ + buildid_dir_unlock(fd); +} + +void buildid_dir_read_unlock(int fd) +{ + buildid_dir_unlock(fd); +} diff --git a/tools/perf/util/build-id.h b/tools/perf/util/build-id.h index 8501122..326cd2e 100644 --- a/tools/perf/util/build-id.h +++ b/tools/perf/util/build-id.h @@ -31,4 +31,9 @@ int build_id_cache__add_s(const char *sbuild_id, int build_id_cache__remove_s(const char *sbuild_id); void disable_buildid_cache(void); +void buildid_dir_write_lock(int *); +void buildid_dir_read_lock(int *); +void buildid_dir_write_unlock(int); +void buildid_dir_read_unlock(int); + #endif -- 2.4.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/