aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-06-08 23:57:49 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-06-09 00:01:11 +0200
commitfb80276084a9adc0014b1008f8bf8253f126293b (patch)
tree2951be23576c32ddcda656c04c98539591981622
parentdocker: bump base image (diff)
downloadcimple-fb80276084a9adc0014b1008f8bf8253f126293b.tar.gz
cimple-fb80276084a9adc0014b1008f8bf8253f126293b.zip
add {file,net}_close as a wrapper to close(2)
-rw-r--r--src/file.c5
-rw-r--r--src/file.h2
-rw-r--r--src/msg.c2
-rw-r--r--src/net.c12
-rw-r--r--src/net.h1
-rw-r--r--src/process.c6
-rw-r--r--src/tcp_server.c5
-rw-r--r--src/worker_queue.c4
8 files changed, 25 insertions, 12 deletions
diff --git a/src/file.c b/src/file.c
index 4aa0003..f40f849 100644
--- a/src/file.c
+++ b/src/file.c
@@ -98,6 +98,11 @@ free:
return NULL;
}
+void file_close(int fd)
+{
+ log_errno_if(close(fd), "close");
+}
+
int file_exists(const char *path)
{
struct stat stat;
diff --git a/src/file.h b/src/file.h
index ec989c3..c25ef40 100644
--- a/src/file.h
+++ b/src/file.h
@@ -15,6 +15,8 @@ int rm_rf(const char *dir);
int my_chdir(const char *dir, char **old);
char *my_readlink(const char *path);
+void file_close(int fd);
+
int file_exists(const char *path);
int file_read(int fd, char **output, size_t *size);
diff --git a/src/msg.c b/src/msg.c
index 918ae43..7bce01d 100644
--- a/src/msg.c
+++ b/src/msg.c
@@ -245,7 +245,7 @@ int msg_connect_and_communicate(const char *host, const char *port, const struct
goto close;
close:
- log_errno_if(close(fd), "close");
+ net_close(fd);
return ret;
}
diff --git a/src/net.c b/src/net.c
index a641d3b..463f5f8 100644
--- a/src/net.c
+++ b/src/net.c
@@ -6,6 +6,7 @@
*/
#include "net.h"
+#include "file.h"
#include "log.h"
#include <netdb.h>
@@ -65,7 +66,7 @@ int net_bind(const char *port)
break;
close_socket:
- log_errno_if(close(socket_fd), "close");
+ net_close(socket_fd);
}
freeaddrinfo(result);
@@ -84,7 +85,7 @@ int net_bind(const char *port)
return socket_fd;
fail:
- log_errno_if(close(socket_fd), "close");
+ net_close(socket_fd);
return ret;
}
@@ -133,7 +134,7 @@ int net_connect(const char *host, const char *port)
break;
close_socket:
- log_errno_if(close(socket_fd), "close");
+ net_close(socket_fd);
}
freeaddrinfo(result);
@@ -146,6 +147,11 @@ int net_connect(const char *host, const char *port)
return socket_fd;
}
+void net_close(int fd)
+{
+ file_close(fd);
+}
+
static ssize_t net_send_part(int fd, const void *buf, size_t size)
{
static const int flags = MSG_NOSIGNAL;
diff --git a/src/net.h b/src/net.h
index 3151cba..618dbc3 100644
--- a/src/net.h
+++ b/src/net.h
@@ -14,6 +14,7 @@
int net_bind(const char *port);
int net_accept(int fd);
int net_connect(const char *host, const char *port);
+void net_close(int fd);
int net_send(int fd, const void *, size_t);
int net_recv(int fd, void *, size_t);
diff --git a/src/process.c b/src/process.c
index c0888b4..f38f4f5 100644
--- a/src/process.c
+++ b/src/process.c
@@ -66,7 +66,7 @@ static int redirect_and_exec_child(int pipe_fds[2], const char *args[], const ch
{
int ret = 0;
- log_errno_if(close(pipe_fds[0]), "close");
+ file_close(pipe_fds[0]);
ret = dup2(pipe_fds[1], STDOUT_FILENO);
if (ret < 0) {
@@ -103,7 +103,7 @@ int proc_capture(const char *args[], const char *envp[], struct proc_output *res
if (!child_pid)
exit(redirect_and_exec_child(pipe_fds, args, envp));
- log_errno_if(close(pipe_fds[1]), "close");
+ file_close(pipe_fds[1]);
ret = file_read(pipe_fds[0], &result->output, &result->output_len);
if (ret < 0)
@@ -119,7 +119,7 @@ free_output:
free(result->output);
close_pipe:
- log_errno_if(close(pipe_fds[0]), "close");
+ file_close(pipe_fds[0]);
/* No errno checking here, we might've already closed the write end. */
close(pipe_fds[1]);
diff --git a/src/tcp_server.c b/src/tcp_server.c
index 277f81e..2e65bc5 100644
--- a/src/tcp_server.c
+++ b/src/tcp_server.c
@@ -13,7 +13,6 @@
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
-#include <unistd.h>
struct tcp_server {
int fd;
@@ -45,7 +44,7 @@ free:
void tcp_server_destroy(struct tcp_server *server)
{
- log_errno_if(close(server->fd), "close");
+ net_close(server->fd);
free(server);
}
@@ -116,7 +115,7 @@ restore_mask:
signal_restore(&old_mask);
close_conn:
- log_errno_if(close(ctx->fd), "close");
+ net_close(ctx->fd);
free_ctx:
free(ctx);
diff --git a/src/worker_queue.c b/src/worker_queue.c
index 06119e2..b0dda38 100644
--- a/src/worker_queue.c
+++ b/src/worker_queue.c
@@ -7,11 +7,11 @@
#include "worker_queue.h"
#include "log.h"
+#include "net.h"
#include <pthread.h>
#include <stdlib.h>
#include <sys/queue.h>
-#include <unistd.h>
struct worker {
pthread_t thread;
@@ -38,7 +38,7 @@ void worker_destroy(struct worker *entry)
{
log("Waiting for worker %d thread to exit\n", entry->fd);
pthread_errno_if(pthread_join(entry->thread, NULL), "pthread_join");
- log_errno_if(close(entry->fd), "close");
+ net_close(entry->fd);
free(entry);
}