2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file audioc.c the client program used to connect to para_audiod */
9 #include "audioc.cmdline.h"
18 /** the gengetopt structure containing command line args */
19 struct audioc_args_info conf;
21 INIT_STDERR_LOGGING(conf.loglevel_arg);
23 static char *concat_args(unsigned argc, char * const *argv)
25 int i; char *buf = NULL;
26 for (i = 0; i < argc; i++) {
27 buf = para_strcat(buf, argv[i]);
29 buf = para_strcat(buf, "\n");
34 static char *configfile_exists(void)
36 static char *config_file;
41 char *home = para_homedir();
42 config_file = make_message("%s/.paraslash/audioc.conf", home);
45 if (!stat(config_file, &statbuf))
51 * the client program to connect to para_audiod
53 * \param argc usual argument count
54 * \param argv usual argument vector
56 * It creates a temporary local socket in order to communicate with para_audiod.
57 * Authentication consists in sending a ucred buffer that contains the user id.
59 * Any output received through the local socket is sent to stdout.
61 * \return EXIT_SUCCESS or EXIT_FAILURE
63 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
65 int main(int argc, char *argv[])
67 struct sockaddr_un unix_addr;
68 int ret = -E_AUDIOC_SYNTAX, fd;
69 char *cf, *socket_name, *randname = para_tmpname(), *tmpsocket_name = NULL,
70 *buf = NULL, *hn = para_hostname(), *args, *home = para_homedir();
71 size_t bufsize, loaded = 0;
73 if (audioc_cmdline_parser(argc, argv, &conf))
75 HANDLE_VERSION_FLAG("audioc", conf);
76 cf = configfile_exists();
78 struct audioc_cmdline_parser_params params = {
84 if (audioc_cmdline_parser_config_file(cf, &conf, ¶ms)) {
85 fprintf(stderr, "parse error in config file\n");
89 args = conf.inputs_num?
90 concat_args(conf.inputs_num, conf.inputs) :
92 bufsize = conf.bufsize_arg;
93 buf = para_malloc(bufsize);
94 if (conf.socket_given)
95 socket_name = para_strdup(conf.socket_arg);
97 socket_name = make_message(
98 "/var/paraslash/audiod_socket.%s", hn);
99 if (conf.tmpdir_given)
100 tmpsocket_name = make_message("%s/audioc.sock.%s.%s",
101 conf.tmpdir_arg, hn, randname);
103 tmpsocket_name = make_message("%s/.paraslash/audioc_sock.%s.%s",
106 ret = create_local_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
107 unlink(tmpsocket_name);
111 ret = -E_INIT_SOCK_ADDR;
112 if (init_unix_addr(&unix_addr, socket_name) < 0)
114 ret = -E_AUDIOC_CONNECT;
115 if (connect(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX) < 0)
117 ret = send_cred_buffer(fd, args);
121 int max_fileno = -1, check_write = 0;
126 if (loaded < bufsize)
127 para_fd_set(fd, &rfd, &max_fileno);
129 para_fd_set(STDOUT_FILENO, &wfd, &max_fileno);
132 ret = -E_AUDIOC_OVERRUN;
135 ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
138 if (loaded < bufsize && FD_ISSET(fd, &rfd)) {
139 len = recv_bin_buffer(fd, buf + loaded,
142 ret = len < 0? -E_AUDIOC_READ : 0;
147 if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
148 ret = write(STDOUT_FILENO, buf, loaded);
150 ret = -E_AUDIOC_WRITE;
157 if (!ret && loaded && buf)
158 ret = write(STDOUT_FILENO, buf, loaded);
160 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
161 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;