summaryrefslogtreecommitdiffstats
path: root/mountconfig/fuser.py
blob: 9e4ec11825b43c7e9884acbe8173bd45641a17b2 (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/python
###########################################################################
# fuser.py - description                                                  #
# ------------------------------                                          #
# begin     : Wed Jun 15 2005                                             #
# copyright : (C) 2005-2006 by Sebastian Kuegler                          #
# email     : sebas@vizZzion.org                                          #
#                                                                         #
###########################################################################
#                                                                         #
#   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.                                   #
#                                                                         #
###########################################################################
"""
TODO:
- Fix running standalone:
    * TDECmdLineArgs stuff.
"""

import sys
import os
from qt import *
from tdeui import *
#import kdedesigner
from fuser_ui import *
from SimpleCommandRunner import *

standalone = __name__ == "__main__"

class FileProcess(QListViewItem):
    """ A FileProcess is simply one line from lsof, one filedescriptor that's in use 
        by a process represented as a listviewitem in the lsof processtable. """

    # Available signals.
    signals = {
        "TERM":15,
        "KILL":9 }

    # Column names mapping.
    cols = {
        "pname":0,
        "pid":1,
        "powner":2,
        "pfile":3 }

    def __init__(self,parent,pid,isparent=False):
        QListViewItem.__init__(self,parent)
        self.setPid(pid)
        self.isparent = isparent
        self.pfile = ""
        self.pix = None

    def setPid(self,pid):
        self.pid = pid

    def setName(self,pname):
        self.pname = pname

    def setOwner(self,powner):
        self.powner = powner

    def setFile(self,pfile):
        self.pfile = pfile

    def setPixmaps(self,pix):
        """ Eats a dict with pixmaps. """
        self.pix = pix

    def sendSignal(self,signal):
        """ Parses a signal string representation or a signal number and sends it to
            the process."""
        if not self.isparent:
            print "Item is not a process, only a filedescriptor."
            return
        try:
            signal_int = int(signal)
        except ValueError:
            try:
                signal_int = self.signals[signal]
            except IndexError:
                print "No known signal received ", signal
                return False
        try:
            rc = os.kill(int(self.pid),signal_int) # TODO: Catch OSError
        except OSError, message:
            print "OSError: Couldn't %s %s %s" % (signal,self.pname,self.pid)
            print message
        if not rc:
            print "Successfully sent signal ", signal_int, " to process ", self.pid
            return True
        print "Signal %i didn't succeed" % signal_int
        return False

    def fillColumns(self):
        """ Writes strings into columns once an entry is completed. """
        if self.isparent:
            self.setText(self.cols["pid"],self.pid)
            self.setText(self.cols["pname"],self.pname)
            self.setText(self.cols["powner"],self.powner)
            self.setPixmap(0,self.pix["exec"])
            self.setPixmap(1,self.pix["pid"])
            self.setPixmap(2,self.pix["owner"])
        else:
            self.setText(self.cols["pfile"],self.pfile)
            self.setPixmap(3,self.pix["file"])

########################################################################################################
class FUser(FUserUI):
    """ done() / result() return 0 on successful umount and 1 if cancelled. """

    def __init__(self,device,parentdialog=None,lsof_bin='/usr/sbin/lsof',kapp=None):
        FUserUI.__init__(self,parentdialog,name = None,modal = 0,fl = 0)
        self.device = device
        self.fileprocesses = []
        self.lsof_bin = '/usr/sbin/lsof'
        self.setLsof(lsof_bin)
        self.setApp(kapp)

        self.processlist.clear()
        self.processhidden = False
        # We're having processes blocking umounting, show that.
        self.umountbutton.setEnabled(False)

        self.explanationlabel.setText(
            unicode(i18n("""The volume %s is in use and can not be disabled.<br>
            <br>
            The processes that are blocking %s are listed below. These processes must be closed 
            before %s can be disabled.
            Killing a process may cause data loss! Make sure all work is saved before killing an 
            application.
            """)) % (self.device,self.device,self.device))

        self.connect(self.cancelbutton,SIGNAL("clicked()"),self.slotCancelButtonClicked)
        self.connect(self.killbutton,SIGNAL("clicked()"),self.slotKillButtonClicked)
        self.connect(self.killallbutton,SIGNAL("clicked()"),self.slotKillallButtonClicked)
        self.connect(self.refreshbutton,SIGNAL("clicked()"),self.refreshProcesslist)
        self.connect(self.processlist,SIGNAL("selectionChanged()"),self.slotSelectionChanged)
        self.connect(self.umountbutton,SIGNAL("clicked()"),self.slotUmountButtonClicked)

        # TODO: Make optionsbutton resize dialog if processframe is hidden, hide Optionsbutton until then.
        self.optionsbutton.hide()
        self.readPixmaps()
        self.warningimage.setPixmap(MainBarIcon("messagebox_warning"))

        # Delayed initialisation.
        QTimer.singleShot(0,self.isMounted)
        QTimer.singleShot(0,self.refreshProcesslist)

    def setApp(self,app):
        """ We need a reference to the (K|Q)Application for certain things, e.g. setting 
            the MouseCursor. """
        self.app = app

    def setLsof(self,path):
        """ Where's the lsof binary? """
        if os.path.isfile(path):
            self.lsof_bin = path
        else:
            print path, " is not a valid binary, keeping %s", self.lsof_bin

    def readPixmaps(self):
        self.pix = {
            "exec": UserIcon("exec"),
            "owner": UserIcon("user"),
            "pid": UserIcon("tux"),
            "file": UserIcon("file")}

    def refreshProcesslist(self):
        """ Read lsof output and add the processdescriptors to the listview. """
        kapp = self.app

        kapp.setOverrideCursor(QCursor(Qt.BusyCursor))

        self.processlist.clear()
        rc, output = SimpleCommandRunner().run([self.lsof_bin,'-FpcLn',self.device],True)
        procs = output.split()

        self.processes = []
        self.realprocesses = []
        for line in procs:
            line = str(line)
            type = line[0]
            info = line[1:]

            if type is "p":
                pid = info
                parentproc = FileProcess(self.processlist,pid,True)
                self.processes.append(parentproc)
                self.realprocesses.append(parentproc)
                parentproc.setPixmaps(self.pix)
                files = 0

            if type == "c":
                pname = info
                parentproc.setName(pname)

            if type == "L":
                powner = info
                parentproc.setOwner(powner)

            if type == "n":
                pfile = info
                childproc = FileProcess(parentproc,pid)
                self.processes.append(childproc)
                childproc.setPixmaps(self.pix)
                childproc.setFile(pfile)
                childproc.setOwner(powner)
                childproc.setName(pname)
                if files == 0:
                    parentproc.fillColumns()
                files += 1
                childproc.fillColumns()

        kapp.restoreOverrideCursor()

        # Enable / disable buttons which are (in)appropriate.
        self.killallbutton.setEnabled(len(self.realprocesses)!=0)
        self.killbutton.setEnabled(len(self.realprocesses)!=0)
        self.umountbutton.setEnabled(len(self.realprocesses)==0)
        if self.processlist.selectedItem() == None:
            self.killbutton.setEnabled(False)

    def isMounted(self):
        rc,output = SimpleCommandRunner().run(["/bin/mount"],False)
        mounts = []
        for line in output.split('\n'):
            try:
                mounts.append(line.split()[0])
            except IndexError:
                pass
        ismounted = self.device in mounts
        self.umountbutton.setEnabled(ismounted)
        return ismounted

    def slotCancelButtonClicked(self):
        self.done(1)

    def slotKillButtonClicked(self):
        try:
            self.processlist.selectedItem().sendSignal("KILL")
            self.refreshProcesslist()
        except AttributeError:
            print "No killable item selected."

    def slotKillallButtonClicked(self):
        for process in self.realprocesses:
            process.sendSignal("KILL")
        self.refreshProcesslist()

    def slotOptionsButtonCLicked(self):
        self.processhidden = not self.processhidden
        self.processframe.setHidden(self.processhidden)

    def slotSelectionChanged(self):
        """ Check if item is a process or a file, disable killbutton for children. """
        selected = self.processlist.selectedItem()
        if not selected.isparent:
            self.killbutton.setEnabled(False)
        else:
            self.killbutton.setEnabled(True)

    def slotUmountButtonClicked(self):
        SimpleCommandRunner
        rc, output = SimpleCommandRunner().run(['/bin/umount',self.device])
        if rc == 0:
            print "%s successfully unmounted." % self.device
            # Close dialog and return 0 - sucessfully umounted.
            self.done(0)
        else:
            print "Unmounting %s failed: %s" % (self.device,output[:-1])
        self.isMounted()

################################################################################################
if standalone:
    device = "/dev/hda1"
    print 'Device is ', device

    cmd_args = TDECmdLineArgs.init(sys.argv, "FUser",
        "A graphical frontend to fuser, but without using it :-)", "0.2")

    # ----------------------------------------------------------------------------
    # FIXME: All the arg-parsing stuff does not work yet since I don't understand TDECmdLineArgs.
    options = [("device <device>", "Device to umount")]
    TDECmdLineArgs.addCmdLineOptions(options)
    args = TDECmdLineArgs.parsedArgs()
    # print args.count()
    # ----------------------------------------------------------------------------

    kapp = TDEApplication()
    TDEGlobal.iconLoader().addAppDir("guidance")
    fuserapp = FUser(device)

    fuserapp.setApp(kapp)
    kapp.setMainWidget(fuserapp)
    fuserapp.show()
    kapp.exec_loop()