diff options
author | Michele Calgaro <[email protected]> | 2016-02-24 21:05:19 +0700 |
---|---|---|
committer | Michele Calgaro <[email protected]> | 2016-02-26 22:22:06 +0700 |
commit | 3811fc1773ad7e6140d0dd3f5c6bba4524d71ef2 (patch) | |
tree | d68f1df74ecc6c5f188b0eb2f05311d8093b5584 /kmobile | |
parent | b80f9a2251d5f0993a7a3438f4a083813a315024 (diff) | |
download | tdepim-3811fc1773ad7e6140d0dd3f5c6bba4524d71ef2.tar.gz tdepim-3811fc1773ad7e6140d0dd3f5c6bba4524d71ef2.zip |
Fixed FTBFS in Debian/Ubuntu due to missing liblockdev1-dev package. Device locking is now done through 'flock()'
Signed-off-by: Michele Calgaro <[email protected]>
(cherry picked from commit 3ed5f6bda5dabbb74b0126982cb515de1a8e2558)
Diffstat (limited to 'kmobile')
-rw-r--r-- | kmobile/DESIGN | 12 | ||||
-rw-r--r-- | kmobile/Makefile.am | 4 | ||||
-rw-r--r-- | kmobile/configure.in.in | 16 | ||||
-rw-r--r-- | kmobile/kmobiledevice.cpp | 124 | ||||
-rw-r--r-- | kmobile/kmobiledevice.h | 3 |
5 files changed, 34 insertions, 125 deletions
diff --git a/kmobile/DESIGN b/kmobile/DESIGN index 90ea509ec..c8e28f068 100644 --- a/kmobile/DESIGN +++ b/kmobile/DESIGN @@ -7,7 +7,7 @@ "kmobile" is suite to easily access "Mobile Devices", which means, that you have one single interface to access -any type of mobile device (e.g. cellular phone, PDAs, +any type of mobile device (e.g. cellular phone, PDAs, MP3-Players, Digital Cameras and a lot more). Each of this devices have different types of information, @@ -17,11 +17,11 @@ Each of this devices have different types of information, - calendar entries, - a file storage section (e.g. pictures in digital cameras) - and more - + The whole interface is pretty extendable. Each device has -a device driver, which reports the capatibilities of the +a device driver, which reports the capatibilities of the connected device to the higher level. -So, if you once write a device driver, you can access it's +So, if you once write a device driver, you can access it's contents from any KDE application later. Currently the whole interface is divided into 3 sections: @@ -48,7 +48,7 @@ The mid-layer handles the main functionality, which is entirely implemented in the kmobile application. All low-level drivers are loaded by kmobile only, and then all low-level functions to any device is made available to other applications -with a DCOP interface. Normal KDE applications should prefer the +with a DCOP interface. Normal KDE applications should prefer the userland library (see below) instead of using direct DCOP calls. Nevertheless, the DCOP interface might be very interesting to write standalone command line tools. @@ -86,7 +86,7 @@ HINTS FOR DRIVER DEVELOPERS: all calls to your driver, so you always will have a clean state - use lockDevice("/dev/ttyS1") and unlockDevice("/dev/ttyS1") to - lock those devices system-wide (creates /var/lock/LCK..<devname> files), + lock those devices system-wide, and to prevent other applications to access the same physical ports/devices - use the helper functions createDirEntry() and createFileEntry() to diff --git a/kmobile/Makefile.am b/kmobile/Makefile.am index 6d2640ee2..249ab533e 100644 --- a/kmobile/Makefile.am +++ b/kmobile/Makefile.am @@ -2,7 +2,7 @@ SUBDIRS = . devices # tdeioslave -bin_PROGRAMS = kmobile +bin_PROGRAMS = kmobile lib_LTLIBRARIES = libkmobiledevice.la libkmobileclient.la @@ -21,7 +21,7 @@ noinst_HEADERS = kmobile.h kmobileview.h kmobileitem.h pref.h # the low-level devices driver library libkmobiledevice_la_SOURCES = kmobiledevice.cpp libkmobiledevice_la_LDFLAGS = $(all_libraries) -no-undefined -avoid-version -libkmobiledevice_la_LIBADD = $(LIB_TDEABC) $(LIB_LOCKDEV) ../libkcal/libkcal.la +libkmobiledevice_la_LIBADD = $(LIB_TDEABC) ../libkcal/libkcal.la # the KDE application's client library to KMobile libkmobileclient_la_SOURCES = kmobileclient.cpp diff --git a/kmobile/configure.in.in b/kmobile/configure.in.in deleted file mode 100644 index cf47242d2..000000000 --- a/kmobile/configure.in.in +++ /dev/null @@ -1,16 +0,0 @@ -DO_NOT_COMPILE="$DO_NOT_COMPILE kmobile" - -AC_CHECK_HEADERS(baudboy.h,,, -[ -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#define inline __inline__ -]) -AC_CHECK_HEADERS(lockdev.h) -AC_CHECK_LIB(lockdev, dev_lock, [ LIB_LOCKDEV="-llockdev" ]) -dnl We don't need liblockdev if we have baudboy working -if [[ -n "$HAVE_BAUDBOY_H" ]]; then - LIB_LOCKDEV="" -fi -AC_SUBST(LIB_LOCKDEV) diff --git a/kmobile/kmobiledevice.cpp b/kmobile/kmobiledevice.cpp index c408d72d0..abf750579 100644 --- a/kmobile/kmobiledevice.cpp +++ b/kmobile/kmobiledevice.cpp @@ -19,7 +19,7 @@ #include <sys/types.h> #include <sys/stat.h> -#include <sys/types.h> +#include <sys/file.h> #include <unistd.h> #include <time.h> #include <errno.h> @@ -48,8 +48,7 @@ */ KMobileDevice::KMobileDevice(TQObject *obj, const char *name, const TQStringList &args) - : KLibFactory(obj,name), - m_config(0L), d(0L) + : KLibFactory(obj,name), m_config(0L), d(0L), m_fd(-1) { setClassType(Unclassified); setCapabilities(hasNothing); @@ -348,103 +347,33 @@ void KMobileDevice::special( const TQByteArray & ) // and no one is in this group. Only the binary /usr/sbin/lockdev is // owned by this group and has the setgid flag set. This binary is called // in ttylock etc ... -# include <sys/types.h> -# include <sys/stat.h> -# include <fcntl.h> # include <baudboy.h> # include <cstdlib> -#else -# ifdef HAVE_LOCKDEV_H -// Library shipped with newer RedHat and Debian(?) distributions. -// Use this if bauddev is not available. -# include <lockdev.h> -# include <sys/types.h> -# include <unistd.h> -# else -// If lockdev.h is also unavailable do locking -// like described in the serial HOWTO. -# include <sys/types.h> -# include <sys/stat.h> -# include <unistd.h> -# include <tqfile.h> -# include <signal.h> -# include <errno.h> -# endif #endif -#define DEVICE_LOCK_PATH_PREFIX "/var/lock/LCK.." - bool KMobileDevice::lockDevice(const TQString &device, TQString &err_reason) { #ifdef HAVE_BAUDBOY_H - return ttylock(device.local8bit()) == EXIT_SUCCESS; + return ttylock(device.local8Bit()) == EXIT_SUCCESS; #else -# ifdef HAVE_LOCKDEV_H - return !dev_lock(device.local8bit()); -# else - int pid = -1; - TQStringList all = TQStringList::split('/', device); - if (!all.count()) { - err_reason = i18n("Invalid device (%1)").arg(device); - return false; - } - TQString lockName = DEVICE_LOCK_PATH_PREFIX + all[all.count()-1]; - TQFile 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 { - TQTextStream ts(&file); - ts >> pid; /* Ascii lockfile */ - } - file.close(); - - if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) { - PRINT_DEBUG << TQString("Lockfile %1 is stale. Overriding it..\n").arg(lockName); - sleep(1); - if (!file.remove()) { - PRINT_DEBUG << TQString("Overriding failed, please check the permissions\n"); - PRINT_DEBUG << TQString("Cannot lock device %1\n").arg(device); - err_reason = i18n("Lockfile %1 is stale. Please check permissions.").arg(lockName); + if (m_fd != -1) + return true; + + TQCString dev = TQFile::encodeName(device.local8Bit()); + const char *fdev = dev.data(); + if ((m_fd = open(fdev, O_RDWR)) == -1) + { + PRINT_DEBUG << i18n("Unable to open device '%1'. " + "Please check that you have sufficient permissions.").arg(fdev); 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; - } - TQString lockText; - lockText = TQString("%1 kmobile\n").arg(getpid(),10); - write(fd, lockText.utf8(), lockText.utf8().length()); - close(fd); - - PRINT_DEBUG << TQString("%1: Device %2 locked with lockfile %3.\n") - .arg(deviceName()).arg(device).arg(lockName); - - err_reason = TQString(); - + if (flock(m_fd, LOCK_EX)) + { + // Locking failed + PRINT_DEBUG << i18n("Unable to lock device '%1'.").arg(fdev); + return false; + } return true; -# endif #endif } @@ -453,17 +382,12 @@ bool KMobileDevice::unlockDevice(const TQString &device) #ifdef HAVE_BAUDBOY_H return ttyunlock(device.local8bit()) == EXIT_SUCCESS; #else -# ifdef HAVE_LOCKDEV_H - return 0 <= dev_unlock(device.local8bit(), getpid()); -# else - TQStringList all = TQStringList::split('/', device); - if (!all.count()) return false; - TQString lockName = DEVICE_LOCK_PATH_PREFIX + all[all.count()-1]; - TQFile file(lockName); - if (!file.exists()) - return true; - return file.remove(); -# endif + if (m_fd != -1) + { + flock(m_fd, LOCK_UN); + } + close(m_fd); + m_fd = -1; #endif } diff --git a/kmobile/kmobiledevice.h b/kmobile/kmobiledevice.h index d4469d358..78fc50e80 100644 --- a/kmobile/kmobiledevice.h +++ b/kmobile/kmobiledevice.h @@ -65,7 +65,7 @@ class TDEConfig; class KDE_EXPORT KMobileDevice : public KLibFactory { Q_OBJECT - + friend class KMobileView; public: @@ -497,6 +497,7 @@ protected: TQString m_connectionName; // e.g. "IRDA", "USB", "Cable", "gnokii", "gammu", ... int m_caps; // see enum Capabilities bool m_connected; + int m_fd; // file descriptor used for locking/unlocking devices private: class KMobileDevicePrivate *d; |