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

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/rw.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 <sys/types.h>
 
14
#include <sys/stat.h>
 
15
#include <fcntl.h>
 
16
#include <signal.h>
 
17
#include <string.h>
 
18
 
 
19
static int gotsigusr1;
 
20
 
 
21
void handler(int sig)
 
22
{
 
23
        ++gotsigusr1;
 
24
}
 
25
 
 
26
int main(int argc, char *argv[])
 
27
{
 
28
int fd, rc, pass;
 
29
const char *data="hello world";
 
30
char buf[128];
 
31
sigset_t sigset;
 
32
 
 
33
        if (argc != 2){
 
34
                fprintf(stderr, "usage: %s file\n",
 
35
                        argv[0]);
 
36
                return 1;
 
37
        }
 
38
 
 
39
        if (signal(SIGUSR1, handler) == SIG_ERR){
 
40
                fprintf(stderr, "FAIL: internal error signal failed - %s\n",
 
41
                        strerror(errno));
 
42
                return 1;
 
43
        }
 
44
 
 
45
        if (sigemptyset(&sigset) == -1) {
 
46
                perror ("FAIL: nullifying sigset");
 
47
                return 1;
 
48
        }
 
49
 
 
50
        fd=open(argv[1], O_CREAT|O_EXCL|O_RDWR, S_IWUSR|S_IRUSR);
 
51
        if (fd == -1){
 
52
                fprintf(stderr, "FAIL: create %s failed - %s\n",
 
53
                        argv[1],
 
54
                        strerror(errno));
 
55
                return 1;
 
56
        }
 
57
 
 
58
        pass=1;
 
59
 
 
60
nextpass:
 
61
        (void)lseek(fd, 0, SEEK_SET);
 
62
        rc=write(fd, data, strlen(data));
 
63
 
 
64
        if (rc != strlen(data)){
 
65
                fprintf(stderr, "FAIL: write failed - %s\n",
 
66
                        strerror(errno));
 
67
                return 1;
 
68
        }
 
69
 
 
70
        (void)lseek(fd, 0, SEEK_SET);
 
71
        rc=read(fd, buf, sizeof(buf));
 
72
 
 
73
        if (rc != strlen(data)){
 
74
                fprintf(stderr, "FAIL: read failed - %s\n",
 
75
                        strerror(errno));
 
76
                return 1;
 
77
        }
 
78
 
 
79
        if (memcmp(buf, data, strlen(data)) != 0){
 
80
                fprintf(stderr, "FAIL: comparison failed - %s\n",
 
81
                        strerror(errno));
 
82
                return 1;
 
83
        }
 
84
 
 
85
        gotsigusr1=0;
 
86
 
 
87
        if (pass == 1){
 
88
                while (!gotsigusr1){
 
89
                        (void)sigsuspend(&sigset);
 
90
                }
 
91
 
 
92
                pass++;
 
93
                goto nextpass;
 
94
        }
 
95
 
 
96
        printf("PASS\n");
 
97
 
 
98
        return 0;
 
99
}