Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3629092pxu; Tue, 15 Dec 2020 11:21:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJwj/vbR+z5O8DujFj3wNOhCzJPPgsRgMXHFr2hVpp7edE9gdFxyeARGWgTo82gQc9AC8Wp8 X-Received: by 2002:aa7:d3d4:: with SMTP id o20mr31028688edr.190.1608060094701; Tue, 15 Dec 2020 11:21:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608060094; cv=none; d=google.com; s=arc-20160816; b=ooTfAHQEwO5B37ahk/JokMceuLk/YuaF4Rn4foXOpMi6CpQgMS9g/zYT+vM+j/Kld+ JbzC2vo1NuIggHXlUwJ9bePCRNJD+yel1ZJrImIHela5QeMvhEcDQ+lyg2LmScYt5pR8 4qEk1KhE+W50QiTbNopn6P4dSD77UsDBQQ1CWdLWuE/n4t02r9Jjfros/BkNfeV1KNO5 gswHOcRFP8c3oiSZ8vUr9RPk7EkMlRq0732BhuB8s+Fmz1BLIe+cuHRa8xCWZi51mvD3 BuSjRm8saPJnV/Y4SDEyf+Vpny91WQWa4JTzjkyRooIBOAbrZWn47/FzvgeM13bpOKjv d/Rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=xbweWu+fIhygk8uKC+yLgEfU1JyW6oZGEstxLfFUZx4=; b=UmYjcS8dn1eD4/+ZFcUmQ9GQ9gSIJ7OFlz3b1Lp/KqpCBTJpBAnWMvL3iH3I3FaRN+ YrXqMeZKHYqM+HV3fn3u2o/+ugZAvxQNVwOYJF+sNbjTuCQH39onmHUZi18gSM41wMpe eCon7JhD4kOEESG22mZ2GYxTOiVPsmmI1XpAZ3d1f6qSH7T0LgMoixtWzWmW6TGuJOl+ rdXlirJ3RI4mvntpoVX8n3LmOxnh4qrV5WE25Bcz+P8vJkyyDGUJmnu/D7Ksy7xXwYUR arU3IIArOh0GQb835233m9XMS5k/hzn6HX60VwrihHns6faET3Mkm4ftDYRnQ0ElGKNR ccLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=M3RXhVKf; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t27si1405685ejb.240.2020.12.15.11.21.10; Tue, 15 Dec 2020 11:21:34 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=M3RXhVKf; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731933AbgLOTSE (ORCPT + 99 others); Tue, 15 Dec 2020 14:18:04 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:27545 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729453AbgLOTSE (ORCPT ); Tue, 15 Dec 2020 14:18:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1608059797; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=xbweWu+fIhygk8uKC+yLgEfU1JyW6oZGEstxLfFUZx4=; b=M3RXhVKfRVOCvlEX9YVG5G5NFVRmgX5BAQbshGyJATBKI71P3J1NdWwT4MSOukHYg59fWm frIMquNG6nVcR4mT2Xj39PSAInbVESMMDBGgU2OZxlKcN0Pb4w04lrymHPrNfDSvK1Xse5 lN9Uij3UpdYzyIhWLIkpLIO3fdWa/wo= 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-245-Ac600_fpP9mVn8kjgz92yQ-1; Tue, 15 Dec 2020 14:16:34 -0500 X-MC-Unique: Ac600_fpP9mVn8kjgz92yQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A4EE518C9F40; Tue, 15 Dec 2020 19:16:32 +0000 (UTC) Received: from krava (ovpn-112-106.ams2.redhat.com [10.36.112.106]) by smtp.corp.redhat.com (Postfix) with SMTP id 3EA711A839; Tue, 15 Dec 2020 19:16:30 +0000 (UTC) Date: Tue, 15 Dec 2020 20:16:29 +0100 From: Jiri Olsa To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , lkml , Peter Zijlstra , Ingo Molnar , Mark Rutland , Namhyung Kim , Alexander Shishkin , Michael Petlan , Ian Rogers , Stephane Eranian , Alexei Budankov Subject: Re: [PATCH 7/8] perf daemon: Allow only one daemon over base directory Message-ID: <20201215191629.GF698181@krava> References: <20201212104358.412065-1-jolsa@kernel.org> <20201212104358.412065-8-jolsa@kernel.org> <20201215154627.GH258566@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201215154627.GH258566@kernel.org> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 15, 2020 at 12:46:27PM -0300, Arnaldo Carvalho de Melo wrote: > Em Sat, Dec 12, 2020 at 11:43:57AM +0100, Jiri Olsa escreveu: > > 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 > > So the way to disambiguate is the "daemon base"? yes, the 'base' is path where perf daemon will create all control files and 'session' directories and does chdir for 'perf record' session on top of that jirka > > - Arnaldo > > > 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 > > > > -- > > - Arnaldo >