Device-QuickCam

 view release on metacpan or  search on metacpan

libcqcam/port.C  view on Meta::CPAN

#ifdef LINUX
#ifdef arm
  if (devport >= 0)
    close(devport);
#else
  if (port > 0 && geteuid() == 0)
    if (ioperm(port, 3, 0) != 0)  // drop port permissions -- still must
                                  // be root
      perror("ioperm()");
#endif /* arm */
#elif defined(FREEBSD)
  if (devio != NULL)
    fclose(devio);
#elif defined(SOLARIS)
  close(iopfd);
#endif /* which OS */
}

#ifdef LOCKING
int port_t::lock(int portnum) {
  char lockfile[80];
  sprintf(lockfile, "/tmp/LOCK.qcam.0x%x", portnum);
  while ((lock_fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, 0600)) == -1) {
    if (errno != EEXIST) {
      perror(lockfile);
      return -1;
    }
    struct stat stat_buf;
    if (lstat(lockfile, &stat_buf) < 0) continue;
    if (S_ISLNK(stat_buf.st_mode) || stat_buf.st_uid != 0) {
      if (unlink(lockfile)) {
        if (errno == ENOENT) continue;
        if (errno != EISDIR || (rmdir(lockfile) && errno != ENOENT)) {
            /* known problem: if lockfile exists and is a non-empty
               directory, we give up instead of doing an rm-r of it */
          perror(lockfile);
          return -1;
        }
      }
      continue;
    }
    lock_fd = open(lockfile, O_WRONLY, 0600);
    if (lock_fd == -1) {
      perror(lockfile);
      return -1;
    }
    break;
  }

  static struct flock lock_info;
  lock_info.l_type = F_WRLCK;
#ifdef LOCK_FAIL
  if (fcntl(lock_fd, F_SETLK, &lock_info) != 0) {
#else
  if (fcntl(lock_fd, F_SETLKW, &lock_info) != 0) {
#endif /* LOCK_FAIL */
    if (errno != EAGAIN)
      perror("fcntl");
    return -1;
  }
  chown(lockfile, getuid(), getgid());
#ifdef DEBUG
  fprintf(stderr, "Locked port 0x%x\n", portnum);
#endif /* DEBUG */
  return 0;
}
    
void port_t::unlock(int portnum) {
  if (portnum == -1)
    return;
  close(lock_fd);    // this clears the lock
  char lockfile[80];
  sprintf(lockfile, "/tmp/LOCK.qcam.0x%x", portnum);
  if (unlink(lockfile)) perror(lockfile);
#ifdef DEBUG
  fprintf(stderr, "Unlocked port 0x%x\n", portnum);
#endif /* DEBUG */
}
#endif /* LOCKING */



( run in 0.582 second using v1.01-cache-2.11-cpan-71847e10f99 )