.\" This manpage is Copyright (C) 2006 Jens Axboe .\" and Copyright (C) 2006 Michael Kerrisk .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2007 Akihiro MOTOKI .\" all rights reserved. .\" Translated 2007-02-04, Akihiro MOTOKI .\" Updated 2008-11-10, Akihiro MOTOKI , LDP v3.13 .\" .TH SPLICE 2 2020\-06\-09 Linux "Linux Programmer's Manual" .SH 名前 splice \- パイプとの間でデータを継ぎ合わせる .SH 書式 .nf \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */ \fB#include \fP .PP .\" Return type was long before glibc 2.7 \fBssize_t splice(int \fP\fIfd_in\fP\fB, loff_t *\fP\fIoff_in\fP\fB, int \fP\fIfd_out\fP\fB,\fP \fB loff_t *\fP\fIoff_out\fP\fB, size_t \fP\fIlen\fP\fB, unsigned int \fP\fIflags\fP\fB);\fP .fi .SH 説明 \fBsplice\fP() は、カーネルアドレス空間とユーザーアドレス空間との間のコピーを伴わずに、 2 つのファイルディスクリプター間でデータの移動を行う。 ファイルディスクリプター \fIfd_in\fP からファイルディスクリプター \fIfd_out\fP へ最大 \fIlen\fP バイトを転送する。 2 つのファイルディスクリプターのうち一つは パイプを参照していなければならない。 .PP \fIfd_in\fP と \fIoff_in\fP には以下のルールが適用される。 .IP * 3 \fIfd_in\fP がパイプを参照している場合、 \fIoff_in\fP は NULL でなければならない。 .IP * \fIfd_in\fP がパイプを参照しておらず、かつ \fIoff_in\fP が NULL の場合、 バイトは \fIfd_in\fP のファイルオフセットから読み出され、 ファイルオフセットは適切に調整される。 .IP * \fIfd_in\fP がパイプを参照しておらず、 \fIoff_in\fP が NULL でない場合、 \fIoff_in\fP は \fIfd_in\fP からのデータ読み出しを開始する先頭オフセットを格納したバッファー へのポインターでなければならない。この場合、 \fIfd_in\fP のファイルオフセットは変更されない。 .PP \fIfd_out\fP と \fIoff_out\fP に関しても同様である。 .PP \fIflags\fP 引数には、以下の値の 0 個以上をビット毎の論理和の形で指定する。 .TP \fBSPLICE_F_MOVE\fP ページのコピーでなく移動を試みる。 これはカーネルに対するヒントでしかない。 つまり、カーネルがパイプからページを移動できない場合や、 パイプバッファーがページ全部を参照していない場合は、 ページのコピーが行われることもある。 このフラグの最初の実装にはバグがあった。そのため、 Linux 2.6.21 以降ではこのフラグの操作はできないようになっている (ただし、 \fBsplice\fP() コールでこのフラグを指定することは今も認められている)。 将来、正しい実装が行われることだろう。 .TP \fBSPLICE_F_NONBLOCK\fP 入出力時に停止 (block) しない。 このフラグを指定すると、 splice によるパイプ操作を非停止モード (nonblocking) で 行おうとするが、その場合でも \fBsplice\fP() は停止することもある。なぜなら、データのやり取りを行う ファイルディスクリプターは (\fBO_NONBLOCK\fP フラグをセットされていない場合) 停止する可能性があるからである。 .TP \fBSPLICE_F_MORE\fP この後の splice でさらに転送されるデータがあることを示す。 このフラグは \fIfd_out\fP がソケットを参照している場合に有用なヒントとなる (\fBsend\fP(2) の \fBMSG_MORE\fP や \fBtcp\fP(7) の \fBTCP_CORK\fP の説明も参照)。 .TP \fBSPLICE_F_GIFT\fP \fBsplice\fP() では使用しない。 \fBvmsplice\fP(2) 参照。 .SH 返り値 Upon successful completion, \fBsplice\fP() returns the number of bytes spliced to or from the pipe. .PP A return value of 0 means end of input. If \fIfd_in\fP refers to a pipe, then this means that there was no data to transfer, and it would not make sense to block because there are no writers connected to the write end of the pipe. .PP エラーの場合、 \fBsplice\fP() は \-1 を返し、 \fIerrno\fP にエラーを示す値を設定する。 .SH エラー .TP \fBEAGAIN\fP \fBSPLICE_F_NONBLOCK\fP was specified in \fIflags\fP or one of the file descriptors had been marked as nonblocking (\fBO_NONBLOCK\fP)\fB,\fP and the operation would block. .TP \fBEBADF\fP ファイルディスクリプターの一方または両方が有効ではない、 もしくは適切な read\-write モードではない。 .TP \fBEINVAL\fP The target filesystem doesn't support splicing. .TP \fBEINVAL\fP .\" The append-mode error is given since 2.6.27; in earlier kernels, .\" splice() in append mode was broken The target file is opened in append mode. .TP \fBEINVAL\fP どのファイルディスクリプターもパイプを参照していない。 .TP \fBEINVAL\fP An offset was given for nonseekable device (e.g., a pipe). .TP \fBEINVAL\fP \fIfd_in\fP と \fIfd_out\fP が同じパイプを参照している。 .TP \fBENOMEM\fP メモリー不足。 .TP \fBESPIPE\fP \fIoff_in\fP か \fIoff_out\fP のいずれかが NULL ではないが、対応するファイルディスクリプターが パイプを参照している。 .SH バージョン \fBsplice\fP() システムコールは Linux 2.6.17 で初めて登場した。 ライブラリによるサポートは glibc バージョン 2.5 で追加された。 .SH 準拠 このシステムコールは Linux 固有である。 .SH 注意 3 つのシステムコール (\fBsplice\fP(), \fBvmsplice\fP(2), \fBtee\fP(2)) を使うと、ユーザー空間プログラムは任意のカーネルバッファーに対する 完全な制御ができる。カーネルバッファーは、パイプに使用されているのと 同種のバッファーを使ってカーネル内に実装されている。 大まかにいうと、これらのシステムコールは以下の仕事を行う: .IP \(bu 2 \fBsplice\fP() は、バッファーから任意のファイルディスクリプターや、その逆方向、 もしくはあるバッファーから別のバッファーへの、データ移動を行う。 .IP \(bu \fBtee\fP(2) は、あるバッファーから別のバッファーへのデータ「コピー」を行う。 .IP \(bu \fBvmsplice\fP(2) は、ユーザー空間からバッファーへのデータ「コピー」を行う。 .PP .\" .\" Linus: Now, imagine using the above in a media server, for example. .\" Let's say that a year or two has passed, so that the video drivers .\" have been updated to be able to do the splice thing, and what can .\" you do? You can: .\" .\" - splice from the (mpeg or whatever - let's just assume that the video .\" input is either digital or does the encoding on its own - like they .\" pretty much all do) video input into a pipe (remember: no copies - the .\" video input will just DMA directly into memory, and splice will just .\" set up the pages in the pipe buffer) .\" - tee that pipe to split it up .\" - splice one end to a file (ie "save the compressed stream to disk") .\" - splice the other end to a real-time video decoder window for your .\" real-time viewing pleasure. .\" .\" Linus: Now, the advantage of splice()/tee() is that you can .\" do zero-copy movement of data, and unlike sendfile() you can .\" do it on _arbitrary_ data (and, as shown by "tee()", it's more .\" than just sending the data to somebody else: you can duplicate .\" the data and choose to forward it to two or more different .\" users - for things like logging etc.). .\" ここではコピーの話をしているが、実際のコピーは一般的に回避される。 カーネルは、パイプバッファーをカーネルメモリーのページへのポインター集合として 実装し、ページへの参照回数を管理することで、これを実現している。 カーネルは、対象となるページを参照する (出力バッファー用の) ポインターを 新規に作成することでバッファー内のページの「コピー」を作成し、 そのページの参照回数を増やす。つまり、ポインターだけがコピーされ、 バッファーのページはコピーされない。 .PP .\" commit 7c77f0b3f9208c339a4b40737bb2cb0f0319bb8d In Linux 2.6.30 and earlier, exactly one of \fIfd_in\fP and \fIfd_out\fP was required to be a pipe. Since Linux 2.6.31, both arguments may refer to pipes. .SH 例 \fBtee\fP(2) 参照。 .SH 関連項目 \fBcopy_file_range\fP(2), \fBsendfile\fP(2), \fBtee\fP(2), \fBvmsplice\fP(2), \fBpipe\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。