ioctl() - lockup / freeze

sadvk

New Member
Joined
Feb 4, 2021
Messages
1
Reaction score
0
Credits
17
Hello.

I have written an app in C for the R-Pi-4 on the latest Raspbian release. I use a little bit of code (found on stack exchange) that copies the IP address - see below if interested. This all works fine when I run the program manually after some time after boot. However if run immediately after boot then the code section works fine for around 30 seconds, then hangs, specifically on the ioctl line: ioctl never returns. Other things in the program keep working - I have various background tasks (via SIGINT and timers) running state machines for a TCP server and keyboard buffer and they still work fine - but this one little section of code that tries to copy the IP address bombs out at that line. This happens even with a static IP address so doesn't seem related to DHCP delays.

Can anyone explain what might be happening here?
TIA. Kris.

Code:
    char ipaddr[30];
    int fd;
    struct ifreq ifr;
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    // IPv4 IP address.
    ifr.ifr_addr.sa_family = AF_INET;
    strncpy(ifr.ifr_name, "wlan", IFNAMSIZ-1);
    ioctl(fd, SIOCGIFADDR, &ifr);    // THIS IS THE OFFENDING FUNCTION!!
    close(fd);
    strcpy(ipaddr, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
 

Members online


Top