2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 01:49:11 +00:00

Flush I/O logs before we send a commit point.

The commit point message means we have written the data to disk so
we should not be buffering it any longer.
We do not currently fsync(2) the data after flushing, perhaps we should.
This commit is contained in:
Todd C. Miller 2021-10-15 19:10:34 -06:00
parent 3e4f6aa2e3
commit a554629d84
8 changed files with 114 additions and 13 deletions

View File

@ -125,6 +125,7 @@ lib/iolog/iolog_clearerr.c
lib/iolog/iolog_close.c
lib/iolog/iolog_conf.c
lib/iolog/iolog_eof.c
lib/iolog/iolog_flush.c
lib/iolog/iolog_gets.c
lib/iolog/iolog_json.c
lib/iolog/iolog_json.h

View File

@ -121,6 +121,7 @@ off_t iolog_seek(struct iolog_file *iol, off_t offset, int whence);
ssize_t iolog_read(struct iolog_file *iol, void *buf, size_t nbytes, const char **errstr);
ssize_t iolog_write(struct iolog_file *iol, const void *buf, size_t len, const char **errstr);
void iolog_clearerr(struct iolog_file *iol);
bool iolog_flush(struct iolog_file *iol, const char **errstr);
void iolog_rewind(struct iolog_file *iol);
unsigned int iolog_get_maxseq(void);
uid_t iolog_get_uid(void);

View File

@ -98,8 +98,8 @@ DEVEL = @DEVEL@
SHELL = @SHELL@
LIBIOLOG_OBJS = host_port.lo hostcheck.lo iolog_clearerr.lo iolog_close.lo \
iolog_conf.lo iolog_eof.lo iolog_gets.lo iolog_json.lo \
iolog_legacy.lo iolog_loginfo.lo iolog_mkdirs.lo \
iolog_conf.lo iolog_eof.lo iolog_flush.lo iolog_gets.lo \
iolog_json.lo iolog_legacy.lo iolog_loginfo.lo iolog_mkdirs.lo \
iolog_mkdtemp.lo iolog_mkpath.lo iolog_nextid.lo \
iolog_open.lo iolog_openat.lo iolog_path.lo iolog_read.lo \
iolog_seek.lo iolog_swapids.lo iolog_timing.lo iolog_util.lo \
@ -551,6 +551,18 @@ iolog_eof.i: $(srcdir)/iolog_eof.c $(incdir)/compat/stdbool.h \
$(CC) -E -o $@ $(CPPFLAGS) $<
iolog_eof.plog: iolog_eof.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/iolog_eof.c --i-file $< --output-file $@
iolog_flush.lo: $(srcdir)/iolog_flush.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_iolog.h $(incdir)/sudo_queue.h \
$(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/iolog_flush.c
iolog_flush.i: $(srcdir)/iolog_flush.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_iolog.h $(incdir)/sudo_queue.h \
$(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
iolog_flush.plog: iolog_flush.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/iolog_flush.c --i-file $< --output-file $@
iolog_gets.lo: $(srcdir)/iolog_gets.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_iolog.h $(incdir)/sudo_queue.h \

View File

@ -45,8 +45,8 @@ static mode_t iolog_dirmode = S_IRWXU;
static uid_t iolog_uid = ROOT_UID;
static gid_t iolog_gid = ROOT_GID;
static bool iolog_gid_set;
static bool iolog_compress;
static bool iolog_flush;
static bool iolog_docompress;
static bool iolog_doflush;
/*
* Reset I/O log settings to default values.
@ -60,8 +60,8 @@ iolog_set_defaults(void)
iolog_uid = ROOT_UID;
iolog_gid = ROOT_GID;
iolog_gid_set = false;
iolog_compress = false;
iolog_flush = false;
iolog_docompress = false;
iolog_doflush = false;
}
/*
@ -134,24 +134,24 @@ iolog_set_mode(mode_t mode)
}
/*
* Set iolog_compress
* Set iolog_docompress
*/
void
iolog_set_compress(bool newval)
{
debug_decl(iolog_set_compress, SUDO_DEBUG_UTIL);
iolog_compress = newval;
iolog_docompress = newval;
debug_return;
}
/*
* Set iolog_flush
* Set iolog_doflush
*/
void
iolog_set_flush(bool newval)
{
debug_decl(iolog_set_flush, SUDO_DEBUG_UTIL);
iolog_flush = newval;
iolog_doflush = newval;
debug_return;
}
@ -192,11 +192,11 @@ iolog_get_dir_mode(void)
bool
iolog_get_compress(void)
{
return iolog_compress;
return iolog_docompress;
}
bool
iolog_get_flush(void)
{
return iolog_flush;
return iolog_doflush;
}

66
lib/iolog/iolog_flush.c Normal file
View File

@ -0,0 +1,66 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2021 Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include "sudo_compat.h"
#include "sudo_debug.h"
#include "sudo_iolog.h"
/*
* I/O log wrapper for fflush/gzflush.
*/
bool
iolog_flush(struct iolog_file *iol, const char **errstr)
{
debug_decl(iolog_flush, SUDO_DEBUG_UTIL);
bool ret = true;
#ifdef HAVE_ZLIB_H
if (iol->compressed) {
int errnum;
if (gzflush(iol->fd.g, Z_SYNC_FLUSH) != Z_OK) {
if (errstr != NULL) {
*errstr = gzerror(iol->fd.g, &errnum);
if (errnum == Z_ERRNO)
*errstr = strerror(errno);
}
ret = false;
}
} else
#endif
{
if (fflush(iol->fd.f) != 0) {
if (errstr != NULL)
*errstr = strerror(errno);
ret = false;
}
}
debug_return_bool(ret);
}

View File

@ -633,7 +633,7 @@ iolog_close_all(struct connection_closure *closure)
{
const char *errstr;
int i;
debug_decl(iolog_close, SUDO_DEBUG_UTIL);
debug_decl(iolog_close_all, SUDO_DEBUG_UTIL);
for (i = 0; i < IOFD_MAX; i++) {
if (!closure->iolog_files[i].enabled)
@ -648,6 +648,25 @@ iolog_close_all(struct connection_closure *closure)
debug_return;
}
bool
iolog_flush_all(struct connection_closure *closure)
{
const char *errstr;
int i, ret = true;
debug_decl(iolog_flush_all, SUDO_DEBUG_UTIL);
for (i = 0; i < IOFD_MAX; i++) {
if (!closure->iolog_files[i].enabled)
continue;
if (!iolog_flush(&closure->iolog_files[i], &errstr)) {
sudo_warnx(U_("error flushing iofd %d: %s"), i, errstr);
ret = false;
}
}
debug_return_bool(ret);
}
bool
iolog_init(AcceptMessage *msg, struct connection_closure *closure)
{

View File

@ -1164,6 +1164,7 @@ server_commit_cb(int unused, int what, void *v)
commit_point.tv_nsec = closure->elapsed_time.tv_nsec;
if (!schedule_commit_point(&commit_point, closure))
connection_close(closure);
iolog_flush_all(closure);
debug_return;
}

View File

@ -185,6 +185,7 @@ struct eventlog *evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_
bool iolog_init(AcceptMessage *msg, struct connection_closure *closure);
bool iolog_create(int iofd, struct connection_closure *closure);
void iolog_close_all(struct connection_closure *closure);
bool iolog_flush_all(struct connection_closure *closure);
bool iolog_rewrite(const struct timespec *target, struct connection_closure *closure);
void update_elapsed_time(TimeSpec *delta, struct timespec *elapsed);