]> code.delx.au - pulseaudio/blob - src/utils/pacmd.c
9583385b24b2025c734c04482544a9f07c972ecc
[pulseaudio] / src / utils / pacmd.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <assert.h>
29 #include <signal.h>
30 #include <sys/select.h>
31 #include <sys/socket.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <sys/un.h>
36
37 #include <pulse/error.h>
38 #include <pulse/util.h>
39
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/pid.h>
43
44 int main(PA_GCC_UNUSED int argc, PA_GCC_UNUSED char*argv[]) {
45 pid_t pid ;
46 int fd = -1;
47 int ret = 1, i;
48 struct sockaddr_un sa;
49 char ibuf[256], obuf[256];
50 size_t ibuf_index, ibuf_length, obuf_index, obuf_length;
51 fd_set ifds, ofds;
52
53 if (pa_pid_file_check_running(&pid) < 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 pa_runtime_path("cli", sa.sun_path, sizeof(sa.sun_path));
66
67 for (i = 0; i < 5; i++) {
68 int r;
69
70 if ((r = connect(fd, (struct sockaddr*) &sa, sizeof(sa))) < 0 && (errno != ECONNREFUSED && errno != ENOENT)) {
71 pa_log("connect(): %s", strerror(errno));
72 goto fail;
73 }
74
75 if (r >= 0)
76 break;
77
78 if (pa_pid_file_kill(SIGUSR2, NULL) < 0) {
79 pa_log("failed to kill PulseAudio daemon.");
80 goto fail;
81 }
82
83 pa_msleep(300);
84 }
85
86 if (i >= 5) {
87 pa_log("daemon not responding.");
88 goto fail;
89 }
90
91 ibuf_index = ibuf_length = obuf_index = obuf_length = 0;
92
93
94 FD_ZERO(&ifds);
95 FD_SET(0, &ifds);
96 FD_SET(fd, &ifds);
97
98 FD_ZERO(&ofds);
99
100 for (;;) {
101 if (select(FD_SETSIZE, &ifds, &ofds, NULL, NULL) < 0) {
102 pa_log("select(): %s", strerror(errno));
103 goto fail;
104 }
105
106 if (FD_ISSET(0, &ifds)) {
107 ssize_t r;
108 assert(!ibuf_length);
109
110 if ((r = read(0, ibuf, sizeof(ibuf))) <= 0) {
111 if (r == 0)
112 break;
113
114 pa_log("read(): %s", strerror(errno));
115 goto fail;
116 }
117
118 ibuf_length = (size_t) r;
119 ibuf_index = 0;
120 }
121
122 if (FD_ISSET(fd, &ifds)) {
123 ssize_t r;
124 assert(!obuf_length);
125
126 if ((r = read(fd, obuf, sizeof(obuf))) <= 0) {
127 if (r == 0)
128 break;
129
130 pa_log("read(): %s", strerror(errno));
131 goto fail;
132 }
133
134 obuf_length = (size_t) r;
135 obuf_index = 0;
136 }
137
138 if (FD_ISSET(1, &ofds)) {
139 ssize_t r;
140 assert(obuf_length);
141
142 if ((r = write(1, obuf + obuf_index, obuf_length)) < 0) {
143 pa_log("write(): %s", strerror(errno));
144 goto fail;
145 }
146
147 obuf_length -= (size_t) r;
148 obuf_index += obuf_index;
149
150 }
151
152 if (FD_ISSET(fd, &ofds)) {
153 ssize_t r;
154 assert(ibuf_length);
155
156 if ((r = write(fd, ibuf + ibuf_index, ibuf_length)) < 0) {
157 pa_log("write(): %s", strerror(errno));
158 goto fail;
159 }
160
161 ibuf_length -= (size_t) r;
162 ibuf_index += obuf_index;
163
164 }
165
166 FD_ZERO(&ifds);
167 FD_ZERO(&ofds);
168
169 if (obuf_length <= 0)
170 FD_SET(fd, &ifds);
171 else
172 FD_SET(1, &ofds);
173
174 if (ibuf_length <= 0)
175 FD_SET(0, &ifds);
176 else
177 FD_SET(fd, &ofds);
178 }
179
180
181 ret = 0;
182
183 fail:
184 if (fd >= 0)
185 close(fd);
186
187 return ret;
188 }