LeopardでのApacheモジュール作成作業記録

Apacheモジュールの作成とgdbとloggerでのデバッグ方法 - よねのはてな

あまりに素晴らしいエントリなのでLeopardですこし試してみました。これは作業記録です。

作業前の状態

$ httpd -v
Server version: Apache/2.2.9 (Unix)
Server built:   Sep 19 2008 10:58:54
$ httpd -M
Loaded Modules:
 core_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 authn_file_module (shared)
 authn_dbm_module (shared)
 authn_anon_module (shared)
 authn_dbd_module (shared)
 authn_default_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_dbm_module (shared)
 authz_owner_module (shared)
 authz_default_module (shared)
 auth_basic_module (shared)
 auth_digest_module (shared)
 cache_module (shared)
 disk_cache_module (shared)
 mem_cache_module (shared)
 dbd_module (shared)
 dumpio_module (shared)
 ext_filter_module (shared)
 include_module (shared)
 filter_module (shared)
 deflate_module (shared)
 log_config_module (shared)
 log_forensic_module (shared)
 logio_module (shared)
 env_module (shared)
 mime_magic_module (shared)
 cern_meta_module (shared)
 expires_module (shared)
 headers_module (shared)
 ident_module (shared)
 usertrack_module (shared)
 setenvif_module (shared)
 version_module (shared)
 proxy_module (shared)
 proxy_connect_module (shared)
 proxy_ftp_module (shared)
 proxy_http_module (shared)
 proxy_ajp_module (shared)
 proxy_balancer_module (shared)
 ssl_module (shared)
 mime_module (shared)
 dav_module (shared)
 status_module (shared)
 autoindex_module (shared)
 asis_module (shared)
 info_module (shared)
 cgi_module (shared)
 dav_fs_module (shared)
 vhost_alias_module (shared)
 negotiation_module (shared)
 dir_module (shared)
 imagemap_module (shared)
 actions_module (shared)
 speling_module (shared)
 userdir_module (shared)
 alias_module (shared)
 rewrite_module (shared)
 bonjour_module (shared)
Syntax OK

モジュール作成開始

$ apxs -g -n orz
Creating [DIR]  orz
Creating [FILE] orz/Makefile
Creating [FILE] orz/modules.mk
Creating [FILE] orz/mod_orz.c
Creating [FILE] orz/.deps
$ ls
orz/
$ cd orz
$ ls
Makefile    mod_orz.c   modules.mk

mod_orz.cはテンプレートのままです。

/* The sample content handler */
static int orz_handler(request_rec *r)
{
    if (strcmp(r->handler, "orz")) {
        return DECLINED;
    }
    r->content_type = "text/html";

    if (!r->header_only)
        ap_rputs("The sample page from mod_orz.c\n", r);
    return OK;
}

ビルドします。

$ sudo apxs -i -a -c mod_orz.c 
Password:
/usr/share/apr-1/build-1/libtool --silent --mode=compile gcc    -DDARWIN -DSIGPROCMASK_SETS_THREAD_MASK -no-cpp-precomp  -I/usr/include/apache2  -I/usr/include/apr-1   -I/usr/include/apr-1   -c -o mod_orz.lo mod_orz.c && touch mod_orz.slo
/usr/share/apr-1/build-1/libtool --silent --mode=link gcc -o mod_orz.la  -rpath /usr/libexec/apache2 -module -avoid-version    mod_orz.lo
/usr/share/httpd/build/instdso.sh SH_LIBTOOL='/usr/share/apr-1/build-1/libtool' mod_orz.la /usr/libexec/apache2
/usr/share/apr-1/build-1/libtool --mode=install cp mod_orz.la /usr/libexec/apache2/
cp .libs/mod_orz.so /usr/libexec/apache2/mod_orz.so
cp .libs/mod_orz.lai /usr/libexec/apache2/mod_orz.la
cp .libs/mod_orz.a /usr/libexec/apache2/mod_orz.a
ranlib /usr/libexec/apache2/mod_orz.a
chmod 644 /usr/libexec/apache2/mod_orz.a
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/libexec/apache2

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
     during execution

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/libexec/apache2/mod_orz.so
[activating module `orz' in /private/etc/apache2/httpd.conf]
$ nm /usr/libexec/apache2/mod_orz.so 
00000ee8 t __dyld_func_lookup
00000000 t __mh_bundle_header
         U _ap_hook_handler
         U _ap_rputs
00000ef6 t _orz_handler
00001020 D _orz_module
00000f8b t _orz_register_hooks
00001000 d dyld__mach_header
00000ed4 t dyld_stub_binding_helper

なんかエラーがでます。

$ httpd -M
httpd: Syntax error on line 116 of /private/etc/apache2/httpd.conf: Cannot load /usr/libexec/apache2/mod_orz.so into server: dlopen(/usr/libexec/apache2/mod_orz.so, 10): no suitable image found.  Did find:\n\t/usr/libexec/apache2/mod_orz.so: mach-o, but wrong architecture

Installing RedMine for Mac OS X(Leopard) - use No::Name;によると以下のようにすればいいみたいです。

$ cd /usr/sbin
$ sudo cp -p httpd httpd.org
$ sudo lipo httpd -thin i386 -output httpd

うまくいったようです。

$ httpd -M
Loaded Modules:
 core_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 authn_file_module (shared)
 authn_dbm_module (shared)
 authn_anon_module (shared)
 authn_dbd_module (shared)
 authn_default_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_dbm_module (shared)
 authz_owner_module (shared)
 authz_default_module (shared)
 auth_basic_module (shared)
 auth_digest_module (shared)
 cache_module (shared)
 disk_cache_module (shared)
 mem_cache_module (shared)
 dbd_module (shared)
 dumpio_module (shared)
 ext_filter_module (shared)
 include_module (shared)
 filter_module (shared)
 deflate_module (shared)
 log_config_module (shared)
 log_forensic_module (shared)
 logio_module (shared)
 env_module (shared)
 mime_magic_module (shared)
 cern_meta_module (shared)
 expires_module (shared)
 headers_module (shared)
 ident_module (shared)
 usertrack_module (shared)
 setenvif_module (shared)
 version_module (shared)
 proxy_module (shared)
 proxy_connect_module (shared)
 proxy_ftp_module (shared)
 proxy_http_module (shared)
 proxy_ajp_module (shared)
 proxy_balancer_module (shared)
 ssl_module (shared)
 mime_module (shared)
 dav_module (shared)
 status_module (shared)
 autoindex_module (shared)
 asis_module (shared)
 info_module (shared)
 cgi_module (shared)
 dav_fs_module (shared)
 vhost_alias_module (shared)
 negotiation_module (shared)
 dir_module (shared)
 imagemap_module (shared)
 actions_module (shared)
 speling_module (shared)
 userdir_module (shared)
 alias_module (shared)
 rewrite_module (shared)
 bonjour_module (shared)
 orz_module (shared)
Syntax OK

/etc/apache2/httpd.confにLocationを追加しapacheを起動して確認します。

$ sudo vi /etc/apache2/httpd.conf
$ sudo apachectl restart
$ lynx -mime_header http://localhost/orz
HTTP/1.1 200 OK
Date: Wed, 20 May 2009 11:55:09 GMT
Server: Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7l DAV/2
Content-Length: 31
Connection: close
Content-Type: text/html

The sample page from mod_orz.c

Apacheをもっと知りたいと思った今日この頃です。でもこういう情報ってあんま無い気がするなあ。

追記

DSAS開発者の部屋:apache module 開発事始めも参考になりますね。