/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2009-2021 Todd C. Miller <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718#include <config.h>1920#include <stdio.h>21#include <stdlib.h>22#ifdef HAVE_STDBOOL_H23# include <stdbool.h>24#else25# include <compat/stdbool.h>26#endif27#include <string.h>28#include <errno.h>29#include <time.h>3031#include <sudo_compat.h>32#include <sudo_debug.h>33#include <sudo_iolog.h>3435/*36* Close an I/O log.37*/38bool39iolog_close(struct iolog_file *iol, const char **errstr)40{41bool ret = true;42debug_decl(iolog_close, SUDO_DEBUG_UTIL);4344#ifdef HAVE_ZLIB_H45if (iol->compressed) {46int errnum;4748/* Must check error indicator before closing. */49if (iol->writable) {50if (gzflush(iol->fd.g, Z_SYNC_FLUSH) != Z_OK) {51ret = false;52if (errstr != NULL) {53*errstr = gzerror(iol->fd.g, &errnum);54if (errnum == Z_ERRNO)55*errstr = strerror(errno);56}57}58}59errnum = gzclose(iol->fd.g);60if (ret && errnum != Z_OK) {61ret = false;62if (errstr != NULL)63*errstr = errnum == Z_ERRNO ? strerror(errno) : "unknown error";64}65} else66#endif67if (fclose(iol->fd.f) != 0) {68ret = false;69if (errstr != NULL)70*errstr = strerror(errno);71}7273debug_return_bool(ret);74}757677