From patchwork Wed Oct 11 11:57:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Carlo Zancanaro X-Patchwork-Id: 54750 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 9875A27BBEA; Wed, 11 Oct 2023 12:58:49 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id A826227BBE2 for ; Wed, 11 Oct 2023 12:58:48 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qqXrQ-0000Xr-8y; Wed, 11 Oct 2023 07:58:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qqXrM-0000XS-Rl for guix-patches@gnu.org; Wed, 11 Oct 2023 07:58:40 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qqXrM-0000XX-Cx for guix-patches@gnu.org; Wed, 11 Oct 2023 07:58:40 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qqXri-0000Bu-4b for guix-patches@gnu.org; Wed, 11 Oct 2023 07:59:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#66387] [PATCH] home: services: Fix race condition when detecting first login Resent-From: Carlo Zancanaro Original-Sender: "Debbugs-submit" Resent-CC: , guix-patches@gnu.org Resent-Date: Wed, 11 Oct 2023 11:59:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66387 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 66387@debbugs.gnu.org Cc: ( , Andrew Tropin , Ludovic =?utf-8?q?Court=C3=A8s?= X-Debbugs-Original-Xcc: ( , Andrew Tropin , Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 66387-submit@debbugs.gnu.org id=B66387.1697025517701 (code B ref 66387); Wed, 11 Oct 2023 11:59:02 +0000 Received: (at 66387) by debbugs.gnu.org; 11 Oct 2023 11:58:37 +0000 Received: from localhost ([127.0.0.1]:37687 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qqXrI-0000BF-Q4 for submit@debbugs.gnu.org; Wed, 11 Oct 2023 07:58:37 -0400 Received: from voltorb.zancanaro.id.au ([45.77.50.64]:59542) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qqXrG-0000B3-5v for 66387@debbugs.gnu.org; Wed, 11 Oct 2023 07:58:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; s=k1; bh=xSfowpn6ptj3E0e O6DXSIuON0h7+cn6GdA62mhtGlzk=; h=references:in-reply-to:date:subject: to:from; d=zancanaro.id.au; b=Wg8QwHPBGWtrrSteFCwEHYTWgZbD2jGSRe5gxzw5 RaeFXD9gtfFJRfSMYa0cjpDbN3cW5qhdOfnP533JzshWK7UvnC+x0mhKFY5XLw6xBw+38g bgKVCxAKeoFCa1Es05xQ6TPpx+h84AyVIrrdMpiqjc20E8G9XSBHof+zvte8Q= Received: by voltorb.zancanaro.id.au (OpenSMTPD) with ESMTPSA id 1f5d753f (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for <66387@debbugs.gnu.org>; Wed, 11 Oct 2023 11:57:46 +0000 (UTC) From: Carlo Zancanaro Date: Wed, 11 Oct 2023 22:57:19 +1100 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: <87bkd8djri.fsf@zancanaro.id.au> References: <87bkd8djri.fsf@zancanaro.id.au> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * gnu/home/services.scm (compute-on-first-login-script): Use open to atomically check whether a file exists and create it if not. --- gnu/home/services.scm | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) base-commit: 9ad9113fc238ee8de5191a5e15b5153fd149e9fa diff --git a/gnu/home/services.scm b/gnu/home/services.scm index 8d53f2f4d3..7137925b30 100644 --- a/gnu/home/services.scm +++ b/gnu/home/services.scm @@ -2,6 +2,7 @@ ;;; Copyright © 2021-2023 Andrew Tropin ;;; Copyright © 2021 Xinglu Chen ;;; Copyright © 2022-2023 Ludovic Courtès +;;; Copyright © 2023 Carlo Zancanaro ;;; ;;; This file is part of GNU Guix. ;;; @@ -412,20 +413,29 @@ (define (compute-on-first-login-script _ gexps) #~(begin (use-modules (guix i18n) (guix diagnostics)) + + (define (claim-first-run file-name) + (catch #t + (lambda () + ;; This incantation will raise an error if the file at + ;; flag-file-path already exists, and will create it otherwise. + (close (open file-name (logior O_CREAT O_EXCL))) + #t) + (lambda _ + #f))) + #$%initialize-gettext (let* ((xdg-runtime-dir (or (getenv "XDG_RUNTIME_DIR") (format #f "/run/user/~a" (getuid)))) (flag-file-path (string-append - xdg-runtime-dir "/on-first-login-executed")) - (touch (lambda (file-name) - (call-with-output-file file-name (const #t))))) + xdg-runtime-dir "/on-first-login-executed"))) ;; XDG_RUNTIME_DIR dissapears on logout, that means such trick ;; allows to launch on-first-login script on first login only ;; after complete logout/reboot. (if (file-exists? xdg-runtime-dir) - (unless (file-exists? flag-file-path) - (begin #$@gexps (touch flag-file-path))) + (when (claim-first-run flag-file-path) + #$@gexps) ;; TRANSLATORS: 'on-first-login' is the name of a service and ;; shouldn't be translated (warning (G_ "XDG_RUNTIME_DIR doesn't exists, on-first-login script