public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@suse.de>
To: linux-kernel@vger.kernel.org
Cc: Linus Torvalds <torvalds@osdl.org>
Subject: Re: [PATCH][RFC] splice support
Date: Wed, 29 Mar 2006 14:30:22 +0200	[thread overview]
Message-ID: <20060329123022.GD8186@suse.de> (raw)
In-Reply-To: <20060329122841.GC8186@suse.de>

[-- Attachment #1: Type: text/plain, Size: 533 bytes --]

On Wed, Mar 29 2006, Jens Axboe wrote:
> splice-in file
>         Splice file to stdout
> 
> splice-out
>         Splice stdin to file
> 
> splice-net hostname port
>         Splice stdin to hostname:port
> 
> Examples - splice copying a file can be done as:
> 
> # splice-in file | splice-out new_file
> 
> Sending a file over the network
> 
> # cat file | splice-net hostname port
> 
> and then have the other end run eg netcat -l -p port to receive the
> data.

Which I naturally forgot to attach, here they are.

-- 
Jens Axboe


[-- Attachment #2: splice-in.c --]
[-- Type: text/x-c++src, Size: 986 bytes --]

/*
 * Splice argument file to stdout
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#if defined(__i386__)
#define __NR_splice	313
#elif defined(__x86_64__)
#define __NR_splice	275
#elif defined(__powerpc__) || defined(__powerpc64__)
#define __NR_splice	283
#else
#error unsupported arch
#endif

static inline int splice(int fdin, int fdout, size_t len, unsigned long flags)
{
	return syscall(__NR_splice, fdin, fdout, len, flags);
}

int main(int argc, char *argv[])
{
	struct stat sb;
	int fd;

	if (argc < 2) {
		printf("%s: infile\n", argv[0]);
		return 1;
	}

	fd = open(argv[1], O_RDONLY);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	if (fstat(fd, &sb) < 0) {
		perror("stat");
		return 1;
	}

	do {
		int ret = splice(fd, STDOUT_FILENO, sb.st_size, 0);

		if (ret < 0) {
			perror("splice");
			break;
		} else if (!ret)
			break;

		sb.st_size -= ret;
	} while (1);

	close(fd);
	return 0;
}

[-- Attachment #3: splice-out.c --]
[-- Type: text/x-c++src, Size: 903 bytes --]

/*
 * Splice stdout to file
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#define SPLICE_SIZE	(64*1024)

#if defined(__i386__)
#define __NR_splice	313
#elif defined(__x86_64__)
#define __NR_splice	275
#elif defined(__powerpc__) || defined(__powerpc64__)
#define __NR_splice	283
#else
#error unsupported arch
#endif

static inline int splice(int fdin, int fdout, size_t len, unsigned long flags)
{
	return syscall(__NR_splice, fdin, fdout, len, flags);
}

int main(int argc, char *argv[])
{
	int fd;

	if (argc < 2) {
		printf("%s: outfile\n", argv[0]);
		return 1;
	}

	fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	do {
		int ret = splice(STDIN_FILENO, fd, SPLICE_SIZE, 0);

		if (ret < 0) {
			perror("splice");
			break;
		} else if (ret < SPLICE_SIZE)
			break;
	} while (1);

	close(fd);
	return 0;
}

[-- Attachment #4: splice-net.c --]
[-- Type: text/x-c++src, Size: 1471 bytes --]

/*
 * Splice stdin to net
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#define SPLICE_SIZE	(64*1024)

#if defined(__i386__)
#define __NR_splice	313
#elif defined(__x86_64__)
#define __NR_splice	275
#elif defined(__powerpc__) || defined(__powerpc64__)
#define __NR_splice	283
#else
#error unsupported arch
#endif

static inline int splice(int fdin, int fdout, size_t len, unsigned long flags)
{
	return syscall(__NR_splice, fdin, fdout, len, flags);
}

int main(int argc, char *argv[])
{
	struct sockaddr_in addr;
	unsigned short port;
	int fd;

	if (argc < 3) {
		printf("%s: target port\n", argv[0]);
		return 1;
	}

	port = atoi(argv[2]);

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);

	if (inet_aton(argv[1], &addr.sin_addr) != 1) {
		struct hostent *hent = gethostbyname(argv[1]);

		if (!hent) {
			perror("gethostbyname");
			return 1;
		}

		memcpy(&addr.sin_addr, hent->h_addr, 4);
	}

	printf("Connecting to %s/%d\n", argv[1], port);

	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd < 0) {
		perror("socket");
		return 1;
	}

	if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		perror("connect");
		return 1;
	}

	do {
		int ret = splice(STDIN_FILENO, fd, SPLICE_SIZE, 0);

		if (ret < 0) {
			perror("splice");
			break;
		} else if (!ret) {
			break;
		}
	} while (1);

	close(fd);
	return 0;
}

  reply	other threads:[~2006-03-29 12:30 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-29 12:28 [PATCH][RFC] splice support Jens Axboe
2006-03-29 12:30 ` Jens Axboe [this message]
2006-03-29 13:15 ` Jeff Garzik
2006-03-29 13:27   ` Jens Axboe
2006-03-29 21:49     ` Nathan Scott
2006-03-29 20:06   ` Linus Torvalds
2006-03-29 20:42     ` Jens Axboe
2006-03-29 20:43       ` Jens Axboe
2006-03-29 21:14         ` Linus Torvalds
2006-03-30  6:17           ` Jens Axboe
2006-03-29 22:37 ` Andrew Morton
2006-03-30  0:50   ` Linus Torvalds
2006-03-30  1:04     ` Jeff Garzik
2006-03-30  1:20       ` Andrew Morton
2006-03-30  6:18         ` Jens Axboe
2006-03-30  2:08     ` Andrew Morton
2006-03-30  3:44       ` Nick Piggin
2006-03-30  7:21       ` Jens Axboe
2006-03-30  7:30         ` Andrew Morton
2006-03-30  7:33           ` Jens Axboe
2006-03-30  8:02             ` Jan Engelhardt
2006-03-30  3:10     ` Nick Piggin
2006-03-30  7:16     ` Jens Axboe
2006-03-30  8:09     ` Jan Engelhardt
2006-03-30  7:45   ` Jens Axboe
2006-03-30  8:02     ` Andrew Morton
2006-03-30  8:10       ` Jens Axboe
2006-03-30  8:25         ` Nick Piggin
2006-03-30  8:27         ` Andrew Morton
2006-03-30  8:50           ` Nick Piggin
2006-03-30  8:51           ` Jens Axboe
2006-03-30  9:15             ` Jens Axboe
2006-03-30  9:40               ` Andrew Morton
2006-03-30  9:45                 ` Jens Axboe
2006-03-30  9:56                   ` Andrew Morton
2006-03-30 10:01                     ` Jens Axboe
2006-03-30  2:36 ` Nick Piggin
2006-03-30  7:00   ` Jens Axboe
2006-03-30  7:33     ` Nick Piggin
2006-03-30  8:54 ` KAMEZAWA Hiroyuki
2006-03-30 13:53   ` Jens Axboe
2006-03-30 14:05     ` KAMEZAWA Hiroyuki
2006-03-30 14:38       ` Jens Axboe
2006-03-30 14:55         ` KAMEZAWA Hiroyuki
  -- strict thread matches above, loose matches on Subject: below --
2005-12-19  9:16 Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060329123022.GD8186@suse.de \
    --to=axboe@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox