mirror of
https://github.com/systemd/systemd.git
synced 2025-02-27 01:57:35 +03:00
Merge pull request #30338 from keszybz/fwrite-error-handling
Fix fwrite() error handling
This commit is contained in:
commit
4cb8f4abd4
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "analyze.h"
|
#include "analyze.h"
|
||||||
#include "analyze-srk.h"
|
#include "analyze-srk.h"
|
||||||
|
#include "fileio.h"
|
||||||
#include "tpm2-util.h"
|
#include "tpm2-util.h"
|
||||||
|
|
||||||
int verb_srk(int argc, char *argv[], void *userdata) {
|
int verb_srk(int argc, char *argv[], void *userdata) {
|
||||||
@ -33,12 +34,15 @@ int verb_srk(int argc, char *argv[], void *userdata) {
|
|||||||
return log_error_errno(r, "Failed to marshal SRK: %m");
|
return log_error_errno(r, "Failed to marshal SRK: %m");
|
||||||
|
|
||||||
if (isatty(STDOUT_FILENO))
|
if (isatty(STDOUT_FILENO))
|
||||||
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Refusing to write binary data to TTY, please redirect output to file.");
|
return log_error_errno(SYNTHETIC_ERRNO(EIO),
|
||||||
|
"Refusing to write binary data to TTY, please redirect output to file.");
|
||||||
|
|
||||||
if (fwrite(marshalled, 1, marshalled_size, stdout) != marshalled_size)
|
if (fwrite(marshalled, 1, marshalled_size, stdout) != marshalled_size)
|
||||||
return log_error_errno(errno, "Failed to write SRK to stdout: %m");
|
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write SRK to stdout: %m");
|
||||||
|
|
||||||
fflush(stdout);
|
r = fflush_and_check(stdout);
|
||||||
|
if (r < 0)
|
||||||
|
return log_error_errno(r, "Failed to write SRK to stdout: %m");
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
#else
|
#else
|
||||||
|
@ -350,14 +350,15 @@ static int write_blob(FILE *f, const void *data, size_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fwrite(data, 1, size, f) != size)
|
if (fwrite(data, 1, size, f) != size)
|
||||||
return log_error_errno(errno, "Failed to write credential data: %m");
|
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write credential data: %m");
|
||||||
|
|
||||||
r = print_newline(f, data, size);
|
r = print_newline(f, data, size);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
if (fflush(f) != 0)
|
r = fflush_and_check(f);
|
||||||
return log_error_errno(errno, "Failed to flush output: %m");
|
if (r < 0)
|
||||||
|
return log_error_errno(r, "Failed to flush output: %m");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#include "conf-files.h"
|
#include "conf-files.h"
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
#include "fileio.h"
|
#include "fileio.h"
|
||||||
|
#include "fs-util.h"
|
||||||
#include "hashmap.h"
|
#include "hashmap.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "memory-util.h"
|
#include "memory-util.h"
|
||||||
@ -382,10 +383,8 @@ static int64_t write_catalog(
|
|||||||
CatalogItem *items,
|
CatalogItem *items,
|
||||||
size_t n) {
|
size_t n) {
|
||||||
|
|
||||||
|
_cleanup_(unlink_and_freep) char *p = NULL;
|
||||||
_cleanup_fclose_ FILE *w = NULL;
|
_cleanup_fclose_ FILE *w = NULL;
|
||||||
_cleanup_free_ char *p = NULL;
|
|
||||||
CatalogHeader header;
|
|
||||||
size_t k;
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
r = mkdir_parents(database, 0755);
|
r = mkdir_parents(database, 0755);
|
||||||
@ -394,54 +393,35 @@ static int64_t write_catalog(
|
|||||||
|
|
||||||
r = fopen_temporary(database, &w, &p);
|
r = fopen_temporary(database, &w, &p);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_error_errno(r, "Failed to open database for writing: %s: %m",
|
return log_error_errno(r, "Failed to open database for writing: %s: %m", database);
|
||||||
database);
|
|
||||||
|
|
||||||
header = (CatalogHeader) {
|
CatalogHeader header = {
|
||||||
.signature = CATALOG_SIGNATURE,
|
.signature = CATALOG_SIGNATURE,
|
||||||
.header_size = htole64(CONST_ALIGN_TO(sizeof(CatalogHeader), 8)),
|
.header_size = htole64(CONST_ALIGN_TO(sizeof(CatalogHeader), 8)),
|
||||||
.catalog_item_size = htole64(sizeof(CatalogItem)),
|
.catalog_item_size = htole64(sizeof(CatalogItem)),
|
||||||
.n_items = htole64(n),
|
.n_items = htole64(n),
|
||||||
};
|
};
|
||||||
|
|
||||||
r = -EIO;
|
if (fwrite(&header, sizeof(header), 1, w) != 1)
|
||||||
|
return log_error_errno(SYNTHETIC_ERRNO(EIO), "%s: failed to write header.", p);
|
||||||
|
|
||||||
k = fwrite(&header, 1, sizeof(header), w);
|
if (fwrite(items, sizeof(CatalogItem), n, w) != n)
|
||||||
if (k != sizeof(header)) {
|
return log_error_errno(SYNTHETIC_ERRNO(EIO), "%s: failed to write database.", p);
|
||||||
log_error("%s: failed to write header.", p);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
k = fwrite(items, 1, n * sizeof(CatalogItem), w);
|
if (fwrite(sb->buf, sb->len, 1, w) != 1)
|
||||||
if (k != n * sizeof(CatalogItem)) {
|
return log_error_errno(SYNTHETIC_ERRNO(EIO), "%s: failed to write strings.", p);
|
||||||
log_error("%s: failed to write database.", p);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
k = fwrite(sb->buf, 1, sb->len, w);
|
|
||||||
if (k != sb->len) {
|
|
||||||
log_error("%s: failed to write strings.", p);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = fflush_and_check(w);
|
r = fflush_and_check(w);
|
||||||
if (r < 0) {
|
if (r < 0)
|
||||||
log_error_errno(r, "%s: failed to write database: %m", p);
|
return log_error_errno(r, "%s: failed to write database: %m", p);
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
(void) fchmod(fileno(w), 0644);
|
(void) fchmod(fileno(w), 0644);
|
||||||
|
|
||||||
if (rename(p, database) < 0) {
|
if (rename(p, database) < 0)
|
||||||
r = log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database);
|
return log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database);
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
p = mfree(p); /* free without unlinking */
|
||||||
return ftello(w);
|
return ftello(w);
|
||||||
|
|
||||||
error:
|
|
||||||
(void) unlink(p);
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int catalog_update(const char* database, const char* root, const char* const* dirs) {
|
int catalog_update(const char* database, const char* root, const char* const* dirs) {
|
||||||
|
@ -384,7 +384,7 @@ static int journal_file_object_verify(JournalFile *f, uint64_t offset, Object *o
|
|||||||
|
|
||||||
static int write_uint64(FILE *fp, uint64_t p) {
|
static int write_uint64(FILE *fp, uint64_t p) {
|
||||||
if (fwrite(&p, sizeof(p), 1, fp) != 1)
|
if (fwrite(&p, sizeof(p), 1, fp) != 1)
|
||||||
return -errno;
|
return -EIO;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -147,8 +147,8 @@ int link_lldp_save(Link *link) {
|
|||||||
goto finish;
|
goto finish;
|
||||||
|
|
||||||
u = htole64(sz);
|
u = htole64(sz);
|
||||||
(void) fwrite(&u, 1, sizeof(u), f);
|
fwrite(&u, 1, sizeof(u), f);
|
||||||
(void) fwrite(p, 1, sz, f);
|
fwrite(p, 1, sz, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
r = fflush_and_check(f);
|
r = fflush_and_check(f);
|
||||||
|
@ -284,7 +284,8 @@ static int run(int argc, char *argv[]) {
|
|||||||
if (runtime_key.pkey) {
|
if (runtime_key.pkey) {
|
||||||
if (memcmp_nn(tpm2_key.fingerprint, tpm2_key.fingerprint_size,
|
if (memcmp_nn(tpm2_key.fingerprint, tpm2_key.fingerprint_size,
|
||||||
runtime_key.fingerprint, runtime_key.fingerprint_size) != 0)
|
runtime_key.fingerprint, runtime_key.fingerprint_size) != 0)
|
||||||
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Saved runtime SRK differs from TPM SRK, refusing.");
|
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
|
||||||
|
"Saved runtime SRK differs from TPM SRK, refusing.");
|
||||||
|
|
||||||
if (arg_early) {
|
if (arg_early) {
|
||||||
log_info("SRK saved in '%s' matches SRK in TPM2.", runtime_key.path);
|
log_info("SRK saved in '%s' matches SRK in TPM2.", runtime_key.path);
|
||||||
@ -351,7 +352,8 @@ static int run(int argc, char *argv[]) {
|
|||||||
return log_error_errno(r, "Failed to marshal TPM2_PUBLIC key.");
|
return log_error_errno(r, "Failed to marshal TPM2_PUBLIC key.");
|
||||||
|
|
||||||
if (fwrite(marshalled, 1, marshalled_size, f) != marshalled_size)
|
if (fwrite(marshalled, 1, marshalled_size, f) != marshalled_size)
|
||||||
return log_error_errno(errno, "Failed to write SRK public key file '%s'.", tpm2b_public_path);
|
return log_error_errno(SYNTHETIC_ERRNO(EIO),
|
||||||
|
"Failed to write SRK public key file '%s'.", tpm2b_public_path);
|
||||||
|
|
||||||
if (fchmod(fileno(f), 0444) < 0)
|
if (fchmod(fileno(f), 0444) < 0)
|
||||||
return log_error_errno(errno, "Failed to adjust access mode of SRK public key file '%s' to 0444: %m", tpm2b_public_path);
|
return log_error_errno(errno, "Failed to adjust access mode of SRK public key file '%s' to 0444: %m", tpm2b_public_path);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user