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
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2013 Gary Mills
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2019 Joyent, Inc.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* ps -- print things about processes.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mkdev.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <dirent.h>
#include <sys/signal.h>
#include <sys/fault.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <procfs.h>
#include <locale.h>
#include <wctype.h>
#include <wchar.h>
#include <libw.h>
#include <stdarg.h>
#include <sys/proc.h>
#include <sys/pset.h>
#include <project.h>
#include <zone.h>
#include <assert.h>
#include <stdbool.h>
#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) < (b) ? (b) : (a))
#define NTTYS 20 /* initial size of table for -t option */
#define SIZ 30 /* initial size of tables for -p, -s, -g, -h and -z */
/*
* Size of buffer holding args for t, p, s, g, u, U, G, z options.
* Set to ZONENAME_MAX, the minimum value needed to allow any
* zone to be specified.
*/
#define ARGSIZ ZONENAME_MAX
/* Max chars in a user/group name or printed u/g id */
#define MAXUGNAME (LOGNAME_MAX+2)
/* Structure for storing user or group info */
struct ugdata {
id_t id; /* numeric user-id or group-id */
char name[MAXUGNAME+1]; /* user/group name, null terminated */
};
struct ughead {
size_t size; /* number of ugdata structs allocated */
size_t nent; /* number of active entries */
struct ugdata *ent; /* pointer to array of actual entries */
};
enum fname { /* enumeration of field names */
F_USER, /* effective user of the process */
F_RUSER, /* real user of the process */
F_GROUP, /* effective group of the process */
F_RGROUP, /* real group of the process */
F_UID, /* numeric effective uid of the process */
F_RUID, /* numeric real uid of the process */
F_GID, /* numeric effective gid of the process */
F_RGID, /* numeric real gid of the process */
F_PID, /* process id */
F_PPID, /* parent process id */
F_PGID, /* process group id */
F_SID, /* session id */
F_PSR, /* bound processor */
F_LWP, /* lwp-id */
F_LWPNAME, /* lwp name */
F_NLWP, /* number of lwps */
F_OPRI, /* old priority (obsolete) */
F_PRI, /* new priority */
F_F, /* process flags */
F_S, /* letter indicating the state */
F_C, /* processor utilization (obsolete) */
F_PCPU, /* percent of recently used cpu time */
F_PMEM, /* percent of physical memory used (rss) */
F_OSZ, /* virtual size of the process in pages */
F_VSZ, /* virtual size of the process in kilobytes */
F_RSS, /* resident set size of the process in kilobytes */
F_NICE, /* "nice" value of the process */
F_CLASS, /* scheduler class */
F_STIME, /* start time of the process, hh:mm:ss or Month Day */
F_ETIME, /* elapsed time of the process, [[dd-]hh:]mm:ss */
F_TIME, /* cpu time of the process, [[dd-]hh:]mm:ss */
F_TTY, /* name of the controlling terminal */
F_ADDR, /* address of the process (obsolete) */
F_WCHAN, /* wait channel (sleep condition variable) */
F_FNAME, /* file name of command */
F_COMM, /* name of command (argv[0] value) */
F_ARGS, /* name of command plus all its arguments */
F_TASKID, /* task id */
F_PROJID, /* project id */
F_PROJECT, /* project name of the process */
F_PSET, /* bound processor set */
F_ZONE, /* zone name */
F_ZONEID, /* zone id */
F_CTID, /* process contract id */
F_LGRP, /* process home lgroup */
F_DMODEL /* process data model */
};
struct field {
struct field *next; /* linked list */
int fname; /* field index */
const char *header; /* header to use */
int width; /* width of field */
};
static struct field *fields = NULL; /* fields selected via -o */
static struct field *last_field = NULL;
static int do_header = 0;
static struct timeval now;
/* array of defined fields, in fname order */
struct def_field {
const char *fname;
const char *header;
int width;
int minwidth;
};
static struct def_field fname[] = {
/* fname header width minwidth */
{ "user", "USER", 8, 8 },
{ "ruser", "RUSER", 8, 8 },
{ "group", "GROUP", 8, 8 },
{ "rgroup", "RGROUP", 8, 8 },
{ "uid", "UID", 5, 5 },
{ "ruid", "RUID", 5, 5 },
{ "gid", "GID", 5, 5 },
{ "rgid", "RGID", 5, 5 },
{ "pid", "PID", 5, 5 },
{ "ppid", "PPID", 5, 5 },
{ "pgid", "PGID", 5, 5 },
{ "sid", "SID", 5, 5 },
{ "psr", "PSR", 3, 2 },
{ "lwp", "LWP", 6, 2 },
{ "lwpname", "LWPNAME", 32, 8 },
{ "nlwp", "NLWP", 4, 2 },
{ "opri", "PRI", 3, 2 },
{ "pri", "PRI", 3, 2 },
{ "f", "F", 2, 2 },
{ "s", "S", 1, 1 },
{ "c", "C", 2, 2 },
{ "pcpu", "%CPU", 4, 4 },
{ "pmem", "%MEM", 4, 4 },
{ "osz", "SZ", 4, 4 },
{ "vsz", "VSZ", 4, 4 },
{ "rss", "RSS", 4, 4 },
{ "nice", "NI", 2, 2 },
{ "class", "CLS", 4, 2 },
{ "stime", "STIME", 8, 8 },
{ "etime", "ELAPSED", 11, 7 },
{ "time", "TIME", 11, 5 },
{ "tty", "TT", 7, 7 },
#ifdef _LP64
{ "addr", "ADDR", 16, 8 },
{ "wchan", "WCHAN", 16, 8 },
#else
{ "addr", "ADDR", 8, 8 },
{ "wchan", "WCHAN", 8, 8 },
#endif
{ "fname", "COMMAND", 8, 8 },
{ "comm", "COMMAND", 80, 8 },
{ "args", "COMMAND", 80, 80 },
{ "taskid", "TASKID", 5, 5 },
{ "projid", "PROJID", 5, 5 },
{ "project", "PROJECT", 8, 8 },
{ "pset", "PSET", 3, 3 },
{ "zone", "ZONE", 8, 8 },
{ "zoneid", "ZONEID", 5, 5 },
{ "ctid", "CTID", 5, 5 },
{ "lgrp", "LGRP", 4, 2 },
{ "dmodel", "DMODEL", 6, 6 },
};
#define NFIELDS (sizeof (fname) / sizeof (fname[0]))
static int retcode = 1;
static int lflg;
static int Aflg;
static int uflg;
static int Uflg;
static int Gflg;
static int aflg;
static int dflg;
static int Lflg;
static int Pflg;
static int Wflg;
static int yflg;
static int pflg;
static int fflg;
static int cflg;
static int jflg;
static int gflg;
static int sflg;
static int tflg;
static int zflg;
static int Zflg;
static int hflg;
static int Hflg;
static uid_t tuid = (uid_t)-1;
static int errflg;
static int ndev; /* number of devices */
static int maxdev; /* number of devl structures allocated */
#define DNINCR 100
#define DNSIZE 14
static struct devl { /* device list */
char dname[DNSIZE]; /* device name */
dev_t ddev; /* device number */
} *devl;
static struct tty {
char *tname;
dev_t tdev;
} *tty = NULL; /* for t option */
static size_t ttysz = 0;
static int ntty = 0;
static pid_t *pid = NULL; /* for p option */
static size_t pidsz = 0;
static size_t npid = 0;
static int *lgrps = NULL; /* list of lgroup IDs for for h option */
static size_t lgrps_size = 0; /* size of the lgrps list */
static size_t nlgrps = 0; /* number elements in the list */
/* Maximum possible lgroup ID value */
#define MAX_LGRP_ID 256
static pid_t *grpid = NULL; /* for g option */
static size_t grpidsz = 0;
static int ngrpid = 0;
static pid_t *sessid = NULL; /* for s option */
static size_t sessidsz = 0;
static int nsessid = 0;
static zoneid_t *zoneid = NULL; /* for z option */
static size_t zoneidsz = 0;
static int nzoneid = 0;
static int kbytes_per_page;
static int pidwidth;
static char procdir[MAXPATHLEN]; /* standard /proc directory */
static struct ughead euid_tbl; /* table to store selected euid's */
static struct ughead ruid_tbl; /* table to store selected real uid's */
static struct ughead egid_tbl; /* table to store selected egid's */
static struct ughead rgid_tbl; /* table to store selected real gid's */
static prheader_t *lpsinfobuf; /* buffer to contain lpsinfo */
static size_t lpbufsize;
/*
* This constant defines the sentinal number of process IDs below which we
* only examine individual entries in /proc rather than scanning through
* /proc. This optimization is a huge win in the common case.
*/
#define PTHRESHOLD 40
#define UCB_OPTS "-aceglnrtuvwxSU"
static void usage(void);
static char *getarg(char **);
static char *parse_format(char *);
static char *gettty(psinfo_t *);
static int prfind(int, psinfo_t *, char **);
static void prcom(psinfo_t *, char *);
static void prtpct(ushort_t, int);
static void print_time(time_t, int);
static void print_field(psinfo_t *, struct field *, const char *);
static void print_zombie_field(psinfo_t *, struct field *, const char *);
static void pr_fields(psinfo_t *, const char *,
void (*print_fld)(psinfo_t *, struct field *, const char *));
static int search(pid_t *, int, pid_t);
static void add_ugentry(struct ughead *, char *);
static int uconv(struct ughead *);
static int gconv(struct ughead *);
static int ugfind(id_t, struct ughead *);
static void prtime(timestruc_t, int, int);
static void przom(psinfo_t *);
static int namencnt(char *, int, int);
static char *err_string(int);
static int print_proc(char *pname);
static time_t delta_secs(const timestruc_t *);
static int str2id(const char *, pid_t *, long, long);
static int str2uid(const char *, uid_t *, unsigned long, unsigned long);
static void *Realloc(void *, size_t);
static int pidcmp(const void *p1, const void *p2);
extern int ucbmain(int, char **);
static int stdmain(int, char **);
/* also used by ucbps.c */
void get_psargs(bool, bool, psinfo_t *, char *, size_t);
void print_psargs(char *, int);
int
main(int argc, char **argv)
{
const char *me;
const char *zroot = zone_get_nroot();
/*
* If this is a branded zone, the native procfs may mounted in a
* non-standard location. Apply such a path prefix if it exists.
*/
(void) snprintf(procdir, sizeof (procdir), "%s/proc", zroot != NULL ?
zroot : "");
/*
* The original two ps'es are linked in a single binary;
* their main()s are renamed to stdmain for /usr/bin/ps and
* ucbmain for /usr/ucb/ps.
* We try to figure out which instance of ps the user wants to run.
* Traditionally, the UCB variant doesn't require the flag argument
* start with a "-". If the first argument doesn't start with a
* "-", we call "ucbmain".
* If there's a first argument and it starts with a "-", we check
* whether any of the options isn't acceptable to "ucbmain"; in that
* case we run "stdmain".
* If we can't tell from the options which main to call, we check
* the binary we are running. We default to "stdmain" but
* any mention in the executable name of "ucb" causes us to call
* ucbmain.
*/
if (argv[1] != NULL) {
if (argv[1][0] != '-')
return (ucbmain(argc, argv));
else if (argv[1][strspn(argv[1], UCB_OPTS)] != '\0')
return (stdmain(argc, argv));
}
me = getexecname();
if (me != NULL && strstr(me, "ucb") != NULL)
return (ucbmain(argc, argv));
else
return (stdmain(argc, argv));
}
static int
stdmain(int argc, char **argv)
{
char *p;
char *p1;
char *parg;
int c;
int i;
int pgerrflg = 0; /* err flg: non-numeric arg w/p & g options */
size_t size, len;
DIR *dirp;
struct dirent *dentp;
pid_t maxpid;
pid_t id;
int ret;
char loc_stime_str[32];
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
#endif
(void) textdomain(TEXT_DOMAIN);
(void) memset(&euid_tbl, 0, sizeof (euid_tbl));
(void) memset(&ruid_tbl, 0, sizeof (ruid_tbl));
(void) memset(&egid_tbl, 0, sizeof (egid_tbl));
(void) memset(&rgid_tbl, 0, sizeof (rgid_tbl));
kbytes_per_page = sysconf(_SC_PAGESIZE) / 1024;
(void) gettimeofday(&now, NULL);
/*
* calculate width of pid fields based on configured MAXPID
* (must be at least 5 to retain output format compatibility)
*/
id = maxpid = (pid_t)sysconf(_SC_MAXPID);
pidwidth = 1;
while ((id /= 10) > 0)
++pidwidth;
pidwidth = pidwidth < 5 ? 5 : pidwidth;
fname[F_PID].width = fname[F_PPID].width = pidwidth;
fname[F_PGID].width = fname[F_SID].width = pidwidth;
/*
* TRANSLATION_NOTE
* Specify the printf format with width and precision for
* the STIME field.
*/
len = snprintf(loc_stime_str, sizeof (loc_stime_str),
dcgettext(NULL, "%8.8s", LC_TIME), "STIME");
if (len >= sizeof (loc_stime_str))
len = sizeof (loc_stime_str) - 1;
fname[F_STIME].width = fname[F_STIME].minwidth = len;
while ((c = getopt(argc, argv, "jlfceAadLPWyZHh:t:p:g:u:U:G:n:s:o:z:"))
!= EOF)
switch (c) {
case 'H': /* Show home lgroups */
Hflg++;
break;
case 'h':
/*
* Show processes/threads with given home lgroups
*/
hflg++;
p1 = optarg;
do {
int id;
/*
* Get all IDs in the list, verify for
* correctness and place in lgrps array.
*/
parg = getarg(&p1);
/* Convert string to integer */
ret = str2id(parg, (pid_t *)&id, 0,
MAX_LGRP_ID);
/* Complain if ID didn't parse correctly */
if (ret != 0) {
pgerrflg++;
(void) fprintf(stderr,
gettext("ps: %s "), parg);
if (ret == EINVAL)
(void) fprintf(stderr,
gettext("is an invalid "
"non-numeric argument"));
else
(void) fprintf(stderr,
gettext("exceeds valid "
"range"));
(void) fprintf(stderr,
gettext(" for -h option\n"));
continue;
}
/* Extend lgrps array if needed */
if (nlgrps == lgrps_size) {
/* Double the size of the lgrps array */
if (lgrps_size == 0)
lgrps_size = SIZ;
lgrps_size *= 2;
lgrps = Realloc(lgrps,
lgrps_size * sizeof (int));
}
/* place the id in the lgrps table */
lgrps[nlgrps++] = id;
} while (*p1);
break;
case 'l': /* long listing */
lflg++;
break;
case 'f': /* full listing */
fflg++;
break;
case 'j':
jflg++;
break;
case 'c':
/*
* Format output to reflect scheduler changes:
* high numbers for high priorities and don't
* print nice or p_cpu values. 'c' option only
* effective when used with 'l' or 'f' options.
*/
cflg++;
break;
case 'A': /* list every process */
case 'e': /* (obsolete) list every process */
Aflg++;
tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0;
zflg = hflg = 0;
break;
case 'a':
/*
* Same as 'e' except no session group leaders
* and no non-terminal processes.
*/
aflg++;
break;
case 'd': /* same as e except no session leaders */
dflg++;
break;
case 'L': /* show lwps */
Lflg++;
break;
case 'P': /* show bound processor */
Pflg++;
break;
case 'W': /* truncate long names */
Wflg++;
break;
case 'y': /* omit F & ADDR, report RSS & SZ in Kby */
yflg++;
break;
case 'n': /* no longer needed; retain as no-op */
(void) fprintf(stderr,
gettext("ps: warning: -n option ignored\n"));
break;
case 't': /* terminals */
#define TSZ 30
tflg++;
p1 = optarg;
do {
char nambuf[TSZ+6]; /* for "/dev/" + '\0' */
struct stat64 s;
parg = getarg(&p1);
p = Realloc(NULL, TSZ+1); /* for '\0' */
/* zero the buffer before using it */
p[0] = '\0';
size = TSZ;
if (isdigit(*parg)) {
(void) strcpy(p, "tty");
size -= 3;
}
(void) strncat(p, parg, size);
if (ntty == ttysz) {
if ((ttysz *= 2) == 0)
ttysz = NTTYS;
tty = Realloc(tty,
(ttysz + 1) * sizeof (struct tty));
}
tty[ntty].tdev = PRNODEV;
(void) strcpy(nambuf, "/dev/");
(void) strcat(nambuf, p);
if (stat64(nambuf, &s) == 0)
tty[ntty].tdev = s.st_rdev;
tty[ntty++].tname = p;
} while (*p1);
break;
case 'p': /* proc ids */
pflg++;
p1 = optarg;
do {
pid_t id;
parg = getarg(&p1);
if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
pgerrflg++;
(void) fprintf(stderr,
gettext("ps: %s "), parg);
if (ret == EINVAL)
(void) fprintf(stderr,
gettext("is an invalid "
"non-numeric argument"));
else
(void) fprintf(stderr,
gettext("exceeds valid "
"range"));
(void) fprintf(stderr,
gettext(" for -p option\n"));
continue;
}
if (npid == pidsz) {
if ((pidsz *= 2) == 0)
pidsz = SIZ;
pid = Realloc(pid,
pidsz * sizeof (pid_t));
}
pid[npid++] = id;
} while (*p1);
break;
case 's': /* session */
sflg++;
p1 = optarg;
do {
pid_t id;
parg = getarg(&p1);
if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
pgerrflg++;
(void) fprintf(stderr,
gettext("ps: %s "), parg);
if (ret == EINVAL)
(void) fprintf(stderr,
gettext("is an invalid "
"non-numeric argument"));
else
(void) fprintf(stderr,
gettext("exceeds valid "
"range"));
(void) fprintf(stderr,
gettext(" for -s option\n"));
continue;
}
if (nsessid == sessidsz) {
if ((sessidsz *= 2) == 0)
sessidsz = SIZ;
sessid = Realloc(sessid,
sessidsz * sizeof (pid_t));
}
sessid[nsessid++] = id;
} while (*p1);
break;
case 'g': /* proc group */
gflg++;
p1 = optarg;
do {
pid_t id;
parg = getarg(&p1);
if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
pgerrflg++;
(void) fprintf(stderr,
gettext("ps: %s "), parg);
if (ret == EINVAL)
(void) fprintf(stderr,
gettext("is an invalid "
"non-numeric argument"));
else
(void) fprintf(stderr,
gettext("exceeds valid "
"range"));
(void) fprintf(stderr,
gettext(" for -g option\n"));
continue;
}
if (ngrpid == grpidsz) {
if ((grpidsz *= 2) == 0)
grpidsz = SIZ;
grpid = Realloc(grpid,
grpidsz * sizeof (pid_t));
}
grpid[ngrpid++] = id;
} while (*p1);
break;
case 'u': /* effective user name or number */
uflg++;
p1 = optarg;
do {
parg = getarg(&p1);
add_ugentry(&euid_tbl, parg);
} while (*p1);
break;
case 'U': /* real user name or number */
Uflg++;
p1 = optarg;
do {
parg = getarg(&p1);
add_ugentry(&ruid_tbl, parg);
} while (*p1);
break;
case 'G': /* real group name or number */
Gflg++;
p1 = optarg;
do {
parg = getarg(&p1);
add_ugentry(&rgid_tbl, parg);
} while (*p1);
break;
case 'o': /* output format */
p = optarg;
while ((p = parse_format(p)) != NULL)
;
break;
case 'z': /* zone name or number */
zflg++;
p1 = optarg;
do {
zoneid_t id;
parg = getarg(&p1);
if (zone_get_id(parg, &id) != 0) {
pgerrflg++;
(void) fprintf(stderr,
gettext("ps: unknown zone %s\n"),
parg);
continue;
}
if (nzoneid == zoneidsz) {
if ((zoneidsz *= 2) == 0)
zoneidsz = SIZ;
zoneid = Realloc(zoneid,
zoneidsz * sizeof (zoneid_t));
}
zoneid[nzoneid++] = id;
} while (*p1);
break;
case 'Z': /* show zone name */
Zflg++;
break;
default: /* error on ? */
errflg++;
break;
}
if (errflg || optind < argc || pgerrflg)
usage();
if (tflg)
tty[ntty].tname = NULL;
/*
* If an appropriate option has not been specified, use the
* current terminal and effective uid as the default.
*/
if (!(aflg|Aflg|dflg|Gflg|hflg|Uflg|uflg|tflg|pflg|gflg|sflg|zflg)) {
psinfo_t info;
int procfd;
char *name;
char pname[100];
/* get our own controlling tty name using /proc */
(void) snprintf(pname, sizeof (pname),
"%s/self/psinfo", procdir);
if ((procfd = open(pname, O_RDONLY)) < 0 ||
read(procfd, (char *)&info, sizeof (info)) < 0 ||
info.pr_ttydev == PRNODEV) {
(void) fprintf(stderr,
gettext("ps: no controlling terminal\n"));
exit(1);
}
(void) close(procfd);
i = 0;
name = gettty(&info);
if (*name == '?') {
(void) fprintf(stderr,
gettext("ps: can't find controlling terminal\n"));
exit(1);
}
if (ntty == ttysz) {
if ((ttysz *= 2) == 0)
ttysz = NTTYS;
tty = Realloc(tty, (ttysz + 1) * sizeof (struct tty));
}
tty[ntty].tdev = info.pr_ttydev;
tty[ntty++].tname = name;
tty[ntty].tname = NULL;
tflg++;
tuid = getuid();
}
if (Aflg) {
Gflg = Uflg = uflg = pflg = sflg = gflg = aflg = dflg = 0;
zflg = hflg = 0;
}
if (Aflg | aflg | dflg)
tflg = 0;
i = 0; /* prepare to exit on name lookup errors */
i += uconv(&euid_tbl);
i += uconv(&ruid_tbl);
i += gconv(&egid_tbl);
i += gconv(&rgid_tbl);
if (i)
exit(1);
/* allocate a buffer for lwpsinfo structures */
lpbufsize = 4096;
if (Lflg && (lpsinfobuf = malloc(lpbufsize)) == NULL) {
(void) fprintf(stderr,
gettext("ps: no memory\n"));
exit(1);
}
if (fields) { /* print user-specified header */
if (do_header) {
struct field *f;
for (f = fields; f != NULL; f = f->next) {
if (f != fields)
(void) printf(" ");
switch (f->fname) {
case F_TTY:
(void) printf("%-*s",
f->width, f->header);
break;
case F_LWPNAME:
case F_FNAME:
case F_COMM:
case F_ARGS:
/*
* Print these headers full width
* unless they appear at the end.
*/
if (f->next != NULL) {
(void) printf("%-*s",
f->width, f->header);
} else {
(void) printf("%s",
f->header);
}
break;
default:
(void) printf("%*s",
f->width, f->header);
break;
}
}
(void) printf("\n");
}
} else { /* print standard header */
/*
* All fields before 'PID' are printed with a trailing space
* as a separator and that is how we print the headers too.
*/
if (lflg) {
if (yflg)
(void) printf("S ");
else
(void) printf(" F S ");
}
if (Zflg)
(void) printf(" ZONE ");
if (fflg) {
(void) printf(" UID ");
} else if (lflg)
(void) printf(" UID ");
(void) printf("%*s", pidwidth, "PID");
if (lflg || fflg)
(void) printf(" %*s", pidwidth, "PPID");
if (jflg)
(void) printf(" %*s %*s", pidwidth, "PGID",
pidwidth, "SID");
if (Lflg)
(void) printf(" LWP");
if (Pflg)
(void) printf(" PSR");
if (Lflg && fflg)
(void) printf(" NLWP");
if (cflg)
(void) printf(" CLS PRI");
else if (lflg || fflg) {
(void) printf(" C");
if (lflg)
(void) printf(" PRI NI");
}
if (lflg) {
if (yflg)
(void) printf(" RSS SZ WCHAN");
else
(void) printf(" ADDR SZ WCHAN");
}
if (fflg)
(void) printf(" %s", loc_stime_str);
if (Hflg)
(void) printf(" LGRP");
if (Lflg)
(void) printf(" TTY LTIME CMD\n");
else
(void) printf(" TTY TIME CMD\n");
}
if (pflg && !(aflg|Aflg|dflg|Gflg|Uflg|uflg|hflg|tflg|gflg|sflg|zflg) &&
npid <= PTHRESHOLD) {
/*
* If we are looking at specific processes go straight
* to their /proc entries and don't scan /proc.
*/
int i;
(void) qsort(pid, npid, sizeof (pid_t), pidcmp);
for (i = 0; i < npid; i++) {
char pname[12];
if (i >= 1 && pid[i] == pid[i - 1])
continue;
(void) sprintf(pname, "%d", (int)pid[i]);
if (print_proc(pname) == 0)
retcode = 0;
}
} else {
/*
* Determine which processes to print info about by searching
* the /proc directory and looking at each process.
*/
if ((dirp = opendir(procdir)) == NULL) {
(void) fprintf(stderr,
gettext("ps: cannot open PROC directory %s\n"),
procdir);
exit(1);
}
/* for each active process --- */
while ((dentp = readdir(dirp)) != NULL) {
if (dentp->d_name[0] == '.') /* skip . and .. */
continue;
if (print_proc(dentp->d_name) == 0)
retcode = 0;
}
(void) closedir(dirp);
}
return (retcode);
}
int
print_proc(char *pid_name)
{
char pname[PATH_MAX];
int pdlen;
int found;
int procfd; /* filedescriptor for /proc/nnnnn/psinfo */
char *tp; /* ptr to ttyname, if any */
psinfo_t info; /* process information from /proc */
lwpsinfo_t *lwpsinfo; /* array of lwpsinfo structs */
pdlen = snprintf(pname, sizeof (pname), "%s/%s/", procdir, pid_name);
if (pdlen >= sizeof (pname) - 10)
return (1);
retry:
(void) strcpy(&pname[pdlen], "psinfo");
if ((procfd = open(pname, O_RDONLY)) == -1) {
/* Process may have exited meanwhile. */
return (1);
}
/*
* Get the info structure for the process and close quickly.
*/
if (read(procfd, (char *)&info, sizeof (info)) < 0) {
int saverr = errno;
(void) close(procfd);
if (saverr == EAGAIN)
goto retry;
if (saverr != ENOENT)
(void) fprintf(stderr,
gettext("ps: read() on %s: %s\n"),
pname, err_string(saverr));
return (1);
}
(void) close(procfd);
found = 0;
if (info.pr_lwp.pr_state == 0) /* can't happen? */
return (1);
/*
* Omit session group leaders for 'a' and 'd' options.
*/
if ((info.pr_pid == info.pr_sid) && (dflg || aflg))
return (1);
if (Aflg || dflg)
found++;
else if (pflg && search(pid, npid, info.pr_pid))
found++; /* ppid in p option arg list */
else if (uflg && ugfind((id_t)info.pr_euid, &euid_tbl))
found++; /* puid in u option arg list */
else if (Uflg && ugfind((id_t)info.pr_uid, &ruid_tbl))
found++; /* puid in U option arg list */
#ifdef NOT_YET
else if (gflg && ugfind((id_t)info.pr_egid, &egid_tbl))
found++; /* pgid in g option arg list */
#endif /* NOT_YET */
else if (Gflg && ugfind((id_t)info.pr_gid, &rgid_tbl))
found++; /* pgid in G option arg list */
else if (gflg && search(grpid, ngrpid, info.pr_pgid))
found++; /* grpid in g option arg list */
else if (sflg && search(sessid, nsessid, info.pr_sid))
found++; /* sessid in s option arg list */
else if (zflg && search(zoneid, nzoneid, info.pr_zoneid))
found++; /* zoneid in z option arg list */
else if (hflg && search((pid_t *)lgrps, nlgrps, info.pr_lwp.pr_lgrp))
found++; /* home lgroup in h option arg list */
if (!found && !tflg && !aflg)
return (1);
if (!prfind(found, &info, &tp))
return (1);
if (Lflg && (info.pr_nlwp + info.pr_nzomb) > 1) {
ssize_t prsz;
(void) strcpy(&pname[pdlen], "lpsinfo");
if ((procfd = open(pname, O_RDONLY)) == -1)
return (1);
/*
* Get the info structures for the lwps.
*/
prsz = read(procfd, lpsinfobuf, lpbufsize);
if (prsz == -1) {
int saverr = errno;
(void) close(procfd);
if (saverr == EAGAIN)
goto retry;
if (saverr != ENOENT)
(void) fprintf(stderr,
gettext("ps: read() on %s: %s\n"),
pname, err_string(saverr));
return (1);
}
(void) close(procfd);
if (prsz == lpbufsize) {
/*
* buffer overflow. Realloc new buffer.
* Error handling is done in Realloc().
*/
lpbufsize *= 2;
lpsinfobuf = Realloc(lpsinfobuf, lpbufsize);
goto retry;
}
if (lpsinfobuf->pr_nent != (info.pr_nlwp + info.pr_nzomb))
goto retry;
lwpsinfo = (lwpsinfo_t *)(lpsinfobuf + 1);
}
if (!Lflg || (info.pr_nlwp + info.pr_nzomb) <= 1) {
prcom(&info, tp);
} else {
int nlwp = 0;
do {
info.pr_lwp = *lwpsinfo;
prcom(&info, tp);
/* LINTED improper alignment */
lwpsinfo = (lwpsinfo_t *)((char *)lwpsinfo +
lpsinfobuf->pr_entsize);
} while (++nlwp < lpsinfobuf->pr_nent);
}
return (0);
}
static int
field_cmp(const void *l, const void *r)
{
struct def_field *lhs = *((struct def_field **)l);
struct def_field *rhs = *((struct def_field **)r);
return (strcmp(lhs->fname, rhs->fname));
}
static void
usage(void) /* print usage message and quit */
{
struct def_field *df, *sorted[NFIELDS];
int pos = 80, i = 0;
static char usage1[] =
"ps [ -aAdefHlcjLPWyZ ] [ -o format ] [ -t termlist ]";
static char usage2[] =
"\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]";
static char usage3[] =
"\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ]";
static char usage4[] =
"\t[ -z zonelist ] [-h lgrplist]";
static char usage5[] =
" 'format' is one or more of:";
(void) fprintf(stderr,
gettext("usage: %s\n%s\n%s\n%s\n%s"),
gettext(usage1), gettext(usage2), gettext(usage3),
gettext(usage4), gettext(usage5));
/*
* Now print out the possible output formats such that they neatly fit
* into eighty columns. Note that the fact that we are determining
* this output programmatically means that a gettext() is impossible --
* but it would be a mistake to localize the output formats anyway as
* they are tokens for input, not output themselves.
*/
for (df = &fname[0]; df < &fname[NFIELDS]; df++)
sorted[i++] = df;
(void) qsort(sorted, NFIELDS, sizeof (void *), field_cmp);
for (i = 0; i < NFIELDS; i++) {
if (pos + strlen((df = sorted[i])->fname) + 1 >= 80) {
(void) fprintf(stderr, "\n\t");
pos = 8;
}
(void) fprintf(stderr, "%s%s", pos > 8 ? " " : "", df->fname);
pos += strlen(df->fname) + 1;
}
(void) fprintf(stderr, "\n");
exit(1);
}
/*
* getarg() finds the next argument in list and copies arg into argbuf.
* p1 first pts to arg passed back from getopt routine. p1 is then
* bumped to next character that is not a comma or blank -- p1 NULL
* indicates end of list.
*/
static char *
getarg(char **pp1)
{
static char argbuf[ARGSIZ];
char *p1 = *pp1;
char *parga = argbuf;
int c;
while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
p1++;
while ((c = *p1) != '\0' && c != ',' && !isspace(c)) {
if (parga < argbuf + ARGSIZ - 1)
*parga++ = c;
p1++;
}
*parga = '\0';
while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
p1++;
*pp1 = p1;
return (argbuf);
}
/*
* parse_format() takes the argument to the -o option,
* sets up the next output field structure, and returns
* a pointer to any further output field specifier(s).
* As a side-effect, it increments errflg if encounters a format error.
*/
static char *
parse_format(char *arg)
{
int c;
char *name;
char *header = NULL;
int width = 0;
struct def_field *df;
struct field *f;
while ((c = *arg) != '\0' && (c == ',' || isspace(c)))
arg++;
if (c == '\0')
return (NULL);
name = arg;
arg = strpbrk(arg, " \t\r\v\f\n,=");
if (arg != NULL) {
c = *arg;
*arg++ = '\0';
if (c == '=') {
char *s;
header = arg;
arg = NULL;
width = strlen(header);
s = header + width;
while (s > header && isspace(*--s))
*s = '\0';
while (isspace(*header))
header++;
}
}
for (df = &fname[0]; df < &fname[NFIELDS]; df++)
if (strcmp(name, df->fname) == 0) {
if (strcmp(name, "lwp") == 0 ||
strcmp(name, "lwpname") == 0)
Lflg++;
break;
}
if (df >= &fname[NFIELDS]) {
(void) fprintf(stderr,
gettext("ps: unknown output format: -o %s\n"),
name);
errflg++;
return (arg);
}
if ((f = malloc(sizeof (*f))) == NULL) {
(void) fprintf(stderr,
gettext("ps: malloc() for output format failed, %s\n"),
err_string(errno));
exit(1);
}
f->next = NULL;
f->fname = df - &fname[0];
f->header = header? header : df->header;
if (width == 0)
width = df->width;
if (*f->header != '\0')
do_header = 1;
f->width = max(width, df->minwidth);
if (fields == NULL)
fields = last_field = f;
else {
last_field->next = f;
last_field = f;
}
return (arg);
}
static char *
devlookup(dev_t ddev)
{
struct devl *dp;
int i;
for (dp = devl, i = 0; i < ndev; dp++, i++) {
if (dp->ddev == ddev)
return (dp->dname);
}
return (NULL);
}
static char *
devadd(char *name, dev_t ddev)
{
struct devl *dp;
int leng, start, i;
if (ndev == maxdev) {
maxdev += DNINCR;
devl = Realloc(devl, maxdev * sizeof (struct devl));
}
dp = &devl[ndev++];
dp->ddev = ddev;
if (name == NULL) {
(void) strcpy(dp->dname, "??");
return (dp->dname);
}
leng = strlen(name);
/* Strip off /dev/ */
if (leng < DNSIZE + 4)
(void) strcpy(dp->dname, &name[5]);
else {
start = leng - DNSIZE - 1;
for (i = start; i < leng && name[i] != '/'; i++)
;
if (i == leng)
(void) strncpy(dp->dname, &name[start], DNSIZE);
else
(void) strncpy(dp->dname, &name[i+1], DNSIZE);
}
return (dp->dname);
}
/*
* gettty returns the user's tty number or ? if none.
*/
static char *
gettty(psinfo_t *psinfo)
{
extern char *_ttyname_dev(dev_t, char *, size_t);
static zoneid_t zid = -1;
char devname[TTYNAME_MAX];
char *retval;
if (zid == -1)
zid = getzoneid();
if (psinfo->pr_ttydev == PRNODEV || psinfo->pr_zoneid != zid)
return ("?");
if ((retval = devlookup(psinfo->pr_ttydev)) != NULL)
return (retval);
retval = _ttyname_dev(psinfo->pr_ttydev, devname, sizeof (devname));
return (devadd(retval, psinfo->pr_ttydev));
}
/*
* Find the process's tty and return 1 if process is to be printed.
*/
static int
prfind(int found, psinfo_t *psinfo, char **tpp)
{
char *tp;
struct tty *ttyp;
if (psinfo->pr_nlwp == 0) {
/* process is a zombie */
*tpp = "?";
if (tflg && !found)
return (0);
return (1);
}
/*
* Get current terminal. If none ("?") and 'a' is set, don't print
* info. If 't' is set, check if term is in list of desired terminals
* and print it if it is.
*/
tp = gettty(psinfo);
if (aflg && *tp == '?') {
*tpp = tp;
return (0);
}
if (tflg && !found) {
int match = 0;
char *other = NULL;
for (ttyp = tty; ttyp->tname != NULL; ttyp++) {
/*
* Look for a name match
*/
if (strcmp(tp, ttyp->tname) == 0) {
match = 1;
break;
}
/*
* Look for same device under different names.
*/
if ((other == NULL) &&
(ttyp->tdev != PRNODEV) &&
(psinfo->pr_ttydev == ttyp->tdev))
other = ttyp->tname;
}
if (!match && (other != NULL)) {
/*
* found under a different name
*/
match = 1;
tp = other;
}
if (!match || (tuid != (uid_t)-1 && tuid != psinfo->pr_euid)) {
/*
* not found OR not matching euid
*/
*tpp = tp;
return (0);
}
}
*tpp = tp;
return (1);
}
/*
* Print info about the process.
*/
static void
prcom(psinfo_t *psinfo, char *ttyp)
{
long tm;
int wcnt;
struct passwd *pwd;
int zombie_lwp;
char zonename[ZONENAME_MAX];
char psargs[PRMAXARGVLEN] = "";
/*
* If process is zombie, call zombie print routine and return.
*/
if (psinfo->pr_nlwp == 0) {
if (fields != NULL)
pr_fields(psinfo, ttyp, print_zombie_field);
else
przom(psinfo);
return;
}
zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
/*
* If user specified '-o format', print requested fields and return.
*/
if (fields != NULL) {
pr_fields(psinfo, ttyp, print_field);
return;
}
/*
* All fields before 'PID' are printed with a trailing space as a
* separator, rather than keeping track of which column is first. All
* other fields are printed with a leading space.
*/
if (lflg) {
if (!yflg)
(void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
(void) printf("%c ", psinfo->pr_lwp.pr_sname); /* S */
}
if (Zflg) { /* ZONE */
if (getzonenamebyid(psinfo->pr_zoneid, zonename,
sizeof (zonename)) < 0) {
if (snprintf(NULL, 0, "%d",
((int)psinfo->pr_zoneid)) > 7)
(void) printf(" %6.6d%c ",
((int)psinfo->pr_zoneid), '*');
else
(void) printf(" %7.7d ",
((int)psinfo->pr_zoneid));
} else {
size_t nw;
nw = mbstowcs(NULL, zonename, 0);
if (nw == (size_t)-1)
(void) printf("%8.8s ", "ERROR");
else if (nw > 8)
(void) wprintf(L"%7.7s%c ", zonename, '*');
else
(void) wprintf(L"%8.8s ", zonename);
}
}
if (fflg) { /* UID */
if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
size_t nw;
nw = mbstowcs(NULL, pwd->pw_name, 0);
if (nw == (size_t)-1)
(void) printf("%8.8s ", "ERROR");
else if (nw > 8)
(void) wprintf(L"%7.7s%c ", pwd->pw_name, '*');
else
(void) wprintf(L"%8.8s ", pwd->pw_name);
} else {
if (snprintf(NULL, 0, "%u",
(psinfo->pr_euid)) > 7)
(void) printf(" %6.6u%c ", psinfo->pr_euid,
'*');
else
(void) printf(" %7.7u ", psinfo->pr_euid);
}
} else if (lflg) {
if (snprintf(NULL, 0, "%u", (psinfo->pr_euid)) > 6)
(void) printf("%5.5u%c ", psinfo->pr_euid, '*');
else
(void) printf("%6u ", psinfo->pr_euid);
}
(void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
if (lflg || fflg)
(void) printf(" %*d", pidwidth,
(int)psinfo->pr_ppid); /* PPID */
if (jflg) {
(void) printf(" %*d", pidwidth,
(int)psinfo->pr_pgid); /* PGID */
(void) printf(" %*d", pidwidth,
(int)psinfo->pr_sid); /* SID */
}
if (Lflg)
(void) printf(" %5d", (int)psinfo->pr_lwp.pr_lwpid); /* LWP */
if (Pflg) {
if (psinfo->pr_lwp.pr_bindpro == PBIND_NONE) /* PSR */
(void) printf(" -");
else
(void) printf(" %3d", psinfo->pr_lwp.pr_bindpro);
}
if (Lflg && fflg) /* NLWP */
(void) printf(" %5d", psinfo->pr_nlwp + psinfo->pr_nzomb);
if (cflg) {
if (zombie_lwp) /* CLS */
(void) printf(" ");
else
(void) printf(" %4s", psinfo->pr_lwp.pr_clname);
(void) printf(" %3d", psinfo->pr_lwp.pr_pri); /* PRI */
} else if (lflg || fflg) {
(void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C */
if (lflg) { /* PRI NI */
/*
* Print priorities the old way (lower numbers
* mean higher priority) and print nice value
* for time sharing procs.
*/
(void) printf(" %3d", psinfo->pr_lwp.pr_oldpri);
if (psinfo->pr_lwp.pr_oldpri != 0)
(void) printf(" %2d", psinfo->pr_lwp.pr_nice);
else
(void) printf(" %2.2s",
psinfo->pr_lwp.pr_clname);
}
}
if (lflg) {
if (yflg) {
if (psinfo->pr_flag & SSYS) /* RSS */
(void) printf(" 0");
else if (psinfo->pr_rssize)
(void) printf(" %5lu",
(ulong_t)psinfo->pr_rssize);
else
(void) printf(" ?");
if (psinfo->pr_flag & SSYS) /* SZ */
(void) printf(" 0");
else if (psinfo->pr_size)
(void) printf(" %6lu",
(ulong_t)psinfo->pr_size);
else
(void) printf(" ?");
} else {
#ifndef _LP64
if (psinfo->pr_addr) /* ADDR */
(void) printf(" %8lx",
(ulong_t)psinfo->pr_addr);
else
#endif
(void) printf(" ?");
if (psinfo->pr_flag & SSYS) /* SZ */
(void) printf(" 0");
else if (psinfo->pr_size)
(void) printf(" %6lu",
(ulong_t)psinfo->pr_size / kbytes_per_page);
else
(void) printf(" ?");
}
if (psinfo->pr_lwp.pr_sname != 'S') /* WCHAN */
(void) printf(" ");
#ifndef _LP64
else if (psinfo->pr_lwp.pr_wchan)
(void) printf(" %8lx",
(ulong_t)psinfo->pr_lwp.pr_wchan);
#endif
else
(void) printf(" ?");
}
if (fflg) { /* STIME */
int width = fname[F_STIME].width;
if (Lflg)
prtime(psinfo->pr_lwp.pr_start, width + 1, 1);
else
prtime(psinfo->pr_start, width + 1, 1);
}
if (Hflg) {
/* Display home lgroup */
(void) printf(" %4d", (int)psinfo->pr_lwp.pr_lgrp);
}
(void) printf(" %-8.14s", ttyp); /* TTY */
if (Lflg) {
tm = psinfo->pr_lwp.pr_time.tv_sec;
if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
tm++;
} else {
tm = psinfo->pr_time.tv_sec;
if (psinfo->pr_time.tv_nsec > 500000000)
tm++;
}
(void) printf(" %4ld:%.2ld ", tm / 60, tm % 60); /* [L]TIME */
if (zombie_lwp) {
(void) printf("<defunct>\n");
return;
}
if (!fflg) { /* CMD */
wcnt = namencnt(psinfo->pr_fname, 16, 8);
(void) printf("%.*s\n", wcnt, psinfo->pr_fname);
return;
}
get_psargs(false, fflg, psinfo, psargs, sizeof (psargs));
print_psargs(psargs, 0);
printf("\n");
}
/*
* Print percent from 16-bit binary fraction [0 .. 1]
* Round up .01 to .1 to indicate some small percentage (the 0x7000 below).
*/
static void
prtpct(ushort_t pct, int width)
{
uint_t value = pct; /* need 32 bits to compute with */
value = ((value * 1000) + 0x7000) >> 15; /* [0 .. 1000] */
if (value >= 1000)
value = 999;
if ((width -= 2) < 2)
width = 2;
(void) printf("%*u.%u", width, value / 10, value % 10);
}
static void
print_time(time_t tim, int width)
{
char buf[30];
time_t seconds;
time_t minutes;
time_t hours;
time_t days;
if (tim < 0) {
(void) printf("%*s", width, "-");
return;
}
seconds = tim % 60;
tim /= 60;
minutes = tim % 60;
tim /= 60;
hours = tim % 24;
days = tim / 24;
if (days > 0) {
(void) snprintf(buf, sizeof (buf), "%ld-%2.2ld:%2.2ld:%2.2ld",
days, hours, minutes, seconds);
} else if (hours > 0) {
(void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld:%2.2ld",
hours, minutes, seconds);
} else {
(void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld",
minutes, seconds);
}
(void) printf("%*s", width, buf);
}
void
get_psargs(bool comm, bool full, psinfo_t *psinfo, char *buf, size_t bufsize)
{
char *path = NULL;
ssize_t size = 0;
char *cp;
int fd;
assert(psinfo->pr_psargs[PRARGSZ - 1] == '\0');
if (full && getenv("SHORT_PSARGS") == NULL &&
asprintf(&path, "%s/%d/cmdline", procdir,
(int)psinfo->pr_pid) != -1 && (fd = open(path, O_RDONLY)) != -1) {
size = read(fd, buf, bufsize);
(void) close(fd);
}
free(path);
if (size <= 0) {
(void) strlcpy(buf, psinfo->pr_psargs, bufsize);
} else {
ssize_t i;
buf[bufsize - 1] = '\0';
for (cp = buf; cp - buf < size; cp++) {
if (*cp == '\0' && (cp - buf) + 1 < size)
*cp = ' ';
}
for (i = strlen(buf) - 1; i >= 0 && isspace(buf[i]); i--) {
buf[i] = '\0';
}
}
if (comm && (cp = strpbrk(buf, " \t\r\v\f\n")) != NULL)
*cp = '\0';
}
void
print_psargs(char *psargs, int width)
{
int bytesleft;
int length;
char *cp;
int wcnt;
bytesleft = strlen(psargs);
for (cp = psargs; *cp != '\0'; cp += length) {
wchar_t wchar;
length = mbtowc(&wchar, cp, MB_LEN_MAX);
if (length == 0)
break;
if (length < 0 || !iswprint(wchar)) {
if (length < 0)
length = 1;
if (bytesleft <= length) {
*cp = '\0';
break;
}
/* omit the unprintable character */
(void) memmove(cp, cp + length, bytesleft - length);
bytesleft -= length;
length = 0;
}
bytesleft -= length;
}
wcnt = namencnt(psargs, PRMAXARGVLEN, width);
if (width != 0) {
(void) printf("%.*s", width, psargs);
} else {
(void) printf("%-.*s", wcnt, psargs);
}
}
static void
print_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
{
char psargs[PRMAXARGVLEN] = "";
int width = f->width;
struct passwd *pwd;
struct group *grp;
time_t cputime;
int wcnt;
ulong_t mask;
int zombie_lwp;
zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
switch (f->fname) {
case F_RUSER:
if ((pwd = getpwuid(psinfo->pr_uid)) != NULL) {
size_t nw;
nw = mbstowcs(NULL, pwd->pw_name, 0);
if (nw == (size_t)-1)
(void) printf("%*s ", width, "ERROR");
else if (Wflg && nw > width)
(void) wprintf(L"%.*s%c", width - 1,
pwd->pw_name, '*');
else
(void) wprintf(L"%*s", width, pwd->pw_name);
} else {
if (Wflg && snprintf(NULL, 0, "%u",
(psinfo->pr_uid)) > width)
(void) printf("%*u%c", width - 1,
psinfo->pr_uid, '*');
else
(void) printf("%*u", width, psinfo->pr_uid);
}
break;
case F_USER:
if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
size_t nw;
nw = mbstowcs(NULL, pwd->pw_name, 0);
if (nw == (size_t)-1)
(void) printf("%*s ", width, "ERROR");
else if (Wflg && nw > width)
(void) wprintf(L"%.*s%c", width - 1,
pwd->pw_name, '*');
else
(void) wprintf(L"%*s", width, pwd->pw_name);
} else {
if (Wflg && snprintf(NULL, 0, "%u",
(psinfo->pr_euid)) > width)
(void) printf("%*u%c", width - 1,
psinfo->pr_euid, '*');
else
(void) printf("%*u", width, psinfo->pr_euid);
}
break;
case F_RGROUP:
if ((grp = getgrgid(psinfo->pr_gid)) != NULL)
(void) printf("%*s", width, grp->gr_name);
else
(void) printf("%*u", width, psinfo->pr_gid);
break;
case F_GROUP:
if ((grp = getgrgid(psinfo->pr_egid)) != NULL)
(void) printf("%*s", width, grp->gr_name);
else
(void) printf("%*u", width, psinfo->pr_egid);
break;
case F_RUID:
(void) printf("%*u", width, psinfo->pr_uid);
break;
case F_UID:
(void) printf("%*u", width, psinfo->pr_euid);
break;
case F_RGID:
(void) printf("%*u", width, psinfo->pr_gid);
break;
case F_GID:
(void) printf("%*u", width, psinfo->pr_egid);
break;
case F_PID:
(void) printf("%*d", width, (int)psinfo->pr_pid);
break;
case F_PPID:
(void) printf("%*d", width, (int)psinfo->pr_ppid);
break;
case F_PGID:
(void) printf("%*d", width, (int)psinfo->pr_pgid);
break;
case F_SID:
(void) printf("%*d", width, (int)psinfo->pr_sid);
break;
case F_PSR:
if (zombie_lwp || psinfo->pr_lwp.pr_bindpro == PBIND_NONE)
(void) printf("%*s", width, "-");
else
(void) printf("%*d", width, psinfo->pr_lwp.pr_bindpro);
break;
case F_LWP:
(void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lwpid);
break;
case F_LWPNAME: {
char lwpname[THREAD_NAME_MAX] = "";
char *path = NULL;
int fd;
if (asprintf(&path, "%s/%d/lwp/%d/lwpname", procdir,
(int)psinfo->pr_pid, (int)psinfo->pr_lwp.pr_lwpid) != -1 &&
(fd = open(path, O_RDONLY)) != -1) {
(void) read(fd, lwpname, sizeof (lwpname));
lwpname[THREAD_NAME_MAX - 1] = '\0';
(void) close(fd);
}
free(path);
if (f->next != NULL)
(void) printf("%-*s", width, lwpname);
else
(void) printf("%s", lwpname);
break;
}
case F_NLWP:
(void) printf("%*d", width, psinfo->pr_nlwp + psinfo->pr_nzomb);
break;
case F_OPRI:
if (zombie_lwp)
(void) printf("%*s", width, "-");
else
(void) printf("%*d", width, psinfo->pr_lwp.pr_oldpri);
break;
case F_PRI:
if (zombie_lwp)
(void) printf("%*s", width, "-");
else
(void) printf("%*d", width, psinfo->pr_lwp.pr_pri);
break;
case F_F:
mask = 0xffffffffUL;
if (width < 8)
mask >>= (8 - width) * 4;
(void) printf("%*lx", width, psinfo->pr_flag & mask);
break;
case F_S:
(void) printf("%*c", width, psinfo->pr_lwp.pr_sname);
break;
case F_C:
if (zombie_lwp)
(void) printf("%*s", width, "-");
else
(void) printf("%*d", width, psinfo->pr_lwp.pr_cpu);
break;
case F_PCPU:
if (zombie_lwp)
(void) printf("%*s", width, "-");
else if (Lflg)
prtpct(psinfo->pr_lwp.pr_pctcpu, width);
else
prtpct(psinfo->pr_pctcpu, width);
break;
case F_PMEM:
prtpct(psinfo->pr_pctmem, width);
break;
case F_OSZ:
(void) printf("%*lu", width,
(ulong_t)psinfo->pr_size / kbytes_per_page);
break;
case F_VSZ:
(void) printf("%*lu", width, (ulong_t)psinfo->pr_size);
break;
case F_RSS:
(void) printf("%*lu", width, (ulong_t)psinfo->pr_rssize);
break;
case F_NICE:
/* if pr_oldpri is zero, then this class has no nice */
if (zombie_lwp)
(void) printf("%*s", width, "-");
else if (psinfo->pr_lwp.pr_oldpri != 0)
(void) printf("%*d", width, psinfo->pr_lwp.pr_nice);
else
(void) printf("%*.*s", width, width,
psinfo->pr_lwp.pr_clname);
break;
case F_CLASS:
if (zombie_lwp)
(void) printf("%*s", width, "-");
else
(void) printf("%*.*s", width, width,
psinfo->pr_lwp.pr_clname);
break;
case F_STIME:
if (Lflg)
prtime(psinfo->pr_lwp.pr_start, width, 0);
else
prtime(psinfo->pr_start, width, 0);
break;
case F_ETIME:
if (Lflg)
print_time(delta_secs(&psinfo->pr_lwp.pr_start),
width);
else
print_time(delta_secs(&psinfo->pr_start), width);
break;
case F_TIME:
if (Lflg) {
cputime = psinfo->pr_lwp.pr_time.tv_sec;
if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
cputime++;
} else {
cputime = psinfo->pr_time.tv_sec;
if (psinfo->pr_time.tv_nsec > 500000000)
cputime++;
}
print_time(cputime, width);
break;
case F_TTY:
(void) printf("%-*s", width, ttyp);
break;
case F_ADDR:
if (zombie_lwp)
(void) printf("%*s", width, "-");
else if (Lflg)
(void) printf("%*lx", width,
(long)psinfo->pr_lwp.pr_addr);
else
(void) printf("%*lx", width, (long)psinfo->pr_addr);
break;
case F_WCHAN:
if (!zombie_lwp && psinfo->pr_lwp.pr_wchan)
(void) printf("%*lx", width,
(long)psinfo->pr_lwp.pr_wchan);
else
(void) printf("%*.*s", width, width, "-");
break;
case F_FNAME:
/*
* Print full width unless this is the last output format.
*/
if (zombie_lwp) {
if (f->next != NULL)
(void) printf("%-*s", width, "<defunct>");
else
(void) printf("%s", "<defunct>");
break;
}
wcnt = namencnt(psinfo->pr_fname, 16, width);
if (f->next != NULL)
(void) printf("%-*.*s", width, wcnt, psinfo->pr_fname);
else
(void) printf("%-.*s", wcnt, psinfo->pr_fname);
break;
case F_COMM:
if (zombie_lwp) {
if (f->next != NULL)
(void) printf("%-*s", width, "<defunct>");
else
(void) printf("%s", "<defunct>");
break;
}
get_psargs(true, false, psinfo, psargs, sizeof (psargs));
print_psargs(psargs, f->next != NULL ? width : 0);
break;
case F_ARGS:
/*
* PRARGSZ == length of cmd arg string.
*/
if (zombie_lwp) {
(void) printf("%-*s", width, "<defunct>");
break;
}
get_psargs(false, fflg, psinfo, psargs, sizeof (psargs));
print_psargs(psargs, f->next != NULL ? width : 0);
break;
case F_TASKID:
(void) printf("%*d", width, (int)psinfo->pr_taskid);
break;
case F_PROJID:
(void) printf("%*d", width, (int)psinfo->pr_projid);
break;
case F_PROJECT:
{
struct project cproj;
char proj_buf[PROJECT_BUFSZ];
if ((getprojbyid(psinfo->pr_projid, &cproj,
(void *)&proj_buf, PROJECT_BUFSZ)) == NULL) {
if (Wflg && snprintf(NULL, 0, "%d",
((int)psinfo->pr_projid)) > width)
(void) printf("%.*d%c", width - 1,
((int)psinfo->pr_projid), '*');
else
(void) printf("%*d", width,
(int)psinfo->pr_projid);
} else {
size_t nw;
if (cproj.pj_name != NULL)
nw = mbstowcs(NULL, cproj.pj_name, 0);
if (cproj.pj_name == NULL)
(void) printf("%*s ", width, "---");
else if (nw == (size_t)-1)
(void) printf("%*s ", width, "ERROR");
else if (Wflg && nw > width)
(void) wprintf(L"%.*s%c", width - 1,
cproj.pj_name, '*');
else
(void) wprintf(L"%*s", width,
cproj.pj_name);
}
}
break;
case F_PSET:
if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE)
(void) printf("%*s", width, "-");
else
(void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset);
break;
case F_ZONEID:
(void) printf("%*d", width, (int)psinfo->pr_zoneid);
break;
case F_ZONE:
{
char zonename[ZONENAME_MAX];
if (getzonenamebyid(psinfo->pr_zoneid, zonename,
sizeof (zonename)) < 0) {
if (Wflg && snprintf(NULL, 0, "%d",
((int)psinfo->pr_zoneid)) > width)
(void) printf("%.*d%c", width - 1,
((int)psinfo->pr_zoneid), '*');
else
(void) printf("%*d", width,
(int)psinfo->pr_zoneid);
} else {
size_t nw;
nw = mbstowcs(NULL, zonename, 0);
if (nw == (size_t)-1)
(void) printf("%*s ", width, "ERROR");
else if (Wflg && nw > width)
(void) wprintf(L"%.*s%c", width - 1,
zonename, '*');
else
(void) wprintf(L"%*s", width, zonename);
}
}
break;
case F_CTID:
if (psinfo->pr_contract == -1)
(void) printf("%*s", width, "-");
else
(void) printf("%*ld", width, (long)psinfo->pr_contract);
break;
case F_LGRP:
/* Display home lgroup */
(void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lgrp);
break;
case F_DMODEL:
(void) printf("%*s", width,
psinfo->pr_dmodel == PR_MODEL_LP64 ? "_LP64" : "_ILP32");
break;
}
}
static void
print_zombie_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
{
int wcnt;
int width = f->width;
switch (f->fname) {
case F_FNAME:
case F_COMM:
case F_ARGS:
/*
* Print full width unless this is the last output format.
*/
wcnt = min(width, sizeof ("<defunct>"));
if (f->next != NULL)
(void) printf("%-*.*s", width, wcnt, "<defunct>");
else
(void) printf("%-.*s", wcnt, "<defunct>");
break;
case F_PSR:
case F_PCPU:
case F_PMEM:
case F_NICE:
case F_CLASS:
case F_STIME:
case F_ETIME:
case F_WCHAN:
case F_PSET:
(void) printf("%*s", width, "-");
break;
case F_OPRI:
case F_PRI:
case F_OSZ:
case F_VSZ:
case F_RSS:
(void) printf("%*d", width, 0);
break;
default:
print_field(psinfo, f, ttyp);
break;
}
}
static void
pr_fields(psinfo_t *psinfo, const char *ttyp,
void (*print_fld)(psinfo_t *, struct field *, const char *))
{
struct field *f;
for (f = fields; f != NULL; f = f->next) {
print_fld(psinfo, f, ttyp);
if (f->next != NULL)
(void) printf(" ");
}
(void) printf("\n");
}
/*
* Returns 1 if arg is found in array arr, of length num; 0 otherwise.
*/
static int
search(pid_t *arr, int number, pid_t arg)
{
int i;
for (i = 0; i < number; i++)
if (arg == arr[i])
return (1);
return (0);
}
/*
* Add an entry (user, group) to the specified table.
*/
static void
add_ugentry(struct ughead *tbl, char *name)
{
struct ugdata *entp;
if (tbl->size == tbl->nent) { /* reallocate the table entries */
if ((tbl->size *= 2) == 0)
tbl->size = 32; /* first time */
tbl->ent = Realloc(tbl->ent, tbl->size*sizeof (struct ugdata));
}
entp = &tbl->ent[tbl->nent++];
entp->id = 0;
(void) strncpy(entp->name, name, MAXUGNAME);
entp->name[MAXUGNAME] = '\0';
}
static int
uconv(struct ughead *uhead)
{
struct ugdata *utbl = uhead->ent;
int n = uhead->nent;
struct passwd *pwd;
int i;
int fnd = 0;
uid_t uid;
/*
* Ask the name service for names.
*/
for (i = 0; i < n; i++) {
/*
* If name is numeric, ask for numeric id
*/
if (str2uid(utbl[i].name, &uid, 0, MAXEPHUID) == 0)
pwd = getpwuid(uid);
else
pwd = getpwnam(utbl[i].name);
/*
* If found, enter found index into tbl array.
*/
if (pwd == NULL) {
(void) fprintf(stderr,
gettext("ps: unknown user %s\n"), utbl[i].name);
continue;
}
utbl[fnd].id = pwd->pw_uid;
(void) strncpy(utbl[fnd].name, pwd->pw_name, MAXUGNAME);
fnd++;
}
uhead->nent = fnd; /* in case it changed */
return (n - fnd);
}
static int
gconv(struct ughead *ghead)
{
struct ugdata *gtbl = ghead->ent;
int n = ghead->nent;
struct group *grp;
gid_t gid;
int i;
int fnd = 0;
/*
* Ask the name service for names.
*/
for (i = 0; i < n; i++) {
/*
* If name is numeric, ask for numeric id
*/
if (str2uid(gtbl[i].name, (uid_t *)&gid, 0, MAXEPHUID) == 0)
grp = getgrgid(gid);
else
grp = getgrnam(gtbl[i].name);
/*
* If found, enter found index into tbl array.
*/
if (grp == NULL) {
(void) fprintf(stderr,
gettext("ps: unknown group %s\n"), gtbl[i].name);
continue;
}
gtbl[fnd].id = grp->gr_gid;
(void) strncpy(gtbl[fnd].name, grp->gr_name, MAXUGNAME);
fnd++;
}
ghead->nent = fnd; /* in case it changed */
return (n - fnd);
}
/*
* Return 1 if puid is in table, otherwise 0.
*/
static int
ugfind(id_t id, struct ughead *ughead)
{
struct ugdata *utbl = ughead->ent;
int n = ughead->nent;
int i;
for (i = 0; i < n; i++)
if (utbl[i].id == id)
return (1);
return (0);
}
/*
* Print starting time of process unless process started more than 24 hours
* ago, in which case the date is printed. The date is printed in the form
* "MMM dd" if old format, else the blank is replaced with an '_' so
* it appears as a single word (for parseability).
*/
static void
prtime(timestruc_t st, int width, int old)
{
char sttim[26];
time_t starttime;
starttime = st.tv_sec;
if (st.tv_nsec > 500000000)
starttime++;
if ((now.tv_sec - starttime) >= 24*60*60) {
(void) strftime(sttim, sizeof (sttim), old?
/*
* TRANSLATION_NOTE
* This time format is used by STIME field when -f option
* is specified. Used for processes that begun more than
* 24 hours.
*/
dcgettext(NULL, "%b %d", LC_TIME) :
/*
* TRANSLATION_NOTE
* This time format is used by STIME field when -o option
* is specified. Used for processes that begun more than
* 24 hours.
*/
dcgettext(NULL, "%b_%d", LC_TIME), localtime(&starttime));
} else {
/*
* TRANSLATION_NOTE
* This time format is used by STIME field when -f or -o option
* is specified. Used for processes that begun less than
* 24 hours.
*/
(void) strftime(sttim, sizeof (sttim),
dcgettext(NULL, "%H:%M:%S", LC_TIME),
localtime(&starttime));
}
(void) printf("%*.*s", width, width, sttim);
}
static void
przom(psinfo_t *psinfo)
{
long tm;
struct passwd *pwd;
char zonename[ZONENAME_MAX];
/*
* All fields before 'PID' are printed with a trailing space as a
* spearator, rather than keeping track of which column is first. All
* other fields are printed with a leading space.
*/
if (lflg) { /* F S */
if (!yflg)
(void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
(void) printf("%c ", psinfo->pr_lwp.pr_sname); /* S */
}
if (Zflg) {
if (getzonenamebyid(psinfo->pr_zoneid, zonename,
sizeof (zonename)) < 0) {
if (snprintf(NULL, 0, "%d",
((int)psinfo->pr_zoneid)) > 7)
(void) printf(" %6.6d%c ",
((int)psinfo->pr_zoneid), '*');
else
(void) printf(" %7.7d ",
((int)psinfo->pr_zoneid));
} else {
size_t nw;
nw = mbstowcs(NULL, zonename, 0);
if (nw == (size_t)-1)
(void) printf("%8.8s ", "ERROR");
else if (nw > 8)
(void) wprintf(L"%7.7s%c ", zonename, '*');
else
(void) wprintf(L"%8.8s ", zonename);
}
}
if (Hflg) {
/* Display home lgroup */
(void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */
}
if (fflg) {
if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
size_t nw;
nw = mbstowcs(NULL, pwd->pw_name, 0);
if (nw == (size_t)-1)
(void) printf("%8.8s ", "ERROR");
else if (nw > 8)
(void) wprintf(L"%7.7s%c ", pwd->pw_name, '*');
else
(void) wprintf(L"%8.8s ", pwd->pw_name);
} else {
if (snprintf(NULL, 0, "%u",
(psinfo->pr_euid)) > 7)
(void) printf(" %6.6u%c ", psinfo->pr_euid,
'*');
else
(void) printf(" %7.7u ", psinfo->pr_euid);
}
} else if (lflg) {
if (snprintf(NULL, 0, "%u", (psinfo->pr_euid)) > 6)
(void) printf("%5.5u%c ", psinfo->pr_euid, '*');
else
(void) printf("%6u ", psinfo->pr_euid);
}
(void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
if (lflg || fflg)
(void) printf(" %*d", pidwidth,
(int)psinfo->pr_ppid); /* PPID */
if (jflg) {
(void) printf(" %*d", pidwidth,
(int)psinfo->pr_pgid); /* PGID */
(void) printf(" %*d", pidwidth,
(int)psinfo->pr_sid); /* SID */
}
if (Lflg)
(void) printf(" %5d", 0); /* LWP */
if (Pflg)
(void) printf(" -"); /* PSR */
if (Lflg && fflg)
(void) printf(" %5d", 0); /* NLWP */
if (cflg) {
(void) printf(" %4s", "-"); /* zombies have no class */
(void) printf(" %3d", psinfo->pr_lwp.pr_pri); /* PRI */
} else if (lflg || fflg) {
(void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C */
if (lflg)
(void) printf(" %3d %2s",
psinfo->pr_lwp.pr_oldpri, "-"); /* PRI NI */
}
if (lflg) {
if (yflg) /* RSS SZ WCHAN */
(void) printf(" %5d %6d %8s", 0, 0, "-");
else /* ADDR SZ WCHAN */
(void) printf(" %8s %6d %8s", "-", 0, "-");
}
if (fflg) {
int width = fname[F_STIME].width;
(void) printf(" %*.*s", width, width, "-"); /* STIME */
}
(void) printf(" %-8.14s", "?"); /* TTY */
tm = psinfo->pr_time.tv_sec;
if (psinfo->pr_time.tv_nsec > 500000000)
tm++;
(void) printf(" %4ld:%.2ld", tm / 60, tm % 60); /* TIME */
(void) printf(" <defunct>\n");
}
/*
* Function to compute the number of printable bytes in a multibyte
* command string ("internationalization").
*/
static int
namencnt(char *cmd, int csisize, int scrsize)
{
int csiwcnt = 0, scrwcnt = 0;
int ncsisz, nscrsz;
wchar_t wchar;
int len;
while (*cmd != '\0') {
if ((len = csisize - csiwcnt) > (int)MB_CUR_MAX)
len = MB_CUR_MAX;
if ((ncsisz = mbtowc(&wchar, cmd, len)) < 0)
return (8); /* default to use for illegal chars */
if ((nscrsz = wcwidth(wchar)) <= 0)
return (8);
if (csiwcnt + ncsisz > csisize ||
(scrsize != 0 && scrwcnt + nscrsz > scrsize))
break;
csiwcnt += ncsisz;
scrwcnt += nscrsz;
cmd += ncsisz;
}
return (csiwcnt);
}
static char *
err_string(int err)
{
static char buf[32];
char *str = strerror(err);
if (str == NULL)
(void) snprintf(str = buf, sizeof (buf), "Errno #%d", err);
return (str);
}
/* If allocation fails, die */
static void *
Realloc(void *ptr, size_t size)
{
ptr = realloc(ptr, size);
if (ptr == NULL) {
(void) fprintf(stderr, gettext("ps: no memory\n"));
exit(1);
}
return (ptr);
}
static time_t
delta_secs(const timestruc_t *start)
{
time_t seconds = now.tv_sec - start->tv_sec;
long nanosecs = now.tv_usec * 1000 - start->tv_nsec;
if (nanosecs >= (NANOSEC / 2))
seconds++;
else if (nanosecs < -(NANOSEC / 2))
seconds--;
return (seconds);
}
/*
* Returns the following:
*
* 0 No error
* EINVAL Invalid number
* ERANGE Value exceeds (min, max) range
*/
static int
str2id(const char *p, pid_t *val, long min, long max)
{
char *q;
long number;
int error;
errno = 0;
number = strtol(p, &q, 10);
if (errno != 0 || q == p || *q != '\0') {
if ((error = errno) == 0) {
/*
* strtol() can fail without setting errno, or it can
* set it to EINVAL or ERANGE. In the case errno is
* still zero, return EINVAL.
*/
error = EINVAL;
}
} else if (number < min || number > max) {
error = ERANGE;
} else {
error = 0;
}
*val = number;
return (error);
}
/*
* Returns the following:
*
* 0 No error
* EINVAL Invalid number
* ERANGE Value exceeds (min, max) range
*/
static int
str2uid(const char *p, uid_t *val, unsigned long min, unsigned long max)
{
char *q;
unsigned long number;
int error;
errno = 0;
number = strtoul(p, &q, 10);
if (errno != 0 || q == p || *q != '\0') {
if ((error = errno) == 0) {
/*
* strtoul() can fail without setting errno, or it can
* set it to EINVAL or ERANGE. In the case errno is
* still zero, return EINVAL.
*/
error = EINVAL;
}
} else if (number < min || number > max) {
error = ERANGE;
} else {
error = 0;
}
*val = number;
return (error);
}
static int
pidcmp(const void *p1, const void *p2)
{
pid_t i = *((pid_t *)p1);
pid_t j = *((pid_t *)p2);
return (i - j);
}
|