[Linux] 嵌入式系統不可或缺的工具 – busybox 分析 ifconfig command

Busybox

玩過嵌入式系統的使用者,一定都會知道 Busybox,它提供一些小型 Linux command,方便在 console 端使用,以及一些 C 語言或者是 shell script 裡面,大家都知道 ifconfig 這指令,為了從 Kernel 2.6.15 轉換到 2.6.34.7 版本,原本的 Busybox 版本只有 1.0.1,現在已經到 1.18.1,轉換過程改了 Kernel netfilter 部份,以及 user space 部份 iptables extension。ifconfig 是 Busybox 其中一個指令用來查看目前有多少網路介面(network interface),來看看他是如何得到這些 interface 資訊,包含介面名稱、type、IP Adress、IP network mask、HW address 等….。

要讀取 interface 相關資訊可以透過兩種方式,一種是讀取 (IPv6 是 /proc/net/if_inet6),另一種透過 Socket 連接SOCK_DGRAM,最後用 iotcl 方式讀取 interface 相關資料,busybox 會先偵測檔案 /proc/net/dev 是否存在,如果 Kernel 有支援,就會讀取此檔案,如果不存在,則利用 socket 讀取資料。

if_readlist_proc 函式裡面:

1
2
3
4
fh = fopen_or_warn(_PATH_PROCNET_DEV, "r");
if (!fh) {
    return if_readconf();
}

看一下 /proc/net/dev 內容

1
2
3
4
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:     104       1    0    0    0     0          0         0      104       1    0    0    0     0       0          0
  eth0:21798505   51360    0    0    0     0          0         0  7693686   46844    0    0    0     0       0          0
[Read More]

[虛擬主機] VPS Linode 贈送 $100,000 美元給新註冊會員

昨天在下班前看到 Linode VPS 送出這個訊息: Linode $100,000 Giveaway!,只要在美國時間2010年12月17日早上九點開放名額1000名購買 VPS Linode 任何一種,就可以獨享 100 元美金的優惠,原本我已經有買一台 Linode 512 方案,我現在又加購一台,省下不少錢呢,到月底就不續約,然後再用新帳號繼續使用 Linode 512 服務,沒圖沒證據,底下附上我購買相關圖片 另外 VPS Linode 也提供了香港主機的服務,以及新的 Ubuntu OS 也上線了 Arch Linux 2010.05 (i386 and x86_64) CentOS 5.5 (i386 and x86_64) Debian 5.0 (i386 and x86_64) Fedora 14 (i386 and x86_64) Slackware 13.1 (i386 and x86_64) Ubuntu 10.04 LTS (i386 and x86_64) Ubuntu 10.10 (i386 and x86_64) OpenSUSE 11.0 Gentoo 2008.0 (i386 and x86_64) 參考主機放在哪裡比較適合: Datacenter Availability,您可以測試一下每個區域的連線速度 download speed test,我利用學網測試了國外的速度,發現速度比較如下: 單檔都是 100 MB: [Read More]

[資料庫] 如何轉換 Mysql varchar type 到 int type

先來說明為什麼有時候需要用到轉換 varchar 到 int 型態,就是因為 order by 的問題,幫學校修改 Mysql 錯誤排序,前人設計全部都用 varchar 型態去存資料,當然包含了學生入學年度,以前不會遇到這問題,但是到了民國100年,就會發生排序錯誤,底下來講個例子,這樣大家就可以瞭解了。 建立 test 資料表,並且先增兩個欄位分別是 a(varchar) 跟 b(int),個別輸入 100, 90 兩列資料

mysql> select * from test;
+------+------+
| a    | b    |
+------+------+
| 100  |  100 |
| 90   |   90 |
+------+------+
先針對 varchar 排序 order by a DESC
mysql> select * from test order by a desc;
+------+------+
| a    | b    |
+------+------+
| 90   |   90 |
| 100  |  100 |
+------+------+
再來針對 int 排序 order by b DESC
mysql> select * from test order by b desc;
+------+------+
| a    | b    |
+------+------+
| 100  |  100 |
| 90   |   90 |
+------+------+
[Read More]

[C/C++] 判斷檔案是否存在 file_exists

在 PHP 函式裡面,有直接 file_exists 可以使用,相當方便: 1 2 3 4 5 <?php if(file_exists("files/appleboy.c")) { echo "File found!"; } ?> 在 C 裡面該如何實做?有兩種方式如下: 1. 直接開檔 1 2 3 4 5 6 7 8 9 bool file_exists(const char * filename) { if (FILE * file = fopen(filename, "r")) { fclose(file); return true; } return false; } C++ 寫法 1 2 3 4 5 6 7 8 std::fstream foo; foo.open("bar"); if(foo.is_open() == true) std::cout << "Exist"; else std::cout << "Doesn't Exist"; 2. [Read More]

[新聞] PHP Framework Codeigniter 1.7.3 釋出 Release

CodeIgniter
剛出爐的新聞:

CodeIgniter 1.7.3 Released,1.7.3 版本的出來,最主要是修正了 Upload class 的安全性,在上一次公告就有 Upload Class Patch,只是 1.7.3 就把它納入進來,還有修正在某些情況下可能出現所有檔案,除此之外沒有其他重大修正了,看起來是為了過不久的 2.0 所準備,目前團隊都朝 2.0 發展了。 原文:

Version 1.7.3 is a security maintenance release, including a previously patched file Upload class, and a new security fix to prevent possible directory traversal in certain circumstances (back ported from a fix made to CodeIgniter 2.0 at BitBucket). There are no other significant changes. 搶先版: 下載 Codeigniter 1.7.3

[Linux] VirtualBox + Ubuntu 10.10 編譯嵌入式系統

vbox_logo2_gradient (by appleboy46)
最近使用

Ubuntu 來編譯嵌入式的環境,由於個人比較不喜歡 Fedora 的系統,所以自己用了 VirtualBox 來搭配 10.10 的 Ubuntu 系統,在這裡提醒一下,請安裝最新版的 VirtualBox 3.2.12 for Windows hosts,否則在安裝 Ubuntu 之後,繼續安裝 Guest Addition 的時候會當機喔,重開機之後可以看到桌面多出一個光碟,是要您繼續安裝 Additions VirtualBox + Ubuntu 切換到該光碟目錄

cd /media/VBOXADDITIONS_3.2.12_68302/
直接執行
sh VBoxLinuxAdditions-x86.run
重新開機就完成了,可以直接切換視窗大小...等,編譯 gcc 必須要一些 Cross tool,利用 apt-get 方式安裝:
apt-get install build-essential
安裝額外 USB 裝置: 視窗上面 Devices -> USB Devices 選擇你要的外接硬碟,會跳出 Windows 安裝額外 Driver

VirtualBox + Ubuntu 10.10 直接按 Continue Anyway VirtualBox + Ubuntu 10.10 完成 VirtualBox + Ubuntu 10.10 補上 Ubuntu 畫面 VirtualBox + Ubuntu 10.10

[Git] 版本控制: 如何使用標籤(Tag)

Git Tag 功能就如同 Cvs Tag 是一樣的,您可以在專案裡面隨意新增 Tag,方便您紀錄訊息,底下一些基本的操作來學習如何使用標籤(Tag)功能(新增標籤、以及各種不同類型標籤之間的差別)。

列出既有標籤

直接使用 git tag 即可

1
2
3
$ git tag -l
v0.1
v1.3

如果整個專案過多 Tag 也可以透過底下方式搜尋出來

1
2
3
4
5
$ git tag -l 'v1.4.2.*'
v1.4.2.1
v1.4.2.2
v1.4.2.3
v1.4.2.4
[Read More]

CodeIgniter 2.0 發展狀況(現在更棒了) 不再支援 PHP4

CodeIgniter 在官網正式公告了一篇 2.0 目前的狀況及改善 CodeIgniter 2.0 - Now with more Awesome,該篇重點莫過於 CI 2.0 將不再支援 PHP 4.0 版本了,這是一項重大改變,官方給目前版本取了一個名稱:CodeIgniterNoPhp4,看到這名字大概就可以知道官方要捨棄 PHP4 了。 自從 CodeIgniter 將所有程式碼搬到 Bitbucket 上面時,就已經宣稱不再 support PHP4,運行了好幾個月,現在以 PHP 5.1.6 來當作開發重點,底下是官方說明 CI 2.0 該注意的地方(其實還有很多地方要注意): 1. 所有類別將以 CI_ 當作前置符號 2. 因為不支援 PHP4 了,所以建構子一律改成 __construct 3. CI_Base 已經被移除,取而代之的是 CI_Controller 4. 之前有提供 Compatibility 輔助函數,目前已經支援 PHP5 了,故將此移除 開始支援 Email and Validation chaining,看一下範例: $this->email->from('your@example.com', 'Your Name') ->to('someone@example.com') ->cc('another@another-example.com') ->bcc('them@their-example.com') ->subject('Email Test') ->message('Testing the email class.') ->send(); 最後官方作者有提到一些事情:ExpressionEngine and CodeIgniter 將不再支援 PHP4,PHP4 從2000年出來,到 2007 年結束,重點來了,官方說 PHP 4 帶給您的困擾,就如同現在 Internet Explorer 6。 可以參考: What’s New in CodeIgniter 2. [Read More]

Upgrading to CodeIgniter 2.0 (升級 CI 到 2.0 版本)

2011.02.09 Update: 由於官方推出 2.0.0 的升級方式,請參考升級版本 1.7.3 到 2.0.0

CodeIgniter
在國外文章看到

Phil Sturgeon 已在今年三月發佈了一篇如何將目前 CodeIgniter 版本升級到 2.0 開發版本,轉移的經驗會在底下做介紹,PyroCMS 是該作者產品之一,也升級到 2.0 了,大家可以參考看看。

[Read More]

利用 jQuery 動態改變網站 CSS

繼前一篇所寫的『jQuery 偵測瀏覽器版本, 作業系統(OS detection)』,當我們遇到手機上網使用者,可以透過 javascript 來判斷目前使用者瀏覽器以及 OS,iPad user agent 如下: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10 我們必須分析上面字串來判斷是否用手機上網,手機網頁跟PC網頁大小一定有所不同,透過底下兩個方法來改變瀏覽器的畫面 自動導向手機網頁 動態改變 CSS 兩個方法都不錯,如果決定使用前者,建議在 Server side 那邊做判斷,底下先列出 javascript, PHP, .htaccess 判斷檢查 The JavaScript var isiPad = navigator.userAgent.match(/iPad/i) != null; The PHP $isiPad = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad'); The .htaccess RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$ RewriteRule ^(.*)$ http://ipad.yourdomain.com [R=301] 如果您在前端做判斷,那就使用 jQuery 方式: if(jQuery){ jQuery("body").addClass("jq"); } CSS 檔案: . [Read More]