Thread: Need help, please - very strange behavior

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    (Solved) Need help, please - very strange behavior

    I'm stumped. I have a program that if run from clicking on the icon of the program, crashes, dies, ends, etc., at a certain point. If I run the same program from a terminal, it works perfectly.

    The program creates a file (call it filea.sh) with commands, using 'expect' and scp to contact a remote computer and copy one file to a local directory. The created file (filea.sh) is then executed using the 'system(./filea.sh)' function.

    If run from the icon, the file gets created, but it looks like it never runs or gets executed. If I run the same program from a terminal, it works perfectly. The file gets created and runs, copying the file from the remote server.

    If I run the file that was created by running the program from the icon, filea.sh, in a terminal, it runs perfectly.

    I don't believe it's anything to do with 'expect' or 'scp' as I'm using the exact same functions in another part of the program that works if run either way. I also can't believe that it's the code since it works perfectly if run from a terminal.

    One more thing to ponder. If I edit the icon and tell it run in a terminal, it works. It only fails at that point when no terminal is involved.

    Anyone have ANY ideas before I try to run it through GDB?

    Thanks
    Last edited by snork; 09-25-2011 at 07:15 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Presumably you're doing a system() call (or maybe exec<something>) to execute the created file in a spawned shell.

    Make sure you have paths correct. Ensure the directory where you think the file is being created is the same place where the shell looks for the file. Make sure your current working directory aligns with whatever assumptions your program is making about what the working directory is.

    Beyond that, it could be anything.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make sure your "run in" directory is correct. If this is a Windows shortcut, you probably don't have that right, so it can't find its files (since I doubt they are in the PATH).


    Quzah
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    First of all, thank you for trying to help. I've tried running this on 3 different computer running 3 different Linux distros with the same results. As I stated in my post, I am doing a system() call to the file that gets created.

    The paths are all stated as 'system(./filea.sh)'. I've tried this with the program in different places, including a usb flash drive, using desktop icons, and clicking directly on the program icon. All scenarios produce the same results. I've tried running this in a terminal from different directories and as long as a terminal is involved in starting the program, it runs without problems.

    I've created desktop icons, with full path names, including working directory. Same problem. If I run it through GDB, it works right, since I'm using a terminal to run GDB.

    I'm wondering why, a terminal makes a difference. The other part of the program that uses the exact same functions to create that shell file and run it, works no matter where I start the program from.

    Here's another clue. The part of the program that downloads the one file from the remote computer, immediately tries to open that downloaded file. It bombs, since that file is not actually downloaded yet. In fact, it looks like that shell file that downloads the one file doesn't even run. However, if I use the other part of the program that does work, and it downloads the file correctly, then run the part that doesn't work properly, it works, since the file is now in it's directory to work on.

    Here is the code that is called from both locations. One works, the other section of the program that calls it, doesn't. The 'get_kod.sh' file never runs if called the program is started from either clicking on the program to run or started from an icon. BUT, it works if run from a terminal. In the non-working part of the program, it never reaches the openssl part of the program and never downloads anything from the remote host.

    Code:
      char text[250];
      strcpy(text,"#!/usr/bin/expect\n");
      strcat(text,"set timeout 30\n");
      strcat(text,"set user [lindex $argv 0]\n");
      strcat(text,"spawn scp ");
      strcat(text,user);
      strcat(text,"@");
      strcat(text,ip);
      strcat(text,":/home/");
      strcat(text,user);
      strcat(text,"/");
      strcat(text,kod_to_get);
      strcat(text," ./\n");
      strcat(text,"expect \"");
      strcat(text,user);
      strcat(text,"@");
      strcat(text,ip);
      strcat(text,"'s password:\"\n");
      strcat(text,"send \"");
      strcat(text,pw);
      strcat(text,"\\r");
      strcat(text,"\"\n");
      strcat(text,"interact\n");
      
    //  g_print("scp text: %s \n",text); 
        
        FILE *fptr;
    
      // Open file for writing
      if ((fptr = fopen ("get_kod.sh","w"))==NULL)
        {
        printf ("Cannot open file\n");
        exit (1);
        }
        fputs(text,fptr);
    
      if(fclose(fptr))
          printf("File close error\n");
    
      system("chmod +x get_kod.sh");
     
      int ret;
      ret = WEXITSTATUS(system("./get_kod.sh"));
      
    //  gtk_label_set_label((GtkLabel *)label_status, ret);
     
      // Setup command to decrypt binkod file
      strcpy(text,"openssl enc -aes-256-cbc -d -pass pass:");
      strcat(text,ukey);
      strcat(text," -in ");
      strcat(text,kod_to_get);
      strcat(text," -out kod.txt");
    
      ret = WEXITSTATUS(system(text));

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Is the terminal setting a home directory when it starts?

    Check the return value of your system() call ... is it failing?
    Last edited by CommonTater; 09-25-2011 at 12:49 PM.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    The program dies before it can print the return value, when run from clicking on it. When run with terminal, return code is success $1=0.

    I commented out everything past where it tries to run the created file (system(./get_kod.sh)). Program still dies when program started by clicking on it. Running it from terminal, works ??????

    update:

    I added some code so if the return from the system(./get_kod.sh) call fails, it does a system("touch fail") call. This will create an empty file called fail if the previous system call to system(./get_kod.sh) fails.

    The file does gets created, indicating that the system(./get_kod.sh) fails. Why would it fail if run by clicking on the program and not fail if run from a terminal? I added full path names to make sure there was no path mix up.
    Last edited by snork; 09-25-2011 at 02:50 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Is it perhaps putting it in a different directory?

    Does the program complete if you comment out the offending system() call...

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    It's not anywhere that I can find it.

    As I mentioned in a previous post, I put some code to see if that system call is returning a success or failure. It's failing to run that command; system("./get_kod.sh") if the program is started by clicking on an icon, but shows success if run from terminal. Why"

    If I edit the icon and tell it to run in a terminal, it works right. What's the difference? Does the program need stdout, stdin or stderr for some reason? Does it need that slight delay that a terminal might offer? This is driving me nuts.

    I know it's going to turn out to be something really stupid or simple, but after spending many, many hours, I'm willing to be made a fool of.

    I'm not sure what to try next.

    The program depends on that file being downloaded, so no, it errors if I comment that system call out.

    update:

    I commented out everything in the program past

    [code]
    ret = WEXITSTATUS(system("./get_kod.sh"));
    if (!ret)
    {
    system("touch fail");
    }

    /* Everything past here is commented out.
    {/code]

    The program doesn't die, it just sits there after writing that get_kod.sh file and creating the 'fail' file to disk. The file never gets executed, if I run the program from an icon, but works perfectly if run from a terminal. Again, I changed the system call, using a fully qualified filename, with the same results.
    Last edited by snork; 09-25-2011 at 03:29 PM.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    I ran my program from the icon in order to have it create the 'getkod' file. Here's what in the getkod file;

    Code:
    #!/usr/bin/expect
    set timeout 30
    set user [lindex $argv 0]
    spawn scp [email protected]:/home/abc/0925.key ./
    expect "[email protected]'s password:"
    send "windtalkabc\r"
    interact
    I then wrote a very simple program to execute that file..below;

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, char *argv[])
    {
      int ret;
      ret = WEXITSTATUS(system("./getkod"));
      if (!ret)
      {
        system("touch fail");
      }
      return 0;
    }
    If I run this little program from an icon, even with all the directories set correctly, the file getkod never get's executed. If I run it from a terminal, it gets executed.

    What am I missing here ????

    The program and file are in the same directory.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is your terminal by any chance root, and your normal user not?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    No. But I have tried using root, with same results. The 'getkod' file gets created with owner: me, group: root. It set as executable for all.
    I've changed the name of the program to run inside the system call and it works. e.g. system("firefox").

    What is so different from running my little program from an icon as opposed to a terminal? It's accessing that directory because it creates that 'fail' file. the getkod file is getting created in the same directory as everything else.

    Is there some way that I can even tell if the system is 'trying' to execute the 'getkod' file? It runs too fast to check the PID. The return code says that it didn't execute (I may misunderstand exactly what the return code is telling me). e.g. it didn't run the program or it didn't run it completely, etc.

    I've narrowed this down to the very basics and have eliminated almost everything but the actual system call. As I mention, that works because it can run other programs.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, put something in there that waits for you to hit enter where ever you want it to pause.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    quzah,

    Brilliant!

    Are you ready for this? I put a 'sleep 20' statement as the last line in the file 'getkod'. It works!

    I'm going to try other values.

    Why? What do you think is happening? What does that have to do with running in a terminal or not? I'm confused...

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You're considering a return value of 0 from system() to be a failure (i.e., if(!ret) touch fail); but this goes against Unix semantics. A return value of 0 typically means success.

    Of course this can't explain why things work one way and don't the other (at best it just flips them), but I think it's still worth pointing out.

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    Thanks, I knew that. I guess I've been too rattled lately, trying to figure out this. I don't know what I was thinking. The test should have been without the exclamation point (not).

    It still doesn't make sense to me what is happening here. I also thought it might be that there was no end of line or linefeed on the last line, but that wasn't the case. 'Sleep 1' works as the last line in the getkod file, but for the life of me, I can't figure out what that has to do with not running in a terminal. 'Sleep 0' does not work, so it doesn't have anything to do with just having another statement in that file. It actually has to sleep for some time period. Why would running it in a terminal allow that to happen automatically?

    Thanks Quzah for the direction to go in to solve this nasty problem. And, thanks for everyone else that has tried to help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange Struct behavior
    By masilva in forum C Programming
    Replies: 5
    Last Post: 05-05-2011, 01:37 PM
  2. strange behavior of pthread_create
    By np2k in forum Linux Programming
    Replies: 13
    Last Post: 05-12-2010, 02:48 AM
  3. Strange behavior
    By onako in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2010, 07:00 AM
  4. strange std::map behavior
    By manav in forum C++ Programming
    Replies: 63
    Last Post: 04-11-2008, 08:00 AM
  5. strange behavior
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 12:03 PM