File: solaris_process_test.c

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (178 lines) | stat: -rw-r--r-- 3,909 bytes parent folder | download | duplicates (6)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <test.h>

#include <compiler.h>
#include <process_lib.h>
#include <process_unix_priv.h>

/*
 * procfs.h is not 64-bit off_t clean, but the only affected structure is
 * priovec, which we don't use. Hence we may work around #error in sys/procfs.h
 * by lying that we are not compiling with large file support (while we do).
 */
#define _FILE_OFFSET_BITS 32

#include <procfs.h>

int open(const char *filename, int flags, ...)
{
    if (!strcmp(filename, "/proc/1/psinfo"))
    {
        return 1;
    }

    if (!strcmp(filename, "/proc/1/status"))
    {
        return 101;
    }

    if (!strcmp(filename, "/proc/2/status"))
    {
        return 102;
    }

    if (!strcmp(filename, "/proc/3/status"))
    {
        return 103;
    }

    errno = ENOENT;
    return -1;
}

int fdpos[4];

ssize_t read(int fd, void *buf, size_t bufsize)
{
    if (fd == 1)
    {
        if (fdpos[0] == 0)
        {
            psinfo_t psinfo;
            psinfo.pr_start.tv_sec = 100;
            memcpy(buf, &psinfo, sizeof(psinfo));
            fdpos[0] = sizeof(psinfo);
            return sizeof(psinfo);
        }
        else
        {
            return 0;
        }
    }

    if (fd == 101)
    {
        if (fdpos[1] == 0)
        {
            pstatus_t pstatus;
            pstatus.pr_nlwp = 1;
            pstatus.pr_lwp.pr_flags = PR_STOPPED;
            pstatus.pr_lwp.pr_why = PR_SIGNALLED;
            memcpy(buf, &pstatus, sizeof(pstatus));
            fdpos[1] = sizeof(pstatus);
            return sizeof(pstatus);
        }
        else
        {
            return 0;
        }
    }

    if (fd == 102)
    {
        if (fdpos[2] == 0)
        {
            pstatus_t pstatus;
            pstatus.pr_nlwp = 1;
            pstatus.pr_lwp.pr_flags = 0;
            pstatus.pr_lwp.pr_why = 0;
            memcpy(buf, &pstatus, sizeof(pstatus));
            fdpos[2] = sizeof(pstatus);
            return sizeof(pstatus);
        }
        else
        {
            return 0;
        }
    }

    if (fd == 103)
    {
        if (fdpos[3] == 0)
        {
            /* Currently this is unused, the test is switched off. */

            pstatus_t pstatus;
            pstatus.pr_nlwp = 0;                                /* zombie */
            memcpy(buf, &pstatus, sizeof(pstatus));
            fdpos[3] = sizeof(pstatus);
            return sizeof(pstatus);
        }
        else
        {
            return 0;
        }
    }

    errno = EIO;
    return -1;
}

int close(int fd)
{
    return 0;
}

static void test_get_start_time_process1(void)
{
    time_t t = GetProcessStartTime(1);
    assert_int_equal(t, 100);
}

static void test_get_start_time_process2(void)
{
    time_t t = GetProcessStartTime(2);
    assert_int_equal(t, PROCESS_START_TIME_UNKNOWN);
}

static void test_get_state_process1(void)
{
    ProcessState s = GetProcessState(1);
    assert_int_equal(s, PROCESS_STATE_STOPPED);
}

static void test_get_state_process2(void)
{
    ProcessState s = GetProcessState(2);
    assert_int_equal(s, PROCESS_STATE_RUNNING);
}

static void test_get_state_process3(void)
{
    ProcessState s = GetProcessState(3);
    assert_int_equal(s, PROCESS_STATE_ZOMBIE);
}

static void test_get_state_process4(void)
{
    ProcessState s = GetProcessState(4);
    assert_int_equal(s, PROCESS_STATE_DOES_NOT_EXIST);
}

int main()
{
    PRINT_TEST_BANNER();

    const UnitTest tests[] =
    {
        unit_test(test_get_start_time_process1),
        unit_test(test_get_start_time_process2),
        unit_test(test_get_state_process1),
        unit_test(test_get_state_process2),
        /* TODO zombie process unit test for solaris is not currently working
         * because of the huge amount of hacks needed to actually figure out
         * if a process is zombie, see libpromises/process_solaris.c. */
//        unit_test(test_get_state_process3),
    };

    return run_tests(tests);
}