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

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/unix_fd_client.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
#define _XOPEN_SOURCE 500
 
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 <sys/types.h>
 
15
#include <string.h>
 
16
#include <stdlib.h>
 
17
#include <sys/socket.h>
 
18
#include <alloca.h>
 
19
#include <fcntl.h>
 
20
#include <sys/uio.h>
 
21
#include <sys/un.h>
 
22
#include <sys/wait.h>
 
23
#include <errno.h>
 
24
#include <stdlib.h>
 
25
 
 
26
int main(int argc, char *argv[]) {
 
27
        int sock, fd, len;
 
28
        struct sockaddr_un remote;
 
29
        char read_buffer[17], f_buf[255];
 
30
        struct iovec vect;
 
31
        struct msghdr mesg;
 
32
        struct cmsghdr *ctrl_mesg;
 
33
 
 
34
        if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
 
35
                fprintf(stderr, "FAIL CLIENT - sock %s\n",
 
36
                        strerror(errno));
 
37
                exit(1);
 
38
        }
 
39
 
 
40
        remote.sun_family = AF_UNIX;
 
41
        strcpy(remote.sun_path, argv[1]);
 
42
        len = strlen(remote.sun_path) + sizeof(remote.sun_family);
 
43
        if (connect(sock, (struct sockaddr *)&remote, len) == -1) {
 
44
                fprintf(stderr, "FAIL CLIENT - connect %s\n",
 
45
                        strerror(errno));
 
46
                exit(1);
 
47
        }
 
48
 
 
49
        vect.iov_base = f_buf;
 
50
        vect.iov_len = 255;
 
51
 
 
52
        mesg.msg_name = NULL;
 
53
        mesg.msg_namelen=0;
 
54
        mesg.msg_iov = &vect;
 
55
        mesg.msg_iovlen = 1;
 
56
 
 
57
        ctrl_mesg = alloca(sizeof (struct cmsghdr) + sizeof(fd));
 
58
        ctrl_mesg->cmsg_len = sizeof(struct cmsghdr) + sizeof(fd);
 
59
        mesg.msg_control = ctrl_mesg;
 
60
        mesg.msg_controllen = ctrl_mesg->cmsg_len;
 
61
 
 
62
        if (!recvmsg(sock, &mesg,0 )) {
 
63
                fprintf(stderr, "FAIL CLIENT - recvmsg\n");
 
64
                exit(1);
 
65
        }
 
66
 
 
67
        /* get mr. file descriptor */
 
68
 
 
69
        memcpy(&fd, CMSG_DATA(ctrl_mesg), sizeof(fd));
 
70
 
 
71
        if (pread(fd, read_buffer, 16, 0) <= 0) {
 
72
                /* Failure */
 
73
                fprintf(stderr, "FAIL CLIENT - could not read\n");
 
74
                send(sock, "FAILFAILFAILFAIL", 16, 0);
 
75
                exit(1);
 
76
        } else {
 
77
                send(sock, read_buffer, strlen(read_buffer),0);
 
78
        }
 
79
        
 
80
        /* looks like it worked */
 
81
        exit(0);
 
82
}