OpenWrt Forum Archive

Topic: Strange behavior when Crosscompiling ?

The content of this topic has been archived on 20 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello everyone!

I made a small code to observe if my ftp port is up.
At first i compiled and runned under ubuntu and acted as ecxpected.

But when i compiled it cross it behaves not as ecxpected?
Why?

Here is my code:

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>

#define MAXBUF    1024
#define PORT    21
#define HOST    "127.0.0.1"

main() {
    pid_t   pid, sid;

    pid = fork();

    if (pid < 0) {
        exit(EXIT_FAILURE);
    } else if (pid > 0) {
        exit(EXIT_SUCCESS);
    }

    umask(0);

    sid = setsid();
    if (sid < 0) {
        exit(EXIT_FAILURE);
    }

    if ((chdir("/")) < 0) {
        exit(EXIT_FAILURE);
    }
    
    loop();                               // Firing up the loop

    exit(EXIT_SUCCESS);
}

loop() {
    while (1) {                            // The loop
    sleep(5);
    hostcheck();
    sleep(5);
    }
}

hostcheck()                            // Anybody home?
{    int sockfd, bytes_read;
    struct sockaddr_in dest;
    char buffer[MAXBUF];

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
    {
        system("logger Error opening Socket !!\n");
    }

    bzero(&dest, sizeof(dest));
    dest.sin_family = AF_INET;
    if ( inet_addr(HOST, &dest.sin_addr.s_addr) == 0 )
    {
        system("logger Error getting Host !!\n");
    }
    dest.sin_port = htons(PORT);

    if ( connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) != 0 )
    {
      system("logger Server is offline !!\n");
                somefunction
    }
    else {
      system("logger Server is online !!\n");
                somefunction
    }
    close(sockfd);
    return 0;
}

I know it is a little bit lame but in Ubuntu it does the Job!



The c# Makefile: ( its from Helloworld)

# build helloworld executable when user executes "make"

ftpwatch: ftpwatch.o
    $(CC) $(LDFLAGS) ftpwatch.o -o ftpwatch

ftpwatch.o: ftpwatch.c
    $(CC) $(CFLAGS) -c ftpwatch.c


# remove object files and executable when user executes "make clean"
clean:
    rm *.o ftpwatch

In ubuntu it sucessfulle checks if the router port 21 ist up.
Several time is stoped and restartet ftp and allways there was the right output an action of corse!
For mor testing the action has taken out for easier reading :]]

In openwrt it allways say server online!

Any Ideas on this?

Thanks!

(Last edited by derdigge on 3 Nov 2012, 05:40)

You can try to see what ltrace or strace returns for each call and compare with the expected values from ubuntu.

Also, why not using a shell script? It works very well. You can even use cron to schedule your check.

$ netstat -atn  | grep LISTEN | grep -q 0.0.0.0:21 && (echo "Port is up"; somefunction) || (echo "Port is down"; anotherfunction)

For OpenWRT, a lua script also might work nicely.

Thanks for your reply!

I had choosen C because i want to learn more about it.
Currenly i am a great shellwarrior allready but dont know much about C!

Lua is also a great Blackbox for me.

Maybe someone has an idea why the crosscompiler behaves that way.....

Still looking for an solution......

You may want to post your complete source codes for readers who are willing to test to find out what causes the problem.

(Last edited by mazilo on 5 Nov 2012, 05:29)

The discussion might have continued from here.