DevNull DevNull - 1 month ago 8
Linux Question

Enable LD_PRELOAD just for BASh after system startup

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

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).

Is there a simple way to accomplish this?

Answer

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.

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

#!/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.

Comments