Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1897692pxu; Sun, 13 Dec 2020 06:58:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJxYGTO0IpnlFuoF+P1Bm7vFqwP/6ryNkhx/ZMtqp67tJ2iW9gKgU2uZZ29E/B1B1yQexa97 X-Received: by 2002:a17:906:cd14:: with SMTP id oz20mr17799181ejb.99.1607871521382; Sun, 13 Dec 2020 06:58:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607871521; cv=none; d=google.com; s=arc-20160816; b=z5BlG8mbkVsEP0uhi5Py+26Wktk2K/fUnE48b2SLS4EveAhbbFlc8o+ZjdwksDBqJO lEBKLMwfkVuQEH3z6TdO5a4b56jgxnkbV8xGqPuk8vt5l2aAVsgfL7yCq5gzvE2mxJaN uQtFM7zHtJRD+n2uoojJHe8mTgJi2vB8aNAvILQYBzFk16z6pHyJUmEOURpvk41sz0n/ v5P2Bx+anB4zKzAc9sCbi8r1G0wUjLlTEed4zQV73GHzKQj0CvKIweFTvBOZU4pJf4Rt 1BaR8TZSSKZAbZS6U2L/6FiAo8/lIa4PwgKqI0QjvPVHoJ9svzVWKypmKumI7fHmI/uR zuUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=dwBC1U4+iWPz6rxc9kKVC9iMkUd6yoxZco0MMbimxaY=; b=ogwYUQ0mx4KH29vyL0sGrRUThVrx9AvYyojAGLGhYmenYfXQ6WeZ/REuoWwJVLnxkw ti2eKoVkK2nr7Btvkx2fahEarvSBxchWTk6kKR1BHr1J55H9c+fCvdATOe3SqwLXH8hy g15UfoNBoDSVWLJA3Y3amGPKsOkgiUaTgtMI6wXY8YCTLlBmY0tGEx2Q25YGGojQYiHZ N+9w+hK5fLpsQ9GNgged0m+6wwKl2Dc0+LOUSVhOWHJeQIiExnq5eeAZ8aothFYRnArz UBn8Jyi1DQ6rL05XiZp0+w/qDiTLPwckB3hema8t+pkfzR4MT4r7FCAEu/yZU+2TJpef OpQw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 97si8512909edr.112.2020.12.13.06.58.18; Sun, 13 Dec 2020 06:58:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2438792AbgLLKqt convert rfc822-to-8bit (ORCPT + 99 others); Sat, 12 Dec 2020 05:46:49 -0500 Received: from us-smtp-delivery-44.mimecast.com ([205.139.111.44]:52972 "EHLO us-smtp-delivery-44.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2407392AbgLLKp0 (ORCPT ); Sat, 12 Dec 2020 05:45:26 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-236-taTX-HrMNk6yMqdfLr3e8g-1; Sat, 12 Dec 2020 05:44:31 -0500 X-MC-Unique: taTX-HrMNk6yMqdfLr3e8g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 345AE107ACE3; Sat, 12 Dec 2020 10:44:29 +0000 (UTC) Received: from krava.redhat.com (unknown [10.40.192.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6059E60CCE; Sat, 12 Dec 2020 10:44:25 +0000 (UTC) From: Jiri Olsa To: Arnaldo Carvalho de Melo Cc: lkml , Peter Zijlstra , Ingo Molnar , Mark Rutland , Namhyung Kim , Alexander Shishkin , Michael Petlan , Ian Rogers , Stephane Eranian , Alexei Budankov Subject: [PATCH 7/8] perf daemon: Allow only one daemon over base directory Date: Sat, 12 Dec 2020 11:43:57 +0100 Message-Id: <20201212104358.412065-8-jolsa@kernel.org> In-Reply-To: <20201212104358.412065-1-jolsa@kernel.org> References: <20201212104358.412065-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jolsa@kernel.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kernel.org Content-Transfer-Encoding: 8BIT Content-Type: text/plain; charset=WINDOWS-1252 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add 'lock' file under daemon base and flock it, so only one perf daemon can run on top of it. # perf daemon --config ./config.daemon # perf daemon --config ./config.daemon failed: another perf daemon (pid 369675) owns /opt/perfdata Signed-off-by: Jiri Olsa --- tools/perf/builtin-daemon.c | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tools/perf/builtin-daemon.c b/tools/perf/builtin-daemon.c index 855fed2fe364..1bd5432a57a3 100644 --- a/tools/perf/builtin-daemon.c +++ b/tools/perf/builtin-daemon.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -639,6 +640,42 @@ static int handle_config_changes(struct daemon *daemon, int conf_fd, return 0; } +static int check_lock(struct daemon *daemon) +{ + char path[PATH_MAX]; + char buf[20]; + int fd, pid; + ssize_t len; + + scnprintf(path, sizeof(path), "%s/lock", daemon->base); + + fd = open(path, O_RDWR|O_CREAT, 0640); + if (fd < 0) + return -1; + + if (lockf(fd, F_TLOCK, 0) < 0) { + filename__read_int(path, &pid); + fprintf(stderr, "failed: another perf daemon (pid %d) owns %s\n", + pid, daemon->base); + return -1; + } + + scnprintf(buf, sizeof(buf), "%d", getpid()); + len = strlen(buf); + + if (write(fd, buf, len) != len) { + perror("write failed"); + return -1; + } + + if (ftruncate(fd, len)) { + perror("ftruncate failed"); + return -1; + } + + return 0; +} + static int go_background(struct daemon *daemon) { int pid, fd; @@ -653,6 +690,9 @@ static int go_background(struct daemon *daemon) if (setsid() < 0) return -1; + if (check_lock(daemon)) + return -1; + umask(0); if (chdir(daemon->base)) { @@ -704,6 +744,9 @@ static int __cmd_daemon(struct daemon *daemon, bool foreground, const char *conf if (setup_server_config(daemon)) return -1; + if (foreground && check_lock(daemon)) + return -1; + if (!foreground && go_background(daemon)) return -1; -- 2.26.2