Skip to main content
added 369 characters in body
Source Link
David Anderson
  • 42k
  • 14
  • 67
  • 111
  • The setbuf.c source code on GitHub
  • The man page for dyld(1)
Expanded explanation.
Source Link
David Anderson
  • 42k
  • 14
  • 67
  • 111

The setbuf command sets one or more environmental variables, then attempts in an attempt to cause athe command given as a positional parameter to use a different library. The functions in this library then act upon one or more of these environmental variables to change the buffering. SIP prevents commands stored in protected directories from being able to use libraries other than the ones specified when the command was created.

What is happening here is that grep is using the functions in libstdbuf.so. These functions are using the variable _STDBUF_O to determine that no buffer is to be used.


To see which additional environmental variables are being passed to grep in the above example, I created the file name myenv.c containing the following source code.

#include <stdio.h>
int main(int argc, char *argv[], char *envp[]) {
    while (*envp) printf("%s\n",*envp++);
    return 0;
}

I used the gcc command shown below to create the executable command myenv. This myenv command prints out the environmental variables.

gcc myenv.c -o myenv

To see which additional environmental variables are being passed, I entered the following command. The output is also shown below.

% diff <(./myenv) <(stdbuf -o0 ./myenv)
0a1,2
> DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
> _STDBUF_O=0
24c26,27
< _=/Users/davidanderson/myenv/./myenv
---
> _=/usr/local/bin/stdbuf
> DYLD_FORCE_FLAT_NAMESPACE=y

This output shows DYLD_FORCE_FLAT_NAMESPACE as an additional environmental variable, which I did not include in the above example. I do not think this variable is needed for macOS Monterey and newer versions of macOS, since the variable is not defined in the output of the man dyld command. However, I did find the DYLD_FORCE_FLAT_NAMESPACE variable defined in the man dyld command output for macOS Catalina and macOS High Sierra. So older versions of macOS and OS X may need to have this variable defined.


References

  • The setbuf.c source code on GitHub
  • The man page for dyld(1)

The setbuf command sets one or more environmental variables, then attempts to cause a command to use a different library. The functions in this library then act upon these environmental variables to change the buffering. SIP prevents commands stored in protected directories from being able to use libraries other than the ones specified when the command was created.

What is happening here is that grep is using the functions in libstdbuf.so. These functions are using the variable _STDBUF_O to determine that no buffer is to be used.

The setbuf command sets one or more environmental variables in an attempt to cause the command given as a positional parameter to use a different library. The functions in this library then act upon one or more of these environmental variables to change the buffering. SIP prevents commands stored in protected directories from being able to use libraries other than the ones specified when the command was created.

What is happening here is that grep is using the functions in libstdbuf.so. These functions are using the variable _STDBUF_O to determine that no buffer is to be used.


To see which additional environmental variables are being passed to grep in the above example, I created the file name myenv.c containing the following source code.

#include <stdio.h>
int main(int argc, char *argv[], char *envp[]) {
    while (*envp) printf("%s\n",*envp++);
    return 0;
}

I used the gcc command shown below to create the executable command myenv. This myenv command prints out the environmental variables.

gcc myenv.c -o myenv

To see which additional environmental variables are being passed, I entered the following command. The output is also shown below.

% diff <(./myenv) <(stdbuf -o0 ./myenv)
0a1,2
> DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
> _STDBUF_O=0
24c26,27
< _=/Users/davidanderson/myenv/./myenv
---
> _=/usr/local/bin/stdbuf
> DYLD_FORCE_FLAT_NAMESPACE=y

This output shows DYLD_FORCE_FLAT_NAMESPACE as an additional environmental variable, which I did not include in the above example. I do not think this variable is needed for macOS Monterey and newer versions of macOS, since the variable is not defined in the output of the man dyld command. However, I did find the DYLD_FORCE_FLAT_NAMESPACE variable defined in the man dyld command output for macOS Catalina and macOS High Sierra. So older versions of macOS and OS X may need to have this variable defined.


References

  • The setbuf.c source code on GitHub
  • The man page for dyld(1)
added 89 characters in body
Source Link
David Anderson
  • 42k
  • 14
  • 67
  • 111
brew install coreutils
brew install coreutils
/usr/local/Cellar/coreutils/9.5/bin/gstdbuf
/usr/local/Cellar/coreutils/9.5/bin/gstdbuf
/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
{ echo 'foo' ; sleep 2 ; echo 'boo'} | stdbuf -o0 grep 'oo' | cat
{ echo 'foo' ; sleep 2 ; echo 'boo'} | stdbuf -o0 grep 'oo' | cat
% diff <(env) <(stdbuf -o0 env)              
0a1
> _STDBUF_O=0
24c25
< _=/usr/bin/env
---
> _=/usr/local/bin/stdbuf
% diff <(env) <(stdbuf -o0 env)              
0a1
> _STDBUF_O=0
24c25
< _=/usr/bin/env
---
> _=/usr/local/bin/stdbuf
rm -f /usr/local/bin/grep
cp /usr/bin/grep /usr/local/bin
rm -f /usr/local/bin/grep
cp /usr/bin/grep /usr/local/bin
{ echo 'foo' ; sleep 2 ; echo 'boo'} | DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so _STDBUF_O=0 grep 'oo' | cat

Note: The description for DYLD_INSERT_LIBRARIES can be found by entering man dyld.

{ echo 'foo' ; sleep 2 ; echo 'boo'} | DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so _STDBUF_O=0 grep 'oo' | cat
export DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
{ echo 'foo' ; sleep 2 ; echo 'boo'} | _STDBUF_O=0 grep 'oo' | cat
export DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
{ echo 'foo' ; sleep 2 ; echo 'boo'} | _STDBUF_O=0 grep 'oo' | cat
brew install coreutils
/usr/local/Cellar/coreutils/9.5/bin/gstdbuf
/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
{ echo 'foo' ; sleep 2 ; echo 'boo'} | stdbuf -o0 grep 'oo' | cat
% diff <(env) <(stdbuf -o0 env)              
0a1
> _STDBUF_O=0
24c25
< _=/usr/bin/env
---
> _=/usr/local/bin/stdbuf
rm -f /usr/local/bin/grep
cp /usr/bin/grep /usr/local/bin
{ echo 'foo' ; sleep 2 ; echo 'boo'} | DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so _STDBUF_O=0 grep 'oo' | cat
export DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
{ echo 'foo' ; sleep 2 ; echo 'boo'} | _STDBUF_O=0 grep 'oo' | cat
brew install coreutils
/usr/local/Cellar/coreutils/9.5/bin/gstdbuf
/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
{ echo 'foo' ; sleep 2 ; echo 'boo'} | stdbuf -o0 grep 'oo' | cat
% diff <(env) <(stdbuf -o0 env)              
0a1
> _STDBUF_O=0
24c25
< _=/usr/bin/env
---
> _=/usr/local/bin/stdbuf
rm -f /usr/local/bin/grep
cp /usr/bin/grep /usr/local/bin

Note: The description for DYLD_INSERT_LIBRARIES can be found by entering man dyld.

{ echo 'foo' ; sleep 2 ; echo 'boo'} | DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so _STDBUF_O=0 grep 'oo' | cat
export DYLD_INSERT_LIBRARIES=/usr/local/Cellar/coreutils/9.5/libexec/coreutils/libstdbuf.so
{ echo 'foo' ; sleep 2 ; echo 'boo'} | _STDBUF_O=0 grep 'oo' | cat
added 16 characters in body
Source Link
David Anderson
  • 42k
  • 14
  • 67
  • 111
Loading
deleted 31 characters in body
Source Link
David Anderson
  • 42k
  • 14
  • 67
  • 111
Loading
Source Link
David Anderson
  • 42k
  • 14
  • 67
  • 111
Loading