Question

I want to run some command in python script

import fcntl

KDSETLED = 0x4B32
SCR_LED  = 0x01

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

I've set a+rw for /dev/console but when I run script from regular user:

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

What should I do if I need to run that script from regular user?

Was it helpful?

Solution

I believe you need to get your script executed with CAP_SYS_TTY_CONFIG. Either that, or (if you're running on the console), using your controlling tty (e.g., /dev/tty1) instead of /dev/console might work.

The kernel code that enforces this appears to be 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;

So, definitely looks like those are your two options.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top