~ubuntu-branches/ubuntu/trusty/python-psutil/trusty-proposed

« back to all changes in this revision

Viewing changes to psutil/_psutil_mswindows.c

  • Committer: Package Import Robot
  • Author(s): Leo Iannacone
  • Date: 2012-01-07 13:18:54 UTC
  • mfrom: (2.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20120107131854-6l22ulo9hwrroa8m
Tags: 0.4.1-1ubuntu1
* Merge with Debian unstable (LP: #913100).  Remaining Ubuntu changes:
  - Switch to dh_python2. (LP: #788514)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: _psutil_mswindows.c 1202 2011-10-24 19:00:50Z g.rodola $
 
2
 * $Id: _psutil_mswindows.c 1238 2011-12-14 19:19:13Z g.rodola $
3
3
 *
4
4
 * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved.
5
5
 * Use of this source code is governed by a BSD-style license that can be
481
481
        CloseHandle(hProcess);
482
482
        return PyErr_SetFromWindowsErr(0);
483
483
    }
484
 
    
 
484
 
 
485
    CloseHandle(hProcess);
 
486
 
485
487
// py 2.4
486
488
#if (PY_MAJOR_VERSION == 2) && (PY_MINOR_VERSION <= 4)
487
489
    return Py_BuildValue("(II)", (unsigned int)counters.WorkingSetSize,
787
789
        }
788
790
    } while (Thread32Next(hThreadSnap, &te32));
789
791
 
 
792
    CloseHandle(hThreadSnap);
790
793
    return TRUE;
791
794
}
792
795
 
1218
1221
} MIB_UDP6TABLE_OWNER_PID, *PMIB_UDP6TABLE_OWNER_PID;
1219
1222
 
1220
1223
 
 
1224
#define ConnDecrefPyObjs() Py_DECREF(_AF_INET); \
 
1225
                           Py_DECREF(_AF_INET6);\
 
1226
                           Py_DECREF(_SOCK_STREAM);\
 
1227
                           Py_DECREF(_SOCK_DGRAM);
 
1228
 
1221
1229
/*
1222
1230
 * Return a list of network connections opened by a process
1223
1231
 */
1231
1239
    PyObject* connectionTuple;
1232
1240
    PyObject *af_filter = NULL;
1233
1241
    PyObject *type_filter = NULL;
 
1242
 
 
1243
    PyObject *_AF_INET = PyLong_FromLong((long)AF_INET);
 
1244
    PyObject *_AF_INET6 = PyLong_FromLong((long)AF_INET6);
 
1245
    PyObject *_SOCK_STREAM = PyLong_FromLong((long)SOCK_STREAM);
 
1246
    PyObject *_SOCK_DGRAM = PyLong_FromLong((long)SOCK_DGRAM);
 
1247
 
1234
1248
    typedef PSTR (NTAPI *_RtlIpv4AddressToStringA)(struct in_addr *,
1235
1249
                                                   PSTR /* __out_ecount(16) */);
1236
1250
    _RtlIpv4AddressToStringA rtlIpv4AddressToStringA;
1256
1270
    PyObject* addressTupleRemote;
1257
1271
 
1258
1272
    if (! PyArg_ParseTuple(args, "lOO", &pid, &af_filter, &type_filter)) {
 
1273
        ConnDecrefPyObjs();
1259
1274
        return NULL;
1260
1275
    }
1261
1276
 
1262
1277
    if (!PySequence_Check(af_filter) || !PySequence_Check(type_filter)) {
 
1278
        ConnDecrefPyObjs();
1263
1279
        PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence");
1264
1280
        return NULL;
1265
1281
    }
1266
1282
 
1267
1283
    if (pid_is_running(pid) == 0) {
 
1284
        ConnDecrefPyObjs();
1268
1285
        return NoSuchProcess();
1269
1286
    }
1270
1287
 
1290
1307
    if ((getExtendedTcpTable == NULL) || (getExtendedUdpTable == NULL)) {
1291
1308
        PyErr_SetString(PyExc_NotImplementedError,
1292
1309
                        "feature not supported on this Windows version");
 
1310
        ConnDecrefPyObjs();
1293
1311
        return NULL;
1294
1312
    }
1295
1313
 
1297
1315
 
1298
1316
    /* TCP IPv4 */
1299
1317
 
1300
 
    if ((PySequence_Contains(af_filter, PyLong_FromLong((long)AF_INET)) == 1) &&
1301
 
        (PySequence_Contains(type_filter, PyLong_FromLong((long)SOCK_STREAM)) == 1))
 
1318
    if ((PySequence_Contains(af_filter, _AF_INET) == 1) &&
 
1319
        (PySequence_Contains(type_filter, _SOCK_STREAM) == 1))
1302
1320
    {
1303
1321
        tableSize = 0;
1304
1322
        getExtendedTcpTable(NULL, &tableSize, FALSE, AF_INET,
1359
1377
                    state_to_string(tcp4Table->table[i].dwState)
1360
1378
                    );
1361
1379
                PyList_Append(connectionsList, connectionTuple);
 
1380
                Py_DECREF(connectionTuple);
1362
1381
            }
1363
1382
        }
1364
1383
 
1367
1386
 
1368
1387
    /* TCP IPv6 */
1369
1388
 
1370
 
    if ((PySequence_Contains(af_filter, PyLong_FromLong((long)AF_INET6)) == 1) &&
1371
 
        (PySequence_Contains(type_filter, PyLong_FromLong((long)SOCK_STREAM)) == 1))
 
1389
    if ((PySequence_Contains(af_filter, _AF_INET6) == 1) &&
 
1390
        (PySequence_Contains(type_filter, _SOCK_STREAM) == 1))
1372
1391
    {
1373
1392
        tableSize = 0;
1374
1393
        getExtendedTcpTable(NULL, &tableSize, FALSE, AF_INET6,
1429
1448
                    state_to_string(tcp6Table->table[i].dwState)
1430
1449
                    );
1431
1450
                PyList_Append(connectionsList, connectionTuple);
 
1451
                Py_DECREF(connectionTuple);
1432
1452
            }
1433
1453
        }
1434
1454
 
1437
1457
 
1438
1458
    /* UDP IPv4 */
1439
1459
 
1440
 
    if ((PySequence_Contains(af_filter, PyLong_FromLong((long)AF_INET)) == 1) &&
1441
 
        (PySequence_Contains(type_filter, PyLong_FromLong((long)SOCK_DGRAM)) == 1))
 
1460
    if ((PySequence_Contains(af_filter, _AF_INET) == 1) &&
 
1461
        (PySequence_Contains(type_filter, _SOCK_DGRAM) == 1))
1442
1462
    {
1443
1463
        tableSize = 0;
1444
1464
        getExtendedUdpTable(NULL, &tableSize, FALSE, AF_INET,
1481
1501
                    ""
1482
1502
                    );
1483
1503
                PyList_Append(connectionsList, connectionTuple);
 
1504
                Py_DECREF(connectionTuple);
1484
1505
            }
1485
1506
        }
1486
1507
 
1489
1510
 
1490
1511
    /* UDP IPv6 */
1491
1512
 
1492
 
    if ((PySequence_Contains(af_filter, PyLong_FromLong((long)AF_INET6)) == 1) &&
1493
 
        (PySequence_Contains(type_filter, PyLong_FromLong((long)SOCK_DGRAM)) == 1))
 
1513
    if ((PySequence_Contains(af_filter, _AF_INET6) == 1) &&
 
1514
        (PySequence_Contains(type_filter, _SOCK_DGRAM) == 1))
1494
1515
    {
1495
1516
        tableSize = 0;
1496
1517
        getExtendedUdpTable(NULL, &tableSize, FALSE,
1533
1554
                    ""
1534
1555
                    );
1535
1556
                PyList_Append(connectionsList, connectionTuple);
 
1557
                Py_DECREF(connectionTuple);
1536
1558
            }
1537
1559
        }
1538
1560
 
1539
1561
        free(table);
1540
1562
    }
1541
1563
 
 
1564
    ConnDecrefPyObjs();
1542
1565
    return connectionsList;
1543
1566
}
1544
1567
 
1706
1729
    PyObject* py_nic_info = NULL;
1707
1730
    PyObject* py_pre_nic_name = NULL;
1708
1731
    PyObject* py_nic_name = NULL;
1709
 
    
 
1732
 
1710
1733
    int attempts = 0;
1711
1734
    int outBufLen = 15000;
1712
1735
    DWORD dwRetVal = 0;
1715
1738
    ULONG family = AF_UNSPEC;
1716
1739
    PIP_ADAPTER_ADDRESSES pAddresses = NULL;
1717
1740
    PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
1718
 
 
1719
 
    do {        
 
1741
 
 
1742
    do {
1720
1743
        pAddresses = (IP_ADAPTER_ADDRESSES *) malloc(outBufLen);
1721
1744
        if (pAddresses == NULL) {
1722
1745
            Py_DECREF(py_retdict);
1723
 
            PyErr_SetString(PyExc_RuntimeError, 
 
1746
            PyErr_SetString(PyExc_RuntimeError,
1724
1747
                "memory allocation failed for IP_ADAPTER_ADDRESSES struct.");
1725
1748
            return NULL;
1726
1749
        }
1727
1750
 
1728
 
        dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, 
 
1751
        dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses,
1729
1752
                                        &outBufLen);
1730
1753
        if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
1731
1754
            free(pAddresses);
1732
1755
            pAddresses = NULL;
1733
 
        } 
 
1756
        }
1734
1757
        else {
1735
1758
            break;
1736
1759
        }
1743
1766
        PyErr_SetString(PyExc_RuntimeError,  "GetAdaptersAddresses() failed.");
1744
1767
        return NULL;
1745
1768
    }
1746
 
    
 
1769
 
1747
1770
    pCurrAddresses = pAddresses;
1748
1771
    while (pCurrAddresses) {
1749
1772
        pIfRow = (MIB_IFROW *) malloc(sizeof(MIB_IFROW));
1750
 
        
 
1773
 
1751
1774
        if (pIfRow == NULL) {
1752
1775
            Py_DECREF(py_retdict);
1753
1776
            Py_XDECREF(py_pre_nic_name);
1754
1777
            Py_XDECREF(py_nic_name);
1755
1778
            Py_XDECREF(py_nic_info);
1756
 
            PyErr_SetString(PyExc_RuntimeError, 
 
1779
            PyErr_SetString(PyExc_RuntimeError,
1757
1780
                "memory allocation failed for MIB_IFROW struct.");
1758
1781
            return NULL;
1759
1782
        }
1765
1788
            Py_XDECREF(py_pre_nic_name);
1766
1789
            Py_XDECREF(py_nic_name);
1767
1790
            Py_XDECREF(py_nic_info);
1768
 
            PyErr_SetString(PyExc_RuntimeError, 
 
1791
            PyErr_SetString(PyExc_RuntimeError,
1769
1792
                "GetIfEntry() failed.");
1770
1793
            return NULL;
1771
1794
        }
1772
 
        
 
1795
 
1773
1796
        py_nic_info = Py_BuildValue("(IIII)",
1774
1797
                                    pIfRow->dwOutOctets,
1775
1798
                                    pIfRow->dwInOctets,
1776
1799
                                    pIfRow->dwOutUcastPkts,
1777
 
                                    pIfRow->dwInUcastPkts);          
1778
 
                          
 
1800
                                    pIfRow->dwInUcastPkts);
 
1801
 
1779
1802
        py_pre_nic_name = PyUnicode_FromWideChar(
1780
1803
                                pCurrAddresses->FriendlyName,
1781
1804
                                wcslen(pCurrAddresses->FriendlyName));
1816
1839
        hDevice = CreateFile (szDevice, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
1817
1840
                              NULL, OPEN_EXISTING, 0, NULL);
1818
1841
 
1819
 
        if (hDevice == INVALID_HANDLE_VALUE)
 
1842
        if (hDevice == INVALID_HANDLE_VALUE) {
 
1843
            // what happens if we get an invalid handle on the first disk?
 
1844
            // we might end up with an empty dict incorrectly in some cases
1820
1845
            break;
 
1846
        }
1821
1847
 
1822
1848
        if (DeviceIoControl (hDevice, IOCTL_DISK_PERFORMANCE, NULL, 0,
1823
1849
            &diskPerformance, sizeof (DISK_PERFORMANCE), &dwSize, NULL)) {