Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
alxchk committed May 18, 2017
1 parent 6cce4e7 commit 9e321db
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
60 changes: 40 additions & 20 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@
* this in Cython which I later on translated in C.
*/

// fix compilation issue on SunOS 5.10, see:
// https://github.com/giampaolo/psutil/issues/421
// https://github.com/giampaolo/psutil/issues/1077
// http://us-east.manta.joyent.com/jmc/public/opensolaris/ARChive/PSARC/2010/111/materials/s10ceval.txt
//
// Because LEGACY_MIB_SIZE defined in the same file there is no way to make autoconfiguration =\
//

#include <Python.h>

// fix for "Cannot use procfs in the large file compilation environment"
// error, see:
// http://sourceware.org/ml/gdb-patches/2010-11/msg00336.html
#undef _FILE_OFFSET_BITS
#define NEW_MIB_COMPLIANT 1
#define _STRUCTURED_PROC 1

// fix compilation issue on SunOS 5.10, see:
// https://github.com/giampaolo/psutil/issues/421
#define NEW_MIB_COMPLIANT
#include <Python.h>

#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
# undef _FILE_OFFSET_BITS
# undef _LARGEFILE64_SOURCE
#endif

#include <sys/types.h>
#include <sys/stat.h>
Expand All @@ -46,9 +51,6 @@
#include "_psutil_posix.h"

#define PSUTIL_TV2DOUBLE(t) (((t).tv_nsec * 0.000000001) + (t).tv_sec)
#ifndef EXPER_IP_AND_ALL_IRES
#define EXPER_IP_AND_ALL_IRES (1024+4)
#endif


/*
Expand Down Expand Up @@ -960,7 +962,7 @@ psutil_net_connections(PyObject *self, PyObject *args) {
#endif
char buf[512];
int i, flags, getcode, num_ent, state;
char lip[200], rip[200];
char lip[INET6_ADDRSTRLEN], rip[INET6_ADDRSTRLEN];
int lport, rport;
int processed_pid;
int databuf_init = 0;
Expand All @@ -986,9 +988,7 @@ psutil_net_connections(PyObject *self, PyObject *args) {
goto error;
}

/*
XXX - These 2 are used in ifconfig.c but they seem unnecessary
ret = ioctl(sd, I_PUSH, "tcp");
int ret = ioctl(sd, I_PUSH, "tcp");
if (ret == -1) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
Expand All @@ -998,8 +998,7 @@ psutil_net_connections(PyObject *self, PyObject *args) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
*/

//
// OK, this mess is basically copied and pasted from nxsensor project
// which copied and pasted it from netstat source code, mibget()
// function. Also see:
Expand All @@ -1009,9 +1008,14 @@ psutil_net_connections(PyObject *self, PyObject *args) {
tor->OPT_length = sizeof (struct opthdr);
tor->MGMT_flags = T_CURRENT;
mibhdr = (struct opthdr *)&tor[1];
mibhdr->level = EXPER_IP_AND_ALL_IRES;
mibhdr->level = MIB2_IP;
mibhdr->name = 0;

#ifdef NEW_MIB_COMPLIANT
mibhdr->len = 1;
#else
mibhdr->len = 0;
#endif

ctlbuf.buf = buf;
ctlbuf.len = tor->OPT_offset + tor->OPT_length;
Expand All @@ -1024,7 +1028,6 @@ psutil_net_connections(PyObject *self, PyObject *args) {

mibhdr = (struct opthdr *)&toa[1];
ctlbuf.maxlen = sizeof (buf);

for (;;) {
flags = 0;
getcode = getmsg(sd, &ctlbuf, (struct strbuf *)0, &flags);
Expand Down Expand Up @@ -1072,7 +1075,11 @@ psutil_net_connections(PyObject *self, PyObject *args) {
tp = (mib2_tcpConnEntry_t *)databuf.buf;
num_ent = mibhdr->len / sizeof(mib2_tcpConnEntry_t);
for (i = 0; i < num_ent; i++, tp++) {
#ifdef NEW_MIB_COMPLIANT
processed_pid = tp->tcpConnCreationProcess;
#else
processed_pid = 0;
#endif
if (pid != -1 && processed_pid != pid)
continue;
// construct local/remote addresses
Expand Down Expand Up @@ -1113,7 +1120,11 @@ psutil_net_connections(PyObject *self, PyObject *args) {
num_ent = mibhdr->len / sizeof(mib2_tcp6ConnEntry_t);

for (i = 0; i < num_ent; i++, tp6++) {
#ifdef NEW_MIB_COMPLIANT
processed_pid = tp6->tcp6ConnCreationProcess;
#else
processed_pid = 0;
#endif
if (pid != -1 && processed_pid != pid)
continue;
// construct local/remote addresses
Expand Down Expand Up @@ -1149,8 +1160,13 @@ psutil_net_connections(PyObject *self, PyObject *args) {
else if (mibhdr->level == MIB2_UDP || mibhdr->level == MIB2_UDP_ENTRY) {
ude = (mib2_udpEntry_t *)databuf.buf;
num_ent = mibhdr->len / sizeof(mib2_udpEntry_t);
assert(num_ent * sizeof(mib2_udpEntry_t) == mibhdr->len);
for (i = 0; i < num_ent; i++, ude++) {
#ifdef NEW_MIB_COMPLIANT
processed_pid = ude->udpCreationProcess;
#else
processed_pid = 0;
#endif
if (pid != -1 && processed_pid != pid)
continue;
// XXX Very ugly hack! It seems we get here only the first
Expand Down Expand Up @@ -1186,7 +1202,11 @@ psutil_net_connections(PyObject *self, PyObject *args) {
ude6 = (mib2_udp6Entry_t *)databuf.buf;
num_ent = mibhdr->len / sizeof(mib2_udp6Entry_t);
for (i = 0; i < num_ent; i++, ude6++) {
#ifdef NEW_MIB_COMPLIANT
processed_pid = ude6->udp6CreationProcess;
#else
processed_pid = 0;
#endif
if (pid != -1 && processed_pid != pid)
continue;
inet_ntop(AF_INET6, &ude6->udp6LocalAddress, lip, sizeof(lip));
Expand Down
12 changes: 6 additions & 6 deletions psutil/arch/solaris/v10/ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@


static struct sockaddr *
sa_dup (struct sockaddr *sa1)
sa_dup (struct sockaddr_storage *sa1)
{
struct sockaddr *sa2;
size_t sz = sizeof(sa1);
size_t sz = sizeof(struct sockaddr_storage);
sa2 = (struct sockaddr *) calloc(1,sz);
memcpy(sa2,sa1,sz);
return(sa2);
Expand Down Expand Up @@ -91,11 +91,11 @@ int getifaddrs (struct ifaddrs **ifap)

if (ioctl(sd, SIOCGLIFADDR, ifr, IFREQSZ) < 0)
goto error;
cifa->ifa_addr = sa_dup((struct sockaddr*)&ifr->lifr_addr);
cifa->ifa_addr = sa_dup(&ifr->lifr_addr);

if (ioctl(sd, SIOCGLIFNETMASK, ifr, IFREQSZ) < 0)
goto error;
cifa->ifa_netmask = sa_dup((struct sockaddr*)&ifr->lifr_addr);
cifa->ifa_netmask = sa_dup(&ifr->lifr_addr);

cifa->ifa_flags = 0;
cifa->ifa_dstaddr = NULL;
Expand All @@ -105,9 +105,9 @@ int getifaddrs (struct ifaddrs **ifap)

if (ioctl(sd, SIOCGLIFDSTADDR, ifr, IFREQSZ) < 0) {
if (0 == ioctl(sd, SIOCGLIFBRDADDR, ifr, IFREQSZ))
cifa->ifa_dstaddr = sa_dup((struct sockaddr*)&ifr->lifr_addr);
cifa->ifa_dstaddr = sa_dup(&ifr->lifr_addr);
}
else cifa->ifa_dstaddr = sa_dup((struct sockaddr*)&ifr->lifr_addr);
else cifa->ifa_dstaddr = sa_dup(&ifr->lifr_addr);

pifa = cifa;
ccp += IFREQSZ;
Expand Down
2 changes: 1 addition & 1 deletion psutil/arch/solaris/v10/ifaddrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ struct ifaddrs {
extern int getifaddrs(struct ifaddrs **);
extern void freeifaddrs(struct ifaddrs *);

#endif
#endif
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def get_ethtool_macro():

elif SUNOS:
macros.append(("PSUTIL_SUNOS", 1))
sources.append('psutil/arch/solaris/v10/ifaddrs.c')
ext = Extension(
'psutil._psutil_sunos',
sources=sources + ['psutil/_psutil_sunos.c'],
Expand Down

0 comments on commit 9e321db

Please sign in to comment.