summaryrefslogtreecommitdiff
path: root/agent/mibgroup/host/hr_swrun.c
blob: 644e7bdca5b3065eb452359fe98abec6af7f60d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
/* Portions of this file are subject to the following copyright(s).  See
 * the Net-SNMP's COPYING file for more details and other copyrights
 * that may apply:
 */
/*
 * Portions of this file are copyrighted by:
 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms specified in the COPYING file
 * distributed with the Net-SNMP package.
 */

/*
 *  Host Resources MIB - Running Software group implementation - hr_swrun.c
 *      (also includes Running Software Performance group )
 *
 */

#include <net-snmp/net-snmp-config.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <fcntl.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <sys/param.h>
#include <ctype.h>
#if HAVE_SYS_PSTAT_H
#include <sys/pstat.h>
#endif
#if HAVE_SYS_USER_H
#ifdef solaris2
#include <libgen.h>
#define _KMEMUSER
#endif
#include <sys/user.h>
#endif
#if HAVE_SYS_PROC_H
#include <sys/proc.h>
#endif
#if HAVE_KVM_H
#include <kvm.h>
#endif
#if HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#if HAVE_DIRENT_H && !defined(cygwin)
#include <dirent.h>
#else
# define dirent direct
# if HAVE_SYS_NDIR_H
#  include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
#  include <sys/dir.h>
# endif
# if HAVE_NDIR_H
#  include <ndir.h>
# endif
#endif
#ifdef cygwin
#include <windows.h>
#include <sys/cygwin.h>
#include <tlhelp32.h>
#include <psapi.h>
#endif

#if _SLASH_PROC_METHOD_
#include <procfs.h>
#endif

#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif

#include <stdio.h>

#include <net-snmp/output_api.h>
#include "host_res.h"
#include "hr_swrun.h"
#include <net-snmp/agent/auto_nlist.h>
#include "kernel.h"
#ifdef solaris2
#if _SLASH_PROC_METHOD_ && defined _ILP32
#include <net-snmp/agent/cache_handler.h>
#include <net-snmp/agent/hardware/memory.h>
#endif

#include "kernel_sunos5.h"
#endif
#if defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
#include <procinfo.h>
#include <sys/types.h>
#endif

        /*********************
	 *
	 *  Initialisation & common implementation functions
	 *
	 *********************/
void            Init_HR_SWRun(void);
int             Get_Next_HR_SWRun(void);
void            End_HR_SWRun(void);
int             header_hrswrun(struct variable *, oid *, size_t *, int,
                               size_t *, WriteMethod **);
int             header_hrswrunEntry(struct variable *, oid *, size_t *,
                                    int, size_t *, WriteMethod **);

#ifdef dynix
pid_t           nextproc;
static prpsinfo_t lowpsinfo, mypsinfo;
#endif
#ifdef cygwin
static struct external_pinfo *curproc;
static struct external_pinfo lowproc;
#elif !defined(linux)
static int      LowProcIndex;
#endif
#if defined(hpux10) || defined(hpux11)
struct pst_status *proc_table;
struct pst_dynamic pst_dyn;
#elif HAVE_KVM_GETPROC2
struct kinfo_proc2 *proc_table;
#elif HAVE_KVM_GETPROCS
struct kinfo_proc *proc_table;
#elif defined(solaris2)
int            *proc_table;
#elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
struct procsinfo *proc_table;
#else
struct proc    *proc_table;
#endif
#ifndef dynix
int             current_proc_entry;
#endif


#define	HRSWRUN_OSINDEX		1

#define	HRSWRUN_INDEX		2
#define	HRSWRUN_NAME		3
#define	HRSWRUN_ID		4
#define	HRSWRUN_PATH		5
#define	HRSWRUN_PARAMS		6
#define	HRSWRUN_TYPE		7
#define	HRSWRUN_STATUS		8

#define	HRSWRUNPERF_CPU		9
#define	HRSWRUNPERF_MEM		10

struct variable4 hrswrun_variables[] = {
    {HRSWRUN_OSINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
     var_hrswrun, 1, {1}},
    {HRSWRUN_INDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
     var_hrswrun, 3, {2, 1, 1}},
    {HRSWRUN_NAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
     var_hrswrun, 3, {2, 1, 2}},
    {HRSWRUN_ID, ASN_OBJECT_ID, NETSNMP_OLDAPI_RONLY,
     var_hrswrun, 3, {2, 1, 3}},
    {HRSWRUN_PATH, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
     var_hrswrun, 3, {2, 1, 4}},
    {HRSWRUN_PARAMS, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
     var_hrswrun, 3, {2, 1, 5}},
    {HRSWRUN_TYPE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
     var_hrswrun, 3, {2, 1, 6}},
    {HRSWRUN_STATUS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
     var_hrswrun, 3, {2, 1, 7}}
};

struct variable4 hrswrunperf_variables[] = {
    {HRSWRUNPERF_CPU, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
     var_hrswrun, 3, {1, 1, 1}},
    {HRSWRUNPERF_MEM, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
     var_hrswrun, 3, {1, 1, 2}}
};

oid             hrswrun_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 4 };
oid             hrswrunperf_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 5 };

#ifdef cygwin

/*
 * a lot of this is "stolen" from cygwin ps.cc
 */

typedef         BOOL(WINAPI * ENUMPROCESSMODULES) (HANDLE hProcess,
                                                   HMODULE * lphModule,
                                                   DWORD cb,
                                                   LPDWORD lpcbNeeded);

typedef         DWORD(WINAPI * GETMODULEFILENAME) (HANDLE hProcess,
                                                   HMODULE hModule,
                                                   LPTSTR lpstrFIleName,
                                                   DWORD nSize);

typedef         DWORD(WINAPI * GETPROCESSMEMORYINFO) (HANDLE hProcess,
                                                      PPROCESS_MEMORY_COUNTERS
                                                      pmc, DWORD nSize);

typedef         HANDLE(WINAPI * CREATESNAPSHOT) (DWORD dwFlags,
                                                 DWORD th32ProcessID);

typedef         BOOL(WINAPI * PROCESSWALK) (HANDLE hSnapshot,
                                            LPPROCESSENTRY32 lppe);

ENUMPROCESSMODULES myEnumProcessModules;
GETMODULEFILENAME myGetModuleFileNameEx;
CREATESNAPSHOT  myCreateToolhelp32Snapshot;
PROCESSWALK     myProcess32First;
PROCESSWALK     myProcess32Next;
GETPROCESSMEMORYINFO myGetProcessMemoryInfo = NULL;
cygwin_getinfo_types query = CW_GETPINFO;

static BOOL WINAPI
dummyprocessmodules(HANDLE hProcess,
                    HMODULE * lphModule, DWORD cb, LPDWORD lpcbNeeded)
{
    lphModule[0] = (HMODULE) * lpcbNeeded;
    *lpcbNeeded = 1;
    return 1;
}

static DWORD WINAPI
GetModuleFileNameEx95(HANDLE hProcess,
                      HMODULE hModule, LPTSTR lpstrFileName, DWORD n)
{
    HANDLE          h;
    DWORD           pid = (DWORD) hModule;
    PROCESSENTRY32  proc;

    h = myCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (!h)
        return 0;
    proc.dwSize = sizeof(proc);
    if (myProcess32First(h, &proc))
        do
            if (proc.th32ProcessID == pid) {
                CloseHandle(h);
                strcpy(lpstrFileName, proc.szExeFile);
                return 1;
            }
        while (myProcess32Next(h, &proc));
    CloseHandle(h);
    return 0;
}

#define FACTOR (0x19db1ded53ea710LL)
#define NSPERSEC 10000000LL
#define NSPERMSEC 10000LL

static time_t   __stdcall
to_time_t(PFILETIME ptr)
{
    long            rem;
    long long       x =
        ((long long) ptr->dwHighDateTime << 32) +
        ((unsigned) ptr->dwLowDateTime);
    x -= FACTOR;
    rem = x % NSPERSEC;
    rem += NSPERSEC / 2;
    x /= NSPERSEC;
    x += rem / NSPERSEC;
    return x;
}

static long
to_msec(PFILETIME ptr)
{
    long long       x =
        ((long long) ptr->dwHighDateTime << 32) +
        (unsigned) ptr->dwLowDateTime;
    x /= NSPERMSEC;
    return x;
}

#endif                          /* cygwin */


void
init_hr_swrun(void)
{
#ifdef cygwin
    OSVERSIONINFO   ver;
    HMODULE         h;

    memset(&ver, 0, sizeof ver);
    ver.dwOSVersionInfoSize = sizeof ver;
    GetVersionEx(&ver);

    if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
        h = LoadLibrary("psapi.dll");
        if (h) {
            myEnumProcessModules =
                (ENUMPROCESSMODULES) GetProcAddress(h,
                                                    "EnumProcessModules");
            myGetModuleFileNameEx =
                (GETMODULEFILENAME) GetProcAddress(h,
                                                   "GetModuleFileNameExA");
            myGetProcessMemoryInfo =
                (GETPROCESSMEMORYINFO) GetProcAddress(h,
                                                      "GetProcessMemoryInfo");
            if (myEnumProcessModules && myGetModuleFileNameEx)
                query = CW_GETPINFO_FULL;
            else
                snmp_log(LOG_ERR, "hr_swrun failed NT init\n");
        } else
            snmp_log(LOG_ERR, "hr_swrun failed to load psapi.dll\n");
    } else {
        h = GetModuleHandle("KERNEL32.DLL");
        myCreateToolhelp32Snapshot =
            (CREATESNAPSHOT) GetProcAddress(h, "CreateToolhelp32Snapshot");
        myProcess32First =
            (PROCESSWALK) GetProcAddress(h, "Process32First");
        myProcess32Next = (PROCESSWALK) GetProcAddress(h, "Process32Next");
        myEnumProcessModules = dummyprocessmodules;
        myGetModuleFileNameEx = GetModuleFileNameEx95;
        if (myCreateToolhelp32Snapshot && myProcess32First
            && myProcess32Next)
#if 0
            /*
             * This doesn't work after all on Win98 SE 
             */
            query = CW_GETPINFO_FULL;
#else
            query = CW_GETPINFO;
#endif
        else
            snmp_log(LOG_ERR, "hr_swrun failed non-NT init\n");
    }
#endif                          /* cygwin */
#ifdef PROC_SYMBOL
    auto_nlist(PROC_SYMBOL, 0, 0);
#endif
#ifdef NPROC_SYMBOL
    auto_nlist(NPROC_SYMBOL, 0, 0);
#endif

    proc_table = NULL;

    REGISTER_MIB("host/hr_swrun", hrswrun_variables, variable4,
                 hrswrun_variables_oid);
    REGISTER_MIB("host/hr_swrun", hrswrunperf_variables, variable4,
                 hrswrunperf_variables_oid);
}

/*
 * header_hrswrun(...
 * Arguments:
 * vp     IN      - pointer to variable entry that points here
 * name    IN/OUT  - IN/name requested, OUT/name found
 * length  IN/OUT  - length of IN/OUT oid's 
 * exact   IN      - TRUE if an exact match was requested
 * var_len OUT     - length of variable or 0 if function returned
 * write_method
 * 
 */

int
header_hrswrun(struct variable *vp,
               oid * name,
               size_t * length,
               int exact, size_t * var_len, WriteMethod ** write_method)
{
#define HRSWRUN_NAME_LENGTH	9
    oid             newname[MAX_OID_LEN];
    int             result;

    DEBUGMSGTL(("host/hr_swrun", "var_hrswrun: "));
    DEBUGMSGOID(("host/hr_swrun", name, *length));
    DEBUGMSG(("host/hr_swrun", " %d\n", exact));

    memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));
    newname[HRSWRUN_NAME_LENGTH] = 0;
    result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
    if ((exact && (result != 0)) || (!exact && (result >= 0)))
        return (MATCH_FAILED);
    memcpy((char *) name, (char *) newname,
           (vp->namelen + 1) * sizeof(oid));
    *length = vp->namelen + 1;

    *write_method = (WriteMethod*)0;
    *var_len = sizeof(long);    /* default to 'long' results */
    return (MATCH_SUCCEEDED);
}

int
header_hrswrunEntry(struct variable *vp,
                    oid * name,
                    size_t * length,
                    int exact,
                    size_t * var_len, WriteMethod ** write_method)
{
#define HRSWRUN_ENTRY_NAME_LENGTH	11
    oid             newname[MAX_OID_LEN];
    int             pid, LowPid = -1;
    int             result;

    DEBUGMSGTL(("host/hr_swrun", "var_hrswrunEntry: "));
    DEBUGMSGOID(("host/hr_swrun", name, *length));
    DEBUGMSG(("host/hr_swrun", " %d\n", exact));

    memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));

    /*
     *  Find the "next" running process
     */
    Init_HR_SWRun();
    for (;;) {
        pid = Get_Next_HR_SWRun();
#ifndef linux
#ifndef dynix
        DEBUGMSG(("host/hr_swrun",
                  "(index %d (entry #%d) ....", pid, current_proc_entry));
#else
        DEBUGMSG(("host/hr_swrun", "pid %d; nextproc %d ....", pid,
                  nextproc));
#endif
#endif
        if (pid == -1)
            break;
        newname[HRSWRUN_ENTRY_NAME_LENGTH] = pid;
        DEBUGMSGOID(("host/hr_swrun", newname, *length));
        DEBUGMSG(("host/hr_swrun", "\n"));
        result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
        if (exact && (result == 0)) {
            LowPid = pid;
#ifdef cygwin
            lowproc = *curproc;
#elif  dynix
            memcpy(&lowpsinfo, &mypsinfo, sizeof(prpsinfo_t));
#elif !defined(linux)
            LowProcIndex = current_proc_entry - 1;
#endif
            DEBUGMSGTL(("host/hr_swrun", " saved\n"));
            /*
             * Save process status information 
             */
            break;
        }
        if ((!exact && (result < 0)) && (LowPid == -1 || pid < LowPid)) {
            LowPid = pid;
#ifdef cygwin
            lowproc = *curproc;
#elif !defined(linux)
            LowProcIndex = current_proc_entry - 1;
#endif
            /*
             * Save process status information 
             */
            DEBUGMSG(("host/hr_swrun", " saved"));
        }
        DEBUGMSG(("host/hr_swrun", "\n"));
    }
    End_HR_SWRun();

    if (LowPid == -1) {
        DEBUGMSGTL(("host/hr_swrun", "... index out of range\n"));
        return (MATCH_FAILED);
    }

    newname[HRSWRUN_ENTRY_NAME_LENGTH] = LowPid;
    memcpy((char *) name, (char *) newname,
           (vp->namelen + 1) * sizeof(oid));
    *length = vp->namelen + 1;
    *write_method = (WriteMethod*)0;
    *var_len = sizeof(long);    /* default to 'long' results */

    DEBUGMSGTL(("host/hr_swrun", "... get process stats "));
    DEBUGMSGOID(("host/hr_swrun", name, *length));
    DEBUGMSG(("host/hr_swrun", "\n"));
    return LowPid;
}

        /*********************
	 *
	 *  System specific implementation functions
	 *
	 *********************/

#if defined(linux)
static char     *
skip_to_next_field(char *cp)
{
    while (*cp && ! isspace(*cp)) /* skip past non-space */
	++cp;
    while (*cp && isspace(*cp)) /* skip past space */
	++cp;
    return cp;

}

static char     *
get_proc_file_line(char *fmt,
		    int pid,
		    char *buf,
		    int buflen )
{
    static char     string[1024];
    FILE *fp;
    *buf = '\0';
    sprintf(string,fmt,pid);
    if (   ((fp = fopen(string, "r")) == NULL)
	|| (fgets(buf, buflen, fp) == NULL) ) {
	if (fp)
	    fclose(fp);
	return NULL;
    }
    fclose(fp);
    return buf;
}

static char     *
get_proc_stat_field(int pid,
		    char *buf,
		    int buflen,
		    int skip )
{
    int i;
    char *cp;

    if ((cp = get_proc_file_line("/proc/%d/stat", pid, buf, buflen)) == NULL )
	return NULL;
    for (i = 0; *cp && i < skip; ++i) {
	cp = skip_to_next_field(cp);
    }
    return cp;
}

static char *
get_proc_name_from_cmdline(int pid,
			    char *buf,
			    int buflen )
{
    return get_proc_file_line("/proc/%d/cmdline", pid, buf, buflen);
}

static char *
get_proc_name_from_status(int pid,
	    char *buf,
	    int buflen )
{
    char *cp,*cp2;
    if ((cp = get_proc_file_line("/proc/%d/status", pid, buf, buflen)) == NULL )
	return NULL;
    cp = strchr(cp, ':');
    if ( cp == NULL ) {
	return NULL;    /* the process file is malformed */
    }
    cp = skip_to_next_field(cp);
    cp2 = strchr(cp, '\n');
    if (cp2)
	*cp2 = 0;
    return cp;
}
#endif

u_char         *
var_hrswrun(struct variable * vp,
            oid * name,
            size_t * length,
            int exact, size_t * var_len, WriteMethod ** write_method)
{
    int             pid = 0;
    static char     string[1024];
#ifdef HAVE_SYS_PSTAT_H
    struct pst_status proc_buf;
#elif defined(solaris2)
#if _SLASH_PROC_METHOD_
    static psinfo_t psinfo;
    static psinfo_t *proc_buf;
    int             procfd;
    int             ret;
    char            procfn[sizeof "/proc/00000/psinfo"];
#else
    static struct proc *proc_buf;
    char           *cp1;
#endif                          /* _SLASH_PROC_METHOD_ */
    static time_t   when = 0;
    time_t          now;
    static int      oldpid = -1;
#endif
#if (defined(HAVE_KVM_GETPROCS) || defined(HAVE_KVM_GETPROC2))
    char          **argv;
#endif
#ifdef linux
    FILE           *fp;
    char            buf[1024];
    int             i;
#endif
    char           *cp;

    if (vp->magic == HRSWRUN_OSINDEX) {
        if (header_hrswrun(vp, name, length, exact, var_len, write_method)
            == MATCH_FAILED)
            return NULL;
    } else {

        pid =
            header_hrswrunEntry(vp, name, length, exact, var_len,
                                write_method);
        if (pid == MATCH_FAILED)
            return NULL;
    }

#ifdef HAVE_SYS_PSTAT_H
    if (pstat_getproc(&proc_buf, sizeof(struct pst_status), 0, pid) == -1)
        return NULL;
#elif defined(solaris2)
    time(&now);
    if (pid == oldpid) {
        if (now != when)
            oldpid = -1;
    }
    if (oldpid != pid || proc_buf == NULL) {
#if _SLASH_PROC_METHOD_
        proc_buf = &psinfo;
        sprintf(procfn, "/proc/%.5d/psinfo", pid);
        if ((procfd = open(procfn, O_RDONLY)) != -1) {
            ret =  read(procfd, proc_buf, sizeof(*proc_buf));
            close(procfd);
            if (ret != sizeof(*proc_buf))
                proc_buf = NULL;
        } else
            proc_buf =  NULL;
#else
        if (kd == NULL)
            return NULL;
        if ((proc_buf = kvm_getproc(kd, pid)) == NULL)
            return NULL;
#endif
        oldpid = pid;
        when = now;
    }
#endif

    switch (vp->magic) {
    case HRSWRUN_OSINDEX:
#if NETSNMP_NO_DUMMY_VALUES
        return NULL;
#else
        /* 
         * per dts, on coders:
         * cos (in general) we won't know which process should
         * be regarded as "the primary O/S process".
         * The most obvious candidate on a Unix box is probably 'init'
         * which is typically (always?) process #1.
         */
        long_return = 1;        /* Probably! */
        return (u_char *) & long_return;
#endif

    case HRSWRUN_INDEX:
        long_return = pid;
        return (u_char *) & long_return;
    case HRSWRUN_NAME:
#ifdef HAVE_SYS_PSTAT_H
        strlcpy(string, proc_buf.pst_cmd, sizeof(string));
        cp = strchr(string, ' ');
        if (cp != NULL)
            *cp = '\0';
#elif defined(dynix)
        strlcpy(string, lowpsinfo.pr_fname, sizeof(string));
        cp = strchr(string, ' ');
        if (cp != NULL)
            *cp = '\0';
#elif defined(solaris2)
#if _SLASH_PROC_METHOD_
        if (proc_buf) { 
            char *pos=strchr(proc_buf->pr_psargs,' ');
            if (pos != NULL) *pos = '\0';
            strlcpy(string, basename(proc_buf->pr_psargs), sizeof(string));
            if (pos != NULL) *pos=' ';
        } else {
            strlcpy(string, "<exited>", sizeof(string));
        }
#else
        strlcpy(string, proc_buf->p_user.u_comm, sizeof(string));
#endif
#elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
        strlcpy(string, proc_table[LowProcIndex].pi_comm, sizeof(string));
        cp = strchr(string, ' ');
        if (cp != NULL)
            *cp = '\0';
#elif HAVE_KVM_GETPROC2
        strlcpy(string, proc_table[LowProcIndex].p_comm, sizeof(string));
        /* process name: truncate the string at the first space */
        cp = strchr(string, ' ');
        if (cp != NULL)
            *cp = '\0';
#elif HAVE_KVM_GETPROCS
    #if defined(freebsd5) && __FreeBSD_version >= 500014
        strcpy(string, proc_table[LowProcIndex].ki_comm);
    #elif defined(dragonfly) && __DragonFly_version >= 190000
        strcpy(string, proc_table[LowProcIndex].kp_comm);
    #else
        strcpy(string, proc_table[LowProcIndex].kp_proc.p_comm);
    #endif
#elif defined(linux)
	if( (cp=get_proc_name_from_status(pid,buf,sizeof(buf))) == NULL ) {
            strcpy(string, "<exited>");
            *var_len = strlen(string);
            return (u_char *) string;
        }
        strcpy(string, cp);
#elif defined(cygwin)
        /* if (lowproc.process_state & (PID_ZOMBIE | PID_EXITED)) */
        if (lowproc.process_state & PID_EXITED || (lowproc.exitcode & ~0xffff))
            strcpy(string, "<defunct>");
        else if (lowproc.ppid) {
            cygwin_conv_to_posix_path(lowproc.progname, string);
            cp = strrchr(string, '/');
            if (cp)
                strcpy(string, cp + 1);
        } else if (query == CW_GETPINFO_FULL) {
            DWORD           n = lowproc.dwProcessId & 0xffff;
            HANDLE          h =
                OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                            FALSE, n);

            if (h) {
                HMODULE         hm[1000];
                if (!myEnumProcessModules(h, hm, sizeof hm, &n)) {
                    snmp_log(LOG_DEBUG, "no module handle for %lu\n", n);
                    n = 0;
                }
                if (n
                    && myGetModuleFileNameEx(h, hm[0], string,
                                             sizeof string)) {
                    cp = strrchr(string, '\\');
                    if (cp)
                        strcpy(string, cp + 1);
                } else
                    strcpy(string, "*** unknown");
                CloseHandle(h);
            } else {
                snmp_log(LOG_INFO, "no process handle for %lu\n", n);
                strcpy(string, "** unknown");
            }
        } else
            strcpy(string, "* unknown");
        cp = strchr(string, '\0') - 4;
        if (cp > string && strcasecmp(cp, ".exe") == 0)
            *cp = '\0';
#else
#if NETSNMP_NO_DUMMY_VALUES
        return NULL;
#endif
        sprintf(string, "process name");
#endif
        *var_len = strlen(string);
        /*
         * remove trailing newline 
         */
        if (*var_len) {
            cp = string + *var_len - 1;
            if (*cp == '\n')
                --(*var_len);
        }
        if (*var_len > 64) { /* MIB limit */
            *var_len = 64;
            string[64] = '\0';
        }
        return (u_char *) string;
    case HRSWRUN_ID:
        *var_len = nullOidLen;
        return (u_char *) nullOid;
    case HRSWRUN_PATH:
#ifdef HAVE_SYS_PSTAT_H
        /*
         * Path not available - use argv[0] 
         */
        sprintf(string, "%s", proc_buf.pst_cmd);
        cp = strchr(string, ' ');
        if (cp != NULL)
            *cp = '\0';
#elif defined(dynix)
        /*
         * Path not available - use argv[0] 
         */
        sprintf(string, "%s", lowpsinfo.pr_psargs);
        cp = strchr(string, ' ');
        if (cp != NULL)
            *cp = '\0';
#elif defined(solaris2)
#ifdef _SLASH_PROC_METHOD_
        if (proc_buf)
            strcpy(string, proc_buf->pr_psargs);
        else
            sprintf(string, "<exited>");
        cp = strchr(string, ' ');
        if (cp)
            *cp = 0;
#else
        cp = proc_buf->p_user.u_psargs;
        cp1 = string;
        while (*cp && *cp != ' ')
            *cp1++ = *cp++;
        *cp1 = 0;
#endif
#elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
        strlcpy(string, proc_table[LowProcIndex].pi_comm, sizeof(string));
        cp = strchr(string, ' ');
        if (cp != NULL)
            *cp = '\0';
#elif HAVE_KVM_GETPROC2
        /* Should be path, but this is not available, just use argv[0] again */
        strlcpy(string, proc_table[LowProcIndex].p_comm, sizeof(string));
        cp = strchr(string, ' ');
        if (cp != NULL)
            *cp = '\0';
#elif HAVE_KVM_GETPROCS
    #if defined(freebsd5) && __FreeBSD_version >= 500014
        strcpy(string, proc_table[LowProcIndex].ki_comm);
    #elif defined(dragonfly) && __DragonFly_version >= 190000
        strcpy(string, proc_table[LowProcIndex].kp_comm);
    #else
        strcpy(string, proc_table[LowProcIndex].kp_proc.p_comm);
    #endif
#elif defined(linux)
        cp = get_proc_name_from_cmdline(pid,buf,sizeof(buf)-1);
        if (cp != NULL && *cp)    /* argv[0] '\0' argv[1] '\0' .... */
            strcpy(string, cp);
        else {
            /*
             * swapped out - no cmdline 
             */
	    if( (cp=get_proc_name_from_status(pid,buf,sizeof(buf)-1)) == NULL ) {
		strcpy(string, "<exited>");
		*var_len = strlen(string);
		return (u_char *) string;
	    }
            strcpy(string, cp);
        }
#elif defined(cygwin)
        /* if (lowproc.process_state & (PID_ZOMBIE | PID_EXITED)) */
        if (lowproc.process_state & PID_EXITED || (lowproc.exitcode & ~0xffff))
            strcpy(string, "<defunct>");
        else if (lowproc.ppid)
            cygwin_conv_to_posix_path(lowproc.progname, string);
        else if (query == CW_GETPINFO_FULL) {
            DWORD           n = lowproc.dwProcessId & 0xFFFF;
            HANDLE          h =
                OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                            FALSE, n);
            if (h) {
                HMODULE         hm[1000];
                if (!myEnumProcessModules(h, hm, sizeof hm, &n))
                    n = 0;
                if (!n
                    || !myGetModuleFileNameEx(h, hm[0], string,
                                              sizeof string))
                    strcpy(string, "*** unknown");
                CloseHandle(h);
            } else
                strcpy(string, "** unknown");
        } else
            strcpy(string, "* unknown");
#else
#if NETSNMP_NO_DUMMY_VALUES
        return NULL;
#endif
        sprintf(string, "/bin/wombat");
#endif
        *var_len = strlen(string);
        if (*var_len > 128) { /* MIB limit */
            *var_len = 128;
            string[128] = '\0';
        }
        return (u_char *) string;
    case HRSWRUN_PARAMS:
#ifdef HAVE_SYS_PSTAT_H
        cp = strchr(proc_buf.pst_cmd, ' ');
        if (cp != NULL) {
            cp++;
            sprintf(string, "%s", cp);
        } else
            string[0] = '\0';
#elif defined(dynix)
        cp = strchr(lowpsinfo.pr_psargs, ' ');
        if (cp != NULL) {
            cp++;
            sprintf(string, "%s", cp);
        } else
            string[0] = '\0';
#elif defined(solaris2)
#ifdef _SLASH_PROC_METHOD_
        if (proc_buf) {
            cp = strchr(proc_buf->pr_psargs, ' ');
            if (cp)
                strcpy(string, cp + 1);
            else
                string[0] = 0;
        } else
            string[0] = 0;
#else
        cp = proc_buf->p_user.u_psargs;
        while (*cp && *cp != ' ')
            cp++;
        if (*cp == ' ')
            cp++;
        strcpy(string, cp);
#endif
#elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
        cp = strchr(proc_table[LowProcIndex].pi_comm, ' ');
        if (cp != NULL) {
            cp++;
            sprintf(string, "%s", cp);
        } else
            string[0] = '\0';
#elif HAVE_KVM_GETPROC2
        string[0] = 0;
        argv = kvm_getargv2(kd, proc_table + LowProcIndex, sizeof(string));
        if (argv)
            argv++;
        while (argv && *argv) {
            if (string[0] != 0)
                strcat(string, " ");
            strcat(string, *argv);
            argv++;
        }
#elif HAVE_KVM_GETPROCS
        string[0] = 0;
        argv = kvm_getargv(kd, proc_table + LowProcIndex, sizeof(string));
        if (argv)
            argv++;
        while (argv && *argv) {
            if (string[0] != 0)
                strcat(string, " ");
            strcat(string, *argv);
            argv++;
        }
#elif defined(linux)
        memset(buf, 0, sizeof(buf));
	if( (cp=get_proc_name_from_cmdline(pid,buf,sizeof(buf)-2)) == NULL ) {
            strcpy(string, "");
            *var_len = 0;
            return (u_char *) string;
        }

        /*
         * Skip over argv[0] 
         */
        cp = buf;
        while (*cp)
            ++cp;
        ++cp;
        /*
         * Now join together separate arguments. 
         */
        while (1) {
            while (*cp)
                ++cp;
            if (*(cp + 1) == '\0')
                break;          /* '\0''\0' => End of command line */
            *cp = ' ';
        }

        cp = buf;
        while (*cp)
            ++cp;
        ++cp;
        strcpy(string, cp);
#elif defined(cygwin)
        string[0] = 0;
#else
#if NETSNMP_NO_DUMMY_VALUES
        return NULL;
#endif
        sprintf(string, "-h -q -v");
#endif
        *var_len = strlen(string);
        if (*var_len > 128) { /* MIB limit */
            *var_len = 128;
            string[128] = '\0';
        }
        return (u_char *) string;
    case HRSWRUN_TYPE:
#ifdef PID_MAXSYS
        if (pid < PID_MAXSYS)
            long_return = 2;    /* operatingSystem */
        else
            long_return = 4;    /* application */
#elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
		if (proc_table[LowProcIndex].pi_flags & SKPROC) {
			long_return = 2;	/* kernel process */
		} else
			long_return = 4;	/* application */
#elif HAVE_KVM_GETPROC2
        if (proc_table[LowProcIndex].p_flag & P_SYSTEM)
	    long_return = 2;	/* operatingSystem */
	else
	    long_return = 4;	/* application */
#elif HAVE_KVM_GETPROCS
    #if defined(freebsd5) && __FreeBSD_version >= 500014
	if (proc_table[LowProcIndex].ki_flag & P_SYSTEM) {
	    if (proc_table[LowProcIndex].ki_pri.pri_class == PRI_ITHD)
		long_return = 3;/* deviceDriver */
	    else
		long_return = 2;/* operatingSystem */
	} else
	    long_return = 4;	/* application */
    #else
      #if defined(dragonfly) && __DragonFly_version >= 190000
        if (proc_table[LowProcIndex].kp_flags & P_SYSTEM)
      #else
        if (proc_table[LowProcIndex].kp_proc.p_flag & P_SYSTEM)
      #endif
	    long_return = 2;	/* operatingSystem */
	else
	    long_return = 4;	/* application */
    #endif
#else
	long_return = 4;	/* application */
#endif
        return (u_char *) & long_return;
    case HRSWRUN_STATUS:
#if defined(cygwin)
        if (lowproc.process_state & PID_STOPPED)
            long_return = 3;    /* notRunnable */
        /* else if (lowproc.process_state & PID_ZOMBIE) */
        else if (lowproc.exitcode & ~0xffff)
            long_return = 4;    /* invalid */
        else
            long_return = 1;    /* running */
#elif !defined(linux)
#if defined(hpux10) || defined(hpux11)
        switch (proc_table[LowProcIndex].pst_stat) {
        case PS_STOP:
            long_return = 3;    /* notRunnable */
            break;
        case PS_SLEEP:
            long_return = 2;    /* runnable */
            break;
        case PS_RUN:
            long_return = 1;    /* running */
            break;
        case PS_ZOMBIE:
        case PS_IDLE:
        case PS_OTHER:
        default:
            long_return = 4;    /* invalid */
            break;
        }
#else
#if HAVE_KVM_GETPROC2
        switch (proc_table[LowProcIndex].p_stat) {
#elif HAVE_KVM_GETPROCS
    #if defined(freebsd5) && __FreeBSD_version >= 500014
        switch (proc_table[LowProcIndex].ki_stat) {
    #elif defined(dragonfly) && __DragonFly_version >= 190000
        switch (proc_table[LowProcIndex].kp_stat) {
    #else
        switch (proc_table[LowProcIndex].kp_proc.p_stat) {
    #endif
#elif defined(dynix)
        switch (lowpsinfo.pr_state) {
#elif defined(solaris2)
#if _SLASH_PROC_METHOD_
        switch (proc_buf ? proc_buf->pr_lwp.pr_state : SIDL) {
#else
        switch (proc_buf->p_stat) {
#endif
#elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
        switch (proc_table[LowProcIndex].pi_state) {
#else
        switch (proc_table[LowProcIndex].p_stat) {
#endif
        case SSTOP:
            long_return = 3;    /* notRunnable */
            break;
        case 0:
#ifdef SSWAP
        case SSWAP:
#endif
#ifdef SSLEEP
        case SSLEEP:
#endif
#ifdef SWAIT
        case SWAIT:
#endif
            long_return = 2;    /* runnable */
            break;
#ifdef SACTIVE
        case SACTIVE:
#endif
#ifdef SRUN
        case SRUN:
#endif
#ifdef SONPROC
        case SONPROC:
#endif
            long_return = 1;    /* running */
            break;
        case SIDL:
        case SZOMB:
        default:
            long_return = 4;    /* invalid */
            break;
        }
#endif
#else
	if ((cp = get_proc_stat_field(pid,buf,sizeof(buf),2)) != NULL ) {
            switch (*cp) {
            case 'R':
                long_return = 1;        /* running */
                break;
            case 'S':
                long_return = 2;        /* runnable */
                break;
            case 'D':
            case 'T':
                long_return = 3;        /* notRunnable */
                break;
            case 'Z':
            default:
                long_return = 4;        /* invalid */
                break;
            }
        } else
            long_return = 4;    /* invalid */
#endif
        return (u_char *) & long_return;

    case HRSWRUNPERF_CPU:
#ifdef HAVE_SYS_PSTAT_H
        long_return = proc_buf.pst_cptickstotal;
        /*
         * Not convinced this is right, but....
         */
#elif defined(dynix)
        long_return = lowpsinfo.pr_time.tv_sec * 100 +
            lowpsinfo.pr_time.tv_nsec / 10000000;
#elif defined(solaris2)
#if _SLASH_PROC_METHOD_
        long_return = proc_buf ? proc_buf->pr_time.tv_sec * 100 +
            proc_buf->pr_time.tv_nsec / 10000000 : 0;
#else
        long_return = proc_buf->p_utime * 100 + proc_buf->p_stime * 100;
#endif
#elif HAVE_KVM_GETPROC2
        long_return = proc_table[LowProcIndex].p_uticks +
            proc_table[LowProcIndex].p_sticks +
            proc_table[LowProcIndex].p_iticks;
#elif HAVE_KVM_GETPROCS
    #if defined(NOT_DEFINED) && defined(freebsd5) && __FreeBSD_version >= 500014
        /* XXX: Accessing ki_paddr causes sig10 ...
        long_return = proc_table[LowProcIndex].ki_paddr->p_uticks +
            proc_table[LowProcIndex].ki_paddr->p_sticks +
            proc_table[LowProcIndex].ki_paddr->p_iticks; */
        long_return = 0;
    #elif defined(freebsd5)
        long_return = proc_table[LowProcIndex].ki_runtime / 100000;
    #elif defined(dragonfly) && __DragonFly_version >= 190000
        long_return = proc_table[LowProcIndex].kp_lwp.kl_uticks +
            proc_table[LowProcIndex].kp_lwp.kl_sticks +
            proc_table[LowProcIndex].kp_lwp.kl_iticks;
    #elif defined(dragonfly)
        long_return = proc_table[LowProcIndex].kp_eproc.e_uticks +
            proc_table[LowProcIndex].kp_eproc.e_sticks +
            proc_table[LowProcIndex].kp_eproc.e_iticks;
    #else
        long_return = proc_table[LowProcIndex].kp_proc.p_uticks +
            proc_table[LowProcIndex].kp_proc.p_sticks +
            proc_table[LowProcIndex].kp_proc.p_iticks;
    #endif
#elif defined(linux)
	if ((cp = get_proc_stat_field(pid,buf,sizeof(buf),13)) == NULL ) {
            long_return = 0;
            return (u_char *) & long_return;
        }

        long_return = atoi(cp); /* utime */

        cp = skip_to_next_field(cp);

        long_return += atoi(cp);        /* + stime */
#elif defined(sunos4)
        long_return = proc_table[LowProcIndex].p_time;
#elif defined(cygwin)
        {
            DWORD           n = lowproc.dwProcessId;
            HANDLE          h =
                OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                            FALSE, n);
            FILETIME        ct, et, kt, ut;

            if (h) {
                if (GetProcessTimes(h, &ct, &et, &kt, &ut))
                    long_return = (to_msec(&kt) + to_msec(&ut)) / 10;
                else {
                    snmp_log(LOG_INFO, "no process times for %lu (%lu)\n",
                             lowproc.pid, n);
                    long_return = 0;
                }
                CloseHandle(h);
            } else {
                snmp_log(LOG_INFO, "no process handle for %lu (%lu)\n",
                         lowproc.pid, n);
                long_return = 0;
            }
        }
#elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
        long_return = proc_table[LowProcIndex].pi_ru.ru_utime.tv_sec * 100 +
            proc_table[LowProcIndex].pi_ru.ru_utime.tv_usec / 10000000 + /* nanoseconds */
            proc_table[LowProcIndex].pi_ru.ru_stime.tv_sec * 100 +
            proc_table[LowProcIndex].pi_ru.ru_stime.tv_usec / 10000000; /* nanoseconds */
#else
        long_return = proc_table[LowProcIndex].p_utime.tv_sec * 100 +
            proc_table[LowProcIndex].p_utime.tv_usec / 10000 +
            proc_table[LowProcIndex].p_stime.tv_sec * 100 +
            proc_table[LowProcIndex].p_stime.tv_usec / 10000;
#endif
        return (u_char *) & long_return;
    case HRSWRUNPERF_MEM:
#ifdef HAVE_SYS_PSTAT_H
# ifdef PGSHIFT
        long_return = (proc_buf.pst_rssize << PGSHIFT) / 1024;
# else
        long_return = proc_buf.pst_rssize * getpagesize() / 1024;
# endif
#elif defined(dynix)
        long_return = (lowpsinfo.pr_rssize * MMU_PAGESIZE) / 1024;
#elif defined(solaris2)
#if _SLASH_PROC_METHOD_
#ifdef _ILP32
        if(NULL != proc_buf && 0 == proc_buf->pr_rssize)
        { /* Odds on that we are looking with a 32 bit app at a 64 bit psinfo.*/
            netsnmp_memory_info *mem;
            netsnmp_memory_load();
            mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 0 );
            if (!mem) 
            {
                snmp_log(LOG_INFO, "netsnmp_memory_get_byIdx returned NULL pointer\n");
                long_return = 0;/* Tried my best, giving up.*/
            } 
            else 
            {/* 0x8000 is the maximum range of pr_pctmem. devision of 1024 is to go from B to kB*/
                uint32_t pct_unit = (mem->size/0x8000) * (mem->units/1024);
                long_return = proc_buf ? proc_buf->pr_pctmem * pct_unit : 0;
            }
        }
        else
        {    
            long_return = proc_buf ? proc_buf->pr_rssize : 0;
        
        }
#else /*_LP64*/
        long_return = proc_buf ? proc_buf->pr_rssize : 0;
#endif /*_LP64*/

#else
        long_return = proc_buf->p_swrss;
#endif
#elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
        long_return = proc_table[LowProcIndex].pi_size * getpagesize() / 1024;
#elif HAVE_KVM_GETPROC2
        long_return = proc_table[LowProcIndex].p_vm_tsize +
            proc_table[LowProcIndex].p_vm_ssize +
            proc_table[LowProcIndex].p_vm_dsize;
        long_return = long_return * (getpagesize() / 1024);
#elif HAVE_KVM_GETPROCS && !defined(darwin8)
  #if defined(NOT_DEFINED) && defined(freebsd5) && __FreeBSD_version >= 500014
	    /* XXX
	    long_return = proc_table[LowProcIndex].ki_vmspace->vm_tsize +
			  proc_table[LowProcIndex].ki_vmspace->vm_ssize +
			  proc_table[LowProcIndex].ki_vmspace->vm_dsize;
	    long_return = long_return * (getpagesize() / 1024); */
	    long_return = 0;
  #elif defined(freebsd3) && !defined(darwin)
        long_return =
    #if defined(freebsd5)
            proc_table[LowProcIndex].ki_size / 1024;
    #elif defined(dragonfly) && __DragonFly_version >= 190000
            proc_table[LowProcIndex].kp_vm_map_size / 1024;
    #else
            proc_table[LowProcIndex].kp_eproc.e_vm.vm_map.size / 1024;
    #endif
  #else
        long_return = proc_table[LowProcIndex].kp_eproc.e_vm.vm_tsize +
            proc_table[LowProcIndex].kp_eproc.e_vm.vm_ssize +
            proc_table[LowProcIndex].kp_eproc.e_vm.vm_dsize;
        long_return = long_return * (getpagesize() / 1024);
  #endif
#elif defined(linux)
	if ((cp = get_proc_stat_field(pid,buf,sizeof(buf),23)) == NULL ) {
            long_return = 0;
            return (u_char *) & long_return;
        }
        long_return = atoi(cp) * (getpagesize() / 1024);        /* rss */
#elif defined(cygwin)
        {
            DWORD           n = lowproc.dwProcessId;
            HANDLE          h =
                OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                            FALSE, n);
            PROCESS_MEMORY_COUNTERS pmc;

            if (h) {
                if (myGetProcessMemoryInfo
                    && myGetProcessMemoryInfo(h, &pmc, sizeof pmc))
                    long_return = pmc.WorkingSetSize / 1024;
                else {
                    snmp_log(LOG_INFO, "no process times for %lu (%lu)\n",
                             lowproc.pid, n);
                    long_return = 0;
                }
                CloseHandle(h);
            } else {
                snmp_log(LOG_INFO, "no process handle for %lu (%lu)\n",
                         lowproc.pid, n);
                long_return = 0;
            }
        }
#else
#if NETSNMP_NO_DUMMY_VALUES
        return NULL;
#endif
        long_return = 16 * 1024;        /* XXX - 16M! */
#endif
        return (u_char *) & long_return;
    default:
        DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrswrun\n",
                    vp->magic));
    }
    return NULL;
}


        /*********************
	 *
	 *  Internal implementation functions
	 *
	 *********************/

#if defined(linux)

DIR            *procdir = NULL;
struct dirent  *procentry_p;

void
Init_HR_SWRun(void)
{
    if (procdir != NULL)
        closedir(procdir);
    procdir = opendir("/proc");
}

int
Get_Next_HR_SWRun(void)
{
    int             pid;
    if (procdir == NULL)
      return -1;
    procentry_p = readdir(procdir);

    if (procentry_p == NULL)
        return -1;

    pid = atoi(procentry_p->d_name);
    if (pid == 0)
        return (Get_Next_HR_SWRun());
    return pid;
}

void
End_HR_SWRun(void)
{
    if (procdir)
        closedir(procdir);
    procdir = NULL;
}

#elif defined(cygwin)

static pid_t    curpid;

void
Init_HR_SWRun(void)
{
    cygwin_internal(CW_LOCK_PINFO, 1000);
    curpid = 0;
}

int
Get_Next_HR_SWRun(void)
{
    curproc =
        (struct external_pinfo *) cygwin_internal(query,
                                                  curpid | CW_NEXTPID);
    if (curproc)
        curpid = curproc->pid;
    else {
        curpid = -1;
    }
    return curpid;
}

void
End_HR_SWRun(void)
{
    cygwin_internal(CW_UNLOCK_PINFO);
}

#elif defined(dynix)

void
Init_HR_SWRun(void)
{
    nextproc = 0;
}

int
Get_Next_HR_SWRun(void)
{
    getprpsinfo_t  *select = 0;

    DEBUGMSGTL(("host/hr_swrun::GetNextHR_SWRun",
                "nextproc == %d... &nextproc = %u\n", nextproc,
                &nextproc));
    if ((nextproc = getprpsinfo(nextproc, select, &mypsinfo)) < 0) {
        return -1;
    } else {
        DEBUGMSGTL(("host/hr_swrun::GetNextHR_SWRun",
                    "getprpsinfo returned %d\n", nextproc));
        return mypsinfo.pr_pid;
    }

}

void
End_HR_SWRun(void)
{
    /*
     * just a stub... because it's declared 
     */
}

#else                           /* linux */

static int      nproc;

void
Init_HR_SWRun(void)
{
    size_t          bytes;
    static time_t   iwhen = 0;
    time_t          now;

    time(&now);
    if (now == iwhen) {
        current_proc_entry = 0;
        return;
    }
    iwhen = now;

#if defined(hpux10) || defined(hpux11)
    pstat_getdynamic(&pst_dyn, sizeof(struct pst_dynamic), 1, 0);
    nproc = pst_dyn.psd_activeprocs;
    bytes = nproc * sizeof(struct pst_status);
    if ((proc_table =
         (struct pst_status *) realloc(proc_table, bytes)) == NULL) {
        current_proc_entry = nproc + 1;
        return;
    }
    pstat_getproc(proc_table, sizeof(struct pst_status), nproc, 0);

#elif defined(solaris2)
    if (getKstatInt("unix", "system_misc", "nproc", &nproc)) {
        current_proc_entry = nproc + 1;
        return;
    }
    bytes = nproc * sizeof(int);
    if ((proc_table = (int *) realloc(proc_table, bytes)) == NULL) {
        current_proc_entry = nproc + 1;
        return;
    }
    {
        DIR            *f;
        struct dirent  *dp;
#if _SLASH_PROC_METHOD_ == 0
        if (kd == NULL) {
            current_proc_entry = nproc + 1;
            return;
        }
#endif
        f = opendir("/proc");
        current_proc_entry = 0;
        while ((dp = readdir(f)) != NULL && current_proc_entry < nproc)
            if (dp->d_name[0] != '.')
                proc_table[current_proc_entry++] = atoi(dp->d_name);
        /*
         * if we are in a Solaris zone, nproc > current_proc_entry !
         * but we only want the processes from the local zone
         */
        if (current_proc_entry != nproc)
            nproc = current_proc_entry;
        closedir(f);
    }
#elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
    {
		pid_t proc_index = 0;
		int avail = 1024;
		if (proc_table) {
			free(proc_table);
		}
		nproc = 0;
		proc_table = malloc(sizeof(proc_table[0]) * avail);
		for (;;) {
			int got;
			if (!proc_table) {
				nproc = 0;
				snmp_log_perror("Init_HR_SWRun-malloc");
				return;
			}
			got = getprocs(proc_table + nproc, sizeof(proc_table[0]),
						   0, sizeof(struct fdsinfo),
						   &proc_index, avail - nproc);
			nproc += got;
			if (nproc < avail) {
				break;
			}
			avail += 1024;
			proc_table = realloc(proc_table, avail * sizeof(proc_table[0]));
		}
    }
#elif HAVE_KVM_GETPROC2
    {
        if (kd == NULL) {
            nproc = 0;
            return;
        }
        proc_table = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof (struct kinfo_proc2), &nproc);
    }
#elif HAVE_KVM_GETPROCS
    {
        if (kd == NULL) {
            nproc = 0;
            return;
        }
        proc_table = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
    }
#else

    current_proc_entry = 1;
#ifndef bsdi2
    nproc = 0;

    if (auto_nlist(NPROC_SYMBOL, (char *) &nproc, sizeof(int)) == 0) {
        snmp_log_perror("Init_HR_SWRun-auto_nlist NPROC");
        return;
    }
#endif
    bytes = nproc * sizeof(struct proc);

    if (proc_table)
        free((char *) proc_table);
    if ((proc_table = (struct proc *) malloc(bytes)) == NULL) {
        nproc = 0;
        snmp_log_perror("Init_HR_SWRun-malloc");
        return;
    }

    {
        int             proc_table_base;
        if (auto_nlist
            (PROC_SYMBOL, (char *) &proc_table_base,
             sizeof(proc_table_base)) == 0) {
            nproc = 0;
            snmp_log_perror("Init_HR_SWRun-auto_nlist PROC");
            return;
        }
        if (NETSNMP_KLOOKUP(proc_table_base, (char *) proc_table, bytes) == 0) {
            nproc = 0;
            snmp_log_perror("Init_HR_SWRun-klookup");
            return;
        }
    }
#endif
    current_proc_entry = 0;
}

int
Get_Next_HR_SWRun(void)
{
    while (current_proc_entry < nproc) {
#if defined(hpux10) || defined(hpux11)
        return proc_table[current_proc_entry++].pst_pid;
#elif defined(solaris2)
        return proc_table[current_proc_entry++];
#elif HAVE_KVM_GETPROC2
        if (proc_table[current_proc_entry].p_stat != 0)
            return proc_table[current_proc_entry++].p_pid;
#elif HAVE_KVM_GETPROCS
    #if defined(freebsd5) && __FreeBSD_version >= 500014
        if (proc_table[current_proc_entry].ki_stat != 0)
            return proc_table[current_proc_entry++].ki_pid;
    #elif defined(dragonfly) && __DragonFly_version >= 190000
        if (proc_table[current_proc_entry].kp_stat != 0)
            return proc_table[current_proc_entry++].kp_pid;
    #else
        if (proc_table[current_proc_entry].kp_proc.p_stat != 0)
            return proc_table[current_proc_entry++].kp_proc.p_pid;
    #endif
#elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
        if (proc_table[current_proc_entry].pi_state != 0)
            return proc_table[current_proc_entry++].pi_pid;
        else
            ++current_proc_entry;
#else
        if (proc_table[current_proc_entry].p_stat != 0)
            return proc_table[current_proc_entry++].p_pid;
        else
            ++current_proc_entry;
#endif

    }
    return -1;
}

void
End_HR_SWRun(void)
{
    current_proc_entry = nproc + 1;
}
#endif

int
count_processes(void)
{
#if !(defined(linux) || defined(cygwin) || defined(hpux10) || defined(hpux11) || defined(solaris2) || HAVE_KVM_GETPROCS || HAVE_KVM_GETPROC2 || defined(dynix))
    int             i;
#endif
    int             total = 0;

    Init_HR_SWRun();
#if defined(hpux10) || defined(hpux11) || HAVE_KVM_GETPROCS || HAVE_KVM_GETPROC2 || defined(solaris2)
    total = nproc;
#else
#if defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
    for (i = 0; i < nproc; ++i) {
        if (proc_table[i].pi_state != 0)
#elif !defined(linux) && !defined(cygwin) && !defined(dynix)
    for (i = 0; i < nproc; ++i) {
        if (proc_table[i].p_stat != 0)
#else
    while (Get_Next_HR_SWRun() != -1) {
#endif
        ++total;
    }
#endif                          /* !hpux10 && !hpux11 && !HAVE_KVM_GETPROCS && !HAVE_KVM_GETPROC2 && !solaris2 */
    End_HR_SWRun();
    return total;
}