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
This commit is contained in:
Elliott Hughes 2020-08-07 15:55:02 -07:00
parent eebb1ec687
commit 9a1d3976f1
6 changed files with 73 additions and 199 deletions

View File

@ -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",

View File

@ -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.

View File

@ -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 <string.h>
#include <stdio.h>
#include <utmp.h>
#include <errno.h>
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;
}

View File

@ -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 <sys/cdefs.h>
#include <sys/types.h>
@ -57,22 +62,18 @@
#define DEAD_PROCESS 8
#define ACCOUNTING 9
struct lastlog
{
struct lastlog {
time_t ll_time;
char ll_line[UT_LINESIZE];
char ll_host[UT_HOSTSIZE];
};
struct exit_status
{
struct exit_status {
short int e_termination;
short int e_exit;
};
struct utmp
{
struct utmp {
short int ut_type;
pid_t ut_pid;
char ut_line[UT_LINESIZE];
@ -86,23 +87,46 @@ struct utmp
struct timeval ut_tv;
int32_t ut_addr_v6[4];
char unsed[20];
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_ */

View File

@ -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 <sys/cdefs.h>
#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 <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <utmp.h>
#include <sys/stat.h>
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;
}

View File

@ -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));
}