Reputation: 252
I have an issue with my program and I don't know what the source of this problem is. My program is to read data from several RFID readers (up to 8 readers), so I'm using select()
to determine whether or not there is data to be read. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <signal.h>
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
int main(int argc, char *argv[]) {
// Variables and constants
int fd[8];
int max_fd;
char *devices[8];
char name[256] = "Unknown";
fd_set readset;
printf("\n");
// 1. Setup check
// 1.1 Check if user is root
if ((getuid()) != 0) {
printf(KRED "Warning: " KNRM "You are not root! This may not work.\n");
}
// 1.2 Check that all of the argument vectors has data (within the argument count)
for(int i = 1; i < argc; i++){
if (argv[i] == NULL) {
printf(KRED "Error: " KNRM "Something is wrong with the path to the device event\n");
exit(0);
}
}
// 1.3 Copy the vectors to device array
for(int i = 1; i < argc; i++){
devices[i] = argv[i];
}
// 2. Open Devices
for(int i = 1; i < argc; i++){
if ((fd[i] = open(devices[i], O_RDONLY)) == -1){
printf(KRED "Warning: " KNRM "%s is not a valid device.\n", devices[i]);
}
}
// 3. Print Device Names
printf("\n----------------------------------------------------------------------------\n");
for(int i = 1; i < argc; i++){
// Check if device open was successful
if(fd[i] != -1){
ioctl(fd[i], EVIOCGNAME(sizeof(name)), name);
printf(" Reading From : %s (%s)\n", devices[i], name);
}
}
printf("----------------------------------------------------------------------------\n");
printf("\n");
// 3.1 Initialize set
FD_ZERO(&readset);
max_fd = 0;
for(int j = 0; j < argc; j++){
printf(KGRN "now here" KNRM "\n");
FD_SET(fd[j], &readset); // <-- Stalls here!!
printf("not here\n");
max_fd = (max_fd > fd[j]) ? max_fd : fd[j];
printf("%i\n", max_fd);
}
// 4. Now, check for readability
int result = select(max_fd+1, &readset, NULL, NULL, NULL);
if (result == -1) {
printf(KRED "Warning: " KNRM "Some error occurred while checking for readability\n\n");
} else {
for (int i = 0; i < argc; i++) {
if (FD_ISSET(fd[i], &readset)) {
printf("%u is readable\n", fd[i]);
}
}
}
return 0;
}
And it hangs in this line (when point 4 is commented, if point 4 is not commented the program exits at this same line):
FD_SET(fd[j], &readset); // <-- Stalls here!!
I have no idea what is wrong so any suggestions is appreciated! (Down below is a screen-shot illustrating the program being run)
Upvotes: 0
Views: 520
Reputation: 409196
Your indexing of the arrays in your program is not consistent, giving you undefined behavior when you access index 0
with is uninitialized.
You also don't check for file descriptors not opened, and blindly attempt to set and use all in fd
.
Upvotes: 1