9.28.2011

Solaris: How to find if the process is 64-bit or 32-bit

Solaris: 動作中のプロセスが 64bit か 32bit か調べる方法

プロセスが64bitで稼動しているか、32bitで稼動しているかを調べるには pflags コマンドを使えばよい。

# pflags 2594 |grep model
        data model = _ILP32  flags = ORPHAN|MSACCT|MSFORK

データモデルの内容を確認。
_ILP32 => 32bit (int / long / pointer が 32 ビット)
_LP64  => 64bit (long /pointer が 64 ビット)
_LLP64 => 64bit Windows (long long / pointer が 64ビット)

参考:
http://www.ginnokagi.com/2009/02/weblogic.html
http://blogs.oracle.com/yappri/entry/32_or_64

9.26.2011

Format the JavaVM GC log: pt.2

GCログの整形 その2

GCログのタイムスタンプ(JavaVMを起動してからの秒数)をシステム時刻に整形するスクリプト。
ファイルの最終更新時刻と最終エントリのタイムスタンプを突き合わせる発想は凄い。

http://www.theserverlabs.com/blog/2010/05/26/human-readable-jvm-gc-timestamps/

以下転載

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
   
import sys, os, datetime
   
# true if string is a positive float
def validSeconds(str_sec):
    try:
        return 0 < float(str_sec)
    except ValueError:
        return False
   
# show usage              
if len(sys.argv) < 2:
    print "Usage: %s <gc.log>" % (sys.argv[0])
    sys.exit(1)
   
file_str = sys.argv[1]
lastmod_date = datetime.datetime.fromtimestamp(os.path.getmtime(file_str))
   
file = open(file_str, 'r')
lines = file.readlines()
file.close()
   
# get last elapsed time
for line in reversed(lines):
    parts = line.split(':')
    if validSeconds(parts[0]):
        break
   
# calculate start time
start_date = lastmod_date - datetime.timedelta(seconds=float(parts[0]))
   
# print file prepending human readable time where appropiate
for line in lines:
    parts = line.split(':')
    if not validSeconds(parts[0]):
        print line.rstrip()
        continue
    line_date = start_date + datetime.timedelta(seconds=float(parts[0]))
    print "%s: %s" % (line_date.isoformat(), line.rstrip())

Format the JavaVM GC log: pt.1

GCログの整形 その1

GCログから数値だけを抽出したい場合、sed で文字を消してしまうのが一番簡単。

sed –e 's/[^0-9.]/ /g'

9.25.2011

Uninstalling Eclipse plugins

Eclipse プラグインのアンインストール

現行のEclipseでは、プラグインのアンインストール機能が装備されている。

「Help」-「About Eclipse SDK」から「Installation Details」。

参考:
http://onlineconsultant.jp/pukiwiki/?Eclipse%20%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%E3%81%AE%E3%82%A2%E3%83%B3%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB

9.24.2011

Overloading constructor in Python

Python: コンストラクタのオーバーロード

・パラメータの型によって挙動を変える

1
2
3
4
5
6
7
class Widget():
  def __init__(self, arg):
    if isinstance(arg, int): print arg * 10
    elif isinstance(arg, basestring): print arg + '0'
 
Widget(1234# 12340
Widget('1234'# 12340

・パラメータの数によって挙動を変える

1
2
3
4
5
6
7
class Widget():
  def __init__(self, *args):
    if len(args) == 1: print '1 arg.'
    elif len(args) == 2: print '2 args.'
 
Widget(1# 1 arg.
Widget(1, 2# 2 args.

・個数と型を両方チェック

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Widget():
  def __init__(self, *args):
    def check(*types):
      if len(args) != len(types): return False
      for i, arg in enumerate(args):
        if not isinstance(arg, types[i]): return False
      return True
    if check(int): print args[0]
    elif check(int, int, basestring): print args[2]
    else: print 'other'
 
Widget(1# 1
Widget(1, 2, '3'# 3
Widget('3'# other

9.20.2011

Printing to standard error in Python

Python: 標準エラーへの出力

Python 2.x系の場合

1
2
3
4
5
6
>>> import sys
>>> sys.stderr.write('STDERR')
STDERR
>>> print >> sys.stderr, 'STDERR'
STDERR
>>>

参考:
http://diveintopython.org/scripts_and_streams/stdin_stdout_stderr.html

9.18.2011

UML modeling in Eclipse pt.2

Eclipse で UMLモデリング その2

EclipseUML のFree版を使うという手もある。
http://www.ejb3.org/download.html

jarファイルをダウンロードして実行すると、インストーラーが走る。

eclipse.ini にメモリオプションを追加するのを忘れずに。
javaプロジェクト以外には使えないようだ。

その他のプラグイン一覧。
http://www.eclipsewiki.net/eclipse/index.php?UML%A5%E2%A5%C7%A5%EA%A5%F3%A5%B0%A5%D7%A5%E9%A5%B0%A5%A4%A5%F3

9.16.2011

UML modeling in Eclipse

Eclipse でUMLモデリング

AmaterasUML というプラグインを使えば、Eclipse 上でクラス図やシーケンス図を作ることができる。

前提として、GEFの導入が必要。

・GEF
Eclipse上で「Help」-「Install New Software...」を選択。
「Work with」に http://download.eclipse.org/tools/gef/updates を指定して「Add」。
Nameは「GEF」など適当に。

表示される「GEF SDK x.x.x」(最新版でOK)をインストール。

・AmaterasUML
http://amateras.sourceforge.jp/cgi-bin/fswiki/wiki.cgi?page=AmaterasUML
zip ファイルをダウンロードして、展開したjarファイル群を plugins ディレクトリに入れる。
Eclipse が新しければ、dropins 配下でも構わない。その後、Eclipse を再起動。

参考:
http://www.eclipse.org/gef/

9.10.2011

Python: Exceptions of urllib2 - opener.open

Python: urllib2 – opener.open の例外

opener.open() を行う際、urllib2.URLError だけの把捉だけでは不十分。

予め「import socket」し、「socket.timeout」もキャッチする必要がある。

9.04.2011

Apache: CVE-2011-3192 httpd: multiple ranges DoS

Apache 脆弱性 CVE-2011-3192 のメモ

いわゆるApache Killer 関連について。

複数の(大抵は1バイト・インクリメントな)rangeヘッダ要求によるhttpdプロセスサイズ肥大化の主役は
apr_bucket という構造体。

Apache 2.0系の場合、ソースは srclib\apr-util\include\apr_buckets.h にあった。

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
 * apr_bucket structures are allocated on the malloc() heap and
 * their lifetime is controlled by the parent apr_bucket_brigade
 * structure. Buckets can move from one brigade to another e.g. by
 * calling APR_BRIGADE_CONCAT(). In general the data in a bucket has
 * the same lifetime as the bucket and is freed when the bucket is
 * destroyed; if the data is shared by more than one bucket (e.g.
 * after a split) the data is freed when the last bucket goes away.
 */
struct apr_bucket {
    /** Links to the rest of the brigade */
    APR_RING_ENTRY(apr_bucket) link;
    /** The type of bucket.  */
    const apr_bucket_type_t *type;
    /** The length of the data in the bucket.  This could have been implemented
     *  with a function, but this is an optimization, because the most
     *  common thing to do will be to get the length.  If the length is unknown,
     *  the value of this field will be (apr_size_t)(-1).
     */
    apr_size_t length;
    /** The start of the data in the bucket relative to the private base
     *  pointer.  The vast majority of bucket types allow a fixed block of
     *  data to be referenced by multiple buckets, each bucket pointing to
     *  a different segment of the data.  That segment starts at base+start
     *  and ends at base+start+length. 
     *  If the length == (apr_size_t)(-1), then start == -1.
     */
    apr_off_t start;
    /** type-dependent data hangs off this pointer */
    void *data;   
    /**
     * Pointer to function used to free the bucket. This function should
     * always be defined and it should be consistent with the memory
     * function used to allocate the bucket. For example, if malloc() is
     * used to allocate the bucket, this pointer should point to free().
     * @param e Pointer to the bucket being freed
     */
    void (*free)(void *e);
    /** The freelist from which this bucket was allocated */
    apr_bucket_alloc_t *list;
};

参考:
http://www.ipa.go.jp/security/ciadr/vul/20110831-apache.html
http://httpd.apache.org/download.cgi#apache20
http://d.hatena.ne.jp/nice20/20110829/p1