OSDN Git Service

長音記号の修正を release に反映
[linuxjm/LDP_man-pages.git] / release / man2 / mprotect.2
1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 1995 Michael Shields <shields@tembel.org>.
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and author of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified 1997-05-31 by Andries Brouwer <aeb@cwi.nl>
28 .\" Modified 2003-08-24 by Andries Brouwer <aeb@cwi.nl>
29 .\" Modified 2004-08-16 by Andi Kleen <ak@muc.de>
30 .\" 2007-06-02, mtk: Fairly substantial rewrites and additions, and
31 .\" a much improved example program.
32 .\"
33 .\" FIXME The following protection flags need documenting:
34 .\"         PROT_SEM
35 .\"         PROT_GROWSDOWN
36 .\"         PROT_GROWSUP
37 .\"         PROT_SAO (PowerPC)
38 .\"
39 .\"*******************************************************************
40 .\"
41 .\" This file was generated with po4a. Translate the source file.
42 .\"
43 .\"*******************************************************************
44 .\"
45 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
46 .\"         all rights reserved.
47 .\" Translated 1997-06-01, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
48 .\" Updated and revised 2003-10-24, Suzuki Takashi.
49 .\" Updated & Modified 2005-02-08, Yuichi SATO <ysato444@yahoo.co.jp>
50 .\" Updated & Modified 2005-10-15, Yuichi SATO
51 .\" Updated 2007-06-11, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.54
52 .\"
53 .TH MPROTECT 2 2014\-01\-05 Linux "Linux Programmer's Manual"
54 .SH 名前
55 mprotect \- メモリー領域の保護を設定する
56 .SH 書式
57 .nf
58 \fB#include <sys/mman.h>\fP
59 .sp
60 \fBint mprotect(void *\fP\fIaddr\fP\fB, size_t \fP\fIlen\fP\fB, int \fP\fIprot\fP\fB);\fP
61 .fi
62 .SH 説明
63 \fBmprotect\fP()  は、区間 [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1] のアドレス範囲を含む
64 呼び出し元のプロセスのメモリーページのアクセス保護を変更する。 \fIaddr\fP はページ境界に一致していなければならない。
65
66 呼び出し元のプロセスがアクセス保護に違反するようなメモリーアクセスを 行おうとすると、カーネルはシグナル \fBSIGSEGV\fP
67 をそのプロセスに対して生成する。
68 .PP
69 \fIprot\fP には、 \fBPROT_NONE\fP か、以下のリストの \fBPROT_NONE\fP 以外の値をビット毎の論理和 (bitwize\-or)
70 で指定する:
71 .TP  1.1i
72 \fBPROT_NONE\fP
73 そのメモリーには全くアクセスできない。
74 .TP 
75 \fBPROT_READ\fP
76 そのメモリーを読み取ることができる。
77 .TP 
78 \fBPROT_WRITE\fP
79 そのメモリーを変更できる。
80 .TP 
81 \fBPROT_EXEC\fP
82 そのメモリーは実行可能である。
83 .SH 返り値
84 成功した場合、 \fBmprotect\fP()  は 0 を返す。エラーの場合は \-1 が返り、 \fIerrno\fP が適切に設定される。
85 .SH エラー
86 .TP 
87 \fBEACCES\fP
88 指定されたアクセスをメモリーに設定することができない。 これは、例えば ファイルを読み取り専用で \fBmmap\fP(2)  しており、その領域に対して
89 \fBmprotect\fP()  を呼び出して \fBPROT_WRITE\fP に設定しようとした場合に発生する。
90 .TP 
91 \fBEINVAL\fP
92 .\" Or: both PROT_GROWSUP and PROT_GROWSDOWN were specified in 'prot'.
93 \fIaddr\fP が有効なポインターでないか、 システムのページサイズの倍数でない。
94 .TP 
95 \fBENOMEM\fP
96 カーネル内部の構造体を割り当てることができなかった。
97 .TP 
98 \fBENOMEM\fP
99 [\fIaddr\fP, \fIaddr\fP+\fIlen\fP\-1] という範囲のアドレスがプロセスのアドレス空間として不正であるか、
100 その範囲のアドレスがマップされていない 1 つ以上のページを指している (カーネル 2.4.19 より前では、この状況でエラー \fBEFAULT\fP
101 が間違って生成されていた)。
102 .SH 準拠
103 .\" SVr4 defines an additional error
104 .\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's.
105 SVr4, POSIX.1\-2001.  POSIX では、 \fBmmap\fP(2)  経由で獲得していないメモリー領域に対して
106 \fBmprotect\fP()  を行った場合の \fBmprotect\fP()  の動作は未定義であるとされている。
107 .SH 注意
108 Linux では、(カーネル vsyscall 領域以外の)  任意のプロセスアドレス空間に対して \fBmprotect\fP()
109 を呼び出すことが、常に許されている。 これは特に既存のコードマッピングを書き込み可能にするために使われる。
110
111 \fBPROT_EXEC\fP が \fBPROT_READ\fP と異なる影響を持つか否かは、アーキテクチャーとカーネルのバージョンに依存する。 (i386
112 などの) いくつかのアーキテクチャーでは、 \fBPROT_WRITE\fP をセットすると、暗黙のうちに \fBPROT_READ\fP がセットされる。
113
114 POSIX.1\-2001 では、 \fIprot\fP で指定されていないアクセスを許可する実装を認めている。 ただし、最低限、 \fBPROT_WRITE\fP
115 がセットされている場合にのみ書き込みアクセスが許可され、 \fBPROT_NONE\fP がセットされている場合にはアクセスは許可されない点だけは
116 満たす必要がある。
117 .SH 例
118 .\" sigaction.2 refers to this example
119 .PP
120 以下のプログラムは、メモリーページを 4つ確保し、そのうち 3番目のページを 読み込み専用に設定する。その後で、確保した領域のアドレスの小さい方から
121 大きな方に向かって順番にバイト値を変更するループを実行する。
122
123 プログラムを実行した場合の一例を以下に示す。
124
125 .in +4n
126 .nf
127 $\fB ./a.out\fP
128 Start of region:        0x804c000
129 Got SIGSEGV at address: 0x804e000
130 .fi
131 .in
132 .SS プログラムのソース
133 \&
134 .nf
135 #include <unistd.h>
136 #include <signal.h>
137 #include <stdio.h>
138 #include <malloc.h>
139 #include <stdlib.h>
140 #include <errno.h>
141 #include <sys/mman.h>
142
143 #define handle_error(msg) \e
144     do { perror(msg); exit(EXIT_FAILURE); } while (0)
145
146 static char *buffer;
147
148 static void
149 handler(int sig, siginfo_t *si, void *unused)
150 {
151     printf("Got SIGSEGV at address: 0x%lx\en",
152             (long) si\->si_addr);
153     exit(EXIT_FAILURE);
154 }
155
156 int
157 main(void)
158 {
159     char *p;
160     int pagesize;
161     struct sigaction sa;
162
163     sa.sa_flags = SA_SIGINFO;
164     sigemptyset(&sa.sa_mask);
165     sa.sa_sigaction = handler;
166     if (sigaction(SIGSEGV, &sa, NULL) == \-1)
167         handle_error("sigaction");
168
169     pagesize = sysconf(_SC_PAGE_SIZE);
170     if (pagesize == \-1)
171         handle_error("sysconf");
172
173     /* Allocate a buffer aligned on a page boundary;
174        initial protection is PROT_READ | PROT_WRITE */
175
176     buffer = memalign(pagesize, 4 * pagesize);
177     if (buffer == NULL)
178         handle_error("memalign");
179
180     printf("Start of region:        0x%lx\en", (long) buffer);
181
182     if (mprotect(buffer + pagesize * 2, pagesize,
183                 PROT_READ) == \-1)
184         handle_error("mprotect");
185
186     for (p = buffer ; ; )
187         *(p++) = \(aqa\(aq;
188
189     printf("Loop completed\en");     /* Should never happen */
190     exit(EXIT_SUCCESS);
191 }
192 .fi
193 .SH 関連項目
194 \fBmmap\fP(2), \fBsysconf\fP(3)
195 .SH この文書について
196 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.78 の一部
197 である。プロジェクトの説明とバグ報告に関する情報は
198 http://www.kernel.org/doc/man\-pages/ に書かれている。