我的openwrt学习笔记(三十):webserver之uhttpd

                        我的openwrt学习笔记(三十):webserver之uhttpd

         Openwrt使用的WEBSERVER是UHTTP。

一.Uhttp简介

1.网页的挂载方式

openwrt挂载网页启动的是uhttpd,uhttp是LuCI所在的Web Server。用户登录所用网页放置在/www下边,index-htm指向了/www/cgi-bin/luci。openwrt中利用它作为web服务器,实现客户端web页面配置功能。对于request处理方式,采用的是cgi脚本,而所用的cgi程序就是luci。

 

2.工作框架如下:

我的openwrt学习笔记(三十):webserver之uhttpd_第1张图片

 

  Client端和serv端采用cgi方式交互,uhttpd服务器的cgi方式中,fork出一个子进程,子进程利用execl替换为luci进程空间,并通过setenv环境变量的方式,传递一些固定格式的数据(如PATH_INFO)给luci。另外一些非固定格式的数据(post-data)则由父进程通过一个w_pipe写给luci的stdin,而luci的返回数据则写在stdout上,由父进程通过一个r_pipe读取。

 

二.编译uhttp源代码包

   Uhttp的编译时cmake编译系统,按照cmake编译方法编译即可。Uhttp是基于ubox ubus json-c的,如果加上SSL ,还需要openssl库。

 

git clone git://git.openwrt.org/project/libubox.git

 git clonegit://git.openwrt.org/project/ubus.git

 

1.openwrt libubox 开发简介

libubox是openwrt新版本中的一个基础库,在openwrt1407中有很多应用程序是基于libubox开发的。(如:uhttpd,libubus等)。

    libubox主要提供一下两种功能:

        1、提供一套基于事件驱动的机制。

        2、提供多种开发支持接口。(如:链表、kv链表、平衡查找二叉树、md5、json)

    使用libubox开发的好处有如下几点:

        1、可以使程序基于事件驱动,从而可实现在单线程中处理多个任务。

        2、基于libubox提供的开发API可以加快开发进度的同事提高程序的稳定性。

        3、能更好的将程序融入openwrt的架构中,因为新的openwrt的很多应用和库都基于libubox开发的。

    综上所述,libubox是您玩新版openwrt必修的一个东东,相信它也值得大家去研究学习。

 

2.openwrt ubus简介以及libubus开发说明

ubus是新openwrt引入的一个消息总线,主要作用是实现不同应用程序之间的信息交互。

    ubus启动后会在后台运行ubusd进程,该进程监听一个unix套接字用于与其他应用程序通信。其他应用程序可基于libubox提供的接口(或自己实现)与其通信。

    使用ubus的方式主要有:1、向其注册消息或控制接口。2、向其调用其他应用程序的消息或控制接口。3、向其注册关心的事件。

ubus命令使用说明

ubus命令用于控制调试相关ubus接口,主要命令说明如下:

 

linux@ubuntu:~/linux_c/ubus/ubus-2014-09-17$./ubus

Usage: ./ubus [] [arguments...]

Options:

 -s:            Set the unixdomain socket to connect to

 -t:          Set the timeout (inseconds) for a command to complete

 -S:                     Use simplified output (forscripts)

 -v:                     More verbose output

 

Commands:

 -list []                   Listobjects

 -call []    Call an object method

 -listen [...]                    Listenfor events

 - send []            Sendan event

 -wait_for [...]   Waitfor multiple objects to appear on ubus

ubus (OpenWrt micro bus 架构)  http://wiki.openwrt.org/zh-cn/doc/techref/ubus 

3.编译uhttp

Uhttp编译需要先安装cmake工具链

 

linux@ubuntu:~/linux_c/uhttpd/uhttpd-2014-10-27$ sudo apt-get install cmake

Reading package lists... Done

Building dependency tree      

Reading state information... Done

The following extra packages will be installed:

  cmake-data emacsen-common libxmlrpc-core-c3

The following NEW packages will be installed:

  cmake cmake-data emacsen-common libxmlrpc-core-c3

0 upgraded, 4 newly installed, 0 to remove and 623 not upgraded.

Need to get 196 kB/5,348 kB of archives.

After this operation, 13.9 MB of additional disk space will be used.

Do you want to continue [Y/n]? y

Get:1 http://cn.archive.ubuntu.com/ubuntu/ precise-updates/main libxmlrpc-core-c3 i386 1.16.33-3.1ubuntu5.1 [179 kB]

Get:2 http://cn.archive.ubuntu.com/ubuntu/ precise/main emacsen-common all 1.4.22ubuntu1 [16.9 kB]

Fetched 196 kB in 0s (608 kB/s)     

Selecting previously unselected package libxmlrpc-core-c3.

(Reading database ... 148423 files and directories currently installed.)

Unpacking libxmlrpc-core-c3 (from .../libxmlrpc-core-c3_1.16.33-3.1ubuntu5.1_i386.deb) ...

Selecting previously unselected package emacsen-common.

Unpacking emacsen-common (from .../emacsen-common_1.4.22ubuntu1_all.deb) ...

Selecting previously unselected package cmake-data.

Unpacking cmake-data (from .../cmake-data_2.8.7-0ubuntu5_all.deb) ...

Selecting previously unselected package cmake.

Unpacking cmake (from .../cmake_2.8.7-0ubuntu5_i386.deb) ...

Processing triggers for man-db ...

Setting up libxmlrpc-core-c3 (1.16.33-3.1ubuntu5.1) ...

Setting up emacsen-common (1.4.22ubuntu1) ...

emacsen-common: Handling install of emacsen flavor emacs

Setting up cmake-data (2.8.7-0ubuntu5) ...

emacsen-common: Handling install of emacsen flavor emacs

Setting up cmake (2.8.7-0ubuntu5) ...

Processing triggers for libc-bin ...

ldconfig deferred processing now taking place

  执行编译,注意点号

linux@ubuntu:~/linux_c/uhttpd/libubox-2014-08-04$ cmake .

-- The C compiler identification is GNU

-- Check for working C compiler: /usr/bin/gcc

-- Check for working C compiler: /usr/bin/gcc -- works

-- Detecting C compiler ABI info

-- Detecting C compiler ABI info - done

-- checking for modules 'json-c;json'

--   package 'json-c' not found

--   package 'json' not found

-- Looking for clock_gettime

-- Looking for clock_gettime - not found

-- Looking for clock_gettime in rt

-- Looking for clock_gettime in rt - found

CMake Error at lua/CMakeLists.txt:36 (MESSAGE):

  Lua was not found on your system

 

 

CMake Error at lua/CMakeLists.txt:49 (INSTALL):

  install TARGETS given no LIBRARY DESTINATION for module target "uloop_lua".

 

 

n       Configuring incomplete, errors occurred!

 

Jsonc 编译安装

 

linux@ubuntu:~/linux_c/jsonc/json-c-0.11$ ./configure

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for a thread-safe mkdir -p... /bin/mkdir -p

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

checking whether make sets $(MAKE)... (cached) yes

checking for style of include used by make... GNU

checking for gcc... gcc

checking whether the C compiler works... yes

checking for C compiler default output file name... a.out

checking for suffix of executables...

checking whether we are cross compiling... no

checking for suffix of object files... o

checking whether we are using the GNU C compiler... yes

checking whether gcc accepts -g... yes

checking for gcc option to accept ISO C89... none needed

checking dependency style of gcc... gcc3

checking how to run the C preprocessor... gcc -E

checking for grep that handles long lines and -e... /bin/grep

checking for egrep... /bin/grep -E

checking for ANSI C header files... yes

checking for sys/types.h... yes

checking for sys/stat.h... yes

checking for stdlib.h... yes

checking for string.h... yes

checking for memory.h... yes

checking for strings.h... yes

checking for inttypes.h... yes

checking for stdint.h... yes

checking for unistd.h... yes

checking fcntl.h usability... yes

checking fcntl.h presence... yes

checking for fcntl.h... yes

checking limits.h usability... yes

checking limits.h presence... yes

checking for limits.h... yes

checking for strings.h... (cached) yes

checking syslog.h usability... yes

checking syslog.h presence... yes

checking for syslog.h... yes

checking for unistd.h... (cached) yes

checking sys/cdefs.h usability... yes

checking sys/cdefs.h presence... yes

checking for sys/cdefs.h... yes

checking sys/param.h usability... yes

checking sys/param.h presence... yes

checking for sys/param.h... yes

checking stdarg.h usability... yes

checking stdarg.h presence... yes

checking for stdarg.h... yes

checking locale.h usability... yes

checking locale.h presence... yes

checking for locale.h... yes

checking for inttypes.h... (cached) yes

checking for an ANSI C-conforming const... yes

checking for size_t... yes

checking for vprintf... yes

checking for _doprnt... no

checking for working memcmp... yes

checking for stdlib.h... (cached) yes

checking for GNU libc compatible malloc... yes

checking for stdlib.h... (cached) yes

checking for GNU libc compatible realloc... yes

checking for strcasecmp... yes

checking for strdup... yes

checking for strndup... yes

checking for strerror... yes

checking for snprintf... yes

checking for vsnprintf... yes

checking for vasprintf... yes

checking for open... yes

checking for vsyslog... yes

checking for strncasecmp... yes

checking for setlocale... yes

checking if .gnu.warning accepts long strings... no

checking build system type... i686-pc-linux-gnu

checking host system type... i686-pc-linux-gnu

checking for a sed that does not truncate output... /bin/sed

checking for fgrep... /bin/grep -F

checking for ld used by gcc... /usr/bin/ld

checking if the linker (/usr/bin/ld) is GNU ld... yes

checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B

checking the name lister (/usr/bin/nm -B) interface... BSD nm

checking whether ln -s works... yes

checking the maximum length of command line arguments... 1572864

checking whether the shell understands some XSI constructs... yes

checking whether the shell understands "+="... yes

checking for /usr/bin/ld option to reload object files... -r

checking for objdump... objdump

checking how to recognize dependent libraries... pass_all

checking for ar... ar

checking for strip... strip

checking for ranlib... ranlib

checking command to parse /usr/bin/nm -B output from gcc object... ok

checking for dlfcn.h... yes

checking for objdir... .libs

checking if gcc supports -fno-rtti -fno-exceptions... no

checking for gcc option to produce PIC... -fPIC -DPIC

checking if gcc PIC flag -fPIC -DPIC works... yes

checking if gcc static flag -static works... yes

checking if gcc supports -c -o file.o... yes

checking if gcc supports -c -o file.o... (cached) yes

checking whether the gcc linker (/usr/bin/ld) supports shared libraries... yes

checking whether -lc should be explicitly linked in... no

checking dynamic linker characteristics... GNU/Linux ld.so

checking how to hardcode library paths into programs... immediate

checking whether stripping libraries is possible... yes

checking if libtool supports shared libraries... yes

checking whether to build shared libraries... yes

checking whether to build static libraries... yes

configure: creating ./config.status

config.status: creating Makefile

config.status: creating json.pc

config.status: creating json-c.pc

config.status: creating tests/Makefile

config.status: creating json-c-uninstalled.pc

config.status: creating config.h

config.status: config.h is unchanged

config.status: creating json_config.h

config.status: json_config.h is unchanged

config.status: executing depfiles commands

config.status: executing libtool commands

 

linux@ubuntu:~/linux_c/jsonc/json-c-0.11$ make

。。。

 

linux@ubuntu:~/linux_c/jsonc/json-c-0.11$ sudo make install

See any operating system documentation about shared libraries for

more information, such as the ld(1) and ld.so(8) manual pages.

----------------------------------------------------------------------

test -z "/usr/local/include/json-c" || /bin/mkdir -p "/usr/local/include/json-c"

 /usr/bin/install -c -m 644 arraylist.h bits.h debug.h json.h json_config.h json_c_version.h json_inttypes.h json_object.h json_object_iterator.h json_object_private.h json_tokener.h json_util.h linkhash.h printbuf.h '/usr/local/include/json-c'

test -z "/usr/local/lib/pkgconfig" || /bin/mkdir -p "/usr/local/lib/pkgconfig"

 /usr/bin/install -c -m 644 json-c.pc json.pc '/usr/local/lib/pkgconfig'

make  install-data-hook

make[3]: Entering directory `/home/linux/linux_c/jsonc/json-c-0.11'

test \! -e "/usr/local/include/json" || rm "/usr/local/include/json"

ln -s json-c "/usr/local/include/json"

make[3]: Leaving directory `/home/linux/linux_c/jsonc/json-c-0.11'

make[2]: Leaving directory `/home/linux/linux_c/jsonc/json-c-0.11'

make[1]: Leaving directory `/home/linux/linux_c/jsonc/json-c-0.11'

Making install in tests

make[1]: Entering directory `/home/linux/linux_c/jsonc/json-c-0.11/tests'

make[2]: Entering directory `/home/linux/linux_c/jsonc/json-c-0.11/tests'

make[2]: Nothing to be done for `install-exec-am'.

make[2]: Nothing to be done for `install-data-am'.

make[2]: Leaving directory `/home/linux/linux_c/jsonc/json-c-0.11/tests'

make[1]: Leaving directory `/home/linux/linux_c/jsonc/json-c-0.11/tests'

 

 

重新编译libubox

linux@ubuntu:~/linux_c/uhttpd/libubox-2014-08-04$ cmake -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_LUA=off

-- checking for modules 'json-c;json'

--   found json-c, version 0.11

--   found json, version 0.11

-- Configuring done

-- Generating done

-- Build files have been written to: /home/linux/linux_c/uhttpd/libubox-2014-08-04

 Json c错误以及LUA错误没了,并生成了Makefile

执行编译ubox

linux@ubuntu:~/linux_c/uhttpd/libubox-2014-08-04$ make

OK  编译通过

开始安装

linux@ubuntu:~/linux_c/uhttpd/libubox-2014-08-04$ sudo make install

[sudo] password for linux:

[ 45%] Built target ubox

[ 48%] Built target blobmsg_json

[ 51%] Built target jshn

[ 54%] Built target json_script

[100%] Built target ubox-static

Install the project...

-- Install configuration: ""

-- Installing: /usr/include/libubox/uloop.h

-- Installing: /usr/include/libubox/vlist.h

-- Installing: /usr/include/libubox/blobmsg.h

-- Installing: /usr/include/libubox/blobmsg_json.h

-- Installing: /usr/include/libubox/runqueue.h

-- Installing: /usr/include/libubox/ustream.h

-- Installing: /usr/include/libubox/utils.h

-- Installing: /usr/include/libubox/blob.h

-- Installing: /usr/include/libubox/safe_list.h

-- Installing: /usr/include/libubox/list.h

-- Installing: /usr/include/libubox/md5.h

-- Installing: /usr/include/libubox/kvlist.h

-- Installing: /usr/include/libubox/usock.h

-- Installing: /usr/include/libubox/json_script.h

-- Installing: /usr/include/libubox/avl-cmp.h

-- Installing: /usr/include/libubox/avl.h

-- Installing: /usr/lib/libubox.so

-- Installing: /usr/lib/libubox.a

-- Installing: /usr/lib/libblobmsg_json.so

-- Removed runtime path from "/usr/lib/libblobmsg_json.so"

-- Installing: /usr/bin/jshn

-- Removed runtime path from "/usr/bin/jshn"

-- Installing: /usr/lib/libjson_script.so

-- Removed runtime path from "/usr/lib/libjson_script.so"

-- Installing: /usr/share/libubox/jshn.sh

 

 

Ubus编译安装

linux@ubuntu:~/linux_c/ubus/ubus-2014-09-17$ cmake .

-- The C compiler identification is GNU

-- Check for working C compiler: /usr/bin/gcc

-- Check for working C compiler: /usr/bin/gcc -- works

-- Detecting C compiler ABI info

-- Detecting C compiler ABI info - done

CMake Error at lua/CMakeLists.txt:36 (MESSAGE):

  Lua was not found on your system

 

 

CMake Error at lua/CMakeLists.txt:49 (INSTALL):

  install TARGETS given no LIBRARY DESTINATION for module target "ubus_lua".

 

 

-- checking for module 'systemd'

--   package 'systemd' not found

-- Configuring incomplete, errors occurred!

linux@ubuntu:~/linux_c/ubus/ubus-2014-09-17$ cmake -DBUILD_LUA=off

-- checking for module 'systemd'

--   package 'systemd' not found

-- Configuring done

-- Generating done

-- Build files have been written to: /home/linux/linux_c/ubus/ubus-2014-09-17

linux@ubuntu:~/linux_c/ubus/ubus-2014-09-17$ make

Scanning dependencies of target ubus

[  6%] Building C object CMakeFiles/ubus.dir/libubus.c.o

[ 13%] Building C object CMakeFiles/ubus.dir/libubus-io.c.o

[ 20%] Building C object CMakeFiles/ubus.dir/libubus-obj.c.o

[ 26%] Building C object CMakeFiles/ubus.dir/libubus-sub.c.o

[ 33%] Building C object CMakeFiles/ubus.dir/libubus-req.c.o

Linking C shared library libubus.so

[ 33%] Built target ubus

Scanning dependencies of target cli

[ 40%] Building C object CMakeFiles/cli.dir/cli.c.o

Linking C executable ubus

[ 40%] Built target cli

Scanning dependencies of target ubusd

[ 46%] Building C object CMakeFiles/ubusd.dir/ubusd.c.o

[ 53%] Building C object CMakeFiles/ubusd.dir/ubusd_id.c.o

[ 60%] Building C object CMakeFiles/ubusd.dir/ubusd_obj.c.o

[ 66%] Building C object CMakeFiles/ubusd.dir/ubusd_proto.c.o

[ 73%] Building C object CMakeFiles/ubusd.dir/ubusd_event.c.o

Linking C executable ubusd

[ 73%] Built target ubusd

Scanning dependencies of target client

[ 80%] Building C object examples/CMakeFiles/client.dir/client.c.o

[ 86%] Building C object examples/CMakeFiles/client.dir/count.c.o

Linking C executable client

[ 86%] Built target client

Scanning dependencies of target server

[ 93%] Building C object examples/CMakeFiles/server.dir/server.c.o

[100%] Building C object examples/CMakeFiles/server.dir/count.c.o

Linking C executable server

[100%] Built target server

linux@ubuntu:~/linux_c/ubus/ubus-2014-09-17$ sudo make install

[sudo] password for linux:

[ 33%] Built target ubus

[ 40%] Built target cli

[ 73%] Built target ubusd

[ 86%] Built target client

[100%] Built target server

Install the project...

-- Install configuration: ""

-- Installing: /usr/local/lib/libubus.so

-- Installing: /usr/local/bin/ubus

-- Removed runtime path from "/usr/local/bin/ubus"

-- Installing: /usr/local/sbin/ubusd

-- Installing: /usr/local/include/ubusmsg.h

-- Installing: /usr/local/include/ubus_common.h

-- Installing: /usr/local/include/libubus.h

 

 

编译并安装了json-c ubox  ubus基础包后重新进入Uhttpd 编译

 

linux@ubuntu:~/linux_c/uhttpd/uhttpd-2014-10-27$ cmake .

-- The C compiler identification is GNU

-- Check for working C compiler: /usr/bin/gcc

-- Check for working C compiler: /usr/bin/gcc -- works

-- Detecting C compiler ABI info

-- Detecting C compiler ABI info - done

-- Looking for getspnam

-- Looking for getspnam - found

-- Configuring done

-- Generating done

-- Build files have been written to: /home/linux/linux_c/uhttpd/uhttpd-2014-10-27

生成Makefile

linux@ubuntu:~/linux_c/uhttpd/uhttpd-2014-10-27$ ls -l|grep Makefile

-rw-rw-r-- 1 linux linux 14339 Sep 13 16:20 Makefile

 

 

 

你可能感兴趣的:(openwrt)