static int gp_port_serial_lock (GPPort *dev, const char *path) { #if defined(HAVE_LOCKDEV) int pid; #endif gp_log (GP_LOG_DEBUG, "gphoto2-port-serial", "Trying to lock '%s'...", path); #if defined(HAVE_TTYLOCK) if (ttylock ((char*) path)) { if (dev) gp_port_set_error (dev, _("Could not lock device " "'%s'"), path); return (GP_ERROR_IO_LOCK); } #define __HAVE_LOCKING #elif defined(HAVE_LOCKDEV) pid = dev_lock (path); if (pid) { if (dev) { if (pid > 0) gp_port_set_error (dev, _("Device '%s' is " "locked by pid %d"), path, pid); else gp_port_set_error (dev, _("Device '%s' could " "not be locked (dev_lock returned " "%d)"), path, pid); } return (GP_ERROR_IO_LOCK); } #define __HAVE_LOCKING #endif #ifndef __HAVE_LOCKING # ifdef __GCC__ # warning No locking library found. # warning You will run into problems if you use # warning gphoto2 with a serial (RS232) camera in # warning combination with Konqueror (KDE) or Nautilus (GNOME). # warning This will *not* concern USB cameras. # endif #endif return (GP_OK); }
int lockfile_create(void) { int n; if (portfd_is_socket) return 0; #if !HAVE_LOCKDEV if (!lockfile[0]) return 0; int fd; n = umask(022); /* Create lockfile compatible with UUCP-1.2 */ if ((fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0) { werror(_("Cannot create lockfile!")); } else { // FHS format: char buf[12]; snprintf(buf, sizeof(buf), "%10d\n", getpid()); buf[sizeof(buf) - 1] = 0; write(fd, buf, strlen(buf)); close(fd); } umask(n); return 0; #else n = ttylock(dial_tty); if (n < 0) { fprintf(stderr, _("Cannot create lockfile for %s: %s\n"), dial_tty, strerror(-n)); } else if (n > 0) { fprintf(stderr, _("Device %s is locked.\n"), dial_tty); } return n; #endif }
bool KMobileDevice::lockDevice(const QString &device, QString &err_reason) { #ifdef HAVE_BAUDBOY_H return ttylock(device.local8bit()) == EXIT_SUCCESS; #else # ifdef HAVE_LOCKDEV_H return !dev_lock(device.local8bit()); # else int pid = -1; QStringList all = QStringList::split('/', device); if (!all.count()) { err_reason = i18n("Invalid device (%1)").arg(device); return false; } QString lockName = DEVICE_LOCK_PATH_PREFIX + all[all.count()-1]; QFile file(lockName); if (file.exists() && file.open(IO_ReadOnly)) { if (file.size() == 0) { err_reason = i18n("Unable to read lockfile %s. Please check for reason and " "remove the lockfile by hand.").arg(lockName); PRINT_DEBUG << err_reason; return false; } if (file.size() == 4 && sizeof(int)==4) { file.readLine((char *)(&pid), 4); /* Kermit-style lockfile */ } else { QTextStream ts(&file); ts >> pid; /* Ascii lockfile */ } file.close(); if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) { PRINT_DEBUG << QString("Lockfile %1 is stale. Overriding it..\n").arg(lockName); sleep(1); if (!file.remove()) { PRINT_DEBUG << QString("Overriding failed, please check the permissions\n"); PRINT_DEBUG << QString("Cannot lock device %1\n").arg(device); err_reason = i18n("Lockfile %1 is stale. Please check permissions.").arg(lockName); return false; } } else { err_reason = i18n("Device %1 already locked.").arg(device); return false; } } /* Try to create a new file, with 0644 mode */ int fd = open(lockName.local8Bit(), O_CREAT | O_EXCL | O_WRONLY, 0644); if (fd == -1) { if (errno == EEXIST) err_reason = i18n("Device %1 seems to be locked by unknown process.").arg(device); else if (errno == EACCES) err_reason = i18n("Please check permission on lock directory."); else if (errno == ENOENT) err_reason = i18n("Cannot create lockfile %1. Please check for existence of path.").arg(lockName); else err_reason = i18n("Could not create lockfile %1. Error-Code is %2.").arg(lockName).arg(errno); return false; } QString lockText; lockText = QString("%1 kmobile\n").arg(getpid(),10); write(fd, lockText.utf8(), lockText.utf8().length()); close(fd); PRINT_DEBUG << QString("%1: Device %2 locked with lockfile %3.\n") .arg(deviceName()).arg(device).arg(lockName); err_reason = QString::null; return true; # endif #endif }