~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kinfocenter/Modules/samba/ksmbstatus.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ksmbstatus.cpp
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include <signal.h>
 
20
#include <stdio.h>
 
21
#include <stdlib.h>
 
22
#include <string.h>
 
23
#include <time.h>
 
24
 
 
25
#include <QLayout>
 
26
 
 
27
#include <klocale.h>
 
28
#include <kdialog.h>
 
29
 
 
30
#include "ksmbstatus.h"
 
31
#include "ksmbstatus.moc"
 
32
 
 
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(""))
 
35
 
 
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());
 
41
 
 
42
        list=new Q3ListView(this,"Hello");
 
43
        topLayout->addWidget(list);
 
44
        version=new QLabel(this);
 
45
        topLayout->addWidget(version);
 
46
 
 
47
        list->setAllColumnsShowFocus(true);
 
48
        list->setMinimumSize(425, 200);
 
49
        list->setShowSortIndicator(true);
 
50
 
 
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"));
 
58
 
 
59
        timer = new QTimer(this);
 
60
        timer->start(15000);
 
61
        QObject::connect(timer, SIGNAL(timeout()), this, SLOT(update()));
 
62
        update();
 
63
}
 
64
 
 
65
void NetMon::processNFSLine(char *bufline, int) {
 
66
        QByteArray line(bufline);
 
67
        if (line.contains(":/"))
 
68
                new Q3ListViewItem(list,"NFS",After(":",line),Before(":/",line));
 
69
}
 
70
 
 
71
void NetMon::processSambaLine(char *bufline, int) {
 
72
        QByteArray line(bufline);
 
73
        rownumber++;
 
74
        if (rownumber == 2)
 
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);
 
88
 
 
89
                line=line.mid(iMachine, line.length());
 
90
                strMachine=line;
 
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))
 
95
                readingpart=finished;
 
96
        else if (readingpart==locked_files) {
 
97
                if ((strncmp(bufline, "Pi", 2) !=0) // "Pid DenyMode ..."
 
98
                                && (strncmp(bufline, "--", 2) !=0)) // "------------"
 
99
                {
 
100
                        char *tok=strtok(bufline, " ");
 
101
                        if (tok) {
 
102
                                int pid=atoi(tok);
 
103
                                (lo)[pid]++;
 
104
                        }
 
105
                }
 
106
        }
 
107
}
 
108
 
 
109
// called when we get some data from smbstatus
 
110
// can be called for any size of buffer (one line, several lines,
 
111
// half of one ...)
 
112
void NetMon::slotReceivedData(K3Process *, char *buffer, int) {
 
113
        //kDebug()<<"received stuff";
 
114
        char s[250], *start, *end;
 
115
        size_t len;
 
116
        start = buffer;
 
117
        while ((end = strchr(start, '\n'))) // look for '\n'
 
118
        {
 
119
                len = end-start;
 
120
                if (len>=sizeof(s))
 
121
                        len=sizeof(s)-1;
 
122
                strncpy(s, start, len);
 
123
                s[len] = '\0';
 
124
                //kDebug() << "recived: "<<s;
 
125
                if (readingpart==nfs)
 
126
                        processNFSLine(s, len);
 
127
                else
 
128
                        processSambaLine(s, len); // process each line
 
129
                start=end+1;
 
130
        }
 
131
        if (readingpart==nfs) {
 
132
                list->viewport()->update();
 
133
                list->update();
 
134
        }
 
135
        // here we could save the remaining part of line, if ever buffer
 
136
        // doesn't end with a '\n' ... but will this happen ?
 
137
}
 
138
 
 
139
void NetMon::update() {
 
140
        K3Process * process = new K3Process();
 
141
 
 
142
        memset(&lo, 0, sizeof(lo));
 
143
        list->clear();
 
144
        /* Re-read the Contents ... */
 
145
 
 
146
        QString path(::getenv("PATH"));
 
147
        path += "/bin:/sbin:/usr/bin:/usr/sbin";
 
148
 
 
149
        rownumber=0;
 
150
        readingpart=header;
 
151
        nrpid=0;
 
152
        process->setEnvironment("PATH", path);
 
153
        connect(process,
 
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\""));
 
161
        else
 
162
        {
 
163
                // ok -> count the number of locked files for each pid
 
164
                for (Q3ListViewItem *row=list->firstChild();row!=0;row=row->itemBelow())
 
165
                {
 
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]));
 
169
                }
 
170
        }
 
171
        delete process;
 
172
        process=0;
 
173
 
 
174
        readingpart=nfs;
 
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
 
186
        {
 
187
                delete showmountProc;
 
188
                showmountProc=0;
 
189
        }
 
190
 
 
191
        version->adjustSize();
 
192
        list->show();
 
193
}
 
194
 
 
195
void NetMon::killShowmount() {
 
196
        //kDebug()<<"killShowmount()";
 
197
        delete showmountProc;
 
198
        showmountProc=0;
 
199
}
 
200