File: devices

package info (click to toggle)
hibernate 2.0%2B15%2Bg88d54a8-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 740 kB
  • ctags: 114
  • sloc: sh: 1,223; makefile: 17
file content (62 lines) | stat: -rw-r--r-- 1,749 bytes parent folder | download
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
# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler DevicesOptions
AddConfigHelp "IncompatibleDevices <device name> [...]" "If there are any processes accessing these devices, then suspending is aborted. If the --kill option is passed, the offending processes are terminated and the suspend continues. For example programs accessing the sound card (/dev/dsp*) or tuner cards (/dev/video*) would deny the respective modules from being unloaded."

DevicesFree() {
    local needsleep # always! ;)
    local device
    local ret

    [ -z "$INCOMPATIBLE_DEVICES" ] && return 0

    needsleep=0

    # Send TERM to processes first
    if [ x"$KILL_PROGRAMS" = "x1" ] ; then
	for device in $INCOMPATIBLE_DEVICES ; do
	    if fuser -s $device ; then
		vecho 1 "Sending SIGTERM to processes using $device..."
		fuser -s -k -15 $device 2>/dev/null
		fuser -s $device && needsleep=1
	    fi
	done
    fi

    [ "$needsleep" -eq 1 ] && sleep 1

    # Send KILL to processes
    ret=0
    for device in $INCOMPATIBLE_DEVICES ; do
	if fuser -s $device ; then
	    if [ x"$KILL_PROGRAMS" = "x1" ] ; then
		vecho 1 "Sending SIGKILL to processes using $device..."
		fuser -s -k -9 $device 2>/dev/null
	    else
		vecho 1 "Device $device is still in use by the following processes: `fuser $device 2>/dev/null`"
		ret=1
	    fi
	fi
    done

    # We tried our best.
    return $ret
}

DevicesOptions() {
    case $1 in
	incompatibledevices)
	    # we have to kill before are services stopped, modules unloaded.
	    # but should it run before programs, I suggest yes!
	    [ -z "$INCOMPATIBLE_DEVICES" ] && AddSuspendHook 19 DevicesFree
	    shift
	    INCOMPATIBLE_DEVICES="$INCOMPATIBLE_DEVICES $@"
	    ;;
	*)
	    return 1
    esac
    return 0
}

# $Id$