OSDN Git Service

c192936baffa068f6414e79eb8095755d583dba9
[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 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
33 .\"         all rights reserved.
34 .\" Translated 1997-06-01, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
35 .\" Updated and revised 2003-10-24, Suzuki Takashi.
36 .\" Updated & Modified 2005-02-08, Yuichi SATO <ysato444@yahoo.co.jp>
37 .\" Updated & Modified 2005-10-15, Yuichi SATO
38 .\" Updated 2007-06-11, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.54
39 .\"
40 .\"WORD: bitwise-or             ¥Ó¥Ã¥È¤´¤È¤ÎÏÀÍýÏÂ
41 .\"WORD: protection             ¥¢¥¯¥»¥¹Êݸî
42 .\"WORD: read-only              Æɤ߼è¤êÀìÍÑ
43 .\"
44 .TH MPROTECT 2 2008-08-06 "Linux" "Linux Programmer's Manual"
45 .SH Ì¾Á°
46 mprotect \- ¥á¥â¥êÎΰè¤ÎÊݸî¤òÀßÄꤹ¤ë
47 .SH ½ñ¼°
48 .nf
49 .B #include <sys/mman.h>
50 .sp
51 .BI "int mprotect(const void *" addr ", size_t " len ", int " prot );
52 .fi
53 .SH ÀâÌÀ
54 .BR mprotect ()
55 ¤Ï¡¢¶è´Ö [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1] ¤Î¥¢¥É¥ì¥¹ÈϰϤò´Þ¤à
56 ¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤Î¥á¥â¥ê¥Ú¡¼¥¸¤Î¥¢¥¯¥»¥¹Êݸî¤òÊѹ¹¤¹¤ë¡£
57 .I addr
58 ¤Ï¥Ú¡¼¥¸¶­³¦¤Ë°ìÃפ·¤Æ¤¤¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
59
60 ¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤¬¥¢¥¯¥»¥¹Êݸî¤Ë°ãÈ¿¤¹¤ë¤è¤¦¤Ê¥á¥â¥ê¥¢¥¯¥»¥¹¤ò
61 ¹Ô¤ª¤¦¤È¤¹¤ë¤È¡¢¥«¡¼¥Í¥ë¤Ï¥·¥°¥Ê¥ë
62 .B SIGSEGV
63 ¤ò¤½¤Î¥×¥í¥»¥¹¤ËÂФ·¤ÆÀ¸À®¤¹¤ë¡£
64 .PP
65 .I prot
66 ¤Ë¤Ï¡¢
67 .B PROT_NONE
68 ¤«¡¢°Ê²¼¤Î¥ê¥¹¥È¤Î
69 .B PROT_NONE
70 °Ê³°¤ÎÃͤò¥Ó¥Ã¥ÈËè¤ÎÏÀÍýÏ (bitwize-or) ¤Ç»ØÄꤹ¤ë:
71 .TP 1.1i
72 .B PROT_NONE
73 ¤½¤Î¥á¥â¥ê¤Ë¤ÏÁ´¤¯¥¢¥¯¥»¥¹¤Ç¤­¤Ê¤¤¡£
74 .TP
75 .B PROT_READ
76 ¤½¤Î¥á¥â¥ê¤òÆɤ߼è¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
77 .TP
78 .B PROT_WRITE
79 ¤½¤Î¥á¥â¥ê¤òÊѹ¹¤Ç¤­¤ë¡£
80 .TP
81 .B PROT_EXEC
82 ¤½¤Î¥á¥â¥ê¤Ï¼Â¹Ô²Äǽ¤Ç¤¢¤ë¡£
83 .\" FIXME
84 .\" Document PROT_GROWSUP and PROT_GROWSDOWN
85 .SH ÊÖ¤êÃÍ
86 À®¸ù¤·¤¿¾ì¹ç¡¢
87 .BR mprotect ()
88 ¤Ï 0 ¤òÊÖ¤¹¡£¥¨¥é¡¼¤Î¾ì¹ç¤Ï \-1 ¤¬Ê֤ꡢ
89 .I errno
90 ¤¬Å¬ÀÚ¤ËÀßÄꤵ¤ì¤ë¡£
91 .SH ¥¨¥é¡¼
92 .TP
93 .B EACCES
94 »ØÄꤵ¤ì¤¿¥¢¥¯¥»¥¹¤ò¥á¥â¥ê¤ËÀßÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤Ê¤¤¡£
95 ¤³¤ì¤Ï¡¢Î㤨¤Ð
96 ¥Õ¥¡¥¤¥ë¤òÆɤ߼è¤êÀìÍѤÇ
97 .BR mmap (2)
98 ¤·¤Æ¤ª¤ê¡¢¤½¤ÎÎΰè¤ËÂФ·¤Æ
99 .BR mprotect ()
100 ¤ò¸Æ¤Ó½Ð¤·¤Æ
101 .B PROT_WRITE
102 ¤ËÀßÄꤷ¤è¤¦¤È¤·¤¿¾ì¹ç¤ËȯÀ¸¤¹¤ë¡£
103 .TP
104 .B EINVAL
105 \fIaddr\fP ¤¬Í­¸ú¤Ê¥Ý¥¤¥ó¥¿¤Ç¤Ê¤¤¤«¡¢
106 ¥·¥¹¥Æ¥à¤Î¥Ú¡¼¥¸¥µ¥¤¥º¤ÎÇÜ¿ô¤Ç¤Ê¤¤¡£
107 .\" Or: both PROT_GROWSUP and PROT_GROWSDOWN were specified in 'prot'.
108 .TP
109 .B ENOMEM
110 ¥«¡¼¥Í¥ëÆâÉô¤Î¹½Â¤ÂΤò³ä¤êÅö¤Æ¤ë¤³¤È¤¬¤Ç¤­¤Ê¤«¤Ã¤¿¡£
111 .TP
112 .B ENOMEM
113 .RI [ addr ,
114 .IR addr + len ]
115 ¤È¤¤¤¦ÈϰϤΥ¢¥É¥ì¥¹¤¬¥×¥í¥»¥¹¤Î¥¢¥É¥ì¥¹¶õ´Ö¤È¤·¤ÆÉÔÀµ¤Ç¤¢¤ë¤«¡¢
116 ¤½¤ÎÈϰϤΥ¢¥É¥ì¥¹¤¬¥Þ¥Ã¥×¤µ¤ì¤Æ¤¤¤Ê¤¤ 1 ¤Ä°Ê¾å¤Î¥Ú¡¼¥¸¤ò»Ø¤·¤Æ¤¤¤ë
117 (¥«¡¼¥Í¥ë 2.4.19 ¤è¤êÁ°¤Ç¤Ï¡¢¤³¤Î¾õ¶·¤Ç¥¨¥é¡¼
118 .B EFAULT
119 ¤¬´Ö°ã¤Ã¤ÆÀ¸À®¤µ¤ì¤Æ¤¤¤¿)¡£
120 .SH ½àµò
121 SVr4, POSIX.1-2001.
122 .\" SVr4 ¤Ç¤Ï¾¤Ë¥¨¥é¡¼¥³¡¼¥É EAGAIN ¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
123 .\" SVr4 ¤Î¥¨¥é¡¼¾ò·ï¤Ï Linux ¤Î¤â¤Î¤Ë¤­¤Á¤ó¤ÈÂбþ¤·¤Ê¤¤¡£
124 POSIX ¤Ç¤Ï¡¢
125 .BR mmap (2)
126 ·Ðͳ¤Ç³ÍÆÀ¤·¤Æ¤¤¤Ê¤¤¥á¥â¥êÎΰè¤ËÂФ·¤Æ
127 .BR mprotect ()
128 ¤ò¹Ô¤Ã¤¿¾ì¹ç¤Î
129 .BR mprotect ()
130 ¤ÎÆ°ºî¤Ï̤ÄêµÁ¤Ç¤¢¤ë¤È¤µ¤ì¤Æ¤¤¤ë¡£
131 .SH Ãí°Õ
132 Linux ¤Ç¤Ï¡¢(¥«¡¼¥Í¥ë vsyscall Îΰè°Ê³°¤Î)
133 Ǥ°Õ¤Î¥×¥í¥»¥¹¥¢¥É¥ì¥¹¶õ´Ö¤ËÂФ·¤Æ
134 .BR mprotect ()
135 ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤¬¡¢¾ï¤Ëµö¤µ¤ì¤Æ¤¤¤ë¡£
136 ¤³¤ì¤ÏÆä˴û¸¤Î¥³¡¼¥É¥Þ¥Ã¥Ô¥ó¥°¤ò½ñ¤­¹þ¤ß²Äǽ¤Ë¤¹¤ë¤¿¤á¤Ë»È¤ï¤ì¤ë¡£
137
138 .B PROT_EXEC
139 ¤¬
140 .B PROT_READ
141 ¤È°Û¤Ê¤ë±Æ¶Á¤ò»ý¤Ä¤«Èݤ«¤Ï¡¢¥¢¡¼¥­¥Æ¥¯¥Á¥ã¤È¥«¡¼¥Í¥ë¤Î¥Ð¡¼¥¸¥ç¥ó¤Ë°Í¸¤¹¤ë¡£
142 (i386 ¤Ê¤É¤Î) ¤¤¤¯¤Ä¤«¤Î¥¢¡¼¥­¥Æ¥¯¥Á¥ã¤Ç¤Ï¡¢
143 .B PROT_WRITE
144 ¤ò¥»¥Ã¥È¤¹¤ë¤È¡¢°ÅÌۤΤ¦¤Á¤Ë
145 .B PROT_READ
146 ¤¬¥»¥Ã¥È¤µ¤ì¤ë¡£
147
148 POSIX.1-2001 ¤Ç¤Ï¡¢
149 .I prot
150 ¤Ç»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¥¢¥¯¥»¥¹¤òµö²Ä¤¹¤ë¼ÂÁõ¤òǧ¤á¤Æ¤¤¤ë¡£
151 ¤¿¤À¤·¡¢ºÇÄã¸Â¡¢
152 .B PROT_WRITE
153 ¤¬¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ë¤Î¤ß½ñ¤­¹þ¤ß¥¢¥¯¥»¥¹¤¬µö²Ä¤µ¤ì¡¢
154 .B PROT_NONE
155 ¤¬¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï¥¢¥¯¥»¥¹¤Ïµö²Ä¤µ¤ì¤Ê¤¤ÅÀ¤À¤±¤Ï
156 Ëþ¤¿¤¹É¬Íפ¬¤¢¤ë¡£
157 .SH Îã
158 .\" sigaction.2 refers to this example
159 .PP
160 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï¡¢¥á¥â¥ê¥Ú¡¼¥¸¤ò 4¤Ä³ÎÊݤ·¡¢¤½¤Î¤¦¤Á 3ÈÖÌܤΥڡ¼¥¸¤ò
161 Æɤ߹þ¤ßÀìÍѤËÀßÄꤹ¤ë¡£¤½¤Î¸å¤Ç¡¢³ÎÊݤ·¤¿Îΰè¤Î¥¢¥É¥ì¥¹¤Î¾®¤µ¤¤Êý¤«¤é
162 Â礭¤ÊÊý¤Ë¸þ¤«¤Ã¤Æ½çÈ֤˥Х¤¥ÈÃͤòÊѹ¹¤¹¤ë¥ë¡¼¥×¤ò¼Â¹Ô¤¹¤ë¡£
163
164 ¥×¥í¥°¥é¥à¤ò¼Â¹Ô¤·¤¿¾ì¹ç¤Î°ìÎã¤ò°Ê²¼¤Ë¼¨¤¹¡£
165
166 .in +4n
167 .nf
168 .RB "$" " ./a.out"
169 Start of region:        0x804c000
170 Got SIGSEGV at address: 0x804e000
171 .fi
172 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
173 \&
174 .in
175 .nf
176 #include <unistd.h>
177 #include <signal.h>
178 #include <stdio.h>
179 #include <malloc.h>
180 #include <stdlib.h>
181 #include <errno.h>
182 #include <sys/mman.h>
183
184 #define handle_error(msg) \\
185     do { perror(msg); exit(EXIT_FAILURE); } while (0)
186
187 char *buffer;
188
189 static void
190 handler(int sig, siginfo_t *si, void *unused)
191 {
192     printf("Got SIGSEGV at address: 0x%lx\\n",
193             (long) si\->si_addr);
194     exit(EXIT_FAILURE);
195 }
196
197 int
198 main(void)
199 {
200     char *p;
201     int pagesize;
202     struct sigaction sa;
203
204     sa.sa_flags = SA_SIGINFO;
205     sigemptyset(&sa.sa_mask);
206     sa.sa_sigaction = handler;
207     if (sigaction(SIGSEGV, &sa, NULL) == \-1)
208         handle_error("sigaction");
209
210     pagesize = sysconf(_SC_PAGE_SIZE);
211     if (pagesize == \-1)
212         handle_error("sysconf");
213
214     /* Allocate a buffer aligned on a page boundary;
215        initial protection is PROT_READ | PROT_WRITE */
216
217     buffer = memalign(pagesize, 4 * pagesize);
218     if (buffer == NULL)
219         handle_error("memalign");
220
221     printf("Start of region:        0x%lx\\n", (long) buffer);
222
223     if (mprotect(buffer + pagesize * 2, pagesize,
224                 PROT_NONE) == \-1)
225         handle_error("mprotect");
226
227     for (p = buffer ; ; )
228         *(p++) = \(aqa\(aq;
229
230     printf("Loop completed\\n");     /* Should never happen */
231     exit(EXIT_SUCCESS);
232 }
233 .fi
234 .SH ´ØÏ¢¹àÌÜ
235 .BR mmap (2),
236 .BR sysconf (3)