LIBPASE

Common 2014 (speaker: Tony Cairns)

37RF - 590282 Monday, May 5, 2014: 05:00 PM - 06:15 PM, RPR, Java Sea 2 Unix-assisted workloads via RPG calling PASE

LIBPASE

This RPG class demonstrated how to call PASE shared libraries in the current job, nothing special RPG, nothing special PASE. However, one of the examples demonstrated how to generate callable machine instructions for Power, used in this case as callback proxy from PASE libcurl to RPG procedure. That’s correct, writing good old machine instructions via your RPG program and running them, not for malicious intend, but to expand your abiltiy to interact with PASE in an RPG process (any job), only Java JIT people understand this simple technique until this RPG LIBPASE download for the rest of us (see RPG ZZHACK in download).

Versions:
2014–05–16 - RPG_libpase_1.0.1.zip

Demonstration:

Common demonstration ordinary RPG calling perzl libcurl.a, this case, for simplicity of PASE APIs, the LIBPASE SRVPGM was created, so you do not have to code the IBM i QP2 ILE APIs … feel free to steal any part or use as is.

PGMs using LIBPASE and LIBCURL

  • ZZCURL — simple RPG libcurl program dumps results to green screen
  • ZZBACK — advanced RPG libcurl program callback from PASE with results using gcc PASE compiler (move terminal data to RPG callback)
  • ZZHACK — expert RPG libcurl program callback from PASE with results generating Power code stream, NOT USING a PASE compiler (move terminal data to RPG callback)
    • zzhack.rpgle — on-the-fly generate machine code for callback (yep, you can) … forget about it, no need for PASE proxy module, no need for PASE compiler, generate machine code, cool.
      • Note: Generating PowerPC instructions has little to do with profile security model of IBM i, your IBM i running profile was/is set by sign-on (or switch profile API), matters not if you are running PASE code or ILE code or hand generated PowerPC instruction (LIBPASE example). Yes, hand code generation enables many dynamic load/calls, and, most ILE folks are not familiar with dynamic code beyond obvious code generation used by Java JIT (just copying Java JIT technique), but code generation does not hijack or tinker with IBM profile security (*EXCLUDE, etc.).

Installation

Set up perzl download on your IBM i:

http://youngiprofessionals.com/wiki/index.php/PASE/OpenSourceBinaries
1) Download (laptop):
unzip download-2.0.tar.zip (will create download-2.0.tar)
2) FTP
ftp myibmi
> cd /QOpenSys
> bin
> put download-2.0.tar
2) Setup rpm and perzl *.deps (local copy)
call qp2term (or ssh uid@myibmi)
> cd /QOpenSys
> tar -xf download-2.0.tar
> cd /QOpenSys/download
> ./setup.sh
(a) setup installs rpm.rte
(b) setup rpm install wget
(c) setup untar perzl deplists (www.oss4aix.org/rpmdb/deplists/aix53,aix61)
Note: (c) can take significant time (setting up prezl depend tree ...)

Download libcurl from perzl.org:

call qp2term (or ssh uid@myibmi)
> cd /QopenSys/download
Help ...
> ./wwwperzl.sh help
wwwperzl.sh aix53|aix61 option [scan]
Install curl (libcurl) ...
> ./wwwperzl.sh aix53 wget curl-7.27.0-1
> ./wwwperzl.sh aix53 rpm curl-7.27.0-1
Run curl (command line) ...
> bash-4.2$ curl --help
Usage: curl [options...] <url>
bash-4.2$ curl http://example.com
<!doctype html>
<html>
<head>
<title>Example Domain</title>

LIBCURL (LIBPASE/LIBCURL.SRVPGM)

/copy libpase/srccurl,libcurl_h

     D curl_load_libcurl...
     D                 PR            10U 0
     D  iCurlLib                     48a   value
     D  iCurlSo                      48a   value

     D curl_global_init...
     D                 PR            10U 0
     D curlopt                       10U 0 value

     D curl_easy_init...
     D                 PR            10U 0

     D curl_easy_setopt...
     D                 PR            10U 0
     D curl                          10U 0 value
     D curlopt                       10U 0 value
     D curlval                       10U 0 value

     D curl_easy_perform...
     D                 PR            10U 0
     D curl                          10U 0 value

     D curl_easy_cleanup...
     D                 PR
     D curl                          10U 0 value

     D curl_easy_strerror...
     D                 PR              *
     D curlcode                      10U 0 value

LIBPASE functions (LIBPASE/LIBPASE.SRVPGM)

/copy libpase/srcpase,libdl_h

     D pLIBPATH...
     D                 PR
     D  iPath                      2048a   value

     D pERROR...
     D                 PR          2048a

     D pLoad...
     D                 PR              *
     D  iLib                       1024a   value
     D  iFunc                      1024a   value
     D  iArgSig                       5i 0 value dim(PMAXARGS)
     D  iRetSig                      10i 0 value

     D pCall...
     D                 PR            10i 0
     D iProc                           *   value
     D iArgs                           *   value
     D iRet                            *   value

/copy libpase/srcpase,libc_h

     D sLibc           c                   const('libc.a')

     D pHeapFree...
     D                 PR
     D  pHeapIle                       *   value

     D pHeapAlloc32...
     D                 PR              *
     D  iSz                          10i 0 value
     D  pHeapPase                      *   value

     D pAsciiString32...
     D                 PR              *
     D  ileStr                    65000A   value
     D  pHeapPase                      *   value

     D psleep...
     D                 PR
     D  nsecs                        10i 0 value

     D pSETSPP32...
     D                 PR
     D  pTgt                         10U 0 value
     D  pSrc                         10U 0 value

     D pMMap32...
     D                 PR            10U 0
     D  addr                         10U 0 value
     D  size                         10i 0 value
     D  prot                         10i 0 value
     D  flag                         10i 0 value
     D  fdes                         10i 0 value
     D  foff                         10U 0 value

     D pUMap32...
     D                 PR            10i 0
     D  addr                         10U 0 value
     D  size                         10i 0 value

/copy libpase/srcpase,libpase_h

     d PaseLstCCSID...
     d                 PR            10I 0

     d PaseJobCCSID...
     d                 PR            10I 0

     D PaseStart32     PR              *
     D  myLIBPATH                      *   value
     D  myInit                         *   value
     D  mySize                       10i 0 value

     D PaseStop        PR

      * pase errno
     D PaseEZero       PR             1N
     D PaseErrno       PR            10I 0

/copy libpase/srcpase,libile_h

      *****************************************************
      * ile functions
      *****************************************************
     D iHeapFree...
     D                 PR
     D  pHeapIle                       *   value

     D iHeapAlloc...
     D                 PR              *
     D  iSz                          10i 0 value

     D iEbcdicString...
     D                 PR              *
     D  paseStr                        *   value

     D memset          PR              *   ExtProc('__memset')
     D  pTarget                        *   Value
     D  nChar                        10I 0 Value
     D  nBufLen                      10U 0 Value

     D memcmp          PR            10I 0 ExtProc('__memcmp')
     D  pS1                            *   Value
     D  pS2                            *   Value
     D  nBufLen                      10U 0 Value

     D cpybytes        PR                  ExtProc('_CPYBYTES') 
     D  pTarget                        *   Value 
     D  pSource                        *   Value 
     D  nLength                      10U 0 Value 

     d cpybwp          PR                  extproc('_CPYBWP')
     d  pTarget                        *   value
     d  pSource                        *   value
     d  nLength                      10u 0 value

     D islower         PR            10i 0 ExtProc('islower')
     D  nBufLen                      10i 0 Value

     D toupper         PR            10i 0 ExtProc('toupper')
     D  nBufLen                      10i 0 Value

     D strlen          PR            10I 0 ExtProc('strlen')
     D  pVal                           *   Value options(*string)

     D strchr          PR              *   ExtProc('strchr')
     D  pVal                           *   Value options(*string)
     D  nChar                        10I 0 Value

     D strrchr         PR              *   ExtProc('strrchr')
     D  pVal                           *   Value options(*string)
     D  nChar                        10I 0 Value

     D strstr          PR              *   ExtProc('strstr')
     D  pVal1                          *   Value options(*string)
     D  pVal2                          *   Value options(*string)

     D RSLOBJ_TS_PGM   c                   const(X'0201')
     D RSLOBJ_TS_SRVPGM...
     D                 c                   const(X'0203')

     D ileRslv         PR             1N
     D  pgm2                         10A   value
     D  lib2                         10A   value
     D  pSym                           *
     D  sym2                        128A   value options(*nopass)

      ** resolve system pointer
      *  PGM      x'201'
      *  SRVPGM   x'203'
      *  LIB      x'401'
     D rslvsp          PR              *   procptr ExtProc('rslvsp') 
     D  ObjType                       2A   Value                     
     D  ObjName                        *   Value OPTIONS(*STRING)    
     D  ObjLibr                        *   Value OPTIONS(*STRING)    
     D  ObjAuth                       2A   Value  

     D actbndpgm       PR            20i 0 ExtProc('QleActBndPgmLong') 
     D  objPtr                         *   Value 

     D actsympgm       PR              *   ExtProc('QleGetExpLong') 
     D  objAct                       20i 0  
     D  zeroA1                       10i 0 Value
     D  symLen                       10i 0
     D  symName                        *   Value OPTIONS(*STRING)    
     D  expPtr                         *   Value 
     D  expTyp                       10i 0


      *****************************************************
      * file
      *****************************************************
     D O_RDONLY        C                   1
     D O_WRONLY        C                   2
     D O_RDWR          C                   4
     D O_CREAT         C                   8
     D O_EXCL          C                   16
     D O_TRUNC         C                   64
     D O_APPEND        C                   256
     D O_CODEPAGE      C                   8388608
     D O_TEXTDATA      C                   16777216

     D S_IRUSR         C                   256
     D S_IWUSR         C                   128
     D S_IXUSR         C                   64
     D S_IRWXU         C                   448
     D S_IRGRP         C                   32
     D S_IWGRP         C                   16
     D S_IXGRP         C                   8
     D S_IRWXG         C                   56
     D S_IROTH         C                   4
     D S_IWOTH         C                   2
     D S_IXOTH         C                   1
     D S_IRWXO         C                   7

     D ftok            PR            10i 0 extproc('ftok')
     D   path                          *   value options(*string)
     D   ID                          10i 0 value

     D mkdir           PR            10i 0 extproc('mkdir')
     D   path                          *   value options(*string)
     D   mode                        10i 0 value

     D openIFS         PR            10I 0 extproc('open')
     D   path                          *   value options(*string)
     D   oflag                       10I 0 value
     D   mode                        10U 0 value options(*nopass)
     D   codepage                    10U 0 value options(*nopass)

     D closeIFS        PR            10I 0 extproc('close')
     D   fildes                      10I 0 value


     D readIFS         PR            20I 0 ExtProc('read')     
     D   fd                          10I 0 value                       
     D   buf                           *   value
     D   size                        10I 0 value                       

     D writeIFS        PR            20I 0 ExtProc('write')     
     D   fd                          10I 0 value                       
     D   buf                           *   value
     D   size                        10I 0 value 

     D fsyncIFS        PR            20I 0 ExtProc('fsync')     
     D   fd                          10I 0 value                       

     D unlink          PR            10I 0 ExtProc('unlink')                 
     D   path                          *   value options(*string)            

     D rmdir           PR            10I 0 ExtProc('rmdir')                 
     D   path                          *   value options(*string)            

      *****************************************************
      * file _C_IFS_fopen, _C_NEU_DM_fopen, fopen
      *****************************************************
     DopenSRC          Pr              *   ExtProc( 'fopen' )
     D                                 *   Value  Options( *String )
     D                                 *   Value  Options( *String )

     DfgetSRC          Pr              *   ExtProc( 'fgets' )
     D                                 *   Value
     D                               10i 0 Value
     D                                 *   Value

     DfputSRC          Pr            10i 0 ExtProc( 'fputs' )
     D                                 *   Value  Options( *String )
     D                                 *   Value

     DcloseSRC         Pr            10i 0 ExtProc( 'fclose' )
     D                                 *   Value

/copy libpase/srcpase,license_h

      ***************************************************************************
      *
      * Project            RPG
      *
      * Copyright (c) 2014, IBM Corporation
      * All rights reserved.
      *
      * In order to be useful for every potential user, curl and libcurl are
      * dual-licensed under the MPL and the MIT/X-derivate licenses.
      *
      * You may opt to use, copy, modify, merge, publish, distribute and/or sell
      * copies of the Software, and permit persons to whom the Software is
      * furnished to do so, under the terms of the MPL or the MIT/X-derivate
      * licenses. You may pick one of these licenses.
      *
      * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
      * KIND, either express or implied.
      *
      * Redistribution and use in source and binary forms, 
      * with or without modification, are permitted provided 
      * that the following conditions are met:
      * - Redistributions of source code must retain 
      *   the above copyright notice, this list of conditions 
      *   and the following disclaimer. 
      * - Redistributions in binary form must reproduce the 
      *   above copyright notice, this list of conditions 
      *   and the following disclaimer in the documentation 
      *   and/or other materials provided with the distribution.
      * - Neither the name of the IBM Corporation nor the names 
      *   of its contributors may be used to endorse or promote 
      *   products derived from this software without specific 
      *   prior written permission. 
      *
      * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
      * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
      * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
      * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
      * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
      * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
      * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
      * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
      * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
      * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
      * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
      * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
      * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
      * POSSIBILITY OF SUCH DAMAGE.
      ***************************************************************************