diff --git a/configure.ac b/configure.ac index 88cbd7c5..00ae505d 100644 --- a/configure.ac +++ b/configure.ac @@ -38,7 +38,7 @@ AC_CHECK_HEADERS(stdio.h stdlib.h fcntl.h sys/time.h unistd.h) AC_CHECK_HEADERS(ctype.h errno.h byteswap.h string.h time.h) AC_CHECK_HEADERS(endian.h sys/endian.h arpa/inet.h syslog.h) AC_CHECK_HEADERS(alloca.h pthread.h stdio.h math.h) -AC_CHECK_HEADERS(sys/types.h sys/socket.h netdb.h) +AC_CHECK_HEADERS(sys/types.h sys/socket.h linux/un.h netdb.h) AC_CHECK_HEADERS(stdint.h netinet/in.h netinet/tcp.h) AC_CHECK_HEADERS(jansson.h) diff --git a/src/libckpool.c b/src/libckpool.c index cb3788e8..6d9dc545 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -11,6 +11,11 @@ #include #include +#ifdef HAVE_LINUX_UN_H +#include +#else +#include +#endif #include #include #include @@ -26,6 +31,10 @@ #include "libckpool.h" #include "sha2.h" +#ifndef UNIX_PATH_MAX +#define UNIX_PATH_MAX 108 +#endif + /* Place holders for when we add lock debugging */ #define GETLOCK(_lock, _file, _func, _line) #define GOTLOCK(_lock, _file, _func, _line) @@ -220,6 +229,7 @@ void _ck_wunlock(cklock_t *lock, const char *file, const char *func, const int l _mutex_unlock(&lock->mutex, file, func, line); } + bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port) { char *url_begin, *url_end, *ipv6_begin, *ipv6_end, *port_start = NULL; @@ -490,6 +500,53 @@ void empty_socket(int fd) } while (ret > 0); } +int open_unix_server(const char *server_path) +{ + struct sockaddr_un serveraddr; + int sockd = -1, len, ret; + + if (likely(server_path)) { + len = strlen(server_path); + if (unlikely(len < 1 || len > UNIX_PATH_MAX)) { + LOGERR("Invalid server path length %d in open_unix_server", len); + goto out; + } + } else { + LOGERR("Null passed as server_path to open_unix_server"); + goto out; + } + + sockd = socket(AF_UNIX, SOCK_STREAM, 0); + if (unlikely(sockd < 0)) { + LOGERR("Failed to open socket in open_unix_server with errno %d", errno); + goto out; + } + memset(&serveraddr, 0, sizeof(serveraddr)); + serveraddr.sun_family = AF_UNIX; + strcpy(serveraddr.sun_path, server_path); + + ret = bind(sockd, (struct sockaddr *)&serveraddr, sizeof(&serveraddr)); + if (unlikely(ret < 0)) { + LOGERR("Failed to bind to socket in open_unix_server with errno %d", errno); + close(sockd); + sockd = -1; + goto out; + } + + ret = listen(sockd, 1); + if (unlikely(ret < 0)) { + LOGERR("Failed to listen to socket in open_unix_server with errno %d", errno); + close(sockd); + sockd = -1; + goto out; + } + + LOGDEBUG("Opened server path %s successfully on socket %d", server_path, sockd); +out: + return sockd; +} + + json_t *json_rpc_call(connsock_t *cs, const char *rpc_req) { char http_req[PAGESIZE]; diff --git a/src/libckpool.h b/src/libckpool.h index e4615256..27caa598 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -182,6 +182,8 @@ int connect_socket(char *url, char *port); int write_socket(int fd, const void *buf, size_t nbyte); int read_socket_line(connsock_t *cs); void empty_socket(int fd); +int open_unix_server(const char *server_path); + json_t *json_rpc_call(connsock_t *cs, const char *rpc_req); void align_len(size_t *len);