group_leave.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /****************************************************************************
  2. * sched/group/group_leave.c
  3. *
  4. * Copyright (C) 2013-2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sched.h>
  40. #include <assert.h>
  41. #include <errno.h>
  42. #include <debug.h>
  43. #include <nuttx/irq.h>
  44. #include <nuttx/fs/fs.h>
  45. #include <nuttx/net/net.h>
  46. #include <nuttx/lib/lib.h>
  47. #ifdef CONFIG_BINFMT_LOADABLE
  48. # include <nuttx/binfmt/binfmt.h>
  49. #endif
  50. #include "environ/environ.h"
  51. #include "signal/signal.h"
  52. #include "pthread/pthread.h"
  53. #include "mqueue/mqueue.h"
  54. #include "group/group.h"
  55. #ifdef HAVE_TASK_GROUP
  56. /****************************************************************************
  57. * Private Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Name: group_remove
  61. *
  62. * Description:
  63. * Remove a group from the list of groups.
  64. *
  65. * Input Parameters:
  66. * group - The group to be removed.
  67. *
  68. * Returned Value:
  69. * None.
  70. *
  71. * Assumptions:
  72. * Called during task deletion in a safe context. No special precautions
  73. * are required here.
  74. *
  75. ****************************************************************************/
  76. #if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
  77. static void group_remove(FAR struct task_group_s *group)
  78. {
  79. FAR struct task_group_s *curr;
  80. FAR struct task_group_s *prev;
  81. irqstate_t flags;
  82. /* Let's be especially careful while access the global task group list.
  83. * This is probably un-necessary.
  84. */
  85. flags = enter_critical_section();
  86. /* Find the task group structure */
  87. for (prev = NULL, curr = g_grouphead;
  88. curr && curr != group;
  89. prev = curr, curr = curr->flink);
  90. /* Did we find it? If so, remove it from the list. */
  91. if (curr)
  92. {
  93. /* Do we remove it from mid-list? Or from the head of the list? */
  94. if (prev)
  95. {
  96. prev->flink = curr->flink;
  97. }
  98. else
  99. {
  100. g_grouphead = curr->flink;
  101. }
  102. curr->flink = NULL;
  103. }
  104. leave_critical_section(flags);
  105. }
  106. #endif
  107. /****************************************************************************
  108. * Name: group_release
  109. *
  110. * Description:
  111. * Release group resources after the last member has left the group.
  112. *
  113. * Input Parameters:
  114. * group - The group to be removed.
  115. *
  116. * Returned Value:
  117. * None.
  118. *
  119. * Assumptions:
  120. * Called during task deletion in a safe context. No special precautions
  121. * are required here.
  122. *
  123. ****************************************************************************/
  124. static inline void group_release(FAR struct task_group_s *group)
  125. {
  126. /* Free all un-reaped child exit status */
  127. #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
  128. group_removechildren(group);
  129. #endif
  130. #ifndef CONFIG_DISABLE_SIGNALS
  131. /* Release pending signals */
  132. nxsig_release(group);
  133. #endif
  134. #ifndef CONFIG_DISABLE_PTHREAD
  135. /* Release pthread resources */
  136. pthread_release(group);
  137. #endif
  138. #if CONFIG_NFILE_DESCRIPTORS > 0
  139. /* Free all file-related resources now. We really need to close files as
  140. * soon as possible while we still have a functioning task.
  141. */
  142. /* Free resources held by the file descriptor list */
  143. files_releaselist(&group->tg_filelist);
  144. #if CONFIG_NFILE_STREAMS > 0
  145. /* Free resource held by the stream list */
  146. lib_stream_release(group);
  147. #endif /* CONFIG_NFILE_STREAMS */
  148. #endif /* CONFIG_NFILE_DESCRIPTORS */
  149. #if CONFIG_NSOCKET_DESCRIPTORS > 0
  150. /* Free resource held by the socket list */
  151. net_releaselist(&group->tg_socketlist);
  152. #endif /* CONFIG_NSOCKET_DESCRIPTORS */
  153. #ifndef CONFIG_DISABLE_ENVIRON
  154. /* Release all shared environment variables */
  155. env_release(group);
  156. #endif
  157. #ifndef CONFIG_DISABLE_MQUEUE
  158. /* Close message queues opened by members of the group */
  159. nxmq_release(group);
  160. #endif
  161. #if defined(CONFIG_BUILD_KERNEL) && defined(CONFIG_MM_SHM)
  162. /* Release any resource held by shared memory virtual page allocator */
  163. (void)shm_group_release(group);
  164. #endif
  165. #ifdef CONFIG_ARCH_ADDRENV
  166. /* Destroy the group address environment */
  167. (void)up_addrenv_destroy(&group->tg_addrenv);
  168. /* Mark no address environment */
  169. g_gid_current = 0;
  170. #endif
  171. #if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
  172. /* Remove the group from the list of groups */
  173. group_remove(group);
  174. #endif
  175. #ifdef HAVE_GROUP_MEMBERS
  176. /* Release the members array */
  177. if (group->tg_members)
  178. {
  179. sched_kfree(group->tg_members);
  180. group->tg_members = NULL;
  181. }
  182. #endif
  183. #if CONFIG_NFILE_STREAMS > 0 && defined(CONFIG_MM_KERNEL_HEAP)
  184. /* In a flat, single-heap build. The stream list is part of the
  185. * group structure and, hence will be freed when the group structure
  186. * is freed. Otherwise, it is separately allocated an must be
  187. * freed here.
  188. */
  189. # if defined(CONFIG_BUILD_PROTECTED)
  190. /* In the protected build, the task's stream list is always allocated
  191. * and freed from the single, global user allocator.
  192. */
  193. sched_ufree(group->tg_streamlist);
  194. # elif defined(CONFIG_BUILD_KERNEL)
  195. /* In the kernel build, the unprivileged process' stream list will be
  196. * allocated from with its per-process, private user heap. But in that
  197. * case, there is no reason to do anything here: That allocation resides
  198. * in the user heap which which be completely freed when we destroy the
  199. * process' address environment.
  200. */
  201. if ((group->tg_flags & GROUP_FLAG_PRIVILEGED) != 0)
  202. {
  203. /* But kernel threads are different in this build configuration: Their
  204. * stream lists were allocated from the common, global kernel heap and
  205. * must explicitly freed here.
  206. */
  207. sched_kfree(group->tg_streamlist);
  208. }
  209. # endif
  210. #endif
  211. #ifdef CONFIG_BINFMT_LOADABLE
  212. /* If the exiting task was loaded into RAM from a file, then we need to
  213. * lease all of the memory resource when the last thread exits the task
  214. * group.
  215. */
  216. if (group->tg_bininfo != NULL)
  217. {
  218. binfmt_exit(group->tg_bininfo);
  219. group->tg_bininfo = NULL;
  220. }
  221. #endif
  222. #if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
  223. /* If there are threads waiting for this group to be freed, then we cannot
  224. * yet free the memory resources. Instead just mark the group deleted
  225. * and wait for those threads complete their waits.
  226. */
  227. if (group->tg_nwaiters > 0)
  228. {
  229. group->tg_flags |= GROUP_FLAG_DELETED;
  230. }
  231. else
  232. #endif
  233. {
  234. /* Release the group container itself */
  235. sched_kfree(group);
  236. }
  237. }
  238. /****************************************************************************
  239. * Name: group_removemember
  240. *
  241. * Description:
  242. * Remove a member from a group.
  243. *
  244. * Input Parameters:
  245. * group - The group from which to remove the member.
  246. * pid - The member to be removed.
  247. *
  248. * Returned Value:
  249. * On success, returns the number of members remaining in the group (>=0).
  250. * Can fail only if the member is not found in the group. On failure,
  251. * returns -ENOENT
  252. *
  253. * Assumptions:
  254. * Called during task deletion and also from the reparenting logic, both
  255. * in a safe context. No special precautions are required here.
  256. *
  257. ****************************************************************************/
  258. #ifdef HAVE_GROUP_MEMBERS
  259. static inline void group_removemember(FAR struct task_group_s *group, pid_t pid)
  260. {
  261. irqstate_t flags;
  262. int i;
  263. DEBUGASSERT(group);
  264. /* Find the member in the array of members and remove it */
  265. for (i = 0; i < group->tg_nmembers; i++)
  266. {
  267. /* Does this member have the matching pid */
  268. if (group->tg_members[i] == pid)
  269. {
  270. /* Remove the member from the array of members. This must be an
  271. * atomic operation because the member array may be accessed from
  272. * interrupt handlers (read-only).
  273. */
  274. flags = enter_critical_section();
  275. group->tg_members[i] = group->tg_members[group->tg_nmembers - 1];
  276. group->tg_nmembers--;
  277. leave_critical_section(flags);
  278. }
  279. }
  280. }
  281. #endif /* HAVE_GROUP_MEMBERS */
  282. /****************************************************************************
  283. * Public Functions
  284. ****************************************************************************/
  285. /****************************************************************************
  286. * Name: group_leave
  287. *
  288. * Description:
  289. * Release a reference on a group. This function is called when a task or
  290. * thread exits. It decrements the reference count on the group. If the
  291. * reference count decrements to zero, then it frees the group and all of
  292. * resources contained in the group.
  293. *
  294. * Input Parameters:
  295. * tcb - The TCB of the task that is exiting.
  296. *
  297. * Returned Value:
  298. * None.
  299. *
  300. * Assumptions:
  301. * Called during task deletion in a safe context. No special precautions
  302. * are required here.
  303. *
  304. ****************************************************************************/
  305. #ifdef HAVE_GROUP_MEMBERS
  306. void group_leave(FAR struct tcb_s *tcb)
  307. {
  308. FAR struct task_group_s *group;
  309. DEBUGASSERT(tcb);
  310. /* Make sure that we have a group. */
  311. group = tcb->group;
  312. if (group)
  313. {
  314. /* Remove the member from group. This function may be called
  315. * during certain error handling before the PID has been
  316. * added to the group. In this case tcb->pid will be uninitialized
  317. * group_removemember() will fail.
  318. */
  319. group_removemember(group, tcb->pid);
  320. /* Have all of the members left the group? */
  321. if (group->tg_nmembers == 0)
  322. {
  323. /* Yes.. Release all of the resource held by the task group */
  324. group_release(group);
  325. }
  326. /* In any event, we can detach the group from the TCB so that we won't
  327. * do this again.
  328. */
  329. tcb->group = NULL;
  330. }
  331. }
  332. #else /* HAVE_GROUP_MEMBERS */
  333. void group_leave(FAR struct tcb_s *tcb)
  334. {
  335. FAR struct task_group_s *group;
  336. DEBUGASSERT(tcb);
  337. /* Make sure that we have a group */
  338. group = tcb->group;
  339. if (group)
  340. {
  341. /* Yes, we have a group.. Is this the last member of the group? */
  342. if (group->tg_nmembers > 1)
  343. {
  344. /* No.. just decrement the number of members in the group */
  345. group->tg_nmembers--;
  346. }
  347. /* Yes.. that was the last member remaining in the group */
  348. else
  349. {
  350. /* Release all of the resource held by the task group */
  351. group_release(group);
  352. }
  353. /* In any event, we can detach the group from the TCB so we won't do
  354. * this again.
  355. */
  356. tcb->group = NULL;
  357. }
  358. }
  359. #endif /* HAVE_GROUP_MEMBERS */
  360. #endif /* HAVE_TASK_GROUP */