From cba4c5d58a7c98f0a48ee12663e1cb33bee8c84b Mon Sep 17 00:00:00 2001 From: Arnon Yaari Date: Sun, 22 Mar 2015 12:00:29 +0200 Subject: [PATCH 1/8] Fix get mtu on Solaris 10 --- psutil/_psutil_sunos.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/psutil/_psutil_sunos.c b/psutil/_psutil_sunos.c index 0611e0145..aa6c702db 100644 --- a/psutil/_psutil_sunos.c +++ b/psutil/_psutil_sunos.c @@ -1157,7 +1157,7 @@ psutil_net_if_stats(PyObject* self, PyObject* args) { for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { if (strcmp(ksp->ks_class, "net") == 0) { - struct ifreq ifr; + struct lifreq ifr; kstat_read(kc, ksp, NULL); if (ksp->ks_type != KSTAT_TYPE_NAMED) @@ -1165,13 +1165,13 @@ psutil_net_if_stats(PyObject* self, PyObject* args) { if (strcmp(ksp->ks_class, "net") != 0) continue; - strncpy(ifr.ifr_name, ksp->ks_name, sizeof(ifr.ifr_name)); - ret = ioctl(sock, SIOCGIFFLAGS, &ifr); + strncpy(ifr.lifr_name, ksp->ks_name, sizeof(ifr.lifr_name)); + ret = ioctl(sock, SIOCGLIFFLAGS, &ifr); if (ret == -1) continue; // not a network interface // is up? - if ((ifr.ifr_flags & IFF_UP) != 0) { + if ((ifr.lifr_flags & IFF_UP) != 0) { if ((knp = kstat_data_lookup(ksp, "link_up")) != NULL) { if (knp->value.ui32 != 0u) py_is_up = Py_True; @@ -1204,12 +1204,12 @@ psutil_net_if_stats(PyObject* self, PyObject* args) { speed = 0; // mtu - ret = ioctl(sock, SIOCGIFMTU, &ifr); + ret = ioctl(sock, SIOCGLIFMTU, &ifr); if (ret == -1) goto error; py_ifc_info = Py_BuildValue("(Oiii)", py_is_up, duplex, speed, - ifr.ifr_mtu); + ifr.lifr_mtu); if (!py_ifc_info) goto error; if (PyDict_SetItemString(py_retdict, ksp->ks_name, py_ifc_info)) From 69a4efef63a2410097722bca00bc54cccebc86e6 Mon Sep 17 00:00:00 2001 From: Arnon Yaari Date: Sat, 5 Sep 2015 10:53:37 +0300 Subject: [PATCH 2/8] Add ifaddrs.c for Solaris 10 ifaddrs doesn't exist on this platform, so I added an implementation of it for use in _psutil_posix.c --- psutil/_psutil_posix.c | 7 +- psutil/arch/solaris10/ifaddrs.c | 117 ++++++++++++++++++++++++++++++++ psutil/arch/solaris10/ifaddrs.h | 26 +++++++ setup.py | 13 ++-- 4 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 psutil/arch/solaris10/ifaddrs.c create mode 100644 psutil/arch/solaris10/ifaddrs.h diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c index 241b9d599..b46bac0d7 100644 --- a/psutil/_psutil_posix.c +++ b/psutil/_psutil_posix.c @@ -13,9 +13,14 @@ #include #include #include -#include #include +#ifdef _SUNOS10 +#include "arch/solaris10/ifaddrs.h" +#else +#include +#endif + #ifdef __linux #include #include diff --git a/psutil/arch/solaris10/ifaddrs.c b/psutil/arch/solaris10/ifaddrs.c new file mode 100644 index 000000000..535f815a9 --- /dev/null +++ b/psutil/arch/solaris10/ifaddrs.c @@ -0,0 +1,117 @@ +/* Refrences: + * https://lists.samba.org/archive/samba-technical/2009-February/063079.html + * http://stackoverflow.com/questions/4139405/#4139811 + * https://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/getifaddrs.c + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ifaddrs.h" + +#define MAX(x,y) ((x)>(y)?(x):(y)) +#define SIZE(p) MAX((p).ss_len,sizeof(p)) + + +static struct sockaddr * +sa_dup (struct sockaddr *sa1) +{ + struct sockaddr *sa2; + size_t sz = sizeof(sa1); + sa2 = (struct sockaddr *) calloc(1,sz); + memcpy(sa2,sa1,sz); + return(sa2); +} + + +void freeifaddrs (struct ifaddrs *ifp) +{ + if (NULL == ifp) return; + free(ifp->ifa_name); + free(ifp->ifa_addr); + free(ifp->ifa_netmask); + free(ifp->ifa_dstaddr); + freeifaddrs(ifp->ifa_next); + free(ifp); +} + + +int getifaddrs (struct ifaddrs **ifap) +{ + int sd = -1; + char *ccp, *ecp; + struct lifconf ifc; + struct lifreq *ifr; + struct lifnum lifn; + struct ifaddrs *cifa = NULL; /* current */ + struct ifaddrs *pifa = NULL; /* previous */ + const size_t IFREQSZ = sizeof(struct lifreq); + + sd = socket(AF_INET, SOCK_STREAM, 0); + if (sd < 0) goto error; + + ifc.lifc_buf = NULL; + *ifap = NULL; + /* find how much memory to allocate for the SIOCGLIFCONF call */ + lifn.lifn_family = AF_UNSPEC; + lifn.lifn_flags = 0; + if (ioctl(sd, SIOCGLIFNUM, &lifn) < 0) goto error; + + /* Sun and Apple code likes to pad the interface count here in case interfaces + * are coming up between calls */ + lifn.lifn_count += 4; + + ifc.lifc_family = AF_UNSPEC; + ifc.lifc_len = lifn.lifn_count * sizeof(struct lifreq); + ifc.lifc_buf = calloc(1, ifc.lifc_len); + if (ioctl(sd, SIOCGLIFCONF, &ifc) < 0) goto error; + + ccp = (char *)ifc.lifc_req; + ecp = ccp + ifc.lifc_len; + + while (ccp < ecp) { + + ifr = (struct lifreq *) ccp; + cifa = (struct ifaddrs *) calloc(1, sizeof(struct ifaddrs)); + cifa->ifa_next = NULL; + cifa->ifa_name = strdup(ifr->lifr_name); + + if (pifa == NULL) *ifap = cifa; /* first one */ + else pifa->ifa_next = cifa; + + if (ioctl(sd, SIOCGLIFADDR, ifr, IFREQSZ) < 0) goto error; + cifa->ifa_addr = sa_dup((struct sockaddr*)&ifr->lifr_addr); + + if (ioctl(sd, SIOCGLIFNETMASK, ifr, IFREQSZ) < 0) goto error; + cifa->ifa_netmask = sa_dup((struct sockaddr*)&ifr->lifr_addr); + + cifa->ifa_flags = 0; + cifa->ifa_dstaddr = NULL; + + if (0 == ioctl(sd, SIOCGLIFFLAGS, ifr)) /* optional */ + cifa->ifa_flags = ifr->lifr_flags; + + if (ioctl(sd, SIOCGLIFDSTADDR, ifr, IFREQSZ) < 0) { + if (0 == ioctl(sd, SIOCGLIFBRDADDR, ifr, IFREQSZ)) + cifa->ifa_dstaddr = sa_dup((struct sockaddr*)&ifr->lifr_addr); + } + else cifa->ifa_dstaddr = sa_dup((struct sockaddr*)&ifr->lifr_addr); + + pifa = cifa; + ccp += IFREQSZ; + } + free(ifc.lifc_buf); + close(sd); + return 0; +error: + if (ifc.lifc_buf != NULL) free(ifc.lifc_buf); + if (sd != -1) close(sd); + return (-1); +} diff --git a/psutil/arch/solaris10/ifaddrs.h b/psutil/arch/solaris10/ifaddrs.h new file mode 100644 index 000000000..49a57ea79 --- /dev/null +++ b/psutil/arch/solaris10/ifaddrs.h @@ -0,0 +1,26 @@ +/* Reference: https://lists.samba.org/archive/samba-technical/2009-February/063079.html */ + + +#ifndef __IFADDRS_H___ +#define __IFADDRS_H___ + +#include +#include + +#undef ifa_dstaddr +#undef ifa_broadaddr +#define ifa_broadaddr ifa_dstaddr + +struct ifaddrs { + struct ifaddrs *ifa_next; + char *ifa_name; + unsigned int ifa_flags; + struct sockaddr *ifa_addr; + struct sockaddr *ifa_netmask; + struct sockaddr *ifa_dstaddr; +}; + +extern int getifaddrs(struct ifaddrs **); +extern void freeifaddrs(struct ifaddrs *); + +#endif \ No newline at end of file diff --git a/setup.py b/setup.py index b575db965..7cc669077 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,7 @@ import os import sys import tempfile +import platform try: from setuptools import setup, Extension except ImportError: @@ -64,13 +65,15 @@ def write(self, s): # POSIX if os.name == 'posix': - libraries = [] - if sys.platform.startswith("sunos"): - libraries.append('socket') posix_extension = Extension( 'psutil._psutil_posix', - sources=['psutil/_psutil_posix.c'], - libraries=libraries) + sources=['psutil/_psutil_posix.c']) + if sys.platform.startswith("sunos"): + posix_extension.libraries.append('socket') + if platform.release() == '5.10': + posix_extension.sources.append('psutil/arch/solaris10/ifaddrs.c') + posix_extension.define_macros.append(('_SUNOS10', 1)) + # Windows if sys.platform.startswith("win32"): From 5fac9633d8e9e651f625747da41b0a6642b8585b Mon Sep 17 00:00:00 2001 From: Arnon Yaari Date: Sun, 22 Mar 2015 15:37:06 +0200 Subject: [PATCH 3/8] swap -k is not supported in Solaris 10 --- psutil/_pssunos.py | 10 ++++------ test/_sunos.py | 8 +++----- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/psutil/_pssunos.py b/psutil/_pssunos.py index ae7d533b6..10e439a46 100644 --- a/psutil/_pssunos.py +++ b/psutil/_pssunos.py @@ -95,13 +95,13 @@ def swap_memory(): # ...nevertheless I can't manage to obtain the same numbers as 'swap' # cmdline utility, so let's parse its output (sigh!) p = subprocess.Popen(['/usr/bin/env', 'PATH=/usr/sbin:/sbin:%s' % - os.environ['PATH'], 'swap', '-l', '-k'], + os.environ['PATH'], 'swap', '-l'], stdout=subprocess.PIPE) stdout, stderr = p.communicate() if PY3: stdout = stdout.decode(sys.stdout.encoding) if p.returncode != 0: - raise RuntimeError("'swap -l -k' failed (retcode=%s)" % p.returncode) + raise RuntimeError("'swap -l' failed (retcode=%s)" % p.returncode) lines = stdout.strip().split('\n')[1:] if not lines: @@ -110,10 +110,8 @@ def swap_memory(): for line in lines: line = line.split() t, f = line[-2:] - t = t.replace('K', '') - f = f.replace('K', '') - total += int(int(t) * 1024) - free += int(int(f) * 1024) + total += int(int(t) * 512) + free += int(int(f) * 512) used = total - free percent = usage_percent(used, total, _round=1) return _common.sswap(total, used, free, percent, diff --git a/test/_sunos.py b/test/_sunos.py index 3d54ccd8c..7520afc27 100644 --- a/test/_sunos.py +++ b/test/_sunos.py @@ -17,7 +17,7 @@ class SunOSSpecificTestCase(unittest.TestCase): def test_swap_memory(self): - out = sh('env PATH=/usr/sbin:/sbin:%s swap -l -k' % os.environ['PATH']) + out = sh('env PATH=/usr/sbin:/sbin:%s swap -l' % os.environ['PATH']) lines = out.strip().split('\n')[1:] if not lines: raise ValueError('no swap device(s) configured') @@ -25,10 +25,8 @@ def test_swap_memory(self): for line in lines: line = line.split() t, f = line[-2:] - t = t.replace('K', '') - f = f.replace('K', '') - total += int(int(t) * 1024) - free += int(int(f) * 1024) + total += int(int(t) * 512) + free += int(int(f) * 512) used = total - free psutil_swap = psutil.swap_memory() From 5103137e2a35df8ce3173fac81c0bcdd99a8a777 Mon Sep 17 00:00:00 2001 From: Arnon Yaari Date: Sun, 22 Mar 2015 15:51:54 +0200 Subject: [PATCH 4/8] fix test on Solaris --- test/test_psutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_psutil.py b/test/test_psutil.py index 94b330ee6..44199d01a 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -755,7 +755,7 @@ def test_cpu_count(self): self.assertEqual(logical, len(psutil.cpu_times(percpu=True))) self.assertGreaterEqual(logical, 1) # - if LINUX: + if os.path.exists("/proc/cpuinfo"): with open("/proc/cpuinfo") as fd: cpuinfo_data = fd.read() if "physical id" not in cpuinfo_data: From 7845393617bf61ae58b04478edc81306cd474e83 Mon Sep 17 00:00:00 2001 From: Arnon Yaari Date: Sun, 22 Mar 2015 16:16:21 +0200 Subject: [PATCH 5/8] fix test on Solaris os.path.exists will fail if the link is broken. see comment about errno.ENOENT in memory_maps in _pssunos.py --- test/test_psutil.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_psutil.py b/test/test_psutil.py index 44199d01a..a08776b46 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1645,7 +1645,8 @@ def test_memory_maps(self): if not nt.path.startswith('['): assert os.path.isabs(nt.path), nt.path if POSIX: - assert os.path.exists(nt.path), nt.path + assert os.path.exists(nt.path) or \ + os.path.islink(nt.path), nt.path else: # XXX - On Windows we have this strange behavior with # 64 bit dlls: they are visible via explorer but cannot From 5cba1ef72caef295276e6c27b3ac415b4570a826 Mon Sep 17 00:00:00 2001 From: Arnon Yaari Date: Sun, 22 Mar 2015 16:31:22 +0200 Subject: [PATCH 6/8] fix test on Solaris --- test/test_psutil.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/test_psutil.py b/test/test_psutil.py index a08776b46..c15b3a853 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1385,7 +1385,10 @@ def test_create_time(self): def test_terminal(self): terminal = psutil.Process().terminal() if sys.stdin.isatty(): - self.assertEqual(terminal, sh('tty')) + tty = sh('tty') + if os.path.islink(tty): + tty = os.path.abspath(os.readlink(tty)) + self.assertEqual(terminal, tty) else: assert terminal, repr(terminal) From d7df8437d2a8dfb0d91894628117a958bfdc6c0d Mon Sep 17 00:00:00 2001 From: Arnon Yaari Date: Sun, 22 Mar 2015 16:36:54 +0200 Subject: [PATCH 7/8] fix test on Solaris --- test/_posix.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/_posix.py b/test/_posix.py index e6c56aac3..63db0d7e4 100644 --- a/test/_posix.py +++ b/test/_posix.py @@ -150,11 +150,12 @@ def test_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime if SUNOS: - cmd = ["ps", "ax"] + cmd = ["ps", "-A", "-o", "pid"] else: cmd = ["ps", "ax", "-o", "pid"] p = get_test_subprocess(cmd, stdout=subprocess.PIPE) output = p.communicate()[0].strip() + assert p.poll() == 0 if PY3: output = str(output, sys.stdout.encoding) pids_ps = [] From e91f722d671e38a4c0a301d0439b0c68f2881993 Mon Sep 17 00:00:00 2001 From: Arnon Yaari Date: Sat, 5 Sep 2015 20:00:09 +0300 Subject: [PATCH 8/8] Code review fixes --- CREDITS | 4 + HISTORY.rst | 1 + psutil/_psutil_posix.c | 2 +- psutil/arch/solaris/v10/ifaddrs.c | 124 ++++++++++++++++++ .../arch/{solaris10 => solaris/v10}/ifaddrs.h | 12 +- psutil/arch/solaris10/ifaddrs.c | 117 ----------------- setup.py | 4 +- test/test_psutil.py | 4 +- 8 files changed, 139 insertions(+), 129 deletions(-) create mode 100644 psutil/arch/solaris/v10/ifaddrs.c rename psutil/arch/{solaris10 => solaris/v10}/ifaddrs.h (66%) delete mode 100644 psutil/arch/solaris10/ifaddrs.c diff --git a/CREDITS b/CREDITS index 60faa7497..32977c63b 100644 --- a/CREDITS +++ b/CREDITS @@ -325,3 +325,7 @@ I: 670 N: maozguttman W: https://github.com/maozguttman I: 659 + +N: wiggin15 +W: https://github.com/wiggin15 +I: 607, 610 diff --git a/HISTORY.rst b/HISTORY.rst index 76821e929..051efd3d0 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -6,6 +6,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues **Bug fixes** - #677: [Linux] can't install psutil due to bug in setup.py. +- #610: [SunOS] fix build and tests on Solaris 10 3.2.0 - 2015-09-02 diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c index b46bac0d7..1e27cdb00 100644 --- a/psutil/_psutil_posix.c +++ b/psutil/_psutil_posix.c @@ -15,7 +15,7 @@ #include #include -#ifdef _SUNOS10 +#ifdef PSUTIL_SUNOS10 #include "arch/solaris10/ifaddrs.h" #else #include diff --git a/psutil/arch/solaris/v10/ifaddrs.c b/psutil/arch/solaris/v10/ifaddrs.c new file mode 100644 index 000000000..59529e6af --- /dev/null +++ b/psutil/arch/solaris/v10/ifaddrs.c @@ -0,0 +1,124 @@ +/* Refrences: + * https://lists.samba.org/archive/samba-technical/2009-February/063079.html + * http://stackoverflow.com/questions/4139405/#4139811 + * https://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/getifaddrs.c + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ifaddrs.h" + +#define MAX(x,y) ((x)>(y)?(x):(y)) +#define SIZE(p) MAX((p).ss_len,sizeof(p)) + + +static struct sockaddr * +sa_dup (struct sockaddr *sa1) +{ + struct sockaddr *sa2; + size_t sz = sizeof(sa1); + sa2 = (struct sockaddr *) calloc(1,sz); + memcpy(sa2,sa1,sz); + return(sa2); +} + + +void freeifaddrs (struct ifaddrs *ifp) +{ + if (NULL == ifp) return; + free(ifp->ifa_name); + free(ifp->ifa_addr); + free(ifp->ifa_netmask); + free(ifp->ifa_dstaddr); + freeifaddrs(ifp->ifa_next); + free(ifp); +} + + +int getifaddrs (struct ifaddrs **ifap) +{ + int sd = -1; + char *ccp, *ecp; + struct lifconf ifc; + struct lifreq *ifr; + struct lifnum lifn; + struct ifaddrs *cifa = NULL; /* current */ + struct ifaddrs *pifa = NULL; /* previous */ + const size_t IFREQSZ = sizeof(struct lifreq); + + sd = socket(AF_INET, SOCK_STREAM, 0); + if (sd < 0) + goto error; + + ifc.lifc_buf = NULL; + *ifap = NULL; + /* find how much memory to allocate for the SIOCGLIFCONF call */ + lifn.lifn_family = AF_UNSPEC; + lifn.lifn_flags = 0; + if (ioctl(sd, SIOCGLIFNUM, &lifn) < 0) + goto error; + + /* Sun and Apple code likes to pad the interface count here in case interfaces + * are coming up between calls */ + lifn.lifn_count += 4; + + ifc.lifc_family = AF_UNSPEC; + ifc.lifc_len = lifn.lifn_count * sizeof(struct lifreq); + ifc.lifc_buf = calloc(1, ifc.lifc_len); + if (ioctl(sd, SIOCGLIFCONF, &ifc) < 0) + goto error; + + ccp = (char *)ifc.lifc_req; + ecp = ccp + ifc.lifc_len; + + while (ccp < ecp) { + + ifr = (struct lifreq *) ccp; + cifa = (struct ifaddrs *) calloc(1, sizeof(struct ifaddrs)); + cifa->ifa_next = NULL; + cifa->ifa_name = strdup(ifr->lifr_name); + + if (pifa == NULL) *ifap = cifa; /* first one */ + else pifa->ifa_next = cifa; + + if (ioctl(sd, SIOCGLIFADDR, ifr, IFREQSZ) < 0) + goto error; + cifa->ifa_addr = sa_dup((struct sockaddr*)&ifr->lifr_addr); + + if (ioctl(sd, SIOCGLIFNETMASK, ifr, IFREQSZ) < 0) + goto error; + cifa->ifa_netmask = sa_dup((struct sockaddr*)&ifr->lifr_addr); + + cifa->ifa_flags = 0; + cifa->ifa_dstaddr = NULL; + + if (0 == ioctl(sd, SIOCGLIFFLAGS, ifr)) /* optional */ + cifa->ifa_flags = ifr->lifr_flags; + + if (ioctl(sd, SIOCGLIFDSTADDR, ifr, IFREQSZ) < 0) { + if (0 == ioctl(sd, SIOCGLIFBRDADDR, ifr, IFREQSZ)) + cifa->ifa_dstaddr = sa_dup((struct sockaddr*)&ifr->lifr_addr); + } + else cifa->ifa_dstaddr = sa_dup((struct sockaddr*)&ifr->lifr_addr); + + pifa = cifa; + ccp += IFREQSZ; + } + free(ifc.lifc_buf); + close(sd); + return 0; +error: + if (ifc.lifc_buf != NULL) + free(ifc.lifc_buf); + if (sd != -1) + close(sd); + return (-1); +} diff --git a/psutil/arch/solaris10/ifaddrs.h b/psutil/arch/solaris/v10/ifaddrs.h similarity index 66% rename from psutil/arch/solaris10/ifaddrs.h rename to psutil/arch/solaris/v10/ifaddrs.h index 49a57ea79..e1d885963 100644 --- a/psutil/arch/solaris10/ifaddrs.h +++ b/psutil/arch/solaris/v10/ifaddrs.h @@ -12,12 +12,12 @@ #define ifa_broadaddr ifa_dstaddr struct ifaddrs { - struct ifaddrs *ifa_next; - char *ifa_name; - unsigned int ifa_flags; - struct sockaddr *ifa_addr; - struct sockaddr *ifa_netmask; - struct sockaddr *ifa_dstaddr; + struct ifaddrs *ifa_next; + char *ifa_name; + unsigned int ifa_flags; + struct sockaddr *ifa_addr; + struct sockaddr *ifa_netmask; + struct sockaddr *ifa_dstaddr; }; extern int getifaddrs(struct ifaddrs **); diff --git a/psutil/arch/solaris10/ifaddrs.c b/psutil/arch/solaris10/ifaddrs.c deleted file mode 100644 index 535f815a9..000000000 --- a/psutil/arch/solaris10/ifaddrs.c +++ /dev/null @@ -1,117 +0,0 @@ -/* Refrences: - * https://lists.samba.org/archive/samba-technical/2009-February/063079.html - * http://stackoverflow.com/questions/4139405/#4139811 - * https://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/getifaddrs.c - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "ifaddrs.h" - -#define MAX(x,y) ((x)>(y)?(x):(y)) -#define SIZE(p) MAX((p).ss_len,sizeof(p)) - - -static struct sockaddr * -sa_dup (struct sockaddr *sa1) -{ - struct sockaddr *sa2; - size_t sz = sizeof(sa1); - sa2 = (struct sockaddr *) calloc(1,sz); - memcpy(sa2,sa1,sz); - return(sa2); -} - - -void freeifaddrs (struct ifaddrs *ifp) -{ - if (NULL == ifp) return; - free(ifp->ifa_name); - free(ifp->ifa_addr); - free(ifp->ifa_netmask); - free(ifp->ifa_dstaddr); - freeifaddrs(ifp->ifa_next); - free(ifp); -} - - -int getifaddrs (struct ifaddrs **ifap) -{ - int sd = -1; - char *ccp, *ecp; - struct lifconf ifc; - struct lifreq *ifr; - struct lifnum lifn; - struct ifaddrs *cifa = NULL; /* current */ - struct ifaddrs *pifa = NULL; /* previous */ - const size_t IFREQSZ = sizeof(struct lifreq); - - sd = socket(AF_INET, SOCK_STREAM, 0); - if (sd < 0) goto error; - - ifc.lifc_buf = NULL; - *ifap = NULL; - /* find how much memory to allocate for the SIOCGLIFCONF call */ - lifn.lifn_family = AF_UNSPEC; - lifn.lifn_flags = 0; - if (ioctl(sd, SIOCGLIFNUM, &lifn) < 0) goto error; - - /* Sun and Apple code likes to pad the interface count here in case interfaces - * are coming up between calls */ - lifn.lifn_count += 4; - - ifc.lifc_family = AF_UNSPEC; - ifc.lifc_len = lifn.lifn_count * sizeof(struct lifreq); - ifc.lifc_buf = calloc(1, ifc.lifc_len); - if (ioctl(sd, SIOCGLIFCONF, &ifc) < 0) goto error; - - ccp = (char *)ifc.lifc_req; - ecp = ccp + ifc.lifc_len; - - while (ccp < ecp) { - - ifr = (struct lifreq *) ccp; - cifa = (struct ifaddrs *) calloc(1, sizeof(struct ifaddrs)); - cifa->ifa_next = NULL; - cifa->ifa_name = strdup(ifr->lifr_name); - - if (pifa == NULL) *ifap = cifa; /* first one */ - else pifa->ifa_next = cifa; - - if (ioctl(sd, SIOCGLIFADDR, ifr, IFREQSZ) < 0) goto error; - cifa->ifa_addr = sa_dup((struct sockaddr*)&ifr->lifr_addr); - - if (ioctl(sd, SIOCGLIFNETMASK, ifr, IFREQSZ) < 0) goto error; - cifa->ifa_netmask = sa_dup((struct sockaddr*)&ifr->lifr_addr); - - cifa->ifa_flags = 0; - cifa->ifa_dstaddr = NULL; - - if (0 == ioctl(sd, SIOCGLIFFLAGS, ifr)) /* optional */ - cifa->ifa_flags = ifr->lifr_flags; - - if (ioctl(sd, SIOCGLIFDSTADDR, ifr, IFREQSZ) < 0) { - if (0 == ioctl(sd, SIOCGLIFBRDADDR, ifr, IFREQSZ)) - cifa->ifa_dstaddr = sa_dup((struct sockaddr*)&ifr->lifr_addr); - } - else cifa->ifa_dstaddr = sa_dup((struct sockaddr*)&ifr->lifr_addr); - - pifa = cifa; - ccp += IFREQSZ; - } - free(ifc.lifc_buf); - close(sd); - return 0; -error: - if (ifc.lifc_buf != NULL) free(ifc.lifc_buf); - if (sd != -1) close(sd); - return (-1); -} diff --git a/setup.py b/setup.py index 7cc669077..d0d970d9b 100644 --- a/setup.py +++ b/setup.py @@ -71,8 +71,8 @@ def write(self, s): if sys.platform.startswith("sunos"): posix_extension.libraries.append('socket') if platform.release() == '5.10': - posix_extension.sources.append('psutil/arch/solaris10/ifaddrs.c') - posix_extension.define_macros.append(('_SUNOS10', 1)) + posix_extension.sources.append('psutil/arch/solaris/v10/ifaddrs.c') + posix_extension.define_macros.append(('PSUTIL_SUNOS10', 1)) # Windows if sys.platform.startswith("win32"): diff --git a/test/test_psutil.py b/test/test_psutil.py index c15b3a853..0112dc2bf 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1385,9 +1385,7 @@ def test_create_time(self): def test_terminal(self): terminal = psutil.Process().terminal() if sys.stdin.isatty(): - tty = sh('tty') - if os.path.islink(tty): - tty = os.path.abspath(os.readlink(tty)) + tty = os.path.realpath(sh('tty')) self.assertEqual(terminal, tty) else: assert terminal, repr(terminal)