Python – Operations are not allowed

Operations are not allowed… here is a solution to the problem.

Operations are not allowed

I want to run some commands in a 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 the script from a normal user:

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

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

Solution

I believe you need to use CAP_SYS_TTY_CONFIG to execute your script. Or (if you’re running on a console), using your control tty (e.g., /dev/tty1) instead of /dev/console might work.

The kernel code to do 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, it looks like these are definitely your two options.

Related Problems and Solutions