src/server/util/io.c

Thu, 09 Aug 2012 11:08:49 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 09 Aug 2012 11:08:49 +0200
changeset 34
2b4574e617c0
parent 32
ebba53de8b18
child 41
bb7a1f5a8b48
permissions
-rw-r--r--

merge

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2011 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <unistd.h>
#include <stdlib.h>
#include <sys/uio.h>

#include <limits.h> /* asprintf */

#include "io.h"
#include "pool.h"

IOStream native_io_funcs = {
    system_write,
    system_read
};

IOStream net_io_funcs = {
    net_stream_write,
    net_stream_read
};


IOStream* stream_new_from_fd(int fd) {
    SystemIOStream *st = malloc(sizeof(SystemIOStream));
    st->st = native_io_funcs;
    st->fd = fd;
    return (IOStream*)st;
}

ssize_t system_write(IOStream *st, void *buf, size_t nbytes) {
    return write(((SystemIOStream*)st)->fd, buf, nbytes);
}

ssize_t system_read(IOStream *st, void *buf, size_t nbytes) {
    return read(((SystemIOStream*)st)->fd, buf, nbytes);
}


IOStream* net_stream_from_fd(int fd) {
    NetIOStream *st = malloc(sizeof(NetIOStream));
    st->st = net_io_funcs;
    st->fd = fd;
    st->max_read = 0;
    st->rd = 0;
    return (IOStream*)st;
}

ssize_t net_stream_write(IOStream *st, void *buf, size_t nbytes) {
    // TODO: implement
}

ssize_t net_stream_read(IOStream *st, void *buf, size_t nbytes) {
    NetIOStream *n = (NetIOStream*)st;
    if(n->max_read != 0 && n->rd >= n->max_read) {
        return 0;
    }
    ssize_t r = read(n->fd, buf, nbytes);
    n->rd += r;
    return r;
}


ssize_t net_read(SYS_NETFD fd, void *buf, size_t nbytes) {
    ssize_t r = ((IOStream*)fd)->read(fd, buf, nbytes);
    if(r == 0) {
        return IO_EOF;
    }
    return r;
}

ssize_t net_write(SYS_NETFD fd, void *buf, size_t nbytes) {
    ssize_t r = ((IOStream*)fd)->write(fd, buf, nbytes);
    if(r < 0) {
        return IO_ERROR;
    }  
    return r;
}

ssize_t net_printf(SYS_NETFD fd, char *format, ...) {
    va_list arg;
    va_start(arg, format);
    char *buf;
    ssize_t len = vasprintf(&buf, format, arg);
    net_write(fd, buf, len);
    free(buf);
    return len;
}


/* iovec buffer */
iovec_buf_t *iovec_buf_create(pool_handle_t *pool) {
    iovec_buf_t *buf = pool_malloc(pool, sizeof(iovec_buf_t));

    buf->pool = pool;
    buf->iov  = pool_calloc(pool, 32, sizeof(struct iovec));
    buf->maxiovec = 32;
    buf->iovctn = 0;

    return buf;
}

void iovec_buf_write(iovec_buf_t *io, void *buf, size_t nbyte) {
    if(io->iovctn >= io->maxiovec) {
        io->iov = pool_realloc(
                io->pool,
                io->iov,
                (io->maxiovec + 16) * sizeof(struct iovec));
    }

    io->iov[io->iovctn].iov_base = buf;
    io->iov[io->iovctn].iov_len = nbyte;
    io->iovctn++;
}

ssize_t iovec_buf_flush(iovec_buf_t *io, int fd) {
    return writev(fd, io->iov, io->iovctn);
}


/* TODO: add asprintf to new file */

/* 
 * asprintf implementation for Solaris 10 
 * source from OpenSolaris
 * file: /onnv/onnv-gate/usr/src/lib/libc/port/print/asprintf.c
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright (c) 2004 Darren Tucker.
 *
 * Based originally on asprintf.c from OpenBSD:
 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef __SunOS_5_10

#define	INIT_SZ	128

/* VARARGS2 */
int
vasprintf(char **str, const char *format, va_list ap)
{
	char string[INIT_SZ];
	char *newstr;
	int ret;
	size_t len;

	*str = NULL;
	ret = vsnprintf(string, INIT_SZ, format, ap);
	if (ret < 0)	/* retain the value of errno from vsnprintf() */
		return (-1);
	if (ret < INIT_SZ) {
		len = ret + 1;
		if ((newstr = malloc(len)) == NULL)
			return (-1);	/* retain errno from malloc() */
		(void) strlcpy(newstr, string, len);
		*str = newstr;
		return (ret);
	}
	/*
	 * We will perform this loop more than once only if some other
	 * thread modifies one of the vasprintf() arguments after our
	 * previous call to vsnprintf().
	 */
	for (;;) {
		if (ret == INT_MAX) {	/* Bad length */
			errno = ENOMEM;
			return (-1);
		}
		len = ret + 1;
		if ((newstr = malloc(len)) == NULL)
			return (-1);	/* retain errno from malloc() */
		ret = vsnprintf(newstr, len, format, ap);
		if (ret < 0) {		/* retain errno from vsnprintf() */
			free(newstr);
			return (-1);
		}
		if (ret < len) {
			*str = newstr;
			return (ret);
		}
		free(newstr);
	}
}

int
asprintf(char **str, const char *format, ...)
{
	va_list ap;
	int ret;

	*str = NULL;
	va_start(ap, format);
	ret = vasprintf(str, format, ap);
	va_end(ap);

	return (ret);
}

#endif

mercurial