From patchwork Thu Nov 9 17:01:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 79501 X-Patchwork-Delegate: fweimer@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B1D093857738 for ; Thu, 9 Nov 2023 17:01:57 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 1EE963858CDB for ; Thu, 9 Nov 2023 17:01:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1EE963858CDB 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 1EE963858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699549296; cv=none; b=lvqAATJgXsgjv2eyWgIiGG9zz9Y7/UQVrSM26hVY2luDZUsBLVQ/aSjIScYcM+5hyLswSj7TeqdxY1tC/e/e7+fFez7OLy1aFniXX+eP+s/4gTJfoDnDumyYflGA/avNbGjE99LoJ03Fdsq9dnKS5oXnP8SYxgQyj5jrV2A71/g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699549296; c=relaxed/simple; bh=Lu5JurMcsFe4WIM6QAMOw+qIzr5/FZB4MjmwyD+hSPw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=M1mzS/Bk5YLmjWDLtcUd619Bdcq4f8TCvs2YnPLQunyj9ggF1c28nXhG1VwhkSQq/BsxKoeNKyA78VExmyhfWcMcxjt8pz8Z6I/5Lz9+J7sLKTzEkeMtfVr1uBiajRnwnQljfJQvdLaVVl2PEeXNtxyJziWNN46MNvGve4YlRW8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1cc0d0a0355so9518535ad.3 for ; Thu, 09 Nov 2023 09:01:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699549289; x=1700154089; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=zsr8BPGGgXh00bfKbaXgaLWaCF7XZYBN/mlarMRCqfQ=; b=fJhae+EO7eL8oldBOVkXvvh9YoUS+PN/3Jyo71MhzlINwItXdIL9GPpi4pF9mq2Lwg mL80OnVJzOHzlhEZ+IJq2TMsumqW5JyBYbqTKWuf2qeI2rLYIlnJIgwoDSxbLCgRAlCJ B5SMOgRJLEMYyFPIoS4vzplDIlt9WYYocon0I8h6U+BkbQp7vN2Ho2HnAYis/eYq0AZS /dnDHvkuWIUhZ96IJXCVwE8kU1hFZ52WHUICzKJp2G6KTa0ukvWY+VI/n/a5HboRPr2C R/BfaQsrQBztBntdmsuE0X/IagRV9h0aR7Mh5+erJVLdVepKxhSnAzrgPQCGkdGJYx9W w/3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699549289; x=1700154089; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zsr8BPGGgXh00bfKbaXgaLWaCF7XZYBN/mlarMRCqfQ=; b=mSQKogGyfRAlmMJDcpVNfzgdWBbsILhUCsX9dzgVrpT5gAOsqiQ8Lb42s4d0UWIhQS ArDecckl1U4qJEGFTZfg6yWfK6LpCR3S8kYEpW9kUQydVuuQKauDlQ8kUJ9AFDpnrXdD SOVg+HOE6nKcnxBJGxl9fC1VvGnl5EI1prIIiO0Gk/zVnRYizp9LwswEHCUporpOlxrB M18hQzd7AxO/oIIauWrpbS0QkaAmeCB2utQo8yUCHF7t+jjhUVCOXZ5KagbDPXj6EANF AbKIk79PGyblKQ3ogTn0tLdO+h0gVhS6hKEtUSRlb7gOBkPB6VVxL/QUzDfi90u+mLJF eZ6A== X-Gm-Message-State: AOJu0Yye5Apjk5u/lr7J6J2ur2MAE7tuVH1ZTJVSqof4bzVXMKEtOlae 0SSr51mrojIPl4nj8c+U7YdpqTr2hNOsHZXwH/zKHQ== X-Google-Smtp-Source: AGHT+IFEpSarrUtPXrl931fcpxR/5wt042Cz9I4xOjHLxHnJ0v1r/8XJXhlq7UtiK7EPcbm/8LqwNQ== X-Received: by 2002:a17:903:244b:b0:1cc:644a:211b with SMTP id l11-20020a170903244b00b001cc644a211bmr6594358pls.47.1699549289616; Thu, 09 Nov 2023 09:01:29 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a715:4c3a:174b:ee6:b242]) by smtp.gmail.com with ESMTPSA id y13-20020a17090322cd00b001c3f7fd1ef7sm3783556plg.12.2023.11.09.09.01.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 09:01:29 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Thorsten Kukuk Subject: [PATCH 2/3] login: Remove utmp fallback for getlogin Date: Thu, 9 Nov 2023 14:01:18 -0300 Message-Id: <20231109170119.1664204-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231109170119.1664204-1-adhemerval.zanella@linaro.org> References: <20231109170119.1664204-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 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, T_SCC_BODY_TEXT_LINE 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+patchwork=sourceware.org@sourceware.org The utmp is currently broken for 64 bit time_t support on some ABIs where ut_tv is defined depending of the time_t AI used (BZ#30701). It means that the fallback might eventually fail depending of the environment. 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 4c0e3b6660..9d6abf4462 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 3f1b9a6f3c..0000000000 --- a/sysdeps/mach/hurd/getlogin.c +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (C) 1991-2023 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 225a24a2b5..0000000000 --- a/sysdeps/unix/getlogin.c +++ /dev/null @@ -1,81 +0,0 @@ -/* Copyright (C) 1991-2023 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 2f7e8ac479..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-2023 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 df50b847e3..0000000000 --- a/sysdeps/unix/sysv/linux/getlogin.c +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright (C) 2010-2023 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 4ae9a53503..3d3e75cc24 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)