从python运行c代​​码并删除root访问权限

时间:2020-09-16 13:55:56

标签: python

好的,我正在使用ctypes在python中运行c程序。 C程序需要root访问权限,因此我使用sudo python3 reader.py运行python也可以,但是一旦我将c程序置于无限循环以连续读取时,第二次读取突然就没有root访问权限。

代码:

from ctypes import *
so_file = "/home/pi/rfid/rc522/rc522.so"
rf = CDLL(so_file)
import time

def readcard()
    ID = rf.start()
    return ID

while True:
    ID = readcard()
    print(ID)
    print("wait 5 sec")
    time.sleep(5)

编辑: C代码:

int start() {
    int     tmp;
    int     d_write = 0,d_read = 0, action=0, change=0, add_act=0;
    int     option = 0, upd_key=0;
    
    // for write  & read  message from command line
    int     start_addr, get_bytes=0, disp_as_alpha =0;
    int     start_sector = 1, start_block = 0, got_mess=0, length = 0;
    char    *message = NULL;
    
    // for value block
    int     create_block = 0, incr_block = 0, decr_block=0, show_block=0, rem_block=0, block;
    double  set_val = 0, value;

    int SerNum;

    // must be run as root to open /dev/mem in BMC2835
    `if (geteuid() != 0)
    {
        p_printf(RED,"Must be run as root.\n");
        exit(1);
    }

    // catch signals
    set_signals();
        
    /* read /etc/rc522.conf */
    if (get_config_file()) exit(1);

    /* set BCM2835 Pins correct */
    if (HW_init(spi_speed,gpio)) close_out(1);
    
    /* initialise the RC522 */
    InitRc522();

    /* read & set GID and UID from config file */
    if (read_conf_uid()!= 0) close_out(1);
    
    SerNum = ret_card_details();
    //disp_card_details();
    PcdHalt();
    if (use_gpio) bcm2835_gpio_write(gpio, LOW); // set reset
    bcm2835_spi_end();
    bcm2835_close();
    close_config_file();

    
    // stop -Wall complaining
    return SerNum;
}

0 个答案:

没有答案