list.h, ortp.h: Add missing documentation
[paraslash.git] / audioc.c
1 /*
2  * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file audioc.c the client program used to connect to para_audiod */
20
21 #include "audioc.cmdline.h"
22 #include "para.h"
23 #include "net.h"
24 #include "string.h"
25 #include "fd.h"
26 #include "error.h"
27
28 INIT_AUDIOC_ERRLISTS;
29
30 /** the gengetopt structure containing command line args */
31 struct audioc_args_info conf;
32
33 INIT_STDERR_LOGGING(conf.loglevel_arg);
34
35 static char *concat_args(const int argc, char * const *argv)
36 {
37         int i; char *buf = NULL;
38         for (i = 0; i < argc; i++) {
39                 buf = para_strcat(buf, argv[i]);
40                 if (i != argc - 1)
41                         buf = para_strcat(buf, "\n");
42         }
43         return buf;
44 }
45
46 static char *configfile_exists(void)
47 {
48         static char *config_file;
49         struct stat statbuf;
50
51
52         if (!config_file) {
53                 char *home = para_homedir();
54                 config_file = make_message("%s/.paraslash/audioc.conf", home);
55                 free(home);
56         }
57         if (!stat(config_file, &statbuf))
58                 return config_file;
59         return NULL;
60 }
61
62 /**
63  * the client program to connect to para_audiod
64  *
65  * \param argc usual argument count
66  * \param argv usual argument vector
67  *
68  * It creates a temporary local socket in order to communicate with para_audiod.
69  * Authentication consists in sending a ucred buffer that contains the user id.
70  *
71  * Any output received through the local socket is sent to stdout.
72  *
73  * \return EXIT_SUCCESS or EXIT_FAILURE
74  *
75  * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
76  */
77 int main(int argc, char *argv[])
78 {
79         struct sockaddr_un unix_addr;
80         int ret = -E_AUDIOC_SYNTAX, fd, loaded = 0;
81         char *cf, *socket_name, *randname = para_tmpname(), *tmpsocket_name = NULL,
82                 *buf = NULL, *hn = para_hostname(), *args, *home = para_homedir();
83
84
85         if (audioc_cmdline_parser(argc, argv, &conf))
86                 goto out;
87         HANDLE_VERSION_FLAG("audioc", conf);
88         cf = configfile_exists();
89         if (cf) {
90                 if (audioc_cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
91                         fprintf(stderr, "parse error in config file\n");
92                         exit(EXIT_FAILURE);
93                 }
94         }
95         args = conf.inputs_num?
96                 concat_args(conf.inputs_num, conf.inputs) :
97                 para_strdup("stat");
98         buf = para_malloc(conf.bufsize_arg);
99         if (conf.socket_given)
100                 socket_name = para_strdup(conf.socket_arg);
101         else
102                 socket_name = make_message(
103                         "/var/paraslash/audiod_socket.%s", hn);
104         if (conf.tmpdir_given)
105                 tmpsocket_name = make_message("%s/audioc.sock.%s.%s",
106                         conf.tmpdir_arg, hn, randname);
107         else
108                 tmpsocket_name = make_message("%s/.paraslash/audioc_sock.%s.%s",
109                         home, hn, randname);
110
111         ret = create_pf_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
112         unlink(tmpsocket_name);
113         if (ret < 0)
114                 goto out;
115         fd = ret;
116         ret = -E_INIT_SOCK_ADDR;
117         if (init_unix_addr(&unix_addr, socket_name) < 0)
118                 goto out;
119         ret = - E_AUDIOC_CONNECT;
120         if (connect(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX) < 0)
121                 goto out;
122         ret = send_cred_buffer(fd, args);
123         if (ret < 0)
124                 goto out;
125         for (;;) {
126                 int max_fileno = -1, check_write = 0;
127                 ssize_t len;
128                 fd_set rfd, wfd;
129                 FD_ZERO(&rfd);
130                 FD_ZERO(&wfd);
131                 if (loaded < conf.bufsize_arg)
132                         para_fd_set(fd, &rfd, &max_fileno);
133                 if (loaded > 0) {
134                         para_fd_set(STDOUT_FILENO, &wfd, &max_fileno);
135                         check_write = 1;
136                 }
137                 ret = -E_AUDIOC_OVERRUN;
138                 if (max_fileno < 0)
139                         goto out;
140                 ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
141                 if (ret < 0)
142                         goto out;
143                 if (loaded < conf.bufsize_arg && FD_ISSET(fd, &rfd)) {
144                         len = recv_bin_buffer(fd, buf + loaded,
145                                 conf.bufsize_arg - loaded);
146                         if (len <= 0) {
147                                 ret = len < 0? -E_AUDIOC_READ : 0;
148                                 goto out;
149                         }
150                         loaded += len;
151                 }
152                 if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
153                         ret = write(STDOUT_FILENO, buf, loaded);
154                         if (ret < 0) {
155                                 ret = -E_AUDIOC_WRITE;
156                                 goto out;
157                         }
158                         loaded -= ret;
159                 }
160         }
161 out:
162         if (!ret && loaded && buf)
163                 ret = write(STDOUT_FILENO, buf, loaded);
164         if (ret < 0)
165                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
166         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
167 }