Blame aserver/aserver.c

Packit 4a16fb
/*
Packit 4a16fb
 *  ALSA server
Packit 4a16fb
 *  Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU General Public License as published by
Packit 4a16fb
 *   the Free Software Foundation; either version 2 of the License, or
Packit 4a16fb
 *   (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU General Public License
Packit 4a16fb
 *   along with this program; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include <sys/shm.h>
Packit 4a16fb
#include <sys/socket.h>
Packit 4a16fb
#include <poll.h>
Packit 4a16fb
#include <sys/un.h>
Packit 4a16fb
#include <sys/uio.h>
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <unistd.h>
Packit 4a16fb
#include <fcntl.h>
Packit 4a16fb
#include <stddef.h>
Packit 4a16fb
#include <getopt.h>
Packit 4a16fb
#include <netinet/in.h>
Packit 4a16fb
#include <netdb.h>
Packit 4a16fb
#include <limits.h>
Packit 4a16fb
#include <signal.h>
Packit 4a16fb
Packit 4a16fb
#include "aserver.h"
Packit 4a16fb
Packit 4a16fb
char *command;
Packit 4a16fb
Packit 4a16fb
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
Packit 4a16fb
#define ERROR(...) do {\
Packit 4a16fb
	fprintf(stderr, "%s %s:%i:(%s) ", command, __FILE__, __LINE__, __func__); \
Packit 4a16fb
	fprintf(stderr, __VA_ARGS__); \
Packit 4a16fb
	putc('\n', stderr); \
Packit 4a16fb
} while (0)
Packit 4a16fb
#else
Packit 4a16fb
#define ERROR(args...) do {\
Packit 4a16fb
	fprintf(stderr, "%s %s:%i:(%s) ", command, __FILE__, __LINE__, __func__); \
Packit 4a16fb
	fprintf(stderr, ##args); \
Packit 4a16fb
	putc('\n', stderr); \
Packit 4a16fb
} while (0)
Packit 4a16fb
#endif	
Packit 4a16fb
Packit 4a16fb
#define SYSERROR(string) ERROR(string ": %s", strerror(errno))
Packit 4a16fb
Packit 4a16fb
static int make_local_socket(const char *filename)
Packit 4a16fb
{
Packit 4a16fb
	size_t l = strlen(filename);
Packit 4a16fb
	size_t size = offsetof(struct sockaddr_un, sun_path) + l;
Packit 4a16fb
	struct sockaddr_un *addr = alloca(size);
Packit 4a16fb
	int sock;
Packit 4a16fb
Packit 4a16fb
	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
Packit 4a16fb
	if (sock < 0) {
Packit 4a16fb
		int result = -errno;
Packit 4a16fb
		SYSERROR("socket failed");
Packit 4a16fb
		return result;
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	unlink(filename);
Packit 4a16fb
Packit 4a16fb
	addr->sun_family = AF_LOCAL;
Packit 4a16fb
	memcpy(addr->sun_path, filename, l);
Packit 4a16fb
Packit 4a16fb
	if (bind(sock, (struct sockaddr *) addr, size) < 0) {
Packit 4a16fb
		int result = -errno;
Packit 4a16fb
		SYSERROR("bind failed");
Packit 4a16fb
		close(sock);
Packit 4a16fb
		return result;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return sock;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int make_inet_socket(int port)
Packit 4a16fb
{
Packit 4a16fb
	struct sockaddr_in addr;
Packit 4a16fb
	int sock;
Packit 4a16fb
Packit 4a16fb
	sock = socket(PF_INET, SOCK_STREAM, 0);
Packit 4a16fb
	if (sock < 0) {
Packit 4a16fb
		int result = -errno;
Packit 4a16fb
		SYSERROR("socket failed");
Packit 4a16fb
		return result;
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	memset(&addr, 0, sizeof(addr));
Packit 4a16fb
	addr.sin_family = AF_INET;
Packit 4a16fb
	addr.sin_port = htons(port);
Packit 4a16fb
	addr.sin_addr.s_addr = INADDR_ANY;
Packit 4a16fb
Packit 4a16fb
	if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Packit 4a16fb
		int result = -errno;
Packit 4a16fb
		SYSERROR("bind failed");
Packit 4a16fb
		close(sock);
Packit 4a16fb
		return result;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return sock;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
struct pollfd *pollfds;
Packit 4a16fb
unsigned int pollfds_count = 0;
Packit 4a16fb
typedef struct waiter waiter_t;
Packit 4a16fb
typedef int (*waiter_handler_t)(waiter_t *waiter, unsigned short events);
Packit 4a16fb
struct waiter {
Packit 4a16fb
	int fd;
Packit 4a16fb
	void *private_data;
Packit 4a16fb
	waiter_handler_t handler;
Packit 4a16fb
};
Packit 4a16fb
waiter_t *waiters;
Packit 4a16fb
Packit 4a16fb
static void add_waiter(int fd, unsigned short events, waiter_handler_t handler,
Packit 4a16fb
		void *data)
Packit 4a16fb
{
Packit 4a16fb
	waiter_t *w = &waiters[fd];
Packit 4a16fb
	struct pollfd *pfd = &pollfds[pollfds_count];
Packit 4a16fb
	assert(!w->handler);
Packit 4a16fb
	pfd->fd = fd;
Packit 4a16fb
	pfd->events = events;
Packit 4a16fb
	pfd->revents = 0;
Packit 4a16fb
	w->fd = fd;
Packit 4a16fb
	w->private_data = data;
Packit 4a16fb
	w->handler = handler;
Packit 4a16fb
	pollfds_count++;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void del_waiter(int fd)
Packit 4a16fb
{
Packit 4a16fb
	waiter_t *w = &waiters[fd];
Packit 4a16fb
	unsigned int k;
Packit 4a16fb
	assert(w->handler);
Packit 4a16fb
	w->handler = 0;
Packit 4a16fb
	for (k = 0; k < pollfds_count; ++k) {
Packit 4a16fb
		if (pollfds[k].fd == fd)
Packit 4a16fb
			break;
Packit 4a16fb
	}
Packit 4a16fb
	assert(k < pollfds_count);
Packit 4a16fb
	pollfds_count--;
Packit 4a16fb
	memmove(&pollfds[k], &pollfds[k + 1], pollfds_count - k);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
typedef struct client client_t;
Packit 4a16fb
Packit 4a16fb
typedef struct {
Packit 4a16fb
	int (*open)(client_t *client, int *cookie);
Packit 4a16fb
	int (*cmd)(client_t *client);
Packit 4a16fb
	int (*close)(client_t *client);
Packit 4a16fb
} transport_ops_t;
Packit 4a16fb
Packit 4a16fb
struct client {
Packit 4a16fb
	struct list_head list;
Packit 4a16fb
	int poll_fd;
Packit 4a16fb
	int ctrl_fd;
Packit 4a16fb
	int local;
Packit 4a16fb
	int transport_type;
Packit 4a16fb
	int dev_type;
Packit 4a16fb
	char name[256];
Packit 4a16fb
	int stream;
Packit 4a16fb
	int mode;
Packit 4a16fb
	transport_ops_t *ops;
Packit 4a16fb
	snd_async_handler_t *async_handler;
Packit 4a16fb
	int async_sig;
Packit 4a16fb
	pid_t async_pid;
Packit 4a16fb
	union {
Packit 4a16fb
		struct {
Packit 4a16fb
			snd_pcm_t *handle;
Packit 4a16fb
			int fd;
Packit 4a16fb
		} pcm;
Packit 4a16fb
		struct {
Packit 4a16fb
			snd_ctl_t *handle;
Packit 4a16fb
			int fd;
Packit 4a16fb
		} ctl;
Packit 4a16fb
#if 0
Packit 4a16fb
		struct {
Packit 4a16fb
			snd_rawmidi_t *handle;
Packit 4a16fb
		} rawmidi;
Packit 4a16fb
		struct {
Packit 4a16fb
			snd_timer_open_t *handle;
Packit 4a16fb
		} timer;
Packit 4a16fb
		struct {
Packit 4a16fb
			snd_hwdep_t *handle;
Packit 4a16fb
		} hwdep;
Packit 4a16fb
		struct {
Packit 4a16fb
			snd_seq_t *handle;
Packit 4a16fb
		} seq;
Packit 4a16fb
#endif
Packit 4a16fb
	} device;
Packit 4a16fb
	int polling;
Packit 4a16fb
	int open;
Packit 4a16fb
	int cookie;
Packit 4a16fb
	union {
Packit 4a16fb
		struct {
Packit 4a16fb
			int ctrl_id;
Packit 4a16fb
			void *ctrl;
Packit 4a16fb
		} shm;
Packit 4a16fb
	} transport;
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
LIST_HEAD(clients);
Packit 4a16fb
Packit 4a16fb
typedef struct {
Packit 4a16fb
	struct list_head list;
Packit 4a16fb
	int fd;
Packit 4a16fb
	uint32_t cookie;
Packit 4a16fb
} inet_pending_t;
Packit 4a16fb
LIST_HEAD(inet_pendings);
Packit 4a16fb
Packit 4a16fb
#if 0
Packit 4a16fb
static int pcm_handler(waiter_t *waiter, unsigned short events)
Packit 4a16fb
{
Packit 4a16fb
	client_t *client = waiter->private_data;
Packit 4a16fb
	char buf[1];
Packit 4a16fb
	ssize_t n;
Packit 4a16fb
	if (events & POLLIN) {
Packit 4a16fb
		n = write(client->poll_fd, buf, 1);
Packit 4a16fb
		if (n != 1) {
Packit 4a16fb
			SYSERROR("write failed");
Packit 4a16fb
			return -errno;
Packit 4a16fb
		}
Packit 4a16fb
	} else if (events & POLLOUT) {
Packit 4a16fb
		n = read(client->poll_fd, buf, 1);
Packit 4a16fb
		if (n != 1) {
Packit 4a16fb
			SYSERROR("read failed");
Packit 4a16fb
			return -errno;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	del_waiter(waiter->fd);
Packit 4a16fb
	client->polling = 0;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static void pcm_shm_hw_ptr_changed(snd_pcm_t *pcm, snd_pcm_t *src ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	client_t *client = pcm->hw.private_data;
Packit 4a16fb
	volatile snd_pcm_shm_ctrl_t *ctrl = client->transport.shm.ctrl;
Packit 4a16fb
	snd_pcm_t *loop;
Packit 4a16fb
Packit 4a16fb
	ctrl->hw.changed = 1;
Packit 4a16fb
	if (pcm->hw.fd >= 0) {
Packit 4a16fb
		ctrl->hw.use_mmap = 1;
Packit 4a16fb
		ctrl->hw.offset = pcm->hw.offset;
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	ctrl->hw.use_mmap = 0;
Packit 4a16fb
	ctrl->hw.ptr = pcm->hw.ptr ? *pcm->hw.ptr : 0;
Packit 4a16fb
	for (loop = pcm->hw.master; loop; loop = loop->hw.master)
Packit 4a16fb
		loop->hw.ptr = &ctrl->hw.ptr;
Packit 4a16fb
	pcm->hw.ptr = &ctrl->hw.ptr;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void pcm_shm_appl_ptr_changed(snd_pcm_t *pcm, snd_pcm_t *src ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	client_t *client = pcm->appl.private_data;
Packit 4a16fb
	volatile snd_pcm_shm_ctrl_t *ctrl = client->transport.shm.ctrl;
Packit 4a16fb
	snd_pcm_t *loop;
Packit 4a16fb
Packit 4a16fb
	ctrl->appl.changed = 1;
Packit 4a16fb
	if (pcm->appl.fd >= 0) {
Packit 4a16fb
		ctrl->appl.use_mmap = 1;
Packit 4a16fb
		ctrl->appl.offset = pcm->appl.offset;
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	ctrl->appl.use_mmap = 0;
Packit 4a16fb
	ctrl->appl.ptr = pcm->appl.ptr ? *pcm->appl.ptr : 0;
Packit 4a16fb
	for (loop = pcm->appl.master; loop; loop = loop->appl.master)
Packit 4a16fb
		loop->appl.ptr = &ctrl->appl.ptr;
Packit 4a16fb
	pcm->appl.ptr = &ctrl->appl.ptr;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int pcm_shm_open(client_t *client, int *cookie)
Packit 4a16fb
{
Packit 4a16fb
	int shmid;
Packit 4a16fb
	snd_pcm_t *pcm;
Packit 4a16fb
	int err;
Packit 4a16fb
	int result;
Packit 4a16fb
	err = snd_pcm_open(&pcm, client->name, client->stream, SND_PCM_NONBLOCK);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	client->device.pcm.handle = pcm;
Packit 4a16fb
	client->device.pcm.fd = _snd_pcm_poll_descriptor(pcm);
Packit 4a16fb
	pcm->hw.private_data = client;
Packit 4a16fb
	pcm->hw.changed = pcm_shm_hw_ptr_changed;
Packit 4a16fb
	pcm->appl.private_data = client;
Packit 4a16fb
	pcm->appl.changed = pcm_shm_appl_ptr_changed;
Packit 4a16fb
Packit 4a16fb
	shmid = shmget(IPC_PRIVATE, PCM_SHM_SIZE, 0666);
Packit 4a16fb
	if (shmid < 0) {
Packit 4a16fb
		result = -errno;
Packit 4a16fb
		SYSERROR("shmget failed");
Packit 4a16fb
		goto _err;
Packit 4a16fb
	}
Packit 4a16fb
	client->transport.shm.ctrl_id = shmid;
Packit 4a16fb
	client->transport.shm.ctrl = shmat(shmid, 0, 0);
Packit 4a16fb
	if (client->transport.shm.ctrl == (void*) -1) {
Packit 4a16fb
		result = -errno;
Packit 4a16fb
		shmctl(shmid, IPC_RMID, 0);
Packit 4a16fb
		SYSERROR("shmat failed");
Packit 4a16fb
		goto _err;
Packit 4a16fb
	}
Packit 4a16fb
	*cookie = shmid;
Packit 4a16fb
	return 0;
Packit 4a16fb
Packit 4a16fb
 _err:
Packit 4a16fb
	snd_pcm_close(pcm);
Packit 4a16fb
	return result;
Packit 4a16fb
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int pcm_shm_close(client_t *client)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_pcm_shm_ctrl_t *ctrl = client->transport.shm.ctrl;
Packit 4a16fb
	if (client->polling) {
Packit 4a16fb
		del_waiter(client->device.pcm.fd);
Packit 4a16fb
		client->polling = 0;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_close(client->device.pcm.handle);
Packit 4a16fb
	ctrl->result = err;
Packit 4a16fb
	if (err < 0) 
Packit 4a16fb
		ERROR("snd_pcm_close");
Packit 4a16fb
	if (client->transport.shm.ctrl) {
Packit 4a16fb
		err = shmdt((void *)client->transport.shm.ctrl);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			SYSERROR("shmdt failed");
Packit 4a16fb
		err = shmctl(client->transport.shm.ctrl_id, IPC_RMID, 0);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			SYSERROR("shmctl IPC_RMID failed");
Packit 4a16fb
		client->transport.shm.ctrl = 0;
Packit 4a16fb
	}
Packit 4a16fb
	client->open = 0;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int shm_ack(client_t *client)
Packit 4a16fb
{
Packit 4a16fb
	struct pollfd pfd;
Packit 4a16fb
	int err;
Packit 4a16fb
	char buf[1];
Packit 4a16fb
	pfd.fd = client->ctrl_fd;
Packit 4a16fb
	pfd.events = POLLHUP;
Packit 4a16fb
	if (poll(&pfd, 1, 0) == 1)
Packit 4a16fb
		return -EBADFD;
Packit 4a16fb
	err = write(client->ctrl_fd, buf, 1);
Packit 4a16fb
	if (err != 1)
Packit 4a16fb
		return -EBADFD;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int shm_ack_fd(client_t *client, int fd)
Packit 4a16fb
{
Packit 4a16fb
	struct pollfd pfd;
Packit 4a16fb
	int err;
Packit 4a16fb
	char buf[1];
Packit 4a16fb
	pfd.fd = client->ctrl_fd;
Packit 4a16fb
	pfd.events = POLLHUP;
Packit 4a16fb
	if (poll(&pfd, 1, 0) == 1)
Packit 4a16fb
		return -EBADFD;
Packit 4a16fb
	err = snd_send_fd(client->ctrl_fd, buf, 1, fd);
Packit 4a16fb
	if (err != 1)
Packit 4a16fb
		return -EBADFD;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int shm_rbptr_fd(client_t *client, snd_pcm_rbptr_t *rbptr)
Packit 4a16fb
{
Packit 4a16fb
	if (rbptr->fd < 0)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	return shm_ack_fd(client, rbptr->fd);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void async_handler(snd_async_handler_t *handler)
Packit 4a16fb
{
Packit 4a16fb
	client_t *client = snd_async_handler_get_callback_private(handler);
Packit 4a16fb
	/* FIXME: use sigqueue */
Packit 4a16fb
	kill(client->async_pid, client->async_sig);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int pcm_shm_cmd(client_t *client)
Packit 4a16fb
{
Packit 4a16fb
	volatile snd_pcm_shm_ctrl_t *ctrl = client->transport.shm.ctrl;
Packit 4a16fb
	char buf[1];
Packit 4a16fb
	int err;
Packit 4a16fb
	int cmd;
Packit 4a16fb
	snd_pcm_t *pcm;
Packit 4a16fb
	err = read(client->ctrl_fd, buf, 1);
Packit 4a16fb
	if (err != 1)
Packit 4a16fb
		return -EBADFD;
Packit 4a16fb
	cmd = ctrl->cmd;
Packit 4a16fb
	ctrl->cmd = 0;
Packit 4a16fb
	pcm = client->device.pcm.handle;
Packit 4a16fb
	switch (cmd) {
Packit 4a16fb
	case SND_PCM_IOCTL_ASYNC:
Packit 4a16fb
		ctrl->result = snd_pcm_async(pcm, ctrl->u.async.sig, ctrl->u.async.pid);
Packit 4a16fb
		if (ctrl->result < 0)
Packit 4a16fb
			break;
Packit 4a16fb
		if (ctrl->u.async.sig >= 0) {
Packit 4a16fb
			assert(client->async_sig < 0);
Packit 4a16fb
			ctrl->result = snd_async_add_pcm_handler(&client->async_handler, pcm, async_handler, client);
Packit 4a16fb
			if (ctrl->result < 0)
Packit 4a16fb
				break;
Packit 4a16fb
		} else {
Packit 4a16fb
			assert(client->async_sig >= 0);
Packit 4a16fb
			snd_async_del_handler(client->async_handler);
Packit 4a16fb
		}
Packit 4a16fb
		client->async_sig = ctrl->u.async.sig;
Packit 4a16fb
		client->async_pid = ctrl->u.async.pid;
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_INFO:
Packit 4a16fb
		ctrl->result = snd_pcm_info(pcm, (snd_pcm_info_t *) &ctrl->u.info);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_HW_REFINE:
Packit 4a16fb
		ctrl->result = snd_pcm_hw_refine(pcm, (snd_pcm_hw_params_t *) &ctrl->u.hw_refine);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_HW_PARAMS:
Packit 4a16fb
		ctrl->result = snd_pcm_hw_params(pcm, (snd_pcm_hw_params_t *) &ctrl->u.hw_params);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_HW_FREE:
Packit 4a16fb
		ctrl->result = snd_pcm_hw_free(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_SW_PARAMS:
Packit 4a16fb
		ctrl->result = snd_pcm_sw_params(pcm, (snd_pcm_sw_params_t *) &ctrl->u.sw_params);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_STATUS:
Packit 4a16fb
		ctrl->result = snd_pcm_status(pcm, (snd_pcm_status_t *) &ctrl->u.status);
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_PCM_IOCTL_STATE:
Packit 4a16fb
		ctrl->result = snd_pcm_state(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_PCM_IOCTL_HWSYNC:
Packit 4a16fb
		ctrl->result = snd_pcm_hwsync(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_DELAY:
Packit 4a16fb
		ctrl->result = snd_pcm_delay(pcm, (snd_pcm_sframes_t *) &ctrl->u.delay.frames);
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_PCM_IOCTL_AVAIL_UPDATE:
Packit 4a16fb
		ctrl->result = snd_pcm_avail_update(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_PREPARE:
Packit 4a16fb
		ctrl->result = snd_pcm_prepare(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_RESET:
Packit 4a16fb
		ctrl->result = snd_pcm_reset(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_START:
Packit 4a16fb
		ctrl->result = snd_pcm_start(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_DRAIN:
Packit 4a16fb
		ctrl->result = snd_pcm_drain(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_DROP:
Packit 4a16fb
		ctrl->result = snd_pcm_drop(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_PAUSE:
Packit 4a16fb
		ctrl->result = snd_pcm_pause(pcm, ctrl->u.pause.enable);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_CHANNEL_INFO:
Packit 4a16fb
		ctrl->result = snd_pcm_channel_info(pcm, (snd_pcm_channel_info_t *) &ctrl->u.channel_info);
Packit 4a16fb
		if (ctrl->result >= 0 &&
Packit 4a16fb
		    ctrl->u.channel_info.type == SND_PCM_AREA_MMAP)
Packit 4a16fb
			return shm_ack_fd(client, ctrl->u.channel_info.u.mmap.fd);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_REWIND:
Packit 4a16fb
		ctrl->result = snd_pcm_rewind(pcm, ctrl->u.rewind.frames);
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_PCM_IOCTL_FORWARD:
Packit 4a16fb
		ctrl->result = snd_pcm_forward(pcm, ctrl->u.forward.frames);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_LINK:
Packit 4a16fb
	{
Packit 4a16fb
		/* FIXME */
Packit 4a16fb
		ctrl->result = -ENOSYS;
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
	case SNDRV_PCM_IOCTL_UNLINK:
Packit 4a16fb
		ctrl->result = snd_pcm_unlink(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_PCM_IOCTL_RESUME:
Packit 4a16fb
		ctrl->result = snd_pcm_resume(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_PCM_IOCTL_MMAP:
Packit 4a16fb
	{
Packit 4a16fb
		ctrl->result = snd_pcm_mmap(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
	case SND_PCM_IOCTL_MUNMAP:
Packit 4a16fb
	{
Packit 4a16fb
		ctrl->result = snd_pcm_munmap(pcm);
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
	case SND_PCM_IOCTL_MMAP_COMMIT:
Packit 4a16fb
		ctrl->result = snd_pcm_mmap_commit(pcm,
Packit 4a16fb
						   ctrl->u.mmap_commit.offset,
Packit 4a16fb
						   ctrl->u.mmap_commit.frames);
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_PCM_IOCTL_POLL_DESCRIPTOR:
Packit 4a16fb
		ctrl->result = 0;
Packit 4a16fb
		return shm_ack_fd(client, _snd_pcm_poll_descriptor(pcm));
Packit 4a16fb
	case SND_PCM_IOCTL_CLOSE:
Packit 4a16fb
		client->ops->close(client);
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_PCM_IOCTL_HW_PTR_FD:
Packit 4a16fb
		return shm_rbptr_fd(client, &pcm->hw);
Packit 4a16fb
	case SND_PCM_IOCTL_APPL_PTR_FD:
Packit 4a16fb
		return shm_rbptr_fd(client, &pcm->appl);
Packit 4a16fb
	default:
Packit 4a16fb
		ERROR("Bogus cmd: %x", ctrl->cmd);
Packit 4a16fb
		ctrl->result = -ENOSYS;
Packit 4a16fb
	}
Packit 4a16fb
	return shm_ack(client);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
transport_ops_t pcm_shm_ops = {
Packit 4a16fb
	.open	= pcm_shm_open,
Packit 4a16fb
	.cmd	= pcm_shm_cmd,
Packit 4a16fb
	.close	= pcm_shm_close,
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static int ctl_handler(waiter_t *waiter, unsigned short events)
Packit 4a16fb
{
Packit 4a16fb
	client_t *client = waiter->private_data;
Packit 4a16fb
	char buf[1];
Packit 4a16fb
	ssize_t n;
Packit 4a16fb
	if (events & POLLIN) {
Packit 4a16fb
		n = write(client->poll_fd, buf, 1);
Packit 4a16fb
		if (n != 1) {
Packit 4a16fb
			SYSERROR("write failed");
Packit 4a16fb
			return -errno;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	del_waiter(waiter->fd);
Packit 4a16fb
	client->polling = 0;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int ctl_shm_open(client_t *client, int *cookie)
Packit 4a16fb
{
Packit 4a16fb
	int shmid;
Packit 4a16fb
	snd_ctl_t *ctl;
Packit 4a16fb
	int err;
Packit 4a16fb
	int result;
Packit 4a16fb
	err = snd_ctl_open(&ctl, client->name, SND_CTL_NONBLOCK);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	client->device.ctl.handle = ctl;
Packit 4a16fb
	client->device.ctl.fd = _snd_ctl_poll_descriptor(ctl);
Packit 4a16fb
Packit 4a16fb
	shmid = shmget(IPC_PRIVATE, CTL_SHM_SIZE, 0666);
Packit 4a16fb
	if (shmid < 0) {
Packit 4a16fb
		result = -errno;
Packit 4a16fb
		SYSERROR("shmget failed");
Packit 4a16fb
		goto _err;
Packit 4a16fb
	}
Packit 4a16fb
	client->transport.shm.ctrl_id = shmid;
Packit 4a16fb
	client->transport.shm.ctrl = shmat(shmid, 0, 0);
Packit 4a16fb
	if (!client->transport.shm.ctrl) {
Packit 4a16fb
		result = -errno;
Packit 4a16fb
		shmctl(shmid, IPC_RMID, 0);
Packit 4a16fb
		SYSERROR("shmat failed");
Packit 4a16fb
		goto _err;
Packit 4a16fb
	}
Packit 4a16fb
	*cookie = shmid;
Packit 4a16fb
	add_waiter(client->device.ctl.fd, POLLIN, ctl_handler, client);
Packit 4a16fb
	client->polling = 1;
Packit 4a16fb
	return 0;
Packit 4a16fb
Packit 4a16fb
 _err:
Packit 4a16fb
	snd_ctl_close(ctl);
Packit 4a16fb
	return result;
Packit 4a16fb
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int ctl_shm_close(client_t *client)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_ctl_shm_ctrl_t *ctrl = client->transport.shm.ctrl;
Packit 4a16fb
	if (client->polling) {
Packit 4a16fb
		del_waiter(client->device.ctl.fd);
Packit 4a16fb
		client->polling = 0;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_ctl_close(client->device.ctl.handle);
Packit 4a16fb
	ctrl->result = err;
Packit 4a16fb
	if (err < 0) 
Packit 4a16fb
		ERROR("snd_ctl_close");
Packit 4a16fb
	if (client->transport.shm.ctrl) {
Packit 4a16fb
		err = shmdt((void *)client->transport.shm.ctrl);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			SYSERROR("shmdt failed");
Packit 4a16fb
		err = shmctl(client->transport.shm.ctrl_id, IPC_RMID, 0);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			SYSERROR("shmctl failed");
Packit 4a16fb
		client->transport.shm.ctrl = 0;
Packit 4a16fb
	}
Packit 4a16fb
	client->open = 0;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int ctl_shm_cmd(client_t *client)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_shm_ctrl_t *ctrl = client->transport.shm.ctrl;
Packit 4a16fb
	char buf[1];
Packit 4a16fb
	int err;
Packit 4a16fb
	int cmd;
Packit 4a16fb
	snd_ctl_t *ctl;
Packit 4a16fb
	err = read(client->ctrl_fd, buf, 1);
Packit 4a16fb
	if (err != 1)
Packit 4a16fb
		return -EBADFD;
Packit 4a16fb
	cmd = ctrl->cmd;
Packit 4a16fb
	ctrl->cmd = 0;
Packit 4a16fb
	ctl = client->device.ctl.handle;
Packit 4a16fb
	switch (cmd) {
Packit 4a16fb
	case SND_CTL_IOCTL_ASYNC:
Packit 4a16fb
		ctrl->result = snd_ctl_async(ctl, ctrl->u.async.sig, ctrl->u.async.pid);
Packit 4a16fb
		if (ctrl->result < 0)
Packit 4a16fb
			break;
Packit 4a16fb
		if (ctrl->u.async.sig >= 0) {
Packit 4a16fb
			assert(client->async_sig < 0);
Packit 4a16fb
			ctrl->result = snd_async_add_ctl_handler(&client->async_handler, ctl, async_handler, client);
Packit 4a16fb
			if (ctrl->result < 0)
Packit 4a16fb
				break;
Packit 4a16fb
		} else {
Packit 4a16fb
			assert(client->async_sig >= 0);
Packit 4a16fb
			snd_async_del_handler(client->async_handler);
Packit 4a16fb
		}
Packit 4a16fb
		client->async_sig = ctrl->u.async.sig;
Packit 4a16fb
		client->async_pid = ctrl->u.async.pid;
Packit 4a16fb
		break;
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
Packit 4a16fb
		ctrl->result = snd_ctl_subscribe_events(ctl, ctrl->u.subscribe_events);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_CARD_INFO:
Packit 4a16fb
		ctrl->result = snd_ctl_card_info(ctl, &ctrl->u.card_info);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_ELEM_LIST:
Packit 4a16fb
	{
Packit 4a16fb
		size_t maxsize = CTL_SHM_DATA_MAXLEN;
Packit 4a16fb
		if (ctrl->u.element_list.space * sizeof(*ctrl->u.element_list.pids) > maxsize) {
Packit 4a16fb
			ctrl->result = -EFAULT;
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
		ctrl->u.element_list.pids = (snd_ctl_elem_id_t*) ctrl->data;
Packit 4a16fb
		ctrl->result = snd_ctl_elem_list(ctl, &ctrl->u.element_list);
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
	case SNDRV_CTL_IOCTL_ELEM_INFO:
Packit 4a16fb
		ctrl->result = snd_ctl_elem_info(ctl, &ctrl->u.element_info);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_ELEM_READ:
Packit 4a16fb
		ctrl->result = snd_ctl_elem_read(ctl, &ctrl->u.element_read);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_ELEM_WRITE:
Packit 4a16fb
		ctrl->result = snd_ctl_elem_write(ctl, &ctrl->u.element_write);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_ELEM_LOCK:
Packit 4a16fb
		ctrl->result = snd_ctl_elem_lock(ctl, &ctrl->u.element_lock);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
Packit 4a16fb
		ctrl->result = snd_ctl_elem_unlock(ctl, &ctrl->u.element_unlock);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
Packit 4a16fb
		ctrl->result = snd_ctl_hwdep_next_device(ctl, &ctrl->u.device);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_HWDEP_INFO:
Packit 4a16fb
		ctrl->result = snd_ctl_hwdep_info(ctl, &ctrl->u.hwdep_info);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
Packit 4a16fb
		ctrl->result = snd_ctl_pcm_next_device(ctl, &ctrl->u.device);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_PCM_INFO:
Packit 4a16fb
		ctrl->result = snd_ctl_pcm_info(ctl, &ctrl->u.pcm_info);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
Packit 4a16fb
		ctrl->result = snd_ctl_pcm_prefer_subdevice(ctl, ctrl->u.pcm_prefer_subdevice);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
Packit 4a16fb
		ctrl->result = snd_ctl_rawmidi_next_device(ctl, &ctrl->u.device);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
Packit 4a16fb
		ctrl->result = snd_ctl_rawmidi_info(ctl, &ctrl->u.rawmidi_info);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
Packit 4a16fb
		ctrl->result = snd_ctl_rawmidi_prefer_subdevice(ctl, ctrl->u.rawmidi_prefer_subdevice);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_POWER:
Packit 4a16fb
		ctrl->result = snd_ctl_set_power_state(ctl, ctrl->u.power_state);
Packit 4a16fb
		break;
Packit 4a16fb
	case SNDRV_CTL_IOCTL_POWER_STATE:
Packit 4a16fb
		ctrl->result = snd_ctl_get_power_state(ctl, &ctrl->u.power_state);
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_CTL_IOCTL_READ:
Packit 4a16fb
		ctrl->result = snd_ctl_read(ctl, &ctrl->u.read);
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_CTL_IOCTL_CLOSE:
Packit 4a16fb
		client->ops->close(client);
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_CTL_IOCTL_POLL_DESCRIPTOR:
Packit 4a16fb
		ctrl->result = 0;
Packit 4a16fb
		return shm_ack_fd(client, _snd_ctl_poll_descriptor(ctl));
Packit 4a16fb
	default:
Packit 4a16fb
		ERROR("Bogus cmd: %x", ctrl->cmd);
Packit 4a16fb
		ctrl->result = -ENOSYS;
Packit 4a16fb
	}
Packit 4a16fb
	return shm_ack(client);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
transport_ops_t ctl_shm_ops = {
Packit 4a16fb
	.open	= ctl_shm_open,
Packit 4a16fb
	.cmd	= ctl_shm_cmd,
Packit 4a16fb
	.close	= ctl_shm_close,
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static int snd_client_open(client_t *client)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_client_open_request_t req;
Packit 4a16fb
	snd_client_open_answer_t ans;
Packit 4a16fb
	char *name;
Packit 4a16fb
	memset(&ans, 0, sizeof(ans));
Packit 4a16fb
	err = read(client->ctrl_fd, &req, sizeof(req));
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		SYSERROR("read failed");
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
	if (err != sizeof(req)) {
Packit 4a16fb
		ans.result = -EINVAL;
Packit 4a16fb
		goto _answer;
Packit 4a16fb
	}
Packit 4a16fb
	name = alloca(req.namelen);
Packit 4a16fb
	err = read(client->ctrl_fd, name, req.namelen);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		SYSERROR("read failed");
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
	if (err != req.namelen) {
Packit 4a16fb
		ans.result = -EINVAL;
Packit 4a16fb
		goto _answer;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	switch (req.transport_type) {
Packit 4a16fb
	case SND_TRANSPORT_TYPE_SHM:
Packit 4a16fb
		if (!client->local) {
Packit 4a16fb
			ans.result = -EINVAL;
Packit 4a16fb
			goto _answer;
Packit 4a16fb
		}
Packit 4a16fb
		switch (req.dev_type) {
Packit 4a16fb
		case SND_DEV_TYPE_PCM:
Packit 4a16fb
			client->ops = &pcm_shm_ops;
Packit 4a16fb
			break;
Packit 4a16fb
		case SND_DEV_TYPE_CONTROL:
Packit 4a16fb
			client->ops = &ctl_shm_ops;
Packit 4a16fb
			break;
Packit 4a16fb
		default:
Packit 4a16fb
			ans.result = -EINVAL;
Packit 4a16fb
			goto _answer;
Packit 4a16fb
		}
Packit 4a16fb
		break;
Packit 4a16fb
	default:
Packit 4a16fb
		ans.result = -EINVAL;
Packit 4a16fb
		goto _answer;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	name[req.namelen] = '\0';
Packit 4a16fb
Packit 4a16fb
	client->transport_type = req.transport_type;
Packit 4a16fb
	strcpy(client->name, name);
Packit 4a16fb
	client->stream = req.stream;
Packit 4a16fb
	client->mode = req.mode;
Packit 4a16fb
Packit 4a16fb
	err = client->ops->open(client, &ans.cookie);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		ans.result = err;
Packit 4a16fb
	} else {
Packit 4a16fb
		client->open = 1;
Packit 4a16fb
		ans.result = 0;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
 _answer:
Packit 4a16fb
	err = write(client->ctrl_fd, &ans, sizeof(ans));
Packit 4a16fb
	if (err != sizeof(ans)) {
Packit 4a16fb
		SYSERROR("write failed");
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int client_poll_handler(waiter_t *waiter, unsigned short events ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	client_t *client = waiter->private_data;
Packit 4a16fb
	if (client->open)
Packit 4a16fb
		client->ops->close(client);
Packit 4a16fb
	close(client->poll_fd);
Packit 4a16fb
	close(client->ctrl_fd);
Packit 4a16fb
	del_waiter(client->poll_fd);
Packit 4a16fb
	del_waiter(client->ctrl_fd);
Packit 4a16fb
	list_del(&client->list);
Packit 4a16fb
	free(client);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int client_ctrl_handler(waiter_t *waiter, unsigned short events)
Packit 4a16fb
{
Packit 4a16fb
	client_t *client = waiter->private_data;
Packit 4a16fb
	if (events & POLLHUP) {
Packit 4a16fb
		if (client->open)
Packit 4a16fb
			client->ops->close(client);
Packit 4a16fb
		close(client->ctrl_fd);
Packit 4a16fb
		del_waiter(client->ctrl_fd);
Packit 4a16fb
		list_del(&client->list);
Packit 4a16fb
		free(client);
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	if (client->open)
Packit 4a16fb
		return client->ops->cmd(client);
Packit 4a16fb
	else
Packit 4a16fb
		return snd_client_open(client);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int inet_pending_handler(waiter_t *waiter, unsigned short events)
Packit 4a16fb
{
Packit 4a16fb
	inet_pending_t *pending = waiter->private_data;
Packit 4a16fb
	inet_pending_t *pdata;
Packit 4a16fb
	client_t *client;
Packit 4a16fb
	uint32_t cookie;
Packit 4a16fb
	struct list_head *item;
Packit 4a16fb
	int remove = 0;
Packit 4a16fb
	if (events & POLLHUP)
Packit 4a16fb
		remove = 1;
Packit 4a16fb
	else {
Packit 4a16fb
		int err = read(waiter->fd, &cookie, sizeof(cookie));
Packit 4a16fb
		if (err != sizeof(cookie))
Packit 4a16fb
			remove = 1;
Packit 4a16fb
		else {
Packit 4a16fb
			err = write(waiter->fd, &cookie, sizeof(cookie));
Packit 4a16fb
			if (err != sizeof(cookie))
Packit 4a16fb
				remove = 1;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	del_waiter(waiter->fd);
Packit 4a16fb
	if (remove) {
Packit 4a16fb
		close(waiter->fd);
Packit 4a16fb
		list_del(&pending->list);
Packit 4a16fb
		free(pending);
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	list_for_each(item, &inet_pendings) {
Packit 4a16fb
		pdata = list_entry(item, inet_pending_t, list);
Packit 4a16fb
		if (pdata->cookie == cookie)
Packit 4a16fb
			goto found;
Packit 4a16fb
	}
Packit 4a16fb
	pending->cookie = cookie;
Packit 4a16fb
	return 0;
Packit 4a16fb
Packit 4a16fb
 found:
Packit 4a16fb
	client = calloc(1, sizeof(*client));
Packit 4a16fb
	client->local = 0;
Packit 4a16fb
	client->poll_fd = pdata->fd;
Packit 4a16fb
	client->ctrl_fd = waiter->fd;
Packit 4a16fb
	add_waiter(client->ctrl_fd, POLLIN | POLLHUP, client_ctrl_handler, client);
Packit 4a16fb
	add_waiter(client->poll_fd, POLLHUP, client_poll_handler, client);
Packit 4a16fb
	client->open = 0;
Packit 4a16fb
	list_add_tail(&client->list, &clients);
Packit 4a16fb
	list_del(&pending->list);
Packit 4a16fb
	list_del(&pdata->list);
Packit 4a16fb
	free(pending);
Packit 4a16fb
	free(pdata);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int local_handler(waiter_t *waiter, unsigned short events ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	int sock;
Packit 4a16fb
	sock = accept(waiter->fd, 0, 0);
Packit 4a16fb
	if (sock < 0) {
Packit 4a16fb
		int result = -errno;
Packit 4a16fb
		SYSERROR("accept failed");
Packit 4a16fb
		return result;
Packit 4a16fb
	} else {
Packit 4a16fb
		client_t *client = calloc(1, sizeof(*client));
Packit 4a16fb
		client->ctrl_fd = sock;
Packit 4a16fb
		client->local = 1;
Packit 4a16fb
		client->open = 0;
Packit 4a16fb
		add_waiter(sock, POLLIN | POLLHUP, client_ctrl_handler, client);
Packit 4a16fb
		list_add_tail(&client->list, &clients);
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int inet_handler(waiter_t *waiter, unsigned short events ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	int sock;
Packit 4a16fb
	sock = accept(waiter->fd, 0, 0);
Packit 4a16fb
	if (sock < 0) {
Packit 4a16fb
		int result = -errno;
Packit 4a16fb
		SYSERROR("accept failed");
Packit 4a16fb
		return result;
Packit 4a16fb
	} else {
Packit 4a16fb
		inet_pending_t *pending = calloc(1, sizeof(*pending));
Packit 4a16fb
		pending->fd = sock;
Packit 4a16fb
		pending->cookie = 0;
Packit 4a16fb
		add_waiter(sock, POLLIN, inet_pending_handler, pending);
Packit 4a16fb
		list_add_tail(&pending->list, &inet_pendings);
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int server(const char *sockname, int port)
Packit 4a16fb
{
Packit 4a16fb
	int err, result, sockn = -1, socki = -1;
Packit 4a16fb
	unsigned int k;
Packit 4a16fb
	long open_max;
Packit 4a16fb
Packit 4a16fb
	if (!sockname && port < 0)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	open_max = sysconf(_SC_OPEN_MAX);
Packit 4a16fb
	if (open_max < 0) {
Packit 4a16fb
		result = -errno;
Packit 4a16fb
		SYSERROR("sysconf failed");
Packit 4a16fb
		return result;
Packit 4a16fb
	}
Packit 4a16fb
	pollfds = calloc((size_t) open_max, sizeof(*pollfds));
Packit 4a16fb
	waiters = calloc((size_t) open_max, sizeof(*waiters));
Packit 4a16fb
Packit 4a16fb
	if (sockname) {
Packit 4a16fb
		sockn = make_local_socket(sockname);
Packit 4a16fb
		if (sockn < 0)
Packit 4a16fb
			return sockn;
Packit 4a16fb
		if (fcntl(sockn, F_SETFL, O_NONBLOCK) < 0) {
Packit 4a16fb
			result = -errno;
Packit 4a16fb
			SYSERROR("fcntl O_NONBLOCK failed");
Packit 4a16fb
			goto _end;
Packit 4a16fb
		}
Packit 4a16fb
		if (listen(sockn, 4) < 0) {
Packit 4a16fb
			result = -errno;
Packit 4a16fb
			SYSERROR("listen failed");
Packit 4a16fb
			goto _end;
Packit 4a16fb
		}
Packit 4a16fb
		add_waiter(sockn, POLLIN, local_handler, NULL);
Packit 4a16fb
	}
Packit 4a16fb
	if (port >= 0) {
Packit 4a16fb
		socki = make_inet_socket(port);
Packit 4a16fb
		if (socki < 0)
Packit 4a16fb
			return socki;
Packit 4a16fb
		if (fcntl(socki, F_SETFL, O_NONBLOCK) < 0) {
Packit 4a16fb
			result = -errno;
Packit 4a16fb
			SYSERROR("fcntl failed");
Packit 4a16fb
			goto _end;
Packit 4a16fb
		}
Packit 4a16fb
		if (listen(socki, 4) < 0) {
Packit 4a16fb
			result = -errno;
Packit 4a16fb
			SYSERROR("listen failed");
Packit 4a16fb
			goto _end;
Packit 4a16fb
		}
Packit 4a16fb
		add_waiter(socki, POLLIN, inet_handler, NULL);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	while (1) {
Packit 4a16fb
		struct pollfd pfds[open_max];
Packit 4a16fb
		size_t pfds_count;
Packit 4a16fb
		do {
Packit 4a16fb
			err = poll(pollfds, pollfds_count, -1);
Packit 4a16fb
		} while (err == 0);
Packit 4a16fb
		if (err < 0) {
Packit 4a16fb
			SYSERROR("poll failed");
Packit 4a16fb
			continue;
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		pfds_count = pollfds_count;
Packit 4a16fb
		memcpy(pfds, pollfds, sizeof(*pfds) * pfds_count);
Packit 4a16fb
		for (k = 0; k < pfds_count; k++) {
Packit 4a16fb
			struct pollfd *pfd = &pfds[k];
Packit 4a16fb
			if (pfd->revents) {
Packit 4a16fb
				waiter_t *w = &waiters[pfd->fd];
Packit 4a16fb
				if (!w->handler)
Packit 4a16fb
					continue;
Packit 4a16fb
				err = w->handler(w, pfd->revents);
Packit 4a16fb
				if (err < 0)
Packit 4a16fb
					ERROR("waiter handler failed");
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
 _end:
Packit 4a16fb
	if (sockn >= 0)
Packit 4a16fb
		close(sockn);
Packit 4a16fb
	if (socki >= 0)
Packit 4a16fb
		close(socki);
Packit 4a16fb
	free(pollfds);
Packit 4a16fb
	free(waiters);
Packit 4a16fb
	return result;
Packit 4a16fb
}
Packit 4a16fb
					
Packit 4a16fb
Packit 4a16fb
static void usage(void)
Packit 4a16fb
{
Packit 4a16fb
	fprintf(stderr,
Packit 4a16fb
		"Usage: %s [OPTIONS] server\n"
Packit 4a16fb
		"--help			help\n",
Packit 4a16fb
		command);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int main(int argc, char **argv)
Packit 4a16fb
{
Packit 4a16fb
	static const struct option long_options[] = {
Packit 4a16fb
		{"help", 0, 0, 'h'},
Packit 4a16fb
		{ 0 , 0 , 0, 0 }
Packit 4a16fb
	};
Packit 4a16fb
	int c;
Packit 4a16fb
	snd_config_t *conf;
Packit 4a16fb
	snd_config_iterator_t i, next;
Packit 4a16fb
	const char *sockname = NULL;
Packit 4a16fb
	long port = -1;
Packit 4a16fb
	int err;
Packit 4a16fb
	char *srvname;
Packit 4a16fb
Packit 4a16fb
	command = argv[0];
Packit 4a16fb
	while ((c = getopt_long(argc, argv, "h", long_options, 0)) != -1) {
Packit 4a16fb
		switch (c) {
Packit 4a16fb
		case 'h':
Packit 4a16fb
			usage();
Packit 4a16fb
			return 0;
Packit 4a16fb
		default:
Packit 4a16fb
			fprintf(stderr, "Try `%s --help' for more information\n", command);
Packit 4a16fb
			return 1;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	if (argc - optind != 1) {
Packit 4a16fb
		ERROR("you need to specify server name");
Packit 4a16fb
		return 1;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_config_update();
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		ERROR("cannot read configuration file");
Packit 4a16fb
		return 1;
Packit 4a16fb
	}
Packit 4a16fb
	srvname = argv[optind];
Packit 4a16fb
	err = snd_config_search_definition(snd_config, "server", srvname, &conf;;
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		ERROR("Missing definition for server %s", srvname);
Packit 4a16fb
		return 1;
Packit 4a16fb
	}
Packit 4a16fb
	if (snd_config_get_type(conf) != SND_CONFIG_TYPE_COMPOUND) {
Packit 4a16fb
		SNDERR("Invalid type for server %s definition", srvname);
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	snd_config_for_each(i, next, conf) {
Packit 4a16fb
		snd_config_t *n = snd_config_iterator_entry(i);
Packit 4a16fb
		const char *id;
Packit 4a16fb
		if (snd_config_get_id(n, &id) < 0)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (strcmp(id, "comment") == 0)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (strcmp(id, "host") == 0)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (strcmp(id, "socket") == 0) {
Packit 4a16fb
			err = snd_config_get_string(n, &sockname);
Packit 4a16fb
			if (err < 0) {
Packit 4a16fb
				ERROR("Invalid type for %s", id);
Packit 4a16fb
				return 1;
Packit 4a16fb
			}
Packit 4a16fb
			continue;
Packit 4a16fb
		}
Packit 4a16fb
		if (strcmp(id, "port") == 0) {
Packit 4a16fb
			err = snd_config_get_integer(n, &port);
Packit 4a16fb
			if (err < 0) {
Packit 4a16fb
				ERROR("Invalid type for %s", id);
Packit 4a16fb
				return 1;
Packit 4a16fb
			}
Packit 4a16fb
			continue;
Packit 4a16fb
		}
Packit 4a16fb
		ERROR("Unknown field %s", id);
Packit 4a16fb
		return 1;
Packit 4a16fb
	}
Packit 4a16fb
	if (!sockname && port < 0) {
Packit 4a16fb
		ERROR("either socket or port need to be defined");
Packit 4a16fb
		return 1;
Packit 4a16fb
	}
Packit 4a16fb
	server(sockname, port);
Packit 4a16fb
	return 0;
Packit 4a16fb
}