2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* sched/group/group_create.c
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* 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.
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01:00
|
|
|
* Included Files
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2013-01-25 18:23:38 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2016-11-03 19:42:02 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2019-08-02 18:01:30 +02:00
|
|
|
#include <nuttx/sched.h>
|
2021-06-29 12:35:42 +02:00
|
|
|
#include <nuttx/tls.h>
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2014-08-08 21:53:29 +02:00
|
|
|
#include "environ/environ.h"
|
2019-08-06 22:13:43 +02:00
|
|
|
#include "sched/sched.h"
|
2014-09-03 22:58:24 +02:00
|
|
|
#include "group/group.h"
|
2013-01-26 00:21:27 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01:00
|
|
|
* Pre-processor Definitions
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2019-08-06 22:13:43 +02:00
|
|
|
|
2013-01-25 20:15:05 +01:00
|
|
|
/* Is this worth making a configuration option? */
|
|
|
|
|
|
|
|
#define GROUP_INITIAL_MEMBERS 4
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-26 18:28:20 +01:00
|
|
|
* Public Data
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-08-26 22:54:39 +02:00
|
|
|
|
|
|
|
#if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
|
2013-01-26 18:28:20 +01:00
|
|
|
/* This is the head of a list of all group members */
|
|
|
|
|
|
|
|
FAR struct task_group_s *g_grouphead;
|
|
|
|
#endif
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01:00
|
|
|
* Private Functions
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2019-08-06 22:13:43 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: group_inherit_identity
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* All inherit the user identity from the parent task group.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* group - The new task group.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The parent of the new task is the task at the head of the assigned task
|
|
|
|
* list for the current CPU.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_USER_IDENTITY
|
|
|
|
static inline void group_inherit_identity(FAR struct task_group_s *group)
|
|
|
|
{
|
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
|
|
|
FAR struct task_group_s *rgroup = rtcb->group;
|
|
|
|
|
|
|
|
/* Inherit the user identity from the parent task group. */
|
|
|
|
|
|
|
|
DEBUGASSERT(group != NULL);
|
|
|
|
group->tg_uid = rgroup->tg_uid;
|
|
|
|
group->tg_gid = rgroup->tg_gid;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define group_inherit_identity(group)
|
|
|
|
#endif
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01:00
|
|
|
* Public Functions
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 20:15:05 +01:00
|
|
|
* Name: group_allocate
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2013-01-25 20:15:05 +01:00
|
|
|
* Create and a new task group structure for the specified TCB. This
|
|
|
|
* function is called as part of the task creation sequence. The structure
|
2014-09-03 21:49:35 +02:00
|
|
|
* allocated and zeroed, but otherwise uninitialized. The full creation
|
2013-01-25 20:15:05 +01:00
|
|
|
* of the group of a two step process: (1) First, this function allocates
|
2020-04-30 20:43:08 +02:00
|
|
|
* group structure early in the task creation sequence in order to provide
|
|
|
|
* a group container, then (2) group_initialize() is called to set up the
|
2013-01-25 20:15:05 +01:00
|
|
|
* group membership.
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-09-03 22:58:24 +02:00
|
|
|
* tcb - The tcb in need of the task group.
|
|
|
|
* ttype - Type of the thread that is the parent of the group
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-01-25 18:23:38 +01:00
|
|
|
* 0 (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during task creation in a safe context. No special precautions
|
|
|
|
* are required here.
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2014-09-03 22:58:24 +02:00
|
|
|
int group_allocate(FAR struct task_tcb_s *tcb, uint8_t ttype)
|
2013-01-25 18:23:38 +01:00
|
|
|
{
|
2013-02-03 17:43:58 +01:00
|
|
|
FAR struct task_group_s *group;
|
2021-06-29 12:35:42 +02:00
|
|
|
int ret = -ENOMEM;
|
2013-01-26 00:21:27 +01:00
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
DEBUGASSERT(tcb && !tcb->cmn.group);
|
2013-01-25 20:15:05 +01:00
|
|
|
|
|
|
|
/* Allocate the group structure and assign it to the TCB */
|
|
|
|
|
2014-09-01 01:34:44 +02:00
|
|
|
group = (FAR struct task_group_s *)kmm_zalloc(sizeof(struct task_group_s));
|
2013-02-03 17:43:58 +01:00
|
|
|
if (!group)
|
2013-01-26 00:21:27 +01:00
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:35:42 +02:00
|
|
|
#if defined(CONFIG_MM_KERNEL_HEAP)
|
2020-04-30 20:43:08 +02:00
|
|
|
/* If this group is being created for a privileged thread, then all
|
|
|
|
* elements of the group must be created for privileged access.
|
2014-09-03 21:49:35 +02:00
|
|
|
*/
|
|
|
|
|
2014-09-03 22:58:24 +02:00
|
|
|
if ((ttype & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL)
|
2014-09-03 21:49:35 +02:00
|
|
|
{
|
|
|
|
group->tg_flags |= GROUP_FLAG_PRIVILEGED;
|
|
|
|
}
|
2013-03-11 14:21:33 +01:00
|
|
|
|
2021-06-29 12:35:42 +02:00
|
|
|
# if defined(CONFIG_FILE_STREAM)
|
|
|
|
|
2013-03-11 14:21:33 +01:00
|
|
|
/* In a flat, single-heap build. The stream list is allocated with the
|
|
|
|
* group structure. But in a kernel build with a kernel allocator, it
|
|
|
|
* must be separately allocated using a user-space allocator.
|
2018-09-13 22:03:09 +02:00
|
|
|
*
|
|
|
|
* REVISIT: Kernel threads should not require a stream allocation. They
|
|
|
|
* should not be using C buffered I/O at all.
|
2013-03-11 14:21:33 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
group->tg_streamlist = (FAR struct streamlist *)
|
2014-09-03 21:49:35 +02:00
|
|
|
group_zalloc(group, sizeof(struct streamlist));
|
2013-03-11 14:21:33 +01:00
|
|
|
|
|
|
|
if (!group->tg_streamlist)
|
|
|
|
{
|
2021-06-29 12:35:42 +02:00
|
|
|
goto errout_with_group;
|
2013-03-11 14:21:33 +01:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:35:42 +02:00
|
|
|
# endif /* defined(CONFIG_FILE_STREAM) */
|
|
|
|
#endif /* defined(CONFIG_MM_KERNEL_HEAP) */
|
|
|
|
|
|
|
|
/* Alloc task info for group */
|
|
|
|
|
|
|
|
group->tg_info = (FAR struct task_info_s *)
|
|
|
|
group_zalloc(group, sizeof(struct task_info_s));
|
|
|
|
|
|
|
|
if (!group->tg_info)
|
|
|
|
{
|
|
|
|
goto errout_with_stream;
|
|
|
|
}
|
2013-03-11 14:21:33 +01:00
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
/* Attach the group to the TCB */
|
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
tcb->cmn.group = group;
|
2013-02-03 17:43:58 +01:00
|
|
|
|
2019-08-06 22:13:43 +02:00
|
|
|
/* Inherit the user identity from the parent task group */
|
|
|
|
|
|
|
|
group_inherit_identity(group);
|
|
|
|
|
2014-08-26 22:54:39 +02:00
|
|
|
/* Duplicate the parent tasks environment */
|
2013-01-26 00:21:27 +01:00
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
ret = env_dup(group);
|
2013-01-26 00:21:27 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2013-02-04 23:38:59 +01:00
|
|
|
tcb->cmn.group = NULL;
|
2021-06-29 12:35:42 +02:00
|
|
|
goto errout_with_group;
|
2013-01-26 00:21:27 +01:00
|
|
|
}
|
|
|
|
|
2016-08-10 15:37:25 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2013-02-03 17:43:58 +01:00
|
|
|
/* Initialize the pthread join semaphore */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&group->tg_joinsem, 0, 1);
|
2013-02-03 17:43:58 +01:00
|
|
|
#endif
|
|
|
|
|
2016-08-10 15:37:25 +02:00
|
|
|
#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
|
2016-11-03 19:42:02 +01:00
|
|
|
/* Initialize the exit/wait semaphores
|
|
|
|
*
|
|
|
|
* This semaphore is used for signaling and, hence, should not have
|
|
|
|
* priority inheritance enabled.
|
|
|
|
*/
|
2016-08-10 15:37:25 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&group->tg_exitsem, 0, 0);
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_set_protocol(&group->tg_exitsem, SEM_PRIO_NONE);
|
2016-08-10 15:37:25 +02:00
|
|
|
#endif
|
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
return OK;
|
2021-06-29 12:35:42 +02:00
|
|
|
|
|
|
|
errout_with_stream:
|
|
|
|
#if defined(CONFIG_FILE_STREAM) && defined(CONFIG_MM_KERNEL_HEAP)
|
|
|
|
group_free(group, group->tg_streamlist);
|
|
|
|
#endif
|
|
|
|
errout_with_group:
|
|
|
|
group_deallocate(group);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: group_deallocate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free an existing task group structure.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* group = The group structure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void group_deallocate(FAR struct task_group_s *group)
|
|
|
|
{
|
|
|
|
if (group)
|
|
|
|
{
|
|
|
|
if (group->tg_info)
|
|
|
|
{
|
|
|
|
group_free(group, group->tg_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
kmm_free(group);
|
|
|
|
}
|
2013-01-25 20:15:05 +01:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 20:15:05 +01:00
|
|
|
* Name: group_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add the task as the initial member of the group. The full creation of
|
|
|
|
* the group of a two step process: (1) First, this group structure is
|
|
|
|
* allocated by group_allocate() early in the task creation sequence, then
|
|
|
|
* (2) this function is called to set up the initial group membership.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2013-01-25 20:15:05 +01:00
|
|
|
* tcb - The tcb in need of the task group.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-01-25 20:15:05 +01:00
|
|
|
* 0 (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during task creation in a safe context. No special precautions
|
|
|
|
* are required here.
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 20:15:05 +01:00
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
int group_initialize(FAR struct task_tcb_s *tcb)
|
2013-01-25 20:15:05 +01:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
FAR struct task_group_s *group;
|
2014-08-26 22:54:39 +02:00
|
|
|
#if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
|
2013-01-26 18:28:20 +01:00
|
|
|
irqstate_t flags;
|
|
|
|
#endif
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
DEBUGASSERT(tcb && tcb->cmn.group);
|
|
|
|
group = tcb->cmn.group;
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
2013-01-25 20:15:05 +01:00
|
|
|
/* Allocate space to hold GROUP_INITIAL_MEMBERS members of the group */
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2020-04-30 20:43:08 +02:00
|
|
|
group->tg_members = kmm_malloc(GROUP_INITIAL_MEMBERS * sizeof(pid_t));
|
2013-01-25 20:15:05 +01:00
|
|
|
if (!group->tg_members)
|
2013-01-25 18:23:38 +01:00
|
|
|
{
|
2021-06-29 12:35:42 +02:00
|
|
|
group_deallocate(group);
|
2020-01-07 16:06:02 +01:00
|
|
|
tcb->cmn.group = NULL;
|
2013-01-25 18:23:38 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
/* Assign the PID of this new task as a member of the group. */
|
2013-01-25 20:15:05 +01:00
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
group->tg_members[0] = tcb->cmn.pid;
|
2013-01-25 20:15:05 +01:00
|
|
|
|
|
|
|
/* Initialize the non-zero elements of group structure and assign it to
|
|
|
|
* the tcb.
|
|
|
|
*/
|
|
|
|
|
|
|
|
group->tg_mxmembers = GROUP_INITIAL_MEMBERS; /* Number of members in allocation */
|
2013-01-26 18:28:20 +01:00
|
|
|
|
2014-08-26 22:54:39 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
|
2013-01-26 18:28:20 +01:00
|
|
|
/* Add the initialized entry to the list of groups */
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2013-01-26 18:28:20 +01:00
|
|
|
group->flink = g_grouphead;
|
|
|
|
g_grouphead = group;
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2013-01-25 20:15:05 +01:00
|
|
|
#endif
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
/* Save the ID of the main task within the group of threads. This needed
|
2020-10-29 01:12:53 +01:00
|
|
|
* for things like SIGCHLD. It ID is also saved in the TCB of the main
|
2013-02-04 23:38:59 +01:00
|
|
|
* task but is also retained in the group which may persist after the main
|
|
|
|
* task has exited.
|
|
|
|
*/
|
|
|
|
|
2021-03-03 18:11:40 +01:00
|
|
|
group->tg_pid = tcb->cmn.pid;
|
2013-02-04 23:38:59 +01:00
|
|
|
|
|
|
|
/* Mark that there is one member in the group, the main task */
|
|
|
|
|
|
|
|
group->tg_nmembers = 1;
|
2013-01-25 18:23:38 +01:00
|
|
|
return OK;
|
|
|
|
}
|