ドルヲタ系インフラエンジニア じーふーの備忘録

クラウドをメインに扱うインフラエンジニアが書くメモやら雑感、たまにドルヲタ的活動記録残します。最近の推しはAzureのData Factory(V2)です。

【linux / postgresql】 自作パッチファイル適用方法(postgresql 9.3.5ソース修正)

自作パッチファイルの作成・適用方法をメモします。
今回はPostgreSQL 9.3.5のソースを修正し、エラーメッセージの文言を変更する方法を試します。

変更対象

今回は下記エラーメッセージを以下のように変えてみました。

$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data2/ -A md5
initdb: must specify a password for the superuser to enable md5 authentication

↓ パッチ適用後

$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data2/ -A md5
hogehoge fugafuga piyopiyo

パッチファイル作成

パッチファイルを作成する方法として、今回はdiffコマンドを利用します。

修正対象

/tmp/work/postgresql-9.3.5/src/bin/initdb/initdb.cの2812行目に記述されているエラーメッセージを変更します。

2803 static void
2804 check_need_password(const char *authmethodlocal, const char *authmethodhost)
2805 {
2806         if ((strcmp(authmethodlocal, "md5") == 0 ||
2807                  strcmp(authmethodlocal, "password") == 0) &&
2808                 (strcmp(authmethodhost, "md5") == 0 ||
2809                  strcmp(authmethodhost, "password") == 0) &&
2810                 !(pwprompt || pwfilename))
2811         {
2812                 fprintf(stderr, _("%s: must specify a password for the superuser to enable      %s authentication\n"), progname,
2813                                 (strcmp(authmethodlocal, "md5") == 0 ||
2814                                  strcmp(authmethodlocal, "password") == 0)
2815                                 ? authmethodlocal
2816                                 : authmethodhost);
2817                 exit(1);
2818         }
2819 }

修正の流れ

  • バックアップ作成
$ cd /tmp/work/postgresql-9.3.5
$ cp src/bin/initdb/initdb.c{,.bak}
$ ll src/bin/initdb/
total 760
lrwxrwxrwx 1 vagrant vagrant     40 May  1 10:32 encnames.c -> ../../../src/backend/utils/mb/encnames.c
-rw-rw-r-- 1 vagrant vagrant  23528 May  1 10:32 encnames.o
-rw-r--r-- 1 vagrant vagrant  34675 Jul 22  2014 findtimezone.c
-rw-rw-r-- 1 vagrant vagrant  26176 May  1 10:32 findtimezone.o
-rwxrwxr-x 1 vagrant vagrant 253846 May  1 10:32 initdb
-rw-r--r-- 1 vagrant vagrant  92377 Jul 22  2014 initdb.c
-rw-r--r-- 1 vagrant vagrant  92377 May  1 10:43 initdb.c.bak   <=# バックアップ作成
-rw-rw-r-- 1 vagrant vagrant 165944 May  1 10:32 initdb.o
lrwxrwxrwx 1 vagrant vagrant     33 May  1 10:32 localtime.c -> ../../../src/timezone/localtime.c
-rw-rw-r-- 1 vagrant vagrant  64824 May  1 10:32 localtime.o
-rw-r--r-- 1 vagrant vagrant   1721 Jul 22  2014 Makefile
-rw-r--r-- 1 vagrant vagrant    270 Jul 22  2014 nls.mk
drwxrwxr-x 2 vagrant vagrant   4096 Jul 22  2014 po
  • 修正
2812               fprintf(stderr, _("%s: must specify a password for the superuser to enable      %s authentication\n"), progname,

↓
2812                fprintf(stderr, _("hogehoge fugafuga piyopiyo\n"), progname,
  • diffコマンドを利用しパッチファイル作成
$ diff -u src/bin/initdb/initdb.c.bak src/bin/initdb/initdb.c > err_msg_u.patch
$ cat err_msg_u.patch
--- src/bin/initdb/initdb.c.bak 2015-05-01 10:43:15.921200076 +0900
+++ src/bin/initdb/initdb.c     2015-05-01 10:49:50.347199840 +0900
@@ -2809,7 +2809,7 @@
                 strcmp(authmethodhost, "password") == 0) &&
                !(pwprompt || pwfilename))
        {
-               fprintf(stderr, _("%s: must specify a password for the superuser to enable %s authentication\n"), progname,
+               fprintf(stderr, _("hogehoge fugafuga piyopiyo\n"), progname,
                                (strcmp(authmethodlocal, "md5") == 0 ||
                                 strcmp(authmethodlocal, "password") == 0)
                                ? authmethodlocal

パッチファイル適用

patchコマンドを実行してdiffコマンドで作成したパッチファイルを適用します。

パッチファイル適用の流れ

  • patchコマンドを実行しパッチ適用
$ patch -p0 < err_msg_u.patch
patching file src/bin/initdb/initdb.c
Reversed (or previously applied) patch detected!  Assume -R? [n] # enter
Apply anyway? [n] # enter
Skipping patch.
1 out of 1 hunk ignored -- saving rejects to file src/bin/initdb/initdb.c.rej
  • 確認
$ ll src/bin/initdb/
total 764
lrwxrwxrwx 1 vagrant vagrant     40 May  1 10:32 encnames.c -> ../../../src/backend/utils/mb/encnames.c
-rw-rw-r-- 1 vagrant vagrant  23528 May  1 10:32 encnames.o
-rw-r--r-- 1 vagrant vagrant  34675 Jul 22  2014 findtimezone.c
-rw-rw-r-- 1 vagrant vagrant  26176 May  1 10:32 findtimezone.o
-rwxrwxr-x 1 vagrant vagrant 253846 May  1 10:32 initdb
-rw-r--r-- 1 vagrant vagrant  92328 May  1 10:49 initdb.c
-rw-r--r-- 1 vagrant vagrant  92377 May  1 10:43 initdb.c.bak
-rw-r--r-- 1 vagrant vagrant    525 May  1 11:01 initdb.c.rej
-rw-rw-r-- 1 vagrant vagrant 165944 May  1 10:32 initdb.o
lrwxrwxrwx 1 vagrant vagrant     33 May  1 10:32 localtime.c -> ../../../src/timezone/localtime.c
-rw-rw-r-- 1 vagrant vagrant  64824 May  1 10:32 localtime.o
-rw-r--r-- 1 vagrant vagrant   1721 Jul 22  2014 Makefile
-rw-r--r-- 1 vagrant vagrant    270 Jul 22  2014 nls.mk
drwxrwxr-x 2 vagrant vagrant   4096 Jul 22  2014 po
$ view src/bin/initdb/initdb.c
   2803 static void
   2804 check_need_password(const char *authmethodlocal, const char *authmethodhost)
   2805 {
   2806         if ((strcmp(authmethodlocal, "md5") == 0 ||
   2807                  strcmp(authmethodlocal, "password") == 0) &&
   2808                 (strcmp(authmethodhost, "md5") == 0 ||
   2809                  strcmp(authmethodhost, "password") == 0) &&
   2810                 !(pwprompt || pwfilename))
   2811         {
   2812                 fprintf(stderr, _("hogehoge fugafuga piyopiyo\n"), progname,
   2813                                 (strcmp(authmethodlocal, "md5") == 0 ||
   2814                                  strcmp(authmethodlocal, "password") == 0)
   2815                                 ? authmethodlocal
   2816                                 : authmethodhost);
   2817                 exit(1);
   2818         }
   2819 }

PostgreSQLの再コンパイル、インストール

コンパイルからインストールまで

$ ./configure
$ gmake
$ su -
# cd /tmp/work/postgresql-9.3.5
# gmake install

~中略~

PostgreSQL installation complete.

結果確認

$ su - postgres
$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data2/ -A md5
hogehoge fugafuga piyopiyo

適用パッチを戻す場合

patchコマンド実行時に -Rオプションを付けることでリバースパッチ適用となります。

$ patch -R -p0 < err_msg_u.patch
patching file src/bin/initdb/initdb.c
   2812                 fprintf(stderr, _("%s: must specify a password for the superuser to enab        le %s authentication\n"), progname,