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)
*(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;
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;
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;
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.
*
* \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;
* \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.
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)
{
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 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)))