2011-03-10 15:42:28 +01:00
|
|
|
/****************************************************************************
|
2012-12-23 21:22:41 +01:00
|
|
|
* apps/builtin/exec_builtin.c
|
2011-03-10 15:42:28 +01:00
|
|
|
*
|
2021-06-07 16:27:53 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-10 15:42:28 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
#include <spawn.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "builtin/builtin.h"
|
2011-03-10 15:42:28 +01:00
|
|
|
|
2013-01-15 22:01:37 +01:00
|
|
|
/****************************************************************************
|
2013-02-02 20:32:46 +01:00
|
|
|
* Public Functions
|
2013-01-15 22:01:37 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-02-02 20:32:46 +01:00
|
|
|
* Name: exec_builtin
|
2013-01-15 22:01:37 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2013-02-02 20:32:46 +01:00
|
|
|
* Executes builtin applications registered during 'make context' time.
|
|
|
|
* New application is run in a separate task context (and thread).
|
|
|
|
*
|
|
|
|
* Input Parameter:
|
2024-08-06 23:29:50 +02:00
|
|
|
* filename - Name of the linked-in binary to be started.
|
|
|
|
* argv - Argument list
|
|
|
|
* redirfile_in - If input is redirected, this parameter will be non-NULL
|
|
|
|
* and will provide the full path to the file.
|
|
|
|
* redirfile_out - If output is redirected, this parameter will be non-NULL
|
|
|
|
* and will provide the full path to the file.
|
|
|
|
* oflags - If output is redirected, this parameter will provide the
|
|
|
|
* open flags to use. This will support file replacement
|
|
|
|
* of appending to an existing file.
|
2013-01-15 22:01:37 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2013-02-02 20:32:46 +01:00
|
|
|
* This is an end-user function, so it follows the normal convention:
|
2021-06-05 18:36:13 +02:00
|
|
|
* Returns the PID of the exec'ed module. On failure, it returns
|
2013-02-02 20:32:46 +01:00
|
|
|
* -1 (ERROR) and sets errno appropriately.
|
2013-01-15 22:01:37 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
int exec_builtin(FAR const char *appname, FAR char * const *argv,
|
2024-08-06 23:29:50 +02:00
|
|
|
FAR const char *redirfile_in, FAR const char *redirfile_out,
|
|
|
|
int oflags)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2013-02-02 20:32:46 +01:00
|
|
|
FAR const struct builtin_s *builtin;
|
|
|
|
posix_spawnattr_t attr;
|
|
|
|
posix_spawn_file_actions_t file_actions;
|
|
|
|
struct sched_param param;
|
|
|
|
pid_t pid;
|
|
|
|
int index;
|
2013-01-15 22:01:37 +01:00
|
|
|
int ret;
|
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
/* Verify that an application with this name exists */
|
2013-01-17 15:43:55 +01:00
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
index = builtin_isavail(appname);
|
|
|
|
if (index < 0)
|
|
|
|
{
|
|
|
|
ret = ENOENT;
|
|
|
|
goto errout_with_errno;
|
2013-01-17 15:43:55 +01:00
|
|
|
}
|
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
/* Get information about the builtin */
|
2013-01-15 22:01:37 +01:00
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
builtin = builtin_for_index(index);
|
|
|
|
if (builtin == NULL)
|
2014-04-14 00:24:28 +02:00
|
|
|
{
|
2013-02-02 20:32:46 +01:00
|
|
|
ret = ENOENT;
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2013-01-15 22:01:37 +01:00
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
/* Initialize attributes for task_spawn(). */
|
2013-01-15 22:01:37 +01:00
|
|
|
|
2013-02-03 00:56:54 +01:00
|
|
|
ret = posix_spawnattr_init(&attr);
|
2013-02-02 20:32:46 +01:00
|
|
|
if (ret != 0)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2013-02-02 20:32:46 +01:00
|
|
|
goto errout_with_errno;
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
2013-02-03 00:56:54 +01:00
|
|
|
ret = posix_spawn_file_actions_init(&file_actions);
|
2013-02-02 20:32:46 +01:00
|
|
|
if (ret != 0)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2013-02-03 00:56:54 +01:00
|
|
|
goto errout_with_attrs;
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
/* Set the correct task size and priority */
|
2013-01-15 22:01:37 +01:00
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
param.sched_priority = builtin->priority;
|
|
|
|
ret = posix_spawnattr_setschedparam(&attr, ¶m);
|
|
|
|
if (ret != 0)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2013-02-03 00:56:54 +01:00
|
|
|
goto errout_with_actions;
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
2022-10-31 05:37:50 +01:00
|
|
|
ret = posix_spawnattr_setstacksize(&attr, builtin->stacksize);
|
2013-02-02 20:32:46 +01:00
|
|
|
if (ret != 0)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2013-02-03 00:56:54 +01:00
|
|
|
goto errout_with_actions;
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
/* If robin robin scheduling is enabled, then set the scheduling policy
|
|
|
|
* of the new task to SCHED_RR before it has a chance to run.
|
|
|
|
*/
|
2013-01-15 22:01:37 +01:00
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
#if CONFIG_RR_INTERVAL > 0
|
|
|
|
ret = posix_spawnattr_setschedpolicy(&attr, SCHED_RR);
|
|
|
|
if (ret != 0)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2013-02-03 00:56:54 +01:00
|
|
|
goto errout_with_actions;
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
ret = posix_spawnattr_setflags(&attr,
|
|
|
|
POSIX_SPAWN_SETSCHEDPARAM |
|
|
|
|
POSIX_SPAWN_SETSCHEDULER);
|
|
|
|
if (ret != 0)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2013-02-03 00:56:54 +01:00
|
|
|
goto errout_with_actions;
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
2015-07-24 18:19:29 +02:00
|
|
|
|
2013-01-17 21:25:32 +01:00
|
|
|
#else
|
2013-02-02 20:32:46 +01:00
|
|
|
ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDPARAM);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2013-02-03 00:56:54 +01:00
|
|
|
goto errout_with_actions;
|
2013-02-02 20:32:46 +01:00
|
|
|
}
|
2015-07-24 18:19:29 +02:00
|
|
|
|
2013-01-18 02:52:42 +01:00
|
|
|
#endif
|
|
|
|
|
2024-08-06 23:29:50 +02:00
|
|
|
/* Is input being redirected? */
|
|
|
|
|
|
|
|
if (redirfile_in)
|
|
|
|
{
|
|
|
|
/* Set up to close open redirfile and set to stdin (0) */
|
|
|
|
|
|
|
|
ret = posix_spawn_file_actions_addopen(&file_actions, 0,
|
|
|
|
redirfile_in, O_RDONLY, 0);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
serr("ERROR: posix_spawn_file_actions_addopen failed: %d\n", ret);
|
|
|
|
goto errout_with_actions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
/* Is output being redirected? */
|
2012-07-11 04:43:12 +02:00
|
|
|
|
2024-08-06 23:29:50 +02:00
|
|
|
if (redirfile_out)
|
2011-03-28 15:01:57 +02:00
|
|
|
{
|
2013-02-02 20:32:46 +01:00
|
|
|
/* Set up to close open redirfile and set to stdout (1) */
|
2012-07-11 04:43:12 +02:00
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
ret = posix_spawn_file_actions_addopen(&file_actions, 1,
|
2024-08-06 23:29:50 +02:00
|
|
|
redirfile_out, oflags, 0644);
|
2013-02-02 20:32:46 +01:00
|
|
|
if (ret != 0)
|
2011-03-28 15:01:57 +02:00
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
serr("ERROR: posix_spawn_file_actions_addopen failed: %d\n", ret);
|
2013-02-03 00:56:54 +01:00
|
|
|
goto errout_with_actions;
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
2011-03-28 15:01:57 +02:00
|
|
|
}
|
2012-07-11 04:43:12 +02:00
|
|
|
|
2018-08-23 19:06:15 +02:00
|
|
|
#ifdef CONFIG_LIBC_EXECFUNCS
|
2020-05-18 08:50:24 +02:00
|
|
|
/* Load and execute the application. */
|
2018-08-23 19:06:15 +02:00
|
|
|
|
2021-06-13 08:10:24 +02:00
|
|
|
ret = posix_spawn(&pid, builtin->name, &file_actions, &attr, argv, NULL);
|
2020-05-18 08:50:24 +02:00
|
|
|
if (ret != 0 && builtin->main != NULL)
|
2018-08-23 19:06:15 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Start the built-in */
|
|
|
|
|
2021-05-12 05:47:42 +02:00
|
|
|
pid = task_spawn(builtin->name, builtin->main, &file_actions,
|
2022-10-15 19:55:16 +02:00
|
|
|
&attr, argv ? &argv[1] : NULL, NULL);
|
2021-05-12 05:47:42 +02:00
|
|
|
ret = pid < 0 ? -pid : 0;
|
2018-08-23 19:06:15 +02:00
|
|
|
}
|
2013-02-02 20:32:46 +01:00
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
serr("ERROR: task_spawn failed: %d\n", ret);
|
2013-02-03 00:56:54 +01:00
|
|
|
goto errout_with_actions;
|
2013-02-02 20:32:46 +01:00
|
|
|
}
|
2012-07-11 04:43:12 +02:00
|
|
|
|
2018-08-17 18:45:06 +02:00
|
|
|
/* Free attributes and file actions. Ignoring return values in the case
|
2013-02-03 00:56:54 +01:00
|
|
|
* of an error.
|
|
|
|
*/
|
|
|
|
|
2018-08-17 18:45:06 +02:00
|
|
|
/* Return the task ID of the new task if the task was successfully
|
2013-01-15 22:01:37 +01:00
|
|
|
* started. Otherwise, ret will be ERROR (and the errno value will
|
|
|
|
* be set appropriately).
|
|
|
|
*/
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
posix_spawn_file_actions_destroy(&file_actions);
|
|
|
|
posix_spawnattr_destroy(&attr);
|
2013-02-02 20:32:46 +01:00
|
|
|
return pid;
|
|
|
|
|
2013-02-03 00:56:54 +01:00
|
|
|
errout_with_actions:
|
2020-01-02 13:09:50 +01:00
|
|
|
posix_spawn_file_actions_destroy(&file_actions);
|
2013-02-03 00:56:54 +01:00
|
|
|
|
|
|
|
errout_with_attrs:
|
2020-01-02 13:09:50 +01:00
|
|
|
posix_spawnattr_destroy(&attr);
|
2013-02-03 00:56:54 +01:00
|
|
|
|
2013-02-02 20:32:46 +01:00
|
|
|
errout_with_errno:
|
2020-05-07 22:07:08 +02:00
|
|
|
errno = ret;
|
2013-02-02 20:32:46 +01:00
|
|
|
return ERROR;
|
2011-03-10 15:42:28 +01:00
|
|
|
}
|