OSDN Git Service

(split) LDP: Release pages for LDP v3.39.
[linuxjm/LDP_man-pages.git] / release / man2 / mprotect.2
1 .\" -*- nroff -*-
2 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" and Copyright (C) 1995 Michael Shields <shields@tembel.org>.
4 .\"
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 .\"
25 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
26 .\" Modified 1997-05-31 by Andries Brouwer <aeb@cwi.nl>
27 .\" Modified 2003-08-24 by Andries Brouwer <aeb@cwi.nl>
28 .\" Modified 2004-08-16 by Andi Kleen <ak@muc.de>
29 .\" 2007-06-02, mtk: Fairly substantial rewrites and additions, and
30 .\" a much improved example program.
31 .\"
32 .\"*******************************************************************
33 .\"
34 .\" This file was generated with po4a. Translate the source file.
35 .\"
36 .\"*******************************************************************
37 .TH MPROTECT 2 2011\-09\-08 Linux "Linux Programmer's Manual"
38 .SH 名前
39 mprotect \- メモリ領域の保護を設定する
40 .SH 書式
41 .nf
42 \fB#include <sys/mman.h>\fP
43 .sp
44 \fBint mprotect(const void *\fP\fIaddr\fP\fB, size_t \fP\fIlen\fP\fB, int \fP\fIprot\fP\fB);\fP
45 .fi
46 .SH 説明
47 \fBmprotect\fP()  は、区間 [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1] のアドレス範囲を含む
48 呼び出し元のプロセスのメモリページのアクセス保護を変更する。 \fIaddr\fP はページ境界に一致していなければならない。
49
50 呼び出し元のプロセスがアクセス保護に違反するようなメモリアクセスを 行おうとすると、カーネルはシグナル \fBSIGSEGV\fP
51 をそのプロセスに対して生成する。
52 .PP
53 \fIprot\fP には、 \fBPROT_NONE\fP か、以下のリストの \fBPROT_NONE\fP 以外の値をビット毎の論理和 (bitwize\-or)
54 で指定する:
55 .TP  1.1i
56 \fBPROT_NONE\fP
57 そのメモリには全くアクセスできない。
58 .TP 
59 \fBPROT_READ\fP
60 そのメモリを読み取ることができる。
61 .TP 
62 \fBPROT_WRITE\fP
63 そのメモリを変更できる。
64 .TP 
65 \fBPROT_EXEC\fP
66 .\" FIXME
67 .\" Document PROT_GROWSUP and PROT_GROWSDOWN
68 そのメモリは実行可能である。
69 .SH 返り値
70 成功した場合、 \fBmprotect\fP()  は 0 を返す。エラーの場合は \-1 が返り、 \fIerrno\fP が適切に設定される。
71 .SH エラー
72 .TP 
73 \fBEACCES\fP
74 指定されたアクセスをメモリに設定することができない。 これは、例えば ファイルを読み取り専用で \fBmmap\fP(2)  しており、その領域に対して
75 \fBmprotect\fP()  を呼び出して \fBPROT_WRITE\fP に設定しようとした場合に発生する。
76 .TP 
77 \fBEINVAL\fP
78 .\" Or: both PROT_GROWSUP and PROT_GROWSDOWN were specified in 'prot'.
79 \fIaddr\fP が有効なポインタでないか、 システムのページサイズの倍数でない。
80 .TP 
81 \fBENOMEM\fP
82 カーネル内部の構造体を割り当てることができなかった。
83 .TP 
84 \fBENOMEM\fP
85 [\fIaddr\fP, \fIaddr\fP+\fIlen\fP\-1] という範囲のアドレスがプロセスのアドレス空間として不正であるか、
86 その範囲のアドレスがマップされていない 1 つ以上のページを指している (カーネル 2.4.19 より前では、この状況でエラー \fBEFAULT\fP
87 が間違って生成されていた)。
88 .SH 準拠
89 .\" SVr4 defines an additional error
90 .\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's.
91 SVr4, POSIX.1\-2001.  POSIX では、 \fBmmap\fP(2)  経由で獲得していないメモリ領域に対して \fBmprotect\fP()
92 を行った場合の \fBmprotect\fP()  の動作は未定義であるとされている。
93 .SH 注意
94 Linux では、(カーネル vsyscall 領域以外の)  任意のプロセスアドレス空間に対して \fBmprotect\fP()
95 を呼び出すことが、常に許されている。 これは特に既存のコードマッピングを書き込み可能にするために使われる。
96
97 \fBPROT_EXEC\fP が \fBPROT_READ\fP と異なる影響を持つか否かは、アーキテクチャとカーネルのバージョンに依存する。 (i386
98 などの) いくつかのアーキテクチャでは、 \fBPROT_WRITE\fP をセットすると、暗黙のうちに \fBPROT_READ\fP がセットされる。
99
100 POSIX.1\-2001 では、 \fIprot\fP で指定されていないアクセスを許可する実装を認めている。 ただし、最低限、 \fBPROT_WRITE\fP
101 がセットされている場合にのみ書き込みアクセスが許可され、 \fBPROT_NONE\fP がセットされている場合にはアクセスは許可されない点だけは
102 満たす必要がある。
103 .SH 例
104 .\" sigaction.2 refers to this example
105 .PP
106 以下のプログラムは、メモリページを 4つ確保し、そのうち 3番目のページを 読み込み専用に設定する。その後で、確保した領域のアドレスの小さい方から
107 大きな方に向かって順番にバイト値を変更するループを実行する。
108
109 プログラムを実行した場合の一例を以下に示す。
110
111 .in +4n
112 .nf
113 $\fB ./a.out\fP
114 Start of region:        0x804c000
115 Got SIGSEGV at address: 0x804e000
116 .fi
117 .in
118 .SS プログラムのソース
119 \&
120 .nf
121 #include <unistd.h>
122 #include <signal.h>
123 #include <stdio.h>
124 #include <malloc.h>
125 #include <stdlib.h>
126 #include <errno.h>
127 #include <sys/mman.h>
128
129 #define handle_error(msg) \e
130     do { perror(msg); exit(EXIT_FAILURE); } while (0)
131
132 char *buffer;
133
134 static void
135 handler(int sig, siginfo_t *si, void *unused)
136 {
137     printf("Got SIGSEGV at address: 0x%lx\en",
138             (long) si\->si_addr);
139     exit(EXIT_FAILURE);
140 }
141
142 int
143 main(void)
144 {
145     char *p;
146     int pagesize;
147     struct sigaction sa;
148
149     sa.sa_flags = SA_SIGINFO;
150     sigemptyset(&sa.sa_mask);
151     sa.sa_sigaction = handler;
152     if (sigaction(SIGSEGV, &sa, NULL) == \-1)
153         handle_error("sigaction");
154
155     pagesize = sysconf(_SC_PAGE_SIZE);
156     if (pagesize == \-1)
157         handle_error("sysconf");
158
159     /* Allocate a buffer aligned on a page boundary;
160        initial protection is PROT_READ | PROT_WRITE */
161
162     buffer = memalign(pagesize, 4 * pagesize);
163     if (buffer == NULL)
164         handle_error("memalign");
165
166     printf("Start of region:        0x%lx\en", (long) buffer);
167
168     if (mprotect(buffer + pagesize * 2, pagesize,
169                 PROT_READ) == \-1)
170         handle_error("mprotect");
171
172     for (p = buffer ; ; )
173         *(p++) = \(aqa\(aq;
174
175     printf("Loop completed\en");     /* Should never happen */
176     exit(EXIT_SUCCESS);
177 }
178 .fi
179 .SH 関連項目
180 \fBmmap\fP(2), \fBsysconf\fP(3)