D3 and mvBase

 View Only
  • 1.  forking _CP_logon

    Posted 01-25-2022 13:18
    Hopefully this is the right forum for this question.  Has anyone used fork (or forkpty) and successfully called _CP_logon from inside of a C program?  Is there a trick to making this work? When I try it, the child dies with a segfault somewhere in pick_init_termio in a call to strcpy.

    ------------------------------
    Tom Marracci
    General Manager
    Aircraft Spruce
    corona CA US
    ------------------------------


  • 2.  RE: forking _CP_logon

    ROCKETEER
    Posted 01-25-2022 13:56
    Got small example code? I can then ask somebody.

    ------------------------------
    Brian S. Cram
    Principal Technical Support Engineer
    Rocket Software
    ------------------------------



  • 3.  RE: forking _CP_logon

    Posted 01-25-2022 14:14
    Hi Brian,

    I guess the post went through after all. I couldn’t see it on the list of open threads

    Here’s a snippet:

    int master;
    pid_t pid = forkpty(&master,0,0,0);
    if (pid > 0) {
    int stat = 0;
    waitpid(pid,&stat,0);
    return stat != 0;
    }
    MimeRunner rr(&r);
    if (c.login(config->machine, config->user, config->password, config->account, config->account_password)) {
    c.run(rr);
    c.logout();
    exit(r.send(200,"OK",getContentType(),rr.c_str(),rr.size()));
    } else {
    exit(r.send(500,"SERVER ERROR","text/html","",13));
    }


    and the login method in the c object (which I have as class called Runtime) is:

    int Runtime::login(const char* machine, const char* user, const char* psw, const char* acct, const char* apsw)
    {
    CPSTR* m = _CP_mkstr((char*)machine);
    CPSTR* u = _CP_mkstr((char*)user);
    CPSTR* p = psw[0] ? _CP_mkstr((char*)psw) : _CP_str_null;
    CPSTR* a = _CP_mkstr((char*)acct);
    CPSTR* w = apsw[0] ? _CP_mkstr((char*)apsw) : _CP_str_null;

    int n = _CP_logon(m,u,p,a,w,-1,0);

    _CP_str_free(m);
    _CP_str_free(u);
    _CP_str_free(a);
    if (psw[0]) _CP_str_free(p);
    if (apsw[0]) _CP_str_free(w);
    return n>-1;
    }

    When I run it, _CP_logon never returns, but instead the child dies. I’ve run it with gdb and from the stack trace it shows it dies in strcpy() called from pick_init_termio(). The session does not need to be user interactive. I’m trying to use it as a database connection with basic calling and access to standard D3 functions via the _CP_ api. The program is owned by root with the +s option set.


    Any advice would be most appreciated.

    Tom


    > On Jan 25, 2022, at 10:58 AM, Brian Cram via Rocket Forum


  • 4.  RE: forking _CP_logon

    Posted 01-25-2022 14:41
    Brian,

    A little more to this story.

    When I run the code provided from within my program (which actually forks as a server and then forks child processes to logon) I get the break where I said it was.

    When I run it standalone, it still does not return from _CP_logon, but instead heads to a bash shell before dying. Not the same error, but the same effect. Maybe that’s a clue to the cure.

    here’s another snippet that still does not work:

    #include


  • 5.  RE: forking _CP_logon

    ROCKETEER
    Posted 01-25-2022 15:26
    Tom, I see you opened a case, and that was probably the smart move ( unless anyone else out there has ideas ??? Hint hint ??? ). So I'm going to take the content from here and post it in the case.

    ------------------------------
    Brian S. Cram
    Principal Technical Support Engineer
    Rocket Software
    ------------------------------



  • 6.  RE: forking _CP_logon

    Posted 01-25-2022 15:58
    cool beans.

    I replied to the cast with the missing content. I’ll include it here for you as well

    Here’s what was after the includes:

    int main()
    {
    int m;
    pid_t pid;

    pid = forkpty(&m,0,0,0);
    if (pid == 0) {
    int n = _CP_logon(_CP_mkstr(“pick0”),_CP_mkstr(“DM”),_CP_str_null,_CP_mkstr(“DM”),_CP_str_null,-1,0); // this never returns
    printf(“logon returned %d\r\n”,n);
    if (n > -1) _CP_logoff();
    } else {
    m = 0;
    waitpid(pid,&m,0);
    }

    return 0;
    }

    However, if I remove the forking and run it like:

    int main()
    {
    int n = _CP_logon(_CP_mkstr(“pick0”),_CP_mkstr(“DM”),_CP_str_null,_CP_mkstr(“DM”),_CP_str_null,-1,0);
    printf(“logon returned %d\r\n”,n);
    if (n > -1) _CP_logoff();

    return 0;
    }

    it works fine
    Tom

    > On Jan 25, 2022, at 12:28 PM, Brian Cram via Rocket Forum


  • 7.  RE: forking _CP_logon

    Posted 01-25-2022 16:24

    hy... 

    i only one question... why do that ?

    you can have some improvements ? i never thought about using cp_logon for anything

    what are the beneficts ?



    ------------------------------
    Alberto Leal
    System Analyst
    Millano Distribuidora de Auto Pecas Ltda
    Varzea Grande MT BR
    ------------------------------



  • 8.  RE: forking _CP_logon

    Posted 01-25-2022 18:13
    This is for a web server ala php. That is, basic code will be compiled and intepreted on the fly with access to the D3 system of database and other functions available in basic.

    So, for example, you would make a page for url https://localhost/test.bas?custid=12345

    <DOCTYPE! html>
    <html>
    <head><title>Some Title</title><head>
    <body>
    <*
      print 'hello world<br>'
      print 'the time is ':oconv(time(),'mth'):'<br>'
      open 'customer' to customer else stop 201,'customer'
      read rec from customer,$_GET['custid'] else rec = ''
      print 'your customers details:'
      for i = 1 to 10
        print '[':i:'] ':htmlencode(rec<i>):'<br>'
      next i
    *>
    </body>
    </html>

    Easy access to the GET, POST, COOKIE, SESSION, and SERVER variables (like php), same syntax as pick basic (with some minor exceptions) and it's really fast and secured by SSL.  Compile time for the above script on my Linux VM is a  few microseconds, execution is on par with running basic within D3 on my Linux server.  

    So to get here, I have a server that preforks some number of worker processes (like apache) and the basic compiler does everything but input (this handled by the browser) and has extra functions like hashing (md5, sha1, sha256), simple AES encryption interface, json encode/decode, urlencode, htmlencode, and some other functions that I found I need but do not come with basic out of the box.  Akin to flash connect without a lot of extra software, licenses, and some other obstacles that I couldn't get around.  My only problem now is _CP_logon does not want to complete the logon process. It crashes half way through and just dies.

    ------------------------------
    Tom Marracci
    General Manager
    Aircraft Spruce
    corona CA US
    ------------------------------