移植嵌入式 Boa web server到TI 達芬奇平臺

陸續記錄移植過程。

1.下載Boa源碼

a.到 http://www.boa.org/ 下載boa源碼,目前最新版本爲:0.94.13
b.在主機上解壓下載到的源碼壓縮包boa-0.94.13.tar.gz,執行:tar xzf boa-0.94.13.tar.gz

2.生成makefile文件

以上步驟解壓後的目錄爲:boa-0.94.13
進入src目錄:cd src
運行configure文件:./configure
看看到主機終端有以下信息輸出:
creating cache ./config.cache
checking for gunzip... /bin/gunzip
checking for flex... flex
checking for yywrap in -lfl... yes
checking for bison... bison -y
checking for gcc... gcc
checking whether the C compiler (gcc  ) works... yes
checking whether the C compiler (gcc  ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
checking how to run the C preprocessor... gcc -E
checking whether make sets ${MAKE}... yes
checking for dirent.h that defines DIR... yes
checking for opendir in -ldir... no
checking for ANSI C header files... yes
checking for sys/wait.h that is POSIX.1 compatible... yes
checking for fcntl.h... yes
checking for sys/fcntl.h... yes
checking for limits.h... yes
checking for sys/time.h... yes
checking for sys/select.h... yes
checking for getopt.h... yes
checking for working const... yes
checking for uid_t in sys/types.h... yes
checking for pid_t... yes
checking whether time.h and sys/time.h may both be included... yes
checking whether setvbuf arguments are reversed... no
checking for unistd.h... yes
checking for getpagesize... yes
checking for working mmap... yes
checking for getcwd... yes
checking for strdup... yes
checking for strstr... yes
checking for gethostname... yes
checking for gethostbyname... yes
checking for select... yes
checking for socket... yes
checking for inet_aton... yes
checking for scandir... yes
checking for alphasort... yes
checking for tm.tm_gmtoff... yes
checking for tm.tm_zone... yes
checking for sockaddr_in.sin_len... no
checking compile and link profiling code... no
checking whether to compile and link debugging code... yes
checking whether to link with the Dmalloc memory debugger/profiler... no
checking whether to link with the Electric Fence memory debugger... no
updating cache ./config.cache
creating ./config.status
creating Makefile
creating config.h
可見makefile文件已經成功生成。

3.作編譯前的一些修改

主要是爲了修正編譯過程中會出現的錯誤
(1)修改 src/compat.h
找到
#define  TIMEZONE_OFFSET(foo)      foo##->tm_gmtoff
修改爲
#define  TIMEZONE_OFFSET(foo)      (foo)->tm_gmtoff
如果不作以上修改,編譯時會出現以下錯誤:
util.c:100:1: error: pasting "t" and "->" does not give a valid preprocessing token
(2)修改src/log.c
在函數中void open_logs(void) 註釋掉:
        if (dup2(error_log, STDERR_FILENO) == -1) {
            DIE("unable to dup2 the error log");
        }
即改爲:
        /*if (dup2(error_log, STDERR_FILENO) == -1) {
            DIE("unable to dup2 the error log");
        }*/
此修改主要是修正運行boa時出現的以下錯誤:
log.c:73 - unable to dup2 the error log: Bad file descriptor
注:此修改主要是修正將log重定向到log文件出現的錯誤問題,如果按照以上修改,則沒有重定向功能,即沒有了錯誤日誌查詢,所有的錯誤信息都會打印在終端控制檯上。
boa日誌有兩部分,爲errorlog和access_log,日誌文件一般位於var/log/boa/error_log和var/log/boa/access_log(見下文提及的boa.conf),如果這裏不對src/log.c文件進行修改,則第一需要下文配置boa.conf中修改errorlog和access_log的路徑配置,第二保證boa.conf指定的user和group均對日誌所在目錄有讀寫權限。
(3)修改 src/boa.c
在函數static void drop_privs(void)中,
註釋掉:
        if (passwdbuf == NULL) {
            DIE("getpwuid");
        }
        if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) {
            DIE("initgroups");
        }
即修改爲:
#if 0
        if (passwdbuf == NULL) {
            DIE("getpwuid");
        }
        if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) {
            DIE("initgroups");
        }
#endif
此修改主要防止運行boa時出現的一下錯誤:
boa.c:211 - getpwuid:No such file or direactory
註釋掉
        if (setuid(0) != -1) {
            DIE("icky Linux kernel bug!");
        }
即修改爲:
#if 0
        if (setuid(0) != -1) {
            DIE("icky Linux kernel bug!");
        }
#endif
此修改主要防止運行boa時出現的一下錯誤:
 boa.c:226 - icky Linux kernel bug!: No such file or directory

4.修改makefile

修改步驟2生成的makefile,主要是更改使用嵌入式交叉編譯工具鏈進行編譯。修改之前務必在主機上添加必要的環境變量,使得編譯boa時,能找到主機上的交叉工具鏈位置。
CC = gcc 
CPP = gcc -E
修改爲:
CC = arm-arago-linux-gnueabi-gcc 
CPP = arm-arago-linux-gnueabi-gcc -E

5.編譯

在src目錄編譯,運行命令:make
沒意外的話會在src目錄生成boa文件。

6.去除調試信息

步驟5中生成的boa包含調試信息,文件較大,爲減少對嵌入式設備的資源消耗,在這裏去除其調試信息。
去除調試信息前查看boa信息
ls -l boa
-rwxr-xr-x 1 vincent vincent 208275 2013-05-10 10:51 boa
執行命令去除調試信息;
arm-arago-linux-gnueabi-strip boa
再次查看boa信息
ls -l boa
-rwxr-xr-x 1 vincent vincent 62760 2013-05-10 11:18 boa
OK,boa精簡了好多好多。

7.配置與構建

在嵌入式平臺根文件系統下建立運行boa時需要的一些目錄:
依次新建如下目錄
/etc/boa
/etc/boa/www
/etc/boa/www/cgi-bin
將上面編譯後生成的boa拷貝到新建的目錄/etc/boa下。
將boa源碼跟目錄下的boa.conf拷貝到新建的目錄/etc/boa下。
注:boa.conf的位置是由boa源碼中src/defines.h的宏SERVER_ROOT確定的,默認是在etc/boa下,若想存放其他位置,有兩種方法,一在編譯boa前修改defines.h
二在運行boa時通過-c參數項指定boa.conf文件位置。
修改拷貝後的boa.conf (etc/boa/boa.conf),修改項如下:
(1)修改 User nobody 爲:User 0
(2)修改 Group nogroup爲:Group 0
PS:以上的修改原則是根據嵌入式linux用戶和用戶組修改的,一般修改爲0沒問題,root用戶嘛。
(3)修改 ScriptAlias /cgi-bin/    /usr/lib/cgi-bin/
          爲:ScriptAlias /cgi-bin/    /etc/boa/www/cgi-bin/
(4)修改DocumentRoot /var/www
         爲DocumentRoot /etc/boa/www
(5)修改#ServerName www.your.org.here
          爲ServerName www.your.org.here
(6)修改AccessLog /var/log/boa/access_log 和 errorLog /var/log/boa/error_log
          爲:AccessLog /你的目錄 和 errorLog /你的目錄
         並確認你的嵌入式設備的根文件系統有這些目錄,如果沒有要手動創建,日誌文件不用建,運行boa時它會自動建。
注::如果在上面3中未對src/log.c文件修改,則一定要作此步驟修改,如果上面3中修改了src/boa.c則這裏只需屏蔽掉即可(就是在前面加#)。

8.運行測試

將一個靜態網頁重命名爲:index.html 拷貝到嵌入式平臺根文件系統的/etc/boa/www下。
啓動嵌入式設備,運行boa
./etc/boa/boa
如果修改刪除了步驟3中的日誌重定向問題,則在串口控制檯中會看到華麗的啓動信息
root@OMAP3EVM:/# [01/Jan/2000:00:13:54 +0000] boa: server version Boa/0.94.13
[01/Jan/2000:00:13:54 +0000] boa: server built May 10 2013 at 13:59:37.
[01/Jan/2000:00:13:54 +0000] boa: starting server pid=611, port 80
如果未修改刪除步驟3中的日誌重定向,則啓動boa時候沒有啓動信息輸出到串口控制檯上,可以去cat你的error_log查看啓動信息。
ifconfig查看嵌入式設備的IP,及web server IP,
eth0      Link encap:Ethernet  HWaddr 00:24:BA:ED:58:CA  
          inet addr:192.168.188.165  Bcast:192.168.188.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12027 errors:0 dropped:5 overruns:0 frame:0
          TX packets:4822 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:4823486 (4.5 MiB)  TX bytes:799473 (780.7 KiB)
          Interrupt:61 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
IP爲:192.168.188.165
果斷地打開一個PC瀏覽器,在地址欄輸入:http://192.168.188.165沒出意外的話應該可以看到上面拷貝到/etc/boa/www的靜態網頁。







發佈了64 篇原創文章 · 獲贊 3 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章