summaryrefslogtreecommitdiff
path: root/erts/emulator/test/driver_SUITE_data/lots_of_fds_used_wrapper.c
blob: 34d84827d5818c95d228fcddc3275c0bdbf9f221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#if !defined(__WIN32__)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#endif

int
main(int argc, char *argv[])
{
#if !defined(__WIN32__)

    char **exec_argv;
    int fds[12000];
    int max = sizeof(fds)/sizeof(fds[0]);
    int i;

    /* Open a bit more than 1024 file descriptors... */
    for (i = 0; i < max; i++) {
        fds[i] = open("/dev/null", 0, O_WRONLY);
        if (fds[i] < 0) {
            if (i < 1200)
                return 17; /* Not enough fds for the test... */
            max = i;
            break;
        }
    }

    /*
     * Close some of the latest fds to give room for
     * the emulators usage...
     */
    for (i = max-150; i < max; i++)
        close(fds[i]);

    if (argc < 2)
        return 1;

    /*
     * Ensure NULL pointer after last argument...
     */
    exec_argv = malloc(sizeof(char *)*argc);
    if (!exec_argv)
        return 2;

    for (i = 0; i < argc-1; i++) {
        /* printf("arg=%d: %s\n", i, argv[i+1]); */
        exec_argv[i] = argv[i+1];
    }
    exec_argv[i] = NULL;

    execvp(exec_argv[0], exec_argv);

    perror("Failed to exec");

#endif

    return 3;
}