task_spawnparms.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /****************************************************************************
  2. * sched/task/task_spawnparms.c
  3. *
  4. * Copyright (C) 2013, 2015, 2017-2019 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 <semaphore.h>
  40. #include <fcntl.h>
  41. #include <spawn.h>
  42. #include <debug.h>
  43. #include <nuttx/signal.h>
  44. #include <nuttx/spawn.h>
  45. #include <nuttx/fs/fs.h>
  46. #include "task/spawn.h"
  47. #include "task/task.h"
  48. /****************************************************************************
  49. * Public Data
  50. ****************************************************************************/
  51. sem_t g_spawn_parmsem = SEM_INITIALIZER(1);
  52. #ifndef CONFIG_SCHED_WAITPID
  53. sem_t g_spawn_execsem = SEM_INITIALIZER(0);
  54. #endif
  55. struct spawn_parms_s g_spawn_parms;
  56. /****************************************************************************
  57. * Private Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Name: nxspawn_close, nxspawn_dup2, and nxspawn_open
  61. *
  62. * Description:
  63. * Implement individual file actions
  64. *
  65. * Input Parameters:
  66. * action - describes the action to be performed
  67. *
  68. * Returned Value:
  69. * posix_spawn() and posix_spawnp() will return zero on success.
  70. * Otherwise, an error number will be returned as the function return
  71. * value to indicate the error.
  72. *
  73. ****************************************************************************/
  74. static inline int nxspawn_close(FAR struct spawn_close_file_action_s *action)
  75. {
  76. /* The return value from close() is ignored */
  77. sinfo("Closing fd=%d\n", action->fd);
  78. (void)close(action->fd);
  79. return OK;
  80. }
  81. static inline int nxspawn_dup2(FAR struct spawn_dup2_file_action_s *action)
  82. {
  83. int ret;
  84. /* Perform the dup */
  85. sinfo("Dup'ing %d->%d\n", action->fd1, action->fd2);
  86. ret = dup2(action->fd1, action->fd2);
  87. if (ret < 0)
  88. {
  89. int errcode = get_errno();
  90. serr("ERROR: dup2 failed: %d\n", errcode);
  91. return -errcode;
  92. }
  93. return OK;
  94. }
  95. static inline int nxspawn_open(FAR struct spawn_open_file_action_s *action)
  96. {
  97. int fd;
  98. int ret = OK;
  99. /* Open the file */
  100. sinfo("Open'ing path=%s oflags=%04x mode=%04x\n",
  101. action->path, action->oflags, action->mode);
  102. fd = nx_open(action->path, action->oflags, action->mode);
  103. if (fd < 0)
  104. {
  105. ret = fd;
  106. serr("ERROR: open failed: %d\n", ret);
  107. }
  108. /* Does the return file descriptor happen to match the required file
  109. * descriptor number?
  110. */
  111. else if (fd != action->fd)
  112. {
  113. /* No.. dup2 to get the correct file number */
  114. sinfo("Dup'ing %d->%d\n", fd, action->fd);
  115. ret = dup2(fd, action->fd);
  116. if (ret < 0)
  117. {
  118. ret = get_errno();
  119. serr("ERROR: dup2 failed: %d\n", ret);
  120. }
  121. sinfo("Closing fd=%d\n", fd);
  122. close(fd);
  123. }
  124. return ret;
  125. }
  126. /****************************************************************************
  127. * Public Functions
  128. ****************************************************************************/
  129. /****************************************************************************
  130. * Name: spawn_semtake and spawn_semgive
  131. *
  132. * Description:
  133. * Give and take semaphores
  134. *
  135. * Input Parameters:
  136. *
  137. * sem - The semaphore to act on.
  138. *
  139. * Returned Value:
  140. * None
  141. *
  142. ****************************************************************************/
  143. void spawn_semtake(FAR sem_t *sem)
  144. {
  145. int ret;
  146. do
  147. {
  148. ret = nxsem_wait(sem);
  149. DEBUGASSERT(ret == 0 || ret == -EINTR);
  150. }
  151. while (ret == -EINTR);
  152. }
  153. /****************************************************************************
  154. * Name: spawn_execattrs
  155. *
  156. * Description:
  157. * Set attributes of the new child task after it has been spawned.
  158. *
  159. * Input Parameters:
  160. *
  161. * pid - The pid of the new task.
  162. * attr - The attributes to use
  163. *
  164. * Returned Value:
  165. * Errors are not reported by this function. This is not because errors
  166. * cannot occur, but rather that the new task has already been started
  167. * so there is no graceful way to handle errors detected in this context
  168. * (unless we delete the new task and recover).
  169. *
  170. * Assumptions:
  171. * That task has been started but has not yet executed because pre-
  172. * emption is disabled.
  173. *
  174. ****************************************************************************/
  175. int spawn_execattrs(pid_t pid, FAR const posix_spawnattr_t *attr)
  176. {
  177. struct sched_param param;
  178. int ret;
  179. DEBUGASSERT(attr);
  180. /* Now set the attributes. Note that we ignore all of the return values
  181. * here because we have already successfully started the task. If we
  182. * return an error value, then we would also have to stop the task.
  183. */
  184. /* If we are only setting the priority, then call sched_setparm()
  185. * to set the priority of the of the new task.
  186. */
  187. if ((attr->flags & POSIX_SPAWN_SETSCHEDPARAM) != 0)
  188. {
  189. #ifdef CONFIG_SCHED_SPORADIC
  190. /* Get the current sporadic scheduling parameters. Those will not be
  191. * modified.
  192. */
  193. ret = nxsched_getparam(pid, &param);
  194. if (ret < 0)
  195. {
  196. return ret;
  197. }
  198. #endif
  199. /* Get the priority from the attributes */
  200. param.sched_priority = attr->priority;
  201. /* If we are setting *both* the priority and the scheduler,
  202. * then we will call nxsched_setscheduler() below.
  203. */
  204. if ((attr->flags & POSIX_SPAWN_SETSCHEDULER) == 0)
  205. {
  206. sinfo("Setting priority=%d for pid=%d\n",
  207. param.sched_priority, pid);
  208. ret = nxsched_setparam(pid, &param);
  209. if (ret < 0)
  210. {
  211. return ret;
  212. }
  213. }
  214. }
  215. /* If we are only changing the scheduling policy, then reset
  216. * the priority to the default value (the same as this thread) in
  217. * preparation for the nxsched_setscheduler() call below.
  218. */
  219. else if ((attr->flags & POSIX_SPAWN_SETSCHEDULER) != 0)
  220. {
  221. ret = nxsched_getparam(0, &param);
  222. if (ret < 0)
  223. {
  224. return ret;
  225. }
  226. }
  227. /* Are we setting the scheduling policy? If so, use the priority
  228. * setting determined above.
  229. */
  230. if ((attr->flags & POSIX_SPAWN_SETSCHEDULER) != 0)
  231. {
  232. sinfo("Setting policy=%d priority=%d for pid=%d\n",
  233. attr->policy, param.sched_priority, pid);
  234. #ifdef CONFIG_SCHED_SPORADIC
  235. /* But take the sporadic scheduler parameters from the attributes */
  236. param.sched_ss_low_priority = (int)attr->low_priority;
  237. param.sched_ss_max_repl = (int)attr->max_repl;
  238. param.sched_ss_repl_period.tv_sec = attr->repl_period.tv_sec;
  239. param.sched_ss_repl_period.tv_nsec = attr->repl_period.tv_nsec;
  240. param.sched_ss_init_budget.tv_sec = attr->budget.tv_sec;
  241. param.sched_ss_init_budget.tv_nsec = attr->budget.tv_nsec;
  242. #endif
  243. (void)nxsched_setscheduler(pid, attr->policy, &param);
  244. }
  245. return OK;
  246. }
  247. /****************************************************************************
  248. * Name: spawn_proxyattrs
  249. *
  250. * Description:
  251. * Set attributes of the proxy task before it has started the new child
  252. * task.
  253. *
  254. * Input Parameters:
  255. *
  256. * pid - The pid of the new task.
  257. * attr - The attributes to use
  258. * file_actions - The attributes to use
  259. *
  260. * Returned Value:
  261. * 0 (OK) on success; A negated errno value is returned on failure.
  262. *
  263. ****************************************************************************/
  264. int spawn_proxyattrs(FAR const posix_spawnattr_t *attr,
  265. FAR const posix_spawn_file_actions_t *file_actions)
  266. {
  267. FAR struct spawn_general_file_action_s *entry;
  268. int ret = OK;
  269. /* Check if we need to change the signal mask */
  270. if (attr != NULL && (attr->flags & POSIX_SPAWN_SETSIGMASK) != 0)
  271. {
  272. (void)nxsig_procmask(SIG_SETMASK, &attr->sigmask, NULL);
  273. }
  274. /* Were we also requested to perform file actions? */
  275. if (file_actions != NULL)
  276. {
  277. /* Yes.. Execute each file action */
  278. for (entry = (FAR struct spawn_general_file_action_s *)file_actions;
  279. entry && ret == OK;
  280. entry = entry->flink)
  281. {
  282. switch (entry->action)
  283. {
  284. case SPAWN_FILE_ACTION_CLOSE:
  285. ret = nxspawn_close((FAR struct spawn_close_file_action_s *)entry);
  286. break;
  287. case SPAWN_FILE_ACTION_DUP2:
  288. ret = nxspawn_dup2((FAR struct spawn_dup2_file_action_s *)entry);
  289. break;
  290. case SPAWN_FILE_ACTION_OPEN:
  291. ret = nxspawn_open((FAR struct spawn_open_file_action_s *)entry);
  292. break;
  293. case SPAWN_FILE_ACTION_NONE:
  294. default:
  295. serr("ERROR: Unknown action: %d\n", entry->action);
  296. ret = EINVAL;
  297. break;
  298. }
  299. }
  300. }
  301. return ret;
  302. }