merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / 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 #include <pulse/xmalloc.h>
40
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/pid.h>
44
45 int main(PA_GCC_UNUSED int argc, PA_GCC_UNUSED 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     if (pa_pid_file_check_running(&pid, "pulseaudio") < 0) {
56         pa_log("no PulseAudio daemon running");
57         goto fail;
58     }
59
60     if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
61         pa_log("socket(PF_UNIX, SOCK_STREAM, 0): %s", strerror(errno));
62         goto fail;
63     }
64
65     memset(&sa, 0, sizeof(sa));
66     sa.sun_family = AF_UNIX;
67
68     cli = pa_runtime_path("cli");
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 }