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
|
/* $NetBSD: procfs_vnops.c,v 1.206.4.2 2022/06/17 15:25:21 martin Exp $ */
/*-
* Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1993, 1995
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
*/
/*
* Copyright (c) 1993 Jan-Simon Pendry
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
*/
/*
* procfs vnode interface
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.206.4.2 2022/06/17 15:25:21 martin Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/dirent.h>
#include <sys/resourcevar.h>
#include <sys/stat.h>
#include <sys/ptrace.h>
#include <sys/kauth.h>
#include <sys/exec.h>
#include <uvm/uvm_extern.h> /* for PAGE_SIZE */
#include <machine/reg.h>
#include <miscfs/genfs/genfs.h>
#include <miscfs/procfs/procfs.h>
/*
* Vnode Operations.
*
*/
static int procfs_validfile_linux(struct lwp *, struct mount *);
static int procfs_root_readdir_callback(struct proc *, void *);
static void procfs_dir(pfstype, struct lwp *, struct proc *, char **, char *,
size_t);
/*
* This is a list of the valid names in the
* process-specific sub-directories. It is
* used in procfs_lookup and procfs_readdir
*/
static const struct proc_target {
u_char pt_type;
u_char pt_namlen;
const char *pt_name;
pfstype pt_pfstype;
int (*pt_valid)(struct lwp *, struct mount *);
} proc_targets[] = {
#define N(s) sizeof(s)-1, s
/* name type validp */
{ DT_DIR, N("."), PFSproc, NULL },
{ DT_DIR, N(".."), PFSroot, NULL },
{ DT_DIR, N("fd"), PFSfd, NULL },
{ DT_DIR, N("task"), PFStask, procfs_validfile_linux },
{ DT_LNK, N("cwd"), PFScwd, NULL },
{ DT_LNK, N("emul"), PFSemul, NULL },
{ DT_LNK, N("root"), PFSchroot, NULL },
{ DT_REG, N("auxv"), PFSauxv, procfs_validauxv },
{ DT_REG, N("cmdline"), PFScmdline, NULL },
{ DT_REG, N("environ"), PFSenviron, NULL },
{ DT_REG, N("exe"), PFSexe, procfs_validfile },
{ DT_REG, N("file"), PFSfile, procfs_validfile },
{ DT_REG, N("fpregs"), PFSfpregs, procfs_validfpregs },
{ DT_REG, N("limit"), PFSlimit, NULL },
{ DT_REG, N("map"), PFSmap, procfs_validmap },
{ DT_REG, N("maps"), PFSmaps, procfs_validmap },
{ DT_REG, N("mem"), PFSmem, NULL },
{ DT_REG, N("note"), PFSnote, NULL },
{ DT_REG, N("notepg"), PFSnotepg, NULL },
{ DT_REG, N("regs"), PFSregs, procfs_validregs },
{ DT_REG, N("stat"), PFSstat, procfs_validfile_linux },
{ DT_REG, N("statm"), PFSstatm, procfs_validfile_linux },
{ DT_REG, N("status"), PFSstatus, NULL },
#ifdef __HAVE_PROCFS_MACHDEP
PROCFS_MACHDEP_NODETYPE_DEFNS
#endif
#undef N
};
static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
/*
* List of files in the root directory. Note: the validate function will
* be called with p == NULL for these ones.
*/
static const struct proc_target proc_root_targets[] = {
#define N(s) sizeof(s)-1, s
/* name type validp */
{ DT_REG, N("meminfo"), PFSmeminfo, procfs_validfile_linux },
{ DT_REG, N("cpuinfo"), PFScpuinfo, procfs_validfile_linux },
{ DT_REG, N("uptime"), PFSuptime, procfs_validfile_linux },
{ DT_REG, N("mounts"), PFSmounts, procfs_validfile_linux },
{ DT_REG, N("devices"), PFSdevices, procfs_validfile_linux },
{ DT_REG, N("stat"), PFScpustat, procfs_validfile_linux },
{ DT_REG, N("loadavg"), PFSloadavg, procfs_validfile_linux },
{ DT_REG, N("version"), PFSversion, procfs_validfile_linux },
#undef N
};
static const int nproc_root_targets =
sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
int procfs_lookup(void *);
#define procfs_create genfs_eopnotsupp
#define procfs_mknod genfs_eopnotsupp
int procfs_open(void *);
int procfs_close(void *);
int procfs_access(void *);
int procfs_getattr(void *);
int procfs_setattr(void *);
#define procfs_read procfs_rw
#define procfs_write procfs_rw
#define procfs_fcntl genfs_fcntl
#define procfs_ioctl genfs_enoioctl
#define procfs_poll genfs_poll
#define procfs_kqfilter genfs_kqfilter
#define procfs_revoke genfs_revoke
#define procfs_fsync genfs_nullop
#define procfs_seek genfs_nullop
#define procfs_remove genfs_eopnotsupp
int procfs_link(void *);
#define procfs_rename genfs_eopnotsupp
#define procfs_mkdir genfs_eopnotsupp
#define procfs_rmdir genfs_eopnotsupp
int procfs_symlink(void *);
int procfs_readdir(void *);
int procfs_readlink(void *);
#define procfs_abortop genfs_abortop
int procfs_inactive(void *);
int procfs_reclaim(void *);
#define procfs_lock genfs_lock
#define procfs_unlock genfs_unlock
#define procfs_bmap genfs_badop
#define procfs_strategy genfs_badop
int procfs_print(void *);
int procfs_pathconf(void *);
#define procfs_islocked genfs_islocked
#define procfs_advlock genfs_einval
#define procfs_bwrite genfs_eopnotsupp
int procfs_getpages(void *);
#define procfs_putpages genfs_null_putpages
static int atoi(const char *, size_t);
/*
* procfs vnode operations.
*/
int (**procfs_vnodeop_p)(void *);
const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
{ &vop_default_desc, vn_default_error },
{ &vop_lookup_desc, procfs_lookup }, /* lookup */
{ &vop_create_desc, procfs_create }, /* create */
{ &vop_mknod_desc, procfs_mknod }, /* mknod */
{ &vop_open_desc, procfs_open }, /* open */
{ &vop_close_desc, procfs_close }, /* close */
{ &vop_access_desc, procfs_access }, /* access */
{ &vop_getattr_desc, procfs_getattr }, /* getattr */
{ &vop_setattr_desc, procfs_setattr }, /* setattr */
{ &vop_read_desc, procfs_read }, /* read */
{ &vop_write_desc, procfs_write }, /* write */
{ &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
{ &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
{ &vop_fcntl_desc, procfs_fcntl }, /* fcntl */
{ &vop_ioctl_desc, procfs_ioctl }, /* ioctl */
{ &vop_poll_desc, procfs_poll }, /* poll */
{ &vop_kqfilter_desc, procfs_kqfilter }, /* kqfilter */
{ &vop_revoke_desc, procfs_revoke }, /* revoke */
{ &vop_fsync_desc, procfs_fsync }, /* fsync */
{ &vop_seek_desc, procfs_seek }, /* seek */
{ &vop_remove_desc, procfs_remove }, /* remove */
{ &vop_link_desc, procfs_link }, /* link */
{ &vop_rename_desc, procfs_rename }, /* rename */
{ &vop_mkdir_desc, procfs_mkdir }, /* mkdir */
{ &vop_rmdir_desc, procfs_rmdir }, /* rmdir */
{ &vop_symlink_desc, procfs_symlink }, /* symlink */
{ &vop_readdir_desc, procfs_readdir }, /* readdir */
{ &vop_readlink_desc, procfs_readlink }, /* readlink */
{ &vop_abortop_desc, procfs_abortop }, /* abortop */
{ &vop_inactive_desc, procfs_inactive }, /* inactive */
{ &vop_reclaim_desc, procfs_reclaim }, /* reclaim */
{ &vop_lock_desc, procfs_lock }, /* lock */
{ &vop_unlock_desc, procfs_unlock }, /* unlock */
{ &vop_bmap_desc, procfs_bmap }, /* bmap */
{ &vop_strategy_desc, procfs_strategy }, /* strategy */
{ &vop_print_desc, procfs_print }, /* print */
{ &vop_islocked_desc, procfs_islocked }, /* islocked */
{ &vop_pathconf_desc, procfs_pathconf }, /* pathconf */
{ &vop_advlock_desc, procfs_advlock }, /* advlock */
{ &vop_getpages_desc, procfs_getpages }, /* getpages */
{ &vop_putpages_desc, procfs_putpages }, /* putpages */
{ NULL, NULL }
};
const struct vnodeopv_desc procfs_vnodeop_opv_desc =
{ &procfs_vnodeop_p, procfs_vnodeop_entries };
/*
* set things up for doing i/o on
* the pfsnode (vp). (vp) is locked
* on entry, and should be left locked
* on exit.
*
* for procfs we don't need to do anything
* in particular for i/o. all that is done
* is to support exclusive open on process
* memory images.
*/
int
procfs_open(void *v)
{
struct vop_open_args /* {
struct vnode *a_vp;
int a_mode;
kauth_cred_t a_cred;
} */ *ap = v;
struct pfsnode *pfs = VTOPFS(ap->a_vp);
struct lwp *l1;
struct proc *p2;
int error;
if ((error = procfs_proc_lock(pfs->pfs_pid, &p2, ENOENT)) != 0)
return error;
l1 = curlwp; /* tracer */
#define M2K(m) (((m) & FREAD) && ((m) & FWRITE) ? \
KAUTH_REQ_PROCESS_PROCFS_RW : \
(m) & FWRITE ? KAUTH_REQ_PROCESS_PROCFS_WRITE : \
KAUTH_REQ_PROCESS_PROCFS_READ)
mutex_enter(p2->p_lock);
error = kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_PROCFS,
p2, pfs, KAUTH_ARG(M2K(ap->a_mode)), NULL);
mutex_exit(p2->p_lock);
if (error) {
procfs_proc_unlock(p2);
return (error);
}
#undef M2K
switch (pfs->pfs_type) {
case PFSmem:
if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))) {
error = EBUSY;
break;
}
if (!proc_isunder(p2, l1)) {
error = EPERM;
break;
}
if (ap->a_mode & FWRITE)
pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
break;
case PFSregs:
case PFSfpregs:
if (!proc_isunder(p2, l1)) {
error = EPERM;
break;
}
break;
default:
break;
}
procfs_proc_unlock(p2);
return (error);
}
/*
* close the pfsnode (vp) after doing i/o.
* (vp) is not locked on entry or exit.
*
* nothing to do for procfs other than undo
* any exclusive open flag (see _open above).
*/
int
procfs_close(void *v)
{
struct vop_close_args /* {
struct vnode *a_vp;
int a_fflag;
kauth_cred_t a_cred;
} */ *ap = v;
struct pfsnode *pfs = VTOPFS(ap->a_vp);
switch (pfs->pfs_type) {
case PFSmem:
if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
pfs->pfs_flags &= ~(FWRITE|O_EXCL);
break;
default:
break;
}
return (0);
}
/*
* _inactive is called when the pfsnode
* is vrele'd and the reference count goes
* to zero. (vp) will be on the vnode free
* list, so to get it back vget() must be
* used.
*
* (vp) is locked on entry, but must be unlocked on exit.
*/
int
procfs_inactive(void *v)
{
struct vop_inactive_v2_args /* {
struct vnode *a_vp;
bool *a_recycle;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct pfsnode *pfs = VTOPFS(vp);
mutex_enter(proc_lock);
*ap->a_recycle = (proc_find(pfs->pfs_pid) == NULL);
mutex_exit(proc_lock);
return (0);
}
/*
* _reclaim is called when getnewvnode()
* wants to make use of an entry on the vnode
* free list. at this time the filesystem needs
* to free any private data and remove the node
* from any private lists.
*/
int
procfs_reclaim(void *v)
{
struct vop_reclaim_v2_args /* {
struct vnode *a_vp;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct pfsnode *pfs = VTOPFS(vp);
VOP_UNLOCK(vp);
/*
* To interlock with procfs_revoke_vnodes().
*/
mutex_enter(vp->v_interlock);
vp->v_data = NULL;
mutex_exit(vp->v_interlock);
kmem_free(pfs, sizeof(*pfs));
return 0;
}
/*
* Return POSIX pathconf information applicable to special devices.
*/
int
procfs_pathconf(void *v)
{
struct vop_pathconf_args /* {
struct vnode *a_vp;
int a_name;
register_t *a_retval;
} */ *ap = v;
switch (ap->a_name) {
case _PC_LINK_MAX:
*ap->a_retval = LINK_MAX;
return (0);
case _PC_MAX_CANON:
*ap->a_retval = MAX_CANON;
return (0);
case _PC_MAX_INPUT:
*ap->a_retval = MAX_INPUT;
return (0);
case _PC_PIPE_BUF:
*ap->a_retval = PIPE_BUF;
return (0);
case _PC_CHOWN_RESTRICTED:
*ap->a_retval = 1;
return (0);
case _PC_VDISABLE:
*ap->a_retval = _POSIX_VDISABLE;
return (0);
case _PC_SYNC_IO:
*ap->a_retval = 1;
return (0);
default:
return (EINVAL);
}
/* NOTREACHED */
}
/*
* _print is used for debugging.
* just print a readable description
* of (vp).
*/
int
procfs_print(void *v)
{
struct vop_print_args /* {
struct vnode *a_vp;
} */ *ap = v;
struct pfsnode *pfs = VTOPFS(ap->a_vp);
printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
return 0;
}
int
procfs_link(void *v)
{
struct vop_link_v2_args /* {
struct vnode *a_dvp;
struct vnode *a_vp;
struct componentname *a_cnp;
} */ *ap = v;
VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
return (EROFS);
}
int
procfs_symlink(void *v)
{
struct vop_symlink_v3_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
struct vattr *a_vap;
char *a_target;
} */ *ap = v;
VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
return (EROFS);
}
/*
* Works out the path to the target process's current
* working directory or chroot. If the caller is in a chroot and
* can't "reach" the target's cwd or root (or some other error
* occurs), a "/" is returned for the path.
*/
static void
procfs_dir(pfstype t, struct lwp *caller, struct proc *target, char **bpp,
char *path, size_t len)
{
struct cwdinfo *cwdi;
struct vnode *vp, *rvp;
char *bp;
/*
* Lock target cwdi and take a reference to the vnode
* we are interested in to prevent it from disappearing
* before getcwd_common() below.
*/
rw_enter(&target->p_cwdi->cwdi_lock, RW_READER);
switch (t) {
case PFScwd:
vp = target->p_cwdi->cwdi_cdir;
break;
case PFSchroot:
vp = target->p_cwdi->cwdi_rdir;
break;
default:
rw_exit(&target->p_cwdi->cwdi_lock);
return;
}
if (vp != NULL)
vref(vp);
rw_exit(&target->p_cwdi->cwdi_lock);
cwdi = caller->l_proc->p_cwdi;
rw_enter(&cwdi->cwdi_lock, RW_READER);
rvp = cwdi->cwdi_rdir;
bp = bpp ? *bpp : NULL;
/*
* XXX: this horrible kludge avoids locking panics when
* attempting to lookup links that point to within procfs
*/
if (vp != NULL && vp->v_tag == VT_PROCFS) {
if (bpp) {
*--bp = '/';
*bpp = bp;
}
vrele(vp);
rw_exit(&cwdi->cwdi_lock);
return;
}
if (rvp == NULL)
rvp = rootvnode;
if (vp == NULL || getcwd_common(vp, rvp, bp ? &bp : NULL, path,
len / 2, 0, caller) != 0) {
if (bpp) {
bp = *bpp;
*--bp = '/';
}
}
if (bpp)
*bpp = bp;
if (vp != NULL)
vrele(vp);
rw_exit(&cwdi->cwdi_lock);
}
/*
* Invent attributes for pfsnode (vp) and store
* them in (vap).
* Directories lengths are returned as zero since
* any real length would require the genuine size
* to be computed, and nothing cares anyway.
*
* this is relatively minimal for procfs.
*/
int
procfs_getattr(void *v)
{
struct vop_getattr_args /* {
struct vnode *a_vp;
struct vattr *a_vap;
kauth_cred_t a_cred;
} */ *ap = v;
struct pfsnode *pfs = VTOPFS(ap->a_vp);
struct vattr *vap = ap->a_vap;
struct proc *procp;
char *path, *bp, bf[16];
int error;
/* first check the process still exists */
switch (pfs->pfs_type) {
case PFSroot:
case PFScurproc:
case PFSself:
procp = NULL;
break;
default:
error = procfs_proc_lock(pfs->pfs_pid, &procp, ENOENT);
if (error != 0)
return (error);
break;
}
switch (pfs->pfs_type) {
case PFStask:
if (pfs->pfs_fd == -1) {
path = NULL;
break;
}
/*FALLTHROUGH*/
case PFScwd:
case PFSchroot:
path = malloc(MAXPATHLEN + 4, M_TEMP, M_WAITOK);
if (path == NULL && procp != NULL) {
procfs_proc_unlock(procp);
return (ENOMEM);
}
break;
default:
path = NULL;
break;
}
if (procp != NULL) {
mutex_enter(procp->p_lock);
error = kauth_authorize_process(kauth_cred_get(),
KAUTH_PROCESS_CANSEE, procp,
KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
mutex_exit(procp->p_lock);
if (error != 0) {
procfs_proc_unlock(procp);
if (path != NULL)
free(path, M_TEMP);
return (ENOENT);
}
}
error = 0;
/* start by zeroing out the attributes */
vattr_null(vap);
/* next do all the common fields */
vap->va_type = ap->a_vp->v_type;
vap->va_mode = pfs->pfs_mode;
vap->va_fileid = pfs->pfs_fileno;
vap->va_flags = 0;
vap->va_blocksize = PAGE_SIZE;
/*
* Make all times be current TOD.
*
* It would be possible to get the process start
* time from the p_stats structure, but there's
* no "file creation" time stamp anyway, and the
* p_stats structure is not addressable if u. gets
* swapped out for that process.
*/
getnanotime(&vap->va_ctime);
vap->va_atime = vap->va_mtime = vap->va_ctime;
if (procp)
TIMEVAL_TO_TIMESPEC(&procp->p_stats->p_start,
&vap->va_birthtime);
else
getnanotime(&vap->va_birthtime);
switch (pfs->pfs_type) {
case PFSmem:
case PFSregs:
case PFSfpregs:
#if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
PROCFS_MACHDEP_PROTECT_CASES
#endif
/*
* If the process has exercised some setuid or setgid
* privilege, then rip away read/write permission so
* that only root can gain access.
*/
if (procp->p_flag & PK_SUGID)
vap->va_mode &= ~(S_IRUSR|S_IWUSR);
/* FALLTHROUGH */
case PFSstatus:
case PFSstat:
case PFSnote:
case PFSnotepg:
case PFScmdline:
case PFSenviron:
case PFSemul:
case PFSstatm:
case PFSmap:
case PFSmaps:
case PFSlimit:
case PFSauxv:
vap->va_nlink = 1;
vap->va_uid = kauth_cred_geteuid(procp->p_cred);
vap->va_gid = kauth_cred_getegid(procp->p_cred);
break;
case PFScwd:
case PFSchroot:
case PFSmeminfo:
case PFSdevices:
case PFScpuinfo:
case PFSuptime:
case PFSmounts:
case PFScpustat:
case PFSloadavg:
case PFSversion:
case PFSexe:
case PFSself:
case PFScurproc:
case PFSroot:
vap->va_nlink = 1;
vap->va_uid = vap->va_gid = 0;
break;
case PFSproc:
case PFStask:
case PFSfile:
case PFSfd:
break;
default:
panic("%s: %d/1", __func__, pfs->pfs_type);
}
/*
* now do the object specific fields
*
* The size could be set from struct reg, but it's hardly
* worth the trouble, and it puts some (potentially) machine
* dependent data into this machine-independent code. If it
* becomes important then this function should break out into
* a per-file stat function in the corresponding .c file.
*/
switch (pfs->pfs_type) {
case PFSroot:
vap->va_bytes = vap->va_size = DEV_BSIZE;
break;
case PFSself:
case PFScurproc:
vap->va_bytes = vap->va_size =
snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
break;
case PFStask:
if (pfs->pfs_fd != -1) {
vap->va_nlink = 1;
vap->va_uid = 0;
vap->va_gid = 0;
vap->va_bytes = vap->va_size =
snprintf(bf, sizeof(bf), "..");
break;
}
/*FALLTHROUGH*/
case PFSfd:
if (pfs->pfs_fd != -1) {
file_t *fp;
fp = fd_getfile2(procp, pfs->pfs_fd);
if (fp == NULL) {
error = EBADF;
break;
}
vap->va_nlink = 1;
vap->va_uid = kauth_cred_geteuid(fp->f_cred);
vap->va_gid = kauth_cred_getegid(fp->f_cred);
switch (fp->f_type) {
case DTYPE_VNODE:
vap->va_bytes = vap->va_size =
fp->f_vnode->v_size;
break;
default:
vap->va_bytes = vap->va_size = 0;
break;
}
closef(fp);
break;
}
/*FALLTHROUGH*/
case PFSproc:
vap->va_nlink = 2;
vap->va_uid = kauth_cred_geteuid(procp->p_cred);
vap->va_gid = kauth_cred_getegid(procp->p_cred);
vap->va_bytes = vap->va_size = DEV_BSIZE;
break;
case PFSfile:
error = EOPNOTSUPP;
break;
case PFSmem:
vap->va_bytes = vap->va_size =
ctob(procp->p_vmspace->vm_tsize +
procp->p_vmspace->vm_dsize +
procp->p_vmspace->vm_ssize);
break;
case PFSauxv:
vap->va_bytes = vap->va_size = procp->p_execsw->es_arglen;
break;
#if defined(PT_GETREGS) || defined(PT_SETREGS)
case PFSregs:
vap->va_bytes = vap->va_size = sizeof(struct reg);
break;
#endif
#if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
case PFSfpregs:
vap->va_bytes = vap->va_size = sizeof(struct fpreg);
break;
#endif
case PFSstatus:
case PFSstat:
case PFSnote:
case PFSnotepg:
case PFScmdline:
case PFSenviron:
case PFSmeminfo:
case PFSdevices:
case PFScpuinfo:
case PFSuptime:
case PFSmounts:
case PFScpustat:
case PFSloadavg:
case PFSstatm:
case PFSversion:
vap->va_bytes = vap->va_size = 0;
break;
case PFSlimit:
case PFSmap:
case PFSmaps:
/*
* Advise a larger blocksize for the map files, so that
* they may be read in one pass.
*/
vap->va_blocksize = 4 * PAGE_SIZE;
vap->va_bytes = vap->va_size = 0;
break;
case PFScwd:
case PFSchroot:
bp = path + MAXPATHLEN;
*--bp = '\0';
procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path,
MAXPATHLEN);
vap->va_bytes = vap->va_size = strlen(bp);
break;
case PFSexe:
vap->va_bytes = vap->va_size = strlen(procp->p_path);
break;
case PFSemul:
vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name);
break;
#ifdef __HAVE_PROCFS_MACHDEP
PROCFS_MACHDEP_NODETYPE_CASES
error = procfs_machdep_getattr(ap->a_vp, vap, procp);
break;
#endif
default:
panic("%s: %d/2", __func__, pfs->pfs_type);
}
if (procp != NULL)
procfs_proc_unlock(procp);
if (path != NULL)
free(path, M_TEMP);
return (error);
}
/*ARGSUSED*/
int
procfs_setattr(void *v)
{
/*
* just fake out attribute setting
* it's not good to generate an error
* return, otherwise things like creat()
* will fail when they try to set the
* file length to 0. worse, this means
* that echo $note > /proc/$pid/note will fail.
*/
return (0);
}
/*
* implement access checking.
*
* actually, the check for super-user is slightly
* broken since it will allow read access to write-only
* objects. this doesn't cause any particular trouble
* but does mean that the i/o entry points need to check
* that the operation really does make sense.
*/
int
procfs_access(void *v)
{
struct vop_access_args /* {
struct vnode *a_vp;
int a_mode;
kauth_cred_t a_cred;
} */ *ap = v;
struct vattr va;
int error;
if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0)
return (error);
return kauth_authorize_vnode(ap->a_cred,
KAUTH_ACCESS_ACTION(ap->a_mode, ap->a_vp->v_type, va.va_mode),
ap->a_vp, NULL, genfs_can_access(va.va_type, va.va_mode,
va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
}
/*
* lookup. this is incredibly complicated in the
* general case, however for most pseudo-filesystems
* very little needs to be done.
*
* Locking isn't hard here, just poorly documented.
*
* If we're looking up ".", just vref the parent & return it.
*
* If we're looking up "..", unlock the parent, and lock "..". If everything
* went ok, and we're on the last component and the caller requested the
* parent locked, try to re-lock the parent. We do this to prevent lock
* races.
*
* For anything else, get the needed node. Then unlock the parent if not
* the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
* parent in the .. case).
*
* We try to exit with the parent locked in error cases.
*/
int
procfs_lookup(void *v)
{
struct vop_lookup_v2_args /* {
struct vnode * a_dvp;
struct vnode ** a_vpp;
struct componentname * a_cnp;
} */ *ap = v;
struct componentname *cnp = ap->a_cnp;
struct vnode **vpp = ap->a_vpp;
struct vnode *dvp = ap->a_dvp;
const char *pname = cnp->cn_nameptr;
const struct proc_target *pt = NULL;
struct vnode *fvp;
pid_t pid, vnpid;
struct pfsnode *pfs;
struct proc *p = NULL;
struct lwp *plwp;
int i, error;
pfstype type;
*vpp = NULL;
if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred)) != 0)
return (error);
if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
return (EROFS);
if (cnp->cn_namelen == 1 && *pname == '.') {
*vpp = dvp;
vref(dvp);
return (0);
}
pfs = VTOPFS(dvp);
switch (pfs->pfs_type) {
case PFSroot:
/*
* Shouldn't get here with .. in the root node.
*/
if (cnp->cn_flags & ISDOTDOT)
return (EIO);
for (i = 0; i < nproc_root_targets; i++) {
pt = &proc_root_targets[i];
/*
* check for node match. proc is always NULL here,
* so call pt_valid with constant NULL lwp.
*/
if (cnp->cn_namelen == pt->pt_namlen &&
memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
(pt->pt_valid == NULL ||
(*pt->pt_valid)(NULL, dvp->v_mount)))
break;
}
if (i != nproc_root_targets) {
error = procfs_allocvp(dvp->v_mount, vpp, 0,
pt->pt_pfstype, -1);
return (error);
}
if (CNEQ(cnp, "curproc", 7)) {
pid = curproc->p_pid;
vnpid = 0;
type = PFScurproc;
} else if (CNEQ(cnp, "self", 4)) {
pid = curproc->p_pid;
vnpid = 0;
type = PFSself;
} else {
pid = (pid_t)atoi(pname, cnp->cn_namelen);
vnpid = pid;
type = PFSproc;
}
if (procfs_proc_lock(pid, &p, ESRCH) != 0)
break;
error = procfs_allocvp(dvp->v_mount, vpp, vnpid, type, -1);
procfs_proc_unlock(p);
return (error);
case PFSproc:
if (cnp->cn_flags & ISDOTDOT) {
error = procfs_allocvp(dvp->v_mount, vpp, 0, PFSroot,
-1);
return (error);
}
if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
break;
mutex_enter(p->p_lock);
LIST_FOREACH(plwp, &p->p_lwps, l_sibling) {
if (plwp->l_stat != LSZOMB)
break;
}
/* Process is exiting if no-LWPS or all LWPs are LSZOMB */
if (plwp == NULL) {
mutex_exit(p->p_lock);
procfs_proc_unlock(p);
return ESRCH;
}
lwp_addref(plwp);
mutex_exit(p->p_lock);
for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
int found;
found = cnp->cn_namelen == pt->pt_namlen &&
memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
(pt->pt_valid == NULL
|| (*pt->pt_valid)(plwp, dvp->v_mount));
if (found)
break;
}
lwp_delref(plwp);
if (i == nproc_targets) {
procfs_proc_unlock(p);
break;
}
if (pt->pt_pfstype == PFSfile) {
fvp = p->p_textvp;
/* We already checked that it exists. */
vref(fvp);
procfs_proc_unlock(p);
*vpp = fvp;
return (0);
}
error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
pt->pt_pfstype, -1);
procfs_proc_unlock(p);
return (error);
case PFSfd: {
int fd;
file_t *fp;
if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ENOENT)) != 0)
return error;
if (cnp->cn_flags & ISDOTDOT) {
error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
PFSproc, -1);
procfs_proc_unlock(p);
return (error);
}
fd = atoi(pname, cnp->cn_namelen);
fp = fd_getfile2(p, fd);
if (fp == NULL) {
procfs_proc_unlock(p);
return ENOENT;
}
fvp = fp->f_vnode;
/* Don't show directories */
if (fp->f_type == DTYPE_VNODE && fvp->v_type != VDIR) {
vref(fvp);
closef(fp);
procfs_proc_unlock(p);
*vpp = fvp;
return 0;
}
closef(fp);
error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
PFSfd, fd);
procfs_proc_unlock(p);
return error;
}
case PFStask: {
int xpid;
if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ENOENT)) != 0)
return error;
if (cnp->cn_flags & ISDOTDOT) {
error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
PFSproc, -1);
procfs_proc_unlock(p);
return (error);
}
xpid = atoi(pname, cnp->cn_namelen);
if (xpid != pfs->pfs_pid) {
procfs_proc_unlock(p);
return ENOENT;
}
error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
PFStask, 0);
procfs_proc_unlock(p);
return error;
}
default:
return (ENOTDIR);
}
return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
}
int
procfs_validfile(struct lwp *l, struct mount *mp)
{
return l != NULL && l->l_proc != NULL && l->l_proc->p_textvp != NULL;
}
static int
procfs_validfile_linux(struct lwp *l, struct mount *mp)
{
int flags;
flags = VFSTOPROC(mp)->pmnt_flags;
return (flags & PROCFSMNT_LINUXCOMPAT) &&
(l == NULL || l->l_proc == NULL || procfs_validfile(l, mp));
}
struct procfs_root_readdir_ctx {
struct uio *uiop;
off_t *cookies;
int ncookies;
off_t off;
off_t startoff;
int error;
};
static int
procfs_root_readdir_callback(struct proc *p, void *arg)
{
struct procfs_root_readdir_ctx *ctxp = arg;
struct dirent d;
struct uio *uiop;
int error;
uiop = ctxp->uiop;
if (uiop->uio_resid < UIO_MX)
return -1; /* no space */
if (ctxp->off < ctxp->startoff) {
ctxp->off++;
return 0;
}
if (kauth_authorize_process(kauth_cred_get(),
KAUTH_PROCESS_CANSEE, p,
KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL) != 0)
return 0;
memset(&d, 0, UIO_MX);
d.d_reclen = UIO_MX;
d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
d.d_namlen = snprintf(d.d_name,
UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
d.d_type = DT_DIR;
mutex_exit(proc_lock);
error = uiomove(&d, UIO_MX, uiop);
mutex_enter(proc_lock);
if (error) {
ctxp->error = error;
return -1;
}
ctxp->ncookies++;
if (ctxp->cookies)
*(ctxp->cookies)++ = ctxp->off + 1;
ctxp->off++;
return 0;
}
/*
* readdir returns directory entries from pfsnode (vp).
*
* the strategy here with procfs is to generate a single
* directory entry at a time (struct dirent) and then
* copy that out to userland using uiomove. a more efficent
* though more complex implementation, would try to minimize
* the number of calls to uiomove(). for procfs, this is
* hardly worth the added code complexity.
*
* this should just be done through read()
*/
int
procfs_readdir(void *v)
{
struct vop_readdir_args /* {
struct vnode *a_vp;
struct uio *a_uio;
kauth_cred_t a_cred;
int *a_eofflag;
off_t **a_cookies;
int *a_ncookies;
} */ *ap = v;
struct uio *uio = ap->a_uio;
struct dirent d;
struct pfsnode *pfs;
off_t i;
int error;
off_t *cookies = NULL;
int ncookies;
struct vnode *vp;
const struct proc_target *pt;
struct procfs_root_readdir_ctx ctx;
struct lwp *l;
int nfd;
vp = ap->a_vp;
pfs = VTOPFS(vp);
if (uio->uio_resid < UIO_MX)
return (EINVAL);
if (uio->uio_offset < 0)
return (EINVAL);
error = 0;
i = uio->uio_offset;
memset(&d, 0, UIO_MX);
d.d_reclen = UIO_MX;
ncookies = uio->uio_resid / UIO_MX;
switch (pfs->pfs_type) {
/*
* this is for the process-specific sub-directories.
* all that is needed to is copy out all the entries
* from the procent[] table (top of this file).
*/
case PFSproc: {
struct proc *p;
if (i >= nproc_targets)
return 0;
if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
break;
if (ap->a_ncookies) {
ncookies = uimin(ncookies, (nproc_targets - i));
cookies = malloc(ncookies * sizeof (off_t),
M_TEMP, M_WAITOK);
*ap->a_cookies = cookies;
}
for (pt = &proc_targets[i];
uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
if (pt->pt_valid) {
/* XXXSMP LWP can disappear */
mutex_enter(p->p_lock);
l = LIST_FIRST(&p->p_lwps);
KASSERT(l != NULL);
mutex_exit(p->p_lock);
if ((*pt->pt_valid)(l, vp->v_mount) == 0)
continue;
}
d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
pt->pt_pfstype, -1);
d.d_namlen = pt->pt_namlen;
memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
d.d_type = pt->pt_type;
if ((error = uiomove(&d, UIO_MX, uio)) != 0)
break;
if (cookies)
*cookies++ = i + 1;
}
procfs_proc_unlock(p);
break;
}
case PFSfd: {
struct proc *p;
file_t *fp;
int lim, nc = 0;
if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
return error;
/* XXX Should this be by file as well? */
if (kauth_authorize_process(kauth_cred_get(),
KAUTH_PROCESS_CANSEE, p,
KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES), NULL,
NULL) != 0) {
procfs_proc_unlock(p);
return ESRCH;
}
nfd = p->p_fd->fd_dt->dt_nfiles;
lim = uimin((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
if (i >= lim) {
procfs_proc_unlock(p);
return 0;
}
if (ap->a_ncookies) {
ncookies = uimin(ncookies, (nfd + 2 - i));
cookies = malloc(ncookies * sizeof (off_t),
M_TEMP, M_WAITOK);
*ap->a_cookies = cookies;
}
for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
pt = &proc_targets[i];
d.d_namlen = pt->pt_namlen;
d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
pt->pt_pfstype, -1);
(void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
d.d_type = pt->pt_type;
if ((error = uiomove(&d, UIO_MX, uio)) != 0)
break;
if (cookies)
*cookies++ = i + 1;
nc++;
}
if (error) {
ncookies = nc;
break;
}
for (; uio->uio_resid >= UIO_MX && i < nfd; i++) {
/* check the descriptor exists */
if ((fp = fd_getfile2(p, i - 2)) == NULL)
continue;
closef(fp);
d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
"%lld", (long long)(i - 2));
d.d_type = VREG;
if ((error = uiomove(&d, UIO_MX, uio)) != 0)
break;
if (cookies)
*cookies++ = i + 1;
nc++;
}
ncookies = nc;
procfs_proc_unlock(p);
break;
}
case PFStask: {
struct proc *p;
int nc = 0;
if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
return error;
nfd = 3; /* ., .., pid */
if (ap->a_ncookies) {
ncookies = uimin(ncookies, (nfd + 2 - i));
cookies = malloc(ncookies * sizeof (off_t),
M_TEMP, M_WAITOK);
*ap->a_cookies = cookies;
}
for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
pt = &proc_targets[i];
d.d_namlen = pt->pt_namlen;
d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
pt->pt_pfstype, -1);
(void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
d.d_type = pt->pt_type;
if ((error = uiomove(&d, UIO_MX, uio)) != 0)
break;
if (cookies)
*cookies++ = i + 1;
nc++;
}
if (error) {
ncookies = nc;
break;
}
for (; uio->uio_resid >= UIO_MX && i < nfd; i++) {
/* check the descriptor exists */
d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFStask,
i - 2);
d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
"%ld", (long)pfs->pfs_pid);
d.d_type = DT_LNK;
if ((error = uiomove(&d, UIO_MX, uio)) != 0)
break;
if (cookies)
*cookies++ = i + 1;
nc++;
}
ncookies = nc;
procfs_proc_unlock(p);
break;
}
/*
* this is for the root of the procfs filesystem
* what is needed are special entries for "curproc"
* and "self" followed by an entry for each process
* on allproc.
*/
case PFSroot: {
int nc = 0;
if (ap->a_ncookies) {
/*
* XXX Potentially allocating too much space here,
* but I'm lazy. This loop needs some work.
*/
cookies = malloc(ncookies * sizeof (off_t),
M_TEMP, M_WAITOK);
*ap->a_cookies = cookies;
}
error = 0;
/* 0 ... 3 are static entries. */
for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) {
switch (i) {
case 0: /* `.' */
case 1: /* `..' */
d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
d.d_namlen = i + 1;
memcpy(d.d_name, "..", d.d_namlen);
d.d_name[i + 1] = '\0';
d.d_type = DT_DIR;
break;
case 2:
d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
d.d_namlen = sizeof("curproc") - 1;
memcpy(d.d_name, "curproc", sizeof("curproc"));
d.d_type = DT_LNK;
break;
case 3:
d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
d.d_namlen = sizeof("self") - 1;
memcpy(d.d_name, "self", sizeof("self"));
d.d_type = DT_LNK;
break;
}
if ((error = uiomove(&d, UIO_MX, uio)) != 0)
break;
nc++;
if (cookies)
*cookies++ = i + 1;
}
/* 4 ... are process entries. */
ctx.uiop = uio;
ctx.error = 0;
ctx.off = 4;
ctx.startoff = i;
ctx.cookies = cookies;
ctx.ncookies = nc;
proclist_foreach_call(&allproc,
procfs_root_readdir_callback, &ctx);
cookies = ctx.cookies;
nc = ctx.ncookies;
error = ctx.error;
if (error)
break;
/* misc entries. */
if (i < ctx.off)
i = ctx.off;
if (i >= ctx.off + nproc_root_targets)
break;
for (pt = &proc_root_targets[i - ctx.off];
uio->uio_resid >= UIO_MX &&
pt < &proc_root_targets[nproc_root_targets];
pt++, i++) {
if (pt->pt_valid &&
(*pt->pt_valid)(NULL, vp->v_mount) == 0)
continue;
d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
d.d_namlen = pt->pt_namlen;
memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
d.d_type = pt->pt_type;
if ((error = uiomove(&d, UIO_MX, uio)) != 0)
break;
nc++;
if (cookies)
*cookies++ = i + 1;
}
ncookies = nc;
break;
}
default:
error = ENOTDIR;
break;
}
if (ap->a_ncookies) {
if (error) {
if (cookies)
free(*ap->a_cookies, M_TEMP);
*ap->a_ncookies = 0;
*ap->a_cookies = NULL;
} else
*ap->a_ncookies = ncookies;
}
uio->uio_offset = i;
return (error);
}
/*
* readlink reads the link of `curproc' and others
*/
int
procfs_readlink(void *v)
{
struct vop_readlink_args *ap = v;
char bf[16]; /* should be enough */
char *bp = bf;
char *path = NULL;
int len = 0;
int error = 0;
struct pfsnode *pfs = VTOPFS(ap->a_vp);
struct proc *pown = NULL;
if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
len = snprintf(bf, sizeof(bf), "%s", "curproc");
else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFStask, 0))
len = snprintf(bf, sizeof(bf), "..");
else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSexe, -1)) {
if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
return error;
bp = pown->p_path;
len = strlen(bp);
} else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) ||
pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1)) {
if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
return error;
path = malloc(MAXPATHLEN + 4, M_TEMP, M_WAITOK);
if (path == NULL) {
procfs_proc_unlock(pown);
return (ENOMEM);
}
bp = path + MAXPATHLEN;
*--bp = '\0';
procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown,
&bp, path, MAXPATHLEN);
len = strlen(bp);
} else {
file_t *fp;
struct vnode *vxp, *vp;
if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
return error;
fp = fd_getfile2(pown, pfs->pfs_fd);
if (fp == NULL) {
procfs_proc_unlock(pown);
return EBADF;
}
switch (fp->f_type) {
case DTYPE_VNODE:
vxp = fp->f_vnode;
if (vxp->v_type != VDIR) {
error = EINVAL;
break;
}
if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
== NULL) {
error = ENOMEM;
break;
}
bp = path + MAXPATHLEN;
*--bp = '\0';
/*
* XXX: kludge to avoid locking against ourselves
* in getcwd()
*/
if (vxp->v_tag == VT_PROCFS) {
*--bp = '/';
} else {
rw_enter(&curproc->p_cwdi->cwdi_lock,
RW_READER);
vp = curproc->p_cwdi->cwdi_rdir;
if (vp == NULL)
vp = rootvnode;
error = getcwd_common(vxp, vp, &bp, path,
MAXPATHLEN / 2, 0, curlwp);
rw_exit(&curproc->p_cwdi->cwdi_lock);
}
if (error)
break;
len = strlen(bp);
break;
case DTYPE_MISC:
len = snprintf(bf, sizeof(bf), "%s", "[misc]");
break;
case DTYPE_KQUEUE:
len = snprintf(bf, sizeof(bf), "%s", "[kqueue]");
break;
case DTYPE_SEM:
len = snprintf(bf, sizeof(bf), "%s", "[ksem]");
break;
default:
error = EINVAL;
break;
}
closef(fp);
}
if (error == 0)
error = uiomove(bp, len, ap->a_uio);
if (pown)
procfs_proc_unlock(pown);
if (path)
free(path, M_TEMP);
return error;
}
int
procfs_getpages(void *v)
{
struct vop_getpages_args /* {
struct vnode *a_vp;
voff_t a_offset;
struct vm_page **a_m;
int *a_count;
int a_centeridx;
vm_prot_t a_access_type;
int a_advice;
int a_flags;
} */ *ap = v;
if ((ap->a_flags & PGO_LOCKED) == 0)
mutex_exit(ap->a_vp->v_interlock);
return (EFAULT);
}
/*
* convert decimal ascii to int
*/
static int
atoi(const char *b, size_t len)
{
int p = 0;
while (len--) {
char c = *b++;
if (c < '0' || c > '9')
return -1;
p = 10 * p + (c - '0');
}
return p;
}
|