apps/examples/thttpd now uses the Union File System if BINFS is selected for CGI binaris

This commit is contained in:
Gregory Nutt 2015-06-06 12:39:57 -06:00
parent 4986c0660e
commit 3e692f3912
9 changed files with 88 additions and 127 deletions

View File

@ -6,6 +6,7 @@
config EXAMPLES_THTTPD config EXAMPLES_THTTPD
bool "THTTPD web server example" bool "THTTPD web server example"
default n default n
depends on FS_ROMFS
---help--- ---help---
Enable the THTTPD web server example Enable the THTTPD web server example

View File

@ -43,8 +43,14 @@ CONFIG_THTTPD_CGI_STACKSIZE ?= 2048
PRIORITY = $(CONFIG_THTTPD_CGI_PRIORITY) PRIORITY = $(CONFIG_THTTPD_CGI_PRIORITY)
STACKSIZE = $(CONFIG_THTTPD_CGI_STACKSIZE) STACKSIZE = $(CONFIG_THTTPD_CGI_STACKSIZE)
THTTPD_DIR = $(APPDIR)/examples/thttpd
CONTENT_DIR = $(THTTPD_DIR)/content
ROMFS_DIR = $(CONTENT_DIR)/romfs
ROMFS_IMG = $(CONTENT_DIR)/romfs.img
ROMFS_HDR = $(CONTENT_DIR)/romfs.h
ASRCS = ASRCS =
CSRCS = index.c hello.c netstat.c tasks.c CSRCS = hello.c netstat.c tasks.c
AOBJS = $(ASRCS:.S=$(OBJEXT)) AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT))
@ -68,8 +74,8 @@ else
INSTALL_DIR = $(BIN_DIR) INSTALL_DIR = $(BIN_DIR)
endif endif
ROOTDEPPATH = --dep-path index --dep-path hello --dep-path netstat --dep-path tasks ROOTDEPPATH = --dep-path hello --dep-path netstat --dep-path tasks
VPATH = index:hello:netstat:tasks VPATH = hello:netstat:tasks
all: .built all: .built
.PHONY: context headers binaries depend clean .PHONY: context headers binaries depend clean
@ -80,16 +86,38 @@ $(AOBJS): %$(OBJEXT): %.S
$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c $(COBJS) $(MAINOBJ): %$(OBJEXT): %.c
$(call COMPILE, $<, $@) $(call COMPILE, $<, $@)
.built: $(OBJS) # Create the romfs directory
$(ROMFS_DIR):
@mkdir -p $(ROMFS_DIR)
# Populate the romfs directory
$(ROMFS_DIR)/index.html : $(CONTENT_DIR)/index.html
@cp -a $< $@ || { echo "cp of index.html failed"; exit 1; }
$(ROMFS_DIR)/style.css : $(CONTENT_DIR)/style.css
@cp -a $< $@ || { echo "cp of $< failed"; exit 1; }
populate: $(ROMFS_DIR) $(ROMFS_DIR)/index.html $(ROMFS_DIR)/style.css
# Create the romfs.img file from the populated romfs directory
$(ROMFS_IMG): populate
@genromfs -f $@ -d $(ROMFS_DIR) -V "THTTPDTEST"
# Create the romfs.h header file from the romfs.img file
$(ROMFS_HDR) : $(ROMFS_IMG)
@(cd $(CONTENT_DIR); xxd -i romfs.img | sed -e "s/^unsigned/static const unsigned/g" >$@)
.built: $(ROMFS_HDR) $(OBJS)
$(call ARCHIVE, $(BIN), $(OBJS)) $(call ARCHIVE, $(BIN), $(OBJS))
@touch .built @touch .built
binaries: .built binaries: .built
headers: headers: $(ROMFS_HDR)
$(BUILTIN_REGISTRY)$(DELIM)index_main.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,index.cgi,$(PRIORITY),$(STACKSIZE),index_main)
$(BUILTIN_REGISTRY)$(DELIM)hello_main.bdat: $(DEPCONFIG) Makefile $(BUILTIN_REGISTRY)$(DELIM)hello_main.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,hello,$(PRIORITY),$(STACKSIZE),hello_main) $(call REGISTER,hello,$(PRIORITY),$(STACKSIZE),hello_main)
@ -100,7 +128,7 @@ $(BUILTIN_REGISTRY)$(DELIM)netstat_main.bdat: $(DEPCONFIG) Makefile
$(BUILTIN_REGISTRY)$(DELIM)tasks_main.bdat: $(DEPCONFIG) Makefile $(BUILTIN_REGISTRY)$(DELIM)tasks_main.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,tasks,$(PRIORITY),$(STACKSIZE),tasks_main) $(call REGISTER,tasks,$(PRIORITY),$(STACKSIZE),tasks_main)
context: $(BUILTIN_REGISTRY)$(DELIM)index_main.bdat $(BUILTIN_REGISTRY)$(DELIM)hello_main.bdat $(BUILTIN_REGISTRY)$(DELIM)netstat_main.bdat $(BUILTIN_REGISTRY)$(DELIM)tasks_main.bdat context: $(BUILTIN_REGISTRY)$(DELIM)hello_main.bdat $(BUILTIN_REGISTRY)$(DELIM)netstat_main.bdat $(BUILTIN_REGISTRY)$(DELIM)tasks_main.bdat
.depend: Makefile $(SRCS) .depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
@ -109,6 +137,9 @@ context: $(BUILTIN_REGISTRY)$(DELIM)index_main.bdat $(BUILTIN_REGISTRY)$(DELIM)h
depend: .depend depend: .depend
clean: clean:
$(call DELFILE$(ROMFS_HDR))
$(call DELFILE, $(ROMFS_IMG))
@rm -rf $(ROMFS_DIR)
$(call DELFILE, .built) $(call DELFILE, .built)
$(call CLEAN) $(call CLEAN)

View File

@ -66,11 +66,7 @@ int main(int argc, char *argv[])
"</head>\r\n" "</head>\r\n"
"<body bgcolor=\"#fffeec\" text=\"black\">\r\n" "<body bgcolor=\"#fffeec\" text=\"black\">\r\n"
"<div class=\"menu\">\r\n" "<div class=\"menu\">\r\n"
#ifdef CONFIG_THTTPD_BINFS
"<div class=\"menubox\"><a href=\"/index.cgi\">Front page</a></div>\r\n"
#else
"<div class=\"menubox\"><a href=\"/index.html\">Front page</a></div>\r\n" "<div class=\"menubox\"><a href=\"/index.html\">Front page</a></div>\r\n"
#endif
"<div class=\"menubox\"><a href=\"hello\">Say Hello</a></div>\r\n" "<div class=\"menubox\"><a href=\"hello\">Say Hello</a></div>\r\n"
"<div class=\"menubox\"><a href=\"tasks\">Tasks</a></div>\r\n" "<div class=\"menubox\"><a href=\"tasks\">Tasks</a></div>\r\n"
"<div class=\"menubox\"><a href=\"netstat\">Network status</a></div>\r\n" "<div class=\"menubox\"><a href=\"netstat\">Network status</a></div>\r\n"

View File

@ -1,86 +0,0 @@
/****************************************************************************
* examples/thttpd/content/index/index.c
* Generates index.cgi file
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
#ifdef CONFIG_THTTPD_BINFS
int index_main(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
fprintf(stderr, "index.cgi requested from: %s\n", getenv("REMOTE_ADDR"));
puts(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"
"<html>\r\n"
" <head>\r\n"
" <title>NuttX examples/thttpd</title>\r\n"
" </head>\r\n"
" <body bgcolor=\"#fffeec\" text=\"black\">\r\n"
"\r\n"
" <p>\r\n"
" These web pages are served by a port of <a href=\"http://acme.com/software/thttpd/\">THTTPD</a>\r\n"
" running on top of <a href=\"http://www.nuttx.org\">NuttX</a>.\r\n"
" </p>\r\n"
" <p>\r\n"
" Click on the links below to exercise THTTPD's CGI capability under NuttX.\r\n"
" Clicking the links will execute the CGI program from a built-in program\r\n"
" residing in a BINFS file system.\r\n"
" </p>\r\n"
" </ul>\r\n"
" <li><a href=\"index.cgi\">Front page</a></li>\r\n"
" <li><a href=\"hello\">Say Hello</a></li>\r\n"
" <li><a href=\"tasks\">Tasks</a></li>\r\n"
" <li><a href=\"netstat\">Network status</a></li>\r\n"
" </ul>\r\n"
" </body>\r\n"
"</html>\r\n"
);
return 0;
}

View File

@ -121,11 +121,7 @@ int main(int argc, char *argv[])
"</head>\r\n" "</head>\r\n"
"<body bgcolor=\"#fffeec\" text=\"black\">\r\n" "<body bgcolor=\"#fffeec\" text=\"black\">\r\n"
"<div class=\"menu\">\r\n" "<div class=\"menu\">\r\n"
#ifdef CONFIG_THTTPD_BINFS
"<div class=\"menubox\"><a href=\"/index.cgi\">Front page</a></div>\r\n"
#else
"<div class=\"menubox\"><a href=\"/index.html\">Front page</a></div>\r\n" "<div class=\"menubox\"><a href=\"/index.html\">Front page</a></div>\r\n"
#endif
"<div class=\"menubox\"><a href=\"hello\">Say Hello</a></div>\r\n" "<div class=\"menubox\"><a href=\"hello\">Say Hello</a></div>\r\n"
"<div class=\"menubox\"><a href=\"tasks\">Tasks</a></div>\r\n" "<div class=\"menubox\"><a href=\"tasks\">Tasks</a></div>\r\n"
"<div class=\"menubox\"><a href=\"netstat\">Network status</a></div>\r\n" "<div class=\"menubox\"><a href=\"netstat\">Network status</a></div>\r\n"

View File

@ -182,11 +182,7 @@ int main(int argc, char *argv[])
"</head>\r\n" "</head>\r\n"
"<body bgcolor=\"#fffeec\" text=\"black\">\r\n" "<body bgcolor=\"#fffeec\" text=\"black\">\r\n"
"<div class=\"menu\">\r\n" "<div class=\"menu\">\r\n"
#ifdef CONFIG_THTTPD_BINFS
"<div class=\"menubox\"><a href=\"/index.cgi\">Front page</a></div>\r\n"
#else
"<div class=\"menubox\"><a href=\"/index.html\">Front page</a></div>\r\n" "<div class=\"menubox\"><a href=\"/index.html\">Front page</a></div>\r\n"
#endif
"<div class=\"menubox\"><a href=\"hello\">Say Hello</a></div>\r\n" "<div class=\"menubox\"><a href=\"hello\">Say Hello</a></div>\r\n"
"<div class=\"menubox\"><a href=\"tasks\">Tasks</a></div>\r\n" "<div class=\"menubox\"><a href=\"tasks\">Tasks</a></div>\r\n"
"<div class=\"menubox\"><a href=\"netstat\">Network status</a></div>\r\n" "<div class=\"menubox\"><a href=\"netstat\">Network status</a></div>\r\n"

View File

@ -65,6 +65,7 @@
#endif #endif
#ifdef CONFIG_THTTPD_BINFS #ifdef CONFIG_THTTPD_BINFS
# include <nuttx/fs/unionfs.h>
# include <nuttx/binfmt/builtin.h> # include <nuttx/binfmt/builtin.h>
#endif #endif
@ -72,8 +73,9 @@
# include <nuttx/net/net.h> # include <nuttx/net/net.h>
#endif #endif
#ifdef CONFIG_THTTPD_NXFLAT
#include "content/romfs.h" #include "content/romfs.h"
#ifdef CONFIG_THTTPD_NXFLAT
# include "content/symtab.h" # include "content/symtab.h"
#endif #endif
@ -108,19 +110,23 @@
#endif #endif
#ifdef CONFIG_THTTPD_BINFS #ifdef CONFIG_THTTPD_BINFS
# ifndef CONFIG_FS_BINFS # ifndef CONFIG_BUILTIN
# error "You must select CONFIG_BUILTIN=y in your configuration file" # error "You must select CONFIG_BUILTIN=y in your configuration file"
# endif # endif
# ifndef CONFIG_FS_BINFS # ifndef CONFIG_FS_BINFS
# error "You must select CONFIG_FS_BINFS=y in your configuration file" # error "You must select CONFIG_FS_BINFS=y in your configuration file"
# endif # endif
# ifndef CONFIG_FS_UNIONFS
# error "CONFIG_FS_UNIONFS=y is required in this configuration"
# endif # endif
#endif
/* Ethernet specific configuration */ /* Ethernet specific configuration */
#ifdef CONFIG_NET_ETHERNET #ifdef CONFIG_NET_ETHERNET
/* Use the standard Ethernet device name */
/* Otherwise, use the standard Ethernet device name */
# define NET_DEVNAME "eth0" # define NET_DEVNAME "eth0"
@ -153,7 +159,16 @@
#define SECTORSIZE 512 #define SECTORSIZE 512
#define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE) #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
#define ROMFSDEV "/dev/ram0" #define ROMFSDEV "/dev/ram0"
#define MOUNTPT CONFIG_THTTPD_PATH
#ifdef CONFIG_THTTPD_BINFS
# define ROMFS_MOUNTPT "/mnt/tmp1"
# define ROMFS_PREFIX NULL
# define BINFS_MOUNTPT "/mnt/tmp2"
# define BINFS_PREFIX "cgi-bin"
# define UNIONFS_MOUNTPT CONFIG_THTTPD_PATH
#else
# define ROMFS_MOUNTPT CONFIG_THTTPD_PATH
#endif
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
@ -249,6 +264,7 @@ int thttp_main(int argc, char *argv[])
printf("ERROR: Initialization of the NXFLAT loader failed: %d\n", ret); printf("ERROR: Initialization of the NXFLAT loader failed: %d\n", ret);
exit(2); exit(2);
} }
#endif
/* Create a ROM disk for the ROMFS filesystem */ /* Create a ROM disk for the ROMFS filesystem */
@ -258,26 +274,29 @@ int thttp_main(int argc, char *argv[])
if (ret < 0) if (ret < 0)
{ {
printf("ERROR: romdisk_register failed: %d\n", ret); printf("ERROR: romdisk_register failed: %d\n", ret);
#ifdef CONFIG_THTTPD_NXFLAT
nxflat_uninitialize(); nxflat_uninitialize();
#endif
exit(1); exit(1);
} }
/* Mount the ROMFS file system */ /* Mount the ROMFS file system */
printf("Mounting ROMFS filesystem at target=%s with source=%s\n", printf("Mounting ROMFS filesystem at target=%s with source=%s\n",
MOUNTPT, ROMFSDEV); ROMFS_MOUNTPT, ROMFSDEV);
ret = mount(ROMFSDEV, MOUNTPT, "romfs", MS_RDONLY, NULL); ret = mount(ROMFSDEV, ROMFS_MOUNTPT, "romfs", MS_RDONLY, NULL);
if (ret < 0) if (ret < 0)
{ {
printf("ERROR: mount(%s,%s,romfs) failed: %d\n", printf("ERROR: mount(%s,%s,romfs) failed: %d\n",
ROMFSDEV, MOUNTPT, errno); ROMFSDEV, ROMFS_MOUNTPT, errno);
#ifdef CONFIG_THTTPD_NXFLAT
nxflat_uninitialize(); nxflat_uninitialize();
}
#endif #endif
}
#ifdef CONFIG_THTTPD_BINFS #ifdef CONFIG_THTTPD_BINFS
/* Initialize the NXFLAT binary loader */ /* Initialize the BINFS binary loader */
printf("Initializing the Built-In binary loader\n"); printf("Initializing the Built-In binary loader\n");
@ -290,12 +309,23 @@ int thttp_main(int argc, char *argv[])
/* Mount the BINFS file system */ /* Mount the BINFS file system */
printf("Mounting BINFS filesystem at target=%s\n", MOUNTPT); printf("Mounting BINFS filesystem at %s\n", BINFS_MOUNTPT);
ret = mount(NULL, MOUNTPT, "binfs", MS_RDONLY, NULL); ret = mount(NULL, BINFS_MOUNTPT, "binfs", MS_RDONLY, NULL);
if (ret < 0) if (ret < 0)
{ {
printf("ERROR: mount(NULL,%s,binfs) failed: %d\n", MOUNTPT, errno); printf("ERROR: mount(NULL,%s,binfs) failed: %d\n", BINFS_MOUNTPT, errno);
}
/* Now create and mount the union file system */
printf("Creating UNIONFS filesystem at %s\n", UNIONFS_MOUNTPT);
ret = unionfs_mount(ROMFS_MOUNTPT, ROMFS_PREFIX, BINFS_MOUNTPT, BINFS_PREFIX,
UNIONFS_MOUNTPT);
if (ret < 0)
{
printf("ERROR: Failed to create the union file system at %s: %d\n", UNIONFS_MOUNTPT, ret);
} }
#endif #endif

View File

@ -192,7 +192,7 @@ int unionfs_main(int argc, char *argv[])
CONFIG_EXAMPLES_UNIONFS_MOUNTPT); CONFIG_EXAMPLES_UNIONFS_MOUNTPT);
if (ret < 0) if (ret < 0)
{ {
printf("ERROR: Failed to crate the union file system 1: %d\n", ret); printf("ERROR: Failed to create the union file system: %d\n", ret);
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View File

@ -56,22 +56,19 @@ endchoice
config THTTPD_PATH config THTTPD_PATH
string "Path to the server content" string "Path to the server content"
default "/mnt/www" if NXFLAT default "/mnt/www"
default "/bin" if FS_BINFS
---help--- ---help---
Server working directory. Default: "/mnt/www" Server working directory. Default: "/mnt/www"
config THTTPD_CGI_PATH config THTTPD_CGI_PATH
string "Path to CGI content" string "Path to CGI content"
default "/mnt/www/cgi-bin" if NXFLAT default "/mnt/www/cgi-bin"
default "/bin" if FS_BINFS
---help--- ---help---
Path to CGI executables. Default: "/mnt/www/cgi-bin" Path to CGI executables. Default: "/mnt/www/cgi-bin"
config THTTPD_CGI_PATTERN config THTTPD_CGI_PATTERN
string "CGI match pattern" string "CGI match pattern"
default "/mnt/www/cgi-bin/*" if NXFLAT default "/mnt/www/cgi-bin/*"
default "/bin/*" if FS_BINFS
---help--- ---help---
Only CGI programs matching this pattern will be executed. In Only CGI programs matching this pattern will be executed. In
fact, if this value is not defined then no CGI logic will be built. fact, if this value is not defined then no CGI logic will be built.