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

« back to all changes in this revision

Viewing changes to tests/regression/subdomain/fork.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: fork.c 425 2007-03-06 20:13:31Z steve-beattie $ */
 
2
 
 
3
/*
 
4
 *      Copyright (C) 2002-2005 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
#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/ipc.h>
 
19
#include <sys/shm.h>
 
20
#include <signal.h>
 
21
#include <fcntl.h>
 
22
#include <limits.h>
 
23
#include <string.h>
 
24
 
 
25
#define FALSE 0
 
26
#define TRUE !FALSE
 
27
 
 
28
#define max(x,y) (x) > (y) ? (x) : (y)
 
29
 
 
30
#define MAX_FILES 5
 
31
 
 
32
int (*pass)[MAX_FILES];
 
33
 
 
34
void test_files(int num_files, char *files[], int index)
 
35
{
 
36
        int fd, i;
 
37
 
 
38
        for (i = 0; i < num_files; i++) {
 
39
                fd = open(files[i], O_RDWR);
 
40
 
 
41
                if (fd == -1) {
 
42
                        if (errno == ENOENT) {
 
43
                                pass[index][i] = -1;
 
44
                        } else {
 
45
                                pass[index][i] = 0;
 
46
                        }
 
47
                } else {
 
48
                        pass[index][i] = 1;
 
49
                        close(fd);
 
50
                }
 
51
        }
 
52
}
 
53
 
 
54
int main(int argc, char *argv[])
 
55
{
 
56
        int num_files, i, shmid;
 
57
        pid_t pid;
 
58
        struct shmid_ds shm_desc;
 
59
 
 
60
        if (argc < 2) {
 
61
                fprintf(stderr, "usage: %s program [args] \n", argv[0]);
 
62
                return 1;
 
63
        }
 
64
 
 
65
        num_files = max(argc - 1, MAX_FILES);
 
66
 
 
67
        shmid = shmget(IPC_PRIVATE, sizeof(int[2][MAX_FILES]), IPC_CREAT);
 
68
        if (shmid == -1) {
 
69
                fprintf(stderr, "FAIL: shmget failed %s\n", strerror(errno));
 
70
                return 1;
 
71
        }
 
72
 
 
73
        pass = (int(*)[MAX_FILES])shmat(shmid, NULL, 0);
 
74
 
 
75
        if (pass == (void *)-1) {
 
76
                fprintf(stderr, "FAIL: shmat failed %s\n", strerror(errno));
 
77
                return 1;
 
78
        }
 
79
 
 
80
        pid = fork();
 
81
 
 
82
        if (pid) {              /* parent */
 
83
                int status;
 
84
                int allpassed = TRUE;
 
85
 
 
86
                test_files(argc - 1, &argv[1], 0);
 
87
 
 
88
                while (wait(&status) != pid) ;
 
89
 
 
90
                for (i = 0; i < argc - 1; i++) {
 
91
                        if (pass[0][i] != pass[1][i] ||
 
92
                            pass[0][i] == -1 || pass[1][i] == -1) {
 
93
                                if (allpassed) {
 
94
                                        fprintf(stderr, "FAILED:");
 
95
                                        allpassed = FALSE;
 
96
                                }
 
97
 
 
98
                                fprintf(stderr, " file%d(%d:%d)",
 
99
                                        i + 1, pass[0][i], pass[1][i]);
 
100
                        }
 
101
                }
 
102
 
 
103
                if (allpassed) {
 
104
                        printf("PASS\n");
 
105
                } else {
 
106
                        fprintf(stderr, "\n");
 
107
                }
 
108
 
 
109
                (void)shmdt(pass);
 
110
                shmctl(shmid, IPC_RMID, &shm_desc);
 
111
 
 
112
        } else {
 
113
                test_files(argc - 1, &argv[1], 1);
 
114
        }
 
115
 
 
116
        return 0;
 
117
}