2025年4月22日 星期二

【Ubuntu】手動建置lighttpd+FastCGI環境

compile-and-install-lighttpd.md

lighttpd

安裝相依套件

sudo apt-get install autoconf sudo apt-get install automake sudo apt-get install libtool sudo apt-get install m4 sudo apt-get install pkg-config sudo apt-get install libpcre2-dev sudo apt-get install zlib1g-dev

抓取原始碼

git clone https://git.lighttpd.net/lighttpd/lighttpd1.4.git cd lighttpd1.4 git pull

編譯及安裝

  1. In generic PC platform.
cd lighttpd1.4 ./autogen.sh ./configure -C --prefix=/usr/local # ./configure --help for additional options make -j 32 sudo make install
  1. Cross-compiling with arm platform, add toolchain prefix(such as arm-linux) in configure stage.
./configure --build=arm-linux --host=x86_64-pc-linux-gnu --prefix=/usr/local --disable-static --enable-shared

執行及測試

  1. Create lighttpd.conf like following.
server.document-root = "/tmp" server.bind = "127.0.0.1" server.port = 8080 mimetype.assign = (".txt" => "text/plain", ".html" => "text/html" )
  1. Create a simple file to serve:
echo "Hello World!" > /tmp/hello.txt
  1. Running lighttpd
lighttpd -D -f lighttpd.conf & #Run in background
  1. Visit http://127.0.0.1:8080/hello.txt

FastCGI

Install build requires

sudo apt-get install gcc make m4 autoconf automake libtool

Build code

./autogen.sh ./configure make make install

Running sample code with lighttpd

  1. Create lighttpd.conf like following.
server.document-root = "/tmp" server.bind = "127.0.0.1" server.port = 8080 mimetype.assign = (".txt" => "text/plain", ".html" => "text/html" ) server.modules += ( "mod_fastcgi" ) fastcgi.debug = 1 fastcgi.server = (   "/echo-x" => ((     "bin-path" => "/home/jeff/fcgi2/examples/echo-x",     "socket" => "/tmp/echo-x.sock",     "check-local" => "disable",     "max-procs" => 2,   )) )
  1. Running lighttpd
lighttpd -D -f lighttpd.conf & #Run in background
  1. Visit http://127.0.0.1:8080/echo-x

2024年9月20日 星期五

Useful Git command

### 1. Fetch all remote branches ```shell git fetch origin ``` ### 2. Pull update to local branch ```bash git pull origin main --rebase --autostash ``` ### 3. Rebase other branch to main branch ```bash # checkout to main branch git checkout main git rebase xxx ``` ### 4. Show branchs ``` bash # show local branch git branch # show remote branch git branch -a ``` ### 5. Create branch ``` bash git branch ``` ### 6. Switch branch ``` bash git checkout ``` ### 7. Commit code change ``` bash # In one line. git commit -m # Add commit message from file git commit -F # Edit commit message with editor git commit -a ``` ### 8. Revert ``` bash #Reset git reset HEAD # 還原 "指定" 的檔案狀態 git reset HEAD # 還原 "全部" 的檔案狀態 #Checkout git checkout -- # 還原 "指定" 的檔案狀態 git checkout # 還原 "全部" 的檔案狀態 ``` ### 9. Compare ``` bash # compare difference git diff # show difference file git status ``` ### 10. Push ``` bash # first time git push -u origin # normal git push # force git push -f ```

Append ccflag in yocto bbfile

Could append ccfllag with define flag which wanna to append like following ```bash # CFLAGS_append_{actived_machine_name} = "-DHT_QCA_WIFI " CFLAGS_append_chita-clr = "-DHT_QCA_WIFI " ```

2024年3月18日 星期一

【拆雷實錄】fragmeted IP 封包追蹤

最近在Trace fragmeted IP 封包無法被原本程式加的rule "-A INPUT -f -j DROP"阻擋,原因是他是加在input表,其優先權是低於raw表,順序是在defrag之後,這時被切割的packet已經經過defrag重組完,所以無法match fragmented的flag,所以按下面這篇文章需要將rule加在raw表,因為raw表的優先權比defrag高。 [https://blog.csdn.net/caofengtao1314/article/details/120047179](https://blog.csdn.net/caofengtao1314/article/details/120047179) 更改過的rule指令會像是這樣 ```bash iptables -t raw -I PREROUTING -i erouter0 -f -j DROP # 打LOG方便追蹤 iptables -t raw -I PREROUTING -i erouter0 -f -j LOG --log-prefix "[ATK] IP fragmented" --log-level 1 ``` 理論上改成這樣就OK,但在比較後面的kernel版本變得比較複雜,defrag變得比raw表的優先權高,變成就算把上面的rule加到raw表也檔不住,所以按下面這篇文章說明需要在load iptable_raw時傳遞參數 ```bash modprobe iptable_raw raw_before_defrag=1 ``` [https://serverfault.com/questions/964072/how-to-block-incoming-ipv6-fragments](https://serverfault.com/questions/964072/how-to-block-incoming-ipv6-fragments) 而如果是builtin的kernel module,則需要透過Bootloadert傳遞參數或定義Kernel command line(CONFIG_CMDLINE)在Kernel config,在我的情況是第二種builtin的kernel module,然後在不更改Bootloader的前提,所以我需要加Kernel command line ```ini CONFIG_CMDLINE_BOOL=y CONFIG_CMDLINE="raw_before_defrag=1" ```

2024年3月12日 星期二

【拆雷實錄】ping 8.8.8.8 data length問題

最近遇到busybox ping 8.8.8.8的怪問題,當ping的data length > 88就ping不通,後面根據google的一篇文章,修改unpack function,拿掉datalen的檢查就OK了。 ## 程式碼 [https://gist.github.com/JeffTsengTwn/439e7660958709b388c9cc7eeabc21fb#file-ping-c](https://gist.github.com/JeffTsengTwn/439e7660958709b388c9cc7eeabc21fb#file-ping-c)

2023年10月19日 星期四

Raspberry PI 3 B+ Yocto研究筆記(一)

## 前言 因為在工作上會在RDK-B的框架下開發,而RDK-B的Build system就是用Yocto,終於最近比較有動力,就搭建一下環境及手邊樹梅派的板子,把這部份摸熟一點也記錄一下。 ## Yocto簡介 做嵌入式軟體開發應該都會常常聽到BuildRoot和Yocto,他們都是一種建立Embedded Linux distribution的build system,關於更多Yocto介紹可以到他們官網了解更多,這邊就不再贅述,接下來就是開始建置環境。 ## 環境建置 首先需要在一台電腦上先安裝虛擬磯的環境,我是習慣用VMWare,接著就是開一個新的虛擬磯,大概的配置如下: 1. CPU核心:8 2. RAM:8G 3. OS:20.04.6 LTS 4. Storage: 120 GB 一切都就緒後,就可以開始安裝相依性的套件 ## 相依套件安裝 在編譯Yocto之前需要以下的相依套件,更多細節可參考 https://docs.yoctoproject.org/brief-yoctoprojectqs/index.html ```bash $ sudo apt install gawk wget git diffstat unzip texinfo gcc build-essential chrpath socat cpio python3 python3-pip python3-pexpect xz-utils debianutils iputils-ping python3-git python3-jinja2 libegl1-mesa libsdl1.2-dev python3-subunit mesa-common-dev zstd liblz4-tool file locales libacl1 $ sudo locale-gen en_US.UTF-8 ``` ## 配置及編譯 首先當然需要先下載Yocto source code,我們先建立一個資料夾,並透過git clone source code. ```bash mkdir my-rpi && cd my-rpi git clone -b kirkstone git://git.yoctoproject.org/poky.git ``` 接著就是下載Raspberry Pi的meta層 ```bash git clone -b kirkstone git://git.yoctoproject.org/meta-raspberrypi ``` 初始化yocto開發環境 ```bash source poky/oe-init-build-env build-rpi ``` 加入剛剛下載的Raspberry Pi的meta層 ```bash bitbake-layers add-layer ../meta-raspberrypi ``` 修改配置 ```bash sed -i 's/^MACHINE.*/MACHINE ?= "raspberrypi3"/g' conf/local.conf sed -i '/^#DL_DIR ?= "${TOPDIR}\/downloads"/ a DL_DIR ?= \"${HOME}/yocto/downloads"' conf/local.conf sed -i 's/^PACKAGE_CLASSES.*/PACKAGE_CLASSES ?= "package_ipk"/g' conf/local.conf # 設定透過uboot開機 echo 'RPI_USE_U_BOOT = "1"' >> conf/local.conf #設定開啟uart console echo 'ENABLE_UART = "1"' >> conf/local.conf ``` 編譯image ```bash bitbake core-image-minimal ``` 編譯完成後image大概會在下面的路徑 ```bash /my-rpi/build-rpi/tmp/deploy/images/raspberrypi3/core-image-minimal-raspberrypi3.wic.bz2 ``` 他是一個symbollink實際上對應到像這樣檔名的image ```bash core-image-minimal-raspberrypi3-20231111193508.rootfs.wic.bz2 ``` 之後就可以透過balenaEtcher等工具燒錄image到SD卡,再確認開機後的console有帶起來,這樣就大功告成囉! ## 參考資料 [使用 Yocto 打造你的 Raspberry Pi 系統](https://e61983.github.io/2021-07-26-building-raspberry-pi-systems-with-yocto/)

2013年11月14日 星期四

BluetoothChatService改良

最近都在處理android藍芽傳輸的問題,雖然以前處理過不過被eclipse吃掉了,後面也忘記怎麼用,然後同學說官方有提供bluetooth chat的範例直接改比較快,但後來發現此範例字串有漏接的情況,最後改BluetoothchatService中ConnectThread的run部份解決了此問題,程式碼如下 ```Java public void run() { Log.i(TAG, "BEGIN mConnectedThread"); // Keep listening to the InputStream while connected while (true) { Scanner scanner = new Scanner(mmInStream); while( scanner.hasNext()) { String readMessage = scanner.nextLine(); mHandler.obtainMessage(Oscillator.MESSAGE_READ, -1, -1, readMessage) .sendToTarget(); } } } ```