From 7453e82a270f3b56ec5e0f93b5357024e9f97213 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Thu, 19 Sep 2013 04:09:16 +0930 Subject: [PATCH] Issue #20: improve new socket function logging __whence argument and function macros --- serval.h | 26 +++++++++++++++++--------- socket.c | 14 +++++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/serval.h b/serval.h index b41dbf3f..c1ea4316 100644 --- a/serval.h +++ b/serval.h @@ -168,16 +168,24 @@ int create_serval_instance_dir(); int form_serval_instance_path(char *buf, size_t bufsiz, const char *path); void serval_setinstancepath(const char *instancepath); -/* Create a named socket using abstract socket on Linux or a local socket on - * other platforms. +/* Basic socket operations. */ -int esocket(int domain, int type, int protocol); -int socket_setname(struct sockaddr_un *sockname, const char *name, socklen_t *addrlen); -int socket_bind(int sock, const struct sockaddr *addr, socklen_t addrlen); -int socket_connect(int sock, const struct sockaddr *addr, socklen_t addrlen); -int socket_listen(int sock, int backlog); -int socket_set_reuseaddr(int fd, int reuseP); -int socket_set_rcvbufsize(int fd, unsigned buffer_size); +int _esocket(struct __sourceloc __whence, int domain, int type, int protocol); +int _socket_setname(struct __sourceloc __whence, struct sockaddr_un *sockname, const char *name, socklen_t *addrlen); +int _socket_bind(struct __sourceloc __whence, int sock, const struct sockaddr *addr, socklen_t addrlen); +int _socket_connect(struct __sourceloc __whence, int sock, const struct sockaddr *addr, socklen_t addrlen); +int _socket_listen(struct __sourceloc __whence, int sock, int backlog); +int _socket_set_reuseaddr(struct __sourceloc __whence, int sock, int reuseP); +int _socket_set_rcvbufsize(struct __sourceloc __whence, int sock, unsigned buffer_size); + +#define esocket(domain, type, protocol) _esocket(__HERE__, domain, type, protocol) +#define socket_setname(sockname, name, addrlenp) _socket_setname(__HERE__, sockname, name, addrlenp) +#define socket_bind(sock, addr, addrlen) _socket_bind(__HERE__, sock, addr, addrlen) +#define socket_connect(sock, addr, addrlen) _socket_connect(__HERE__, sock, addr, addrlen) +#define socket_listen(sock, backlog) _socket_listen(__HERE__, sock, backlog) +#define socket_set_reuseaddr(sock, reuseP) _socket_set_reuseaddr(__HERE__, sock, reuseP) +#define socket_set_rcvbufsize(sock, buffer_size) _socket_set_rcvbufsize(__HERE__, sock, buffer_size) + #define SERVER_CONFIG_RELOAD_INTERVAL_MS 1000 diff --git a/socket.c b/socket.c index 370e3a94..c48f448f 100644 --- a/socket.c +++ b/socket.c @@ -31,7 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * @author Andrew Bettison * @author Daniel O'Connor */ -int socket_setname(struct sockaddr_un *addr, const char *name, socklen_t *addrlen) +int _socket_setname(struct __sourceloc __whence, struct sockaddr_un *addr, const char *name, socklen_t *addrlen) { bzero(addr, sizeof(*addr)); addr->sun_family = AF_UNIX; @@ -49,7 +49,7 @@ int socket_setname(struct sockaddr_un *addr, const char *name, socklen_t *addrle return 0; } -int esocket(int domain, int type, int protocol) +int _esocket(struct __sourceloc __whence, int domain, int type, int protocol) { int fd; if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) @@ -59,7 +59,7 @@ int esocket(int domain, int type, int protocol) return fd; } -int socket_connect(int sock, const struct sockaddr *addr, socklen_t addrlen) +int _socket_connect(struct __sourceloc __whence, int sock, const struct sockaddr *addr, socklen_t addrlen) { if (connect(sock, (struct sockaddr *)addr, addrlen) == -1) return WHYF_perror("connect(%d,%s,%lu)", sock, alloca_sockaddr(addr, addrlen), (unsigned long)addrlen); @@ -68,7 +68,7 @@ int socket_connect(int sock, const struct sockaddr *addr, socklen_t addrlen) return 0; } -int socket_bind(int sock, const struct sockaddr *addr, socklen_t addrlen) +int _socket_bind(struct __sourceloc __whence, int sock, const struct sockaddr *addr, socklen_t addrlen) { if (addr->sa_family == AF_UNIX && ((struct sockaddr_un *)addr)->sun_path[0]) { if (unlink(((struct sockaddr_un *)addr)->sun_path) == -1 && errno != ENOENT) @@ -83,7 +83,7 @@ int socket_bind(int sock, const struct sockaddr *addr, socklen_t addrlen) return 0; } -int socket_listen(int sock, int backlog) +int _socket_listen(struct __sourceloc __whence, int sock, int backlog) { if (listen(sock, backlog) == -1) return WHYF_perror("listen(%d,%d)", sock, backlog); @@ -92,7 +92,7 @@ int socket_listen(int sock, int backlog) return 0; } -int socket_set_reuseaddr(int sock, int reuseP) +int _socket_set_reuseaddr(struct __sourceloc __whence, int sock, int reuseP) { if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseP, sizeof reuseP) == -1) { WARNF_perror("setsockopt(%d,SOL_SOCKET,SO_REUSEADDR,&%d,%u)", sock, reuseP, (unsigned)sizeof reuseP); @@ -103,7 +103,7 @@ int socket_set_reuseaddr(int sock, int reuseP) return 0; } -int socket_set_rcvbufsize(int sock, unsigned buffer_size) +int _socket_set_rcvbufsize(struct __sourceloc __whence, int sock, unsigned buffer_size) { if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &buffer_size, sizeof buffer_size) == -1) { WARNF_perror("setsockopt(%d,SOL_SOCKET,SO_RCVBUF,&%u,%u)", sock, buffer_size, (unsigned)sizeof buffer_size);