--- linux2300.c 2005-03-05 09:26:58.000000000 +0100 +++ linux2300.c 2005-04-15 11:37:55.000000000 +0200 @@ -28,18 +28,36 @@ WEATHERSTATION open_weatherstation(char *device) { WEATHERSTATION ws2300; + int my_lock; struct termios adtio; int portstatus; + int masque; - //Setup serial port +/* +create if not exist, a world readable and writable file (also all other users can create this lock ex: apache, mysql ...) , with a lock in no blocking mode. +*/ + masque=umask(0); + if ((my_lock = open("/tmp/mylock_ws2300.lck", O_CREAT|O_RDWR,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH )) < 0) + { + printf("\nUnable to open /tmp/mylock_ws2300.lck\n"); + exit(EXIT_FAILURE); + } + + if ( flock(my_lock, LOCK_EX|LOCK_NB) < 0 ) { + perror("\n/tmp/mylock_ws2300.lck is locked by other program\n"); + exit(EXIT_FAILURE); + } + umask(masque); + + //Setup serial port if ((ws2300 = open(device, O_RDWR | O_NOCTTY)) < 0) { printf("\nUnable to open serial device %s\n", device); exit(EXIT_FAILURE); } - if ( flock(ws2300, LOCK_EX) < 0 ) { + if ( flock(ws2300, LOCK_EX|LOCK_NB) < 0 ) { perror("\nSerial device is locked by other program\n"); exit(EXIT_FAILURE); }