3

我想在 python 脚本中运行一些命令

import fcntl

KDSETLED = 0x4B32
SCR_LED  = 0x01

console_fd = os.open('/dev/console', os.O_NOCTTY)
fcntl.ioctl(console_fd, KDSETLED, SCR_LED)

我已经设置a+rw好了,/dev/console但是当我从普通用户运行脚本时:

fcntl.ioctl(console_fd, KDSETLED, SCR_LED) IOError: [Errno 1] Operation not allowed

如果我需要从普通用户运行该脚本,我该怎么办?

4

1 回答 1

4

我相信您需要使用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;

所以,看起来这绝对是你的两个选择。

于 2011-01-14T20:53:02.100 回答