From 27fd6f9615f36aec71877e2b99aa3655e22da75d Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 13 Jun 2023 05:08:30 +0200 Subject: minor refactoring --- src/net.c | 9 ++++++--- src/process.c | 3 ++- src/signal.c | 3 ++- src/sqlite.c | 13 ++++++++----- src/storage_sqlite.c | 6 +++--- 5 files changed, 21 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/net.c b/src/net.c index 463f5f8..e102395 100644 --- a/src/net.c +++ b/src/net.c @@ -21,6 +21,7 @@ int net_bind(const char *port) { + static const int flags = SOCK_CLOEXEC; struct addrinfo *result = NULL, *it = NULL; struct addrinfo hints; int socket_fd = -1, ret = 0; @@ -37,7 +38,7 @@ int net_bind(const char *port) } for (it = result; it; it = it->ai_next) { - socket_fd = socket(it->ai_family, it->ai_socktype | SOCK_CLOEXEC, it->ai_protocol); + socket_fd = socket(it->ai_family, it->ai_socktype | flags, it->ai_protocol); if (socket_fd < 0) { log_errno("socket"); continue; @@ -92,9 +93,10 @@ fail: int net_accept(int fd) { + static const int flags = SOCK_CLOEXEC; int ret = 0; - ret = accept4(fd, NULL, NULL, SOCK_CLOEXEC); + ret = accept4(fd, NULL, NULL, flags); if (ret < 0) { log_errno("accept"); return ret; @@ -105,6 +107,7 @@ int net_accept(int fd) int net_connect(const char *host, const char *port) { + static const int flags = SOCK_CLOEXEC; struct addrinfo *result = NULL, *it = NULL; struct addrinfo hints; int socket_fd = -1, ret = 0; @@ -120,7 +123,7 @@ int net_connect(const char *host, const char *port) } for (it = result; it; it = it->ai_next) { - socket_fd = socket(it->ai_family, it->ai_socktype | SOCK_CLOEXEC, it->ai_protocol); + socket_fd = socket(it->ai_family, it->ai_socktype | flags, it->ai_protocol); if (socket_fd < 0) { log_errno("socket"); continue; diff --git a/src/process.c b/src/process.c index f38f4f5..c908f5c 100644 --- a/src/process.c +++ b/src/process.c @@ -85,10 +85,11 @@ static int redirect_and_exec_child(int pipe_fds[2], const char *args[], const ch int proc_capture(const char *args[], const char *envp[], struct proc_output *result) { + static const int flags = O_CLOEXEC; int pipe_fds[2]; int ret = 0; - ret = pipe2(pipe_fds, O_CLOEXEC); + ret = pipe2(pipe_fds, flags); if (ret < 0) { log_errno("pipe2"); return -1; diff --git a/src/signal.c b/src/signal.c index 15d90ab..cbe4aed 100644 --- a/src/signal.c +++ b/src/signal.c @@ -57,6 +57,7 @@ int signal_block_sigterms(void) int signalfd_create(const sigset_t *set) { + static const int flags = SFD_CLOEXEC; sigset_t old; int ret = 0; @@ -64,7 +65,7 @@ int signalfd_create(const sigset_t *set) if (ret < 0) return ret; - ret = signalfd(-1, set, SFD_CLOEXEC); + ret = signalfd(-1, set, flags); if (ret < 0) goto restore; diff --git a/src/sqlite.c b/src/sqlite.c index 344e98a..7a2e482 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -200,10 +200,10 @@ int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result) int sqlite_exec_as_transaction(sqlite3 *db, const char *stmt) { - static const char *const FMT = "BEGIN; %s COMMIT;"; + static const char *const fmt = "BEGIN; %s COMMIT;"; int ret = 0; - ret = snprintf(NULL, 0, FMT, stmt); + ret = snprintf(NULL, 0, fmt, stmt); size_t nb = (size_t)ret + 1; ret = 0; @@ -212,7 +212,7 @@ int sqlite_exec_as_transaction(sqlite3 *db, const char *stmt) log_errno("malloc"); return -1; } - snprintf(full_stmt, nb, FMT, stmt); + snprintf(full_stmt, nb, fmt, stmt); ret = sqlite_exec(db, stmt, NULL); goto free; @@ -225,10 +225,12 @@ free: int sqlite_get_user_version(sqlite3 *db, unsigned int *output) { + static const char *const sql = "PRAGMA user_version;"; + sqlite3_stmt *stmt = NULL; int result = -1, ret = 0; - ret = sqlite_prepare(db, "PRAGMA user_version;", &stmt); + ret = sqlite_prepare(db, sql, &stmt); if (ret < 0) return ret; ret = sqlite_step(stmt); @@ -253,5 +255,6 @@ finalize: int sqlite_set_foreign_keys(sqlite3 *db) { - return sqlite_exec(db, "PRAGMA foreign_keys = ON;", NULL); + static const char *const sql = "PRAGMA foreign_keys = ON;"; + return sqlite_exec(db, sql, NULL); } diff --git a/src/storage_sqlite.c b/src/storage_sqlite.c index 0bd2e88..507f5b1 100644 --- a/src/storage_sqlite.c +++ b/src/storage_sqlite.c @@ -57,12 +57,12 @@ struct storage_sqlite { static int storage_upgrade_sqlite_to(struct storage_sqlite *storage, size_t version) { - static const char *const FMT = "%s PRAGMA user_version = %zu;"; + static const char *const fmt = "%s PRAGMA user_version = %zu;"; const char *script = sql_sqlite_files[version]; int ret = 0; - ret = snprintf(NULL, 0, FMT, script, version + 1); + ret = snprintf(NULL, 0, fmt, script, version + 1); size_t nb = (size_t)ret + 1; ret = 0; @@ -71,7 +71,7 @@ static int storage_upgrade_sqlite_to(struct storage_sqlite *storage, size_t vers log_errno("malloc"); return -1; } - snprintf(full_script, nb, FMT, script, version + 1); + snprintf(full_script, nb, fmt, script, version + 1); ret = sqlite_exec_as_transaction(storage->db, full_script); goto free; -- cgit v1.2.3