~ubuntu-branches/ubuntu/raring/apparmor/raring

« back to all changes in this revision

Viewing changes to tests/regression/subdomain/clone.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-03-23 16:42:01 UTC
  • Revision ID: james.westby@ubuntu.com-20070323164201-jkax6f0oku087b7l
Tags: upstream-2.0.1+510.dfsg
ImportĀ upstreamĀ versionĀ 2.0.1+510.dfsg

Show diffs side-by-side

added added

removed removed

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