Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp3134465rwl; Sun, 2 Apr 2023 00:51:45 -0700 (PDT) X-Google-Smtp-Source: AKy350by3yfPChPi2PkCg5yZByf3YZuv0rs7QTW0Kxa8kHSumys1Vr5TH0wJm5tVFBXW/I164YnJ X-Received: by 2002:aa7:d618:0:b0:502:4ba4:76 with SMTP id c24-20020aa7d618000000b005024ba40076mr18245893edr.7.1680421905446; Sun, 02 Apr 2023 00:51:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680421905; cv=none; d=google.com; s=arc-20160816; b=io5CAMYOYJ60rD5U4sbwzDUM2f1KMhXJdIx5UPpzA61VYiXyJkPOjZL1scBatsvrIk jLfQfIZCSQ8VLXatKBH4vUnAS4S9UeSITfHTkYHBfpt+1w/uY0t5b7IIW1wPToEC26TI /hvPj5stYPuJAyqtNTfqZqkg0pBW3xbffZu1Oqlgsv5uQaYB2dAAb8lWKjEGKXiyCaC+ GY/DeVzYPlJ51l4SFG0M/wKotfySobHQEzMCV9lW78aYuuDO2X0mD2NEjAbhb9ZLBhES eYK48nPXbPCrwdix8PwWrAxLtLCb/DdLBgpQXXk7WLTCMhV41PKb4EKQhzzXqOWRDs8X CI6w== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=wlN5J8wKSu80CwgoJ/MW5XMZ5IbzSuztlEc1zdM64Tc=; b=SozgqyeLhJFVIWjYQaeLodEUs1ji0zNIuyCpbplKJInxw6h5XPHvLyKxh2yk0VwdUV PCT8Qzdd9BQGvIEJf+Il9d66VuD545N2CpC5Nk1ZHmEM4/IPb2MtUkXxDlYNL6EuaJKj rivOyRkWDpj6DXC5moE43tGIGn6cFriL6qaMe1/WRybYZYGYjCT3TrwGknnVY7yqDJg+ qDMc9em4ITUfOU69dVwQ6SF3+2P/Qd/luKOG20w9FSagRSL7+8+e0ZLWg5POuDIQbOSo DoymTiNN4XhTBIc9buJQbHEXkNmsb4nj3pnMvjhWfDcmiSzGkGYSR7WnvDDXHT03wFJE +L7w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q11-20020a056402032b00b004ab4c6f9996si5739721edw.516.2023.04.02.00.51.19; Sun, 02 Apr 2023 00:51:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230106AbjDBHqN (ORCPT + 99 others); Sun, 2 Apr 2023 03:46:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229379AbjDBHqM (ORCPT ); Sun, 2 Apr 2023 03:46:12 -0400 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 17CEBAD1E; Sun, 2 Apr 2023 00:46:09 -0700 (PDT) Received: (from willy@localhost) by mail.home.local (8.17.1/8.17.1/Submit) id 3327jvBS018311; Sun, 2 Apr 2023 09:45:57 +0200 Date: Sun, 2 Apr 2023 09:45:57 +0200 From: Willy Tarreau To: Thomas =?iso-8859-1?Q?Wei=DFschuh?= Cc: Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH 2/3] tools/nolibc: let FILE streams contain an fd Message-ID: References: <20230328-nolibc-printf-test-v1-0-d7290ec893dd@weissschuh.net> <20230328-nolibc-printf-test-v1-2-d7290ec893dd@weissschuh.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230328-nolibc-printf-test-v1-2-d7290ec893dd@weissschuh.net> X-Spam-Status: No, score=-0.0 required=5.0 tests=SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Thomas, On Tue, Mar 28, 2023 at 09:01:30PM +0000, Thomas Wei?schuh wrote: > This enables the usage of the stream APIs with arbitrary filedescriptors. > > It will be used by a future testcase. > Users can also use nolibc-specific code to do the same. > > Signed-off-by: Thomas Wei?schuh > --- > tools/include/nolibc/stdio.h | 36 +++++++----------------------------- > 1 file changed, 7 insertions(+), 29 deletions(-) > > diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h > index 96ac8afc5aee..cb58912b98e5 100644 > --- a/tools/include/nolibc/stdio.h > +++ b/tools/include/nolibc/stdio.h > @@ -21,17 +21,13 @@ > #define EOF (-1) > #endif > > -/* just define FILE as a non-empty type */ > typedef struct FILE { > - char dummy[1]; > + int fd; > } FILE; In my opinion this makes the usage of FILE* more complicated than before. Look for example at your vfprintf() test case, you had to do this with the fd provided by memfd_create(): w = vfprintf(&(FILE) { fd }, fmt, args); This is particularly ugly especially for code that needs to be exposed to end-user. Also it breaks compatibility with glibc that is seldom used to check if trouble comes from nolibc or from the test itself. It would be much better to have fdopen() here but the new struct makes this impossible. I would propose instead to go back to the previous definition and simply change its semantics a little bit: /* just define FILE as a non-empty type. The value of the pointer gives * the FD: FILE=~fd for fd>=0 or NULL for fd<0. This way positive FILE * are immediately identified as abnormal entries (i.e. possible copies * of valid pointers to something else). typedef struct FILE { char dummy[1]; } FILE; Then we can have: static __attribute__((unused)) FILE* const stdin = (FILE*)(uintptr_t)~STDIN_FILENO; static __attribute__((unused)) FILE* const stdout = (FILE*)(uintptr_t)~STDOUT_FILENO; static __attribute__((unused)) FILE* const stderr = (FILE*)(uintptr_t)~STDERR_FILENO; /* provides a FILE* equivalent of fd. The mode is ignored. */ static __attribute__((unused)) FILE *fdopen(int fd, const char *mode) { if (fd < 0) return NULL; return (FILE*)(uintptr_t)~fd; } And your FD can simply be passed this way: fd = memfd_create("vfprintf", 0); if (fd == -1) { pad_spc(llen, 64, "[FAIL]\n"); return 1; } va_start(args, fmt); w = vfprintf(fdopen(fd, "w+"), fmt, args); va_end(args); This way it works more like common usage and restores glibc compatibility. Regards, Willy