fifo_output_plugin.c 6.02 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2010 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
21 22 23
#include "output_api.h"
#include "utils.h"
#include "timer.h"
24
#include "fd_util.h"
25
#include "open.h"
26

27 28
#include <glib.h>

29 30
#include <sys/types.h>
#include <sys/stat.h>
31 32 33
#include <errno.h>
#include <string.h>
#include <unistd.h>
34

Max Kellermann's avatar
Max Kellermann committed
35 36 37
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "fifo"

38 39
#define FIFO_BUFFER_SIZE 65536 /* pipe capacity on Linux >= 2.6.11 */

Max Kellermann's avatar
Max Kellermann committed
40
struct fifo_data {
41 42 43
	char *path;
	int input;
	int output;
44
	bool created;
45
	Timer *timer;
Max Kellermann's avatar
Max Kellermann committed
46
};
47

48 49 50 51 52 53 54 55 56
/**
 * The quark used for GError.domain.
 */
static inline GQuark
fifo_output_quark(void)
{
	return g_quark_from_static_string("fifo_output");
}

Max Kellermann's avatar
Max Kellermann committed
57
static struct fifo_data *fifo_data_new(void)
58
{
Max Kellermann's avatar
Max Kellermann committed
59
	struct fifo_data *ret;
60

Max Kellermann's avatar
Max Kellermann committed
61
	ret = g_new(struct fifo_data, 1);
62 63 64 65

	ret->path = NULL;
	ret->input = -1;
	ret->output = -1;
66
	ret->created = false;
67

68 69 70
	return ret;
}

Max Kellermann's avatar
Max Kellermann committed
71
static void fifo_data_free(struct fifo_data *fd)
72
{
73 74
	g_free(fd->path);
	g_free(fd);
75 76
}

Max Kellermann's avatar
Max Kellermann committed
77
static void fifo_delete(struct fifo_data *fd)
78
{
Max Kellermann's avatar
Max Kellermann committed
79
	g_debug("Removing FIFO \"%s\"", fd->path);
80 81

	if (unlink(fd->path) < 0) {
Max Kellermann's avatar
Max Kellermann committed
82 83
		g_warning("Could not remove FIFO \"%s\": %s",
			  fd->path, strerror(errno));
84 85 86
		return;
	}

87
	fd->created = false;
88 89
}

Max Kellermann's avatar
Max Kellermann committed
90 91
static void
fifo_close(struct fifo_data *fd)
92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
	struct stat st;

	if (fd->input >= 0) {
		close(fd->input);
		fd->input = -1;
	}

	if (fd->output >= 0) {
		close(fd->output);
		fd->output = -1;
	}

	if (fd->created && (stat(fd->path, &st) == 0))
Max Kellermann's avatar
Max Kellermann committed
106
		fifo_delete(fd);
107 108
}

109
static bool
110
fifo_make(struct fifo_data *fd, GError **error)
111 112
{
	if (mkfifo(fd->path, 0666) < 0) {
113 114 115
		g_set_error(error, fifo_output_quark(), errno,
			    "Couldn't create FIFO \"%s\": %s",
			    fd->path, strerror(errno));
116
		return false;
117 118
	}

119
	fd->created = true;
120

121
	return true;
122 123
}

124
static bool
125
fifo_check(struct fifo_data *fd, GError **error)
126 127 128 129 130 131
{
	struct stat st;

	if (stat(fd->path, &st) < 0) {
		if (errno == ENOENT) {
			/* Path doesn't exist */
132
			return fifo_make(fd, error);
133 134
		}

135 136 137
		g_set_error(error, fifo_output_quark(), errno,
			    "Failed to stat FIFO \"%s\": %s",
			    fd->path, strerror(errno));
138
		return false;
139 140 141
	}

	if (!S_ISFIFO(st.st_mode)) {
142 143 144
		g_set_error(error, fifo_output_quark(), 0,
			    "\"%s\" already exists, but is not a FIFO",
			    fd->path);
145
		return false;
146 147
	}

148
	return true;
149 150
}

Max Kellermann's avatar
Max Kellermann committed
151
static bool
152
fifo_open(struct fifo_data *fd, GError **error)
153
{
154
	if (!fifo_check(fd, error))
155
		return false;
156

157
	fd->input = open_cloexec(fd->path, O_RDONLY|O_NONBLOCK|O_BINARY, 0);
158
	if (fd->input < 0) {
159 160 161
		g_set_error(error, fifo_output_quark(), errno,
			    "Could not open FIFO \"%s\" for reading: %s",
			    fd->path, strerror(errno));
Max Kellermann's avatar
Max Kellermann committed
162
		fifo_close(fd);
163
		return false;
164 165
	}

166
	fd->output = open_cloexec(fd->path, O_WRONLY|O_NONBLOCK|O_BINARY, 0);
167
	if (fd->output < 0) {
168 169 170
		g_set_error(error, fifo_output_quark(), errno,
			    "Could not open FIFO \"%s\" for writing: %s",
			    fd->path, strerror(errno));
Max Kellermann's avatar
Max Kellermann committed
171
		fifo_close(fd);
172
		return false;
173 174
	}

175
	return true;
176 177
}

178
static void *
Max Kellermann's avatar
Max Kellermann committed
179
fifo_output_init(G_GNUC_UNUSED const struct audio_format *audio_format,
180 181
		 const struct config_param *param,
		 GError **error)
182
{
Max Kellermann's avatar
Max Kellermann committed
183
	struct fifo_data *fd;
184
	char *value, *path;
185

186
	value = config_dup_block_string(param, "path", NULL);
187 188 189 190 191
	if (value == NULL) {
		g_set_error(error, fifo_output_quark(), errno,
			    "No \"path\" parameter specified");
		return NULL;
	}
192

193 194
	path = parsePath(value);
	g_free(value);
195
	if (!path) {
196 197 198
		g_set_error(error, fifo_output_quark(), errno,
			    "Could not parse \"path\" parameter");
		return NULL;
199 200
	}

Max Kellermann's avatar
Max Kellermann committed
201
	fd = fifo_data_new();
202
	fd->path = path;
203

204
	if (!fifo_open(fd, error)) {
Max Kellermann's avatar
Max Kellermann committed
205
		fifo_data_free(fd);
206
		return NULL;
207 208
	}

209
	return fd;
210 211
}

Max Kellermann's avatar
Max Kellermann committed
212 213
static void
fifo_output_finish(void *data)
214
{
Max Kellermann's avatar
Max Kellermann committed
215
	struct fifo_data *fd = (struct fifo_data *)data;
216

Max Kellermann's avatar
Max Kellermann committed
217 218
	fifo_close(fd);
	fifo_data_free(fd);
219 220
}

Max Kellermann's avatar
Max Kellermann committed
221
static bool
222 223
fifo_output_open(void *data, struct audio_format *audio_format,
		 G_GNUC_UNUSED GError **error)
224
{
Max Kellermann's avatar
Max Kellermann committed
225
	struct fifo_data *fd = (struct fifo_data *)data;
226

227
	fd->timer = timer_new(audio_format);
228

229
	return true;
230 231
}

Max Kellermann's avatar
Max Kellermann committed
232 233
static void
fifo_output_close(void *data)
234
{
Max Kellermann's avatar
Max Kellermann committed
235
	struct fifo_data *fd = (struct fifo_data *)data;
236

237
	timer_free(fd->timer);
238 239
}

Max Kellermann's avatar
Max Kellermann committed
240 241
static void
fifo_output_cancel(void *data)
242
{
Max Kellermann's avatar
Max Kellermann committed
243
	struct fifo_data *fd = (struct fifo_data *)data;
244 245 246 247 248 249 250 251 252
	char buf[FIFO_BUFFER_SIZE];
	int bytes = 1;

	timer_reset(fd->timer);

	while (bytes > 0 && errno != EINTR)
		bytes = read(fd->input, buf, FIFO_BUFFER_SIZE);

	if (bytes < 0 && errno != EAGAIN) {
Max Kellermann's avatar
Max Kellermann committed
253 254
		g_warning("Flush of FIFO \"%s\" failed: %s",
			  fd->path, strerror(errno));
255 256 257
	}
}

258
static size_t
259 260
fifo_output_play(void *data, const void *chunk, size_t size,
		 GError **error)
261
{
Max Kellermann's avatar
Max Kellermann committed
262
	struct fifo_data *fd = (struct fifo_data *)data;
263
	ssize_t bytes;
264 265 266 267 268 269 270 271

	if (!fd->timer->started)
		timer_start(fd->timer);
	else
		timer_sync(fd->timer);

	timer_add(fd->timer, size);

272
	while (true) {
273
		bytes = write(fd->output, chunk, size);
274 275 276
		if (bytes > 0)
			return (size_t)bytes;

277 278 279 280
		if (bytes < 0) {
			switch (errno) {
			case EAGAIN:
				/* The pipe is full, so empty it */
Max Kellermann's avatar
Max Kellermann committed
281
				fifo_output_cancel(fd);
282 283 284 285 286
				continue;
			case EINTR:
				continue;
			}

287 288 289
			g_set_error(error, fifo_output_quark(), errno,
				    "Failed to write to FIFO %s: %s",
				    fd->path, g_strerror(errno));
290
			return 0;
291 292 293 294
		}
	}
}

Max Kellermann's avatar
Max Kellermann committed
295
const struct audio_output_plugin fifo_output_plugin = {
296
	.name = "fifo",
Max Kellermann's avatar
Max Kellermann committed
297 298 299 300 301 302
	.init = fifo_output_init,
	.finish = fifo_output_finish,
	.open = fifo_output_open,
	.close = fifo_output_close,
	.play = fifo_output_play,
	.cancel = fifo_output_cancel,
303
};