]> code.delx.au - pulseaudio/blob - src/utils/pacmd.c
Merge commit 'flameeyes/autoconf-2.62'
[pulseaudio] / src / utils / pacmd.c
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 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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <signal.h>
28 #include <sys/select.h>
29 #include <sys/socket.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <sys/un.h>
34 #include <locale.h>
35
36 #include <pulse/error.h>
37 #include <pulse/util.h>
38 #include <pulse/xmalloc.h>
39 #include <pulse/i18n.h>
40
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/pid.h>
44
45 int main(int argc, char*argv[]) {
46 pid_t pid ;
47 int fd = -1;
48 int ret = 1, i;
49 struct sockaddr_un sa;
50 char ibuf[256], obuf[256];
51 size_t ibuf_index, ibuf_length, obuf_index, obuf_length;
52 fd_set ifds, ofds;
53 char *cli;
54
55 setlocale(LC_ALL, "");
56 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
57
58 if (pa_pid_file_check_running(&pid, "pulseaudio") < 0) {
59 pa_log("No PulseAudio daemon running");
60 goto fail;
61 }
62
63 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
64 pa_log(_("socket(PF_UNIX, SOCK_STREAM, 0): %s"), strerror(errno));
65 goto fail;
66 }
67
68 memset(&sa, 0, sizeof(sa));
69 sa.sun_family = AF_UNIX;
70
71 if (!(cli = pa_runtime_path("cli")))
72 goto fail;
73
74 pa_strlcpy(sa.sun_path, cli, sizeof(sa.sun_path));
75 pa_xfree(cli);
76
77 for (i = 0; i < 5; i++) {
78 int r;
79
80 if ((r = connect(fd, (struct sockaddr*) &sa, sizeof(sa))) < 0 && (errno != ECONNREFUSED && errno != ENOENT)) {
81 pa_log(_("connect(): %s"), strerror(errno));
82 goto fail;
83 }
84
85 if (r >= 0)
86 break;
87
88 if (pa_pid_file_kill(SIGUSR2, NULL, "pulseaudio") < 0) {
89 pa_log(_("Failed to kill PulseAudio daemon."));
90 goto fail;
91 }
92
93 pa_msleep(300);
94 }
95
96 if (i >= 5) {
97 pa_log(_("Daemon not responding."));
98 goto fail;
99 }
100
101 ibuf_index = ibuf_length = obuf_index = obuf_length = 0;
102
103
104 FD_ZERO(&ifds);
105 FD_SET(0, &ifds);
106 FD_SET(fd, &ifds);
107
108 FD_ZERO(&ofds);
109
110 for (;;) {
111 if (select(FD_SETSIZE, &ifds, &ofds, NULL, NULL) < 0) {
112 pa_log(_("select(): %s"), strerror(errno));
113 goto fail;
114 }
115
116 if (FD_ISSET(0, &ifds)) {
117 ssize_t r;
118 assert(!ibuf_length);
119
120 if ((r = read(0, ibuf, sizeof(ibuf))) <= 0) {
121 if (r == 0)
122 break;
123
124 pa_log(_("read(): %s"), strerror(errno));
125 goto fail;
126 }
127
128 ibuf_length = (size_t) r;
129 ibuf_index = 0;
130 }
131
132 if (FD_ISSET(fd, &ifds)) {
133 ssize_t r;
134 assert(!obuf_length);
135
136 if ((r = read(fd, obuf, sizeof(obuf))) <= 0) {
137 if (r == 0)
138 break;
139
140 pa_log(_("read(): %s"), strerror(errno));
141 goto fail;
142 }
143
144 obuf_length = (size_t) r;
145 obuf_index = 0;
146 }
147
148 if (FD_ISSET(1, &ofds)) {
149 ssize_t r;
150 assert(obuf_length);
151
152 if ((r = write(1, obuf + obuf_index, obuf_length)) < 0) {
153 pa_log(_("write(): %s"), strerror(errno));
154 goto fail;
155 }
156
157 obuf_length -= (size_t) r;
158 obuf_index += obuf_index;
159
160 }
161
162 if (FD_ISSET(fd, &ofds)) {
163 ssize_t r;
164 assert(ibuf_length);
165
166 if ((r = write(fd, ibuf + ibuf_index, ibuf_length)) < 0) {
167 pa_log(_("write(): %s"), strerror(errno));
168 goto fail;
169 }
170
171 ibuf_length -= (size_t) r;
172 ibuf_index += obuf_index;
173
174 }
175
176 FD_ZERO(&ifds);
177 FD_ZERO(&ofds);
178
179 if (obuf_length <= 0)
180 FD_SET(fd, &ifds);
181 else
182 FD_SET(1, &ofds);
183
184 if (ibuf_length <= 0)
185 FD_SET(0, &ifds);
186 else
187 FD_SET(fd, &ofds);
188 }
189
190 ret = 0;
191
192 fail:
193 if (fd >= 0)
194 close(fd);
195
196 return ret;
197 }