how to get poll info from a linux file descriptor
To check out connections to /dev/ttyACM* devices I needed a quick way to see the current poll flags that show error conditions but also the connection state. This simple tool shows you the current poll events.
When you open the tty device you get the file descriptor, which is a simple number for linux, for example 5.
Then, when you want to know the current poll events you just type poll 5 and get something like:
Poll arg:5 FD#:5
Revents set:1 Revents value:4
Flags: POLLOUT
fin
The first line shows the argument and the file descriptor number, the second line gives you the number of poll bits that are ON and the value of the combined poll events. The last part is a list of poll flags (in the example just one) and fin to mark the end of the list.
More details: check out man 2 poll
C code:
// show poll status of a fd
#include <stdio.h>
#include <stdlib.h>
#include <poll.h>
void printrevents(int i){
printf("Revents value:%d\nFlags: ",i);
if (i & POLLIN) puts("POLLIN ");
if (i & POLLOUT) puts("POLLOUT ");
if (i & POLLPRI) puts("POLLPRI ");
if (i & POLLERR) puts("POLLERR ");
if (i & POLLHUP) puts("POLLHUP ");
if (i & POLLNVAL) puts("POLLNVAL ");
}
void main( int argc, char *argv[] )
{ int fd;
struct pollfd polldata[1];
int rval;
fd=atoi(argv[1]);
printf("Poll arg:%s FD#:%d\n", argv[1], fd);
polldata[0].fd=fd;
polldata[0].events=POLLIN|POLLOUT;
rval=poll(polldata,1,100);
if (rval == 0) puts("Timeout ");
if (rval ==-1) perror("Poll error:");
if (rval >= 0) {
printf("Revents set:%d ",rval);
printrevents(polldata[0].revents);
}
if (rval < -1) printf("Unexpected poll return value:%d",rval);
puts("fin");
}
Build with:
gcc -g poll.c -o poll
Try with:
./poll 1