New codename for 0.4.
[paraslash.git] / audioc.c
1 /*
2 * Copyright (C) 2005-2009 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 <sys/types.h>
10 #include <dirent.h>
11
12 #include "audioc.cmdline.h"
13 #include "para.h"
14 #include "error.h"
15 #include "net.h"
16 #include "string.h"
17 #include "fd.h"
18
19 INIT_AUDIOC_ERRLISTS;
20
21 /** the gengetopt structure containing command line args */
22 struct audioc_args_info conf;
23
24 static int loglevel;
25 INIT_STDERR_LOGGING(loglevel);
26
27 static char *concat_args(unsigned argc, char * const *argv)
28 {
29 int i; char *buf = NULL;
30 for (i = 0; i < argc; i++) {
31 buf = para_strcat(buf, argv[i]);
32 if (i != argc - 1)
33 buf = para_strcat(buf, "\n");
34 }
35 return buf;
36 }
37
38 static char *configfile_exists(void)
39 {
40 static char *config_file;
41 struct stat statbuf;
42
43
44 if (!config_file) {
45 char *home = para_homedir();
46 config_file = make_message("%s/.paraslash/audioc.conf", home);
47 free(home);
48 }
49 if (!stat(config_file, &statbuf))
50 return config_file;
51 return NULL;
52 }
53
54 /**
55 * the client program to connect to para_audiod
56 *
57 * \param argc usual argument count
58 * \param argv usual argument vector
59 *
60 * It creates a temporary local socket in order to communicate with para_audiod.
61 * Authentication consists in sending a ucred buffer that contains the user id.
62 *
63 * Any output received through the local socket is sent to stdout.
64 *
65 * \return EXIT_SUCCESS or EXIT_FAILURE
66 *
67 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
68 */
69 int main(int argc, char *argv[])
70 {
71 int ret = -E_AUDIOC_SYNTAX, fd;
72 char *cf, *buf = NULL, *args;
73 size_t bufsize, loaded = 0;
74
75 if (audioc_cmdline_parser(argc, argv, &conf))
76 goto out;
77 HANDLE_VERSION_FLAG("audioc", conf);
78 cf = configfile_exists();
79 if (cf) {
80 struct audioc_cmdline_parser_params params = {
81 .override = 0,
82 .initialize = 0,
83 .check_required = 0,
84 .check_ambiguity = 0
85 };
86 if (audioc_cmdline_parser_config_file(cf, &conf, &params)) {
87 fprintf(stderr, "parse error in config file\n");
88 exit(EXIT_FAILURE);
89 }
90 }
91 loglevel = get_loglevel_by_name(conf.loglevel_arg);
92 args = conf.inputs_num?
93 concat_args(conf.inputs_num, conf.inputs) :
94 para_strdup("stat");
95 bufsize = conf.bufsize_arg;
96 buf = para_malloc(bufsize);
97
98 if (conf.socket_given) {
99 ret = create_remote_socket(conf.socket_arg);
100 } else {
101 char *hn = para_hostname(),
102 *socket_name = make_message("/var/paraslash/audiod_socket.%s", hn);
103
104 ret = create_remote_socket(socket_name);
105 free(hn);
106 free(socket_name);
107 }
108 if (ret < 0)
109 goto out;
110 fd = ret;
111 ret = mark_fd_nonblocking(fd);
112 if (ret < 0)
113 goto out;
114 ret = mark_fd_nonblocking(STDOUT_FILENO);
115 if (ret < 0)
116 goto out;
117 ret = send_cred_buffer(fd, args);
118 if (ret < 0)
119 goto out;
120 for (;;) {
121 int max_fileno = -1, check_write = 0;
122 ssize_t len;
123 fd_set rfd, wfd;
124 FD_ZERO(&rfd);
125 FD_ZERO(&wfd);
126 if (loaded < bufsize)
127 para_fd_set(fd, &rfd, &max_fileno);
128 if (loaded > 0) {
129 para_fd_set(STDOUT_FILENO, &wfd, &max_fileno);
130 check_write = 1;
131 }
132 ret = -E_AUDIOC_OVERRUN;
133 if (max_fileno < 0)
134 goto out;
135 ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
136 if (ret < 0)
137 goto out;
138 if (loaded < bufsize && FD_ISSET(fd, &rfd)) {
139 len = recv_bin_buffer(fd, buf + loaded,
140 bufsize - loaded);
141 if (len <= 0) {
142 ret = len < 0? -E_AUDIOC_READ : 0;
143 goto out;
144 }
145 loaded += len;
146 }
147 if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
148 ret = write(STDOUT_FILENO, buf, loaded);
149 if (ret < 0) {
150 ret = -E_AUDIOC_WRITE;
151 goto out;
152 }
153 loaded -= ret;
154 }
155 }
156 out:
157 if (!ret && loaded && buf)
158 ret = write(STDOUT_FILENO, buf, loaded);
159 if (ret < 0)
160 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
161 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
162 }