Using cffi to call functions of shared library from python

437 views Asked by At

I am calling some c-sdk from python code. This python code is running as lambda on greengrass. I am using cffi to call the function of shared so from python. My lambda (python code) is running as gcc_user but when i print the user in my C code, it prints something like: T� Pp.

Below is the C code that i am using to print the user info:

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");

Below is the python code that i am using call the shared lib (C code):

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()

Is there a way to call c lib from python and force it to run as gcc_user? According to me it should have been running as gcc_user at first place because its just a function call from python code.

1

There are 1 answers

0
Armin Rigo On

It should be char username [10], without the *. Maybe there are other problems too but try to fix that first.