termux / termux-app

Termux - a terminal emulator application for Android OS extendible by variety of packages.
https://f-droid.org/en/packages/com.termux
Other
36.35k stars 3.82k forks source link

Segmentation Fault in C program using select / poll / socket #1813

Closed mattmomo62 closed 4 years ago

mattmomo62 commented 4 years ago

Hello Termux team,

I have an error launching an installed C program using socket in Termux (on Android TV (freebox player pop device))

Here is the code :

include <arpa/inet.h>

include <netinet/in.h>

include <netinet/tcp.h>

include <sys/socket.h>

include <sys/select.h>

include

... static SOCKET g_cancel_fds[2] = {INVALID_SOCKET, INVALID_SOCKET}; ... fd_set fd; int ret; int max_fds = s_sock > g_cancel_fds[0] ? s_sock : g_cancel_fds[0]; SOCKET c_sock = INVALID_SOCKET;

FD_ZERO(&fd); FD_SET(s_sock, &fd); FD_SET(g_cancel_fds[0], &fd);

// use select for the timeout feature, ignore fd // s_sock+1 allows us to check fd "s_sock" but ignore the rest if ((ret = select(max_fds+1, &fd, NULL, NULL, NULL)) == SOCKET_ERROR) <-- Error Segmentation Fault at this line { ...

Same error if I use poll function instead of select.

This code works on a linux server. Does socket run correctly on Android or is it a Termux issue ?

Grimler91 commented 4 years ago

What is your output of termux-info?

mattmomo62 commented 4 years ago

Packages CPU architecture: arm Subscribed repositories:

sources.list

deb https://dl.bintray.com/termux/termux-packages-24/ stable main

game-repo (sources.list.d/game.list)

deb https://dl.bintray.com/grimler/game-packages-24 games stable

science-repo (sources.list.d/science.list)

deb https://dl.bintray.com/grimler/science-packages-24 science stable

root-repo (sources.list.d/root.list)

deb https://dl.bintray.com/grimler/termux-root-packages-24 root stable

x11-repo (sources.list.d/x11.list)

deb https://dl.bintray.com/xeffyr/x11-packages x11 main Updatable packages: apt/stable 2.1.11-2 arm [upgradable from: 1.4.10-6] ca-certificates/stable 20201027 all [upgradable from: 20201016] less/stable 563 arm [upgradable from: 551-1] libass/stable 0.15.0 arm [upgradable from: 0.14.0-4] libcroco/stable 0.6.13-5 arm [upgradable from: 0.6.13-4] Android version: 9 Kernel build information: Linux localhost 4.9.113 #1 SMP PREEMPT Thu Jun 18 18:34:55 CEST 2020 armv8l Android Device manufacturer: Freebox Device model: Freebox Player POP

Grimler91 commented 4 years ago

Could you provide a compilable example that segfaults for you?

ghost commented 4 years ago

Here is the code :

Submit FULL code next time.

This code works on a linux server.

Android is different from typical GNU/Linux at least by having a different libc (Bionic).

Does socket run correctly on Android or is it a Termux issue ?

Very unlikely. We have lots of networking software which works perfectly. But even if there is "bug", it exists somewhere in libc and not subject for fixing in Termux as we use libraries provided by Android OS.


Following code using select(2) works without issues:

#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

int main() {
    char buffer[128];
    int result, nread;

    fd_set inputs, fds;
    FD_ZERO(&inputs);
    FD_SET(0, &inputs);

    while(1) {
        fds = inputs;
        result = select(FD_SETSIZE, &fds, NULL, NULL, NULL);

        switch(result) {
            case 0:
                printf("timeout");
                break;
            case -1:
                perror("select");
                exit(1);
            default:
            if (FD_ISSET(0, &fds)) {
                ioctl(0, FIONREAD, &nread);

                if (nread == 0) {
                    printf("finish");
                    exit(0);
                }

                nread = read(0, buffer, nread);
                buffer[nread] = 0;
                printf("read %d from keyboard: %s", nread, buffer);
            }
            break;
        }
    }
}

So select(2) syscall works on Android, which is expected... Double check your code. Segmentation failure may be on the next lines, not on the one which uses select.

ghost commented 4 years ago

Also note that termux-app/issues is not a place where we will debug third-party code (i.e. which is not part of Termux). Termux bug tracker is not a StackOverflow or similar.

mattmomo62 commented 4 years ago

Hello Xeffyr and Grimler91,

You were faster than me to put the response code 👍 , I was preparing it.

My initial purpose was only to know if select ,poll and socket was supposed to work in Termux because my code works in another env. Sorry if it ended by debugging it.. and thank you a lot for the help and the great job in your app and support ;)

Regards, mattmomo62