From patchwork Fri Aug 9 14:22:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1970968 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=nPbMaPP/; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WgR3F1kTgz1yYl for ; Sat, 10 Aug 2024 00:23:21 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AFD92385C6CD for ; Fri, 9 Aug 2024 14:23:18 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x231.google.com (mail-oi1-x231.google.com [IPv6:2607:f8b0:4864:20::231]) by sourceware.org (Postfix) with ESMTPS id 07BED3858420 for ; Fri, 9 Aug 2024 14:22:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 07BED3858420 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 07BED3858420 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::231 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723213379; cv=none; b=aZLF9kxZyOEMwzYGsHAVbh5GQCJJNuPmaklAhWzwpG0fT/6UwzExqjgEbDIAheMn3EEkRSbb94qaktI2y5LnXmSevV8KhiifKcmi990LtAcQfgEtmXZU79aT6mNeysX73LiaC9SwwAcyyV3Bvj4npAVF0UAUAxjtixGWu6+7dMM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723213379; c=relaxed/simple; bh=jgwc7aPuVboeURUbd54/M7ge4wtaBPc7fshvFfpsU80=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=u7hX35VCxEIsIyMUGG0fL7VZARb3k4tsKmE6/j2vXruWd1SUFmW53l3xCeGJQFyuRvz3jp8PYEj4XYH0Dm1OcvDU6DWoKzqW9MX0fufQOup8Z0McPvmAhU8FaJvF8GJixBDuehQ3dnl0wy+JbFCUWwvxWCkhq2sXTF0EyFO9MIE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3db145c8010so1367741b6e.3 for ; Fri, 09 Aug 2024 07:22:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1723213376; x=1723818176; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TGvSvn01E0d7GjJz8njF/UaOKkcNTsv6FEGurLX6ZMw=; b=nPbMaPP/QnEAK8umgyxKu3WnA+3YSnOfqzQI4Dq50QMNFmKqtOtb6s81DLN/g3kH/7 32u/D9JS6jUxrgoRXp1UPsKmMX4yyU3yy9X2nFOZ9WRNQTBRX/MLI88Le37LbkT3W91q iAM0q8ADv9uF20bHVKAp69pgkzhpTfJtkAuGC6Ybca3onbWuN01azjWOwmNm7q0Nge0l rnJP4bxkR3okJb6+8kIOWevfxlRz5/41g/WyoREFLWOg0YgkyZFwN9+APRqsc4kumtbQ vUxH4cH3wQryOYH0kQQvyNXEooRuY3M5KKBitKvPTv7BnoXHAU9GdinYBH4R2maAMOXH /9bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723213376; x=1723818176; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TGvSvn01E0d7GjJz8njF/UaOKkcNTsv6FEGurLX6ZMw=; b=WPR9X+Pf/V07q8iy/py/N6QhhH5Ov6WzgJC26+/hOQh8WAaozEa76rfyvG23N4aoyN dSGIvyYCePRUKOBMfXke3WjpYkesx4DsA768q4SvJLsYWSxzdZL8aE6TVim08EttR81k BpPKGpLqPgygq792fQtcvJkTL8Q4H1t5crbHpHHnEvL8EJM/myhjcTYNcsAu3Ep9SZvV 0QCdarrfGlRmNyZhn0c1dvWnQka3lqtAStyFfJLeY1L9TxpdoZ6oyvU0QpDHwoT2xh11 JscgghnWSCLm89piFoBj8qR70Xr8+2G6bPnO5Dlyekb2mCKuJ4hnmG18L1KcQ/D2mBML 38xA== X-Gm-Message-State: AOJu0Yy73JBgwTBbEr2KZwvJjE8awD2iE/szCOd3/CYLBTtcF7AXz2qk MkovfKKrHMZALg3b8Itf6h3w8wZ23qqNHNTtxwgilRshKEQBkhFCRHOG/aG1Y3er/yAvIVofX+v thpI= X-Google-Smtp-Source: AGHT+IEZqnWfPq7/ezJPe72tE+kDVcUad1Qsyp1uCihqILzY3px8mLizWc/FKEhSE53d6E+B4Xt7Rg== X-Received: by 2002:a05:6870:1681:b0:25e:b999:d24 with SMTP id 586e51a60fabf-26c6297c975mr1740094fac.0.1723213375490; Fri, 09 Aug 2024 07:22:55 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:347e:ece3:50dc:7701:b6f7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-710cb20d8e1sm2668222b3a.39.2024.08.09.07.22.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 07:22:54 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Thorsten Kukuk , Paul Eggert , Florian Weimer Subject: [PATCH v2 1/2] login: Remove utmp fallback for getlogin Date: Fri, 9 Aug 2024 11:22:14 -0300 Message-ID: <20240809142248.929824-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240809142248.929824-1-adhemerval.zanella@linaro.org> References: <20240809142248.929824-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org With upcoming utmp no-op, the getutline_r will always fail. The default getlogin is now based on getlogin_r, which allows to consolidate Linux and Hurd implementation. Checked on x86_64-linux-gnu and i686-linux-gnu. --- include/unistd.h | 3 - login/getlogin.c | 13 ++-- sysdeps/mach/hurd/getlogin.c | 35 --------- sysdeps/unix/getlogin.c | 81 --------------------- sysdeps/unix/getlogin_r.c | 103 --------------------------- sysdeps/unix/sysv/linux/getlogin.c | 39 ---------- sysdeps/unix/sysv/linux/getlogin_r.c | 34 ++------- 7 files changed, 16 insertions(+), 292 deletions(-) delete mode 100644 sysdeps/mach/hurd/getlogin.c delete mode 100644 sysdeps/unix/getlogin.c delete mode 100644 sysdeps/unix/getlogin_r.c delete mode 100644 sysdeps/unix/sysv/linux/getlogin.c diff --git a/include/unistd.h b/include/unistd.h index e241603b81..f9bf9f133f 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -215,9 +215,6 @@ extern __pid_t __libc_fork (void); This always returns -1 and sets `errno' to EINTR. */ extern int __libc_pause (void); -extern int __getlogin_r_loginuid (char *name, size_t namesize) - attribute_hidden; - # if IS_IN (rtld) # include # endif diff --git a/login/getlogin.c b/login/getlogin.c index 6f77574db6..e06d84aa3f 100644 --- a/login/getlogin.c +++ b/login/getlogin.c @@ -18,14 +18,19 @@ #include #include #include +#include + +#ifndef LOGIN_NAME_MAX +# define LOGIN_NAME_MAX 1024 +#endif +static char name[LOGIN_NAME_MAX]; /* Return the login name of the user, or NULL if it can't be determined. The returned pointer, if not NULL, is good only until the next call. */ char * getlogin (void) { - __set_errno (ENOSYS); - return NULL; + if (__getlogin_r (name, sizeof name) != 0) + return NULL; + return name[0] != '\0' ? name : NULL; } - -stub_warning (getlogin) diff --git a/sysdeps/mach/hurd/getlogin.c b/sysdeps/mach/hurd/getlogin.c deleted file mode 100644 index 05b287e413..0000000000 --- a/sysdeps/mach/hurd/getlogin.c +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (C) 1991-2024 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include -#include - -/* Return the login name of the user, or NULL if it can't be determined. - The returned pointer, if not NULL, is good only until the next call. */ -char * -getlogin (void) -{ - static char login[1024]; /* XXX */ - error_t err; - - if (err = __USEPORT (PROC, __proc_getlogin (port, login))) - return __hurd_fail (err), NULL; - - return login; -} diff --git a/sysdeps/unix/getlogin.c b/sysdeps/unix/getlogin.c deleted file mode 100644 index 4a7fc03ef5..0000000000 --- a/sysdeps/unix/getlogin.c +++ /dev/null @@ -1,81 +0,0 @@ -/* Copyright (C) 1991-2024 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include -#include -#include -#include - -#include - -static char name[UT_NAMESIZE + 1]; - -/* Return the login name of the user, or NULL if it can't be determined. - The returned pointer, if not NULL, is good only until the next call. */ - -#ifdef STATIC -STATIC -#endif -char * -getlogin (void) -{ - char tty_pathname[2 + 2 * NAME_MAX]; - char *real_tty_path = tty_pathname; - int err; - char *result = NULL; - struct utmp *ut, line, buffer; - - /* Get name of tty connected to fd 0. Return NULL if not a tty or - if fd 0 isn't open. Note that a lot of documentation says that - getlogin() is based on the controlling terminal---what they - really mean is "the terminal connected to standard input". The - getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all - return NULL if fd 0 has been closed, so this is the compatible - thing to do. Note that ttyname(open("/dev/tty")) on those - systems returns /dev/tty, so that is not a possible solution for - getlogin(). */ - err = __ttyname_r (0, real_tty_path, sizeof (tty_pathname)); - if (err != 0) - { - __set_errno (err); - return NULL; - } - - real_tty_path += 5; /* Remove "/dev/". */ - - __setutent (); - strncpy (line.ut_line, real_tty_path, sizeof line.ut_line); - if (__getutline_r (&line, &buffer, &ut) < 0) - { - if (errno == ESRCH) - /* The caller expects ENOENT if nothing is found. */ - __set_errno (ENOENT); - result = NULL; - } - else - { - strncpy (name, ut->ut_user, UT_NAMESIZE); - name[UT_NAMESIZE] = '\0'; - result = name; - } - - __endutent (); - - return result; -} diff --git a/sysdeps/unix/getlogin_r.c b/sysdeps/unix/getlogin_r.c deleted file mode 100644 index 77281f85b4..0000000000 --- a/sysdeps/unix/getlogin_r.c +++ /dev/null @@ -1,103 +0,0 @@ -/* Reentrant function to return the current login name. Unix version. - Copyright (C) 1991-2024 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include -#include -#include -#include - -#include -#include "../login/utmp-private.h" - -/* Return at most NAME_LEN characters of the login name of the user in NAME. - If it cannot be determined or some other error occurred, return the error - code. Otherwise return 0. */ - -#ifdef STATIC -STATIC -#endif -int -__getlogin_r (char *name, size_t name_len) -{ - char tty_pathname[2 + 2 * NAME_MAX]; - char *real_tty_path = tty_pathname; - int result; - struct utmp *ut, line, buffer; - - /* Get name of tty connected to fd 0. Return if not a tty or - if fd 0 isn't open. Note that a lot of documentation says that - getlogin() is based on the controlling terminal---what they - really mean is "the terminal connected to standard input". The - getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all - return NULL if fd 0 has been closed, so this is the compatible - thing to do. Note that ttyname(open("/dev/tty")) on those - systems returns /dev/tty, so that is not a possible solution for - getlogin(). */ - - result = __ttyname_r (0, real_tty_path, sizeof (tty_pathname)); - - if (result != 0) - return result; - - real_tty_path += 5; /* Remove "/dev/". */ - strncpy (line.ut_line, real_tty_path, sizeof line.ut_line); - - /* We don't use the normal entry points __setutent et al, because we - want setutent + getutline_r + endutent all to happen with the lock - held so that our search is thread-safe. */ - - __libc_lock_lock (__libc_utmp_lock); - __libc_setutent (); - result = __libc_getutline_r (&line, &buffer, &ut); - if (result < 0) - { - if (errno == ESRCH) - /* The caller expects ENOENT if nothing is found. */ - result = ENOENT; - else - result = errno; - } - __libc_endutent (); - __libc_lock_unlock (__libc_utmp_lock); - - if (result == 0) - { - size_t needed = __strnlen (ut->ut_user, UT_NAMESIZE) + 1; - - if (needed > name_len) - { - __set_errno (ERANGE); - result = ERANGE; - } - else - { - memcpy (name, ut->ut_user, needed - 1); - name[needed - 1] = 0; - result = 0; - } - } - - return result; -} -#ifndef STATIC -libc_hidden_def (__getlogin_r) -weak_alias (__getlogin_r, getlogin_r) -libc_hidden_weak (getlogin_r) -#endif diff --git a/sysdeps/unix/sysv/linux/getlogin.c b/sysdeps/unix/sysv/linux/getlogin.c deleted file mode 100644 index 5019062b48..0000000000 --- a/sysdeps/unix/sysv/linux/getlogin.c +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright (C) 2010-2024 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include - -#define STATIC static -#define getlogin getlogin_fd0 -#include -#undef getlogin - - -/* Return the login name of the user, or NULL if it can't be determined. - The returned pointer, if not NULL, is good only until the next call. */ - -char * -getlogin (void) -{ - int res = __getlogin_r_loginuid (name, sizeof (name)); - if (res >= 0) - return res == 0 ? name : NULL; - - return getlogin_fd0 (); -} diff --git a/sysdeps/unix/sysv/linux/getlogin_r.c b/sysdeps/unix/sysv/linux/getlogin_r.c index 8f8decc8c0..c32f6658e8 100644 --- a/sysdeps/unix/sysv/linux/getlogin_r.c +++ b/sysdeps/unix/sysv/linux/getlogin_r.c @@ -19,29 +19,24 @@ #include #include #include +#include -#define STATIC static -static int getlogin_r_fd0 (char *name, size_t namesize); -#define __getlogin_r getlogin_r_fd0 -#include -#undef __getlogin_r - +/* Return at most NAME_LEN characters of the login name of the user in NAME. + If it cannot be determined or some other error occurred, return the error + code. Otherwise return 0. -/* Try to determine login name from /proc/self/loginuid and return 0 + Try to determine login name from /proc/self/loginuid and return 0 if successful. If /proc/self/loginuid cannot be read return -1. Otherwise return the error number. */ int -attribute_hidden -__getlogin_r_loginuid (char *name, size_t namesize) +__getlogin_r (char *name, size_t namesize) { int fd = __open_nocancel ("/proc/self/loginuid", O_RDONLY); if (fd == -1) return -1; - /* We are reading a 32-bit number. 12 bytes are enough for the text - representation. If not, something is wrong. */ - char uidbuf[12]; + char uidbuf[INT_BUFSIZE_BOUND (uid_t)]; ssize_t n = TEMP_FAILURE_RETRY (__read_nocancel (fd, uidbuf, sizeof (uidbuf))); __close_nocancel_nostatus (fd); @@ -98,21 +93,6 @@ __getlogin_r_loginuid (char *name, size_t namesize) scratch_buffer_free (&tmpbuf); return result; } - - -/* Return at most NAME_LEN characters of the login name of the user in NAME. - If it cannot be determined or some other error occurred, return the error - code. Otherwise return 0. */ - -int -__getlogin_r (char *name, size_t namesize) -{ - int res = __getlogin_r_loginuid (name, namesize); - if (res >= 0) - return res; - - return getlogin_r_fd0 (name, namesize); -} libc_hidden_def (__getlogin_r) weak_alias (__getlogin_r, getlogin_r) libc_hidden_weak (getlogin_r)