2011年3月1日 星期二

install ubcd to usb disk

1. Mount ubcd503.iso to /mnt
-> sudo mount -o loop ubcd503.iso /mnt

(Read /mnt/ubcd/tools/linux/ubcd2usb/readme.txt)

2. Create mbr:
-> cd /mnt/ubcd/tools/linux/ubcd2usb
-> sudo dd if=mbr.bin of=/dev/sdX

(replacing X with your USB key drive letter)

3. Format your usb disk to fat32
-> sudo mkfs.vfat -F 32 /dev/sdX1

4. Mount your usb disk and copy all files of the extracted UBCD iso to your USB thumb drive
-> sudo mount /dev/sdX1 /usbmnt
-> sudo cp -rf /mnt/* /usbmnt

5. Install bootloader syslinux:
-> cd /mnt/ubcd/tools/linux/ubcd2usb
-> sudo ./syslinux -s -d /boot/syslinux /dev/sdX1
(install syslinux to a directory relative to /boot/syslinux on the device)

6. make the partition bootable:
-> sudo parted /dev/sdX set 1 boot on

Ref:
ubcd-extracted/ubcd/tools/linux/ubcd2usb/readme.txt

2011年2月21日 星期一

java jar signing

以下用top-down的方式講述如何對一個 jar 簽章:

keystore & key 說明:
=============

java 將用來簽章的key存在keystore中,每個key包含private key(密秘)和 public-key certificate(公開)。

一個 keystore檔案就是一個專存這些key的資料庫,而用 alias 來索引其中的一個 key。

keystore檔有一個密碼保護,而其中的每個key也可以有(也可以不設)自己的密碼保護。


簽章操作:
=======

-> jarsigner -keystore my.keystore -storepass 123456 myprogram.jar myprogram

(若這個key有設密碼保護,會再要求輸入密碼;或是直接在 jarsigner 後多加參數-keypass xxxx)

這樣就可以用 my.keystore 中的那個 alias 為 myprogram 的key,對 myprogram.jar 簽章。


如何產生keystore 和 key:
================


-> keytool -genkey -alias myprogram -keystore my.keystore -storepass 123456 -validity 1800 < myprogram.keyconf


(如果沒有先做好myprogram.keyconf,則輸手動輸入key的資訊)
這樣會產生一個 keystore 檔 my.keystore,裡面有一個 alias 叫 myprogram 的 key,此 key 的有效期限為1800天。

Ref:
Signing and Verifying JAR FilesJAR Signing

2011年2月17日 星期四

map dir to msys

Hot to map a dir from host Windows to msys ?
Ans:

Edit msys/1.0/etc/fstab, for example:

f:/mingw /mingw
f:/jdk21 /java

You also need to create the corresponding dir in msys. In this example,
mkdir /mingw
mkdir /java

2011年1月13日 星期四

Run jnlp program as root (sudo)

In Linux, a non-root user usually clicks a jnlp link in a web browser or double-clicks a jnlp file to run a Java Web Start program.
The program has no root privilege.

How to run it as root ?
-> sudo javaws http://your-website/link.jnlp

or download the jnlp file and
-> sudo javaws ~/Test/link.jnlp

Reference:

2010年12月16日 星期四

JNLP and JNI native libraries

Topic: How to use JNLP to load jar files which include native libraries ?

1. Build native libraries
* HelloWorld.dll for Windows
* libHelloWorld.so for Linux (library name must be libXXX.so in Linux !!)

(How to build those native libraries ? see jni book)

2. Pack native libraries into jar

jar -cvf Linux32_lib.jar libHelloWorld.so
jar -cvf Win32_lib.jar HelloWorld.dll

3. Add tags in jnlp
(please replace ( with <)
(resources os="Windows">
(j2se version="1.5+"/>
(nativelib href="native/Win32_lib.jar"/>
(/resources>

(resources os="Linux">
(j2se version="1.5+"/>
(nativelib href="native/Linux32_lib.jar"/>
(/resources>

4. In code, explicitly load the library
System.loadLibrary("HelloWorld");
(windows->HelloWorld.dll ; linux -> libHelloWorld.so)

2010年12月2日 星期四

TCP Keepalive mechanism

RFC1122 says

"Implementors MAY include "keep-alives" in their TCP implementations, although this practice is not universally accepted. If keep-alives are included, the application MUST be able to turn them on or off for each TCP connection, and they MUST default to off." [1]


In C, use setsockopt() to set the following arguments on a specific socket [2]
* SO_KEEPALIVE: enable tcp keepalive
* TCP_KEEPCNT: overrides tcp_keepalive_probes
* TCP_KEEPIDLE: overrides tcp_keepalive_time
* TCP_KEEPINTVL: overrides tcp_keepalive_intvl

These settings are only for the current socket.

In Linux, to see the default values (OS-wide variables) of the later 3 options:
# cat /proc/sys/net/ipv4/tcp_keepalive_time
7200
# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75
# cat /proc/sys/net/ipv4/tcp_keepalive_probes
9

This means "wait for two hours (7200 secs) before sending the first keepalive probe, and then resend it every 75 seconds. If no ACK response is received for nine consecutive times, the connection is marked as broken." [2]


Ref:

2010年5月11日 星期二

[socket] set read/write timeout values for blocking socket

Set timeout to 10 seconds when doing read () or write () a blocking sock :

#define SOCK_RCV_TIMEOUT 10
#define SOCK_SND_TIMEOUT 10

void set_rw_timeout (int sock, int clear_timeout)
{
struct timeval rw_timeout;

memset (&rw_timeout, 0, sizeof (struct timeval));
rw_timeout.tv_sec = clear_timeout ? 0 : SOCK_RCV_TIMEOUT;

if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &rw_timeout, sizeof (struct timeval))) {
fprintf (stderr, "setsockopt SO_RCVTIMEO failed\n");
}

memset (&rw_timeout, 0, sizeof (struct timeval));
rw_timeout.tv_sec = clear_timeout ? 0 : SOCK_SND_TIMEOUT;

if (setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO, &rw_timeout, sizeof (struct timeval))) {
fprintf (stderr, "setsockopt SO_SNDTIMEO failed\n");
}
}