/src/sudo/lib/iolog/iolog_close.c
Line | Count | Source |
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 | | #include <config.h> |
20 | | |
21 | | #include <stdio.h> |
22 | | #include <stdlib.h> |
23 | | #ifdef HAVE_STDBOOL_H |
24 | | # include <stdbool.h> |
25 | | #else |
26 | | # include <compat/stdbool.h> |
27 | | #endif |
28 | | #include <string.h> |
29 | | #include <errno.h> |
30 | | #include <time.h> |
31 | | |
32 | | #include <sudo_compat.h> |
33 | | #include <sudo_debug.h> |
34 | | #include <sudo_iolog.h> |
35 | | |
36 | | /* |
37 | | * Close an I/O log. |
38 | | */ |
39 | | bool |
40 | | iolog_close(struct iolog_file *iol, const char **errstr) |
41 | 825 | { |
42 | 825 | bool ret = true; |
43 | 825 | debug_decl(iolog_close, SUDO_DEBUG_UTIL); |
44 | | |
45 | | #ifdef HAVE_ZLIB_H |
46 | | if (iol->compressed) { |
47 | | int errnum; |
48 | | |
49 | | /* Must check error indicator before closing. */ |
50 | | if (iol->writable) { |
51 | | if (gzflush(iol->fd.g, Z_SYNC_FLUSH) != Z_OK) { |
52 | | ret = false; |
53 | | if (errstr != NULL) { |
54 | | *errstr = gzerror(iol->fd.g, &errnum); |
55 | | if (errnum == Z_ERRNO) |
56 | | *errstr = strerror(errno); |
57 | | } |
58 | | } |
59 | | } |
60 | | errnum = gzclose(iol->fd.g); |
61 | | if (ret && errnum != Z_OK) { |
62 | | ret = false; |
63 | | if (errstr != NULL) |
64 | | *errstr = errnum == Z_ERRNO ? strerror(errno) : "unknown error"; |
65 | | } |
66 | | } else |
67 | | #endif |
68 | 825 | if (fclose(iol->fd.f) != 0) { |
69 | 0 | ret = false; |
70 | 0 | if (errstr != NULL) |
71 | 0 | *errstr = strerror(errno); |
72 | 0 | } |
73 | | |
74 | | debug_return_bool(ret); |
75 | 825 | } |