[Python-Dev] PyConfig.h On Solaris

John Abel john.abel@pa.press.net
Tue, 22 Jul 2003 22:48:58 +0100


> > OK, I can see why Python.h has to be first, but surely, abiding by the
spec
> > shouldn't prevent code from executing properly?  Should I raise a bug
report
> > for this?
>
> For what? That you can't compile a certain Solaris header, if the
> application adds some system-specifed #defines? That is a Solaris bug,
> not a Python bug. All system headers ought to work under
> _LARGEFILE_SOURCE and _FILE_OFFSET_BITS=64.
>
> > I'm guessing that only Solaris is affected (though maybe other
> > BSD-types are, too?  AFAIK, only Solaris has the /proc oddity).  I'd
> > like to be able to produce compliant modules.  I'd like to help, if
> > I can.
>
> I don't see that Python can or should change here. We have to define
> _FILE_OFFSET_BITS, and we have to define it consistently across all
> object files, or else we can't free exchange data structures which
> contain off_t fields across functions.
>
Martin,

Thanks for your help.  It's a shame that Sun don't hurry up, and deprecate
the ioctl method of reading /proc, as it seems that where most of the bother
comes from.  Maybe that's why no-one else seems to have written anything for
parsing of /proc (in Python, or Perl).  The module I've written, only uses
open, lseek, and read.   When it's finally finished, I'll distribute it with
a warning.

Regards

John

PS, for those that are interested, I've included the working (non-compliant)
code.  I'd appreciate any pointers.

/*
        psinfomodule.c - a Python module to return PS information
*/


#include <procfs.h>

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "Python.h"

static PyObject *
psinfo_pidinfo(PyObject *self, PyObject *args)
{
        PyObject *queryPid;
        char procBuf[32];
        char cmdLine[255];
        char cmdArgs[255];
        psinfo_t psBuf;
        int fdPSFile;

        if (!PyArg_ParseTuple(args, "l:pidinfo", &queryPid) ) {
            return NULL;
        }
        (void)sprintf(procBuf,"/proc/%ld/psinfo", (long)queryPid);
        if ((fdPSFile = open(procBuf, O_RDONLY)) != -1 ) {
            if ( read(fdPSFile, &psBuf, sizeof(psBuf)) == sizeof(psBuf) ) {
                (void)sprintf(cmdLine, "%s", psBuf.pr_fname );
                (void)sprintf(cmdArgs, "%s", psBuf.pr_psargs );
            }
            else{
                return NULL;
            }
            close(fdPSFile);
        }
        return Py_BuildValue( "ss", cmdLine, cmdArgs );
}

static PyObject *
psinfo_fullpidinfo(PyObject *self, PyObject *args)
{
    PyObject *queryPid;
    char procBuf[50];
    char asBuf[50];
    char argStr[512], cmdArgs[1024];
    char **argvp;
    int fdFile, counter;
    struct psinfo psBuf;

    if (!PyArg_ParseTuple(args, "i:pidinfo", &queryPid) ) {
        return NULL;
    }

    (void)sprintf(procBuf,"/proc/%i/psinfo", (int)queryPid);
    (void)sprintf(asBuf,"/proc/%i/as", (int)queryPid);

    if ((fdFile = open(procBuf, O_RDONLY)) != -1 ) {
        if (read(fdFile, &psBuf, sizeof(struct psinfo)) == -1 ) {
            return NULL;
        }
        close(fdFile);
        if ((fdFile = open(asBuf, O_RDONLY)) == -1 ) {
            return NULL;
        }
        else {

            argvp=(char **) malloc(sizeof(char *) * (psBuf.pr_argc + 1));

            if (lseek(fdFile, psBuf.pr_argv, SEEK_SET) == -1) {
                return NULL;
            }

            if (read(fdFile, argvp, sizeof(char *) * (psBuf.pr_argc + 1))
== -1 ) {
                printf( "Read Failed" );
                return NULL;
            }
            for ( counter=0; counter < psBuf.pr_argc; counter++ ) {
                if (lseek(fdFile, argvp[ counter ], SEEK_SET) == -1) {
                    printf( "2nd LSeek Failed");
                    return NULL;
                }
                if (read(fdFile, argStr, 512) == -1) {
                    printf( "Read Of AS File Failed");
                    return NULL;
                }
                strcat( cmdArgs, argStr );
                strcat( cmdArgs, " " );
            }
            close(fdFile);
        }
    }
    else {
        return NULL;
    }
    return Py_BuildValue( "s", cmdArgs );
}


static struct PyMethodDef PSInfoMethods[]={
    { "pidinfo", psinfo_pidinfo, METH_VARARGS, "Display Details For A Given
PID" },
    { "fullpidinfo", psinfo_fullpidinfo, METH_VARARGS, "Display Full Arg
Details For A Given PID" },
    { NULL, NULL, 0, NULL }
};

void initpsinfo(void)
{

    (void)Py_InitModule("psinfo", PSInfoMethods);
}