You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/01/04 03:10:59 UTC

[incubator-nuttx-apps] 01/02: apps/examples/webserver: When NSH app, allow terminating

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 819454ee6ef2e885a04fca2ff7264d17d8215c17
Author: Nathan Hartman <59...@users.noreply.github.com>
AuthorDate: Sun Jan 3 17:58:04 2021 -0500

    apps/examples/webserver: When NSH app, allow terminating
    
    The examples/webserver app can be built in two modes:
    
        (1) in standalone mode, or
        (2) as a NSH built-in app.
    
    When run in standalone mode, the webserver program is responsible for
    bringing up the network (including DHCP if configured).  Also, the
    webserver program must never exit, so if httpd fails (i.e., if
    httpd_listen() returns), webserver_main() goes into an endless loop.
    
    When run as a NSH built-in app, network bring-up is the responsibility
    of other processes and the webserver program assumes the network is
    already properly configured when it starts.  Also, if httpd_listen()
    returns, the webserver program should terminate.
    
    Prior to this change, the webserver program would *not* terminate,
    even when running as a NSH built-in app.  For example:
    
        nsh> webserver &
        webserver [6:100]
        nsh> Starting webserver
    
        nsh> kill -9 6
        nsh> webserver_main: Still running
        nsh> webserver_main: Still running
        nsh> webserver_main: Still running
        nsh> webserver_main: Still running
    
    The line "webserver_main: Still running" would be forever printed
    every 3 seconds, however httpd_listen() is no longer running and the
    webserver is not functional.
    
    This change makes the webserver play nicely when running as a NSH
    built-in app.  With this change applied:
    
        nsh> webserver &
        webserver [6:100]
        nsh> Starting webserver
    
        nsh> kill -9 6
        nsh> webserver_main: Exiting
    
    apps/examples/webserver/webserver_main.c:
    
        * main(): Infer from CONFIG_NSH_BUILTIN_APPS if this is a
          standalone program or a NSH built-in app.  (See [1], where
          similar logic was added to decide whether to do network bring-up
          or not.)  If standalone, run forever as before.  If built-in
          app, exit when httpd terminates.
    
    References:
    [1] Commit 3a21b0b22263c06b5ba54ea2525521c8ca84b349
---
 examples/webserver/webserver_main.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/examples/webserver/webserver_main.c b/examples/webserver/webserver_main.c
index e3f3e9f..9d2313f 100644
--- a/examples/webserver/webserver_main.c
+++ b/examples/webserver/webserver_main.c
@@ -197,6 +197,7 @@ int main(int argc, FAR char *argv[])
   httpd_listen();
 #endif
 
+#ifndef CONFIG_NSH_NETINIT
   /* We are running standalone (as opposed to a NSH built-in app). Therefore
    * we should not exit after httpd failure.
    */
@@ -208,5 +209,23 @@ int main(int argc, FAR char *argv[])
       fflush(stdout);
     }
 
+#else /* CONFIG_NSH_NETINIT */
+  /* We are running as a NSH built-in app.  Therefore we should exit.  This
+   * allows to 'kill -9' the webserver app, assuming it was started as a
+   * background process.  For example:
+   *
+   *    nsh> webserver &
+   *    webserver [6:100]
+   *    nsh> Starting webserver
+   *
+   *    nsh> kill -9 6
+   *    nsh> webserver_main: Exiting
+   */
+
+  printf("webserver_main: Exiting\n");
+  fflush(stdout);
+
+#endif /* CONFIG_NSH_NETINIT */
+
   return 0;
 }