[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1431588493-1474-2-git-send-email-milos@redhat.com>
Date: Thu, 14 May 2015 09:28:10 +0200
From: Milos Vyletel <milos@...hat.com>
To: Peter Zijlstra <a.p.zijlstra@...llo.nl>,
Paul Mackerras <paulus@...ba.org>,
Ingo Molnar <mingo@...hat.com>,
Arnaldo Carvalho de Melo <acme@...nel.org>,
Masami Hiramatsu <masami.hiramatsu.pt@...achi.com>,
Namhyung Kim <namhyung@...nel.org>,
Milos Vyletel <milos@...hat.com>, Jiri Olsa <jolsa@...nel.org>,
He Kuang <hekuang@...wei.com>,
Adrian Hunter <adrian.hunter@...el.com>,
linux-kernel@...r.kernel.org (open list:PERFORMANCE EVENT...)
Subject: [PATCH 1/2] perf/tools: add read/write buildid dir locks
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@...hat.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 majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists