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

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/sysctl_proc.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 <sys/types.h>
 
13
#include <sys/stat.h>
 
14
#include <fcntl.h>
 
15
#include <errno.h>
 
16
#include <string.h>
 
17
 
 
18
#define BUFSIZE 4096
 
19
int main(int argc, char *argv[])
 
20
{
 
21
        char read_buffer[BUFSIZE], verify_buffer[BUFSIZE];
 
22
        ssize_t read_size, write_size;
 
23
        int fd;
 
24
 
 
25
        if ((argc < 3) || (argc == 4 && strcmp(argv[2],"w")) || argc > 4) {
 
26
                fprintf(stderr, "Usage: %s sysctl_path {r,w,rw} [value]\n", argv[0]);
 
27
                return 1;
 
28
        }
 
29
 
 
30
        if (strcmp(argv[2],"r") == 0) {
 
31
                fd = open(argv[1], O_RDONLY);
 
32
                if (fd == -1) {
 
33
                        fprintf(stderr, "FAIL: proc sysctl open r failed - %s\n",
 
34
                                strerror(errno));
 
35
                        return 1;
 
36
                }
 
37
                read_size = read(fd, &read_buffer, sizeof(read_buffer));
 
38
                if (read_size == -1) {
 
39
                        fprintf(stderr, "FAIL: proc sysctl read failed - %s\n",
 
40
                                strerror(errno));
 
41
                        return 1;
 
42
                }
 
43
        }
 
44
 
 
45
        if (strcmp(argv[2], "w") == 0) {
 
46
                fd = open(argv[1], O_WRONLY);
 
47
                if (fd == -1) {
 
48
                        fprintf(stderr, "FAIL: proc sysctl open w failed - %s\n",
 
49
                                strerror(errno));
 
50
                        return 1;
 
51
                }
 
52
                write_size = write(fd, argv[3], strlen(argv[3]));
 
53
                if (write_size == -1) {
 
54
                        fprintf(stderr, "FAIL: proc sysctl write failed - %s\n",
 
55
                                strerror(errno));
 
56
                        return 1;
 
57
                }
 
58
 
 
59
        }
 
60
 
 
61
        if (strcmp(argv[2], "rw") == 0) {
 
62
                fd = open(argv[1], O_RDWR);
 
63
                if (fd == -1) {
 
64
                        fprintf(stderr, "FAIL: proc sysctl open rw failed - %s\n",
 
65
                                strerror(errno));
 
66
                        return 1;
 
67
                }
 
68
                read_size = read(fd, &read_buffer, sizeof(read_buffer));
 
69
                if (read_size == -1) {
 
70
                        fprintf(stderr, "FAIL: proc sysctl read(rw) failed - %s\n",
 
71
                                strerror(errno));
 
72
                        return 1;
 
73
                }
 
74
                lseek(fd, 0, SEEK_SET);
 
75
                write_size = write(fd, &read_buffer, read_size);
 
76
                if (write_size == -1 || write_size != read_size) {
 
77
                        fprintf(stderr, "FAIL: proc sysctl write(rw) failed - %s\n",
 
78
                                strerror(errno));
 
79
                        return 1;
 
80
                }
 
81
 
 
82
                lseek(fd, 0, SEEK_SET);
 
83
                read_size = read(fd, &verify_buffer, sizeof(verify_buffer));
 
84
                if (read_size == -1 || read_size != write_size) {
 
85
                        fprintf(stderr, "FAIL: proc sysctl verify(rw) failed || %zd != %zd - %s\n", read_size, write_size,
 
86
                                strerror(errno));
 
87
                        return 1;
 
88
                }
 
89
                if (memcmp(read_buffer, verify_buffer, read_size) != 0) {
 
90
                        fprintf(stderr, "FAIL: proc sysctl verify failed - %s\n",
 
91
                                strerror(errno));
 
92
                        return 1;
 
93
                }
 
94
        }
 
95
 
 
96
        printf("PASS\n");
 
97
 
 
98
        return 0;
 
99
}