I am using LD_PRELOAD, and I am seeing a difference between bash and dash when using the system() command.

Let's consider this simple C program:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char *argv[]) { 
    system("echo $LD_PRELOAD > /tmp/blah.txt");
    return 0;
}

I would start my test program like this:

LD_PRELOAD=preload.so ./test

Both in bash and dash, I got:

~$ cat /tmp/blah.txt 
 preload.so

So far so good except that the LD_PRELOAD is not applied to the command given to system when dash is the shell. I mean

  • if /bin/sh points to /bin/bash, LD_PRELOAD is applied to the command given to system()
  • if /bin/sh points to /bin/dash, LD_PRELOAD is NOT applied to the command given to system().

My preload.so library override open(). It is executed with the test program when bash is used (/bin/sh=/bin/bash), but not when dash is used (/bin/sh=/bin/dash).

I am guessing that bash and dash might treat the environment variables passed to execve differently, but I can not find a way with dash to have LD_PRELOAD applied to the command given to system... Unfortunately I have to use dash, and using bash is not an option.

share|improve this question
    
using your C program is just confusing things. You should be able to do this on bash or dash command line. system() simply invokes the shell, the effects being the same as command line. Post how you try your command line on dash, what you expect, and what you get. – Mark Galeck Aug 15 '16 at 20:44
    
This program is just a minimal example. I have to use LD_PRELOAD to transform pathname (existent code which I could not change, at least for now). The issue is that LD_PRELOAD do not propagate to the program called by execve (that's what system is calling) when DASH is used. No issue at all with BASH. By 'propagate' I mean that the preload library is NOT applied to the program called by execve (again, just when DASH is used). Everything is already explained clearly in my original email. – Olivier Aug 16 '16 at 22:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.