Non blocking select for inotify - c++

I try to use inotify in c++ inside a thread
but the select is blocking, so I can never get outside the thread when my application exits
how I create the inotify watch
fd=inotify_init1(IN_NONBLOCK);
// checking for error
if ( fd < 0 )
log->Print("Could not init files listener");
else
{
// use select watch list for non-blocking inotify read
FD_ZERO( &watch_set );
FD_SET( fd, &watch_set );
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
// watch directory for any activity and report it back to me
int wd=inotify_add_watch(fd,folder.c_str(),IN_ALL_EVENTS);
// add wd and directory name to Watch map
watch.insert( -1, folder, wd );
// start listening thread
run(FilesListener::threadBootstrap);
}
here is the function called in my thread loop
void FilesListener::refresh()
{
char buffer[1024];
// select waits until inotify has 1 or more events.
// select needs the highest fd (+1) as the first parameter.
select( fd+1, &watch_set, NULL, NULL, NULL );
// Read event(s) from non-blocking inotify fd (non-blocking specified in inotify_init1 above).
int length = read( fd, buffer, EVENT_BUF_LEN );
if ( length < 0 )
log->Print("Could not read inotify file descriptor");
else
{
....

Check https://github.com/paulorb/FileMonitor it has an easy interface for achieving what you want. It is a port of windows API to Linux using inotify.
Example:
#include "FileMonitor.hpp"
int main(void)
{
int m_EventID = FindFirstChangeNotification("/media/sf_P_DRIVE/FileMonitor/", 0, FILE_NOTIFY_CHANGE_FILE_NAME);
int ret = WaitForSingleObject(m_EventID, 10000);
printf("\nFinish %d", ret);
fflush(stdout);
FindNextChangeNotification(m_EventID);
int ret2 = WaitForSingleObject(m_EventID, 10000);
printf("\nFinish %d", ret2);
FindCloseChangeNotification(1);
printf("\nChangeNotification done");
fflush(stdout);
return 0;
}
If you prefer to do it yourself try to use the poll function, you can use it inside of a thread.
struct pollfd pfd = { th_params->fd, POLLIN, 0 };
int ret = poll(&pfd, 1, 50); // timeout of 50ms
if (ret < 0) {
printf("\failed poll");
}
else if (ret == 0) {
// Timeout with no events, move on.
printf("\nTimeout poll");
}
else {
i = 0;
int lenght = read(th_params->fd, buffer, 1024);
}

void FilesListener::refresh()
{
char buffer[1024];
int length = read( fd, buffer, EVENT_BUF_LEN );
if ( length >=0 )
{
....

Related

select() returns -1 while reading from socket

I'm trying to solve reading from socket timeout problem using select(). Unfortunately this function returns -1 immediatly after getting called. What might be wrong?
commStatus communicate( const char * tx, char * rx, const int bufSize , const char * inetAddr, const int port )
{
commStatus r;
if (!sockInitialised) initSock();
if (sockInitialised)
{
SOCKET s;
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr(inetAddr);
server.sin_family = AF_INET;
server.sin_port = htons( port );
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
std:stringstream d; d <<"Could not create socket : " << WSAGetLastError();
LogIt(d,Level::Error);
} else
{
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
r= commStatus::COMM_NO_TRANSMIT ;
} else
{
int l =strlen(tx)+strlen("DATA ");
char* dtx ;
dtx = (char*) calloc(sizeof(char),strlen(tx)+strlen("DATA ") + 1 );
sprintf(dtx,"DATA %s",tx);
if( send(s , dtx , strlen(dtx) , 0) < 0)
{
puts("Send failed");
r= commStatus::COMM_NO_TRANSMIT;
} else
{
int recv_size = 0;
struct timeval selTimeout;
selTimeout.tv_sec = 20; /* timeout (secs.) */
selTimeout.tv_usec = 0; /* 0 microseconds */
fd_set readSet;
FD_ZERO(&readSet);
#define STDIN_FILENO 0
FD_SET(STDIN_FILENO, &readSet);//stdin manually trigger reading
FD_SET(s, &readSet);//tcp socket
int numReady = select(s+1, &readSet, NULL, NULL, &selTimeout);
if(numReady > 0)
{
if((recv_size = recv(s , rx , bufSize ,0)) == SOCKET_ERROR)
{
r= commStatus::COMM_NO_RECEIVE;
} else
{
rx[recv_size] = '\0';
r= commStatus::COMM_OK;
}
} else r=commStatus::COMM_NO_RECEIVE;
}
free(dtx);
}
}
} else r= commStatus::COMM_NO_TRANSMIT;
return r;
}
As far as I can tell, you're trying to pass stdin to Winsock's select() function.
Unfortunately, under Windows, select() only takes socket handles, not generic file descriptors.
If you were to call WSAGetLastError(), you'd probably see that it returns WSAENOTSOCK.
Under C, you can examine the errno variable, it should tell you the specific problem that caused your error.
Alternatively, you may have the perror() function which will give you a readable version:
#include <stdio.h>
:
perror ("select");
If you're under Windows, WSAGetLastError() will serve as the errno variable, with this page giving the possibilities and explanations:
WSANOTINITIALISED - A successful WSAStartup call must occur before using this function.
WSAEFAULT - The Windows Sockets implementation was unable to allocate needed resources for its internal operations, or the readfds, writefds, exceptfds, or timeval parameters are not part of the user address space.
WSAENETDOWN - The network subsystem has failed.
WSAEINVAL - The time-out value is not valid, or all three descriptor parameters were null.
WSAEINTR - A blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall.
WSAEINPROGRESS - A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAENOTSOCK - One of the descriptor sets contains an entry that is not a socket.

Win32 API: how to read the serial, or exit within a timeout if wasn't a data

I need a function to read data from a serial port or return if none came within a time interval. E.g. on GNU/Linux you could use poll()orselect()+read(). Anything analogous in Windows?
Below is something I tried: it ought to work, but the function GetOverlappedResult() is either buggy or not well documented; instead of number of bytes read it reports nothing (not even zero, e.g. if I don't initialize the lpNumberOfBytesTransferred argument, it just contains junk).
// NOTE: the `file` should be opened in non-blocking mode
long ReadData(HANDLE file, char* buf, long szbuf, int msTimeout){
OVERLAPPED ReadState = {0};
ReadState.hEvent = CreateEvent(0, true, false, "☠");
if (!ReadState.hEvent){
PrintLastErr();
return -1;
}
unsigned long BytesRead = 0; //number of bytes was read
if (!ReadFile(file, buf, szbuf, &BytesRead, &ReadState)){
// This creates a reading event. Below we wait for it to complete, and cancel on timeout
if (GetLastError() != ERROR_IO_PENDING){
PrintLastErr();
return -1;
}
// No, I can't use WaitForSingleObject(), it exits with disregard to whether
// reading is ongoing or no more data left (e.g. in case of a serial port).
while(1) {
std::this_thread::sleep_for(std::chrono::milliseconds( msTimeout ));
unsigned long ret;
puts("Getting result");
if (!GetOverlappedResult(file, &ReadState, &ret, false)
&& GetLastError() != ERROR_IO_INCOMPLETE){
PrintLastErr();
return -1;
}
printf("result is %lu\n",ret);
if(ret == BytesRead){
return BytesRead;
}
BytesRead = ret;
}
} else { //completed immediately
printf("Bytes read %i\n");
assert(BytesRead <= LONG_MAX);
return (long)BytesRead;
}
}
Call SetCommTimeouts after you open the COM port. This sets ReadFile to return after an interval if no data is received. Then you simply call ReadFile. No overlap or event or GetOverlappedResult is needed.
It was really hard to figure; but thanks to a forum and lots of experimentation I finally found a way. Here's the code:
/**
* Opens a file in overlapped mode.
* \returns HANDLE to a file, or 0 on fail
*/
HANDLE OpenFile(const char* FileName){
HANDLE file = CreateFile( FileName,
GENERIC_READ | GENERIC_WRITE,
0, //we're greedy(or just lazy)
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if (file == INVALID_HANDLE_VALUE){
return 0;
}
if(!SetCommMask(file, EV_RXCHAR)){ // set a mask for incoming characters event.
return 0;
}
return file;
}
/**
* Waits for data to arrive
* \param file a file opened in overlapped mode
* \param msTimeout is a maximum time to wait
* \returns -1 on system error, 0 on success, and 1 if time out
*/
int WaitForData(HANDLE file, unsigned long msTimeout){
int ret;
unsigned long Occured;//returns the type of an occured event
OVERLAPPED FileEvent = {0};
FileEvent.hEvent = CreateEvent(0, true, false, 0);
do{
if(!WaitCommEvent(file, &Occured, &FileEvent)){
if(GetLastError() != ERROR_IO_PENDING){
ret = -1;
break;
}
}
switch(WaitForSingleObject(FileEvent.hEvent, msTimeout)){
case WAIT_OBJECT_0: //the requested event happened
ret = 0; //a success
break;
case WAIT_TIMEOUT://time out
ret = 1;
break;
default://error in WaitForSingleObject
ret = -1;
break;
}
break;
}while(0);
CloseHandle(FileEvent.hEvent);
return ret;
}
/**
* Reads data from a file
* \param file a file opened in overlapped mode
* \param buf a buf for data
* \param szbuf size of buffer
* \returns number of bytes read or -1 on fail
*/
unsigned long ReadData(HANDLE file, char* buf, unsigned long szbuf){
int ret;
unsigned long BytesRead = 0; //number of bytes was read
OVERLAPPED ReadState = {0};
do{
ReadState.hEvent = CreateEvent(0, true, false, 0);
if (!GetOverlappedResult(file, &ReadState, &BytesRead, false)){//get how many bytes incame
ret = ULONG_MAX;
break;
}
if (ReadFile( file,
buf,
(BytesRead<=szbuf) ? BytesRead : szbuf,
0,
&ReadState) == 0){
ret = ULONG_MAX;
break;
}
ret = BytesRead;
}while(0);
CloseHandle(ReadState.hEvent);
return ret;
}
So, how does it work?
Open the port with FILE_FLAG_OVERLAPPED flag
Use SetCommMask() to set EV_RXCHAR as the only events we're interested in is incoming data.
In a cycle wait for data for a specified amount of time, read if anything came.
Also #ScottMcP-MVP's answer is wrong: there's no use to SetCommTimeouts(). On the first sight this function and COMMTIMEOUTS looks like exactly what you'd want though, which drove me to hours of confusion. ☺

how to detect eventfd file descriptor close by other program

I have a client/server communicate through eventfd. If either client or server call close(fd) I would like the other end to find out (like file descriptor is closed now). I tried to use select with non-zero timeout, it always return 0 which is timeout. I saw people suggesting use fcntl it doesn't seems to be working either. Any suggestions?
Addtion Details (omitted non important part code, you can see here for how to exchange file descriptor detail code:
It is multi processes application. Server process created eventfd by calling
struct msghdr control_message;
int fd = eventfd(0,0);
*CMSG_DATA(control_message) = fd;
message.msg_control = &control_message;
sendmsg(socket_fd, & message,0); //send this to client
From client side:
recvmsg(socket_fd, & message,0);
//loop using CMSG_NXTHDR(&message, control_message)
int fd = *(int *) CMSG_DATA(contro_message);
Then on server side:
close(fd);
On Client side:
int rc;
rc = dup2(fd,fd);
rc is never invalid.
Checking for a closed file descriptor? How about this?
#include <errno.h>
#include <stdio.h>
#include <string.h>
static void checkit ()
{
int rc;
rc = dup2(2, 2);
if ( rc == -1 )
printf("error %d on dup2(2, 2): %s\n", errno, strerror(errno));
else
printf("dup2 successful\n");
write(2, "still working\n", 14);
}
int main (int argc, char **argv)
{
int rc;
checkit();
close(2);
checkit();
return 0;
}
Running it generates this output:
dup2 successful
still working
error 9 on dup2(2, 2): Bad file descriptor
If this is a multi-threaded application using poll and you want poll to return when the file descriptor is closed by another thread, POLLERR, POLLHUP, or POLLNVAL might help.
Multi-Threaded Version using Poll
And here's a sample that shows how to detect a closed fd with poll (POLLNVAL is the event) in a multi-threaded program:
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static void *run_poll (void *arg)
{
struct pollfd fds[1];
int rc;
fds[0].fd = 2;
fds[0].events = POLLERR | POLLHUP | POLLNVAL;
//
// Poll indefinitely
//
printf("starting poll\n");
fflush(stdout);
rc = poll((struct pollfd *) &fds, 1, -1);
if ( rc == -1 )
{
printf("POLL returned error %d: %s\n", errno, strerror(errno));
}
else
{
printf("POLL returned %d (revents = 0x%08x): %s %s %s\n",
rc,
fds[0].revents,
( ( fds[0].revents & POLLERR ) ? "pollerr" : "noerr" ),
( ( fds[0].revents & POLLHUP ) ? "pollhup" : "nohup" ),
( ( fds[0].revents & POLLNVAL ) ? "pollnval" : "nonval" ));
}
return NULL;
}
int main (int argc, char **argv)
{
pthread_t thread;
int rc;
rc = pthread_create(&thread, NULL, run_poll, NULL);
usleep(100);
printf("closing stderr\n");
close(2);
usleep(100);
return 0;
}
This generates the output
starting poll
closing stderr
POLL returned 1 (revents = 0x00000020): noerr nohup pollnval

Use of select in c++ to do a timeout in a protocol to transfer files over serial port

I have a function to write data to the serial port with a certain protocol. When the function writes one frame, it waits for one answer of receiver. If no answer is received it has to resend data during 3 timeouts and in the end of 3 timeouts with no success, close the communication...
I have this function:
int serial_write(int fd, unsigned char* send, size_t send_size) {
......
int received_counter = 0;
while (!RECEIVED) {
Timeout.tv_usec = 0; // milliseconds
Timeout.tv_sec = timeout; // seconds
FD_SET(fd, &readfs);
//set testing for source 1
res = select(fd + 1, &readfs, NULL, NULL, &Timeout);
//timeout occurred.
if (received_counter == 3) {
printf(
"Connection maybe turned off! Number of resends exceeded!\n");
exit(-1);
}
if (res == 0) {
printf("Timeout occured\n");
write(fd, (&I[0]), I.size());
numTimeOuts++;
received_counter++;
} else {
RECEIVED = true;
break;
}
}
......
}
I have verified that this function, when it goes into timeout, does not resend the data. Why?

Serial Comm using WriteFile/ReadFile

//#include "StdAfx.h"
#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#include <iostream>
#include <tchar.h>
using namespace std;
int main()
{
int com = 'COM2';
string data = "\n 010400 \n";
char output[32];
//unsigned int length = 0;
DCB config = {0};
bool abContinue = true;
DWORD dwBytesWritten;
DWORD dwBytesRead;
int isRead = false;
HANDLE m_hCommPort = ::CreateFile(L"COM2",
GENERIC_READ|GENERIC_WRITE,//access ( read and write)
0, //(share) 0:cannot share the COM port
0, //security (None)
OPEN_EXISTING,// creation : open_existing
0, // we dont want overlapped operation
0// no templates file for COM port...
);
config.DCBlength = sizeof(config);
if((GetCommState(m_hCommPort, &config) == 0))
{
printf("Get configuration port has a problem.");
return FALSE;
}
config.BaudRate = 9600;
config.StopBits = ONESTOPBIT;
config.Parity = PARITY_NONE;
config.ByteSize = DATABITS_8;
config.fDtrControl = 0;
config.fRtsControl = 0;
if (!SetCommState(m_hCommPort, &config))
{
printf( "Failed to Set Comm State Reason: %d\n",GetLastError());
//return E_FAIL;
}
printf("Current Settings\n Baud Rate %d\n Parity %d\n Byte Size %d\n Stop Bits %d", config.BaudRate,
config.Parity, config.ByteSize, config.StopBits);
int isWritten = WriteFile(m_hCommPort, &data,(DWORD) sizeof(data), &dwBytesWritten, NULL);
//memset(output, 0, sizeof(output));
while (abContinue)
{
isRead = ReadFile(m_hCommPort, output, sizeof(output), &dwBytesRead, NULL);
if(!isRead)
{
abContinue = false;
break;
}
}
cin.get();
}
I am having trouble reading from the com port. If I step through the code, it goes into "isRead = ReadFile(m_hCommPort, output, sizeof(output), &dwBytesRead, NULL);" and doesn't come back out.... This is my first try at this with no success.
You might try some code something like this after you've opened the file, but before you try to use it:
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 1;
timeouts.ReadTotalTimeoutMultiplier = 1;
timeouts.ReadTotalTimeoutConstant = 1;
timeouts.WriteTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 1;
if (!SetCommTimeouts(m_hCommPort, &timeouts))
// setting timeouts failed.
Edit: perhaps it's easier to start with some code that works, and make it do what you want rather than trying to get your code to work. Here's a simple terminal program. It's minimalist in the extreme, but does work (at least well enough to let me see output from my GPS, for one example). It's a long ways from what anybody (least of all me) would call sophisticated, but should give at least some idea of how to get started.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
void system_error(char *name) {
// Retrieve, format, and print out a message from the last error. The
// `name' that's passed should be in the form of a present tense noun
// (phrase) such as "opening file".
//
char *ptr = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
0,
GetLastError(),
0,
(char *)&ptr,
1024,
NULL);
fprintf(stderr, "\nError %s: %s\n", name, ptr);
LocalFree(ptr);
}
int main(int argc, char **argv) {
int ch;
char buffer[1];
HANDLE file;
COMMTIMEOUTS timeouts;
DWORD read, written;
DCB port;
HANDLE keyboard = GetStdHandle(STD_INPUT_HANDLE);
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode;
char port_name[128] = "\\\\.\\COM3";
char init[] = ""; // e.g., "ATZ" to completely reset a modem.
if ( argc > 2 )
sprintf(port_name, "\\\\.\\COM%c", argv[1][0]);
// open the comm port.
file = CreateFile(port_name,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if ( INVALID_HANDLE_VALUE == file) {
system_error("opening file");
return 1;
}
// get the current DCB, and adjust a few bits to our liking.
memset(&port, 0, sizeof(port));
port.DCBlength = sizeof(port);
if ( !GetCommState(file, &port))
system_error("getting comm state");
if (!BuildCommDCB("baud=19200 parity=n data=8 stop=1", &port))
system_error("building comm DCB");
if (!SetCommState(file, &port))
system_error("adjusting port settings");
// set short timeouts on the comm port.
timeouts.ReadIntervalTimeout = 1;
timeouts.ReadTotalTimeoutMultiplier = 1;
timeouts.ReadTotalTimeoutConstant = 1;
timeouts.WriteTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 1;
if (!SetCommTimeouts(file, &timeouts))
system_error("setting port time-outs.");
// set keyboard to raw reading.
if (!GetConsoleMode(keyboard, &mode))
system_error("getting keyboard mode");
mode &= ~ ENABLE_PROCESSED_INPUT;
if (!SetConsoleMode(keyboard, mode))
system_error("setting keyboard mode");
if (!EscapeCommFunction(file, CLRDTR))
system_error("clearing DTR");
Sleep(200);
if (!EscapeCommFunction(file, SETDTR))
system_error("setting DTR");
if ( !WriteFile(file, init, sizeof(init), &written, NULL))
system_error("writing data to port");
if (written != sizeof(init))
system_error("not all data written to port");
// basic terminal loop:
do {
// check for data on port and display it on screen.
ReadFile(file, buffer, sizeof(buffer), &read, NULL);
if ( read )
WriteFile(screen, buffer, read, &written, NULL);
// check for keypress, and write any out the port.
if ( kbhit() ) {
ch = getch();
WriteFile(file, &ch, 1, &written, NULL);
}
// until user hits ctrl-backspace.
} while ( ch != 127);
// close up and go home.
CloseHandle(keyboard);
CloseHandle(file);
return 0;
}
If you do not explicitly set the timeouts, then ReadFile will indefinitely block until data becomes available.
ReadFile function may be blocking your thread,if so, it will remain blocked until some data can be read from Serial port. Here is a link see if its help. Good luck.
I had this problem on a readfile, with the timeouts set. This was driving me crackers so I ended up getting some code from the web which did work and then changing line by line to see where the error was.
Turns out he readfile was fine. My problem was a WaitCommEvent which was hanging when the port was disconnected as no com event is ever received...