我想在python脚本中运行一些命令
import fcntl
KDSETLED = 0x4B32
SCR_LED = 0x01
console_fd = os.open('/dev/console', os.O_NOCTTY)
fcntl.ioctl(console_fd, KDSETLED, SCR_LED)
我为/ dev / console设置了一个rw,但是当我从普通用户那里运行脚本时:
fcntl.ioctl(console_fd, KDSETLED,
SCR_LED) IOError: [Errno 1] Operation
not permitted
如果需要普通用户运行该脚本,该怎么办?
解决方法:
我相信您需要使用CAP_SYS_TTY_CONFIG执行脚本.或者,或者(如果您正在控制台上运行),使用控制性tty(例如/ dev / tty1)而不是/ dev / console可能有效.
强制执行此操作的内核代码似乎是drivers / tty / vt / vt_ioctl.c:
/*
* To have permissions to do most of the vt ioctls, we either have
* to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
*/
perm = 0;
if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
perm = 1;
⋮
case KDSETLED:
if (!perm)
goto eperm;
setledstate(kbd, arg);
break;
因此,绝对看起来这是您的两个选择.