Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117587 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 94080 invoked from network); 24 Apr 2022 12:50:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Apr 2022 12:50:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EC57B1804CF for ; Sun, 24 Apr 2022 07:25:22 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f180.google.com (mail-lj1-f180.google.com [209.85.208.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 24 Apr 2022 07:25:22 -0700 (PDT) Received: by mail-lj1-f180.google.com with SMTP id v1so11561567ljv.3 for ; Sun, 24 Apr 2022 07:25:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=a3KBF39E5GMiBomoUGjJtl6Zdnpx5xeWWvMrAZGaaiY=; b=djUsCKESoj3+P4Yrajx3gr5rHBLFssTkYUMlbPxowPTHkBSAzq3VOptN4Mniz3GI1A D10OWaoviZ5kHt8mS44u0OoBVYHmDGfkRgpXpCKERi75neMfcC+hO8Uovh1Qi/XUnbVi 3x/S/ieN4x3rlvsJe4eHdrDejPUIpzlg+ChBkZPMwowCc048wN1o19/1W6ZTkzohl1QI z+AmP8/cwkJHePOYamGE1YaCixsZDNCHGbGs3Q/0cuNOpbyCMIeGYzALX8EQ8oHmCX/b 0xebgzvmsYEjk9yW/xJC7M9W1sZgEyEcEMIHHGy0aA+iMNr9QtK/Yo6BuLSY/fyx8kJG pSkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=a3KBF39E5GMiBomoUGjJtl6Zdnpx5xeWWvMrAZGaaiY=; b=w1pk4AhO4IkCU+cGGJUThzodGYNNMOOvD9PT2pF5ggeJhuXCcdia6Vv+mCHvBZ7O9l ad7uJBuPjbKjQiK+96CX4TpxaIscK0/6P3dzmwbfceYhqG6OX1y4FLLjH9LaUFVv51nc MKlE7ns/+GjWbRGC1nMTJ0TaABr+fz9+c+YbdzchrPhbMrvKRJtnEouhpYPVEBksmLTA UJPzkIIPIAa71jbtSWg9L+z8chdBQChC1fSq3yq+Ku19m9oe0ZQCdQVSpkQwPE+lMuwv t3XkCUvOW8EaZW74tWAxjz7LmNDPUbSCxDS4ShwUElXxIo2xrT9eTzRG7n/HVOLcwKcv /nrA== X-Gm-Message-State: AOAM530xSp0drdWXu9WCCuezCzXWohM9ybFbryHPKzxAryJpfiFjbpTt BgEZER7O3/ARD07g60qFb87VxqSlZ2rx/qjOyAI= X-Google-Smtp-Source: ABdhPJzrapLIY2316Tlh0aR/3Euj9RJNsgH5WiatmSPq/KT+ssCrFng+/eBUxe+CVrHH2ks6i6t/C+Xf4OrRnOf43tU= X-Received: by 2002:a2e:91cf:0:b0:24f:11ea:d493 with SMTP id u15-20020a2e91cf000000b0024f11ead493mr155346ljg.408.1650810320641; Sun, 24 Apr 2022 07:25:20 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 24 Apr 2022 16:25:04 +0200 Message-ID: To: Ilija Tovilo Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000c0e32f05dd673aca" Subject: Re: [PHP-DEV] Change the values of the LOCK_* constants From: nikita.ppv@gmail.com (Nikita Popov) --000000000000c0e32f05dd673aca Content-Type: text/plain; charset="UTF-8" On Sun, Apr 24, 2022 at 12:14 PM Ilija Tovilo wrote: > Hi everyone > > The issue was raised that PHPs LOCK_* constants don't match the Unix > LOCK_* constants. > https://github.com/php/php-src/pull/8429 > > // Unix > #define LOCK_SH 1 > #define LOCK_EX 2 > #define LOCK_NB 4 > #define LOCK_UN 8 > > // PHP > #define PHP_LOCK_SH 1 > #define PHP_LOCK_EX 2 > #define PHP_LOCK_UN 3 > #define PHP_LOCK_NB 4 > > Essentially, in PHPs binary representation UN doesn't get its own bit, > but is instead represented as 0b11. I'm guessing the reasoning was > that SH, EX and UN must not be combined, while they can all be > combined with NB. This avoids additional error handling when multiple > of those bits were to be set. > > However, this has a downside of making checking of bits harder and > different from how you would do it in other languages. > https://3v4l.org/41ebV > > We could update the PHP constants to match the Unix values of those > constants. Unfortunately, there seems to be a not insignificant number > of usages of flock with hard-coded integer values. > > > https://sourcegraph.com/search?q=context:global+file:%5C.php%24+count:100000+flock%5C%28%5C%24%5Ba-zA-Z0-9_%5D%2B%2C+%5B0-9%5D%2B%5C%29&patternType=regexp > > (The regex engine of sourcegraph is flaky, but the majority of results > are correct) > > The process of replacing these hard-coded values could be partially > automated with a few caveats. > > 1. The value must be direct ($flags = 1; flock($file, $flags); would not > work) > 2. The migration script would assume that flock is a global and not > local function > > Overall, I'm not completely sure this change is worth it since flock > flags are just passed and not read. > > Let me know what you think. > > Ilija > I think the current state of things here makes perfect sense. I might help to think of it as a structure of the form: struct { unsigned lock_type : 2; unsigned non_blocking : 1; } The first member of that structure is not a bitmask -- the three options are mutually exclusive, and doing something like LOCK_SH | LOCK_UN is semantically meaningless. Consulting https://man7.org/linux/man-pages/man2/flock.2.html, nothing on the flock() man page suggests that LOCK_SH, LOCK_EX and LOCK_UN can be used as bitflags -- it so happens that they can in C, but this is not an API guarantee. The kernel code for these flags handles things properly by first removing the LOCK_NB flag and then doing equality comparisons against the lock type -- not flag checks. Incidentally these get mapped to F_RDLCK, F_WRLCK and F_UNLCK internally, which just so happen to have the same values as LOCK_SH, LOCK_EX and LOCK_UN in PHP ;) Is there some kind of evidence that people are actually trying to use these as bitflags, and you're trying to solve a real problem here? Or is the only problem being solved that somebody is celebrating their own ignorance and incompetence over at r/lolphp again? Regards, Nikita --000000000000c0e32f05dd673aca--