系统启动后,仅为BASh启用LD_PRELOAD

时间:2022-08-25 18:52:38

Is there a way to inject/enable LD_PRELOAD just for new sessions (ie: BASh)?

有没有办法为新会话注入/启用LD_PRELOAD(即:BASh)?

I have a syntax highlighting library that I want to have automatically enabled (ie: highlight warnings for certain users), and just need it loaded for BASh rather than all processes. If I put it in /etc/ld.so.preload, it's disruptive and causes issues for all the system services and other programs that don't need it running, wrapping system calls (printf and exec mainly).

我有一个语法突出显示库,我想自动启用(即:突出显示某些用户的警告),只需要为BASh而不是所有进程加载它。如果我将它放在/etc/ld.so.preload中,它会造成中断并导致所有系统服务和其他不需要运行的程序出现问题,包装系统调用(主要是printf和exec)。

Is there a simple way to accomplish this?

有没有一种简单的方法来实现这一目标?

1 个解决方案

#1


2  

The easiest solution is probably to replace bash with a shell script that performs the LD_PRELOAD logic, then calls the actual (renamed) bash binary.

最简单的解决方案可能是用执行LD_PRELOAD逻辑的shell脚本替换bash,然后调用实际(重命名的)bash二进制文件。

That is, move /bin/bash to /bin/bash.original, then create a script /bin/bash with the following contents:

也就是说,将/ bin / bash移动到/bin/bash.original,然后创建一个脚本/ bin / bash,其中包含以下内容:

#!/bin/sh

LD_PRELOAD=/path/to/my/library.so
export LD_PRELOAD
exec /bin/bash.original "$@"

You could include logic here (e.g., "is stdout a tty") if you want to only perform the LD_PRELOAD when connected to an interactive session. Trying to perform any sort of terminal manipulation when bash isn't connected to a tty will probably yield weird results.

如果您只想在连接到交互式会话时执行LD_PRELOAD,则可以在此处包含逻辑(例如,“stdout tty”)。当bash没有连接到tty时,尝试执行任何类型的终端操作可能会产生奇怪的结果。

#1


2  

The easiest solution is probably to replace bash with a shell script that performs the LD_PRELOAD logic, then calls the actual (renamed) bash binary.

最简单的解决方案可能是用执行LD_PRELOAD逻辑的shell脚本替换bash,然后调用实际(重命名的)bash二进制文件。

That is, move /bin/bash to /bin/bash.original, then create a script /bin/bash with the following contents:

也就是说,将/ bin / bash移动到/bin/bash.original,然后创建一个脚本/ bin / bash,其中包含以下内容:

#!/bin/sh

LD_PRELOAD=/path/to/my/library.so
export LD_PRELOAD
exec /bin/bash.original "$@"

You could include logic here (e.g., "is stdout a tty") if you want to only perform the LD_PRELOAD when connected to an interactive session. Trying to perform any sort of terminal manipulation when bash isn't connected to a tty will probably yield weird results.

如果您只想在连接到交互式会话时执行LD_PRELOAD,则可以在此处包含逻辑(例如,“stdout tty”)。当bash没有连接到tty时,尝试执行任何类型的终端操作可能会产生奇怪的结果。