0

我正在从 python 代码中调用一些 c-sdk。此 python 代码在 greengrass 上作为 lambda 运行。我正在使用 cffi 从 python 调用 shared so 的函数。我的 lambda(python 代码)作为 gcc_user 运行,但是当我在 C 代码中打印用户时,它会打印出类似:T�Pp。

下面是我用来打印用户信息的 C 代码:

char* username[10];
getlogin_r(username, 10);
printf("current user in c program ");
for(index = 0;index<10; index++)
{
   printf("%c",username[index]);
}
printf("\n");

下面是我正在使用的 python 代码调用共享库(C 代码):

import cffi
import _cffi_backend
from sys import exit, platform
import boto3
from multiprocessing import Process
import sys, os
import logging
import getpass

def StartVideoStreaming(channelName):
    ffi = cffi.FFI()
    cdef_from_file = None
    header = '/home/admin/alprwebrtcdemo/amazon-kinesis-video-streams-webrtc-sdk-c/samples/kvsWebRTCClientMasterGstreamer.h'
    try:
        with open(header, 'r') as libtestcffi_header:
            cdef_from_file = libtestcffi_header.read()
    except FileNotFoundError:
        print('Unable to find "%s"' % header)
        exit(2)
    except IOError:
        print('Unable to open "%s"' % header)
        exit(3)
    finally:
        if cdef_from_file == '':
            print('File "%s" is empty' % header)
            exit(1)

    ffi.cdef(cdef_from_file)
    lib_extension = ''
    if platform.startswith('freebsd') or platform.startswith('linux'):
        lib_extension = 'so'
    elif platform.startswith('win'):
        lib_extension = 'dll'
    CLibTC = ffi.dlopen('/home/admin/alprwebrtcdemo/amazon-kinesis-video-streams-webrtc-sdk-c/build/liblibkvsWebrtcClientMasterGstSample.' + lib_extension)
    arg_0 = "arg0".encode('utf-8')
    arg_1 = channelName.encode('utf-8')
    argv_keepalive = [ffi.new("char[]", arg_0),
    ffi.new("char[]", arg_1)]
    argv = ffi.new("char *[]", argv_keepalive)
    session = boto3.Session()
    logging.info("user in process: {}".format(getpass.getuser()))
    credentials = session.get_credentials()
    accountKeyID = credentials.access_key
    accountSecret = credentials.secret_key
    sessionToken = credentials.token
    logging.info("ID: {}, secret: {}, session: {}".format(accountKeyID, accountSecret, sessionToken))

    arg1 = accountKeyID.encode('utf-8')
    arg2 = accountSecret.encode('utf-8')
    arg3 = sessionToken.encode('utf-8')
    CLibTC.start(2,argv,arg1,arg2,arg3)
    print('Everything is fine!!!')  


def main():
logging.info("XDG_CACHE_HOME env variable value: {}".format(os.environ.get(HOME)))
logging.info("user in python: {}".format(getpass.getuser()))
sys.path.insert(0,'')
p1 = Process(target=StartVideoStreaming, args=('channeltest666',))
p1.start()
p1.join()

有没有办法从 python 调用 c lib 并强制它作为 gcc_user 运行?根据我的说法,它应该首先作为 gcc_user 运行,因为它只是来自 python 代码的函数调用。

4

1 回答 1

0

应该是char username [10],没有*. 也许还有其他问题,但请先尝试解决。

于 2020-12-11T19:47:01.107 回答