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
This commit is contained in:
Nathan Hartman 2021-01-03 17:58:04 -05:00 committed by Xiang Xiao
parent 1b5b8c5167
commit 819454ee6e

View File

@ -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;
}