poll() not blocking, returns immediately - c

I'm trying to catch an interrupt on GPIO through sysfs using poll(). I have -1 in the third position so it can block, but it seems to be always returning. I've checked out some similar posts on SO. Notably this (1), this (2), and this (3).
In (1), it was solved by placing a dummy read() before calling poll(). If I do this (see commented read() in code). My code runs through the loop once and blocks forever on poll() the second time around.
In (2), this might be an explanation, yet doesn't really provide a solution to my problem.
In (3), I already have an lseek() before my read()
How can I get this poll() to block and only return as in interrupt when the the gpio's value has changed?
Here's the code snippet:
int read_gpio(char *path, void (*callback)(int)){
int fd;
char buf[11];
int res = 0;
char c;
int off;
struct pollfd gpio_poll_fd = {
.fd = fd,
.events = POLLPRI,
.revents = 0
};
for(;;){
gpio_poll_fd.fd = open(path, O_RDONLY);
if(fd == -1){
perror("error opening file");
return -1;
}
// char c;
// read(fd,&c,1);
LOGD("for begins");
res = poll(&gpio_poll_fd,1,-1);
LOGD("polling ended");
if(res == -1){
perror("error polling");
break;
}
if((gpio_poll_fd.revents & POLLPRI) == POLLPRI){
LOGD("POLLPRI");
off = lseek(fd, 0, SEEK_SET);
if(off == -1) break;
memset(&buf[0], 0, 11);
size_t num = read(fd, &buf[0], 10*sizeof(char));
LOGD("Before callback");
callback(atoi(buf));
LOGD("After Callback");
}
if((gpio_poll_fd.revents & POLLERR) == POLLERR) {
//seems always to be true ..
//LOGD("POLLERR");
}
close(fd);
LOGD("for ends");
}
LOGD("for exits");
return 0;
}
Note: As I'm doing this on Android JNI, I've been getting info for debugging from LOGD()
Update:
Following the advice in jxh's comment I've arranged the structure like so, although now it blocks on poll() indefinitely. When the content of value is changed from the externally applied voltage, POLLPRI doesn't go high, and poll() doesn't return:
int read_gpio(char *path, void (*callback)(int)){
int fd = open(path, O_RDONLY);
if(fd == -1){
perror("error opening file");
return -1;
}
char buf[11];
int res, off;
char c;
struct pollfd pfd = {
.fd = fd,
.events = POLLPRI,
.revents = 0
};
for(;;){
LOGD("for begins");
// dummy read causes poll never to run
// lseek() alone here cause poll never to run
// read(fd, &buf[],1);
// lseek(fd, 0, SEEK_SET);
res = poll(&pfd,1,-1);
LOGD("polling ended");
if(res == -1){
perror("error polling");
break;
}
if((pfd.revents & POLLPRI) == POLLPRI){
LOGD("POLLPRI");
off = lseek(fd, 0, SEEK_SET);
if(off == -1) break;
memset(&buf[0], 0, 11);
read(fd, &buf[0], 10*sizeof(char));
// These two lines will cause it to poll constantly
// close(fd);
// fd = open(path, O_RDONLY);
LOGD("Before callback");
callback(atoi(buf));
LOGD("After Callback");
}
LOGD("for ends");
}
close(fd);
LOGD("for exits");
return 0;
}

In your code, fd is not initialized.
When you open the file, you assign to gpio_poll_fd.fd directly, without using fd, so fd remains uninitialized.
Try:
gpio_poll_fd.fd = fd = open(path, O_RDONLY);
As pointed out in comments, according to the GPIO manual (which I had not read until after going through these comments more carefully), the GPIO sysfs interface is a little special:
If the pin can be configured as interrupt-generating interrupt
and if it has been configured to generate interrupts (see the
description of "edge"), you can poll(2) on that file and
poll(2) will return whenever the interrupt was triggered. If
you use poll(2), set the events POLLPRI and POLLERR. If you
use select(2), set the file descriptor in exceptfds. After
poll(2) returns, either lseek(2) to the beginning of the sysfs
file and read the new value or close the file and re-open it
to read the value.
So, although it is not the typical poll() idiom, your construct of closing and re-opening is correct. However, I would choose to leave the file descriptor open. So, here is how I would structure your code:
int read_gpio(char *path, void (*callback)(int)){
char buf[11];
int fd, res, off;
struct pollfd pfd;
if((pfd.fd = fd = open(path, O_RDONLY)) == -1){
perror("path");
return -1;
}
LOGD("First read");
res = read(fd, buf, 10);
assert(res == 10);
LOGD("Before callback");
callback(atoi(buf));
LOGD("After Callback");
pfd.events = POLLPRI|POLLERR; // poll(2) says setting POLLERR is
// unnecessary, but GPIO may be
// special.
for(;;){
LOGD("for begins");
if((res = poll(&pfd,1,-1)) == -1){
perror("poll");
break;
}
LOGD("polling ended");
if((pfd.revents & POLLPRI) == POLLPRI){
LOGD("POLLPRI");
off = lseek(fd, 0, SEEK_SET);
if(off == -1) break;
memset(buf, 0, 11);
res = read(fd, buf, 10);
assert(res == 10);
LOGD("Before callback");
callback(atoi(buf));
LOGD("After Callback");
} else {
// POLLERR, POLLHUP, or POLLNVAL
break;
}
LOGD("for ends");
}
close(fd);
LOGD("for exits");
return 0;
}

Related

how linux select() system call monitor two files(file descriptors) and how to use timer argument

I need to use select system call where I have to open two files (file descriptors) and do read operation on those files which are ready, I need to use some timeout after every 5ms and read from those files
Here is my sample code:
int main()
{
fd_set readfds,writefds;
ssize_t nbytes,bytes_read;
char buf[20];
int fd_1,fd_2,retval;
struct timeval tv;
fd_1 = open("test1.txt", O_RDWR);
if(fd_1 < 0) {
printf("Cannot open file...\n");
return 0;
}
fd_2 = open("test2.txt", O_RDWR);
if(fd_2 < 0) {
printf("Cannot open file_2...\n");
return 0;
}
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
for(;;)
{
retval = select(FD_SETSIZE, &readfds, &writefds, NULL, &tv);
printf("select");
perror("select");
exit(EXIT_FAILURE);
//}
for(int i=0; i < FD_SETSIZE; i++)
{
FD_ZERO(&readfds);
if (FD_ISSET(i, &readfds))
{
// read call happens here //
nbytes = sizeof(buf);
bytes_read = read(i, buf, nbytes);
printf("%ld", bytes_read);
}
else
{
perror("read");
exit(EXIT_FAILURE);
}
}
}
You need to initialize the file descriptors sets before calling select(). With your current code select ends up with EBADF.
Something like this should do it:
FD_ZERO (&writefds);
for(;;)
{
FD_ZERO (&readfds);
FD_SET(fd_1, &readfds);
FD_SET(fd_2, &readfds);
retval = select(FD_SETSIZE, &readfds, &writefds, NULL, &tv);
if (retval < 0) {
perror("select");
exit(EXIT_FAILURE);
}
for(int i=0; i<FD_SETSIZE; i++)
{
if (FD_ISSET(i, &readfds))
{
// read call happens here //
printf("reading from file: %d\n", i);
nbytes = sizeof(buf);
bytes_read = read(i, buf, nbytes);
printf("read %ld bytes\n", bytes_read);
}
}
}
And also you may want to check that for(;;) loop and only exit on error. Once your select behaves properly you can proceed with debugging inside your second for loop. You don't seem to use writefds here, so you may also just set it to NULL in select.
One more note: since you only have two files in the read set you could simply check FD_ISSET(fd_1) / FD_ISSET(fd_2) instead of iterating over all FD_SETSIZE entries.

Read() stuck on trying to read file

I have a checkpoint file that receives a server state. This states represents serialized commands that pass trough my network.
I'm trying to read the file but the it gets stuck on the read while loop.
My read function:
struct message_t *pmanager_readop(int fd){
if (fd < 0) return NULL;
// Variables
char *buffer = NULL;
int result, msg_size;
struct message_t *msg;
// Check if file has data
lseek (fd, 0, SEEK_END);
int size_ckp = lseek(fd, 0, SEEK_CUR);
if (size_ckp <= 0)
return NULL;
// Read message size
result = read_all(fd, (char *) &msg_size, 4);
if (result < 0) {
return NULL;
}
msg_size = ntohl(msg_size);
// ............
My read_all() function:
int read_all(int sock, char *buf, int len){
int bufsize = len;
while(len > 0){
int res = read(sock,buf,len);
if(res < 0){
if (errno == EINTR) continue;
return res;
}
buf += res;
len -= res;
}
return bufsize;
}
I use this same function to read data from my server/client connection with the same serialization and format but with a socket descriptor, and it works perfectly.
You ought to handle the case that read() returns 0, telling you that the other side shut-down the connection if reading from a socket descriptor, or EOF encountered if reading from a file descriptor.

How to decide if a file descriptor is attached to a file or socket in Linux

Using C language in Linux, how to decide if a file descriptor is attached to a file or socket?
Use getsockopt to get SO_TYPE on the file descriptor. If it is not a socket, it will return -1 with the error ENOTSOCK:
int fd = /* ... */;
bool is_socket;
int socket_type;
socklen_t length = sizeof(socket_type);
if(getsockopt(fd, SOL_SOCKET, SO_TYPE, &socket_type, &length) != -1) {
is_socket = true;
} else {
if(errno == ENOTSOCK) {
is_socket = false;
} else {
abort(); /* genuine error */
}
}
/* whether it is a socket will be stored in is_socket */
Alternatively, you can use fstat and the S_ISSOCK macro.
int main(int argc, char *argv[])
{
int fds[2];
fds[0] = 0; //stdin
fds[1] = socket(AF_INET,SOCK_STREAM, 0);
for (int i = 0; i < 2; ++i)
{
struct stat statbuf;
if (fstat(fds[i], &statbuf) == -1)
{
perror("fstat");
exit(1);
}
if (S_ISSOCK(statbuf.st_mode))
printf("%d is a socket\n", fds[i]);
else
printf("%d is NOT a socket\n", fds[i]);
}
return(0);
}
According to socket(7) man page:
Seeking, or calling pread(2) or pwrite(2) with a nonzero position is not supported on sockets.
So, another option would be to do one of those unsupported operations and see if you get an error. If you managed to seek, then FD is probably a file (or a device). Otherwise it may be a socket. Or a pipe.

How to convert blocking file io to non-blocking in C

I am writing a code send the output of a terminal command over a socket in C. I have tried using select for asynchronous reading and avoid blocking the event-loop, but I wasn't successful.
How can I change this code to make the file stream IO non-blocking?
int maxfdpl;
fd_set rset;
char sendline[100], recvline[100], my_msg[100];
FILE *in;
char str[30]="ping 192.168.26.219";
if(!(in = popen(str, "r"))){
return EXIT_FAILURE;
}
FD_ZERO(&rset);
FD_SET(fileno(in), &rset);
maxfdpl =fileno(in) + 1;
select(maxfdpl, &rset, NULL, NULL, NULL);
while(1) {
if (FD_ISSET(fileno(in), &rset)) {
if (fgets(sendline, 100, in)) {
send_over_socket(sendline);
}
}
}
How can I remove the while loop (which is blocking the event-loop) and replace the code with a non-blocking IO operation?
int blockFD(int fd, int blocking)
{
/* Save the current flags */
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1)
return 0;
if (blocking)
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
return fcntl(fd, F_SETFL, flags) != -1;
}
Returns 0 if failed.

How to rewrite full content of a file in C

I have text file which uses for ajax source. Every 1 sec browser sends ajax request to read actual data from this file.
Also I have deamon written on C which writes actual data to that file. Look at the following code:
static void writeToFile_withLock(const char * file_path, const char * str)
{
struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0 };
int fd;
const char * begin = str;
const char * const end = begin + strlen(str);
fl.l_pid = getpid();
if ((fd = open(file_path, O_CREAT | O_WRONLY)) == -1) {
perror("open");
exit(1);
}
printf("Trying to get lock...\n");
if (fcntl(fd, F_SETLKW, &fl) == -1) {
perror("fcntl");
exit(1);
}
printf("got lock\n");
printf("Try to write %s\n", str);
while (begin < end)
{
size_t remaining = end - begin;
ssize_t res = write(fd, begin, remaining);
if (res >= 0)
{
begin += res;
continue; // Let's send the remaining part of this message
}
if (EINTR == errno)
{
continue; // It's just a signal, try again
}
// It's a real error
perror("Write to file");
break;
}
fl.l_type = F_UNLCK; /* set to unlock same region */
if (fcntl(fd, F_SETLK, &fl) == -1) {
perror("fcntl");
exit(1);
}
printf("Unlocked.\n");
close(fd);
}
The problem: If former data was > the new data then old several symbols keeped at the end of the file.
How I can rewrite full file content?
Thanks in advance.
Add O_TRUNC to the open() call...
O_TRUNC
If the file already exists and is a regular file and the open mode
allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to
length 0. If the file is a FIFO or terminal device file, the O_TRUNC
flag is ignored. Otherwise the effect of O_TRUNC is unspecified.
You basically have two options. Either set the O_TRUNC bit of the 2nd parameter of open to discard all content when you open the file, or call ftruncate when you are finished to discard the content of the file that you do not want. (Or use truncate, but since you already have an open file descriptor, there's no advantage to doing that.)

Resources