Updated to fedora-glibc-20051010T1417
[glibc.git] / sysdeps / unix / sysv / linux / readonly-area.c
blob29224d98b9e24645ba2a6117c43e8c8a5f927f2e
1 /* Copyright (C) 2004, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdio_ext.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "libio/libioP.h"
27 /* Return 1 if the whole area PTR .. PTR+SIZE is not writable.
28 Return -1 if it is writable. */
30 int
31 __readonly_area (const char *ptr, size_t size)
33 const void *ptr_end = ptr + size;
35 FILE *fp = fopen ("/proc/self/maps", "rc");
36 if (fp == NULL)
38 if (errno == ENOENT)
39 /* It is the system administrator's choice to not have /proc
40 available to this process (e.g., because it runs in a chroot
41 environment. Don't fail in this case. */
42 return 1;
43 return -1;
46 /* We need no locking. */
47 __fsetlocking (fp, FSETLOCKING_BYCALLER);
49 char *line = NULL;
50 size_t linelen = 0;
52 while (! feof_unlocked (fp))
54 if (_IO_getdelim (&line, &linelen, '\n', fp) <= 0)
55 break;
57 char *p;
58 uintptr_t from = strtoul (line, &p, 16);
60 if (p == line || *p++ != '-')
61 break;
63 char *q;
64 uintptr_t to = strtoul (p, &q, 16);
66 if (q == p || *q++ != ' ')
67 break;
69 if (from < (uintptr_t) ptr_end && to > (uintptr_t) ptr)
71 /* Found an entry that at least partially covers the area. */
72 if (*q++ != 'r' || *q++ != '-')
73 break;
75 if (from <= (uintptr_t) ptr && to >= (uintptr_t) ptr_end)
77 size = 0;
78 break;
80 else if (from <= (uintptr_t) ptr)
81 size -= to - (uintptr_t) ptr;
82 else if (to >= (uintptr_t) ptr_end)
83 size -= (uintptr_t) ptr_end - from;
84 else
85 size -= to - from;
87 if (!size)
88 break;
92 fclose (fp);
93 free (line);
95 /* If the whole area between ptr and ptr_end is covered by read-only
96 VMAs, return 1. Otherwise return -1. */
97 return size == 0 ? 1 : -1;