File: process_terminate_unix_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 (371 lines) | stat: -rw-r--r-- 7,116 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <test.h>

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

/* This mock implements single fake process with a several tunable parameters:
   - process' start time,
   - reaction time for STOP/CONT/INT/TERM/KILL signal,
   - whether SIGINT/SIGTERM are blocked,
   - does the caller have permission to send signals to fake process

   This mock also records what signals were delivered to the process to check later.
*/

/* Settings */

time_t proc_1_start_time;
time_t proc_1_reaction_time;
bool proc_1_int_blocked;
bool proc_1_term_blocked;
bool proc_1_have_access;

/* State */

time_t current_time;

bool exists;
bool stopped;

time_t signal_time;
bool has_stop;
bool has_cont;
bool has_int;
bool has_term;
bool has_kill;

/* History */

bool was_stopped;
int exit_signal;

void InitTime(void)
{
    current_time = 1;
}

void InitFakeProcess(time_t start_time, time_t reaction_time,
                     bool int_blocked, bool term_blocked, bool have_access)
{
    proc_1_start_time = start_time;
    proc_1_reaction_time = reaction_time;
    proc_1_int_blocked = int_blocked;
    proc_1_term_blocked = term_blocked;
    proc_1_have_access = have_access;

    exists = true;
    stopped = false;

    signal_time = -1;
    has_stop = false;
    has_cont = false;
    has_int = false;
    has_term = false;
    has_kill = false;

    was_stopped = false;
    exit_signal = 0;
}

time_t GetProcessStartTime(pid_t pid)
{
    assert_int_equal(pid, 1);

    if (proc_1_have_access && exists)
    {
        return proc_1_start_time;
    }
    else
    {
        return PROCESS_START_TIME_UNKNOWN;
    }
}

ProcessState GetProcessState(pid_t pid)
{
    assert_int_equal(pid, 1);

    if (!proc_1_have_access || !exists)
    {
        return PROCESS_STATE_DOES_NOT_EXIST;
    }

    if (stopped)
    {
        return PROCESS_STATE_STOPPED;
    }
    else
    {
        return PROCESS_STATE_RUNNING;
    }
}

void FakeProcessDoSignals(void)
{
    /* React immediately to STOP and CONT in order to properly do the
     * STOP-kill-CONT sequence inside SafeKill(). */
    if (has_stop)
    {
        stopped = true;
        was_stopped = true;
        has_stop = false;
    }

    if (has_cont)
    {
        stopped = false;
        has_cont = false;
    }

    /* Do NOT react to other signals if it's not the time yet. */
    if (current_time < signal_time)
    {
        return;
    }

    if (has_int)
    {
        if (!proc_1_int_blocked)
        {
            exists = false;
            exit_signal = SIGINT;
            stopped = false;
            signal_time = -1;
        }
        has_int = false;
    }

    if (has_term)
    {
        if (!proc_1_term_blocked)
        {
            exists = false;
            exit_signal = SIGTERM;
            stopped = false;
            signal_time = -1;
        }
        has_term = false;
    }

    if (has_kill)
    {
        exists = false;
        exit_signal = SIGKILL;
        stopped = false;
        signal_time = -1;

        has_kill = false;
    }

    signal_time = -1;
}

int kill(pid_t pid, int signal)
{
    assert_int_equal(pid, 1);

    if (!proc_1_have_access)
    {
        errno = EPERM;
        return -1;
    }

    if (!exists)
    {
        errno = ESRCH;
        return -1;
    }

    if (signal == 0)
    {
        return 0;
    }

    if (signal_time == -1)
    {
        signal_time = current_time + proc_1_reaction_time;
    }

    if (signal == SIGSTOP)
    {
        has_stop = true;
    }
    else if (signal == SIGCONT)
    {
        has_cont = true;
    }
    else if (signal == SIGINT)
    {
        has_int = true;
    }
    else if (signal == SIGTERM)
    {
        has_term = true;
    }
    else if (signal == SIGKILL)
    {
        has_kill = true;
    }
    else
    {
        errno = EINVAL;
        return -1;
    }

    FakeProcessDoSignals();

    return 0;
}

int nanosleep(const struct timespec *req, struct timespec *rem)
{
    time_t sleep_time;

    /* Simulate EINTR every second time */

    static bool got_eintr = false;

    if (!got_eintr)
    {
        got_eintr = true;
        sleep_time = 2 * req->tv_nsec / 3;
    }
    else
    {
        got_eintr = false;
        sleep_time = req->tv_nsec;
    }

    time_t next_time = current_time + sleep_time;
    if (signal_time != -1 && next_time >= signal_time)
    {
        FakeProcessDoSignals();
    }

    current_time = next_time;

    if (got_eintr)
    {
        rem->tv_sec = 0;
        rem->tv_nsec = req->tv_nsec - sleep_time;
        errno = EINTR;
        return -1;
    }
    else
    {
        return 0;
    }
}

/* Tests */

void test_kill_simple_process(void)
{
    InitTime();
    InitFakeProcess(12345, 100, false, false, true);

    int res = GracefulTerminate(1, 12345);
    assert_true(res);

    FakeProcessDoSignals();

    assert_false(exists);
    assert_int_equal(exit_signal, SIGINT);
}

void test_kill_wrong_process(void)
{
    InitTime();
    InitFakeProcess(66666, 100, false, false, true);

    int res = GracefulTerminate(1, 12345);
    assert_false(res);

    FakeProcessDoSignals();

    assert_true(exists);
    assert_false(stopped);
    assert_false(was_stopped); /* We should not touch this process at all */
    assert_int_equal(signal_time, (time_t)-1); /* No pending signals either */
}

void test_kill_long_reacting_signal(void)
{
    /* This process is very slow in reaction. It should not be left stopped though */
    InitTime();
    InitFakeProcess(12345, 2000000000, false, false, true);

    int res = GracefulTerminate(1, 12345);
    assert_true(res);

    FakeProcessDoSignals();

    /* This process is not even reacting to SIGKILL. */
    assert_true(exists);
}

void test_kill_no_sigint(void)
{
    /* This process blocks SIGINT */
    InitTime();
    InitFakeProcess(12345, 100, true, false, true);

    int res = GracefulTerminate(1, 12345);
    assert_true(res);

    FakeProcessDoSignals();

    assert_false(exists);
    assert_int_equal(exit_signal, SIGTERM);
}

void test_kill_no_sigint_sigterm(void)
{
    /* This process only can be killed by SIGKILL */
    InitTime();
    InitFakeProcess(12345, 100, true, true, true);

    int res = GracefulTerminate(1, 12345);
    assert_true(res);

    /* Sleep a bit so that KILL is processed. */
    current_time += 100;

    FakeProcessDoSignals();

    assert_false(exists);
    assert_int_equal(exit_signal, SIGKILL);
}

void test_kill_anothers_process(void)
{
    /* This process is not owned by killer */
    InitTime();
    InitFakeProcess(12345, 100, true, true, false);

    int res = GracefulTerminate(1, 12345);
    assert_false(res);

    FakeProcessDoSignals();

    assert_true(exists);
    assert_false(was_stopped);
}

int main()
{
    PRINT_TEST_BANNER();

    const UnitTest tests[] =
    {
        unit_test(test_kill_simple_process),
        unit_test(test_kill_wrong_process),
        unit_test(test_kill_long_reacting_signal),
        unit_test(test_kill_no_sigint),
        unit_test(test_kill_no_sigint_sigterm),
        unit_test(test_kill_anothers_process),
    };

    return run_tests(tests);
}