lkml.org 
[lkml]   [2015]   [May]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH 1/2] perf/tools: add read/write buildid dir locks
Date
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 <milos@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


\
 
 \ /
  Last update: 2015-05-14 10:01    [W:1.033 / U:0.336 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site