1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*
* ksmbstatus.cpp
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <tqlayout.h>
#include <tdelocale.h>
#include <kdialog.h>
#include "ksmbstatus.h"
#include "ksmbstatus.moc"
#define Before(ttf,in) in.left(in.find(ttf))
#define After(ttf,in) (in.contains(ttf)?TQString(in.mid(in.find(ttf)+TQString(ttf).length())):TQString(""))
NetMon::NetMon( TQWidget * parent, TDEConfig *config, const char * name )
: TQWidget(parent, name)
,configFile(config)
,showmountProc(0)
,strShare("")
,strUser("")
,strGroup("")
,strMachine("")
,strSince("")
,strPid("")
,iUser(0)
,iGroup(0)
,iMachine(0)
,iPid(0)
{
TQBoxLayout *topLayout = new TQVBoxLayout(this, KDialog::marginHint(),
KDialog::spacingHint());
topLayout->setAutoAdd(true);
list=new TQListView(this,"Hello");
version=new TQLabel(this);
list->setAllColumnsShowFocus(true);
list->setMinimumSize(425,200);
list->setShowSortIndicator(true);
list->addColumn(i18n("Type"));
list->addColumn(i18n("Service"));
list->addColumn(i18n("Accessed From"));
list->addColumn(i18n("UID"));
list->addColumn(i18n("GID"));
list->addColumn(i18n("PID"));
list->addColumn(i18n("Open Files"));
timer = new TQTimer(this);
timer->start(15000);
TQObject::connect(timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(update()));
update();
}
void NetMon::processNFSLine(char *bufline, int)
{
TQCString line(bufline);
if (line.contains(":/"))
new TQListViewItem(list,"NFS",After(":",line),Before(":/",line));
}
void NetMon::processSambaLine(char *bufline, int)
{
TQCString line(bufline);
rownumber++;
if (rownumber == 2)
version->setText(bufline); // second line = samba version
if ((readingpart==header) && line.contains("Service"))
{
iUser=line.find("uid");
iGroup=line.find("gid");
iPid=line.find("pid");
iMachine=line.find("machine");
}
else if ((readingpart==header) && (line.contains("---")))
{
readingpart=connexions;
}
else if ((readingpart==connexions) && (int(line.length())>=iMachine))
{
strShare=line.mid(0,iUser);
strUser=line.mid(iUser,iGroup-iUser);
strGroup=line.mid(iGroup,iPid-iGroup);
strPid=line.mid(iPid,iMachine-iPid);
line=line.mid(iMachine,line.length());
strMachine=line;
new TQListViewItem(list,"SMB",strShare,strMachine, strUser,strGroup,strPid/*,strSince*/);
}
else if (readingpart==connexions)
readingpart=locked_files;
else if ((readingpart==locked_files) && (line.find("No ")==0))
readingpart=finished;
else if (readingpart==locked_files)
{
if ((strncmp(bufline,"Pi", 2) !=0) // "Pid DenyMode ..."
&& (strncmp(bufline,"--", 2) !=0)) // "------------"
{
char *tok=strtok(bufline," ");
if (tok) {
int pid=atoi(tok);
(lo)[pid]++;
}
}
}
}
// called when we get some data from smbstatus
// can be called for any size of buffer (one line, several lines,
// half of one ...)
void NetMon::slotReceivedData(TDEProcess *, char *buffer, int )
{
//kdDebug()<<"received stuff"<<endl;
char s[250],*start,*end;
size_t len;
start = buffer;
while ((end = strchr(start,'\n'))) // look for '\n'
{
len = end-start;
if (len>=sizeof(s))
len=sizeof(s)-1;
strncpy(s,start,len);
s[len] = '\0';
//kdDebug() << "recived: "<<s << endl;
if (readingpart==nfs)
processNFSLine(s,len);
else
processSambaLine(s,len); // process each line
start=end+1;
}
if (readingpart==nfs)
{
list->viewport()->update();
list->update();
}
// here we could save the remaining part of line, if ever buffer
// doesn't end with a '\n' ... but will this happen ?
}
void NetMon::update()
{
TDEProcess * process = new TDEProcess();
memset(&lo, 0, sizeof(lo));
list->clear();
/* Re-read the Contents ... */
TQString path(::getenv("PATH"));
path += "/bin:/sbin:/usr/bin:/usr/sbin";
rownumber=0;
readingpart=header;
nrpid=0;
process->setEnvironment("PATH", path);
connect(process,
TQT_SIGNAL(receivedStdout(TDEProcess *, char *, int)),
TQT_SLOT(slotReceivedData(TDEProcess *, char *, int)));
*process << "smbstatus";
if (!process->start(TDEProcess::Block,TDEProcess::Stdout))
version->setText(i18n("Error: Unable to run smbstatus"));
else if (rownumber==0) // empty result
version->setText(i18n("Error: Unable to open configuration file \"smb.conf\""));
else
{
// ok -> count the number of locked files for each pid
for (TQListViewItem *row=list->firstChild();row!=0;row=row->itemBelow())
{
// cerr<<"NetMon::update: this should be the pid: "<<row->text(5)<<endl;
int pid=row->text(5).toInt();
row->setText(6,TQString("%1").arg((lo)[pid]));
}
}
delete process;
process=0;
readingpart=nfs;
delete showmountProc;
showmountProc=new TDEProcess();
showmountProc->setEnvironment("PATH", path);
*showmountProc<<"showmount"<<"-a"<<"localhost";
connect(showmountProc,TQT_SIGNAL(receivedStdout(TDEProcess *, char *, int)),TQT_SLOT(slotReceivedData(TDEProcess *, char *, int)));
//without this timer showmount hangs up to 5 minutes
//if the portmapper daemon isn't running
TQTimer::singleShot(5000,this,TQT_SLOT(killShowmount()));
//kdDebug()<<"starting kill timer with 5 seconds"<<endl;
connect(showmountProc,TQT_SIGNAL(processExited(TDEProcess*)),this,TQT_SLOT(killShowmount()));
if (!showmountProc->start(TDEProcess::NotifyOnExit,TDEProcess::Stdout)) // run showmount
{
delete showmountProc;
showmountProc=0;
}
version->adjustSize();
list->show();
}
void NetMon::killShowmount()
{
//kdDebug()<<"killShowmount()"<<endl;
delete showmountProc;
showmountProc=0;
}
|