HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / samples / pktgen / functions.sh
blob065a7e296ee3142bdb344faf69a852fe140d8e5f
2 # Common functions used by pktgen scripts
3 # - Depending on bash 3 (or higher) syntax
5 # Author: Jesper Dangaaard Brouer
6 # License: GPL
8 set -o errexit
10 ## -- General shell logging cmds --
11 function err() {
12 local exitcode=$1
13 shift
14 echo "ERROR: $@" >&2
15 exit $exitcode
18 function warn() {
19 echo "WARN : $@" >&2
22 function info() {
23 if [[ -n "$VERBOSE" ]]; then
24 echo "INFO : $@" >&2
28 ## -- Pktgen proc config commands -- ##
29 export PROC_DIR=/proc/net/pktgen
31 # Three different shell functions for configuring the different
32 # components of pktgen:
33 # pg_ctrl(), pg_thread() and pg_set().
35 # These functions correspond to pktgens different components.
36 # * pg_ctrl() control "pgctrl" (/proc/net/pktgen/pgctrl)
37 # * pg_thread() control the kernel threads and binding to devices
38 # * pg_set() control setup of individual devices
39 function pg_ctrl() {
40 local proc_file="pgctrl"
41 proc_cmd ${proc_file} "$@"
44 function pg_thread() {
45 local thread=$1
46 local proc_file="kpktgend_${thread}"
47 shift
48 proc_cmd ${proc_file} "$@"
51 function pg_set() {
52 local dev=$1
53 local proc_file="$dev"
54 shift
55 proc_cmd ${proc_file} "$@"
58 # More generic replacement for pgset(), that does not depend on global
59 # variable for proc file.
60 function proc_cmd() {
61 local result
62 local proc_file=$1
63 local status=0
64 # after shift, the remaining args are contained in $@
65 shift
66 local proc_ctrl=${PROC_DIR}/$proc_file
67 if [[ ! -e "$proc_ctrl" ]]; then
68 err 3 "proc file:$proc_ctrl does not exists (dev added to thread?)"
69 else
70 if [[ ! -w "$proc_ctrl" ]]; then
71 err 4 "proc file:$proc_ctrl not writable, not root?!"
75 if [[ "$DEBUG" == "yes" ]]; then
76 echo "cmd: $@ > $proc_ctrl"
78 # Quoting of "$@" is important for space expansion
79 echo "$@" > "$proc_ctrl" || status=$?
81 if [[ "$proc_file" != "pgctrl" ]]; then
82 result=$(grep "Result: OK:" $proc_ctrl) || true
83 if [[ "$result" == "" ]]; then
84 grep "Result:" $proc_ctrl >&2
87 if (( $status != 0 )); then
88 err 5 "Write error($status) occurred cmd: \"$@ > $proc_ctrl\""
92 # Old obsolete "pgset" function, with slightly improved err handling
93 function pgset() {
94 local result
96 if [[ "$DEBUG" == "yes" ]]; then
97 echo "cmd: $1 > $PGDEV"
99 echo $1 > $PGDEV
100 local status=$?
102 result=`cat $PGDEV | fgrep "Result: OK:"`
103 if [[ "$result" == "" ]]; then
104 cat $PGDEV | fgrep Result:
106 if (( $status != 0 )); then
107 err 5 "Write error($status) occurred cmd: \"$1 > $PGDEV\""
111 [[ $EUID -eq 0 ]] && trap 'pg_ctrl "reset"' EXIT
113 ## -- General shell tricks --
115 function root_check_run_with_sudo() {
116 # Trick so, program can be run as normal user, will just use "sudo"
117 # call as root_check_run_as_sudo "$@"
118 if [ "$EUID" -ne 0 ]; then
119 if [ -x $0 ]; then # Directly executable use sudo
120 info "Not root, running with sudo"
121 sudo "$0" "$@"
122 exit $?
124 err 4 "cannot perform sudo run of $0"