This file is indexed.

/usr/share/tlp-pm/tlp-rf-func is in tlp 0.9-3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/bin/sh
# tlp - rf switch functions
#
# Copyright (c) 2016 Thomas Koch <linrunner at gmx.net>
# This software is licensed under the GPL v2 or later.

# ----------------------------------------------------------------------------
# Constants

readonly RFKILL="rfkill"
readonly RFKD="/dev/rfkill"

readonly ALLDEV="bluetooth wifi wwan"

# ----------------------------------------------------------------------------
# Functions

get_devc () { # get control device for radio type
              # $1: rftype bluetooth/wifi/wwan
              # retval $devc: sysdev,
              #     $rfkdev: 1/0=is/is not an rfkill device,
              #     $devon, $devoff: value to write directly to the sysdev
              #     to achieve the desired switch state

    local i

    check_sysfs "get_devc" "/sys/class/rfkill"

    # preset retvals
    devc=""
    devs=254
    rfkdev="1"
    devon="1"
    devoff="0"

    case $1 in
        wwan|bluetooth)
            for i in /sys/class/rfkill/rfkill* ; do
                if [ -f $i/type ] && [ "$(cat $i/type)" = "$1" ]; then
                    devc="$i/state"
                    echo_debug "rf" "get_devc($1) = $devc"
                    return 0
                fi
            done
            ;;

        wifi)
            for i in /sys/bus/pci/drivers/ipw2?00/*/rf_kill; do
                if [ -f $i ]; then
                    devc="$i"
                    rfkdev="0"
                    devon="0"
                    devoff="1"
                    echo_debug "rf" "get_devc($1) = $devc"
                    return 0
                fi
            done

            for i in /sys/class/rfkill/rfkill* ; do
                if [ -f $i/type ] && [ "$(cat $i/type)" = "wlan" ]; then
                    devc="$i/state"
                    echo_debug "rf" "get_devc($1) = $devc"
                    return 0
                fi
            done
            ;;

        *)
            echo "Error: unknown device type \"$1\"" 1>&2
            echo_debug "rf" "get_devc($1).unknown_type"
            return 0
            ;;
    esac

    echo_debug "rf" "get_devc($1).not_present"

    return 0
}

get_devs () { # get radio device state -- $1: rftype; retval $devs: 0=off/1=on
    if [ -n "$devc" ]; then
        devs=$(cat $devc)
        [ "$rfkdev" = "0" ] && devs=$(($devs ^ $devoff))
    fi

    echo_debug "rf" "get_devs($1) = $devs"

    return 0
}

err_no_root_priv () { # check root privilege
    echo "Error: missing root privilege." 1>&2
    echo_debug "rf" "$1.missing_root_privilege"

    return 0
}

test_rfkill_perms () { # test if either root priv or rfkill device writable
    test_root || [ -w $RFKD ]
}

check_nm () { # test if NetworkManager is running and nmcli is installed

    cmd_exists $NMCLI
}

invoke_nmcli () { # call nmcli with radio option according to the program version
                  # $1: rftype, $2: on/off, $3: caller; rc: last nmcli rc
    local rc

    check_nm || return 0 # return if NetworkManager not running

    $NMCLI nm $1 $2 > /dev/null 2>&1; rc=$?
    echo_debug "rf" "invoke_nmcli($1, $2).nm: rc=$rc"
    if [ $rc -eq 2 ]; then
        # option "nm" is invalid for this NM version, try "radio" instead
        $NMCLI radio $1 $2 > /dev/null 2>&1; rc=$?
        echo_debug "rf" "invoke_nmcli($1, $2).radio: rc=$rc"
    fi

    return $rc
}

device_state () { # get radio type state -- $1: rftype; retval $devc, $devs: 0=off/1=on
    echo_debug "rf" "device_state($1)"

    get_devc $1
    get_devs $1
}

device_switch () { # switch radio type state
                   # $1: rftype, $2: 1/on/0/off/toggle
                   # $3: lock id, $4: lock duration
                   # rc: 0=switched/1=invalid device or operation/
                   #     2=hard blocked/4=no change
                   # retval $devc, $devs: 0=off/1=on

    local curst newst devn

    echo_debug "rf" "device_switch($1, $2, $3, $4)"

    get_devc $1

    # quit if no device
    if [ -z "$devc" ]; then
        echo_debug "rf" "device_switch($1, $2).no_device: rc=1"
        return 1
    fi

    # quit if invalid operation
    if ! wordinlist $2 "on 1 off 0 toggle"; then
        echo_debug "rf" "device_switch($1, $2).invalid_op: rc=1"
        return 1
    fi

    # get current device state
    get_devs $1
    curst=$devs

    # quit if device is hard blocked
    if [ "$curst" = "2" ]; then
        echo_debug "rf" "device_switch($1, $2).hard_blocked: rc=2"
        return 2
    fi

    # determine desired device state
    case $2 in
        1|on)     newst=1 ;;
        0|off)    newst=0 ;;
        toggle) newst=$(($curst ^ 1)) ;;
    esac

    # wifi, wwan: before rfkill -- notify NM of desired state
    if [ "$X_USE_NMCLI" = "1" ] && [ "$1" != "bluetooth" ]; then
        case $newst in
            1) invoke_nmcli $1 on  ;;
            0) invoke_nmcli $1 off ;;
        esac
        # record device state after nmcli
        get_devs $1
        # update current state
        curst=$devs
    fi

    # compare current and desired device state
    if [ "$curst" = "$newst" ]; then
        # desired matches current state --> do nothing
        echo_debug "rf" "device_switch($1, $2).desired_state"

    else
        # desired does not match current state --> do switch

        # set timed lock if required
        [ -n "$3" ] && [ -n "$4" ] && [ "$1" != "bluetooth" ] && \
            set_timed_lock $3 $4

        # determine value for direct write
        case $newst in
            1) devn=$devon  ;;
            0) devn=$devoff ;;
        esac

        # switch device state when either rfkill isn't disabled
        # or it's a bluetooth device
        if [ "$X_USE_RFKILL" != "0" ] || [ "$1" != "bluetooth" ]; then
            if [ "$rfkdev" = "1" ] && cmd_exists $RFKILL ; then
                if test_rfkill_perms ; then
                    # use rfkill
                    echo_debug "rf" "device_switch($1, $2).rfkill"
                    case $newst in
                        1) $RFKILL unblock $1 > /dev/null 2>&1 ;;
                        0) $RFKILL block $1   > /dev/null 2>&1 ;;
                        *) ;;
                    esac
                    # record device state after rfkill
                    get_devs $1
                else
                    # missing permission to rfkill
                    err_no_root_priv "device_switch($1, $2).rfkill"
                fi
            else
                # use direct write
                if test_root ; then
                    echo_debug "rf" "device_switch($1, $2).devc"
                    echo -n $devn > $devc
                    # record device state after direct write
                    get_devs $1
                else
                    err_no_root_priv "device_switch($1, $2).devc"
                fi
            fi
        fi # rfkill not disabled or bluetooth
    fi # states did not match

    # wifi, wwan: after rfill -- notify NM of desired state
    if [ "$X_USE_NMCLI" != "0" ] && [ "$X_USE_NMCLI" != "1" ] \
       && [ "$1" != "bluetooth" ]; then
        case $newst in
            1) invoke_nmcli $1 on  ;;
            0) invoke_nmcli $1 off ;;
        esac
        # record final device state
        get_devs $1
    fi

    # compare old and new device state
    if [ "$curst" = "$devs" ]; then
        # state did not change
        echo_debug "rf" "device_switch($1, $2).no_change: rc=4"
        return 4
    else
        echo_debug "rf" "device_switch($1, $2).ok: rc=0"
        return 0
    fi
}

echo_device_state () { # print radio type state -- $1: rftype, $2: state
    case $1 in
        bluetooth)
            devstr="bluetooth"
            ;;

        wifi)
            devstr="wifi     "
            ;;

        wwan)
            devstr="wwan     "
            ;;

        *)
            devstr=$1
            ;;
    esac

    case $2 in
        0)
            echo "$devstr = off (software)"
            ;;

        1)
            echo "$devstr = on"
            ;;

        2)
            echo "$devstr = off (hardware)"
            ;;

        254)
            echo "$devstr = none (no device)"
            ;;

        *)
            echo "$devstr = unknown state \"$2\""
    esac

    return 0
}

save_device_states () { # save radio states  -- $1: list of rftypes
    local dev
    local devlist="${1:-$ALLDEV}" # when arg empty -> use all

    echo_debug "rf" "save_device_states($devlist)"

    # create empty state file
    mkdir -p $STATEDIR 2> /dev/null 1>&2
    : > $RFSTATEFILE

    # iterate over all possible devices -> save state in file
    for dev in $devlist; do
        device_state $dev
        echo "$dev $devs" >> $RFSTATEFILE
    done

    return 0
}

restore_device_states () { # restore radio type states
    local sline

    echo_debug "rf" "restore_device_states"

    if [ -f $RFSTATEFILE ]; then
        # read state file
        while read sline; do
            set -- $sline # read dev, state into $1, $2
            device_switch $1 $2
        done < $RFSTATEFILE

        return 0
    else
        return 1
    fi
}

set_radio_device_states () { # set/initialize all radio states
    # $1: start/stop/1/0/radiosw
    # called from init scripts or upon change of power source
    local dev devs2disable devs2enable restore
    local quiet=0

    # save/restore mode is disabled by default
    if [ "$1" != "radiosw" ]; then
        restore=${RESTORE_DEVICE_STATE_ON_STARTUP:-0}
    else
        restore=0
    fi

    if [ "$restore" = "1" ]; then
        # "save/restore" mode
        echo_debug "rf" "set_radio_device_states($1).restore"
        case $1 in
            start)
                restore_device_states
                if [ $? = 0 ]; then
                    echo "Radio device states restored."
                else
                    echo "No saved radio device states found."
                fi
                ;;

            stop)
                save_device_states
                echo "Radio device states saved."
                ;;
        esac
    else
        # "disable/enable on startup/shutdown or bat/ac" or "radiosw" mode
        case $1 in
            start) # system startup
                devs2disable="$DEVICES_TO_DISABLE_ON_STARTUP"
                devs2enable="$DEVICES_TO_ENABLE_ON_STARTUP"
                ;;

            stop) # system shutdown
                devs2disable="$DEVICES_TO_DISABLE_ON_SHUTDOWN"
                devs2enable="$DEVICES_TO_ENABLE_ON_SHUTDOWN"

                # NM workaround: if
                # 1. disable wifi is configured somehow, and
                # 2. wifi is not explicitly configured for shutdown
                # then re-enable wifi on shutdown to prepare for startup
                if wordinlist "wifi" "$DEVICES_TO_DISABLE_ON_BAT
                                      $DEVICES_TO_DISABLE_ON_BAT_NOT_IN_USE
                                      $DEVICES_TO_DISABLE_ON_LAN_CONNECT
                                      $DEVICES_TO_DISABLE_ON_WIFI_CONNECT
                                      $DEVICES_TO_DISABLE_ON_WWAN_CONNECT" && \
                   ! wordinlist "wifi" "$devs2disable $devs2enable"; then
                    devs2enable="wifi $devs2enable"
                fi
                ;;

            1) # battery power --> build disable list
                quiet=1 # do not display progress
                devs2enable=""
                devs2disable="${DEVICES_TO_DISABLE_ON_BAT:-}"

                # check configured list for connected devices
                for dev in ${DEVICES_TO_DISABLE_ON_BAT_NOT_IN_USE:-}; do
                    case $dev in
                        bluetooth) any_bluetooth_in_use ;;
                        wifi) any_wifi_in_use ;;
                        wwan) any_wwan_in_use ;;
                    esac
                    # if device is not connected and not in list yet --> add to disable list
                    [ $? = 0 ] || wordinlist $dev "$devs2disable" || devs2disable="$dev $devs2disable"
                done
                devs2disable="${devs2disable# }"
                ;;

            0) # AC power --> build enable list
                quiet=1 # do not display progress
                devs2enable="${DEVICES_TO_ENABLE_ON_AC:-}"
                devs2disable=""
                ;;

            radiosw)
                devs2disable=""
                devs2enable="$DEVICES_TO_ENABLE_ON_RADIOSW"
                ;;
        esac

        echo_debug "rf" "set_radio_device_states($1): enable=$devs2enable disable=$devs2disable"

        # Disable configured radios
        if [ -n "$devs2disable" ]; then
            [ "$quiet" = "1" ] || echo -n "Disabling radios:"
            for dev in $devs2disable; do
                [ "$quiet" = "1" ] || echo -n " $dev"
                device_switch $dev off
            done
            [ "$quiet" = "1" ] || echo "."
        fi

        # Enable configured radios
        if [ -n "$devs2enable" ]; then
            if [ "$1" = "radiosw" ]; then
                # radiosw mode: disable radios not listed
                for dev in bluetooth wifi wwan; do
                    if ! wordinlist "$dev" "$devs2enable"; then
                        device_switch $dev off
                    fi
                done
            else
                # start mode: enable listed radios
                [ "$quiet" = "1" ] || echo -n "Enabling radios:"
                for dev in $devs2enable; do
                    [ "$quiet" = "1" ] || echo -n " $dev"
                    device_switch $dev on
                done
                [ "$quiet" = "1" ] || echo "."
            fi
        fi

        # clean up: discard state file
        rm -f $RFSTATEFILE 2> /dev/null
    fi

    return 0
}