You are not logged in.

#1 2015-04-14 15:36:32

woodape
Member
Registered: 2015-03-25
Posts: 159

[WORKED-AROUND] Can't write to Samsung specific file without sudo

I have a Samsung Series 9 laptop, which I love, but it means that the function keys don't really work out of the box. No matter, I use DWM and bind the keycodes directly to a script which makes the necessary calls to do all the nifty stuff that the laptop can do. Here's the script:

#!/bin/bash

##Script for function keys of Samsung Series 9 ultrabook
## Fn-F5 already works out the box, no need to map.

case $1 in 
	F1)	#To be set up to configure some kind of setting

	;;
	F2)	#Increment down the backlight by 10%
		xbacklight -inc -10
	;;
	F3)	#Increment up the backlight by 10%
		xbacklight -inc +10
	;;
	F4)	#Detect HDMI and (dis)connect
		if  xrandr | grep 'HDMI1 connected' ; then
			xrandr --output HDMI1 --mode 1280x720 --pos 1600x0 --rotate normal --output VIRTUAL1 --off --output DP1 --off --output eDP1 --mode 1600x900 --pos 0x0 --rotate normal --output VGA1 --off
		else 
			xrandr --output HDMI1 --off --output VIRTUAL1 --off --output DP1 --off --output eDP1 --mode 1600x900 --pos 0x0 --rotate normal --output VGA1 --off
		fi
	;;
	F6)	#(Un)Mute volume
		amixer set Master toggle
	;;
	F7)	#Increment volume down 5%
		amixer set Master 5%-
	;;
	F8)	#Increment volume up 5%
		amixer set Master 5%+
	;;
	F9)	#Increment Keybacklight down 1 notch
		status=$(grep "" /sys/devices/platform/samsung/leds/samsung::kbd_backlight/brightness)
		if [ $status -gt 0 ] ; then
			echo $(( status - 1 )) > /sys/devices/platform/samsung/leds/samsung::kbd_backlight/brightness 
		fi
	;;
	F10)	#Increment Keybacklight up 1 notch
		status=$(grep "" /sys/devices/platform/samsung/leds/samsung::kbd_backlight/brightness)
		if [ $status -lt 8 ] ; then
			echo $(( status + 1 )) > /sys/devices/platform/samsung/leds/samsung::kbd_backlight/brightness 
		fi
	;;
	F11)	#Toggle Silent/Normal performance mode
		status=$(grep "" /sys/devices/platform/samsung/performance_level)
		if [ "$status" == "normal" ] ; then
			echo "silent" > /sys/devices/platform/samsung/performance_level
		else
			echo "normal" > /sys/devices/platform/samsung/performance_level
		fi
	;;
	F12)	#Toggle soft block of Wireless
		status=$(grep "" /sys/devices/platform/samsung/rfkill/rfkill0/soft)
		if [ $status -eq 1 ] ; then
			echo 0 > /sys/devices/platform/samsung/rfkill/rfkill0/soft
		else
			echo 1 > /sys/devices/platform/samsung/rfkill/rfkill0/soft
		fi
	;; esac

exit 0

In this script there are 3 Samsung platform specific files which need to be modified to get functionality for the keyboard backlight, performance mode, and the soft wifi kill switch:

/sys/devices/platform/samsung/leds/samsung::kbd_backlight/brightness 
/sys/devices/platform/samsung/performance_level
/sys/devices/platform/samsung/rfkill/rfkill0/soft

They all initially are owned by root and in the root group, which creates an issue since the script is bound to keys and I don't want to have change the srcipt to 'NOPASSWD' in sudoers - that seems like it opens an unnessary possibility of someone(me) messing something up. Instead, I created my first systemd service file to change the permissions on boot:

[Unit]
Description=Change Ownership of /sys/devices/platform/samsung to all for fn keys

[Service]
Type=oneshot
ExecStart=/sbin/sudo /sbin/chown woodape /sys/devices/platform/samsung/leds/samsung::kbd_backlight/brightness
ExecStart=/sbin/sudo /sbin/chgrp woodape /sys/devices/platform/samsung/rfkill/rfkill0/soft
ExecStart=/sbin/sudo /sbin/chown woodape /sys/devices/platform/samsung/rfkill/rfkill0/soft
ExecStart=/sbin/sudo /sbin/chown woodape /sys/devices/platform/samsung/performance_level

RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

This script duly changes the ownership of the respective files and the group in the case of rfkill0/soft without issue. The problem is that although I can now echo the necessary parameters to the backlight and performance_level files without sudo, I can't do the same for the rfkill0/soft file:

woodape[rfkill0]$ ll
total 0
drwxr-xr-x 3 root    root       0 Apr 14 17:26 .
drwxr-xr-x 4 root    root       0 Apr 14 17:26 ..
-r--r--r-- 1 root    root    4096 Apr 14 17:26 claim
lrwxrwxrwx 1 root    root       0 Apr 14 17:26 device -> ../../../samsung
-r--r--r-- 1 root    root    4096 Apr 14 17:26 hard
-r--r--r-- 1 root    root    4096 Apr 14 17:26 index
-r--r--r-- 1 root    root    4096 Apr 14 17:26 name
-r--r--r-- 1 root    root    4096 Apr 14 17:26 persistent
drwxr-xr-x 2 root    root       0 Apr 14 17:26 power
-rw-r--r-- 1 woodape woodape 4096 Apr 14 17:27 soft
-rw-r--r-- 1 root    root    4096 Apr 14 17:26 state
lrwxrwxrwx 1 root    root       0 Apr 14 17:26 subsystem -> ../../../../../class/rfkill
-r--r--r-- 1 root    root    4096 Apr 14 17:26 type
-rw-r--r-- 1 root    root    4096 Apr 14 17:26 uevent
woodape[rfkill0]$ cat soft
0
woodape[rfkill0]$ echo 1 > soft
bash: echo: write error: Operation not permitted
woodape[rfkill0]$ sudo echo 1 > soft
woodape[rfkill0]$ cat soft
1

Long story short, even though the user owns the file, the file belongs to the username group and the file has write permissions, I can't write to the file without sudo. Does anyone have any idea why this is and how I can correct this?

Last edited by woodape (2015-04-14 16:48:55)

Offline

#2 2015-04-14 15:52:14

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 7,748
Website

Re: [WORKED-AROUND] Can't write to Samsung specific file without sudo

You could just use the `rfkill` command instead and set that command as "NOPASSWD" in /etc/sudoers

Offline

#3 2015-04-14 16:47:59

woodape
Member
Registered: 2015-03-25
Posts: 159

Re: [WORKED-AROUND] Can't write to Samsung specific file without sudo

Thanks HOS for the workaround, that seems like a simple enough solution. Any idea why I can't simply echo some text to a file I own?

Offline

#4 2015-04-14 17:06:52

Awebb
Member
Registered: 2010-05-06
Posts: 6,309

Re: [WORKED-AROUND] Can't write to Samsung specific file without sudo

While I have no real answer to your question...

1. Why do you use sudo in a unit file? Is this called by the host systemd or from within a user session?
2. Why do you call chown and chgrp for the same file, when you can change both user and group with one invocation of chown?
3. Are the folder permissions of those rfkill folders different from the folders higher in the samsung tree?

I would advise against tempering with those permissions. I would also refrain from allowing all rfkill in sudoers for NOPASSWD. I would write a script that takes arguments, put it in /usr/local/sbin and allow the script in sudoers. You could also use policykit (should it be installed). For some masochistic reason I seem to gravitate towards policykit. Guess I should get a dog.

Offline

#5 2015-04-14 17:50:07

woodape
Member
Registered: 2015-03-25
Posts: 159

Re: [WORKED-AROUND] Can't write to Samsung specific file without sudo

Awebb wrote:

While I have no real answer to your question...

1. Why do you use sudo in a unit file? Is this called by the host systemd or from within a user session?
2. Why do you call chown and chgrp for the same file, when you can change both user and group with one invocation of chown?
3. Are the folder permissions of those rfkill folders different from the folders higher in the samsung tree?

1) Its called by the host I'm assuming. I used 'systemctl enable fnkeys.service' to enable it. I use sudo because randomly the unit would fail to change permissions without it and it works consistently with it in. Unless there is some harm being done by using sudo that I can't see? I've removed this unit anyhow as per your suggestion (below).

2) I'm new to arch and these are the commands I remembered. Again they work, but now looking at the man page for 'chown' I see stylistically 'chown woodape:woodape' would be one less line of code...

3) Every folder in /sys/devices/platform/samsung/ including the rfkill folders default to root ownership and root group with read write and execute permissions for the owner. The three files I mention all have read write and execute permissions, so there is no need to change anything there. So no, there doesn't seem on face value to be anything special about the permissions of the rfkill folders.

Awebb wrote:

I would advise against tempering with those permissions. I would also refrain from allowing all rfkill in sudoers for NOPASSWD. I would write a script that takes arguments, put it in /usr/local/sbin and allow the script in sudoers.

Fair enough, this approach also saves me that precious .01 second of boot time taken up by the service. I've made a second script, "fnkeys2", put it in /usr/local/sbin/, made it executable, and added it under/etc/sudoers.d/fnkeys2 with the NOPASSWD flag. My original script from "F9)" down has changed to:

	F9)	#Increment Keybacklight down 1 notch
		sudo fnkeys2 F9
	;;
	F10)	#Increment Keybacklight up 1 notch
		sudo fnkeys2 F10
	;;
	F11)	#Toggle Silent/Normal performance mode
		sudo fnkeys2 F11
	;;
	F12)	#Toggle soft block of Wireless
		sudo fnkeys2 F12
	;; esac
exit 0

with the previous contents of those sections now in fnkeys2.

Awebb wrote:

You could also use policykit (should it be installed). For some masochistic reason I seem to gravitate towards policykit. Guess I should get a dog.

I think everyone on Arch must be a masochist to some extent, but I'm not enough of one to start playing with policykit. Besides the above seems to work fine without any undo tinkering.

Maybe a dog isn't such a great idea for a masochist though... go with a cat I think.

Last edited by woodape (2015-04-14 17:59:46)

Offline

Board footer

Powered by FluxBB