OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [test0/] [src/] [exectest.c] - Blame information for rev 6

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Execve test.
3
 */
4
#include <stdio.h>
5
#include <unistd.h>
6
#include <tests.h>
7
#include <sys/types.h>
8
#include <sys/stat.h>
9
#include <fcntl.h>
10
#include <string.h>
11
#include <errno.h>
12
#include <alloca.h>
13
#include <l4lib/ipcdefs.h>
14
 
15
extern char _start_test_exec[];
16
extern char _end_test_exec[];
17
 
18
int exectest(pid_t parent_of_all)
19
{
20
        int fd, err;
21
        void *exec_start = (void *)_start_test_exec;
22
        unsigned long size = _end_test_exec - _start_test_exec;
23
        char filename[128];
24
        char env_string1[30];
25
        char env_string2[30];
26
        int left, cnt;
27
        char *argv[5];
28
        char *envp[3];
29
 
30
        memset(filename, 0, 128);
31
        sprintf(filename, "/home/bahadir/execfile%d", getpid());
32
 
33
        /* First create a new file and write the executable data to that file */
34
        if ((fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0) {
35
                err = errno;
36
                test_printf("OPEN: %d, for %s\n", errno, filename);
37
 
38
                /*
39
                 * If it is a minor error like EEXIST,
40
                 * create one with different name
41
                 */
42
                if (errno == EEXIST) {
43
                        sprintf(filename, "/home/bahadir/execfile%d-2",
44
                                getpid());
45
                        if ((fd = open(filename,
46
                                       O_RDWR | O_CREAT | O_TRUNC, S_IRWXU))
47
                                       < 0) {
48
                                printf("OPEN: %d, failed twice, "
49
                                       "last time for %s\n",
50
                                       errno, filename);
51
                                goto out_err;
52
                        }
53
                } else
54
                        goto out_err;
55
        }
56
 
57
        left = size;
58
        test_printf("Writing %x bytes to %s\n", left, filename);
59
        while (left != 0) {
60
                if ((cnt = write(fd, exec_start, left)) < 0)
61
                        goto out_err;
62
                left -= cnt;
63
        }
64
 
65
        if ((err = close(fd)) < 0) {
66
                goto out_err;
67
        }
68
 
69
#if 0
70
        /* Reopen */
71
        if ((fd = open(filename, O_RDONLY, S_IRWXU)) < 0) {
72
                err = errno;
73
                test_printf("OPEN: %d, for %s\n", errno, filename);
74
 
75
        }
76
 
77
        printf("Reopened %s\n", filename);
78
        buf = alloca(PAGE_SIZE);
79
        read(fd, buf, PAGE_SIZE);
80
 
81
        if (memcmp(exec_start, buf, PAGE_SIZE))
82
                printf("First page of read and written file doesn't match\n");
83
        else
84
                printf("First page matches.\n");
85
#endif
86
        /* Set up some arguments */
87
        argv[0] = "FIRST ARG";
88
        argv[1] = "SECOND ARG";
89
        argv[2] = "THIRD ARG";
90
        argv[3] = "FOURTH ARG";
91
        argv[4] = 0;
92
 
93
        memset(env_string1, 0, 30);
94
        memset(env_string2, 0, 30);
95
        sprintf(env_string1, "parent_of_all=%d", parent_of_all);
96
        sprintf(env_string2, "pagerid=%d", pagerid);
97
 
98
        envp[0] = env_string1;
99
        envp[1] = env_string2;
100
        envp[2] = 0; /* This is important as the array needs to end with a null */
101
 
102
        /* Execute the file */
103
        err = execve(filename, argv, envp);
104
 
105
out_err:
106
        printf("EXECVE failed with %d\n", err);
107
        printf("EXECVE TEST      -- FAILED --\n");
108
        return 0;
109
}
110
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.