]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
06_Unix-Domain-Sockets.diff
authorGerrit Renker <gerrit@erg.abdn.ac.uk>
Fri, 30 Nov 2007 09:23:51 +0000 (10:23 +0100)
committerAndre Noll <maan@systemlinux.org>
Fri, 30 Nov 2007 09:23:51 +0000 (10:23 +0100)
[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 <gerrit@erg.abdn.ac.uk>
afs.c
audioc.c
net.c
net.h

diff --git a/afs.c b/afs.c
index 09d4212610e26f5be064d97097bdff4ed8879055..ac7884c7e4e4c959a3ce3b1c17d050705a70b394 100644 (file)
--- 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)];
        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)
        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;
 
        *(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;
        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;
        ret = send_bin_buffer(fd, buf, sizeof(buf));
        if (ret < 0)
                goto out;
index 0036da83144bc180d1f3df104f93e4276caaf1b7..aa7184e09070d4742650cc7b72904ab066ebc482 100644 (file)
--- 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;
        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;
 
        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);
                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);
                free(hn);
-               ret = init_unix_addr(&unix_addr, socket_name);
                free(socket_name);
        }
        if (ret < 0)
                goto out;
                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;
        ret = send_cred_buffer(fd, args);
        if (ret < 0)
                goto out;
diff --git a/net.c b/net.c
index f1c55b9180f5a90011cf7c637bd68b42951a886e..2add48449f26fd46345a5644df00700df3ab5826 100644 (file)
--- a/net.c
+++ b/net.c
@@ -457,27 +457,6 @@ int recv_buffer(int fd, char *buf, size_t size)
        return n;
 }
 
        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.
  *
 /**
  * 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.
  */
  * \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;
 {
        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.
  *
  * \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.
  * two-way, connection-based byte streams.
  *
  * \return The file descriptor, on success, negative on errors.
@@ -564,6 +543,39 @@ err:
        return ret;
 }
 
        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)
 {
 #ifndef HAVE_UCRED
 ssize_t send_cred_buffer(int sock, char *buf)
 {
diff --git a/net.h b/net.h
index 7499ce6267b5f9174e9556a23f82c7707e14dc40..c4412b2fe1040f26f96b023c168b2aa96f50e3c3 100644 (file)
--- 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);
 
 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, ...);
 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 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);
 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)))