From 9a1d3976f195e815649e103b5b930bacb02029b0 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 7 Aug 2020 15:55:02 -0700 Subject: [PATCH] Reimplement our no-op utmp.h functions more simply. Now we're being marked down for our poor coverage, we may as well remove more broken cruft. Despite the amount of effort that seems to have gone into pututline(), it wasn't working with the other utmp.h functions (in particular, utmpname()), and wasn't declared in the header file! Test: treehugger Change-Id: I1a583984189c751168c11c01431433f96f8c548b --- libc/Android.bp | 3 +- libc/NOTICE | 29 ------- libc/bionic/{pututline.c => utmp.cpp} | 48 ++++------ libc/include/utmp.h | 80 +++++++++++------ libc/upstream-netbsd/lib/libc/gen/utmp.c | 106 ----------------------- tests/utmp_test.cpp | 6 +- 6 files changed, 73 insertions(+), 199 deletions(-) rename libc/bionic/{pututline.c => utmp.cpp} (64%) delete mode 100644 libc/upstream-netbsd/lib/libc/gen/utmp.c diff --git a/libc/Android.bp b/libc/Android.bp index 5bd00c8de..8717f7b6f 100644 --- a/libc/Android.bp +++ b/libc/Android.bp @@ -8,7 +8,6 @@ libc_common_src_files = [ "bionic/fts.c", "bionic/initgroups.c", "bionic/isatty.c", - "bionic/pututline.c", "bionic/sched_cpualloc.c", "bionic/sched_cpucount.c", "stdio/fmemopen.cpp", @@ -391,7 +390,6 @@ cc_library_static { "upstream-netbsd/lib/libc/gen/nice.c", "upstream-netbsd/lib/libc/gen/psignal.c", "upstream-netbsd/lib/libc/gen/utime.c", - "upstream-netbsd/lib/libc/gen/utmp.c", "upstream-netbsd/lib/libc/inet/nsap_addr.c", "upstream-netbsd/lib/libc/regex/regcomp.c", "upstream-netbsd/lib/libc/regex/regerror.c", @@ -1154,6 +1152,7 @@ cc_library_static { "bionic/umount.cpp", "bionic/unlink.cpp", "bionic/usleep.cpp", + "bionic/utmp.cpp", "bionic/wait.cpp", "bionic/wchar.cpp", "bionic/wchar_l.cpp", diff --git a/libc/NOTICE b/libc/NOTICE index 7f2e8b0cd..764fd7b6b 100644 --- a/libc/NOTICE +++ b/libc/NOTICE @@ -3456,35 +3456,6 @@ POSSIBILITY OF SUCH DAMAGE. ------------------------------------------------------------------- -Copyright (c) 2002 The NetBSD Foundation, Inc. -All rights reserved. - -This code is derived from software contributed to The NetBSD Foundation -by Christos Zoulas. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: -1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS -``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS -BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. - -------------------------------------------------------------------- - Copyright (c) 2002 Tim J. Robbins All rights reserved. diff --git a/libc/bionic/pututline.c b/libc/bionic/utmp.cpp similarity index 64% rename from libc/bionic/pututline.c rename to libc/bionic/utmp.cpp index 8cbf47057..aa00cd568 100644 --- a/libc/bionic/pututline.c +++ b/libc/bionic/utmp.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 The Android Open Source Project + * Copyright (C) 2020 The Android Open Source Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,40 +25,24 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ -#include -#include + #include +#include -void pututline(struct utmp* utmp) -{ - FILE* f; - struct utmp u; - long i; +void endutent() {} - if (!(f = fopen(_PATH_UTMP, "w+e"))) - return; +void setutent() {} - while (fread(&u, sizeof(struct utmp), 1, f) == 1) - { - if (!strncmp(utmp->ut_line, u.ut_line, sizeof(u.ut_line) -1)) - { - if ((i = ftell(f)) < 0) - goto ret; - if (fseek(f, i - sizeof(struct utmp), SEEK_SET) < 0) - goto ret; - fwrite(utmp, sizeof(struct utmp), 1, f); - goto ret; - } - } - - - fclose(f); - - if (!(f = fopen(_PATH_UTMP, "w+e"))) - return; - fwrite(utmp, sizeof(struct utmp), 1, f); - -ret: - fclose(f); +utmp* getutent() { + return nullptr; +} + +utmp* pututline(const utmp*) { + return nullptr; +} + +int utmpname(const char*) { + errno = ENOTSUP; + return -1; } diff --git a/libc/include/utmp.h b/libc/include/utmp.h index 6a5251153..cb72ce235 100644 --- a/libc/include/utmp.h +++ b/libc/include/utmp.h @@ -25,8 +25,13 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ -#ifndef _UTMP_H_ -#define _UTMP_H_ + +#pragma once + +/** + * @file utmp.h + * @brief POSIX login records. + */ #include #include @@ -57,52 +62,71 @@ #define DEAD_PROCESS 8 #define ACCOUNTING 9 -struct lastlog -{ - time_t ll_time; - char ll_line[UT_LINESIZE]; - char ll_host[UT_HOSTSIZE]; +struct lastlog { + time_t ll_time; + char ll_line[UT_LINESIZE]; + char ll_host[UT_HOSTSIZE]; }; -struct exit_status -{ - short int e_termination; - short int e_exit; +struct exit_status { + short int e_termination; + short int e_exit; }; +struct utmp { + short int ut_type; + pid_t ut_pid; + char ut_line[UT_LINESIZE]; + char ut_id[4]; + char ut_user[UT_NAMESIZE]; + char ut_host[UT_HOSTSIZE]; -struct utmp -{ - short int ut_type; - pid_t ut_pid; - char ut_line[UT_LINESIZE]; - char ut_id[4]; - char ut_user[UT_NAMESIZE]; - char ut_host[UT_HOSTSIZE]; + struct exit_status ut_exit; - struct exit_status ut_exit; + long int ut_session; + struct timeval ut_tv; - long int ut_session; - struct timeval ut_tv; - - int32_t ut_addr_v6[4]; - char unsed[20]; + int32_t ut_addr_v6[4]; + char unused[20]; }; - #define ut_name ut_user #define ut_time ut_tv.tv_sec #define ut_addr ut_addr_v6[0] __BEGIN_DECLS +/** + * Does nothing. + */ int utmpname(const char* __path); +/** + * Does nothing. + */ void setutent(void); +/** + * Does nothing. + */ struct utmp* getutent(void); +/** + * Does nothing. + */ +struct utmp* pututline(const struct utmp* __entry); +/** + * Does nothing. + */ void endutent(void); +/** + * [login_tty(3)](https://www.man7.org/linux/man-pages/man3/login_tty.3.html) + * prepares for login on the given file descriptor. + * + * See also forkpty() which combines openpty(), fork(), and login_tty(). + * + * Returns 0 on success and returns -1 and sets `errno` on failure. + * + * Available since API level 23. + */ int login_tty(int __fd) __INTRODUCED_IN(23); __END_DECLS - -#endif /* _UTMP_H_ */ diff --git a/libc/upstream-netbsd/lib/libc/gen/utmp.c b/libc/upstream-netbsd/lib/libc/gen/utmp.c deleted file mode 100644 index 9fb0799db..000000000 --- a/libc/upstream-netbsd/lib/libc/gen/utmp.c +++ /dev/null @@ -1,106 +0,0 @@ -/* $NetBSD: utmp.c,v 1.10 2011/10/15 23:00:02 christos Exp $ */ - -/*- - * Copyright (c) 2002 The NetBSD Foundation, Inc. - * All rights reserved. - * - * This code is derived from software contributed to The NetBSD Foundation - * by Christos Zoulas. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#include - -#if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: utmp.c,v 1.10 2011/10/15 23:00:02 christos Exp $"); -#endif /* LIBC_SCCS and not lint */ - -#include "namespace.h" -#include -#include -#include -#include -#include -#include -#include - -static struct utmp utmp; -static FILE *ut; -static char utfile[MAXPATHLEN] = _PATH_UTMP; - -void -setutent(void) -{ - if (ut == NULL) - return; - (void)fseeko(ut, (off_t)0, SEEK_SET); -} - -struct utmp * -getutent(void) -{ - if (ut == NULL) { - struct stat st; - off_t numentries; - if ((ut = fopen(utfile, "re")) == NULL) - return NULL; - if (fstat(fileno(ut), &st) == -1) - goto out; - /* - * If we have a an old version utmp file bail. - */ - numentries = st.st_size / sizeof(utmp); - if ((off_t)(numentries * sizeof(utmp)) != st.st_size) - goto out; - } - if (fread(&utmp, sizeof(utmp), 1, ut) == 1) - return &utmp; -out: - (void)fclose(ut); - return NULL; -} - -void -endutent(void) -{ - if (ut != NULL) { - (void)fclose(ut); - ut = NULL; - } -} - -int -utmpname(const char *fname) -{ - size_t len = strlen(fname); - - if (len >= sizeof(utfile)) - return 0; - - /* must not end in x! */ - if (fname[len - 1] == 'x') - return 0; - - (void)strlcpy(utfile, fname, sizeof(utfile)); - endutent(); - return 1; -} diff --git a/tests/utmp_test.cpp b/tests/utmp_test.cpp index 0fa55c74b..6d0d6f127 100644 --- a/tests/utmp_test.cpp +++ b/tests/utmp_test.cpp @@ -24,8 +24,10 @@ TEST(utmp, login_tty) { ASSERT_EQ(-1, login_tty(-1)); } -TEST(utmp, setutent_getutent_endutent) { +TEST(utmp, smoke) { + ASSERT_EQ(-1, utmpname("hello")); setutent(); - getutent(); + ASSERT_EQ(NULL, getutent()); endutent(); + ASSERT_EQ(NULL, pututline(NULL)); }