~ubuntu-branches/ubuntu/saucy/mutextrace/saucy-proposed

« back to all changes in this revision

Viewing changes to tests/common.c

  • Committer: Package Import Robot
  • Author(s): Simon Richter
  • Date: 2013-05-03 14:01:37 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130503140137-jmevik7bws88ya2b
Tags: 0.1.2-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifdef HAVE_CONFIG_H
 
2
#include <config.h>
 
3
#endif
 
4
 
 
5
#define _GNU_SOURCE 1
 
6
 
 
7
#include <stdlib.h>
 
8
#include <stdio.h>
 
9
#include <string.h>
 
10
#include <unistd.h>
 
11
 
 
12
#include <sys/types.h>
 
13
#include <sys/wait.h>
 
14
 
 
15
int test_main(void);
 
16
 
 
17
int main(int argc, char **argv, char **envp)
 
18
{
 
19
        (void)argc;
 
20
 
 
21
        char const *preload = getenv("LD_PRELOAD");
 
22
        if(!preload || !strstr(preload, "mutextrace.so"))
 
23
        {
 
24
                int fds[2];
 
25
 
 
26
                int rc = pipe(fds);
 
27
                if(rc == -1)
 
28
                {
 
29
                        perror("pipe()");
 
30
                        return 1;
 
31
                }
 
32
 
 
33
                pid_t child = fork();
 
34
                if(child == -1)
 
35
                {
 
36
                        perror("fork()");
 
37
                        return 1;
 
38
                }
 
39
 
 
40
                if(child == 0)
 
41
                {
 
42
                        dup2(STDERR_FILENO, fds[1]);
 
43
                        close(fds[0]);
 
44
                        close(fds[1]);
 
45
 
 
46
                        char const *wrapper = "mutextrace";
 
47
 
 
48
                        char *const new_argv[] =
 
49
                        {
 
50
                                (char *)wrapper,
 
51
                                argv[0],
 
52
                                0
 
53
                        };
 
54
 
 
55
                        execve(wrapper, new_argv, envp);
 
56
                        _exit(1);
 
57
                }
 
58
                else
 
59
                {
 
60
                        close(fds[1]);
 
61
 
 
62
                        char buffer[64];
 
63
                        ssize_t count;
 
64
                        while((count = read(fds[0], buffer, sizeof buffer)) > 0)
 
65
                        {
 
66
                                write(STDERR_FILENO, buffer, count);
 
67
                        }
 
68
 
 
69
                        int status;
 
70
 
 
71
                        waitpid(child, &status, 0);
 
72
 
 
73
                        return status;
 
74
                }
 
75
        }
 
76
        else
 
77
        {
 
78
                return test_main();
 
79
        }
 
80
}