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

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/clone.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) 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 <unistd.h>
 
14
#include <errno.h>
 
15
#include <sys/types.h>
 
16
#include <sys/stat.h>
 
17
#include <sys/wait.h>
 
18
#include <sys/user.h>
 
19
#include <fcntl.h>
 
20
#include <string.h>
 
21
#include <stdlib.h>
 
22
#include <getopt.h>
 
23
#include <sched.h>
 
24
#include <linux/unistd.h>
 
25
 
 
26
struct option long_options[] =
 
27
{
 
28
        {"newns", 0, 0, 'n'},  /* create a new namespace */
 
29
        {"help", 0, 0, 'h'},
 
30
};
 
31
 
 
32
static void usage (char * program) {
 
33
        fprintf(stderr, "usage: %s [arguments]\n",
 
34
                program);
 
35
        fprintf(stderr, "%s\n", "$Id$");
 
36
        exit(1);
 
37
}
 
38
 
 
39
static int filedes[2] = {-1, -1};
 
40
 
 
41
static int do_child(void *arg)
 
42
{
 
43
        int rc;
 
44
 
 
45
        close(filedes[0]);
 
46
 
 
47
        rc = write(filedes[1], "PASS\n", strlen("PASS\n"));
 
48
        if (rc < 0) {
 
49
                perror("FAIL: write failed");
 
50
                return 1;
 
51
        }
 
52
 
 
53
        return 0;
 
54
}
 
55
 
 
56
int main(int argc, char *argv[])
 
57
{
 
58
        int rc;
 
59
        int waitstatus;
 
60
        int c;
 
61
        char buf[BUFSIZ];
 
62
        int stack_size = PAGE_SIZE << 4;
 
63
        void *child_stack = malloc(stack_size);
 
64
        int clone_flags = SIGCHLD;
 
65
 
 
66
        while ((c = getopt_long (argc, argv, "+hn", long_options, NULL)) != -1) {
 
67
                switch (c) {
 
68
                    case 'n':
 
69
                        clone_flags |= CLONE_NEWNS;
 
70
                        break;
 
71
                    case 'h':
 
72
                        usage(argv[0]);
 
73
                        break;
 
74
                    default:
 
75
                        usage(argv[0]);
 
76
                        break;
 
77
                }
 
78
        }
 
79
 
 
80
        if (argv[optind])
 
81
                usage(argv[0]);
 
82
 
 
83
        rc = pipe(filedes);
 
84
        if (rc != 0) {
 
85
                perror("FAIL: pipe failed");
 
86
                exit(1);
 
87
        }
 
88
 
 
89
        rc = clone(do_child, child_stack + stack_size, clone_flags, argv);
 
90
        if (rc < 0) {
 
91
                perror("FAIL: clone failed");
 
92
                exit(1);
 
93
        }
 
94
 
 
95
        /* parent */
 
96
        rc = waitpid(-1, (&waitstatus), __WALL);
 
97
        close(filedes[1]);
 
98
        read(filedes[0], &buf, sizeof(buf));
 
99
        if (rc == -1){
 
100
                fprintf(stderr, "FAIL: wait failed - %s\n",
 
101
                        strerror(errno));
 
102
                exit(1);
 
103
        }
 
104
 
 
105
        if ((WEXITSTATUS(waitstatus) == 0) && strcmp("PASS\n", buf) == 0) {
 
106
                printf("PASS\n");
 
107
        }
 
108
 
 
109
        return WEXITSTATUS(waitstatus);
 
110
}