introduce struct writer and struct writer_node
[paraslash.git] / audioc.c
1 /*
2 * Copyright (C) 2005-2006 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
27 struct gengetopt_args_info conf;
28 char *tmpfifo;
29 enum {E_SYNTAX, E_READ, E_WRITE, E_SOCKET, E_INIT_SOCK_ADDR, E_CONNECT, E_CREDENTIALS, E_SELECT, E_OVERRUN};
30
31
32 void para_log(__a_unused int ll, __a_unused const char* fmt,...) /* no logging */
33 {
34 }
35
36 /* audioc does not use encryption */
37 void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata) = NULL;
38 void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *outdata) = NULL;
39
40 static char *concat_args(const int argc, char * const *argv)
41 {
42 int i; char *buf = NULL;
43 for (i = 0; i < argc; i++) {
44 buf = para_strcat(buf, argv[i]);
45 if (i != argc - 1)
46 buf = para_strcat(buf, "\n");
47 }
48 return buf;
49 }
50
51 static char *configfile_exists(void)
52 {
53 static char *config_file;
54 struct stat statbuf;
55
56
57 if (!config_file) {
58 char *home = para_homedir();
59 config_file = make_message("%s/.paraslash/audioc.conf", home);
60 free(home);
61 }
62 if (!stat(config_file, &statbuf))
63 return config_file;
64 return NULL;
65 }
66
67 int main(int argc, char *argv[])
68 {
69 struct sockaddr_un unix_addr;
70 int ret = -E_SYNTAX, fd, loaded = 0;
71 char *cf, *socket_name, *randname = para_tmpname(), *tmpsocket_name = NULL,
72 *buf = NULL, *hn = para_hostname(), *args, *home = para_homedir();
73
74
75 if (cmdline_parser(argc, argv, &conf))
76 goto out;
77 cf = configfile_exists();
78 if (cf) {
79 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
80 fprintf(stderr, "parse error in config file\n");
81 exit(EXIT_FAILURE);
82 }
83 }
84 args = conf.inputs_num?
85 concat_args(conf.inputs_num, conf.inputs) :
86 para_strdup("stat");
87 buf = para_malloc(conf.bufsize_arg);
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 = -E_SOCKET;
101 fd = create_pf_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
102 unlink(tmpsocket_name);
103 if (fd < 0)
104 goto out;
105 ret = -E_INIT_SOCK_ADDR;
106 if (init_unix_addr(&unix_addr, socket_name) < 0)
107 goto out;
108 ret = - E_CONNECT;
109 if (connect(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX) < 0)
110 goto out;
111 ret = -E_CREDENTIALS;
112 if (send_cred_buffer(fd, args) < 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 && loaded > 10000)
121 fprintf(stderr, "loaded: %d\n", loaded);
122 if (loaded < conf.bufsize_arg) {
123 FD_SET(fd, &rfd);
124 max_fileno = PARA_MAX(max_fileno, fd);
125 }
126 if (loaded > 0) {
127 FD_SET(STDOUT_FILENO, &wfd);
128 max_fileno = PARA_MAX(max_fileno, STDOUT_FILENO);
129 check_write = 1;
130 }
131 ret = -E_OVERRUN;
132 if (max_fileno < 0)
133 goto out;
134 ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
135 if (ret < 0) {
136 ret = -E_SELECT;
137 goto out;
138 }
139 if (loaded < conf.bufsize_arg && FD_ISSET(fd, &rfd)) {
140 len = recv_bin_buffer(fd, buf + loaded,
141 conf.bufsize_arg - loaded);
142 if (len <= 0) {
143 ret = len < 0? -E_READ : 0;
144 goto out;
145 }
146 loaded += len;
147 }
148 if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
149 ret = write(STDOUT_FILENO, buf, loaded);
150 if (ret < 0) {
151 ret = -E_WRITE;
152 goto out;
153 }
154 loaded -= ret;
155 }
156 }
157 out:
158 if (!ret && loaded && buf)
159 ret = write(STDOUT_FILENO, buf, loaded);
160 return ret < 0? -ret : EXIT_SUCCESS;
161 }