i've been trying to write something that checks the inputs from the possible devices where a mouse can be connected (debian/linux) in order to make it configure the system according to the right path
i'm proceeding as follow:
i call a function that will invoke multiple threads, each one trying to get some input stream from a device that is declared on a configuration file. i can get the first one (/dev/psaux as it is configured now) to read correctly and it even print those funny character on the screen. the problem is when it gets to the second. guess what, seg fault.
if i take the same device and open it on a small piece of code just for testing, it can open with no seg fault, but with no input, just as it is expected, because it was supposed to be the serial port (/dev/ttyS0)
i've tried lots of thing, such as fscanf and fgetc, the closest shot being the below-presented fread_unlocked
i'm posting the function that is getting me into trouble, any more details you need please contact me
thanks a lot
Expand|Select|Wrap|Line Numbers
- void *getStream(pdevices dev, int i, pthread_mutex_t mutex) {
- FILE *device;
- FILE *log;
- void *inStream;
- char *timeStamp;
- size_t inSize;
- pthread_t self;
- printf("[DEBUG] i = %i | dev->devicesArray[i] = %i\n", i, dev->devicesArray[i]);
- if (dev->devicesArray[i]) {
- self = pthread_self();
- device = fopen(dev->devicesArrayString[i], "r");
- if((inStream = malloc(100 * sizeof(char))) == NULL) {
- puts("Memory allocation error");
- exit(1);
- }
- printf("[DEBUG] dev->devicesArrayString[i] = %s\n", dev->devicesArrayString[i]);
- inSize = fread_unlocked(inStream, sizeof(char), 99, device);
- printf("[DEBUG] Size of inSize = %i\n", (int) inSize);
- if(inSize < 0) {
- APPEND_LOG(log);
- TIMESTAMP(timeStamp);
- fprintf(log, "%s No entries on device %s\n", timeStamp, dev->devicesArrayString[i]);
- FREE(timeStamp);
- CLOSE(log);
- puts("[DEBUG] Nothing read!");
- }
- printf("[DEBUG] inStream = %s\n", (char *)inStream);
- if (inStream != NULL) {
- dev->devicesArray[i] = 1;
- if (pthread_mutex_lock(&mutex)) {
- fprintf(stderr, "mutex lock error\n");
- exit(1);
- }
- APPEND_LOG(log);
- TIMESTAMP(timeStamp);
- fprintf(log, "%s Input on device %s: OK\n", timeStamp, dev->devicesArrayString[i]);
- FREE(timeStamp);
- TIMESTAMP(timeStamp);
- fprintf(log, "%s %s: %d\n", timeStamp, dev->devicesArrayString[i], (int) self);
- FREE(timeStamp);
- CLOSE(log);
- if(pthread_mutex_unlock(&mutex)) {
- fprintf(stderr, "mutex unlock error\n");
- exit(1);
- }
- } else {
- dev->devicesArray[i] = 0;
- }
- FREE(inStream);
- CLOSE(device);
- }
- return 0;
- }