LoginSignup
0
0

More than 3 years have passed since last update.

mac版/usr/libexec/path_helperがシステムを異常動作に導く

Last updated at Posted at 2020-08-28

macOSX 10.9限定

経緯

コメント欄の下まで熟読のこと。

リンク(A)
[Q&A] apple純正のsh,zsh,bash用のダメダメなパスはどのファイルに記載ありますか? - Qiita
https://qiita.com/shiro_yone/questions/3e4fcb26e18e44762446

問題 mac版/usr/libexec/path_helperがシステム設定を異常動作に導く

$ /usr/libexec/path_helper -s
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/stow/ruby/bin:/usr/local/stow/gettext-021/bin:/Users/xxxxxxx/bin"; export PATH;
MANPATH="/usr/share/man:/usr/local/share/man"; export MANPATH;
~ $ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/stow/ruby/bin:/usr/local/stow/gettext-021/bin:/Users/xxxxxxx/bin
~ $ vim

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/stow/ruby/bin:/usr/local/stow/gettext-021/bin:/Users/xxxxxxx/bin

Press ENTER or type command to continue
~ $ zsh --version
zsh 5.8 (x86_64-apple-darwin13.4.0)

上記の質問で書いた、vimとzshのパスの不一致は無くなった。
path_helper周辺が怪しいのは明確になった。

筆者の勉強不足で /usr/libexec/path_helper -s の出力先ファイルが存在するか分かってない。path設定は .zshenv で

export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:~/bin
export PATH=/usr/local/stow/gettext-021/bin:$PATH
export PATH=/usr/local/stow/ruby/bin:$PATH

暫定的解決方法

MacOSXが10.9なので/usr/libexec/path_helper chmod 644とし、実行権限を外した。
この方法は推奨されない。
以下の通り、期待する結果となった。

~ $ echo $PATH
/usr/local/stow/ruby/bin:/usr/local/stow/gettext-021/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/xxxxxxx/bin
~ $ vim

/usr/local/stow/ruby/bin:/usr/local/stow/gettext-021/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/xxxxxxx/bin

Press ENTER or type command to continue

推測される問題の箇所

候補1
/usr/libexec/path_helper周辺。
なお、筆者は /etc/paths のファイルを書き換えている。

/etc $ more paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/etc $

候補2
vim は /usr/libexec/path_helper (-s or -c) を実行しているだけかもしれない。
linux関係では問題が起きてないであろうから、Mac版だけで問題発生と思われる。

新たな問題点

Linuxでは使えるらしい unsetopt GLOBAL_RCS と /usr/libexec/path_helper を
.zshenv で読み込んでも上記の問題が発生する。
根本的かつ、根の深い重大問題である。

# ignore /etc/zprofile, /etc/zshrc, /etc/zlogin, and /etc/zlogout
# ref. http://zsh.sourceforge.net/Doc/Release/Files.html
# ref. http://zsh.sourceforge.net/Doc/Release/Options.html#index-GLOBALRCS
unsetopt GLOBAL_RCS
# copied from /etc/zprofile
# system-wide environment settings for zsh(1)
if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi
$ cd /etc/paths.d
paths.d $ la
total 0
drwxr-xr-x    2 root  wheel    68B  8 25  2013 .
drwxr-xr-x  101 root  wheel   3.4K  8 18 09:31 ..
paths.d $

いじった覚えがないので怖い。

新たな問題点2

unsetopt GLOBAL_RCS を .zshenv の最初に行い
eval /usr/libexec/path_helper -s
はコメントアウトするとリンク(A)に記載したzsh (期待した正常値)とvim (期待しない異常値)でパスの不一致が再現する。

所感

zshは便利だがmacの場合、sshやtmux screen vim cron等々で複合した環境で耐えられなさそうだ。
こんなつまんないAppleのバグで皆、Linux環境をインストしているかもしれない。

path_helperについて。Mac OSX 版 - それマグで!
https://takuya-1st.hatenablog.jp/entry/2013/12/14/040814
ここでは path_helper は擁護されているが私は疑問に思う。

/etc/profile 又は /etc/zshenvを読んだ直後のPATH値はMacであれば
「/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin」
が望ましいと推測。

2020-08-29追跡調査

/etc/zshenv内部でパスをクリアしてみた。

/etc $ /usr/libexec/path_helper
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/stow/ruby/bin:/usr/local/stow/gettext-021/bin:/Users/xxxxxxx/bin"; export PATH;
MANPATH="/usr/share/man:/usr/local/share/man"; export MANPATH;
/etc $
/etc $ cat zshenv
# system-wide environment settings for zsh(1)
if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
    PATH="" (<ーこれを追記した)
fi


~ $ echo $PATH
/usr/local/stow/ruby/bin:/usr/local/stow/gettext-021/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/xxxxxxxx/bin
~ $ vim



Press ENTER or type command to continue

vimからの:!echo $PATHはクリアされた。

vimはmac用内部コードで、zshシェルに対応する /etc/zshenv を読み込むと思われる。(調べてないけどbashは profile?)
Mac版 path_helper が生成する問題パスが明らかになった。
よって、少なくともzsh+vim環境では2つの対策のうちどちらかを行う必要がある。

(1) /etc/zshenv 内部を書き換える
(2) path_helper の実行権限を外す

2020-08-29、今日の時点では(2)のほうがいいと思われる。
bash環境は path_helper が問題パスをどこで正常値にリカバリーしているのか不明。
誰か教えて下さい。

追記

macにおいて、sshやtmux screen vim cron等で読み込むPATHが理想と仕様がどうあるべきか?
(1)/etc側だけで用意するPATH
(2)ユーザーで用意するPATH (ユーザー追加分と /usr/local/bin )

(A) (1)だけ
(B) (1)次に(2)。(1)を優先。
元々のAppleが用意した/usr/local/binが優先されない /etc/paths はこれ。
(C) (1)をクリアして(2)だけ。(2)を優先。
zshだけでsshやtmux screen vim cron等を使わないならこれで問題ない。
(D) (1)と(2)を読むが(2)を優先。

優先とはPATH変数の先頭側に記述する、ということ。
vimはユーザーが自由に使う為、(D) が理想と思われる。

参考リンク

Mac OSX El Capitan での ~/.zshenv の扱いについて - Qiita
https://qiita.com/t-takaai/items/8574ff312f2caa5177c2

zsh で path にディレクトリを追加するときは (N-/) を付けよう - Qiita
https://qiita.com/mollifier/items/42ae46ff4140251290a7

.zshenv で PATH を管理したら罠にハマった - 大学生からの Web 開発
https://karur4n.hatenablog.com/entry/2016/01/18/100000

テキスト処理のための標準的なコマンド群の macOS への導入手順 - Qiita
https://qiita.com/eumesy/items/3bb39fc783c8d4863c5f#1-%E3%82%B7%E3%82%A7%E3%83%AB%E3%81%AE%E8%A8%AD%E5%AE%9A%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E8%AA%BF%E6%95%B4

osx zsh+crontab+rbenvでの/usr/libexec/path_helperパス変更問題 - Qiita
https://qiita.com/github0013@github/items/c910ebb47d9be0a45282

PATH設定がどこにあるか分からないときに見るべきファイル - Qiita
https://qiita.com/gm_kou/items/24dec9f0e51b9343651b

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0