~ubuntu-branches/ubuntu/wily/apparmor/wily

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/mmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2011-04-27 10:38:07 UTC
  • mfrom: (5.1.118 natty)
  • Revision ID: james.westby@ubuntu.com-20110427103807-ym3rhwys6o84ith0
Tags: 2.6.1-2
debian/copyright: clarify for some full organization names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      Copyright (C) 2002-2005 Novell/SUSE
 
3
 *
 
4
 *      This program is free software; you can redistribute it and/or
 
5
 *      modify it under the terms of the GNU General Public License as
 
6
 *      published by the Free Software Foundation, version 2 of the
 
7
 *      License.
 
8
 */
 
9
 
 
10
#include <stdio.h>
 
11
#include <unistd.h>
 
12
#include <errno.h>
 
13
#include <unistd.h>
 
14
#include <fcntl.h>
 
15
#include <sys/types.h>
 
16
#include <sys/stat.h>
 
17
#include <sys/mman.h>
 
18
#include <signal.h>
 
19
#include <string.h>
 
20
 
 
21
static int gotsigusr1;
 
22
 
 
23
void handler(int sig)
 
24
{
 
25
        ++gotsigusr1;
 
26
}
 
27
 
 
28
int main(int argc, char *argv[])
 
29
{
 
30
int fd, rc, pass;
 
31
const char *data="hello world";
 
32
char *mptr;
 
33
char buf[128];
 
34
sigset_t sigset;
 
35
 
 
36
 
 
37
        if (argc != 2){
 
38
                fprintf(stderr, "usage: %s file\n",
 
39
                        argv[0]);
 
40
                return 1;
 
41
        }
 
42
 
 
43
        if (signal(SIGUSR1, handler) == SIG_ERR){
 
44
                fprintf(stderr, "FAIL: internal error signal failed - %s\n",
 
45
                        strerror(errno));
 
46
                return 1;
 
47
        }
 
48
 
 
49
        if (sigemptyset(&sigset) == -1) {
 
50
                perror ("FAIL: nullifying sigset");
 
51
                return 1;
 
52
        }
 
53
 
 
54
        fd=open(argv[1], O_CREAT|O_EXCL|O_RDWR, S_IWUSR|S_IRUSR);
 
55
        if (fd == -1){
 
56
                fprintf(stderr, "FAIL: create %s failed - %s\n",
 
57
                        argv[1],
 
58
                        strerror(errno));
 
59
                return 1;
 
60
        }
 
61
 
 
62
        if (ftruncate(fd, getpagesize()) == -1){
 
63
                fprintf(stderr, "FAIL: ftruncate  %s failed - %s\n",
 
64
                        argv[1],
 
65
                        strerror(errno));
 
66
        }
 
67
 
 
68
        mptr=mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
 
69
 
 
70
        if (mptr == MAP_FAILED){
 
71
                fprintf(stderr, "FAIL: mmap failed - %s\n",
 
72
                        strerror(errno));
 
73
                return 1;
 
74
        }
 
75
 
 
76
        pass=1;
 
77
 
 
78
nextpass:
 
79
        (void)memcpy(mptr, data, strlen(data));
 
80
 
 
81
        if (msync(mptr, strlen(data), MS_SYNC) == -1){
 
82
                fprintf(stderr, "FAIL: msync failed - %s\n",
 
83
                        strerror(errno));
 
84
                return 1;
 
85
        }
 
86
 
 
87
 
 
88
        (void)lseek(fd, 0, SEEK_SET);
 
89
        rc=read(fd, buf, strlen(data));
 
90
 
 
91
        if (rc != strlen(data)){
 
92
                fprintf(stderr, "FAIL: read failed - %s\n",
 
93
                        strerror(errno));
 
94
                return 1;
 
95
        }
 
96
 
 
97
        if (memcmp(buf, mptr, strlen(data)) != 0){
 
98
                fprintf(stderr, "FAIL: read comparison failed\n");
 
99
        }
 
100
 
 
101
        gotsigusr1=0;
 
102
 
 
103
        if (pass == 1){
 
104
                while (!gotsigusr1){
 
105
                        (void)sigsuspend(&sigset);
 
106
                }
 
107
 
 
108
                pass++;
 
109
                goto nextpass;
 
110
        }
 
111
 
 
112
        printf("PASS\n");
 
113
 
 
114
        return 0;
 
115
}