From: Gerrit Renker Date: Fri, 30 Nov 2007 09:23:51 +0000 (+0100) Subject: 06_Unix-Domain-Sockets.diff X-Git-Tag: v0.3.0~73^2~3 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=b9ff47c00a184f572037a05f40d9ea62222f514c;ds=sidebyside 06_Unix-Domain-Sockets.diff [LOCAL SOCKETS]: Tidy up and share common code This patch just suggested itself - after all the other changes have been made, the only remaining customers of PARA_CONNECT were the Unix domain sockets. The patch generates a counterpart to create_local_socket(), for the peer talking over a Unix domain socket. Signed-off-by: Gerrit Renker --- diff --git a/afs.c b/afs.c index 09d42126..ac7884c7 100644 --- a/afs.c +++ b/afs.c @@ -164,7 +164,6 @@ int send_callback_request(callback_function *f, struct osl_object *query, int ret, fd = -1, query_shmid, result_shmid; void *query_shm, *result_shm; char buf[sizeof(afs_socket_cookie) + sizeof(int)]; - struct sockaddr_un unix_addr; size_t query_shm_size = sizeof(*cq); if (query) @@ -189,16 +188,10 @@ int send_callback_request(callback_function *f, struct osl_object *query, *(uint32_t *) buf = afs_socket_cookie; *(int *) (buf + sizeof(afs_socket_cookie)) = query_shmid; - ret = get_stream_socket(PF_UNIX); + ret = create_remote_socket(conf.afs_socket_arg); if (ret < 0) goto out; fd = ret; - ret = init_unix_addr(&unix_addr, conf.afs_socket_arg); - if (ret < 0) - goto out; - ret = PARA_CONNECT(fd, &unix_addr); - if (ret < 0) - goto out; ret = send_bin_buffer(fd, buf, sizeof(buf)); if (ret < 0) goto out; diff --git a/audioc.c b/audioc.c index 0036da83..aa7184e0 100644 --- a/audioc.c +++ b/audioc.c @@ -70,7 +70,6 @@ int main(int argc, char *argv[]) int ret = -E_AUDIOC_SYNTAX, fd; char *cf, *buf = NULL, *args; size_t bufsize, loaded = 0; - struct sockaddr_un unix_addr; if (audioc_cmdline_parser(argc, argv, &conf)) goto out; @@ -93,24 +92,21 @@ int main(int argc, char *argv[]) para_strdup("stat"); bufsize = conf.bufsize_arg; buf = para_malloc(bufsize); - ret = get_stream_socket(PF_UNIX); - if (ret < 0) - goto out; - fd = ret; - if (conf.socket_given) - ret = init_unix_addr(&unix_addr, conf.socket_arg); - else { - char *hn = para_hostname(), *socket_name = make_message( - "/var/paraslash/audiod_socket.%s", hn); + + if (conf.socket_given) { + ret = create_remote_socket(conf.socket_arg); + } else { + char *hn = para_hostname(), + *socket_name = make_message("/var/paraslash/audiod_socket.%s", hn); + + ret = create_remote_socket(socket_name); free(hn); - ret = init_unix_addr(&unix_addr, socket_name); free(socket_name); } if (ret < 0) goto out; - ret = PARA_CONNECT(fd, &unix_addr); - if (ret < 0) - goto out; + fd = ret; + ret = send_cred_buffer(fd, args); if (ret < 0) goto out; diff --git a/net.c b/net.c index f1c55b91..2add4844 100644 --- a/net.c +++ b/net.c @@ -457,27 +457,6 @@ int recv_buffer(int fd, char *buf, size_t size) return n; } -/** - * A wrapper around socket(2). - * - * \param domain The communication domain that selects the protocol family. - * - * Create an IPv4 socket for sequenced, reliable, two-way, connection-based - * byte streams. - * - * \return The socket fd on success, negative on errors. - * - * \sa socket(2). - */ -int get_stream_socket(int domain) -{ - int fd = socket(domain, SOCK_STREAM, 0); - - if (fd < 0) - return -ERRNO_TO_PARA_ERROR(errno); - return fd; -} - /** * Wrapper around the accept system call. * @@ -512,7 +491,7 @@ int para_accept(int fd, void *addr, socklen_t size) * \return Positive on success, \p -E_NAME_TOO_LONG if \a name is longer * than \p UNIX_PATH_MAX. */ -int init_unix_addr(struct sockaddr_un *u, const char *name) +static int init_unix_addr(struct sockaddr_un *u, const char *name) { if (strlen(name) >= UNIX_PATH_MAX) return -E_NAME_TOO_LONG; @@ -529,7 +508,7 @@ int init_unix_addr(struct sockaddr_un *u, const char *name) * \param unix_addr Pointer to the \p AF_UNIX socket structure. * \param mode The desired mode of the socket. * - * This functions creates a local socket for sequenced, reliable, + * This function creates a local socket for sequenced, reliable, * two-way, connection-based byte streams. * * \return The file descriptor, on success, negative on errors. @@ -564,6 +543,39 @@ err: return ret; } +/** + * Prepare, create, and connect to a Unix domain socket for local communication. + * + * \param name The socket pathname. + * + * This function creates a local socket for sequenced, reliable, two-way, + * connection-based byte streams. + * + * \return The file descriptor, on success, negative on errors. + * + * \sa create_local_socket(), unix(7), connect(2) + */ +int create_remote_socket(const char *name) +{ + struct sockaddr_un unix_addr; + int fd, ret; + + ret = init_unix_addr(&unix_addr, name); + if (ret < 0) + return ret; + fd = socket(PF_UNIX, SOCK_STREAM, 0); + if (fd < 0) + return -ERRNO_TO_PARA_ERROR(errno); + if (connect(fd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) == -1) { + ret = -ERRNO_TO_PARA_ERROR(errno); + goto err; + } + return fd; +err: + close(fd); + return ret; +} + #ifndef HAVE_UCRED ssize_t send_cred_buffer(int sock, char *buf) { diff --git a/net.h b/net.h index 7499ce62..c4412b2f 100644 --- a/net.h +++ b/net.h @@ -46,7 +46,6 @@ extern char *remote_name(int sockfd); typedef void crypt_function(unsigned long len, const unsigned char *indata, unsigned char *outdata, void *private_data); -int get_stream_socket(int domain); int send_buffer(int, const char *); int send_bin_buffer(int, const char *, size_t); __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...); @@ -55,33 +54,10 @@ int recv_bin_buffer(int fd, char *buf, size_t size); int para_accept(int, void *addr, socklen_t size); int create_local_socket(const char *name, struct sockaddr_un *unix_addr, mode_t mode); -int init_unix_addr(struct sockaddr_un *, const char *); +int create_remote_socket(const char *name); int recv_cred_buffer(int, char *, size_t); ssize_t send_cred_buffer(int, char*); int recv_pattern(int fd, const char *pattern, size_t bufsize); void enable_crypt(int fd, crypt_function *recv_f, crypt_function *send_f, void *private_data); void disable_crypt(int fd); - -/** - * A wrapper around connect(2). - * - * \param fd The file descriptor. - * \param addr The address to connect. - * \param len The size of \a addr. - * - * This should not be called directly. Always use the PARA_CONNECT macro. - * - * \return \p -E_CONNECT on errors, 1 on success. - * - * \sa connect(2), PARA_CONNECT. - */ -static inline int _para_connect(int fd, void *addr, socklen_t len) -{ - if (connect(fd, (struct sockaddr *)addr, len) == -1) - return -E_CONNECT; - return 1; -} - -/** A macro for connect() which does not need a \a len parameter. */ -#define PARA_CONNECT(fd, addr) _para_connect(fd, addr, sizeof(*(addr)))