~ubuntu-branches/ubuntu/feisty/apparmor/feisty

« back to all changes in this revision

Viewing changes to tests/regression/subdomain/pipe.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: pipe.c 61 2006-05-19 18:32:14Z 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 <fcntl.h>
 
19
#include <linux/unistd.h>
 
20
#include <string.h>
 
21
#include <stdlib.h>
 
22
#include "changehat.h"
 
23
 
 
24
const char *data="hello world";
 
25
 
 
26
int do_read (int fd)
 
27
{
 
28
        int rc;
 
29
        char buf[128];
 
30
 
 
31
        if (fd < 0) {
 
32
                fprintf(stderr, "FAIL: read failed - no descriptor passed\n");
 
33
                return 1;
 
34
        }
 
35
 
 
36
        rc=read(fd, buf, sizeof(buf));
 
37
 
 
38
        if (rc != strlen(data)){
 
39
                fprintf(stderr, "FAIL: read failed - %s\n",
 
40
                        strerror(errno));
 
41
                return 1;
 
42
        }
 
43
 
 
44
        if (memcmp(buf, data, strlen(data)) != 0){
 
45
                fprintf(stderr, "FAIL: comparison failed - %s\n",
 
46
                        strerror(errno));
 
47
                return 1;
 
48
        }
 
49
 
 
50
        close(fd);
 
51
 
 
52
        return 0;
 
53
}
 
54
 
 
55
int do_write (int fd)
 
56
{
 
57
        int rc;
 
58
 
 
59
        rc=write(fd, data, strlen(data));
 
60
 
 
61
        if (rc != strlen(data)){
 
62
                fprintf(stderr, "FAIL: write failed - %s\n",
 
63
                        strerror(errno));
 
64
                return 1;
 
65
        }
 
66
 
 
67
        close(fd);
 
68
 
 
69
        return 0;
 
70
}
 
71
 
 
72
int main(int argc, char *argv[])
 
73
{
 
74
        int rc;
 
75
        pid_t pid;
 
76
        int waitstatus;
 
77
        int filedes[2];
 
78
        int read_error = 0;
 
79
 
 
80
        if (argc != 2){
 
81
                fprintf(stderr, "usage: %s hatname\n",
 
82
                        argv[0]);
 
83
                return 1;
 
84
        }
 
85
 
 
86
        /* change hat if hatname != nochange */
 
87
        if (strcmp(argv[1], "nochange") != 0){
 
88
                rc = change_hat(argv[1], SD_ID_MAGIC+1);
 
89
                if (rc == -1){
 
90
                        fprintf(stderr, "FAIL: changehat %s failed - %s\n",
 
91
                                argv[1], strerror(errno));
 
92
                        exit(1);
 
93
                }
 
94
        }
 
95
 
 
96
        if (pipe(filedes) == -1){
 
97
                fprintf(stderr, "FAIL: pipe() failed - %s\n",
 
98
                        strerror(errno));
 
99
                exit(1);
 
100
        }
 
101
 
 
102
        pid = fork();
 
103
        if (pid == -1) {
 
104
                fprintf(stderr, "FAIL: fork failed - %s\n",
 
105
                        strerror(errno));
 
106
                exit(1);
 
107
        } else if (pid != 0) {
 
108
                /* parent */
 
109
                read_error = do_read(filedes[0]);
 
110
                rc = wait(&waitstatus);
 
111
                if (rc == -1){
 
112
                        fprintf(stderr, "FAIL: wait failed - %s\n",
 
113
                                strerror(errno));
 
114
                        exit(1);
 
115
                }
 
116
        } else {
 
117
                /* child */
 
118
                exit(do_write(filedes[1]));
 
119
        }
 
120
 
 
121
        if ((WIFEXITED(waitstatus) != 0) && (WEXITSTATUS(waitstatus) == 0) 
 
122
                        && read_error == 0) {
 
123
                printf("PASS\n");
 
124
        } else {
 
125
                return (WEXITSTATUS(waitstatus) & read_error);
 
126
        }
 
127
 
 
128
        return 0;
 
129
}