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

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/openat.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-2007 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
#define _GNU_SOURCE
 
11
 
 
12
#include <stdio.h>
 
13
#include <stdlib.h>
 
14
#include <unistd.h>
 
15
#include <errno.h>
 
16
#include <sys/types.h>
 
17
#include <sys/stat.h>
 
18
#include <fcntl.h>
 
19
#include <string.h>
 
20
#include <getopt.h>
 
21
 
 
22
struct option long_options[] = {
 
23
        {"delete", 0, 0, 'd'},  /* delete the directory after opening */
 
24
        {"rename", 1, 0, 'r'},  /* rename the directory to -- */
 
25
        {"help", 0, 0, 'h'},
 
26
};
 
27
 
 
28
static void usage (char * program)
 
29
{
 
30
        fprintf(stderr, "usage: %s [--delete] dir file\n", program);
 
31
        fprintf(stderr, "%s\n", "$Id$");
 
32
        exit(1);
 
33
}
 
34
 
 
35
int main(int argc, char *argv[])
 
36
{
 
37
        int fd = -1, dirfd = -1;
 
38
        int c;
 
39
        int do_delete = 0;
 
40
        int do_rename = 0;
 
41
        char *dir, *file, *newdir = NULL;
 
42
 
 
43
        while ((c = getopt_long (argc, argv, "+hdr:", long_options, NULL)) != -1) {
 
44
                switch (c) {
 
45
                    case 'd':
 
46
                        do_delete = 1;
 
47
                        break;
 
48
                    case 'r':
 
49
                        do_rename = 1;
 
50
                        newdir = optarg;
 
51
                        break;
 
52
                    case 'h':
 
53
                        /* fall through */
 
54
                    default:
 
55
                        usage(argv[0]);
 
56
                        break;
 
57
                }
 
58
        }
 
59
 
 
60
        if (argc - optind != 2)
 
61
                usage(argv[0]);
 
62
 
 
63
        dir = argv[optind];
 
64
        file = argv[optind + 1];
 
65
 
 
66
        dirfd = open(dir, O_RDONLY | O_DIRECTORY);
 
67
        if (dirfd == -1) {
 
68
                fprintf(stderr, "FAIL: open %s failed - %s\n",
 
69
                        dir, strerror(errno));
 
70
                return 1;
 
71
        }
 
72
 
 
73
        if (do_delete && rmdir(dir) == -1) {
 
74
                fprintf(stderr, "FAIL: rmdir %s failed - %s\n",
 
75
                        dir, strerror(errno));
 
76
                return 1;
 
77
        } else if (do_rename && rename(dir, newdir) == -1) {
 
78
                fprintf(stderr, "FAIL: rename %s, %s failed - %s\n",
 
79
                        dir, newdir, strerror(errno));
 
80
                return 1;
 
81
        }
 
82
 
 
83
        fd = openat(dirfd, file, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
 
84
        if (fd == -1) {
 
85
                fprintf(stderr, "FAIL: openat %s failed - %s\n",
 
86
                        file, strerror(errno));
 
87
                close(dirfd);
 
88
                return 1;
 
89
        }
 
90
 
 
91
        close(fd);
 
92
        close(dirfd);
 
93
 
 
94
        printf("PASS\n");
 
95
 
 
96
        return 0;
 
97
}