mirror of
https://github.com/shadow-maint/shadow.git
synced 2024-12-18 06:14:46 +08:00
f45498a6c2
Documentation: - Correct the comment documenting the function: write_full() doesn't write "up to" count bytes (which is write(2)'s behavior, and exactly what this function is designed to avoid), but rather exactly count bytes (on success). - While fixing the documentation, take the time to add a man-page-like comment as in other APIs. Especially, since we'll have to document a few other changes from this patch, such as the modified return values. - Partial writes are still possible on error. It's the caller's responsibility to handle that possibility. API: - In write(2), it's useful to know how many bytes were transferred, since it can have short writes. In this API, since it either writes it all or fails, that value is useless, and callers only want to know if it succeeded or not. Thus, just return 0 or -1. Implementation: - Use `== -1` instead of `< 0` to check for write(2) syscall errors. This is wisdom from Michael Kerrisk. This convention is useful because it more explicitly tells maintainers that the only value which can lead to that path is -1. Otherwise, a maintainer of the code might be confused to think that other negative values are possible. Keep it simple. - The path under `if (res == 0)` was unreachable, since the loop condition `while (count > 0)` precludes that possibility. Remove the dead code. - Use a temporary variable of type `const char *` to avoid a cast. - Rename `res`, which just holds the result from write(2), to `w`, which more clearly shows that it's just a very-short-lived variable (by it's one-letter name), and also relates itself more to write(2). I find it more readable. - Move the definition of `w` to the top of the function. Now that the function is significantly shorter, the lifetime of the variable is clearer, and I find it more readable this way. Use: - Also use `== -1` to check errors. Cc: Christian Göttsche <cgzones@googlemail.com> Cc: Serge Hallyn <serge@hallyn.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
94 lines
2.2 KiB
C
94 lines
2.2 KiB
C
/*
|
|
* SPDX-FileCopyrightText: 1989 - 1994, Julianne Frances Haugh
|
|
* SPDX-FileCopyrightText: 1996 - 1998, Marek Michałkiewicz
|
|
* SPDX-FileCopyrightText: 2003 - 2005, Tomasz Kłoczko
|
|
* SPDX-FileCopyrightText: 2008 , Nicolas François
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
#include <config.h>
|
|
|
|
#ident "$Id$"
|
|
|
|
#include <sys/types.h>
|
|
#include <pwd.h>
|
|
#include <fcntl.h>
|
|
#include <time.h>
|
|
#include "defines.h"
|
|
#include <lastlog.h>
|
|
#include "prototypes.h"
|
|
|
|
/*
|
|
* dolastlog - create lastlog entry
|
|
*
|
|
* A "last login" entry is created for the user being logged in. The
|
|
* UID is extracted from the global (struct passwd) entry and the
|
|
* TTY information is gotten from the (struct utmp).
|
|
*/
|
|
void dolastlog (
|
|
struct lastlog *ll,
|
|
const struct passwd *pw,
|
|
/*@unique@*/const char *line,
|
|
/*@unique@*/const char *host)
|
|
{
|
|
int fd;
|
|
off_t offset;
|
|
struct lastlog newlog;
|
|
time_t ll_time;
|
|
|
|
/*
|
|
* If the file does not exist, don't create it.
|
|
*/
|
|
|
|
fd = open (LASTLOG_FILE, O_RDWR);
|
|
if (-1 == fd) {
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* The file is indexed by UID number. Seek to the record
|
|
* for this UID. Negative UID's will create problems, but ...
|
|
*/
|
|
|
|
offset = (off_t) pw->pw_uid * sizeof newlog;
|
|
|
|
if (lseek (fd, offset, SEEK_SET) != offset) {
|
|
SYSLOG ((LOG_WARN,
|
|
"Can't read last lastlog entry for UID %lu in %s. Entry not updated.",
|
|
(unsigned long) pw->pw_uid, LASTLOG_FILE));
|
|
(void) close (fd);
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* Read the old entry so we can tell the user when they last
|
|
* logged in. Then construct the new entry and write it out
|
|
* the way we read the old one in.
|
|
*/
|
|
|
|
if (read (fd, &newlog, sizeof newlog) != (ssize_t) sizeof newlog) {
|
|
memzero (&newlog, sizeof newlog);
|
|
}
|
|
if (NULL != ll) {
|
|
*ll = newlog;
|
|
}
|
|
|
|
ll_time = newlog.ll_time;
|
|
(void) time (&ll_time);
|
|
newlog.ll_time = ll_time;
|
|
strncpy (newlog.ll_line, line, sizeof (newlog.ll_line) - 1);
|
|
#if HAVE_LL_HOST
|
|
strncpy (newlog.ll_host, host, sizeof (newlog.ll_host) - 1);
|
|
#endif
|
|
if ( (lseek (fd, offset, SEEK_SET) != offset)
|
|
|| (write_full(fd, &newlog, sizeof newlog) == -1)
|
|
|| (close (fd) != 0)) {
|
|
SYSLOG ((LOG_WARN,
|
|
"Can't write lastlog entry for UID %lu in %s.",
|
|
(unsigned long) pw->pw_uid, LASTLOG_FILE));
|
|
(void) close (fd);
|
|
}
|
|
}
|
|
|