oh7bf / PiPIC

PiPIC one chip I/O helper for Raspberry Pi.
4 stars 1 forks source link

flock failure handling #1

Open mladenva opened 6 years ago

mladenva commented 6 years ago

Hi,

I've several i2c devices in my Rpi setup working together with pipicpowerd and sometimes i get error in syslog: " Failed to lock i2c port" from pipicpowerd.

I think problem is in this part of code (writecmd.c, readdata.c):

rd=flock(fd, LOCK_EX|LOCK_NB);

cnt=i2lockmax;
while((rd==1)&&(cnt>0)) // try again if port locking failed
{
  sleep(1);
  rd=flock(fd, LOCK_EX|LOCK_NB);
  cnt--;
}

if(rd)
{
  syslog(LOG_ERR, "Failed to lock i2c port");
  close(fd);
  return -2;

}

flock can never return 1, it returns 0 on success and -1 in case of problem, so this while loops ends after 1st failed flock, cnt is never used. while should be:

while((rd==-1)&&(cnt>0)) // try again if port locking failed { sleep(1); rd=flock(fd, LOCK_EX|LOCK_NB); cnt--; }

/Mladen

oh7bf commented 3 years ago

Hi Mladen,

Thanks for pointing this out. I am sorry that I did not have time to solve this issue earlier.