2009年7月5日

getsockname(2)

soctnameを取得する関数。

用例
len = sizeof(saddr);
if (getsockname(fd1, (struct sockaddr *)&saddr, &len) != 0){
 perror("getsockname");
 exit(1);
}
printf("port number is %d.\n", ntohs(saddr.sin_port));
メモ
manページを眺めても使い方が良く分かんらんかった。試行錯誤の上、上記に到達。

2009年6月24日

自宅サーバ(Debian/etch)にsquid導入

Debian etchにsquid導入した際のメモ。



インストール

# aptitude install squid

設定

# cp -p /etc/squid/squid.conf /etc/squid/squid.conf.org
# vi /etc/squid/squid.conf
以下を追加
acl localnet src 192.168.1.0/24
http_access allow localnet
以下を変更
http_port 8080
cache_dir ufs /var/spool/squid 500 16 256

関連記事

カーネルデバッグ環境の設定

また今日もマニアックなことに挑戦。
CentOSでcrashコマンドを試してみることに。


詳細はこちら




RHEL4系

例)
[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.9-78.0.1.EL #1 Tue Jul 22 17:50:01 EDT 2008 i686 i686 i386 GNU/Linux
[root@localhost rpm-work]# wget http://ftp.redhat.com/pub/redhat/linux/updates/enterprise/4AS/en/os/Debuginfo/i686/RPMS/kernel-debuginfo-2.6.9-78.0.1.EL.i686.rpm
[root@localhost rpm-work]# rpm -ivh kernel-debuginfo-2.6.9-78.0.1.EL.i686.rpm

履歴

2009年06月24日 RHEL4系記載追加
2009年03月05日 新規作成

2009年6月15日

品川アクアスタジアム

今日は水族館(品川アクアスタジアム)に行きました。
娘がまだ小さいので、電車はこちらも周りも娘本人も辛かろうはなから諦めて、車で行きました。車は、ホテルパシフィク東京の地下駐車場(20分で200円)にとめました。
水族館は、まぁ、30分も持たないようなこじんまりとした大きさなんですが、「海中トンネル」と「ペンギン大陸」は見ものです。「海中トンネル」には、なんとマンタも泳いでました。娘も大興奮です。
さて、ここの本当の見ものは、イルカショー(とアシカショー)です。
15分ほどのショーですが、イルカとクジラが飛びはねて、ご希望なら水しぶきを頭からかぶることも出来て、楽しいことこの上ないです。ショーが終わった後も、調教師によるトレーニングをそのまま見学できるし、トレーニングが終わっても数頭のイルカがショープールに残っていて、まったりと余韻が楽しめます。
座席も相当数あって、休日も朝一なら、あまり窮屈な思いもせず、ショーやその後のイルカ達を楽しめます。欠点は、館内の飲食物のレパートリーの少なさと高さ。正直、持込(していいのかな?)推奨です。


2009年6月6日

Linuxのcore dumpの中身についてその3

coreファイル内のnoteセクションにどんな情報が含まれているのか調べてみた。


まず、objdumpでセクション情報を調べる

$ objdump -h core.3313
core.3313:     file format elf32-i386
Sections:
Idx Name          Size      VMA       LMA       File off  Algn
0 note0         000001d8  00000000  00000000  000001d4  2**0
CONTENTS, READONLY
...

readelfで見てみる

$ readelf -a core.3313
...
Notes at offset 0x000001d4 with length 0x000001d8:
Owner         Data size       Description
CORE          0x00000090      NT_PRSTATUS (prstatus structure)
CORE          0x0000007c      NT_PRPSINFO (prpsinfo structure)
CORE          0x00000090      NT_AUXV (auxiliary vector)

出力の一番最後にnoteの情報が出ている。

更に、note0のダンプを取り、readelfの結果と付き合わせてみる。

$ objdump -s -j note0 core.3313
core.3313:     file format elf32-i386
Contents of section note0:
0000 05000000 90000000 01000000 434f5245  ............CORE
0010 00000000 0b000000 00000000 00000000  ................
0020 0b000000 00000000 00000000 f10c0000  ................
0030 460c0000 f10c0000 460c0000 00000000  F.......F.......
0040 e7030000 00000000 87130000 00000000  ................
0050 00000000 00000000 00000000 f43fa300  .............?..
0060 b01db4bf 01000000 a04c8f00 00000000  .........L......
0070 981db4bf 341eb4bf 7b000000 7b000000  ....4...{...{...
0080 00000000 33000000 ffffffff 79830408  ....3.......y...
0090 73000000 82020100 80150590 7b000000  s...........{...
00a0 00000000 05000000 7c000000 03000000  ........|.......
00b0 434f5245 00000000 00520000 00064000  CORE.....R....@.
00c0 f401f401 f10c0000 460c0000 f10c0000  ........F.......
00d0 460c0000 6269675f 61727261 79000000  F...big_array...
00e0 00000000 2e2f6269 675f6172 72617920  ...../big_array
00f0 00000000 00000000 00000000 00000000  ................
0100 00000000 00000000 00000000 00000000  ................
0110 00000000 00000000 00000000 00000000  ................
0120 00000000 00000000 00000000 00000000  ................
0130 00000000 05000000 90000000 06000000  ................
0140 434f5245 00000000 20000000 00c43d00  CORE.... .....=.
0150 21000000 00c03d00 10000000 fffbeb0f  !.....=.........
0160 06000000 00100000 11000000 64000000  ............d...
0170 03000000 34800408 04000000 20000000  ....4....... ...
0180 05000000 07000000 07000000 00000000  ................
0190 08000000 00000000 09000000 80820408  ................
01a0 0b000000 f4010000 0c000000 f4010000  ................
01b0 0d000000 f4010000 0e000000 f4010000  ................
01c0 17000000 00000000 0f000000 3b1fb4bf  ............;...
01d0 00000000 00000000                    ........

noteセクションの構造は、以下のようになっている。

[ ][ ][ ][ ] namesz(4byte)  ノート名の長さ
[ ][ ][ ][ ] descsz(4byte)  ノート本体の長さ
[ ][ ][ ][ ] type(4byte)    ノートのタイプ
[ ][ ][ ]…[ ] name(Nbyte)  ノート名(長さはnamesz) ※4バイト境界
[ ][ ][ ]…[ ] desc(Nbyte)  ノート本体(長さはdescs) ※4バイト境界

次に、NT_PRSTATUS (prstatus structure)、NT_PRPSINFO (prpsinfo structure)、NT_AUXV (auxiliary vector)といったノートのタイプとノート本体。ノート名は只のテキスト情報なので、ダンプ結果を見れば一目瞭然と思う。

ノートのタイプは、/usr/include/elf.hに定義がある。

$ grep "define NT_" /usr/include/elf.h
#define NT_PRSTATUS     1               /* Contains copy of prstatus struct */
#define NT_FPREGSET     2               /* Contains copy of fpregset struct */
#define NT_PRPSINFO     3               /* Contains copy of prpsinfo struct */
#define NT_PRXREG       4               /* Contains copy of prxregset struct */
#define NT_TASKSTRUCT   4               /* Contains copy of task structure */
#define NT_PLATFORM     5               /* String from sysinfo(SI_PLATFORM) */
#define NT_AUXV         6               /* Contains copy of auxv array */
#define NT_GWINDOWS     7               /* Contains copy of gwindows struct */
#define NT_ASRS         8               /* Contains copy of asrset struct */
#define NT_PSTATUS      10              /* Contains copy of pstatus struct */
#define NT_PSINFO       13              /* Contains copy of psinfo struct */
#define NT_PRCRED       14              /* Contains copy of prcred struct */
#define NT_UTSNAME      15              /* Contains copy of utsname struct */
#define NT_LWPSTATUS    16              /* Contains copy of lwpstatus struct */
#define NT_LWPSINFO     17              /* Contains copy of lwpinfo struct */
#define NT_PRFPXREG     20              /* Contains copy of fprxregset struct*/
#define NT_VERSION      1               /* Contains a version string.  */

ノートの本体のうち、今回出てきたprstatus structureとprpsinfo structureは、/usr/include/sys/procfs.hに定義がある。

$ cat /usr/include/sys/procfs.h
...
struct elf_prstatus
{
struct elf_siginfo pr_info;         /* Info associated with signal.  */
short int pr_cursig;                /* Current signal.  */
unsigned long int pr_sigpend;       /* Set of pending signals.  */
unsigned long int pr_sighold;       /* Set of held signals.  */
__pid_t pr_pid;
__pid_t pr_ppid;
__pid_t pr_pgrp;
__pid_t pr_sid;
struct timeval pr_utime;            /* User time.  */
struct timeval pr_stime;            /* System time.  */
struct timeval pr_cutime;           /* Cumulative user time.  */
struct timeval pr_cstime;           /* Cumulative system time.  */
elf_gregset_t pr_reg;               /* GP registers.  */
int pr_fpvalid;                     /* True if math copro being used.  */
};
...
struct elf_prpsinfo
{
char pr_state;                      /* Numeric process state.  */
char pr_sname;                      /* Char for pr_state.  */
char pr_zomb;                       /* Zombie.  */
char pr_nice;                       /* Nice val.  */
unsigned long int pr_flag;          /* Flags.  */
unsigned short int pr_uid;
unsigned short int pr_gid;
int pr_pid, pr_ppid, pr_pgrp, pr_sid;
/* Lots missing */
char pr_fname[16];                  /* Filename of executable.  */
char pr_psargs[ELF_PRARGSZ];        /* Initial part of arg list.  */
};
...

auxiliary vectorとは、id(4byte)とその値Val(4byte)が交互に並んだ構造をしており、id=0となったところが構造の終わりとなっている。

[id(4byte)][val(4byte)][id(4byte)][val(4byte)]...[id(4byte)][val(4byte)]

idは/usr/include/elf.hに定義されている

$ grep "define AT_" /usr/include/elf.h
#define AT_NULL         0               /* End of vector */
#define AT_IGNORE       1               /* Entry should be ignored */
#define AT_EXECFD       2               /* File descriptor of program */
#define AT_PHDR         3               /* Program headers for program */
#define AT_PHENT        4               /* Size of program header entry */
#define AT_PHNUM        5               /* Number of program headers */
#define AT_PAGESZ       6               /* System page size */
#define AT_BASE         7               /* Base address of interpreter */
#define AT_FLAGS        8               /* Flags */
#define AT_ENTRY        9               /* Entry point of program */
#define AT_NOTELF       10              /* Program is not ELF */
#define AT_UID          11              /* Real uid */
#define AT_EUID         12              /* Effective uid */
#define AT_GID          13              /* Real gid */
#define AT_EGID         14              /* Effective gid */
#define AT_CLKTCK       17              /* Frequency of times() */
#define AT_PLATFORM     15              /* String identifying platform.  */
#define AT_HWCAP        16              /* Machine dependent hints about
#define AT_FPUCW        18              /* Used FPU control word.  */
#define AT_DCACHEBSIZE  19              /* Data cache block size.  */
#define AT_ICACHEBSIZE  20              /* Instruction cache block size.  */
#define AT_UCACHEBSIZE  21              /* Unified cache block size.  */
#define AT_IGNOREPPC    22              /* Entry should be ignored.  */
#define AT_SYSINFO      32
#define AT_SYSINFO_EHDR 33
#define AT_L1I_CACHESHAPE       34
#define AT_L1D_CACHESHAPE       35
#define AT_L2_CACHESHAPE        36
#define AT_L3_CACHESHAPE        37

2009年5月30日

Superfetchを止める(Vistaメモ)

Vistaに乗り換えて半年程になる。最近、なにもしていないのにディスクがカリカリいっているのがとても気になっていたのだが、こんな記事を発見。

ふーん。Superfetchというサービスが余計なことをしているのか。

しばらく止めて様子を見てみることにする。

因みに止めるとタスクマネージャから確認できるメモリが40MBほど減った。

2009年5月28日

カーネルのライブHacking(RHEL AS 4U4)

lockdのnlm_hosts構造体の内容を確認することがあったので、その際のcrashコマンドの使い方をメモ書きしておく。


crashコマンドを起動

# crash
...
crash>

デバッグモジュールの読み込み

crash> mod -s lockd lockd.ko.debug

構造体のアドレスを確認

crash> list nlm_hosts
d0b8c7a0

構造体の内容表示

crash> struct nlm_host d0b8c7a0
...

以上

debuginfoの設定(RHEL AS 4U4)

故あって、RHEL AS 4U4にカーネルdebuginfoを設定し、crashコマンドが使えるようにすることに。



まず、カーネルのバージョンを確認。

# uname -a
Linux localhost.localdomain 2.6.9-42.EL #1 Wed Jul 12 23:16:43
EDT 2006 i686 athlon i386 GNU/Linux

debuginfoパッケージを入手。

# wget http://ftp.redhat.com/pub/redhat/linux/updates/enterpri
se/4AS/en/os/Debuginfo/i386/RPMS/kernel-debuginfo-2.6.9-42.EL.
i686.rpm

インストール。

# rpm -ihv kernel-debuginfo-2.6.9-42.EL.i686.rpm

最後に確認。

# crash
...
crash>

crashコマンドのプロンプトが出ればOK。

2009年5月24日

Linuxで回線シミュレーション - 調査

野暮用で、通信パケットの遅延を試してみたくなった。


Google先生に聞いてみたら、Dummynet、NISTNet、iproute2というものを教えてくれた。

RHEL(4U4,5.1で確認)で使うには、iproute2が良いみたい。標準でカーネルが対応しているし、"iproute"というパッケージで標準で用意されている。

使い方は、以下のblogやサイトを参考にした。

Linuxのシステムロケール設定ってどこでするんだっけ?

システムロケール(もしくはシステムの標準のロケール)は、ディストリビューションによって設定場所が異なる。()内は確認版数。

RHEL(4,5)

/etc/sysconfig/i18n

Debian(etch)

/etc/default/local


2009年4月19日

自宅サーバ(Debian/etch)をnmap(Windows XP)でポートスキャン

自宅サーバ(Debian/etch)のセキュリティ検査をするのに、Windows XPマシンからポートスキャンしてみた。

1. http://insecure.org/からnmap-4.85BETA7-setup.exeをダウンロード
2. nmap-4.85BETA7-setup.exeを実行してインストール
3. 以下を実行
C:\Program Files\Nmap>nmap -P0 -n -sS -T4 -p1-65535 192.168.1.132

Starting Nmap 4.85BETA7 ( http://nmap.org ) at 2009-04-19 11:43 東京 (標準時)
Interesting ports on 192.168.1.132:
Not shown: 65528 closed ports
PORT    STATE SERVICE
21/tcp  open  ftp
22/tcp  open  ssh
80/tcp  open  http
111/tcp open  rpcbind
113/tcp open  auth
443/tcp open  https
901/tcp open  samba-swat
MAC Address: xx:xx:xx:xx:xx:xx (Matsushita Electric IND. CO)

Nmap done: 1 IP address (1 host up) scanned in 81.48 seconds

C:\Program Files\Nmap>
4. rpcbind、auth、samba-swatは自宅サーバには必要ないので止めてみる。あと、ftpはscp(sftp)で代替するのでこれも止める。
<rpcbindを止める>
# /etc/init.d/portmap stop
Stopping portmap daemon....
# update-rc.d -f portmap remove
Removing any system startup links for /etc/init.d/portmap ...
   /etc/rc0.d/S32portmap
   /etc/rc1.d/K81portmap
   /etc/rc2.d/S18portmap
   /etc/rc3.d/S18portmap
   /etc/rc4.d/S18portmap
   /etc/rc5.d/S18portmap
   /etc/rc6.d/S32portmap
   /etc/rcS.d/S43portmap
#

<authを止める>
# vi /etc/inetd.conf
...
ident          stream  tcp     wait    identd  /usr/sbin/identd        identd
↓identとある行をコメントアウト
#ident          stream  tcp     wait    identd  /usr/sbin/identd        identd
...
# pkill -HUP inetd

<samba-swatを止める>
# vi /etc/inetd.conf
...
swat           stream  tcp     nowait.400      root    /usr/sbin/tcpd  /usr/sbin/swat
↓swatとある行をコメントアウト
#swat           stream  tcp     nowait.400      root    /usr/sbin/tcpd  /usr/sbin/swat
...
# pkill -HUP inetd

<ftpを止める>
# /etc/init.d/vsftpd stop
Stopping FTP server: vsftpd.
# update-rc.d -f vsftpd remove
Removing any system startup links for /etc/init.d/vsftpd ...
   /etc/rc0.d/K20vsftpd
   /etc/rc1.d/K20vsftpd
   /etc/rc2.d/S20vsftpd
   /etc/rc3.d/S20vsftpd
   /etc/rc4.d/S20vsftpd
   /etc/rc5.d/S20vsftpd
   /etc/rc6.d/K20vsftpd
5. 再度nmapを実行し、効果を確認
C:\Program Files\Nmap>nmap -P0 -n -sS -T4 -p1-65535 192.168.1.132

Starting Nmap 4.85BETA7 ( http://nmap.org ) at 2009-04-19 19:34 東京 (標準時)
Interesting ports on 192.168.1.132:
Not shown: 65532 closed ports
PORT    STATE SERVICE
22/tcp  open  ssh
80/tcp  open  http
443/tcp open  https
MAC Address: xx:xx:xx:xx:xx:xx (Matsushita Electric IND. CO)

Nmap done: 1 IP address (1 host up) scanned in 52.02 seconds

C:\Program Files\Nmap>

自宅サーバ(Debian/etch)のiptablesで、とあるホストを遮断

やってみたのでメモしておく。

/root/myfirewall.shを作成し、アクセス拒否したいipアドレスを登録。
# vi /root/myfirewall.sh
#!/bin/sh
iptables -I INPUT -s XXX.XXX.XXX.XXX -j DROP
定義に問題ないか確認。
# /bin/sh /root/myfirewall.sh
# iptables -L
...
DROP       0    --  xxxx.jp  anywhere ← 拒否したいIPアドレスのFQDN名を確認
...
#
etc/network/interfacesに書き青文字の行を追加。
iface eth0 inet dhcp
        pre-up /bin/sh /root/myfirewall.sh $IFACE $IF_ADDRESS
再起動しても定義がされていることを確認。
# shutdown -r now
...
#  iptables -L
...
DROP       0    --  xxxx.jp  anywhere ← 再起動してもちゃんとある。
...
#