summaryrefslogtreecommitdiff
path: root/ioslay-mgr.sh
blob: 1b8cf73842035af2d5dced17184437b23e755dd2 (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
#!/bin/bash

ioslay=
each_new_process()
{
    while read pid _
    do
        read -N 10001000 procs < /sys/fs/cgroup/$group/cgroup.procs
        set -- $procs

        for pid in "$@"
        do
            if read comm < /proc/$pid/comm && [ "$comm" = 'Web Content' ]
            then
                set -- "$@" "$1"
            fi
            shift
        done

        if [ "$ioslay" ]
        then
            children=$(pgrep -P $ioslay)
            if [ "$children" ]
            then
                kill $children
            fi
            wait $ioslay
        fi

        if [ $# -gt 0 ]
        then
            ioslay-firefox "$@" &
            ioslay=$!
        fi
    done
}

get_uid()
{
    (
        if [ "$SUDO_USER" ]
        then
            IFS=:
            set -- $(getent passwd "${SUDO_USER}") || return
            echo $3
        else
            id -un
        fi
    )
}

#cgroup-show-each-new-process "$group" | grep --line-buffered 'Web Content' | each_new_process

# Really, could just check for any/first cgroup with processes and basename
# 'firefox'.  Fork for each one.

vkill()
{
    if [ $# = 0 ]
    then
        return
    fi
    (
        set -x
        ps u "$@"
        sudo kill "$@"
    ) 
}

uid=1000
group=user.slice/user-$uid.slice/firefox
wantcomm='Web Content'
ioslay=
lastprocs=
trap 'RECEIVED_SIGINT=y' SIGINT
while [ -z "$RECEIVED_SIGINT" ]
do
    read -N 1000100 procs < /sys/fs/cgroup/$group/cgroup.procs
    if [ "$procs" ]
    then
        set --
        for pid in $procs
        do
            read comm < /proc/$pid/comm
            if [ "$comm" = "$wantcomm" ]
            then
                set -- "$@" "$pid"
            fi
        done

        # echo "pids: ($*|$(echo $procs))" >&2
        if [ "$lastargs" != "$*" ]
        then
            [ -z "$ioslay" ] || vkill $ioslay $(pgrep -P $ioslay)
            wait $ioslay
            sudo ioslay-firefox "$@" &
            ioslay=$!
        fi
        lastargs=$*
    fi
    sleep 1
done