File indexing completed on 2024-10-06 04:26:00
0001 /* 0002 SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "k3blsofwrapper.h" 0007 0008 #include "k3bdevice.h" 0009 #include "k3bglobals.h" 0010 0011 #include <KProcess> 0012 0013 #include <QFile> 0014 #include <QFileInfo> 0015 #include <QList> 0016 0017 #include <sys/types.h> 0018 #include <unistd.h> 0019 0020 static K3b::LsofWrapper::Process createProcess( const QString& name, int pid ) 0021 { 0022 K3b::LsofWrapper::Process p; 0023 p.name = name; 0024 p.pid = pid; 0025 return p; 0026 } 0027 0028 0029 class K3b::LsofWrapper::Private 0030 { 0031 public: 0032 QList<Process> apps; 0033 QString lsofBin; 0034 }; 0035 0036 0037 K3b::LsofWrapper::LsofWrapper() 0038 { 0039 d = new Private; 0040 } 0041 0042 0043 K3b::LsofWrapper::~LsofWrapper() 0044 { 0045 delete d; 0046 } 0047 0048 0049 bool K3b::LsofWrapper::checkDevice( K3b::Device::Device* dev ) 0050 { 0051 d->apps.clear(); 0052 0053 if( !findLsofExecutable() ) 0054 return false; 0055 0056 // run lsof 0057 KProcess p; 0058 p.setOutputChannelMode( KProcess::OnlyStdoutChannel ); 0059 0060 // 0061 // We use the following output form: 0062 // p<PID> 0063 // c<COMMAND_NAME> 0064 // 0065 p << d->lsofBin << "-Fpc" << dev->blockDeviceName(); 0066 p.start(); 0067 0068 if( !p.waitForFinished( -1 ) ) 0069 return false; 0070 0071 // 0072 // now process its output 0073 const QStringList l = QString::fromLocal8Bit( p.readAllStandardOutput() ).split( '\n', Qt::SkipEmptyParts ); 0074 QStringList::ConstIterator it = l.constBegin(); 0075 while ( it != l.constEnd() ) { 0076 int pid = it->mid(1).toInt(); 0077 0078 if ( ++it != l.constEnd() ) { 0079 QString app = it->mid( 1 ); 0080 0081 qDebug() << "(K3b::LsofWrapper) matched: app: " << app << " pid: " << pid; 0082 0083 // we don't care about ourselves using the device ;) 0084 if( pid != (int)::getpid() ) 0085 d->apps.append( createProcess( app, pid ) ); 0086 0087 ++it; 0088 } 0089 } 0090 0091 return true; 0092 } 0093 0094 0095 const QList<K3b::LsofWrapper::Process>& K3b::LsofWrapper::usingApplications() const 0096 { 0097 return d->apps; 0098 } 0099 0100 0101 bool K3b::LsofWrapper::findLsofExecutable() 0102 { 0103 if( d->lsofBin.isEmpty() ) 0104 d->lsofBin = K3b::findExe( "lsof" ); 0105 0106 return !d->lsofBin.isEmpty(); 0107 }