2
* Author: Colin King <colin.king@canonical.com>
4
* Copyright (C) 2012 Canonical, Ltd.
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.
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.
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
28
#include <sys/types.h>
33
#define TEST_PASSED (0)
34
#define TEST_FAILED (1)
35
#define TEST_ERROR (2)
37
#define MMAP_LEN (8192)
45
if ((len = klogctl(10, NULL, 0)) < 0)
48
if ((klog = calloc(1, len)) == NULL)
51
if (klogctl(3, klog, len) < 0) {
59
* https://bugs.launchpad.net/ubuntu/+bug/870326
60
* open(), mmap(), close() and write to mapping causes
63
int main(int argc, char **argv)
68
char buffer[MMAP_LEN];
70
char *klog_after, *klog_new_text;
71
size_t n, lastline_len = 0;
74
fprintf(stderr, "Usage: file\n");
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));
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));
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));
103
fprintf(stderr, "Failed to close : %s\n", strerror(errno));
104
munmap(ptr, MMAP_LEN);
109
if ((klog_before = klog_read()) == NULL) {
110
fprintf(stderr, "Failed to read kernel log\n");
111
munmap(ptr, MMAP_LEN);
116
/* Seek back to start of last line */
117
if ((n = strlen(klog_before)) > 0) {
120
while (n > 0 && klog_before[n-1] != '\n') {
126
/* Modify pages, this caused bug LP#870326 */
127
memset(ptr, ' ', MMAP_LEN);
129
if (munmap(ptr, MMAP_LEN) < 0) {
130
fprintf(stderr, "munmap failed : %s\n", strerror(errno));
137
* Get klog again, find previous klog last line
138
* and offset to end of this
140
if ((klog_after = klog_read()) == NULL) {
141
fprintf(stderr, "Failed to read kernel log\n");
146
klog_new_text = strstr(klog_after, klog_before + n);
148
/* Any new kernel log lines contain the error message? */
150
strstr(klog_new_text + lastline_len,
151
"Error attempting to write lower page"))