Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions system/nxinit/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <spawn.h>
Expand All @@ -35,7 +36,9 @@
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>

#include <netutils/netinit.h>

Expand Down Expand Up @@ -125,6 +128,8 @@ static int cmd_rptun(FAR struct action_manager_s *am,
#endif
static int cmd_unlink(FAR struct action_manager_s *am,
int argc, FAR char **argv);
static int cmd_wait(FAR struct action_manager_s *am,
int argc, FAR char **argv);
#ifndef CONFIG_DISABLE_MOUNTPOINT
static int cmd_mount(FAR struct action_manager_s *am,
int argc, FAR char **argv);
Expand Down Expand Up @@ -194,6 +199,7 @@ static const struct cmd_map_s g_builtin[] =
{"umount", 2, 2, cmd_umount},
#endif
{"unlink", 2, 2, cmd_unlink},
{"wait", 2, 3, cmd_wait},
};

/****************************************************************************
Expand Down Expand Up @@ -485,6 +491,63 @@ static int cmd_unlink(FAR struct action_manager_s *am,
return ret;
}

/* wait <path> [<timeout>]
*
* Check for the existence of <path> with stat() every 10 ms and
* return when it is found. <timeout> is in seconds and may be
* fractional; it defaults to 5. When the timeout is reached, only
* warn and continue so a later command still runs.
*/

static int cmd_wait(FAR struct action_manager_s *am,
int argc, FAR char **argv)
{
long timeout_ms = 5000;
struct timespec start;
struct timespec now;
struct stat st;
FAR char *end;
double timeout;
long waited;

UNUSED(am);

if (argc > 2)
{
timeout = strtod(argv[2], &end);
if (end == argv[2] || *end != '\0' || !(timeout >= 0) ||
timeout > (double)LONG_MAX / 1000)
{
init_err("wait: bad timeout '%s'", argv[2]);
return -EINVAL;
}

timeout_ms = (long)(timeout * 1000);
}

clock_gettime(CLOCK_MONOTONIC, &start);
for (; ; )
{
clock_gettime(CLOCK_MONOTONIC, &now);
waited = (now.tv_sec - start.tv_sec) * 1000 +
(now.tv_nsec - start.tv_nsec) / 1000000;

if (stat(argv[1], &st) == 0)
{
init_info("wait for '%s' took %ld ms", argv[1], waited);
return 0;
}

if (waited >= timeout_ms)
{
init_warn("wait for '%s' timed out", argv[1]);
return 0;
}

usleep(10 * 1000);
}
}

#ifndef CONFIG_DISABLE_MOUNTPOINT
/* Copy the comma-separated list in options to data without the flags
* (g_mount_flags), and return the flags. options does not change, as the
Expand Down
1 change: 1 addition & 0 deletions system/nxinit/test/test_nxinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ int main(int argc, FAR char *argv[])
cmocka_unit_test(test_nxinit_action_empty_skipped),
cmocka_unit_test(test_nxinit_action_builtin_mount),
cmocka_unit_test(test_nxinit_action_builtin_pmconfig),
cmocka_unit_test(test_nxinit_action_builtin_wait),
cmocka_unit_test(test_nxinit_service_duplicate_conflict),
cmocka_unit_test(test_nxinit_service_override_replaces_duplicate),
cmocka_unit_test(test_nxinit_service_args_max_boundary),
Expand Down
1 change: 1 addition & 0 deletions system/nxinit/test/test_nxinit.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void test_nxinit_action_event_and_semantics(FAR void **state);
void test_nxinit_action_empty_skipped(FAR void **state);
void test_nxinit_action_builtin_mount(FAR void **state);
void test_nxinit_action_builtin_pmconfig(FAR void **state);
void test_nxinit_action_builtin_wait(FAR void **state);

/****************************************************************************
* Name: test_nxinit_service_*
Expand Down
129 changes: 129 additions & 0 deletions system/nxinit/test/test_nxinit_action.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/boardctl.h>
#include <sys/param.h>
Expand Down Expand Up @@ -532,3 +534,130 @@ void test_nxinit_action_builtin_pmconfig(FAR void **state)
skip();
#endif
}

#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_TMPFS)

/* Create the file of the "late" case of test_nxinit_action_builtin_wait
* while the test is blocked in "wait".
*/

static void *wait_create_late_file(FAR void *arg)
{
int fd;

UNUSED(arg);
usleep(200 * 1000);
fd = open(TEST_MNT "/wait_late", O_CREAT | O_WRONLY, 0644);
if (fd >= 0)
{
close(fd);
}

return NULL;
}

#endif

/****************************************************************************
* Name: test_nxinit_action_builtin_wait
*
* Description:
* "wait" polls for the file and returns when it exists. A missing
* file only warns and returns 0 once the optional timeout (seconds,
* default 5) is reached, and a bad timeout fails with -EINVAL.
****************************************************************************/

void test_nxinit_action_builtin_wait(FAR void **state)
{
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_TMPFS)
FAR char *mnt[] =
{
"mount", "tmpfs", "none", TEST_MNT, NULL
};

FAR char *umnt[] =
{
"umount", TEST_MNT, NULL
};

FAR char *found[] =
{
"wait", TEST_MNT "/wait_file", NULL
};

FAR char *missing[] =
{
"wait", TEST_MNT "/no_such_file", "0.1", NULL
};

FAR char *late[] =
{
"wait", TEST_MNT "/wait_late", NULL
};

FAR char *badnum[] =
{
"wait", TEST_MNT "/no_such_file", "bogus", NULL
};

FAR char *negative[] =
{
"wait", TEST_MNT "/no_such_file", "-1", NULL
};

struct action_manager_s am;
struct timespec start;
struct timespec end;
pthread_t tid;
long waited;
int fd;

action_manager_init(&am);
assert_int_equal(init_builtin_run(&am, 4, mnt), 0);

/* A missing file waits for the timeout, then warns and returns 0 */

clock_gettime(CLOCK_MONOTONIC, &start);
assert_int_equal(init_builtin_run(&am, 3, missing), 0);
clock_gettime(CLOCK_MONOTONIC, &end);
waited = (end.tv_sec - start.tv_sec) * 1000 +
(end.tv_nsec - start.tv_nsec) / 1000000;
assert_true(waited >= 90);

/* A file that shows up while waiting ends the wait */

clock_gettime(CLOCK_MONOTONIC, &start);
assert_int_equal(pthread_create(&tid, NULL, wait_create_late_file,
NULL), 0);
assert_int_equal(init_builtin_run(&am, 2, late), 0);
assert_int_equal(pthread_join(tid, NULL), 0);
clock_gettime(CLOCK_MONOTONIC, &end);
waited = (end.tv_sec - start.tv_sec) * 1000 +
(end.tv_nsec - start.tv_nsec) / 1000000;
assert_true(waited >= 150 && waited < 3000);
assert_int_equal(unlink(TEST_MNT "/wait_late"), 0);

/* A bad or negative timeout fails right away */

assert_int_equal(init_builtin_run(&am, 3, badnum), -EINVAL);
assert_int_equal(init_builtin_run(&am, 3, negative), -EINVAL);

/* An existing file returns 0 right away */

fd = open(TEST_MNT "/wait_file", O_CREAT | O_WRONLY, 0644);
assert_true(fd >= 0);
close(fd);
clock_gettime(CLOCK_MONOTONIC, &start);
assert_int_equal(init_builtin_run(&am, 2, found), 0);
clock_gettime(CLOCK_MONOTONIC, &end);
waited = (end.tv_sec - start.tv_sec) * 1000 +
(end.tv_nsec - start.tv_nsec) / 1000000;
assert_true(waited < 1000);
assert_int_equal(unlink(TEST_MNT "/wait_file"), 0);

assert_int_equal(init_builtin_run(&am, 2, umnt), 0);
action_manager_free_all(&am);
#else
skip();
#endif
}