source: mainline/uspace/lib/pcut/src/os/helenos.c@ 79ea5af

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 79ea5af was 79ea5af, checked in by Jakub Jermar <jakub@…>, 9 years ago

Rename unlink() to vfs_unlink_path() and _vfs_unlink() to vfs_unlink()

  • Also, remove rmdir()
  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[01579ad]1/*
2 * Copyright (c) 2013 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file
30 *
31 * Implementation of platform-dependent functions for HelenOS.
32 */
33
34#include <stdlib.h>
35#include <str.h>
[134ac5d]36#include <str_error.h>
[01579ad]37#include <unistd.h>
38#include <sys/types.h>
39#include <errno.h>
40#include <assert.h>
41#include <stdio.h>
42#include <task.h>
43#include <fcntl.h>
[134ac5d]44#include <fibril_synch.h>
[79ea5af]45#include <vfs/vfs.h>
[01579ad]46#include "../internal.h"
47
48
49/* String functions. */
50
51int pcut_str_equals(const char *a, const char *b) {
52 return str_cmp(a, b) == 0;
53}
54
55
56int pcut_str_start_equals(const char *a, const char *b, int len) {
57 return str_lcmp(a, b, len) == 0;
58}
59
60int pcut_str_size(const char *s) {
61 return str_size(s);
62}
63
64int pcut_str_to_int(const char *s) {
65 int result = strtol(s, NULL, 10);
66 return result;
67}
68
69char *pcut_str_find_char(const char *haystack, const char needle) {
70 return str_chr(haystack, needle);
71}
72
[134ac5d]73void pcut_str_error(int error, char *buffer, int size) {
74 const char *str = str_error(error);
75 if (str == NULL) {
76 str = "(strerror failure)";
77 }
78 str_cpy(buffer, size, str);
79}
80
[01579ad]81
82/* Forking-mode related functions. */
83
84/** Maximum width of a test number. */
85#define MAX_TEST_NUMBER_WIDTH 24
86
87/** Maximum length of a temporary file name. */
88#define PCUT_TEMP_FILENAME_BUFFER_SIZE 128
89
90/** Maximum command-line length. */
91#define MAX_COMMAND_LINE_LENGTH 1024
92
93/** Maximum size of stdout we are able to capture. */
94#define OUTPUT_BUFFER_SIZE 8192
95
96/** Buffer for assertion and other error messages. */
97static char error_message_buffer[OUTPUT_BUFFER_SIZE];
98
99/** Buffer for stdout from the test. */
100static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
101
[134ac5d]102/** Prepare for a new test.
103 *
104 * @param test Test that is about to be run.
105 */
[01579ad]106static void before_test_start(pcut_item_t *test) {
107 pcut_report_test_start(test);
108
109 memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
110 memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
111}
112
[134ac5d]113/** Mutex guard for forced_termination_cv. */
114static fibril_mutex_t forced_termination_mutex
115 = FIBRIL_MUTEX_INITIALIZER(forced_termination_mutex);
116
117/** Condition-variable for checking whether test timed-out. */
118static fibril_condvar_t forced_termination_cv
119 = FIBRIL_CONDVAR_INITIALIZER(forced_termination_cv);
120
121/** Spawned task id. */
122static task_id_t test_task_id;
123
124/** Flag whether test is still running.
125 *
126 * This flag is used when checking whether test timed-out.
127 */
128static int test_running;
129
130/** Main fibril for checking whether test timed-out.
131 *
132 * @param arg Test that is currently running (pcut_item_t *).
133 * @return EOK Always.
134 */
135static int test_timeout_handler_fibril(void *arg) {
136 pcut_item_t *test = arg;
137 int timeout_sec = pcut_get_test_timeout(test);
138 suseconds_t timeout_us = (suseconds_t) timeout_sec * 1000 * 1000;
139
140 fibril_mutex_lock(&forced_termination_mutex);
141 if (!test_running) {
142 goto leave_no_kill;
143 }
144 int rc = fibril_condvar_wait_timeout(&forced_termination_cv,
145 &forced_termination_mutex, timeout_us);
146 if (rc == ETIMEOUT) {
147 task_kill(test_task_id);
148 }
149leave_no_kill:
150 fibril_mutex_unlock(&forced_termination_mutex);
151 return EOK;
152}
153
[01579ad]154/** Run the test as a new task and report the result.
155 *
156 * @param self_path Path to itself, that is to current binary.
157 * @param test Test to be run.
158 */
159void pcut_run_test_forking(const char *self_path, pcut_item_t *test) {
160 before_test_start(test);
161
162 char tempfile_name[PCUT_TEMP_FILENAME_BUFFER_SIZE];
163 snprintf(tempfile_name, PCUT_TEMP_FILENAME_BUFFER_SIZE - 1, "pcut_%lld.tmp", (unsigned long long) task_get_id());
164 int tempfile = open(tempfile_name, O_CREAT | O_RDWR);
165 if (tempfile < 0) {
166 pcut_report_test_done(test, TEST_OUTCOME_ERROR, "Failed to create temporary file.", NULL, NULL);
167 return;
168 }
169
170 char test_number_argument[MAX_TEST_NUMBER_WIDTH];
171 snprintf(test_number_argument, MAX_TEST_NUMBER_WIDTH, "-t%d", test->id);
172
173 const char *const arguments[3] = {
174 self_path,
175 test_number_argument,
176 NULL
177 };
178
179 int status = TEST_OUTCOME_PASS;
180
[9b20126]181 task_wait_t test_task_wait;
[bb9ec2d]182 int rc = task_spawnvf(&test_task_id, &test_task_wait, self_path, arguments,
183 fileno(stdin), tempfile, tempfile);
[01579ad]184 if (rc != EOK) {
185 status = TEST_OUTCOME_ERROR;
186 goto leave_close_tempfile;
187 }
188
[134ac5d]189 test_running = 1;
190
191 fid_t killer_fibril = fibril_create(test_timeout_handler_fibril, test);
192 if (killer_fibril == 0) {
193 /* FIXME: somehow announce this problem. */
194 task_kill(test_task_id);
195 } else {
196 fibril_add_ready(killer_fibril);
197 }
198
[01579ad]199 task_exit_t task_exit;
200 int task_retval;
[9b20126]201 rc = task_wait(&test_task_wait, &task_exit, &task_retval);
[01579ad]202 if (rc != EOK) {
203 status = TEST_OUTCOME_ERROR;
204 goto leave_close_tempfile;
205 }
206 if (task_exit == TASK_EXIT_UNEXPECTED) {
207 status = TEST_OUTCOME_ERROR;
208 } else {
209 status = task_retval == 0 ? TEST_OUTCOME_PASS : TEST_OUTCOME_FAIL;
210 }
211
[134ac5d]212 fibril_mutex_lock(&forced_termination_mutex);
213 test_running = 0;
214 fibril_condvar_signal(&forced_termination_cv);
215 fibril_mutex_unlock(&forced_termination_mutex);
216
[58898d1d]217 aoff64_t pos = 0;
218 read(tempfile, &pos, extra_output_buffer, OUTPUT_BUFFER_SIZE);
[01579ad]219
220leave_close_tempfile:
221 close(tempfile);
[79ea5af]222 vfs_unlink_path(tempfile_name);
[01579ad]223
224 pcut_report_test_done_unparsed(test, status, extra_output_buffer, OUTPUT_BUFFER_SIZE);
225}
[9b20126]226
227void pcut_hook_before_test(pcut_item_t *test) {
228 PCUT_UNUSED(test);
229
230 /* Do nothing. */
231}
Note: See TracBrowser for help on using the repository browser.