From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by dcvr.yhbt.net (Postfix) with ESMTP id A58A61F4D7 for ; Thu, 28 Apr 2022 22:22:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237150AbiD1WZJ (ORCPT ); Thu, 28 Apr 2022 18:25:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237121AbiD1WZH (ORCPT ); Thu, 28 Apr 2022 18:25:07 -0400 Received: from pb-smtp1.pobox.com (pb-smtp1.pobox.com [64.147.108.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E1E67DE21 for ; Thu, 28 Apr 2022 15:21:51 -0700 (PDT) Received: from pb-smtp1.pobox.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id 06CE9110BC1; Thu, 28 Apr 2022 18:21:48 -0400 (EDT) (envelope-from junio@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to:cc :subject:references:date:in-reply-to:message-id:mime-version :content-type; s=sasl; bh=UOHp/FcG2YVQAuTjArOYJskAsQ1H9s7n2fNNDG y1bEA=; b=gykKn/0+w2KKZZr60O7+yRpjylPRP7DwAMLBVlSc3huyLzBvQyGHxc CD7+DVo+OFO1lo/S9lkOmMIexkCf61pKm5xJONrJlsLIgnWi2zNbIZZkMrwHNVKr A93fuFWljiKKJGVtKVEB0BLk2VM24rjznpgJeWaWVudot+K8CBAYM= Received: from pb-smtp1.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id F1398110BC0; Thu, 28 Apr 2022 18:21:47 -0400 (EDT) (envelope-from junio@pobox.com) Received: from pobox.com (unknown [34.83.65.128]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp1.pobox.com (Postfix) with ESMTPSA id 5B744110BBF; Thu, 28 Apr 2022 18:21:47 -0400 (EDT) (envelope-from junio@pobox.com) From: Junio C Hamano To: Cc: "'Carlo Arenas'" , Subject: Re: [PATCH v2 3/3] t: add tests for safe.directory when running with sudo References: <20220428033544.68188-1-carenas@gmail.com> <20220428105852.94449-1-carenas@gmail.com> <20220428105852.94449-4-carenas@gmail.com> <000001d85b39$9d5cfc90$d816f5b0$@nexbridge.com> <000801d85b40$ac11ec80$0435c580$@nexbridge.com> <000f01d85b4a$af8c3aa0$0ea4afe0$@nexbridge.com> Date: Thu, 28 Apr 2022 15:21:46 -0700 In-Reply-To: <000f01d85b4a$af8c3aa0$0ea4afe0$@nexbridge.com> (rsbecker@nexbridge.com's message of "Thu, 28 Apr 2022 17:55:28 -0400") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Pobox-Relay-ID: 97B88006-C741-11EC-8AD1-5E84C8D8090B-77302942!pb-smtp1.pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org writes: >>is_root() { >> id -u >u >> id -u root >r >> cmp u r >>} > > This is about as portable as I can find and works even in ksh. It could be optimized. > > is_root() { > id -u >u > id -u root >r > cmp -s u r > if [ $? -ne 0 ]; then > echo 0 > else > echo 1 > fi > } > > if [ `is_root` -ne 0 ]; then > echo root > else > echo Not root > fi The above looks very roundabout way. With the first three in is_root that ends with "cmp", we already know from its exit status if "id -u" output for ourselves matches that for root, i.e. if we are root then cmp would have exited with 0. So with the first one I quoted from your quote, the caller can say if is_root then echo root else echo not root fi without turning the exit status into string "0" or "1" and comparing that string with "[ `cmd` -ne 0 ]". And the first one is just as portable. I agree that running cmp with "-s" is probably a good idea. What I used to often use in my previous life (in previous century) is technically incorrect, but is a lot more succinct and works well in practice on any sanely installed systems. Just see if the root directory is writable. No sane system makes it writable by anybody but root. I.e. if test -w / then ... we are running as root ... else ... we are not running as root ... fi