0

Part of a program named =program.c

int memoryPercent()
{
    int memoryPercent=0;
    FILE *pipe1 = NULL;

    pipe1=popen("/home/jatin/scripts/memoryPercent.sh","r");

    if(!pipe1)
    {
        snmp_log(LOG_ERR,"popen failed in temp(),errno:%d\n",errno);
        return -1;
    }

    if( fscanf(pipe1,"%d",&memoryPercent) != 1) 
    {
        snmp_log(LOG_ERR,"fscanf failed in temp fun(),errno:\n");
        return -2;
    }

    pclose(pipe1);
    return memoryPercent;
}

above is my calling code /home/jatin/scripts/memoryPercent.sh on this location a scripts is given and it is returning a INTEGER value.

in UBUNTU 12.04 when I compile and execute this code it is running perfectly.

but in my VM-WARE

I have copied all the necessary dependencies at the location /var/snmp3 and executing this binary with CHROOT the syntax is

/usr/sbin/chroot /var/snmp3/ /usr/local/bin/program

it is showing value "-1" only !! Can anyone tell me what I am missing ?

the script is at the same location in /var/snmp3/home/jatin/scripts/memoryPercent.sh as well as /home/jatin/scripts/memoryPercent.sh in 775 mode....

arrowd
  • 33,231
  • 8
  • 79
  • 110
jatin bodarya
  • 72
  • 3
  • 8
  • What is the error message logged? – cdarke Oct 03 '12 at 11:52
  • Did you tried chdir following with chroot in C? – Zaffy Oct 03 '12 at 11:59
  • Have you checked the log? What's the value of `errno`? You can use e.g. [`strerror`](http://linux.die.net/man/3/strerror) to get a descriptive text showing the error message. – Some programmer dude Oct 03 '12 at 12:09
  • Another point, I guess the script uses `#!/bin/sh`, is there a `/var/snmp3/bin/sh` program? – Some programmer dude Oct 03 '12 at 12:33
  • to know the path to command sh: which sh. Say it is /bin/sh, did you try: pipe1=popen("/bin/sh /home/jatin/scripts/memoryPercent.sh","r"); The idea is to force running a shell to execute your script. – Bruno von Paris Oct 03 '12 at 12:59
  • Joachim Pileborg & Bruno von Paris : thanks .. its working now...just want to know is it a safe way to execute the script ? I suppose to call 50-60 scripts every second in infinite for loop. please guide me/.. – jatin bodarya Oct 04 '12 at 05:01

1 Answers1

0

Try running the script manually from a chrooted shell.

I think you are missing the shell used by memoryPercent.sh. Have you copied the shell to the chrooted environment?

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82