~ecryptfs/ecryptfs/oldtrunk2

« back to all changes in this revision

Viewing changes to tests/kernel/lp-870326/test.c

  • Committer: Tyler Hicks
  • Author(s): Colin King
  • Date: 2012-03-26 18:04:51 UTC
  • mfrom: (660.1.6 new-tests)
  • Revision ID: tyhicks@canonical.com-20120326180451-0r8n9al4f8gf29qe
* tests/kernel/lp-870326.sh, tests/kernel/lp-870326/test.c:
  - test case for open(), mmap(), close(), modify mmap'd region

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Author: Colin King <colin.king@canonical.com>
 
3
 *
 
4
 * Copyright (C) 2012 Canonical, Ltd.
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 
19
 */
 
20
 
 
21
#include <unistd.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <fcntl.h>
 
27
#include <sys/mman.h>
 
28
#include <sys/types.h>
 
29
#include <sys/stat.h>
 
30
#include <sys/klog.h>
 
31
#include <limits.h>
 
32
 
 
33
#define TEST_PASSED     (0)
 
34
#define TEST_FAILED     (1)
 
35
#define TEST_ERROR      (2)
 
36
 
 
37
#define MMAP_LEN        (8192)
 
38
 
 
39
/* read kernel log */
 
40
char *klog_read(void)
 
41
{
 
42
        int len;
 
43
        char *klog;
 
44
 
 
45
        if ((len = klogctl(10, NULL, 0)) < 0)
 
46
                return NULL;
 
47
        
 
48
        if ((klog = calloc(1, len)) == NULL)
 
49
                return NULL;
 
50
 
 
51
        if (klogctl(3, klog, len) < 0) {
 
52
                free(klog);
 
53
                return NULL;
 
54
        }
 
55
        return klog;
 
56
}
 
57
 
 
58
/*
 
59
 *  https://bugs.launchpad.net/ubuntu/+bug/870326
 
60
 *      open(), mmap(), close() and write to mapping causes
 
61
 *      an error.
 
62
 */
 
63
int main(int argc, char **argv)
 
64
{
 
65
        int fd;
 
66
        int rc = TEST_PASSED;
 
67
        unsigned int *ptr;
 
68
        char buffer[MMAP_LEN];
 
69
        char *klog_before;
 
70
        char *klog_after, *klog_new_text;
 
71
        size_t n, lastline_len = 0;
 
72
 
 
73
        if (argc < 2) {
 
74
                fprintf(stderr, "Usage: file\n");
 
75
                exit(TEST_ERROR);
 
76
        }
 
77
 
 
78
        if ((fd = open(argv[1], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
 
79
                fprintf(stderr, "Cannot create %s : %s\n",
 
80
                        argv[1], strerror(errno));
 
81
                exit(TEST_ERROR);
 
82
        }
 
83
 
 
84
        memset(buffer, 'X', sizeof(buffer));
 
85
        if (write(fd, buffer, sizeof(buffer)) < 0) {
 
86
                fprintf(stderr, "Failed to write to %s : %s\n",
 
87
                        argv[1], strerror(errno));
 
88
                close(fd);
 
89
                rc = TEST_ERROR;
 
90
                goto tidy;
 
91
        }
 
92
 
 
93
        ptr = mmap(NULL, MMAP_LEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 
94
        if (ptr == MAP_FAILED) {
 
95
                fprintf(stderr, "Cannot mmap file %s : %s\n",
 
96
                        argv[1], strerror(errno));
 
97
                close(fd);
 
98
                rc = TEST_ERROR;
 
99
                goto tidy;
 
100
        }
 
101
        
 
102
        if (close(fd) < 0) {
 
103
                fprintf(stderr, "Failed to close : %s\n", strerror(errno));
 
104
                munmap(ptr, MMAP_LEN);
 
105
                rc = TEST_ERROR;
 
106
                goto tidy;
 
107
        }
 
108
 
 
109
        if ((klog_before = klog_read()) == NULL) {
 
110
                fprintf(stderr, "Failed to read kernel log\n");
 
111
                munmap(ptr, MMAP_LEN);
 
112
                rc = TEST_ERROR;
 
113
                goto tidy;
 
114
        }
 
115
 
 
116
        /* Seek back to start of last line */
 
117
        if ((n = strlen(klog_before)) > 0) {
 
118
                n--;
 
119
                lastline_len++;
 
120
                while (n > 0 && klog_before[n-1] != '\n') {
 
121
                        n--;
 
122
                        lastline_len++;
 
123
                }
 
124
        }
 
125
 
 
126
        /* Modify pages, this caused bug LP#870326 */
 
127
        memset(ptr, ' ', MMAP_LEN);
 
128
 
 
129
        if (munmap(ptr, MMAP_LEN) < 0) {
 
130
                fprintf(stderr, "munmap failed : %s\n", strerror(errno));
 
131
                free(klog_before);
 
132
                rc = TEST_ERROR;
 
133
                goto tidy;
 
134
        }
 
135
        
 
136
        /*
 
137
         * Get klog again, find previous klog last line
 
138
         * and offset to end of this
 
139
         */
 
140
        if ((klog_after = klog_read()) == NULL) {
 
141
                fprintf(stderr, "Failed to read kernel log\n");
 
142
                free(klog_before);
 
143
                rc = TEST_ERROR;
 
144
                goto tidy;
 
145
        }
 
146
        klog_new_text = strstr(klog_after, klog_before + n);
 
147
 
 
148
        /* Any new kernel log lines contain the error message? */
 
149
        if (klog_new_text &&
 
150
            strstr(klog_new_text + lastline_len,
 
151
                  "Error attempting to write lower page"))
 
152
                rc = TEST_FAILED;
 
153
 
 
154
        free(klog_before);
 
155
        free(klog_after);
 
156
tidy:
 
157
        unlink(argv[1]);
 
158
 
 
159
        exit(rc);
 
160
}