23f67fbb0291078266cec811fae51c883958887e
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"
26 struct gengetopt_args_info conf
;
28 enum {E_SYNTAX
, E_READ
, E_WRITE
, E_SOCKET
, E_INIT_SOCK_ADDR
, E_CONNECT
, E_CREDENTIALS
, E_SELECT
, E_OVERRUN
};
31 void para_log(__unused
int ll
, __unused
char* fmt
,...) /* no logging */
35 /* audioc does not use encryption */
36 void (*crypt_function_recv
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
) = NULL
;
37 void (*crypt_function_send
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
) = NULL
;
39 static char *concat_args(const int argc
, char * const *argv
)
41 int i
; char *buf
= NULL
;
42 for (i
= 0; i
< argc
; i
++) {
43 buf
= para_strcat(buf
, argv
[i
]);
45 buf
= para_strcat(buf
, "\n");
50 static char *configfile_exists(void)
52 static char *config_file
;
57 char *home
= para_homedir();
58 config_file
= make_message("%s/.paraslash/audioc.conf", home
);
61 if (!stat(config_file
, &statbuf
))
66 int main(int argc
, char *argv
[])
68 struct sockaddr_un unix_addr
;
69 int ret
= -E_SYNTAX
, fd
, loaded
= 0;
70 char *cf
, *socket_name
, *randname
= para_tmpname(), *tmpsocket_name
= NULL
,
71 *buf
= NULL
, *hn
= para_hostname(), *args
, *home
= para_homedir();
74 if (cmdline_parser(argc
, argv
, &conf
))
76 cf
= configfile_exists();
78 if (cmdline_parser_configfile(cf
, &conf
, 0, 0, 0)) {
79 fprintf(stderr
, "parse error in config file\n");
83 args
= conf
.inputs_num
?
84 concat_args(conf
.inputs_num
, conf
.inputs
) :
86 buf
= para_malloc(conf
.bufsize_arg
);
87 if (conf
.socket_given
)
88 socket_name
= para_strdup(conf
.socket_arg
);
90 socket_name
= make_message(
91 "/var/paraslash/audiod_socket.%s", hn
);
92 if (conf
.tmpdir_given
)
93 tmpsocket_name
= make_message("%s/audioc.sock.%s.%s",
94 conf
.tmpdir_arg
, hn
, randname
);
96 tmpsocket_name
= make_message("%s/.paraslash/audioc_sock.%s.%s",
100 fd
= create_pf_socket(tmpsocket_name
, &unix_addr
, S_IRUSR
| S_IWUSR
);
101 unlink(tmpsocket_name
);
104 ret
= -E_INIT_SOCK_ADDR
;
105 if (init_unix_addr(&unix_addr
, socket_name
) < 0)
108 if (connect(fd
, (struct sockaddr
*)&unix_addr
, UNIX_PATH_MAX
) < 0)
110 ret
= -E_CREDENTIALS
;
111 if (send_cred_buffer(fd
, args
) < 0)
114 int max_fileno
= -1, check_write
= 0;
119 if (loaded
&& loaded
> 10000)
120 fprintf(stderr
, "loaded: %d\n", loaded
);
121 if (loaded
< conf
.bufsize_arg
) {
123 max_fileno
= MAX(max_fileno
, fd
);
126 FD_SET(STDOUT_FILENO
, &wfd
);
127 max_fileno
= MAX(max_fileno
, STDOUT_FILENO
);
133 ret
= select(max_fileno
+ 1, &rfd
, &wfd
, NULL
, NULL
);
138 if (loaded
< conf
.bufsize_arg
&& FD_ISSET(fd
, &rfd
)) {
139 len
= recv_bin_buffer(fd
, buf
+ loaded
,
140 conf
.bufsize_arg
- loaded
);
142 ret
= len
< 0? -E_READ
: 0;
147 if (check_write
&& FD_ISSET(STDOUT_FILENO
, &wfd
)) {
148 ret
= write(STDOUT_FILENO
, buf
, loaded
);
157 if (!ret
&& loaded
&& buf
)
158 ret
= write(STDOUT_FILENO
, buf
, loaded
);
159 return ret
< 0? -ret
: EXIT_SUCCESS
;