Changeset 134ac5d in mainline for uspace/lib/pcut/src/os


Ignore:
Timestamp:
2014-06-06T07:54:24Z (11 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8db09e4
Parents:
eeb23f2d
Message:

Update PCUT to newest version

Location:
uspace/lib/pcut/src/os
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/pcut/src/os/generic.c

    reeb23f2d r134ac5d  
    7676static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
    7777
    78 /** Prepare for a new test. */
     78/** Prepare for a new test.
     79 *
     80 * @param test Test that is about to start.
     81 */
    7982static void before_test_start(pcut_item_t *test) {
    8083        pcut_report_test_start(test);
  • uspace/lib/pcut/src/os/helenos.c

    reeb23f2d r134ac5d  
    3434#include <stdlib.h>
    3535#include <str.h>
     36#include <str_error.h>
    3637#include <unistd.h>
    3738#include <sys/types.h>
     
    4142#include <task.h>
    4243#include <fcntl.h>
     44#include <fibril_synch.h>
    4345#include "../internal.h"
    4446
     
    6870}
    6971
     72void pcut_str_error(int error, char *buffer, int size) {
     73        const char *str = str_error(error);
     74        if (str == NULL) {
     75                str = "(strerror failure)";
     76        }
     77        str_cpy(buffer, size, str);
     78}
     79
    7080
    7181/* Forking-mode related functions. */
     
    8999static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
    90100
    91 /** Prepare for a new test. */
     101/** Prepare for a new test.
     102 *
     103 * @param test Test that is about to be run.
     104 */
    92105static void before_test_start(pcut_item_t *test) {
    93106        pcut_report_test_start(test);
     
    95108        memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
    96109        memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
     110}
     111
     112/** Mutex guard for forced_termination_cv. */
     113static fibril_mutex_t forced_termination_mutex
     114        = FIBRIL_MUTEX_INITIALIZER(forced_termination_mutex);
     115
     116/** Condition-variable for checking whether test timed-out. */
     117static fibril_condvar_t forced_termination_cv
     118        = FIBRIL_CONDVAR_INITIALIZER(forced_termination_cv);
     119
     120/** Spawned task id. */
     121static task_id_t test_task_id;
     122
     123/** Flag whether test is still running.
     124 *
     125 * This flag is used when checking whether test timed-out.
     126 */
     127static int test_running;
     128
     129/** Main fibril for checking whether test timed-out.
     130 *
     131 * @param arg Test that is currently running (pcut_item_t *).
     132 * @return EOK Always.
     133 */
     134static int test_timeout_handler_fibril(void *arg) {
     135        pcut_item_t *test = arg;
     136        int timeout_sec = pcut_get_test_timeout(test);
     137        suseconds_t timeout_us = (suseconds_t) timeout_sec * 1000 * 1000;
     138
     139        fibril_mutex_lock(&forced_termination_mutex);
     140        if (!test_running) {
     141                goto leave_no_kill;
     142        }
     143        int rc = fibril_condvar_wait_timeout(&forced_termination_cv,
     144                &forced_termination_mutex, timeout_us);
     145        if (rc == ETIMEOUT) {
     146                task_kill(test_task_id);
     147        }
     148leave_no_kill:
     149        fibril_mutex_unlock(&forced_termination_mutex);
     150        return EOK;
    97151}
    98152
     
    131185        int status = TEST_OUTCOME_PASS;
    132186
    133         task_id_t task_id;
    134         int rc = task_spawnvf(&task_id, self_path, arguments, files);
     187        int rc = task_spawnvf(&test_task_id, self_path, arguments, files);
    135188        if (rc != EOK) {
    136189                status = TEST_OUTCOME_ERROR;
     
    138191        }
    139192
     193        test_running = 1;
     194
     195        fid_t killer_fibril = fibril_create(test_timeout_handler_fibril, test);
     196        if (killer_fibril == 0) {
     197                /* FIXME: somehow announce this problem. */
     198                task_kill(test_task_id);
     199        } else {
     200                fibril_add_ready(killer_fibril);
     201        }
     202
    140203        task_exit_t task_exit;
    141204        int task_retval;
    142         rc = task_wait(task_id, &task_exit, &task_retval);
     205        rc = task_wait(test_task_id, &task_exit, &task_retval);
    143206        if (rc != EOK) {
    144207                status = TEST_OUTCOME_ERROR;
     
    151214        }
    152215
     216        fibril_mutex_lock(&forced_termination_mutex);
     217        test_running = 0;
     218        fibril_condvar_signal(&forced_termination_cv);
     219        fibril_mutex_unlock(&forced_termination_mutex);
     220
    153221        read_all(tempfile, extra_output_buffer, OUTPUT_BUFFER_SIZE);
    154222
  • uspace/lib/pcut/src/os/stdc.c

    reeb23f2d r134ac5d  
    5454        return strchr(haystack, needle);
    5555}
     56
     57void pcut_str_error(int error, char *buffer, int size) {
     58        const char *str = strerror(error);
     59        if (str == NULL) {
     60                str = "(strerror failure)";
     61        }
     62        strncpy(buffer, str, size - 1);
     63        /* Ensure correct termination. */
     64        buffer[size - 1] = 0;
     65}
  • uspace/lib/pcut/src/os/unix.c

    reeb23f2d r134ac5d  
    3232 */
    3333
     34/** We need _POSX_SOURCE because of kill(). */
     35#define _POSIX_SOURCE
    3436#include <stdlib.h>
    3537#include <unistd.h>
    3638#include <sys/types.h>
     39#include <signal.h>
    3740#include <errno.h>
    3841#include <assert.h>
     
    5154static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
    5255
    53 /** Prepare for a new test. */
     56/** Prepare for a new test.
     57 *
     58 * @param test Test that is about to be run.
     59 */
    5460static void before_test_start(pcut_item_t *test) {
    5561        pcut_report_test_start(test);
     
    5763        memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
    5864        memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
     65}
     66
     67/** PID of the forked process running the actual test. */
     68static pid_t child_pid;
     69
     70/** Signal handler that kills the child.
     71 *
     72 * @param sig Signal number.
     73 */
     74static void kill_child_on_alarm(int sig) {
     75        PCUT_UNUSED(sig);
     76        kill(child_pid, SIGKILL);
    5977}
    6078
     
    124142
    125143        int link_stdout[2], link_stderr[2];
    126         pid_t pid;
    127144
    128145        int rc = pipe(link_stdout);
     
    141158        }
    142159
    143         pid = fork();
    144         if (pid == (pid_t)-1) {
     160        child_pid = fork();
     161        if (child_pid == (pid_t)-1) {
    145162                snprintf(error_message_buffer, OUTPUT_BUFFER_SIZE - 1,
    146163                        "fork() failed: %s.", strerror(rc));
     
    149166        }
    150167
    151         if (pid == 0) {
     168        if (child_pid == 0) {
    152169                /* We are the child. */
    153170                dup2(link_stdout[1], STDOUT_FILENO);
     
    164181        close(link_stderr[1]);
    165182
     183        signal(SIGALRM, kill_child_on_alarm);
     184        alarm(pcut_get_test_timeout(test));
     185
    166186        size_t stderr_size = read_all(link_stderr[0], extra_output_buffer, OUTPUT_BUFFER_SIZE - 1);
    167187        read_all(link_stdout[0], extra_output_buffer, OUTPUT_BUFFER_SIZE - 1 - stderr_size);
     
    169189        int status;
    170190        wait(&status);
     191        alarm(0);
    171192
    172193        rc = convert_wait_status_to_outcome(status);
Note: See TracChangeset for help on using the changeset viewer.