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>
|
2022-02-28 08:34:03 +01:00
|
|
|
#include <nuttx/fs/fs.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>
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2019-08-06 22:13:43 +02:00
|
|
|
#include "sched/sched.h"
|
2014-09-03 22:58:24 +02:00
|
|
|
#include "group/group.h"
|
2022-05-27 22:58:16 +02:00
|
|
|
#include "tls/tls.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
|
|
|
|
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;
|
2023-04-15 08:47:19 +02:00
|
|
|
group->tg_euid = rgroup->tg_euid;
|
|
|
|
group->tg_egid = rgroup->tg_egid;
|
2019-08-06 22:13:43 +02:00
|
|
|
}
|
|
|
|
#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;
|
2023-10-24 16:27:33 +02:00
|
|
|
int ret;
|
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 */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
group = 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;
|
|
|
|
}
|
2021-06-29 12:35:42 +02:00
|
|
|
#endif /* defined(CONFIG_MM_KERNEL_HEAP) */
|
|
|
|
|
2022-03-01 19:24:05 +01:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
/* Allocate space to hold GROUP_INITIAL_MEMBERS members of the group */
|
|
|
|
|
|
|
|
group->tg_members = kmm_malloc(GROUP_INITIAL_MEMBERS * sizeof(pid_t));
|
|
|
|
if (!group->tg_members)
|
|
|
|
{
|
2023-10-24 16:27:33 +02:00
|
|
|
ret = -ENOMEM;
|
2022-12-21 14:13:14 +01:00
|
|
|
goto errout_with_group;
|
2022-03-01 19:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Number of members in allocation */
|
|
|
|
|
|
|
|
group->tg_mxmembers = GROUP_INITIAL_MEMBERS;
|
|
|
|
#endif
|
|
|
|
|
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);
|
|
|
|
|
2022-02-28 08:34:03 +01:00
|
|
|
/* Initialize file descriptors for the TCB */
|
|
|
|
|
|
|
|
files_initlist(&group->tg_filelist);
|
|
|
|
|
2023-01-10 10:54:49 +01:00
|
|
|
/* Alloc task info for group */
|
2022-02-28 08:34:03 +01:00
|
|
|
|
2023-01-10 10:54:49 +01:00
|
|
|
ret = task_init_info(group);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_member;
|
|
|
|
}
|
2022-02-28 08:34:03 +01:00
|
|
|
|
2016-08-10 15:37:25 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Initialize the pthread join mutex */
|
2013-02-03 17:43:58 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&group->tg_joinlock);
|
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)
|
2022-10-18 11:09:22 +02:00
|
|
|
/* Initialize the exit/wait semaphores */
|
2016-08-10 15:37:25 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&group->tg_exitsem, 0, 0);
|
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
|
|
|
|
2022-03-01 19:24:05 +01:00
|
|
|
errout_with_member:
|
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
kmm_free(group->tg_members);
|
2021-06-29 12:35:42 +02:00
|
|
|
errout_with_group:
|
2022-02-27 13:39:43 +01:00
|
|
|
#endif
|
2022-03-01 19:24:05 +01:00
|
|
|
kmm_free(group);
|
|
|
|
return ret;
|
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:
|
2022-03-01 19:24:05 +01:00
|
|
|
* None.
|
2013-01-25 20:15:05 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2022-03-01 19:24:05 +01:00
|
|
|
void 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;
|
|
|
|
|
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
|
|
|
|
2022-04-07 14:17:21 +02:00
|
|
|
/* Allocate mm_map list if required */
|
|
|
|
|
2023-01-11 09:23:08 +01:00
|
|
|
mm_map_initialize(&group->tg_mm_map,
|
|
|
|
(tcb->cmn.flags & TCB_FLAG_TTYPE_KERNEL) != 0);
|
2022-04-07 14:17:21 +02:00
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
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
|
|
|
#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
|
|
|
}
|