Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mattmomo62 opened this issue Nov 6, 2020 · 6 comments
Closed

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

mattmomo62 opened this issue Nov 6, 2020 · 6 comments

Comments

@mattmomo62
Copy link

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 <unistd.h>
...
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
Copy link
Member

What is your output of termux-info?

@mattmomo62
Copy link
Author

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
Copy link
Member

Could you provide a compilable example that segfaults for you?

@ghost
Copy link

ghost commented Nov 6, 2020

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 ghost closed this as completed Nov 6, 2020
@ghost
Copy link

ghost commented Nov 6, 2020

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
Copy link
Author

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

@ghost ghost locked and limited conversation to collaborators Oct 17, 2021
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants