• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of PulseAudio.
3 
4   Copyright 2004-2006 Lennart Poettering
5 
6   PulseAudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2.1 of the License,
9   or (at your option) any later version.
10 
11   PulseAudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15 
16   You should have received a copy of the GNU Lesser General Public License
17   along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <assert.h>
25 #include <signal.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <sys/un.h>
31 #include <getopt.h>
32 #include <locale.h>
33 
34 #include <pulse/util.h>
35 #include <pulse/xmalloc.h>
36 
37 #include <pulsecore/i18n.h>
38 #include <pulsecore/poll.h>
39 #include <pulsecore/macro.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/pid.h>
43 
help(const char * argv0)44 static void help(const char *argv0) {
45     printf("%s %s\n",    argv0, "exit");
46     printf("%s %s\n",    argv0, "help");
47     printf("%s %s\n",    argv0, "list-(modules|sinks|sources|clients|cards|samples)");
48     printf("%s %s\n",    argv0, "list-(sink-inputs|source-outputs)");
49     printf("%s %s\n",    argv0, "stat");
50     printf("%s %s\n",    argv0, "info");
51     printf("%s %s %s\n", argv0, "load-module", _("NAME [ARGS ...]"));
52     printf("%s %s %s\n", argv0, "unload-module", _("NAME|#N"));
53     printf("%s %s %s\n", argv0, "describe-module", _("NAME"));
54     printf("%s %s %s\n", argv0, "set-(sink|source)-volume", _("NAME|#N VOLUME"));
55     printf("%s %s %s\n", argv0, "set-(sink-input|source-output)-volume", _("#N VOLUME"));
56     printf("%s %s %s\n", argv0, "set-(sink|source)-mute", _("NAME|#N 1|0"));
57     printf("%s %s %s\n", argv0, "set-(sink-input|source-output)-mute", _("#N 1|0"));
58     printf("%s %s %s\n", argv0, "update-(sink|source)-proplist", _("NAME|#N KEY=VALUE"));
59     printf("%s %s %s\n", argv0, "update-(sink-input|source-output)-proplist", _("#N KEY=VALUE"));
60     printf("%s %s %s\n", argv0, "set-default-(sink|source)", _("NAME|#N"));
61     printf("%s %s %s\n", argv0, "kill-(client|sink-input|source-output)", _("#N"));
62     printf("%s %s %s\n", argv0, "play-sample", _("NAME SINK|#N"));
63     printf("%s %s %s\n", argv0, "remove-sample", _("NAME"));
64     printf("%s %s %s\n", argv0, "load-sample", _("NAME FILENAME"));
65     printf("%s %s %s\n", argv0, "load-sample-lazy", _("NAME FILENAME"));
66     printf("%s %s %s\n", argv0, "load-sample-dir-lazy", _("PATHNAME"));
67     printf("%s %s %s\n", argv0, "play-file", _("FILENAME SINK|#N"));
68     printf("%s %s\n",    argv0, "dump");
69     printf("%s %s %s\n", argv0, "move-(sink-input|source-output)", _("#N SINK|SOURCE"));
70     printf("%s %s %s\n", argv0, "suspend-(sink|source)", _("NAME|#N 1|0"));
71     printf("%s %s %s\n", argv0, "suspend", _("1|0"));
72     printf("%s %s %s\n", argv0, "set-card-profile", _("CARD PROFILE"));
73     printf("%s %s %s\n", argv0, "set-(sink|source)-port", _("NAME|#N PORT"));
74     printf("%s %s %s\n", argv0, "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET"));
75     printf("%s %s %s\n", argv0, "set-log-target", _("TARGET"));
76     printf("%s %s %s\n", argv0, "set-log-level", _("NUMERIC-LEVEL"));
77     printf("%s %s %s\n", argv0, "set-log-meta", _("1|0"));
78     printf("%s %s %s\n", argv0, "set-log-time", _("1|0"));
79     printf("%s %s %s\n", argv0, "set-log-backtrace", _("FRAMES"));
80 
81     printf(_("\n"
82          "  -h, --help                            Show this help\n"
83          "      --version                         Show version\n"
84          "When no command is given pacmd starts in the interactive mode.\n" ));
85 }
86 
87 enum {
88     ARG_VERSION = 256
89 };
90 
main(int argc,char * argv[])91 int main(int argc, char*argv[]) {
92     pid_t pid;
93     int fd = -1;
94     int ret = 1, i;
95     struct sockaddr_un sa;
96     char *ibuf = NULL;
97     char *obuf = NULL;
98     size_t buf_size, ibuf_size, ibuf_index, ibuf_length, obuf_size, obuf_index, obuf_length;
99     char *cli;
100     bool ibuf_eof, obuf_eof, ibuf_closed, obuf_closed;
101     struct pollfd pollfd[3];
102     struct pollfd *watch_socket, *watch_stdin, *watch_stdout;
103     int stdin_type = 0, stdout_type = 0, fd_type = 0;
104 
105     char *bn = NULL;
106     int c;
107 
108     static const struct option long_options[] = {
109         {"version",     0, NULL, ARG_VERSION},
110         {"help",        0, NULL, 'h'},
111         {NULL,          0, NULL, 0}
112     };
113 
114     setlocale(LC_ALL, "");
115 #ifdef ENABLE_NLS
116     bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
117 #endif
118 
119     bn = pa_path_get_filename(argv[0]);
120 
121     while ((c = getopt_long(argc, argv, "h", long_options, NULL)) != -1) {
122         switch (c) {
123             case 'h' :
124                 help(bn);
125                 ret = 0;
126                 goto quit;
127             case ARG_VERSION:
128                 printf(_("pacmd %s\n"
129                          "Compiled with libpulse %s\n"
130                          "Linked with libpulse %s\n"),
131                        PACKAGE_VERSION,
132                        pa_get_headers_version(),
133                        pa_get_library_version());
134                 ret = 0;
135                 goto quit;
136             default:
137                 goto quit;
138         }
139     }
140 
141     if (pa_pid_file_check_running(&pid, "pulseaudio") < 0) {
142         pa_log(_("No PulseAudio daemon running, or not running as session daemon."));
143         goto quit;
144     }
145 
146     if ((fd = pa_socket_cloexec(PF_UNIX, SOCK_STREAM, 0)) < 0) {
147         pa_log(_("socket(PF_UNIX, SOCK_STREAM, 0): %s"), strerror(errno));
148         goto quit;
149     }
150 
151     pa_zero(sa);
152     sa.sun_family = AF_UNIX;
153 
154     if (!(cli = pa_runtime_path("cli")))
155         goto quit;
156 
157     pa_strlcpy(sa.sun_path, cli, sizeof(sa.sun_path));
158     pa_xfree(cli);
159 
160     for (i = 0; i < 5; i++) {
161         int r;
162 
163         if ((r = connect(fd, (struct sockaddr*) &sa, sizeof(sa))) < 0 && (errno != ECONNREFUSED && errno != ENOENT)) {
164             pa_log(_("connect(): %s"), strerror(errno));
165             goto quit;
166         }
167 
168         if (r >= 0)
169             break;
170 
171         if (pa_pid_file_kill(SIGUSR2, NULL, "pulseaudio") < 0) {
172             pa_log(_("Failed to kill PulseAudio daemon."));
173             goto quit;
174         }
175 
176         pa_msleep(300);
177     }
178 
179     if (i >= 5) {
180         pa_log(_("Daemon not responding."));
181         goto quit;
182     }
183 
184     buf_size = pa_pipe_buf(fd);
185     ibuf_size = PA_MIN(buf_size, pa_pipe_buf(STDIN_FILENO));
186     ibuf = pa_xmalloc(ibuf_size);
187     obuf_size = PA_MIN(buf_size, pa_pipe_buf(STDOUT_FILENO));
188     obuf = pa_xmalloc(obuf_size);
189     ibuf_index = ibuf_length = obuf_index = obuf_length = 0;
190     ibuf_eof = obuf_eof = ibuf_closed = obuf_closed = false;
191 
192     if (argc > 1) {
193         for (i = 1; i < argc; i++) {
194             size_t k;
195 
196             k = PA_MIN(ibuf_size - ibuf_length, strlen(argv[i]));
197             memcpy(ibuf + ibuf_length, argv[i], k);
198             ibuf_length += k;
199 
200             if (ibuf_length < ibuf_size) {
201                 ibuf[ibuf_length] = i < argc-1 ? ' ' : '\n';
202                 ibuf_length++;
203             }
204         }
205 
206         ibuf_eof = true;
207     }
208 
209     if (!ibuf_eof && isatty(STDIN_FILENO)) {
210         /* send hello to enable interactive mode (welcome message, prompt) */
211         if (pa_write(fd, "hello\n", 6, &fd_type) < 0) {
212             pa_log(_("write(): %s"), strerror(errno));
213             goto quit;
214         }
215     }
216 
217     for (;;) {
218         struct pollfd *p;
219 
220         if (ibuf_eof &&
221             obuf_eof &&
222             ibuf_length <= 0 &&
223             obuf_length <= 0)
224             break;
225 
226         if (ibuf_length <= 0 && ibuf_eof && !ibuf_closed) {
227             shutdown(fd, SHUT_WR);
228             ibuf_closed = true;
229         }
230 
231         if (obuf_length <= 0 && obuf_eof && !obuf_closed) {
232             shutdown(fd, SHUT_RD);
233             obuf_closed = true;
234         }
235 
236         pa_zero(pollfd);
237 
238         p = pollfd;
239 
240         if (ibuf_length > 0 || (!obuf_eof && obuf_length <= 0)) {
241             watch_socket = p++;
242             watch_socket->fd = fd;
243             watch_socket->events =
244                 (ibuf_length > 0 ? POLLOUT : 0) |
245                 (!obuf_eof && obuf_length <= 0 ? POLLIN : 0);
246         } else
247             watch_socket = NULL;
248 
249         if (!ibuf_eof && ibuf_length <= 0) {
250             watch_stdin = p++;
251             watch_stdin->fd = STDIN_FILENO;
252             watch_stdin->events = POLLIN;
253         } else
254             watch_stdin = NULL;
255 
256         if (obuf_length > 0) {
257             watch_stdout = p++;
258             watch_stdout->fd = STDOUT_FILENO;
259             watch_stdout->events = POLLOUT;
260         } else
261             watch_stdout = NULL;
262 
263         if (pa_poll(pollfd, p-pollfd, -1) < 0) {
264 
265             if (errno == EINTR)
266                 continue;
267 
268             pa_log(_("poll(): %s"), strerror(errno));
269             goto quit;
270         }
271 
272         if (watch_stdin) {
273             if (watch_stdin->revents & POLLIN) {
274                 ssize_t r;
275                 pa_assert(ibuf_length <= 0);
276 
277                 if ((r = pa_read(STDIN_FILENO, ibuf, ibuf_size, &stdin_type)) <= 0) {
278                     if (r < 0) {
279                         pa_log(_("read(): %s"), strerror(errno));
280                         goto quit;
281                     }
282 
283                     ibuf_eof = true;
284                 } else {
285                     ibuf_length = (size_t) r;
286                     ibuf_index = 0;
287                 }
288             } else if (watch_stdin->revents & POLLHUP)
289                 ibuf_eof = true;
290         }
291 
292         if (watch_socket) {
293             if (watch_socket->revents & POLLIN) {
294                 ssize_t r;
295                 pa_assert(obuf_length <= 0);
296 
297                 if ((r = pa_read(fd, obuf, obuf_size, &fd_type)) <= 0) {
298                     if (r < 0) {
299                         pa_log(_("read(): %s"), strerror(errno));
300                         goto quit;
301                     }
302 
303                     obuf_eof = true;
304                 } else {
305                     obuf_length = (size_t) r;
306                     obuf_index = 0;
307                 }
308             } else if (watch_socket->revents & POLLHUP)
309                 obuf_eof = true;
310         }
311 
312         if (watch_stdout) {
313             if (watch_stdout->revents & POLLHUP) {
314                 obuf_eof = true;
315                 obuf_length = 0;
316             } else if (watch_stdout->revents & POLLOUT) {
317                 ssize_t r;
318                 pa_assert(obuf_length > 0);
319 
320                 if ((r = pa_write(STDOUT_FILENO, obuf + obuf_index, obuf_length, &stdout_type)) < 0) {
321                     pa_log(_("write(): %s"), strerror(errno));
322                     goto quit;
323                 }
324 
325                 obuf_length -= (size_t) r;
326                 obuf_index += obuf_index;
327             }
328         }
329 
330         if (watch_socket) {
331             if (watch_socket->revents & POLLHUP) {
332                 ibuf_eof = true;
333                 ibuf_length = 0;
334             } else if (watch_socket->revents & POLLOUT) {
335                 ssize_t r;
336                 pa_assert(ibuf_length > 0);
337 
338                 if ((r = pa_write(fd, ibuf + ibuf_index, ibuf_length, &fd_type)) < 0) {
339                     pa_log(_("write(): %s"), strerror(errno));
340                     goto quit;
341                 }
342 
343                 ibuf_length -= (size_t) r;
344                 ibuf_index += obuf_index;
345             }
346         }
347     }
348 
349     ret = 0;
350 
351 quit:
352     if (fd >= 0)
353         pa_close(fd);
354 
355     pa_xfree(obuf);
356     pa_xfree(ibuf);
357 
358     return ret;
359 }
360