Ali Hatami Tajik
5 months ago
11 changed files with 208 additions and 0 deletions
@ -0,0 +1,5 @@ |
|||
# Rule for DVD disk insertion |
|||
#ACTION=="change", SUBSYSTEM=="block", KERNEL=="sr[0-9]*", ENV{ID_CDROM_MEDIA}=="1", RUN+="/usr/bin/sudo /usr/local/bin/dvd-storage-action add %E{DEVNAME}" |
|||
# Rule for DVD disk removal |
|||
#ACTION=="change", SUBSYSTEM=="block", KERNEL=="sr[0-9]*", ENV{ID_CDROM_MEDIA}!="1", RUN+="/usr/bin/sudo /usr/local/bin/dvd-storage-action remove %E{DEVNAME}" |
|||
ACTION=="change", SUBSYSTEM=="block", KERNEL=="sr[0-9]*", ENV{ID_CDROM}=="1", RUN+="/usr/bin/sudo /usr/local/bin/dvd-storage-action %E{ACTION} %E{DEVNAME}" |
@ -0,0 +1,4 @@ |
|||
# Rule for USB device connection |
|||
ACTION=="add", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", ENV{ID_FS_USAGE}=="filesystem", RUN+="/usr/bin/sudo /usr/local/bin/usb-storage-action add %E{DEVNAME}" |
|||
# Rule for USB device disconnection |
|||
ACTION=="remove", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", ENV{ID_FS_USAGE}=="filesystem", RUN+="/usr/bin/sudo /usr/local/bin/usb-storage-action remove %E{DEVNAME}" |
@ -0,0 +1,3 @@ |
|||
#!/bin/bash |
|||
# Your script logic here |
|||
echo "Connection received on Unix socket" >> /var/log/exampleDvd.log |
@ -0,0 +1,10 @@ |
|||
[Unit] |
|||
Description=Example Service for Unix Socket |
|||
After=network.target |
|||
|
|||
[Service] |
|||
ExecStart=/usr/local/bin/addDvdScript.sh |
|||
StandardInput=socket |
|||
|
|||
[Install] |
|||
WantedBy=multi-user.target |
@ -0,0 +1,9 @@ |
|||
[Unit] |
|||
Description=Example Unix Socket |
|||
|
|||
[Socket] |
|||
ListenStream=/tmp/dvd-Sono-Socket.socket |
|||
Accept=false |
|||
|
|||
[Install] |
|||
WantedBy=sockets.target |
@ -0,0 +1,93 @@ |
|||
#!/bin/bash |
|||
|
|||
ACTION=$1 |
|||
DEVNAME=$2 |
|||
|
|||
SOCKET_PATH="/tmp/usb-Sono-Socket.socket" |
|||
LOG_FILE="/tmp/tmpDvd.log" |
|||
DEBOUNCE_FILE="/tmp/dvd_debounce_$(echo $DEVNAME | tr '/' '-')" |
|||
DEBOUNCE_TIME=10 # seconds |
|||
|
|||
current_time=$(date +%s) |
|||
|
|||
# Function to log messages |
|||
log_message() { |
|||
local message=$1 |
|||
echo $message | socat - UNIX-CONNECT:$SOCKET_PATH |
|||
echo $message >> $LOG_FILE |
|||
} |
|||
|
|||
# Function to get the size of the DVD |
|||
get_dvd_size() { |
|||
udisksctl info -b $DEVNAME | grep 'Size:' | awk '{print $2}' | head -n 1 |
|||
} |
|||
|
|||
# Function to check for debounce |
|||
check_debounce() { |
|||
SIZE=$(get_dvd_size) |
|||
if [SIZE -eq 0]; then |
|||
ATTEMPTS=3 |
|||
while [ "$SIZE" -eq 0 ] && [ $ATTEMPTS -gt 0 ]; do |
|||
sleep 2 |
|||
SIZE=$(get_dvd_size) |
|||
log_message "Debounced: Retried size of $DEVNAME is $SIZE" |
|||
ATTEMPTS=$((ATTEMPTS - 1)) |
|||
done |
|||
elif [SIZE -eq 0]; then |
|||
log_message "exit on: Retried size of $DEVNAME is $SIZE" |
|||
exit 0 |
|||
fi |
|||
|
|||
if [ -f "$DEBOUNCE_FILE" ]; then |
|||
last_run_time=$(cat $DEBOUNCE_FILE) |
|||
elapsed_time=$((current_time - last_run_time)) |
|||
if [ $elapsed_time -lt $DEBOUNCE_TIME ]; then |
|||
log_message "Debounced: $ACTION $DEVNAME" |
|||
exit 0 |
|||
fi |
|||
fi |
|||
echo $current_time > $DEBOUNCE_FILE |
|||
} |
|||
|
|||
check_debounce |
|||
|
|||
|
|||
|
|||
if [ "$ACTION" == "change" ]; then |
|||
# Introduce a delay before checking the device |
|||
sleep 2 # Wait for 2 seconds before proceeding |
|||
|
|||
# Check if the device is already mounted |
|||
MOUNTED=$(lsblk -o MOUNTPOINT -nr $DEVNAME) |
|||
if [ -z "$MOUNTED" ]; then |
|||
# Device is not mounted, check if it is a raw DVD |
|||
SIZE=$(get_dvd_size) |
|||
|
|||
# Debug output for size |
|||
log_message "DEBUG: Initial size of $DEVNAME is $SIZE" |
|||
|
|||
# Retry logic for getting the size |
|||
ATTEMPTS=3 |
|||
while [ "$SIZE" -eq 0 ] && [ $ATTEMPTS -gt 0 ]; do |
|||
sleep 2 |
|||
SIZE=$(get_dvd_size) |
|||
log_message "DEBUG: Retried size of $DEVNAME is $SIZE" |
|||
ATTEMPTS=$((ATTEMPTS - 1)) |
|||
done |
|||
|
|||
if [ "$SIZE" -gt 0 ]; then |
|||
log_message "success insert raw DVD $DEVNAME" |
|||
else |
|||
# Proceed to mount the device |
|||
udisksctl mount -b $DEVNAME |
|||
if [ $? -eq 0 ]; then |
|||
log_message "success mount $DEVNAME" |
|||
else |
|||
log_message "failed to mount $DEVNAME" |
|||
fi |
|||
fi |
|||
else |
|||
log_message "$DEVNAME is already mounted at $MOUNTED" |
|||
fi |
|||
log_message "success insert $DEVNAME" |
|||
fi |
@ -0,0 +1,3 @@ |
|||
#!/bin/bash |
|||
# Your script logic here |
|||
echo "Connection received on Unix socket" >> /var/log/example.log |
@ -0,0 +1,10 @@ |
|||
[Unit] |
|||
Description=Example Service for Unix Socket |
|||
After=network.target |
|||
|
|||
[Service] |
|||
ExecStart=/usr/local/bin/addUsbScript.sh |
|||
StandardInput=socket |
|||
|
|||
[Install] |
|||
WantedBy=multi-user.target |
@ -0,0 +1,9 @@ |
|||
[Unit] |
|||
Description=Example Unix Socket |
|||
|
|||
[Socket] |
|||
ListenStream=/tmp/usb-Sono-Socket.socket |
|||
Accept=false |
|||
|
|||
[Install] |
|||
WantedBy=sockets.target |
@ -0,0 +1,37 @@ |
|||
#!/bin/bash |
|||
|
|||
# Get the action (add or remove) and the device name from the udev rule |
|||
ACTION=$1 |
|||
|
|||
# Get the device name from the udev rule |
|||
DEVICE_NAME=$2 |
|||
|
|||
SOCKET_PATH="/tmp/usb-Sono-Socket.socket" |
|||
|
|||
if [ "$ACTION" == "add" ]; then |
|||
# Mount the device |
|||
udisksctl mount -b "$DEVICE_NAME" |
|||
|
|||
# Check if the mount was successful |
|||
if [ $? -eq 0 ]; then |
|||
# Write a message to the socket |
|||
echo "success connect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH |
|||
echo "success connect $DEVICE_NAME" >> /tmp/tmp.log |
|||
else |
|||
echo "failure connect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH |
|||
echo "failure connect $DEVICE_NAME" >> /tmp/tmp.log |
|||
fi |
|||
elif [ "$ACTION" == "remove" ]; then |
|||
# Unmount the device |
|||
udisksctl unmount -b "$DEVICE_NAME" |
|||
|
|||
# Check if the unmount was successful |
|||
if [ $? -eq 0 ]; then |
|||
# Write a message to the socket |
|||
echo "success disconnect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH |
|||
echo "success disconnect $DEVICE_NAME" >> /tmp/tmp.log |
|||
else |
|||
echo "failure disconnect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH |
|||
echo "failure disconnect $DEVICE_NAME" >> /tmp/tmp.log |
|||
fi |
|||
fi |
Loading…
Reference in new issue