summaryrefslogtreecommitdiff
path: root/test/regression/interoperability_test/test_frame/tls_test_director_test_start.c
blob: debfe66f73d53e3c172df6841f4a283e72bf7ed5 (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
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
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation                                */
/* Copyright (c) 2026 Eclipse ThreadX contributors                         */
/*                                                                         */
/* This program and the accompanying materials are made available under    */
/* the terms of the MIT License which is available at                      */
/* https://opensource.org/licenses/MIT.                                    */
/*                                                                         */
/* SPDX-License-Identifier: MIT                                            */
/***************************************************************************/

#include "tls_test_frame.h"

static void signal_handler_wait_all( int signum)
{
    /* Wait for all processes in current process group. */
    while ( -1 != wait(NULL));

    /* Raise the same signal to kill itself. */
    raise( signum);
}

static void signal_handler_kill_process_group( int signum)
{
    /* Install an one shot signal handler. */
    struct sigaction sig_act;
    sig_act.sa_handler = signal_handler_wait_all;
    sig_act.sa_flags = SA_RESETHAND;
    sigaction( signum, &sig_act, NULL);

    /* Send received signal to every process in current process group. */
    kill( 0, signum);
}

/* Run test programs. */
INT tls_test_director_test_start( TLS_TEST_DIRECTOR* director_ptr)
{
pid_t pid;
TLS_TEST_INSTANCE* iter, *iter_term, *iter_wait;
INT status = TLS_TEST_SUCCESS, exit_status = 0;
int err = 0;

    /* Check parameters. */
    return_value_if_fail( NULL != director_ptr, TLS_TEST_INVALID_POINTER);
    return_value_if_fail( 0 != director_ptr -> tls_test_registered_test_instances, TLS_TEST_NO_REGISTERED_INSTANCE);

    /* Get the first instance. */
    iter = director_ptr -> tls_test_first_instance_ptr;

    /* Loop to launch all test instances. */
    while ( NULL != iter)
    {

        /* Launch next test instance after given seconds. */
        if ( iter -> tls_test_delay)
        {
            sleep(iter -> tls_test_delay);
        }

        pid = fork();

        /* Error handle. */
        show_error_message_if_fail( -1 != pid);
        if ( -1 == pid)
        {
            /* Cleanup all running test process if fail to fork a new process for the new instance. */
            for ( iter_term = director_ptr -> tls_test_first_instance_ptr; iter_term != iter; tls_test_instance_find_next( iter_term, &iter_term))
            {
                /* Kill the process group of the test instance. */
                status = kill( - iter_term -> tls_test_instance_current_pid, SIGTERM);
                show_error_message_if_fail( -1 != status);
                if ( -1 == status)
                    continue;

                /* Get exit status of test instances. */
                status = waitpid( iter_term -> tls_test_instance_current_pid, &exit_status, 0);
                show_error_message_if_fail( -1 != status);
                if ( -1 == status)
                    continue;

                status = tls_test_instance_set_exit_status( iter_term, exit_status);
                return_value_if_fail( TLS_TEST_SUCCESS == status, status);

            } /* for iter_term */

            return TLS_TEST_UNABLE_TO_CREATE_TEST_PROCESS;
        } /* if -1 == pid */

        /* Child process. */
        else if (0 == pid)
        {

            /* Create a new process group. */
            setpgid( 0, 0);

            /* Install signal handler for SIGALRM and SIGTERM. */
            struct sigaction sig_act;
            sig_act.sa_handler = signal_handler_kill_process_group;    /* Specify signal handler. */
            sig_act.sa_flags = SA_RESETHAND;                /* Set the signal handler as a one shot handler. */
            status = sigaction( SIGALRM, &sig_act, NULL);
            return_value_if_fail( -1 != status, TLS_TEST_SYSTEM_CALL_FAILED);
            status = sigaction( SIGTERM, &sig_act, NULL);
            return_value_if_fail( -1 != status, TLS_TEST_SYSTEM_CALL_FAILED);

            /* Set timeer for the test process. */
            alarm( iter -> tls_test_timeout);

            /* Enter test entry. */
            status = iter -> tls_test_entry( iter); 

            /* Wait until all child process terminated. */
            tls_test_wait_all_child_process( NULL);

            exit( status);
        }
        /* Parent process. */
        else
        {
            /* Set the gid of the child process again. */
            setpgid( pid, pid);

            iter -> tls_test_instance_current_pid = pid;
            iter -> tls_test_instance_status |= TLS_TEST_INSTANCE_STATUS_RUNNING;
            tls_test_instance_find_next( iter, &iter);
        }
    } /* NULL != iter */

    /* Wait for all test instances. */
    iter_wait = director_ptr -> tls_test_first_instance_ptr;
    while (iter_wait != NULL && TLS_TEST_SUCCESS == ( status = tls_test_uninterruptable_wait( &pid, &exit_status)))
    {
        iter = director_ptr -> tls_test_first_instance_ptr;
        while ( NULL != iter)
        {
            if (iter -> tls_test_instance_current_pid == pid)
            {
                status = tls_test_instance_set_exit_status( iter, exit_status);
                show_error_message_if_fail( TLS_TEST_SUCCESS == status);
                if (iter -> tls_test_instance_exit_status != TLS_TEST_SUCCESS)
                {
                    err = 1;
                }

                break; /* NULL != iter */
            } /* if iter -> tls_test_instance_current_pid == pid */

            tls_test_instance_find_next( iter, &iter);
        } /* NULL != iter */

        if (err == 1)
        {
            for (iter_term = director_ptr -> tls_test_first_instance_ptr; iter_term != NULL;)
            {
                if (iter != iter_term)
                {
                    /* Kill the process group of the test instance. */
                    status = kill(-iter_term -> tls_test_instance_current_pid, SIGTERM);
                    show_error_message_if_fail(-1 != status);
                    if (-1 != status)
                    {

                        /* Get exit status of test instances. */
                        status = waitpid(iter_term -> tls_test_instance_current_pid, &exit_status, 0);
                        show_error_message_if_fail(-1 != status);
                        if (-1 != status)
                        {
                            status = tls_test_instance_set_exit_status(iter_term, exit_status);
                            return_value_if_fail(TLS_TEST_SUCCESS == status, status);
                        }
                    }
                }
                tls_test_instance_find_next( iter_term, &iter_term);
            }
            break;
        }
        tls_test_instance_find_next(iter_wait, &iter_wait);
    } /* while exited_test_intances < director_ptr -> tls_tset_registered_test_instances */

    return TLS_TEST_SUCCESS;
}