proot icon indicating copy to clipboard operation
proot copied to clipboard

Connection refused when connect /dev/socket/logdr socket

Open chairwa opened this issue 1 year ago • 3 comments

Problem description From within proot-distro login ubuntu-oldlts, I failed to connect /dev/socket/logdr socket.

Steps to reproduce the test program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>

int main() {
    int sock;
    struct sockaddr_un addr;
    
    // create UNIX domain socket
    sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
    if (sock < 0) {
        printf("socket() failed: %s\n", strerror(errno));
        return 1;
    }
    
    // set socket addr
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, "/dev/socket/logdr", sizeof(addr.sun_path) - 1);
    
    // connect socket
    printf("try connect /dev/socket/logdr...\n");
    if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        printf("conn failed: %s (errno: %d)\n", strerror(errno), errno);
        close(sock);
        return 1;
    }
    
    printf("conn OK!\n");
    
    // close socket
    close(sock);
    return 0;
}

Expected behavior connection OK

Additional information the above code is OK in termux.

chairwa avatar Feb 27 '25 14:02 chairwa

Why exactly do you need connecting logd socket and why can not you use logwrapper?

twaik avatar Feb 27 '25 17:02 twaik

https://github.com/twoyi/twoyi As what Twoyi did, I'm trying to run Android rootfs in termux proot environment.

Twoyi's logcat is OK, but my AOSP logcat failed to connect logdr socket. At first, I thought this was permission problem; but I found the above test program is OK in termux. So the problem might lie in proot.

chairwa avatar Feb 27 '25 22:02 chairwa

It seems to be caused by my modifications to proot. I cannot bind entire /dev to the proot guest, and -b /dev/socket/logdr will also fail. proot warning: can't sanitize binding "/dev/socket/logdr": Permission denied

I have to modify the proot code, hoping to achieve direct access to the host's /dev/socket/logdr, without binding and translation, from within the guest. In the path.c translate_path function, I made the following modification. Because ECONNREFUSED (Connection refused) occurred, the modification seems not right.

status = canonicalize(tracee, guest_path, deref_final, result, 0); changed to

    const char *excluded_paths[] = {
        "/dev/socket/logd",
        "/dev/socket/logdr",
        "/dev/socket/logdw"
    };
    const int num_excluded_paths = sizeof(excluded_paths) / sizeof(excluded_paths[0]);

    // Flag variable, used to determine whether to call canonicalize
    int should_canonicalize = 1;
    // Traverse the exclusion path list and check if it matches
    for (int i = 0; i < num_excluded_paths; i++) {
        if (strcmp(guest_path, excluded_paths[i]) == 0) {
            // If matched, set the flag to 0 and exit the loop
            should_canonicalize = 0;
            break;
        }
    }

	if (should_canonicalize) {
		/* Canonicalize regarding the new root. */
		status = canonicalize(tracee, guest_path, deref_final, result, 0);
		if (status < 0)
			return status;
	}

chairwa avatar Feb 28 '25 05:02 chairwa