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