Merge /home/maan/scm/paraslash_fml/paraslash
[paraslash.git] / audioc.c
1 /*
2  * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file audioc.c the client program used to connect to para_audiod */
8
9 #include "audioc.cmdline.h"
10 #include "para.h"
11 #include "net.h"
12 #include "string.h"
13 #include "fd.h"
14 #include "error.h"
15
16 INIT_AUDIOC_ERRLISTS;
17
18 /** the gengetopt structure containing command line args */
19 struct audioc_args_info conf;
20
21 INIT_STDERR_LOGGING(conf.loglevel_arg);
22
23 static char *concat_args(unsigned argc, char * const *argv)
24 {
25         int i; char *buf = NULL;
26         for (i = 0; i < argc; i++) {
27                 buf = para_strcat(buf, argv[i]);
28                 if (i != argc - 1)
29                         buf = para_strcat(buf, "\n");
30         }
31         return buf;
32 }
33
34 static char *configfile_exists(void)
35 {
36         static char *config_file;
37         struct stat statbuf;
38
39
40         if (!config_file) {
41                 char *home = para_homedir();
42                 config_file = make_message("%s/.paraslash/audioc.conf", home);
43                 free(home);
44         }
45         if (!stat(config_file, &statbuf))
46                 return config_file;
47         return NULL;
48 }
49
50 /**
51  * the client program to connect to para_audiod
52  *
53  * \param argc usual argument count
54  * \param argv usual argument vector
55  *
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.
58  *
59  * Any output received through the local socket is sent to stdout.
60  *
61  * \return EXIT_SUCCESS or EXIT_FAILURE
62  *
63  * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
64  */
65 int main(int argc, char *argv[])
66 {
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;
72
73         if (audioc_cmdline_parser(argc, argv, &conf))
74                 goto out;
75         HANDLE_VERSION_FLAG("audioc", conf);
76         cf = configfile_exists();
77         if (cf) {
78                 if (audioc_cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
79                         fprintf(stderr, "parse error in config file\n");
80                         exit(EXIT_FAILURE);
81                 }
82         }
83         args = conf.inputs_num?
84                 concat_args(conf.inputs_num, conf.inputs) :
85                 para_strdup("stat");
86         bufsize = conf.bufsize_arg;
87         buf = para_malloc(bufsize);
88         if (conf.socket_given)
89                 socket_name = para_strdup(conf.socket_arg);
90         else
91                 socket_name = make_message(
92                         "/var/paraslash/audiod_socket.%s", hn);
93         if (conf.tmpdir_given)
94                 tmpsocket_name = make_message("%s/audioc.sock.%s.%s",
95                         conf.tmpdir_arg, hn, randname);
96         else
97                 tmpsocket_name = make_message("%s/.paraslash/audioc_sock.%s.%s",
98                         home, hn, randname);
99
100         ret = create_pf_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
101         unlink(tmpsocket_name);
102         if (ret < 0)
103                 goto out;
104         fd = ret;
105         ret = -E_INIT_SOCK_ADDR;
106         if (init_unix_addr(&unix_addr, socket_name) < 0)
107                 goto out;
108         ret = -E_AUDIOC_CONNECT;
109         if (connect(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX) < 0)
110                 goto out;
111         ret = send_cred_buffer(fd, args);
112         if (ret < 0)
113                 goto out;
114         for (;;) {
115                 int max_fileno = -1, check_write = 0;
116                 ssize_t len;
117                 fd_set rfd, wfd;
118                 FD_ZERO(&rfd);
119                 FD_ZERO(&wfd);
120                 if (loaded < bufsize)
121                         para_fd_set(fd, &rfd, &max_fileno);
122                 if (loaded > 0) {
123                         para_fd_set(STDOUT_FILENO, &wfd, &max_fileno);
124                         check_write = 1;
125                 }
126                 ret = -E_AUDIOC_OVERRUN;
127                 if (max_fileno < 0)
128                         goto out;
129                 ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
130                 if (ret < 0)
131                         goto out;
132                 if (loaded < bufsize && FD_ISSET(fd, &rfd)) {
133                         len = recv_bin_buffer(fd, buf + loaded,
134                                 bufsize - loaded);
135                         if (len <= 0) {
136                                 ret = len < 0? -E_AUDIOC_READ : 0;
137                                 goto out;
138                         }
139                         loaded += len;
140                 }
141                 if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
142                         ret = write(STDOUT_FILENO, buf, loaded);
143                         if (ret < 0) {
144                                 ret = -E_AUDIOC_WRITE;
145                                 goto out;
146                         }
147                         loaded -= ret;
148                 }
149         }
150 out:
151         if (!ret && loaded && buf)
152                 ret = write(STDOUT_FILENO, buf, loaded);
153         if (ret < 0)
154                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
155         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
156 }