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"
27 struct gengetopt_args_info conf
;
29 enum {E_SYNTAX
, E_READ
, E_WRITE
, E_SOCKET
, E_INIT_SOCK_ADDR
, E_CONNECT
, E_CREDENTIALS
, E_SELECT
, E_OVERRUN
};
32 void para_log(__a_unused
int ll
, __a_unused
const char* fmt
,...) /* no logging */
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
;
40 static char *concat_args(const int argc
, char * const *argv
)
42 int i
; char *buf
= NULL
;
43 for (i
= 0; i
< argc
; i
++) {
44 buf
= para_strcat(buf
, argv
[i
]);
46 buf
= para_strcat(buf
, "\n");
51 static char *configfile_exists(void)
53 static char *config_file
;
58 char *home
= para_homedir();
59 config_file
= make_message("%s/.paraslash/audioc.conf", home
);
62 if (!stat(config_file
, &statbuf
))
67 int main(int argc
, char *argv
[])
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();
75 if (cmdline_parser(argc
, argv
, &conf
))
77 cf
= configfile_exists();
79 if (cmdline_parser_configfile(cf
, &conf
, 0, 0, 0)) {
80 fprintf(stderr
, "parse error in config file\n");
84 args
= conf
.inputs_num
?
85 concat_args(conf
.inputs_num
, conf
.inputs
) :
87 buf
= para_malloc(conf
.bufsize_arg
);
88 if (conf
.socket_given
)
89 socket_name
= para_strdup(conf
.socket_arg
);
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
);
97 tmpsocket_name
= make_message("%s/.paraslash/audioc_sock.%s.%s",
101 fd
= create_pf_socket(tmpsocket_name
, &unix_addr
, S_IRUSR
| S_IWUSR
);
102 unlink(tmpsocket_name
);
105 ret
= -E_INIT_SOCK_ADDR
;
106 if (init_unix_addr(&unix_addr
, socket_name
) < 0)
109 if (connect(fd
, (struct sockaddr
*)&unix_addr
, UNIX_PATH_MAX
) < 0)
111 ret
= -E_CREDENTIALS
;
112 if (send_cred_buffer(fd
, args
) < 0)
115 int max_fileno
= -1, check_write
= 0;
120 if (loaded
&& loaded
> 10000)
121 fprintf(stderr
, "loaded: %d\n", loaded
);
122 if (loaded
< conf
.bufsize_arg
)
123 para_fd_set(fd
, &rfd
, &max_fileno
);
125 para_fd_set(STDOUT_FILENO
, &wfd
, &max_fileno
);
131 ret
= para_select(max_fileno
+ 1, &rfd
, &wfd
, NULL
);
136 if (loaded
< conf
.bufsize_arg
&& FD_ISSET(fd
, &rfd
)) {
137 len
= recv_bin_buffer(fd
, buf
+ loaded
,
138 conf
.bufsize_arg
- loaded
);
140 ret
= len
< 0? -E_READ
: 0;
145 if (check_write
&& FD_ISSET(STDOUT_FILENO
, &wfd
)) {
146 ret
= write(STDOUT_FILENO
, buf
, loaded
);
155 if (!ret
&& loaded
&& buf
)
156 ret
= write(STDOUT_FILENO
, buf
, loaded
);
157 return ret
< 0? -ret
: EXIT_SUCCESS
;