2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 /** \file audioc.c the client program used to connect to para_audiod */
21 #include "audioc.cmdline.h"
30 struct gengetopt_args_info conf
;
37 void para_log(int ll
, const char* fmt
,...)
41 /* ignore log message if loglevel is not high enough */
42 if (ll
< conf
.loglevel_arg
)
45 vfprintf(stderr
, fmt
, argp
);
49 /* audioc does not use encryption */
50 void (*crypt_function_recv
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
) = NULL
;
51 void (*crypt_function_send
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
) = NULL
;
53 static char *concat_args(const int argc
, char * const *argv
)
55 int i
; char *buf
= NULL
;
56 for (i
= 0; i
< argc
; i
++) {
57 buf
= para_strcat(buf
, argv
[i
]);
59 buf
= para_strcat(buf
, "\n");
64 static char *configfile_exists(void)
66 static char *config_file
;
71 char *home
= para_homedir();
72 config_file
= make_message("%s/.paraslash/audioc.conf", home
);
75 if (!stat(config_file
, &statbuf
))
80 int main(int argc
, char *argv
[])
82 struct sockaddr_un unix_addr
;
83 int ret
= -E_AUDIOC_SYNTAX
, fd
, loaded
= 0;
84 char *cf
, *socket_name
, *randname
= para_tmpname(), *tmpsocket_name
= NULL
,
85 *buf
= NULL
, *hn
= para_hostname(), *args
, *home
= para_homedir();
88 if (cmdline_parser(argc
, argv
, &conf
))
90 cf
= configfile_exists();
92 if (cmdline_parser_configfile(cf
, &conf
, 0, 0, 0)) {
93 fprintf(stderr
, "parse error in config file\n");
97 args
= conf
.inputs_num
?
98 concat_args(conf
.inputs_num
, conf
.inputs
) :
100 buf
= para_malloc(conf
.bufsize_arg
);
101 if (conf
.socket_given
)
102 socket_name
= para_strdup(conf
.socket_arg
);
104 socket_name
= make_message(
105 "/var/paraslash/audiod_socket.%s", hn
);
106 if (conf
.tmpdir_given
)
107 tmpsocket_name
= make_message("%s/audioc.sock.%s.%s",
108 conf
.tmpdir_arg
, hn
, randname
);
110 tmpsocket_name
= make_message("%s/.paraslash/audioc_sock.%s.%s",
113 ret
= create_pf_socket(tmpsocket_name
, &unix_addr
, S_IRUSR
| S_IWUSR
);
114 unlink(tmpsocket_name
);
118 ret
= -E_INIT_SOCK_ADDR
;
119 if (init_unix_addr(&unix_addr
, socket_name
) < 0)
121 ret
= - E_AUDIOC_CONNECT
;
122 if (connect(fd
, (struct sockaddr
*)&unix_addr
, UNIX_PATH_MAX
) < 0)
124 fprintf(stderr
, "loaded: %d\n", loaded
);
125 ret
= -E_CREDENTIALS
;
126 if (send_cred_buffer(fd
, args
) < 0)
129 int max_fileno
= -1, check_write
= 0;
134 if (loaded
&& loaded
> 10000)
135 fprintf(stderr
, "loaded: %d\n", loaded
);
136 if (loaded
< conf
.bufsize_arg
)
137 para_fd_set(fd
, &rfd
, &max_fileno
);
139 para_fd_set(STDOUT_FILENO
, &wfd
, &max_fileno
);
142 ret
= -E_AUDIOC_OVERRUN
;
145 ret
= para_select(max_fileno
+ 1, &rfd
, &wfd
, NULL
);
148 if (loaded
< conf
.bufsize_arg
&& FD_ISSET(fd
, &rfd
)) {
149 len
= recv_bin_buffer(fd
, buf
+ loaded
,
150 conf
.bufsize_arg
- loaded
);
152 ret
= len
< 0? -E_AUDIOC_READ
: 0;
157 if (check_write
&& FD_ISSET(STDOUT_FILENO
, &wfd
)) {
158 ret
= write(STDOUT_FILENO
, buf
, loaded
);
160 ret
= -E_AUDIOC_WRITE
;
167 if (!ret
&& loaded
&& buf
)
168 ret
= write(STDOUT_FILENO
, buf
, loaded
);
170 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
171 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;