pipe


Rev.2を表示中。最新版はこちら

パイプシステムコールはpipe2/pipeで、pipeはflag=0でpipe2をコールし、そこからdo_pipe_flags()をコールしています。
SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
{
       int fd[2];
       int error;

       error = do_pipe_flags(fd, flags);
       if (!error) {
               if (copy_to_user(fildes, fd, sizeof(fd))) {
                       sys_close(fd[0]);
                       sys_close(fd[1]);
                       error = -EFAULT;
               }
       }
       return error;
}

SYSCALL_DEFINE1(pipe, int __user *, fildes)
{
       return sys_pipe2(fildes, 0);
}
do_pipe_flags()は、読込み/書込みの2つのstruct fileを作成し、カレントプロセスの空いているファイルインデックに、このstruct fileを設定し、ユーザ引数のint *fdにこの2つのファイルインデックを返します。ユーザプロセスは、この2つのファイルディスクリプタを介すことでデータのやり取りが行えます。
int do_pipe_flags(int *fd, int flags)
{
       struct file *fw, *fr;
       int error;
       int fdw, fdr;

       if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
               return -EINVAL;

       fw = create_write_pipe(flags);
       if (IS_ERR(fw))
               return PTR_ERR(fw);
       fr = create_read_pipe(fw, flags);
       error = PTR_ERR(fr);
       if (IS_ERR(fr))
               goto err_write_pipe;

       error = get_unused_fd_flags(flags);
       if (error < 0)
               goto err_read_pipe;
       fdr = error;

       error = get_unused_fd_flags(flags);
       if (error < 0)
               goto err_fdr;
       fdw = error;

       audit_fd_pair(fdr, fdw);
       fd_install(fdr, fr);
       fd_install(fdw, fw);
       fd[0] = fdr;
       fd[1] = fdw;

       return 0;

err_fdr:
       put_unused_fd(fdr);
err_read_pipe:
       path_put(&fr->f_path);
       put_filp(fr);
err_write_pipe:
       free_write_pipe(fw);
       return error;
}
create_write_pipe()は書き込み用のstruct fileを作成します。 get_pipe_inode()/d_alloc_pseudo()でpipeのinode/dentryを取得します。通常inode(ファイル)からstruct fileを取得するわけで、従ってinodeにはかかるコールバック等かかる情報がが設定されなければなりません。しかしpipeはstruct fileからinodeとなるわけで、inodeのかかる設定等は不要です。inode番号はCPU変数のlast_inoをインクリメントした値となります。dentryについても同じで、pipefs下にディレクトリ等作成する必要も無ありません。struct qstr name = { .name = "" }とあるように名前すらありません。これは、vfs下のインターフェースとする事にあります。
struct file *create_write_pipe(int flags)
{
       int err;
       struct inode *inode;
       struct file *f;
       struct path path;
       struct qstr name = { .name = "" };

       err = -ENFILE;
       inode = get_pipe_inode();
       if (!inode)
               goto err;

       err = -ENOMEM;
       path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
       if (!path.dentry)
               goto err_inode;
       path.mnt = mntget(pipe_mnt);

       d_instantiate(path.dentry, inode);

       err = -ENFILE;
       f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
       if (!f)
               goto err_dentry;
       f->f_mapping = inode->i_mapping;

       f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
       f->f_version = 0;

       return f;

err_dentry:
       free_pipe_info(inode);
       path_put(&path);
       return ERR_PTR(err);

err_inode:
       free_pipe_info(inode);
       iput(inode);
err:
       return ERR_PTR(err);
}
get_pipe_inode()でpipe用のinodeを取得します。alloc_pipe_info()でstruct pipe_inode_infoを取得する事です。それをinode->i_pipeに設定します。struct pipe_inode_infoには、pipeの読み書きのバッファー(別の見方をすれば実デバイス)が確保され、pipeのinodeの読み書きは、このバッファから取得することになります。
static struct inode * get_pipe_inode(void)
{
       struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
       struct pipe_inode_info *pipe;

       if (!inode)
               goto fail_inode;

       inode->i_ino = get_next_ino();

       pipe = alloc_pipe_info(inode);
       if (!pipe)
               goto fail_iput;
       inode->i_pipe = pipe;

       pipe->readers = pipe->writers = 1;
       inode->i_fop = &rdwr_pipefifo_fops;

       inode->i_state = I_DIRTY;
       inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
       inode->i_uid = current_fsuid();
       inode->i_gid = current_fsgid();
       inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;

       return inode;

fail_iput:
       iput(inode);

fail_inode:
       return NULL;
}
create_read_pipe()は、create_write_pipe()で作成したstruct file *wrfを引数として、alloc_file()でstruct fileを取得する時、struct file *wrfで初期化します。 inode->i_pipeは同じとなり、読み込みstruct fileは、書き込みのstruct fileと同じバッファから読み込む事になります。
struct file *create_read_pipe(struct file *wrf, int flags)
{
       struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
                                   &read_pipefifo_fops);
       if (!f)
               return ERR_PTR(-ENFILE);

       path_get(&wrf->f_path);
       f->f_flags = O_RDONLY | (flags & O_NONBLOCK);

       return f;
}
pipe名はpipefs_dentry_operationsの.d_dnameコールバック関数をコールし、pipe:inode番号となっています。
static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
{
       return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
                               dentry->d_inode->i_ino);
}

static const struct dentry_operations pipefs_dentry_operations = {
       .d_dname        = pipefs_dname,
};

最終更新 2013/07/18 18:51:34 - north
(2013/07/18 18:46:41 作成)


検索

アクセス数
3575919
最近のコメント
コアダンプファイル - sakaia
list_head構造体 - yocto_no_yomikata
勧告ロックと強制ロック - wataash
LKMからのファイル出力 - 重松 宏昌
kprobe - ななし
ksetの実装 - スーパーコピー
カーネルスレッドとは - ノース
カーネルスレッドとは - nbyst
asmlinkageってなに? - ノース
asmlinkageってなに? - よろしく
Adsense
広告情報が設定されていません。