How to delete data in character device

I wrote some data in my character device in /dev/my_char. What to do to delete data without removing the device from the kernel ?.

The method that I follow to remove the contents is 1) rm /dev/my_charand 2) rmmod My_Char.

But using this method, I need to reinsert the module into the kernel and create the device in the dev folder, which is a lengthy process.

Using only rm /dev/my_chardoes not delete its contents.

I would like to know if there is another method besides this.

+4
source share
3 answers

, , kernel.ie "rmmod My_Char" "insmod My_Char". /dev/my _char, .

+1

ioctl reset .

  • ioctl .
  • file_operations. .unlocked_ioctl = ( )
  • ioctl, reset , , , .

script, . ( reload):

#!/bin/bash
if [ -d /device/my_device ]; then
    sudo rmmod my_device.ko
fi

VERBOSE=0
MESSAGES=0
VENDOR=
DEVICEID=

while (( $# > 0 ))
do
    arg="$1"
    shift

    case $arg in
    v=* | ve=* | ver=* | verb=* | verbo=* | verbos=* | verbose=*)
        VERBOSE=${arg#*=}
        ;;

    v | ve | ver | verb | verbo | verbos | verbose)
        VERBOSE=1
        ;;

    t  | tt | tty)
        MESSAGES=1
        ;;

    ven=* | vend=* | vendo=* | vendor=*)
        VENDOR="opt_vendor_id=${arg#*=}"
        ;;

    ven | vend | vendo | vendor)
        VENDOR="opt_vendor_id=$1"
        shift
        ;;

    d=* | de=* | dev=* | devi=* | devic=* | device=*)
        DEVICEID="opt_device_id=${arg#*=}"
        ;;

    d | de | dev | devi | devic | device)
        DEVICEID="opt_device_id=$1"
        shift
        ;;

    *)
        echo "Invalid option '$arg':"
        echo "Options are 'verbose', 'tty', 'vendor='<vendor number>, and 'deviceid='<device id>"
        exit 1
        ;;
    esac
done

echo "insmod my_device.ko opt_verbose=$VERBOSE opt_tty_msgs=$MESSAGES $VENDOR $DEVICEID"
sudo insmod my_device.ko opt_verbose=$VERBOSE opt_tty_msgs=$MESSAGES $VENDOR $DEVICEID

, . , , :

#!/bin/bash
if [ -d /device/my_device ]; then
    sudo rmmod my_device.ko
fi

sudo insmod my_device.ko
+2

You can work with your character device as if it were a shared file

cat /dev/null > /dev/my_char
+1
source

Source: https://habr.com/ru/post/1531707/


All Articles