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
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <logging.h>
#include <process_lib.h>
#include <process_unix_priv.h>
/**
* This file contains process-handling tests that need to fork() a child process
* which totally confuses the unit test framework used by the other tests.
*/
static bool failure = false;
#define assert_int_equal(expr1, expr2) \
if ((expr1) != (expr2)) \
{ \
fprintf(stderr, "FAIL: "#expr1" != "#expr2" [%jd != %jd] (%s:%d)\n", ((intmax_t) expr1), ((intmax_t) expr2), __FILE__, __LINE__); \
failure = true; \
}
#define assert_int_not_equal(expr1, expr2) \
if ((expr1) == (expr2)) \
{ \
fprintf(stderr, "FAIL: "#expr1" == "#expr2" [%jd == %jd] (%s:%d)\n", ((intmax_t) expr1), ((intmax_t) expr2), __FILE__, __LINE__); \
failure = true; \
}
#define assert_true(expr) \
if (!(expr)) \
{ \
fprintf(stderr, "FAIL: "#expr" is FALSE (%s:%d)\n", __FILE__, __LINE__); \
failure = true; \
}
#define assert_false(expr) \
if ((expr)) \
{ \
fprintf(stderr, "FAIL: "#expr" is TRUE (%s:%d)\n", __FILE__, __LINE__); \
failure = true; \
}
pid_t SPAWNED_PID;
pid_t THIS_PID;
time_t THIS_STARTTIME;
static void test_process_start_time(void)
{
/* Wait a couple of seconds so that process start time differs. */
printf("Sleeping 2 seconds...\n");
sleep(2);
pid_t new_pid = fork();
assert_true(new_pid >= 0);
if (new_pid == 0) /* child */
{
execl("/bin/sleep", "/bin/sleep", "30", NULL);
assert_true(false); /* unreachable */
}
SPAWNED_PID = new_pid;
time_t newproc_starttime = GetProcessStartTime(new_pid);
printf("Spawned a \"sleep\" child with PID %jd and start_time %jd\n",
(intmax_t) new_pid, (intmax_t) newproc_starttime);
// We might have slipped by a few seconds, but shouldn't be much.
assert_int_not_equal(newproc_starttime, PROCESS_START_TIME_UNKNOWN);
assert_true(newproc_starttime >= THIS_STARTTIME + 1);
assert_true(newproc_starttime <= THIS_STARTTIME + 15);
kill(new_pid, SIGKILL);
wait(NULL);
SPAWNED_PID = 0;
}
static void test_process_state(void)
{
int ret;
pid_t new_pid = fork();
assert_true(new_pid >= 0);
if (new_pid == 0) /* child */
{
execl("/bin/sleep", "/bin/sleep", "30", NULL);
assert_true(false); /* unreachable */
}
SPAWNED_PID = new_pid;
printf("Spawned a \"sleep\" child with PID %d\n", new_pid);
int state = -1000;
for (int c = 0; c < 10; c++)
{
state = GetProcessState(new_pid);
if (state == PROCESS_STATE_RUNNING)
{
break;
}
else
{
usleep(200000);
}
}
printf("Started, state: %d\n", state);
assert_int_equal(state, PROCESS_STATE_RUNNING);
ret = kill(new_pid, SIGSTOP);
assert_int_equal(ret, 0);
for (int c = 0; c < 10; c++)
{
state = GetProcessState(new_pid);
if (state == PROCESS_STATE_STOPPED)
{
break;
}
else
{
usleep(200000);
}
}
printf("Stopped, state: %d\n", state);
assert_int_equal(state, PROCESS_STATE_STOPPED);
ret = kill(new_pid, SIGCONT);
assert_int_equal(ret, 0);
for (int c = 0; c < 10; c++)
{
state = GetProcessState(new_pid);
if (state == PROCESS_STATE_RUNNING)
{
break;
}
else
{
usleep(200000);
}
}
printf("Resumed, state: %d\n", state);
assert_int_equal(state, PROCESS_STATE_RUNNING);
/* Terminate the child process and reap the zombie. */
kill(new_pid, SIGKILL);
wait(NULL);
state = GetProcessState(new_pid);
printf("Killed, state: %d\n", state);
assert_int_equal(state, PROCESS_STATE_DOES_NOT_EXIST);
SPAWNED_PID = 0;
}
static void test_graceful_terminate(void)
{
int ret, state;
pid_t new_pid = fork();
assert_true(new_pid >= 0);
if (new_pid == 0) /* child */
{
execl("/bin/sleep", "/bin/sleep", "30", NULL);
assert_true(false); /* unreachable */
}
time_t start_time = GetProcessStartTime(new_pid);
SPAWNED_PID = new_pid;
printf("Spawned a \"sleep\" child with PID %jd and start_time %jd\n",
(intmax_t) new_pid, (intmax_t) start_time);
state = GetProcessState(new_pid);
assert_int_equal(state, PROCESS_STATE_RUNNING);
printf("Killing child with wrong start_time, child should not die...\n");
ret = GracefulTerminate(new_pid, 12345); /* fake start time */
assert_false(ret);
state = GetProcessState(new_pid);
assert_int_equal(state, PROCESS_STATE_RUNNING);
printf("Killing child with correct start_time, child should die...\n");
ret = GracefulTerminate(new_pid, start_time);
assert_true(ret);
state = GetProcessState(new_pid);
assert_int_equal(state, PROCESS_STATE_ZOMBIE);
wait(NULL); /* reap child */
state = GetProcessState(new_pid);
assert_int_equal(state, PROCESS_STATE_DOES_NOT_EXIST);
printf("Child Dead!\n");
SPAWNED_PID = 0;
printf("Killing ourself, should fail...\n");
ret = GracefulTerminate(THIS_PID, THIS_STARTTIME);
assert_false(ret);
printf("Killing ourself without specifying starttime, should fail...\n");
ret = GracefulTerminate(THIS_PID, PROCESS_START_TIME_UNKNOWN);
assert_false(ret);
}
int main()
{
puts("==================================================");
puts("Starting test process_test.c");
puts("==================================================");
/* Don't miss the messages about GetProcessStartTime not implemented. */
LogSetGlobalLevel(LOG_LEVEL_DEBUG);
THIS_PID = getpid();
THIS_STARTTIME = GetProcessStartTime(THIS_PID);
printf("This parent process has PID %jd and start_time %jd\n",
(intmax_t) THIS_PID, (intmax_t) THIS_STARTTIME);
test_process_start_time();
test_process_state();
test_graceful_terminate();
/* Make sure no child is alive. */
if (SPAWNED_PID > 0)
{
kill(SPAWNED_PID, SIGKILL);
}
return failure ? 1 : 0;
}
|