Skip to content

Commit

Permalink
nuttx/tasks.cpp: Add px4_exec to tasks.cpp (move it from cdcacm check)
Browse files Browse the repository at this point in the history
Add a generic px4 exec function to spawn processes either via:
- builtin list if configuration does not use file apps
- posix_spawn if configuration uses file apps
  • Loading branch information
pussuw committed Nov 2, 2023
1 parent 930c673 commit 9c28f6e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 52 deletions.
6 changes: 6 additions & 0 deletions platforms/common/include/px4_platform_common/tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,10 @@ __EXPORT int px4_prctl(int option, const char *arg2, px4_task_t pid);
/** return the name of the current task */
__EXPORT const char *px4_get_taskname(void);

/** Execute a named task from filesystem */
__EXPORT int px4_exec(const char *appname,
char *const *argv,
const char *redirfile,
int oflags);

__END_DECLS
56 changes: 4 additions & 52 deletions platforms/nuttx/src/px4/common/cdc_acm_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,16 @@ __BEGIN_DECLS
#include <syslog.h>
#include <nuttx/config.h>
#include <nuttx/wqueue.h>
#include <builtin/builtin.h>

#include <termios.h>
#include <sys/ioctl.h>
#include <sys/boardctl.h>
#include <fcntl.h>
#include <libgen.h>
#include <spawn.h>

#include <stdio.h>

__END_DECLS

#include <px4_platform_common/shutdown.h>
#include <px4_platform_common/tasks.h>

#include <uORB/Subscription.hpp>
#include <uORB/topics/actuator_armed.h>
Expand Down Expand Up @@ -118,49 +114,6 @@ static int serial_disconnect(void)
return 0;
}

static int exec_wrap(const char *appname, char *const *argv, const char *redirfile, int oflags)
{
#ifdef CONFIG_BUILTIN
return exec_builtin(appname, argv, redirfile, oflags);
#else
char path[CONFIG_PATH_MAX];
posix_spawn_file_actions_t file_actions;
posix_spawnattr_t attr;
pid_t pid;
int ret;

/* We launch processes from the /bin/ folder only */

sprintf(path, "/bin/");
strcat(path, basename((char *)appname));

/* Return ERROR if spawn fails */

pid = (pid_t)ERROR;

/* Initialize the attributes file actions structure */

ret = posix_spawn_file_actions_init(&file_actions);

if (ret != 0) {
goto errout;
}

ret = posix_spawnattr_init(&attr);

if (ret != 0) {
goto errout;
}

ret = posix_spawnp(&pid, path, &file_actions, &attr, argv, environ);

errout:
posix_spawn_file_actions_destroy(&file_actions);
posix_spawnattr_destroy(&attr);

return (int)pid;
#endif
}

static void mavlink_usb_check(void *arg)
{
Expand Down Expand Up @@ -381,7 +334,7 @@ static void mavlink_usb_check(void *arg)
else if (launch_passthru) {
sched_lock();
exec_argv = (char **)gps_argv;
exec_wrap(exec_argv[0], exec_argv, nullptr, 0);
px4_exec(exec_argv[0], exec_argv, nullptr, 0);
sched_unlock();
exec_argv = (char **)passthru_argv;
}
Expand All @@ -390,7 +343,7 @@ static void mavlink_usb_check(void *arg)

sched_lock();

if (exec_wrap(exec_argv[0], exec_argv, nullptr, 0) > 0) {
if (px4_exec(exec_argv[0], exec_argv, nullptr, 0) > 0) {
usb_auto_start_state = UsbAutoStartState::connected;

} else {
Expand Down Expand Up @@ -419,7 +372,7 @@ static void mavlink_usb_check(void *arg)
if (!vbus_present && !vbus_present_prev) {
sched_lock();
static const char *stop_argv[] {"mavlink", "stop", "-d", USB_DEVICE_PATH, NULL};
exec_wrap(stop_argv[0], (char **)stop_argv, NULL, 0);
px4_exec(stop_argv[0], (char **)stop_argv, NULL, 0);
sched_unlock();

usb_auto_start_state = UsbAutoStartState::disconnecting;
Expand All @@ -442,7 +395,6 @@ static void mavlink_usb_check(void *arg)
}
}


void cdcacm_init(void)
{
#ifdef CONFIG_BUILD_KERNEL
Expand Down
70 changes: 70 additions & 0 deletions platforms/nuttx/src/px4/common/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

#include <px4_platform/task.h>

#include <nuttx/config.h>
#include <nuttx/board.h>
#include <nuttx/kthread.h>

Expand All @@ -56,6 +57,10 @@
#include <sched.h>
#include <errno.h>
#include <stdbool.h>
#include <spawn.h>
#include <libgen.h>

#include <builtin/builtin.h>

int px4_task_spawn_cmd(const char *name, int scheduler, int priority, int stack_size, main_t entry, char *const argv[])
{
Expand Down Expand Up @@ -101,3 +106,68 @@ const char *px4_get_taskname(void)
{
return getprogname();
}

int px4_exec(const char *appname, char *const *argv, const char *redirfile, int oflags)
{
#ifdef CONFIG_BUILTIN
return exec_builtin(appname, argv, redirfile, oflags);
#else
char path[CONFIG_PATH_MAX];
posix_spawn_file_actions_t file_actions;
posix_spawnattr_t attr;
pid_t pid;
int ret;

/* We launch processes from the /bin/ folder only */

sprintf(path, "/bin/");
strcat(path, basename((char *)appname));

/* Initialize the attributes */

ret = posix_spawnattr_init(&attr);

if (ret != 0) {
goto errout;
}

/* Initialize the file actions structure */

ret = posix_spawn_file_actions_init(&file_actions);

if (ret != 0) {
goto errout_with_attrs;
}

/* Redirect output if instructed to do so */

if (redirfile) {
ret = posix_spawn_file_actions_addopen(&file_actions, 1, redirfile, oflags, 0644);

if (ret != 0) {
goto errout_with_actions;
}
}

/* Attempt to load the executable */

ret = posix_spawnp(&pid, path, &file_actions, &attr, argv, environ);

if (ret != 0) {
goto errout_with_actions;
}

posix_spawn_file_actions_destroy(&file_actions);
posix_spawnattr_destroy(&attr);
return pid;

errout_with_actions:
posix_spawn_file_actions_destroy(&file_actions);

errout_with_attrs:
posix_spawnattr_destroy(&attr);

errout:
return ERROR;
#endif
}

0 comments on commit 9c28f6e

Please sign in to comment.