4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include "ksmbstatus.h"
31 #include "ksmbstatus.moc"
33 #define Before(ttf,in) in.left(in.indexOf(ttf))
34 #define After(ttf,in) (in.contains(ttf)?QString(in.mid(in.indexOf(ttf)+QString(ttf).length())):QString(""))
36 NetMon::NetMon(QWidget
* parent
, KConfig
*config
) :
37 QWidget(parent
), configFile(config
), showmountProc(0), strShare(""), strUser(""), strGroup(""), strMachine(""), strSince(""), strPid(""), iUser(0), iGroup(0), iMachine(0), iPid(0) {
38 QBoxLayout
*topLayout
= new QVBoxLayout(this);
39 topLayout
->setMargin(KDialog::marginHint());
40 topLayout
->setSpacing(KDialog::spacingHint());
42 list
=new Q3ListView(this,"Hello");
43 topLayout
->addWidget(list
);
44 version
=new QLabel(this);
45 topLayout
->addWidget(version
);
47 list
->setAllColumnsShowFocus(true);
48 list
->setMinimumSize(425, 200);
49 list
->setShowSortIndicator(true);
51 list
->addColumn(i18n("Type"));
52 list
->addColumn(i18n("Service"));
53 list
->addColumn(i18n("Accessed From"));
54 list
->addColumn(i18n("UID"));
55 list
->addColumn(i18n("GID"));
56 list
->addColumn(i18n("PID"));
57 list
->addColumn(i18n("Open Files"));
59 timer
= new QTimer(this);
61 QObject::connect(timer
, SIGNAL(timeout()), this, SLOT(update()));
65 void NetMon::processNFSLine(char *bufline
, int) {
66 QByteArray
line(bufline
);
67 if (line
.contains(":/"))
68 new Q3ListViewItem(list
,"NFS",After(":",line
),Before(":/",line
));
71 void NetMon::processSambaLine(char *bufline
, int) {
72 QByteArray
line(bufline
);
75 version
->setText(bufline
); // second line = samba version
76 if ((readingpart
==header
) && line
.contains("Service")) {
77 iUser
=line
.indexOf("uid");
78 iGroup
=line
.indexOf("gid");
79 iPid
=line
.indexOf("pid");
80 iMachine
=line
.indexOf("machine");
81 } else if ((readingpart
==header
) && (line
.contains("---"))) {
82 readingpart
=connexions
;
83 } else if ((readingpart
==connexions
) && (int(line
.length())>=iMachine
)) {
84 strShare
=line
.mid(0, iUser
);
85 strUser
=line
.mid(iUser
, iGroup
-iUser
);
86 strGroup
=line
.mid(iGroup
, iPid
-iGroup
);
87 strPid
=line
.mid(iPid
, iMachine
-iPid
);
89 line
=line
.mid(iMachine
, line
.length());
91 new Q3ListViewItem(list
,"SMB",strShare
,strMachine
, strUser
,strGroup
,strPid
/*,strSince*/);
92 } else if (readingpart
==connexions
)
93 readingpart
=locked_files
;
94 else if ((readingpart
==locked_files
) && (line
.indexOf("No ")==0))
96 else if (readingpart
==locked_files
) {
97 if ((strncmp(bufline
, "Pi", 2) !=0) // "Pid DenyMode ..."
98 && (strncmp(bufline
, "--", 2) !=0)) // "------------"
100 char *tok
=strtok(bufline
, " ");
109 // called when we get some data from smbstatus
110 // can be called for any size of buffer (one line, several lines,
112 void NetMon::slotReceivedData(K3Process
*, char *buffer
, int) {
113 //kDebug()<<"received stuff";
114 char s
[250], *start
, *end
;
117 while ((end
= strchr(start
, '\n'))) // look for '\n'
122 strncpy(s
, start
, len
);
124 //kDebug() << "recived: "<<s;
125 if (readingpart
==nfs
)
126 processNFSLine(s
, len
);
128 processSambaLine(s
, len
); // process each line
131 if (readingpart
==nfs
) {
132 list
->viewport()->update();
135 // here we could save the remaining part of line, if ever buffer
136 // doesn't end with a '\n' ... but will this happen ?
139 void NetMon::update() {
140 K3Process
* process
= new K3Process();
142 memset(&lo
, 0, sizeof(lo
));
144 /* Re-read the Contents ... */
146 QString
path(::getenv("PATH"));
147 path
+= "/bin:/sbin:/usr/bin:/usr/sbin";
152 process
->setEnvironment("PATH", path
);
154 SIGNAL(receivedStdout(K3Process
*, char *, int)),
155 SLOT(slotReceivedData(K3Process
*, char *, int)));
156 *process
<< "smbstatus";
157 if (!process
->start(K3Process::Block
,K3Process::Stdout
))
158 version
->setText(i18n("Error: Unable to run smbstatus"));
159 else if (rownumber
==0) // empty result
160 version
->setText(i18n("Error: Unable to open configuration file \"smb.conf\""));
163 // ok -> count the number of locked files for each pid
164 for (Q3ListViewItem
*row
=list
->firstChild();row
!=0;row
=row
->itemBelow())
166 // cerr<<"NetMon::update: this should be the pid: "<<row->text(5)<<endl;
167 int pid
=row
->text(5).toInt();
168 row
->setText(6,QString("%1").arg((lo
)[pid
]));
175 delete showmountProc
;
176 showmountProc
=new K3Process();
177 showmountProc
->setEnvironment("PATH", path
);
178 *showmountProc
<<"showmount"<<"-a"<<"localhost";
179 connect(showmountProc
,SIGNAL(receivedStdout(K3Process
*, char *, int)),SLOT(slotReceivedData(K3Process
*, char *, int)));
180 //without this timer showmount hangs up to 5 minutes
181 //if the portmapper daemon isn't running
182 QTimer::singleShot(5000,this,SLOT(killShowmount()));
183 //kDebug()<<"starting kill timer with 5 seconds";
184 connect(showmountProc
,SIGNAL(processExited(K3Process
*)),this,SLOT(killShowmount()));
185 if (!showmountProc
->start(K3Process::NotifyOnExit
,K3Process::Stdout
)) // run showmount
187 delete showmountProc
;
191 version
->adjustSize();
195 void NetMon::killShowmount() {
196 //kDebug()<<"killShowmount()";
197 delete showmountProc
;