/src/sudo/lib/iolog/iolog_close.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2009-2021 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <stdio.h> |
27 | | #include <stdlib.h> |
28 | | #ifdef HAVE_STDBOOL_H |
29 | | # include <stdbool.h> |
30 | | #else |
31 | | # include "compat/stdbool.h" |
32 | | #endif |
33 | | #include <string.h> |
34 | | #include <errno.h> |
35 | | #include <time.h> |
36 | | |
37 | | #include "sudo_compat.h" |
38 | | #include "sudo_debug.h" |
39 | | #include "sudo_iolog.h" |
40 | | |
41 | | /* |
42 | | * Close an I/O log. |
43 | | */ |
44 | | bool |
45 | | iolog_close(struct iolog_file *iol, const char **errstr) |
46 | 843 | { |
47 | 843 | bool ret = true; |
48 | 843 | debug_decl(iolog_close, SUDO_DEBUG_UTIL); |
49 | | |
50 | | #ifdef HAVE_ZLIB_H |
51 | | if (iol->compressed) { |
52 | | int errnum; |
53 | | |
54 | | /* Must check error indicator before closing. */ |
55 | | if (iol->writable) { |
56 | | if (gzflush(iol->fd.g, Z_SYNC_FLUSH) != Z_OK) { |
57 | | ret = false; |
58 | | if (errstr != NULL) { |
59 | | *errstr = gzerror(iol->fd.g, &errnum); |
60 | | if (errnum == Z_ERRNO) |
61 | | *errstr = strerror(errno); |
62 | | } |
63 | | } |
64 | | } |
65 | | errnum = gzclose(iol->fd.g); |
66 | | if (ret && errnum != Z_OK) { |
67 | | ret = false; |
68 | | if (errstr != NULL) |
69 | | *errstr = errnum == Z_ERRNO ? strerror(errno) : "unknown error"; |
70 | | } |
71 | | } else |
72 | | #endif |
73 | 843 | if (fclose(iol->fd.f) != 0) { |
74 | 0 | ret = false; |
75 | 0 | if (errstr != NULL) |
76 | 0 | *errstr = strerror(errno); |
77 | 0 | } |
78 | | |
79 | 843 | debug_return_bool(ret); |
80 | 843 | } |