Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Tiny6410 移植 Boa 伺服器:交叉編譯、設定與除錯全記錄

Boa 是一款單執行檔、佔用資源極小的輕量級 Web 伺服器,特別適合 Flash 與記憶體都相當吃緊的嵌入式板子。本文以 Tiny6410 為平台,完整記錄交叉編譯、原始碼裁剪、設定檔撰寫,以及編譯期與執行期常見錯誤的排除思路。

一、下載與交叉編譯

1. 先改好 Makefile

  1. 從 www.boa.org 下載 boa-0.94.13.tar.gz 並解開。
  2. 進入 src 目錄執行 ./configure,讓它產生 Makefile。
  3. 編輯 Makefile,把主機端編譯器換成 ARM 交叉工具鏈:
CC = arm-linux-gcc
CPP = arm-linux-gcc -E

2. 修改 boa.c

把 boa.c 中針對 setuid(0) 的那段判斷註解掉,改用 #if 0 ... #endif 包起來更清楚:

#if 0
if (setuid(0) != -1) {
    DIE("icky Linux kernel bug!");
}
#endif

3. 修改 compat.h

舊版 GCC 對 token 拼接的限制會讓下面這行直接編不過,把 ## 拿掉即可:

#define TIMEZONE_OFFSET(foo) ((foo)->tm_gmtoff)

4. 編譯與裁減

#make
#arm-linux-strip boa

strip 之後執行檔體積會小很多,再把它放到板子的 /usr/sbin 底下。

二、撰寫 boa.conf

嵌入式環境不需要完整設定,保留下面這些欄位就能跑起來(也可以自己重寫一份 boa.conf 一起打包):

Port 80                             # 服務監聽埠
User 0
Group 0
ErrorLog /var/log/boa/error_log      # 錯誤日誌
AccessLog /var/log/boa/access_log    # 存取日誌
DocumentRoot /var/www                # 網頁根目錄
UserDir public_html
DirectoryIndex index.html            # 預設首頁
DirectoryMaker /usr/lib/boa/boa_indexer
KeepAliveMax 1000                    # 單一連線允許的最大持續請求數
KeepAliveTimeout 10                  # 兩次請求間的等待秒數
MimeTypes /etc/mime.types            # MIME 對照檔位置
DefaultType text/plain               # 無法判斷副檔名時的預設型別
CGIPath /bin:/usr/bin:/usr/local/bin # CGI 的 PATH 環境變數
Alias /doc /usr/doc                  # 路徑別名
ScriptAlias /cgi-bin/ /var/www/cgi-bin/

三、常見錯誤與解法

錯誤 1:gethostbyname: No such file or directory

把 boa.conf 裡 ServerName www.your.org.here 前面的註解符號 (#) 移除,或直接改成板子實際的位址。

錯誤 2:util.c:100:1: error: pasting "t" and "->" does not give a valid preprocessing token

這是 compat.h 的宏拼接問題,改法同前:

#define TIMEZONE_OFFSET(foo) (foo)->tm_gmtoff

錯誤 3:boa.c:211 - getpwuid: No such file or directory

嵌入式 rootfs 通常沒有完整的 /etc/passwd,把相關那段包起來:

#if 0
if (passwdbuf == NULL) {
    DIE("getpwuid");
}
if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) {
    DIE("initgroups");
}
#endif

錯誤 4:boa.c:228 - icky Linux kernel bug!

同樣用 #if 0 ... #endif 把 setuid(0) 的檢查註解掉即可。

錯誤 5:log.c:73 unable to dup2 the error log: bad file descriptor

優先用方法一:確認日誌目錄存在,且對所有使用者都有可讀寫權限。若仍不行,再考慮改 log.c:

#if 0
if (dup2(error_log, STDERR_FILENO) == -1) {
    DIE("unable to dup2 the error log");
}
#endif

改原始碼只是把警訊藏起來,真正該修的是目錄權限與路徑設定,否則日誌會靜默失效。

Copyright © Linux教程網 All Rights Reserved