You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/04/05 16:54:41 UTC

[GitHub] [incubator-nuttx] v01d opened a new issue #3342: DAEMONIZE ELF PROGRAM

v01d opened a new issue #3342:
URL: https://github.com/apache/incubator-nuttx/issues/3342


   ```
     Description: It is a common practice to "daemonize" to detach a task from
                  its parent.  This is used with NSH, for example, so that NSH
                  will not stall, waiting in waitpid() for the child task to
                  exit.
   
                  Daemonization is done to creating a new task which continues
                  to run while the original task exits (sending the SIGCHLD
                  signal to the parent and awakening waitpid()).  In a pure
                  POSIX system, this is down with fork(), perhaps like:
   
                    if (fork() != 0)
                      {
                        exit();
                      }
   
                  but is usually done with task_create() in NuttX.  But when
                  task_create() is called from within an ELF program, a very
                  perverse situation is created:
   
                  The basic problem involves address environments and task groups:
                  "Task groups" are emulations of Linux processes.  For the
                  case of the FLAT, ELF module, the address environment is
                  allocated memory that contains the ELF module.
   
                  When you call task_create() from the ELF program, you now
                  have two task groups running in the same address environment.
                  That is a perverse situation for which there is no standard
                  solution.  There is nothing comparable to that.  Even in
                  Linux, fork() creates another address environment (although
                  it is an exact copy of the original).
   
                  When the ELF program was created, the function exec() in
                  binfmt/binfmt_exec.c runs.  It sets up a call back that will
                  be invoked when the ELF program exits.
   
                  When ELF program exits, the address environment is destroyed
                  and the other task running in the same address environment is
                  then running in stale memory and will eventually crash.
   
                  Nothing special happens when the other created task running
                  in the allocated address environment exits since has no such
                  call backs.
   
                  In order to make this work you would need logic like:
   
                  1. When the ELF task calls task_create(), it would need to:
   
                    a. Detect that task_create() was called from an ELF program,
                    b. increment a reference count on the address environment, and
                    c. Set up the same exit hook for the newly created task.
   
                  2. Then when either the ELF program task or the created task
                     in the same address environment exits, it would decrement
                     the reference count.  When the last task exits, the reference
                     count would go to zero and the address environment could be
                     destroyed.
   
                  This is complex work and would take some effort and probably
                  requires redesign of existing code and interfaces to get a
                  proper, clean, modular solution.
   
     Status:      Open
     Priority:    Medium-Low.  A simple work-arounds when using NSH is to use
                  the '&' postfix to put the started ELF program into background.
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org