aboutsummaryrefslogtreecommitdiffstats
path: root/package/hotplug2/patches/100-svn_update.patch
blob: 283e6137195e0aa6c572df5f03ee2aa97b14bf44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
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
diff -urN -x .svn hotplug2-0.9/AUTHORS hotplug2/AUTHORS
--- hotplug2-0.9/AUTHORS	2006-10-08 18:13:50.000000000 +0200
+++ hotplug2/AUTHORS	2007-06-30 12:59:20.459674000 +0200
@@ -1,7 +1,11 @@
  Authors:
 ----------
 iSteve <isteve@bofh.cz>
-Tomas Janousek <tomi@nomi.cz>
+
+ Contributions:
+----------------
+nbd (rules override patch, various fixes, suggestions, testing etc.)
+Tomas Janousek <tomi@nomi.cz> (Makefiles, SVN hosting)
 
  Thanks to:
 ------------
@@ -10,5 +14,9 @@
 Randy Dunlap (help with isapnpmap)
 Igor2 (provided testing machines)
 yanek (provided testing machines)
+Zdenek Styblik (provided testing OpenWRT device)
+OpenWRT team (for trusting this project)
+mtu (debugging, testing, ideas)
+mnemoc (trivial sanity changes on makefiles, linux24 compat patches)
 
-...anyone taking more than a short peek at the software.
\ No newline at end of file
+...anyone taking more than a short peek at the software.
diff -urN -x .svn hotplug2-0.9/Changelog hotplug2/Changelog
--- hotplug2-0.9/Changelog	2006-10-08 15:32:31.000000000 +0200
+++ hotplug2/Changelog	2007-07-09 01:17:14.865503750 +0200
@@ -1,3 +1,13 @@
+0.9 - 1.0:
+* Add --set-rules-file.
+* Allow any ACTION.
+* Add 'printdebug' rule.
+* Fix chmod, chown, chgrp.
+* Use octal for chmod and makedev.
+* Add 'nothrottle' flag, allowing overriding max-children from a rule
+* Various bugfixes
+* Code comments
+
 0.8 - 0.9:
 * Use signals to handle children.
 * Separate info and debugging output.
@@ -44,4 +54,4 @@
 * Add more actions.
 * Significant cleanup of rules handling.
 * Better error reporting.
- 
\ No newline at end of file
+ 
diff -urN -x .svn hotplug2-0.9/common.mak hotplug2/common.mak
--- hotplug2-0.9/common.mak	2006-09-26 01:03:08.000000000 +0200
+++ hotplug2/common.mak	2007-07-09 01:17:14.869504000 +0200
@@ -1,6 +1,6 @@
 # vim:set sw=8 nosta:
 
-CFLAGS=-Os -DHAVE_RULES -Wall -g
+CFLAGS=-Os -DHAVE_RULES -Wall -g -Wextra
 LDFLAGS=-g
 
 INSTALL=install -c -m 644
@@ -10,7 +10,7 @@
 .PHONY: all clean dep install install-recursive clean-recursive \
 	dep-recursive all-recursive
 
-MAKEDEP=-gcc $(CFLAGS) -MM $(wildcard *.c *.cc) > .depend
+MAKEDEP=-$(CC) $(CFLAGS) -MM $(wildcard *.c *.cc) > .depend
 dep: dep-recursive
 	$(MAKEDEP)
 .depend:
diff -urN -x .svn hotplug2-0.9/docs/hotplug2.8 hotplug2/docs/hotplug2.8
--- hotplug2-0.9/docs/hotplug2.8	2006-09-26 09:23:36.000000000 +0200
+++ hotplug2/docs/hotplug2.8	2007-06-28 14:50:59.874955160 +0200
@@ -22,6 +22,8 @@
 .TP 
 \fB\-\-dumb\fR, \fB\-\-no\-dumb\fR
 Run or do not run hotplug2 in dumb mode. Dumb mode means that rules are being ignored, the only action taken is mload modules to all devices whose uevent exports MODALIAS. Only available if compiled with HAVE_RULES.
+\fB\-\-override\fR, \fB\-\-no\-override\fR
+Allows hotplug2 behavior overriding for different rules, using various flags. See hotplug2 rules documentation for details. The default is not to allow overriding, the flags are therefore ignored.
 .TP 
 \fB\-\-max\-children <value>\fR
 Set the value of maximum children hotplug2 may have running simultaneously. Default is 20.
@@ -31,6 +33,8 @@
 .TP 
 \fB\-\-set\-modprobe\-cmd <cmd>\fR
 Sets the application used to perform modprobe. It only gets used in dumb mode. Default is to autodetect: if '/bin/modprobe' is from module\-init\-tools, use '/sbin/modprobe', otherwise use '/sbin/hotplug2\-modwrap'.
+\fB\-\-set\-rules\-file <file>\fR
+Sets the path to the file containing hotplug2 rules.
 .SH "SIGNALS"
 .TP 
 \fBSIGUSR1\fR
diff -urN -x .svn hotplug2-0.9/docs/hotplug2.rules.doc hotplug2/docs/hotplug2.rules.doc
--- hotplug2-0.9/docs/hotplug2.rules.doc	2006-09-26 10:19:46.000000000 +0200
+++ hotplug2/docs/hotplug2.rules.doc	2007-06-28 14:50:59.872955464 +0200
@@ -11,12 +11,12 @@
 	[... [...]]
 }
 
-Comments are allowed, they are prefixed with '#', treating the whole rest of the
-line as comment. Please note that comments in place of action parameters are not
-supported.
+Comments are allowed, they are prefixed with '#', treating the whole rest of
+the line as comment. Please note that comments in place of action parameters
+are not supported.
 
-The <key> is one of the environmental variables that have been obtained by the
-uevent netlink. 
+The <key> is one of the environmental variables that have been obtained by
+the uevent netlink. 
 
  COMMON KEYS
  -----------
@@ -66,9 +66,9 @@
   -------
 
  * run <...>
-	Execute an application using system();, takes one parameter. Note that
-	the application has set all environmental variables read by uevent
-	netlink.
+	Execute an application using system();, takes one parameter. Note
+	that the application has set all environmental variables read by 
+	uevent netlink.
 
  * break
 	Break the processing of the current block of actions.
@@ -86,23 +86,22 @@
 
  * exec <application [parameter [parameter [...]]]> ;
 	Execute an application. Takes variable number of parameters, but the
-	last parameter must be terminated with semicolon. Again, all variables
-	are set as environmental.
+	last parameter must be terminated with semicolon. Again, all
+	variables are set as environmental.
 	
-	If you need to escape the ';', use '\\;'. Only applies for actions with
-	variable number of parameters.
+	If you need to escape the ';', use '\\;'. Only applies for actions
+	with variable number of parameters.
 	
  * makedev <path> <mode>
-	Create a device with given mode. The mode is not in octal unless it
-	starts with '0', eg. "0644" != "644".
+	Create a device with given mode. Mode is interpreted as octal.
 	
 	Major, minor and devpath must be set for makedev to be able to work.
 	Tests for these variables are also performed internally in makedev
 	function.
 
  * symlink <target> <linkname>
-	Create a symbolic link (symlink, also known as soft link) pointing at
-	target with name linkname.
+	Create a symbolic link (symlink, also known as soft link) pointing
+	at target with name linkname.
 	
  * chown <path> <owner name>
 	Change owner of path to owner name.
@@ -111,12 +110,32 @@
 	Change group of path to group name.
 	
  * chmod <path> <mode>
-	Change mode of path to given mode. Like with makedev, heading '0' is
-	necessary for the mode to be interpreted as octal.
+	Change mode of path to given mode. Mode is interpreted as octal.
 
  * setenv <key> <value>
 	Sets environmental variable key to the given value. If an env var
 	of the given name already exists, it gets overwritten.
+
+ * printdebug
+ 	Prints all variables read from kernel.
+
+ FLAGS
+ -----
+
+Flags are, syntactically, just like actions; their semantical value is different however.
+Instead of doing something, they instead change the general behavior of the processing
+of the given rule.
+
+Note that for flags to work, you also have to invoke it with --override.
+
+Currently, only one flag is implemented:
+
+ * nothrottle
+ 	Forcibly overrides hotplug2 throttling mechanism. If _all_ rules that match
+	the given kernel event have 'nothrottle' set, hotplug2 will not wait for
+	children count to get under max-children limit. That allows to throttle
+	eg. helper application execution or modprobes, but yet keep node devices
+	fast.
 	
  ESCAPING
  --------
@@ -136,8 +155,9 @@
  SAMPLE CONFIG
  -------------
 
-Below is a sample hotplug2.rules file. It loads modules to all available devices
-quietly and creates device nodes for block devices.
+Below is a sample hotplug2.rules file. It loads modules to all available
+devices quietly and creates device nodes for block devices. Note that this
+sample is not very viable for real life usage.
 ---------------------------------------------------------------------------------
 MODALIAS is set {
 	exec modprobe -q %MODALIAS% ;
@@ -146,3 +166,33 @@
 SUBSYSTEM == block, DEVPATH is set, MAJOR is set, MINOR is set {
 	makedev /dev/%DEVICENAME% 0644
 }
+
+
+Please find also the more complex set of rules, dedicated to handling most
+common needs.
+---------------------------------------------------------------------------------
+#For debugging
+#ACTION is set {
+#	printdebug
+#}
+
+# Load modules (what old hotplug did)
+MODALIAS is set {
+	exec modprobe -q %MODALIAS% ;
+}
+
+# Create device nodes
+DEVPATH is set, MAJOR is set, MINOR is set {
+	makedev /dev/%DEVICENAME% 0644
+}
+
+# Mount a USB flashdisk
+ACTION == add, PHYSDEVPATH ~~ "/usb[0-9]*/", DEVICENAME ~~ "^sd[a-z][0-9]+$", DEVPATH is set, MAJOR is set, MINOR is set {
+	makedev /dev/%DEVICENAME% 0644
+	exec mount /dev/%DEVICENAME% /mnt/%DEVICENAME%
+}
+
+# Unmount a USB flashdisk
+ACTION == remove, PHYSDEVPATH ~~ "/usb[0-9]*/", DEVICENAME ~~ "^sd[a-z][0-9]+$", MAJOR is set, MINOR is set {
+	exec umount /mnt/%DEVICENAME%
+}
diff -urN -x .svn hotplug2-0.9/docs/Makefile hotplug2/docs/Makefile
--- hotplug2-0.9/docs/Makefile	2006-09-26 00:27:02.000000000 +0200
+++ hotplug2/docs/Makefile	2007-06-28 14:50:59.875955008 +0200
@@ -2,12 +2,13 @@
 
 BINS=
 SUBDIRS=
-
+DESTDIR=
+MANDIR=/usr/share/man
 
 all:
 
 install:
-	$(INSTALL) $(wildcard *.8) /usr/share/man/man8/
+	$(INSTALL) $(wildcard *.8) $(DESTDIR)$(MANDIR)/man8/
 
 
 include ../common.mak
diff -urN -x .svn hotplug2-0.9/examples/Makefile hotplug2/examples/Makefile
--- hotplug2-0.9/examples/Makefile	2006-09-26 01:03:08.000000000 +0200
+++ hotplug2/examples/Makefile	2007-06-28 14:50:59.991937376 +0200
@@ -2,19 +2,23 @@
 
 BINS=
 SUBDIRS=
+DESTDIR=
+KERNELVER=`uname -r`
 
 
 all:
 
 install:
-	case "`uname -r`" in \
-		2.6.*) \
-		$(INSTALL) hotplug2.rules-2.6kernel /etc/hotplug2.rules \
-		;; \
-		*) \
-		$(INSTALL) hotplug2.rules-2.4kernel /etc/hotplug2.rules \
-		;; \
-	esac
+	if ! [ -e "/etc/hotplug2.rules" ]; then \
+		case "$(KERNELVER)" in \
+			2.6.*) \
+			$(INSTALL) hotplug2.rules-2.6kernel $(DESTDIR)/etc/hotplug2.rules \
+			;; \
+			*) \
+			$(INSTALL) hotplug2.rules-2.4kernel $(DESTDIR)/etc/hotplug2.rules \
+			;; \
+		esac; \
+	fi;
 
 
 include ../common.mak
diff -urN -x .svn hotplug2-0.9/filemap_utils.c hotplug2/filemap_utils.c
--- hotplug2-0.9/filemap_utils.c	2006-09-25 12:14:12.000000000 +0200
+++ hotplug2/filemap_utils.c	2007-07-09 02:01:10.966249750 +0200
@@ -16,7 +16,15 @@
 
 #include "filemap_utils.h"
 
-int map_file(char *filename, struct filemap_t *filemap) {
+/**
+ * Basic open/mmap wrapper to make things simpler.
+ *
+ * @1 Filename of the mmaped file
+ * @2 Pointer to filemap structure
+ *
+ * Returns: 0 if success, 1 otherwise
+ */
+int map_file(const char *filename, struct filemap_t *filemap) {
 	struct stat statbuf;
 	
 	filemap->fd = open(filename, O_RDONLY);
@@ -40,9 +48,16 @@
 	return 0;
 }
 
+/**
+ * Basic close/munmap wrapper.
+ *
+ * @1 Pointer to filemap structure
+ *
+ * Returns: always 0
+ */
 int unmap_file(struct filemap_t *filemap) {
-	close(filemap->fd);
 	munmap(filemap->map, filemap->size);
+	close(filemap->fd);
 	
 	return 0;
 }
diff -urN -x .svn hotplug2-0.9/filemap_utils.h hotplug2/filemap_utils.h
--- hotplug2-0.9/filemap_utils.h	2006-09-25 22:24:36.000000000 +0200
+++ hotplug2/filemap_utils.h	2007-07-09 02:01:10.962249500 +0200
@@ -14,6 +14,6 @@
 	void *map;
 };
 
-int map_file(char *, struct filemap_t *);
+int map_file(const char *, struct filemap_t *);
 int unmap_file(struct filemap_t *);
 #endif
diff -urN -x .svn hotplug2-0.9/hotplug2.c hotplug2/hotplug2.c
--- hotplug2-0.9/hotplug2.c	2006-10-08 15:18:23.000000000 +0200
+++ hotplug2/hotplug2.c	2007-07-09 02:01:10.962249500 +0200
@@ -23,7 +23,9 @@
 #include <linux/netlink.h>
 
 #include "mem_utils.h"
+#include "filemap_utils.h"
 #include "hotplug2.h"
+#include "hotplug2_utils.h"
 #include "rules.h"
 #include "childlist.h"
 
@@ -32,10 +34,16 @@
 					child == NULL && \
 					highest_seqnum == get_kernel_seqnum())
 
+/*
+ * These variables are accessed from throughout the code.
+ *
+ * TODO: Move this into a hotplug2_t-like variable.
+ */
 event_seqnum_t highest_seqnum = 0;
 pid_t coldplug_p;
 int coldplug = 1;
 int persistent = 0;
+int override = 0;
 int max_child_c = 20;
 int dumb = 0;
 int terminate = 0;
@@ -45,6 +53,14 @@
 
 char *modprobe_command = NULL;
 
+/**
+ * Release all memory associated with an uevent read from kernel. The given
+ * pointer is no longer valid, as it gets freed as well.
+ *
+ * @1 The event that is to be freed.
+ *
+ * Returns: void
+ */
 inline void free_hotplug2_event(struct hotplug2_event_t *event) {
 	int i;
 	
@@ -57,6 +73,13 @@
 	free(event);
 }
 
+/**
+ * A trivial function determining the action that the uevent.
+ *
+ * @1 String containing the action name (null-terminated).
+ *
+ * Returns: Macro of the given action
+ */
 inline int get_hotplug2_event_action(char *action) {
 	if (!strcmp(action, "add"))
 		return ACTION_ADD;
@@ -67,6 +90,14 @@
 	return ACTION_UNKNOWN;
 }
 
+/**
+ * Looks up a value according to the given key.
+ *
+ * @1 A hotplug event structure
+ * @2 Key for lookup
+ *
+ * Returns: The value of the key or NULL if no such key found
+ */
 char *get_hotplug2_value_by_key(struct hotplug2_event_t *event, char *key) {
 	int i;
 	
@@ -78,7 +109,16 @@
 	return NULL;
 }
 
-inline int add_hotplug2_event_env(struct hotplug2_event_t *event, char *item) {
+/**
+ * Appends a key-value pair described by the second argument to the
+ * hotplug event.
+ *
+ * @1 A hotplug event structure
+ * @1 An item in format "key=value" to be appended
+ *
+ * Returns: 0 if success, -1 if the string is malformed
+ */
+int add_hotplug2_event_env(struct hotplug2_event_t *event, char *item) {
 	char *ptr, *tmp;
 	
 	ptr = strchr(item, '=');
@@ -94,6 +134,8 @@
 	
 	/*
 	 * Variables not generated by kernel but demanded nonetheless...
+	 *
+	 * TODO: Split this to a different function
 	 */
 	if (!strcmp(item, "DEVPATH")) {
 		event->env_vars_c++;
@@ -109,6 +151,15 @@
 	return 0;
 }
 
+/**
+ * Duplicates all allocated memory of a source hotplug event
+ * and returns a new hotplug event, an identical copy of the
+ * source event.
+ *
+ * @1 Source hotplug event structure
+ *
+ * Returns: A copy of the source event structure
+ */
 inline struct hotplug2_event_t *dup_hotplug2_event(struct hotplug2_event_t *src) {
 	struct hotplug2_event_t *dest;
 	int i;
@@ -129,6 +180,14 @@
 	return dest;
 }
 
+/**
+ * Parses a string into a hotplug event structurs.
+ *
+ * @1 The event string (not null terminated)
+ * @2 The size of the event string
+ *
+ * Returns: A new event structure
+ */
 inline struct hotplug2_event_t *get_hotplug2_event(char *event_str, int size) {
 	char *ptr;
 	struct hotplug2_event_t *event;
@@ -161,59 +220,15 @@
 	return event;
 }
 
-inline event_seqnum_t get_kernel_seqnum() {
-	FILE *fp;
-	
-	char filename[64];
-	char seqnum[64];
-	
-	strcpy(filename, sysfs_seqnum_path);
-	
-	fp = fopen(filename, "r");
-	if (fp == NULL)
-		return 0;
-	
-	fread(seqnum, 1, 64, fp);
-	fclose(fp);
-	
-	return strtoull(seqnum, NULL, 0);
-}
-
-inline int init_netlink_socket() {
-	int netlink_socket;
-	struct sockaddr_nl snl;
-	int buffersize = 16 * 1024 * 1024;
-	
-	memset(&snl, 0x00, sizeof(struct sockaddr_nl));
-	snl.nl_family = AF_NETLINK;
-	snl.nl_pid = getpid();
-	snl.nl_groups = 1;
-	netlink_socket = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); 
-	if (netlink_socket == -1) {
-		ERROR("opening netlink","Failed socket: %s.", strerror(errno));
-		return -1;
-	}
-	
-	if (setsockopt(netlink_socket, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize))) {
-		ERROR("opening netlink","Failed setsockopt: %s. (non-critical)", strerror(errno));
-		
-		/* Somewhat safe default. */
-		buffersize = 106496;
-		
-		if (setsockopt(netlink_socket, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize))) {
-			ERROR("opening netlink","Failed setsockopt: %s. (critical)", strerror(errno));
-		}
-	}
-	
-	if (bind(netlink_socket, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl))) {
-		ERROR("opening netlink","Failed bind: %s.", strerror(errno));
-		close(netlink_socket);
-		return -1;
-	}
-	
-	return netlink_socket;
-}
-
+/**
+ * Evaluates an argument into a true/false value.
+ *
+ * @1 argument
+ * @2 argument flag
+ * @3 pointer to output value
+ *
+ * Returns: 0 if success, -1 otherwise
+ */
 int get_bool_opt(char *argv, char *name, int *value) {
 	int rv = -1;
 	
@@ -238,7 +253,13 @@
 	}
 }
 
-void cleanup(void) {
+/**
+ * Performs a cleanup; closes uevent socket, resets signal
+ * handlers, waits for all the children.
+ *
+ * Returns: void
+ */
+void cleanup() {
 	pid_t p;
 	
 	close(netlink_socket);
@@ -254,6 +275,13 @@
 	INFO("cleanup", "All children terminated.");
 }
 
+/**
+ * Handles all signals.
+ *
+ * @1 Signal identifier
+ *
+ * Returns: void
+ */
 void sighandler(int sig) {
 	pid_t p;
 	
@@ -313,6 +341,14 @@
 }
 
 #ifdef HAVE_RULES
+/**
+ * Execute all rules for this particular event.
+ *
+ * @1 Hotplug event structure
+ * @2 Rules structure, containing array of rules
+ *
+ * Returns: void
+ */
 void perform_action(struct hotplug2_event_t *event, struct rules_t *rules) {
 	int i, rv;
 	
@@ -324,13 +360,72 @@
 	
 	free_hotplug2_event(event);
 }
+
+/**
+ * Iterates through all rules, and performs an AND between all flags that
+ * would apply during execution (ie. all rules that have conditions matching
+ * the hotplug event).
+ *
+ * @1 Hotplug event structure
+ * @2 Rules structure, containing array of rules
+ *
+ * Returns: Flags that apply to all matching rules
+ */
+int flags_eval(struct hotplug2_event_t *event, struct rules_t *rules) {
+	int flags = FLAG_ALL;
+	int match = 0;
+	int i, j;
+
+	for (i = 0; i < rules->rules_c; i++) {
+		match = 1;
+
+		for (j = 0; j < rules->rules[i].conditions_c; j++) {
+			if (rule_condition_eval(event, &rules->rules[i].conditions[j]) != EVAL_MATCH) {
+				match = 0;
+				break;
+			}
+		}
+
+		/*
+		 * Logical AND between flags we've got already and
+		 * those we're adding.
+		 */
+		if (match) {
+			rule_flags(&rules->rules[i]);
+			flags &= rules->rules[i].flags;
+		}
+	}
+
+	/*
+	 * A little trick; if no rule matched, we return FLAG_ALL
+	 * and have it skipped completely.
+	 */
+
+	return flags;
+}
+#else
+#define perform_action(event, rules)
 #endif
 
+/**
+ * Blindly modprobe the modalias, nothing more.
+ *
+ * @1 Hotplug event structure
+ * @2 Modalias to be loaded
+ *
+ * Returns: void
+ */
 void perform_dumb_action(struct hotplug2_event_t *event, char *modalias) {
 	free_hotplug2_event(event);
 	execl(modprobe_command, modprobe_command, "-q", modalias, NULL);
 }
 
+/**
+ * Attempt to figure out whether our modprobe command can handle modalias.
+ * If not, use our own wrapper.
+ *
+ * Returns: 0 if success, -1 otherwise
+ */
 int get_modprobe_command() {
 	pid_t p;
 	int fds[2];
@@ -381,6 +476,9 @@
 }
 
 int main(int argc, char *argv[]) {
+	/*
+	 * TODO, cleanup
+	 */
 	static char buffer[UEVENT_BUFFER_SIZE+512];
 	struct hotplug2_event_t *tmpevent;
 	char *modalias, *seqnum;
@@ -390,28 +488,39 @@
 	int size;
 	int rv = 0;
 	int i;
+	unsigned int flags;
 	char *coldplug_command = NULL;
+	char *rules_file = HOTPLUG2_RULE_PATH;
 	sigset_t block_mask;
 	
 	struct rules_t *rules = NULL;
-	struct stat statbuf;
-	void *filemap;
-	int rule_fd;
+	struct filemap_t filemap;
 
 	struct options_t bool_options[] = {
 		{"persistent", &persistent},
 		{"coldplug", &coldplug},
 		{"udevtrigger", &coldplug},	/* compatibility */
+		{"override", &override},
 #ifdef HAVE_RULES
 		{"dumb", &dumb},
 #endif
 		{NULL, NULL}
 	};
 	
+	/*
+	 * We parse all the options...
+	 */
 	for (argc--; argc > 0; argc--) {
 		argv++;
+		/*
+		 * TODO, cleanup
+		 */
 		for (i = 0; bool_options[i].name != NULL; i++) {
 			if (!get_bool_opt(*argv, bool_options[i].name, bool_options[i].value)) {
+				/*
+				 * Bool options are --option or --no-options. If we handled
+				 * it, quit iterating.
+				 */
 				break;
 			} else {
 				if (!strcmp(*argv, "--max-children")) {
@@ -435,52 +544,52 @@
 						break;
 					
 					modprobe_command = *argv;
+				} else if (!strcmp(*argv, "--set-rules-file")) {
+					argv++;
+					argc--;
+					if (argc <= 0)
+						break;
+					
+					rules_file = *argv;
 				}
 			}
 		}
 	}
 	
-#ifdef HAVE_RULES
+#ifndef HAVE_RULES
+	/*
+	 * We don't use rules, so we use dumb mode only.
+	 */
+	dumb = 1;
+#else
+	/*
+	 * We're not in dumb mode, parse the rules. If we fail,
+	 * faillback to dumb mode.
+	 */
 	if (!dumb) {
-		filemap = MAP_FAILED;
-		rule_fd = open(HOTPLUG2_RULE_PATH, O_RDONLY | O_NOATIME);
-		if (rule_fd == -1) {
-			dumb = 1;
-			ERROR("rules parse","Unable to open rules file: %s.", strerror(errno));
-			goto end_rules;
-		}
-		
-		if (fstat(rule_fd, &statbuf)) {
-			dumb = 1;
-			ERROR("rules parse","Unable to stat rules file: %s.", strerror(errno));
-			goto end_rules;
-		}
-		
-		filemap = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, rule_fd, 0);
-		if (filemap == MAP_FAILED) {
+		if (map_file(rules_file, &filemap)) {
+			ERROR("rules parse","Unable to open/mmap rules file.");
 			dumb = 1;
-			ERROR("rules parse","Unable to mmap rules file: %s.", strerror(errno));
 			goto end_rules;
 		}
 		
-		rules = rules_from_config((char*)filemap);
+		rules = rules_from_config((char*)(filemap.map), NULL);
 		if (rules == NULL) {
 			ERROR("rules parse","Unable to parse rules file.");
 			dumb = 1;
 		}
+
+		unmap_file(&filemap);
 		
 end_rules:	
-		if (filemap != MAP_FAILED)
-			munmap(filemap, statbuf.st_size);
-		if (rule_fd != -1)
-			close(rule_fd);
-		
 		if (dumb == 1)
 			ERROR("rules parse","Parsing rules failed, switching to dumb mode.");
-	} else if (!modprobe_command)
-#else
-	if (dumb && !modprobe_command)
+	} else
 #endif
+	/*
+	 * No modprobe command specified, let's autodetect it.
+	 */
+	if (!modprobe_command)
 	{
 		if (get_modprobe_command()) {
 			ERROR("modprobe_command","Unable to autodetect modprobe command.");
@@ -489,7 +598,10 @@
 		DBG("modprobe_command", "Using modprobe: `%s'.", modprobe_command);
 	}
 	
-	netlink_socket = init_netlink_socket();
+	/*
+	 * Open netlink socket to read the uevents
+	 */
+	netlink_socket = init_netlink_socket(NETLINK_BIND);
 	
 	if (netlink_socket == -1) {
 		ERROR("netlink init","Unable to open netlink socket.");
@@ -503,6 +615,9 @@
 	signal(SIGINT, sighandler);
 	signal(SIGCHLD, sighandler);
 	
+	/*
+	 * If we desire coldplugging, we initiate it right now.
+	 */
 	if (coldplug) {
 		if (coldplug_command == NULL)
 			coldplug_command = UDEVTRIGGER_COMMAND;
@@ -523,10 +638,19 @@
 		coldplug_p = FORK_FINISHED;
 	}
 	
+	/*
+	 * Main loop reading uevents
+	 */
 	while (!terminate) {
+		/*
+		 * Read the uevent packet
+		 */
 		size = recv(netlink_socket, &buffer, sizeof(buffer), 0);
 		recv_errno = errno;
 	
+		/*
+		 * Parse the event into an event structure
+		 */
 		tmpevent = get_hotplug2_event(buffer, size);
 		
 		if (tmpevent == NULL) {
@@ -534,26 +658,61 @@
 			continue;
 		}
 		
+		/*
+		 * Look up two important items of the event
+		 */
 		modalias = get_hotplug2_value_by_key(tmpevent, "MODALIAS");
 		seqnum = get_hotplug2_value_by_key(tmpevent, "SEQNUM");
-		
+
+		/*
+		 * Seqnum is necessary not to end up in a race with the kernel.
+		 */
 		if (seqnum == NULL) {
 			free_hotplug2_event(tmpevent);
 			ERROR("reading events", "Malformed event read (missing SEQNUM).");
 			continue;
 		}
 		
+		/*
+		 * Maintain seqnum continuity
+		 */
 		cur_seqnum = strtoull(seqnum, NULL, 0);
 		if (cur_seqnum > highest_seqnum)
 			highest_seqnum = cur_seqnum;
 		
-		if (tmpevent->action == ACTION_ADD && (!dumb || modalias != NULL)) {
+		/*
+		 * If we are in smart mode, we'll always pass. If we're in dumb mode,
+		 * we only pass events that have 'add' action and have modalias set.
+		 */
+		if ((dumb && tmpevent->action == ACTION_ADD && modalias != NULL) || (!dumb)) {
+			/*
+			 * Pre-evaluation of the flags
+			 */
+			if (!dumb && override) {
+				flags = flags_eval(tmpevent, rules);
+
+				DBG("flags", "flag returned: %8x", flags);
+
+				if (flags == FLAG_ALL)
+					continue;
+			} else {
+				flags = FLAG_UNSET;
+			}
+
 			/* 
 			 * We have more children than we want. Wait until SIGCHLD handler reduces
 			 * their numbers.
+			 *
+			 * Unless, of course, we've specified otherwise and no rules that match
+			 * need throttling.
 			 */
-			while (child_c >= max_child_c) {
-				usleep(HOTPLUG2_THROTTLE_INTERVAL);
+			if (!flags & FLAG_NOTHROTTLE) {
+				/*
+				 * Okay, throttle away!
+				 */
+				while (child_c >= max_child_c) {
+					usleep(HOTPLUG2_THROTTLE_INTERVAL);
+				}
 			}
 			
 			sigemptyset(&block_mask);
@@ -562,17 +721,18 @@
 			p = fork();
 			switch (p) {
 				case -1:
-					ERROR("event","fork failed: %s.", strerror(errno));
+					ERROR("event", "fork failed: %s.", strerror(errno));
 					break;
 				case 0:
+					/*
+					 * TODO: We do not have to dup here, or do we?
+					 */
 					sigprocmask(SIG_UNBLOCK, &block_mask, 0);
 					signal(SIGCHLD, SIG_DFL);
 					signal(SIGUSR1, SIG_DFL);
-#ifdef HAVE_RULES
 					if (!dumb)
 						perform_action(dup_hotplug2_event(tmpevent), rules);
 					else
-#endif
 						perform_dumb_action(dup_hotplug2_event(tmpevent), modalias);
 					exit(0);
 					break;
@@ -593,12 +753,10 @@
 	signal(SIGINT, SIG_DFL);
 	signal(SIGCHLD, SIG_DFL);
 	
-#ifdef HAVE_RULES
 	if (!dumb) {
 		rules_free(rules);
 		free(rules);
 	}
-#endif
 
 	cleanup();
 	
diff -urN -x .svn hotplug2-0.9/hotplug2-dnode.c hotplug2/hotplug2-dnode.c
--- hotplug2-0.9/hotplug2-dnode.c	2006-09-26 17:35:35.000000000 +0200
+++ hotplug2/hotplug2-dnode.c	2007-07-09 01:17:14.869504000 +0200
@@ -27,6 +27,7 @@
 
 #include "mem_utils.h"
 #include "hotplug2.h"
+#include "hotplug2_utils.h"
 #include "parser_utils.h"
 
 #define MODALIAS_MAX_LEN		1024
@@ -45,59 +46,17 @@
 
 #define TEST_INPUT_BIT(i,bm)	(bm[i / BITS_PER_LONG] & (((unsigned long)1) << (i%BITS_PER_LONG)))
 
-int init_netlink_socket() {
-	int netlink_socket;
-	struct sockaddr_nl snl;
-	int buffersize = 16 * 1024 * 1024;
-	
-	memset(&snl, 0x00, sizeof(struct sockaddr_nl));
-	snl.nl_family = AF_NETLINK;
-	snl.nl_pid = getpid();
-	snl.nl_groups = 1;
-	netlink_socket = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); 
-	if (netlink_socket == -1) {
-		ERROR("opening netlink","Failed socket: %s.", strerror(errno));
-		return -1;
-	}
-	
-	if (setsockopt(netlink_socket, SOL_SOCKET, SO_SNDBUFFORCE, &buffersize, sizeof(buffersize))) {
-		ERROR("opening netlink","Failed setsockopt: %s. (non-critical)", strerror(errno));
-		
-		/* Somewhat safe default. */
-		buffersize = 106496;
-		
-		if (setsockopt(netlink_socket, SOL_SOCKET, SO_SNDBUF, &buffersize, sizeof(buffersize))) {
-			ERROR("opening netlink","Failed setsockopt: %s. (critical)", strerror(errno));
-		}
-	}
-	
-	if (connect(netlink_socket, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl))) {
-		ERROR("opening netlink","Failed bind: %s.", strerror(errno));
-		close(netlink_socket);
-		return -1;
-	}
-	
-	return netlink_socket;
-}
-
-inline event_seqnum_t get_kernel_seqnum() {
-	FILE *fp;
-	
-	char filename[64];
-	char seqnum[64];
-	
-	strcpy(filename, sysfs_seqnum_path);
-	
-	fp = fopen(filename, "r");
-	if (fp == NULL)
-		return 0;
-	
-	fread(seqnum, 1, 64, fp);
-	fclose(fp);
-	
-	return strtoull(seqnum, NULL, 0);
-}
-
+/**
+ * Parses a bitmap; output is a list of offsets of bits of a bitmap
+ * of arbitrary size that are set to 1.
+ *
+ * @1 Name of the bitmap parsed
+ * @2 The actual bitmap pointer
+ * @3 Lower boundary of the bitmap
+ * @4 Upper boundary of the bitmap
+ *
+ * Returns: Newly allocated string containing the offsets
+ */
 char *bitmap_to_bitstring(char name, unsigned long *bm, unsigned int min_bit, unsigned int max_bit)
 {
 	char *rv;
@@ -120,6 +79,15 @@
 	return rv;
 }
 
+/**
+ * Reverses the bitmap_to_bitstring function. 
+ *
+ * @1 Bitstring to be converted
+ * @2 Output bitmap
+ * @3 Size of the whole bitmap
+ *
+ * Returns: void
+ */
 void string_to_bitmap(char *input, unsigned long *bitmap, int bm_len) {
 	char *token, *ptr;
 	int i = 0;
@@ -146,6 +114,14 @@
 	} \
 	bitmap = bitmap_to_bitstring(name, bitmap ## _bits, min, mapkey ## _MAX);
 
+/**
+ * Creates an input modalias out of preset environmental variables.
+ *
+ * @1 Pointer to where modalias will be created
+ * @2 Maximum size of the modalias
+ *
+ * Returns: 0 if success, -1 otherwise
+ */
 int get_input_modalias(char *modalias, int modalias_len) {
 	char *product_env;
 	char *ptr;
@@ -245,6 +221,14 @@
 #undef NBITS
 #undef TEST_INPUT_BIT
 
+/**
+ * Creates a PCI modalias out of preset environmental variables.
+ *
+ * @1 Pointer to where modalias will be created
+ * @2 Maximum size of the modalias
+ *
+ * Returns: 0 if success, -1 otherwise
+ */
 int get_pci_modalias(char *modalias, int modalias_len) {
 	char *class_env, *id_env, *subsys_env;
 	char *ptr;
@@ -290,6 +274,15 @@
 	return 0;
 }
 
+/**
+ * Creates an IEEE1394 (FireWire) modalias out of preset environmental
+ * variables.
+ *
+ * @1 Pointer to where modalias will be created
+ * @2 Maximum size of the modalias
+ *
+ * Returns: 0 if success, -1 otherwise
+ */
 int get_ieee1394_modalias(char *modalias, int modalias_len) {
 	char *vendor_env, *model_env;
 	char *specifier_env, *version_env;
@@ -317,6 +310,14 @@
 	return 0;
 }
 
+/**
+ * Creates a serio modalias out of preset environmental variables.
+ *
+ * @1 Pointer to where modalias will be created
+ * @2 Maximum size of the modalias
+ *
+ * Returns: 0 if success, -1 otherwise
+ */
 int get_serio_modalias(char *modalias, int modalias_len) {
 	char *serio_type_env, *serio_proto_env;
 	char *serio_id_env, *serio_extra_env;
@@ -344,6 +345,14 @@
 	return 0;
 }
 
+/**
+ * Creates an USB modalias out of preset environmental variables.
+ *
+ * @1 Pointer to where modalias will be created
+ * @2 Maximum size of the modalias
+ *
+ * Returns: 0 if success, -1 otherwise
+ */
 int get_usb_modalias(char *modalias, int modalias_len) {
 	char *product_env, *type_env, *interface_env;
 	char *ptr;
@@ -409,6 +418,16 @@
 	return 0;
 }
 
+/**
+ * Distributes modalias generating according to the bus name.
+ *
+ * @1 Bus name
+ * @2 Pointer to where modalias will be created
+ * @3 Maximum size of the modalias
+ *
+ * Returns: The return value of the subsystem modalias function, or -1 if
+ * no match.
+ */
 int get_modalias(char *bus, char *modalias, int modalias_len) {
 	memset(modalias, 0, modalias_len);
 	
@@ -435,6 +454,16 @@
 	return -1;
 }
 
+/**
+ * Turns all environmental variables as set when invoked by /proc/sys/hotplug
+ * into an uevent formatted (thus not null-terminated) string.
+ *
+ * @1 All environmental variables
+ * @2 Bus of the event (as read from argv)
+ * @3 Pointer to size of the returned uevent string
+ *
+ * Returns: Not null terminated uevent string.
+ */
 inline char *get_uevent_string(char **environ, char *bus, unsigned long *uevent_string_len) {
 	char *uevent_string;
 	char *tmp;
@@ -516,7 +545,7 @@
 		return 1;
 	}
 	
-	netlink_socket = init_netlink_socket();
+	netlink_socket = init_netlink_socket(NETLINK_CONNECT);
 	if (netlink_socket == -1) {
 		ERROR("netlink init","Unable to open netlink socket.");
 		goto exit;
diff -urN -x .svn hotplug2-0.9/hotplug2.h hotplug2/hotplug2.h
--- hotplug2-0.9/hotplug2.h	2006-10-08 12:21:56.000000000 +0200
+++ hotplug2/hotplug2.h	2007-07-09 01:17:14.865503750 +0200
@@ -34,7 +34,7 @@
 #endif
 
 #ifndef O_NOATIME
-#define O_NOATIME					01000000
+#define O_NOATIME			01000000
 #endif
 
 #define ERROR(action, fmt, arg...)	fprintf(stderr, "[%s]: " fmt"\n", action, ##arg);
@@ -47,7 +47,7 @@
 
 #define UEVENT_BUFFER_SIZE		2048
 #define HOTPLUG2_POLL_INTERVAL		20000
-#define HOTPLUG2_THROTTLE_INTERVAL		10000
+#define HOTPLUG2_THROTTLE_INTERVAL	10000
 #define HOTPLUG2_RULE_PATH		"/etc/hotplug2.rules"
 
 #define ACTION_ADD			0
diff -urN -x .svn hotplug2-0.9/hotplug2_utils.c hotplug2/hotplug2_utils.c
--- hotplug2-0.9/hotplug2_utils.c	1970-01-01 01:00:00.000000000 +0100
+++ hotplug2/hotplug2_utils.c	2007-07-09 01:17:14.869504000 +0200
@@ -0,0 +1,96 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+#include <linux/types.h>
+#include <linux/netlink.h>
+
+#include "hotplug2_utils.h"
+
+/**
+ * A trivial function that reads kernel seqnum from sysfs.
+ *
+ * Returns: Seqnum as read from sysfs
+ */
+inline event_seqnum_t get_kernel_seqnum() {
+	FILE *fp;
+	
+	char filename[64];
+	char seqnum[64];
+	
+	strcpy(filename, sysfs_seqnum_path);
+	
+	fp = fopen(filename, "r");
+	if (fp == NULL)
+		return 0;
+	
+	fread(seqnum, 1, 64, fp);
+	fclose(fp);
+	
+	return strtoull(seqnum, NULL, 0);
+}
+
+/**
+ * Opens a PF_NETLINK socket into the kernel, to read uevents.
+ *
+ * @1 Specifies type of socket (whether we bind or whether we connect) 
+ *
+ * Returns: Socket fd if succesful, -1 otherwise.
+ */
+inline int init_netlink_socket(int type) {
+	int netlink_socket;
+	struct sockaddr_nl snl;
+	int buffersize = 16 * 1024 * 1024;
+	
+	memset(&snl, 0x00, sizeof(struct sockaddr_nl));
+	snl.nl_family = AF_NETLINK;
+	snl.nl_pid = getpid();
+	snl.nl_groups = 1;
+	netlink_socket = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); 
+	if (netlink_socket == -1) {
+		ERROR("opening netlink","Failed socket: %s.", strerror(errno));
+		return -1;
+	}
+	
+	/*
+	 * We're trying to override buffer size. If we fail, we attempt to set a big buffer and pray.
+	 */
+	if (setsockopt(netlink_socket, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize))) {
+		ERROR("opening netlink","Failed setsockopt: %s. (non-critical)", strerror(errno));
+		
+		/* Somewhat safe default. */
+		buffersize = 106496;
+		
+		if (setsockopt(netlink_socket, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize))) {
+			ERROR("opening netlink","Failed setsockopt: %s. (critical)", strerror(errno));
+		}
+	}
+	
+	/*
+	 * hotplug2-dnode performs connect, while hotplug2 daemon binds
+	 */
+	switch (type) {
+		case NETLINK_CONNECT:
+			if (connect(netlink_socket, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl))) {
+				ERROR("opening netlink","Failed connect: %s.", strerror(errno));
+				close(netlink_socket);
+				return -1;
+			}
+			
+		default:
+			if (bind(netlink_socket, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl))) {
+				ERROR("opening netlink","Failed bind: %s.", strerror(errno));
+				close(netlink_socket);
+				return -1;
+			}
+	}
+	
+	return netlink_socket;
+}
diff -urN -x .svn hotplug2-0.9/hotplug2_utils.h hotplug2/hotplug2_utils.h
--- hotplug2-0.9/hotplug2_utils.h	1970-01-01 01:00:00.000000000 +0100
+++ hotplug2/hotplug2_utils.h	2007-07-09 01:17:14.869504000 +0200
@@ -0,0 +1,21 @@
+/*****************************************************************************\
+*  _  _       _          _              ___                                   *
+* | || | ___ | |_  _ __ | | _  _  __ _ |_  )                                  *
+* | __ |/ _ \|  _|| '_ \| || || |/ _` | / /                                   *
+* |_||_|\___/ \__|| .__/|_| \_,_|\__, |/___|                                  *
+*                 |_|            |___/                                        *
+\*****************************************************************************/
+
+#ifndef HOTPLUG2_UTILS_H
+#define HOTPLUG2_UTILS_H 1
+
+#include "hotplug2.h"
+
+#define NETLINK_UNDEFINED	0
+#define NETLINK_CONNECT		1
+#define NETLINK_BIND		2
+
+inline event_seqnum_t get_kernel_seqnum();
+inline int init_netlink_socket(int);
+
+#endif
diff -urN -x .svn hotplug2-0.9/linux24_compat/hotplug2-coldplug-2.4.c hotplug2/linux24_compat/hotplug2-coldplug-2.4.c
--- hotplug2-0.9/linux24_compat/hotplug2-coldplug-2.4.c	2006-09-25 22:22:47.000000000 +0200
+++ hotplug2/linux24_compat/hotplug2-coldplug-2.4.c	2007-07-09 01:17:14.793499250 +0200
@@ -28,59 +28,7 @@
 #include "../mem_utils.h"
 #include "../parser_utils.h"
 #include "../filemap_utils.h"
-
-inline int init_netlink_socket() {
-	int netlink_socket;
-	struct sockaddr_nl snl;
-	int buffersize = 16 * 1024 * 1024;
-	
-	memset(&snl, 0x00, sizeof(struct sockaddr_nl));
-	snl.nl_family = AF_NETLINK;
-	snl.nl_pid = getpid();
-	snl.nl_groups = 1;
-	netlink_socket = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); 
-	if (netlink_socket == -1) {
-		ERROR("opening netlink","Failed socket: %s.", strerror(errno));
-		return -1;
-	}
-	
-	if (setsockopt(netlink_socket, SOL_SOCKET, SO_SNDBUFFORCE, &buffersize, sizeof(buffersize))) {
-		ERROR("opening netlink","Failed setsockopt: %s. (non-critical)", strerror(errno));
-		
-		/* Somewhat safe default. */
-		buffersize = 106496;
-		
-		if (setsockopt(netlink_socket, SOL_SOCKET, SO_SNDBUF, &buffersize, sizeof(buffersize))) {
-			ERROR("opening netlink","Failed setsockopt: %s. (critical)", strerror(errno));
-		}
-	}
-	
-	if (connect(netlink_socket, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl))) {
-		ERROR("opening netlink","Failed bind: %s.", strerror(errno));
-		close(netlink_socket);
-		return -1;
-	}
-	
-	return netlink_socket;
-}
-
-inline event_seqnum_t get_kernel_seqnum() {
-	FILE *fp;
-	
-	char filename[64];
-	char seqnum[64];
-	
-	strcpy(filename, sysfs_seqnum_path);
-	
-	fp = fopen(filename, "r");
-	if (fp == NULL)
-		return 0;
-	
-	fread(seqnum, 1, 64, fp);
-	fclose(fp);
-	
-	return strtoull(seqnum, NULL, 0);
-}
+#include "../hotplug2_utils.h"
 
 inline char *get_uevent_string(char **environ, unsigned long *uevent_string_len) {
 	char *uevent_string;
@@ -413,7 +361,7 @@
 int main(int argc, char *argv[], char **environ) {
 	int netlink_socket;
 	
-	netlink_socket = init_netlink_socket();
+	netlink_socket = init_netlink_socket(NETLINK_CONNECT);
 	if (netlink_socket == -1) {
 		ERROR("netlink init","Unable to open netlink socket.");
 		return 1;
diff -urN -x .svn hotplug2-0.9/linux24_compat/hotplug2-modwrap.c hotplug2/linux24_compat/hotplug2-modwrap.c
--- hotplug2-0.9/linux24_compat/hotplug2-modwrap.c	2006-09-25 22:23:07.000000000 +0200
+++ hotplug2/linux24_compat/hotplug2-modwrap.c	2007-07-09 01:17:14.789499000 +0200
@@ -30,8 +30,19 @@
 #include "../parser_utils.h"
 #include "../filemap_utils.h"
 
+#define MODULES_PATH		"/lib/modules/"
+#define MODULES_ALIAS		"modules.alias"
+
+/**
+ * A simple fork/exec wrapper
+ *
+ * @1 Complete argv, including app path
+ *
+ * Returns: -1 if error, children return value otherwise
+ */
 int execute(char **argv) {
 	pid_t p;
+	int status;
 	
 	p = fork();
 	switch (p) {
@@ -42,10 +53,11 @@
 			exit(1);
 			break;
 		default:
-			waitpid(p, NULL, 0);
+			waitpid(p, &status, 0);
 			break;
 	}
-	return 0;
+
+	return WEXITSTATUS(status);
 }
 
 int main(int argc, char *argv[]) {
@@ -63,21 +75,36 @@
 	
 	match_alias = strdup(argv[argc - 1]);
 	
+	/*
+	 * If we can't do uname, we're absolutely screwed and there's no
+	 * sense thinking twice about anything.
+	 */
 	if (uname(&unamebuf)) {
 		ERROR("uname", "Unable to perform uname: %s.", strerror(errno));
 		return 1;
 	}
 	
-	/* We use this one */
+	/*
+	 * We allow setting the modprobe command to an arbitrary value.
+	 *
+	 * The whole trick lies in executing modprobe with exactly the
+	 * same argv as this app was executed, except we use a different
+	 * argv[0] (application path) and argv[argc-1] (we substitute
+	 * the given modalias by the matching module name)
+	 */
 	argv[0] = getenv("MODPROBE_COMMAND");
 	if (argv[0] == NULL)
 		argv[0] = "/sbin/modprobe";
-	
-	/* "/lib/modules/" + "/" + "\0" */
-	filename = xmalloc(15 + strlen(unamebuf.release) + strlen("modules.alias"));
-	strcpy(filename, "/lib/modules/");
+
+	/*
+	 * Compose a path, /lib/modules/`uname -r`/modules.alias
+	 *
+	 * "/lib/modules/" + "/" + "\0" 
+	 */
+	filename = xmalloc(strlen(MODULES_PATH) + strlen(unamebuf.release) + strlen(MODULES_ALIAS));
+	strcpy(filename, MODULES_PATH);
 	strcat(filename, unamebuf.release);
-	strcat(filename, "/modules.alias");
+	strcat(filename, MODULES_ALIAS);
 	
 	if (map_file(filename, &aliasmap)) {
 		ERROR("map_file", "Unable to map file: `%s'.", filename);
@@ -86,10 +113,16 @@
 		return 1;
 	}
 	
+	/*
+	 * Read all the aliases, match them against given parameter.
+	 */
 	nptr = aliasmap.map;
 	while ((line = dup_line(nptr, &nptr)) != NULL) {
 		nline = line;
 		
+		/*
+		 * We want aliases only
+		 */
 		token = dup_token(nline, &nline, isspace);
 		if (!token || strcmp(token, "alias")) {
 			free(token);
@@ -98,12 +131,18 @@
 		}
 		free(token);
 		
+		/*
+		 * It's an alias, so fetch it
+		 */
 		cur_alias = dup_token(nline, &nline, isspace);
 		if (!cur_alias) {
 			free(line);
 			continue;
 		}
 		
+		/*
+		 * And now we get the module name
+		 */
 		module = dup_token(nline, &nline, isspace);
 		if (!module) {
 			free(line);
@@ -111,10 +150,14 @@
 			continue;
 		}
 		
+		/*
+		 * If we match, we do the modalias->module name
+		 * substitution as described above and execute.
+		 */
 		if (!fnmatch(cur_alias, match_alias, 0)) {
 			argv[argc - 1] = module;
 			if (execute(argv)) {
-				ERROR("execute", "Unable to execute: `%s'.", argv[0]);
+				ERROR("execute", "Error during exection of: `%s'.", argv[0]);
 			}
 		}
 		
@@ -122,6 +165,17 @@
 		free(module);
 		free(line);
 	}
+
+	/*
+	 * Perhaps we didn't match anything, so we might've been given
+	 * a module name instead of a modalias. Try to modprobe it
+	 * right away.
+	 */
+	if (strcmp(argv[argc - 1], match_alias) == 0) {
+		if (execute(argv)) {
+			ERROR("execute", "Error during exection of: `%s'.", argv[0]);
+		}
+	}	
 	
 	free(filename);
 	free(match_alias);
diff -urN -x .svn hotplug2-0.9/linux24_compat/Makefile hotplug2/linux24_compat/Makefile
--- hotplug2-0.9/linux24_compat/Makefile	2006-09-26 00:26:46.000000000 +0200
+++ hotplug2/linux24_compat/Makefile	2007-07-09 01:17:14.793499250 +0200
@@ -2,16 +2,17 @@
 
 BINS=generate_alias hotplug2-coldplug-2.4 hotplug2-modwrap
 SUBDIRS=
+DESTDIR=
 
 
 all: $(BINS)
 
 install:
-	$(INSTALL_BIN) hotplug2-coldplug-2.4 hotplug2-modwrap /sbin/
-	$(INSTALL_BIN) generate_alias /usr/sbin/
+	$(INSTALL_BIN) hotplug2-coldplug-2.4 hotplug2-modwrap $(DESTDIR)/sbin/
+	$(INSTALL_BIN) generate_alias $(DESTDIR)/usr/sbin/
 
 
-hotplug2-coldplug-2.4: hotplug2-coldplug-2.4.o ../parser_utils.o ../filemap_utils.o ../mem_utils.o
+hotplug2-coldplug-2.4: hotplug2-coldplug-2.4.o ../parser_utils.o ../filemap_utils.o ../mem_utils.o ../hotplug2_utils.o
 hotplug2-modwrap: hotplug2-modwrap.o ../parser_utils.o ../filemap_utils.o ../mem_utils.o
 generate_alias: generate_alias.o ../parser_utils.o ../filemap_utils.o ../mem_utils.o
 
diff -urN -x .svn hotplug2-0.9/Makefile hotplug2/Makefile
--- hotplug2-0.9/Makefile	2006-09-26 01:03:08.000000000 +0200
+++ hotplug2/Makefile	2007-07-09 01:17:14.869504000 +0200
@@ -2,16 +2,17 @@
 
 BINS=hotplug2 hotplug2-dnode
 SUBDIRS=linux24_compat docs examples
+DESTDIR=
 
 
 all: $(BINS)
 
 install:
-	$(INSTALL_BIN) $(BINS) /sbin/
+	$(INSTALL_BIN) $(BINS) $(DESTDIR)/sbin/
 
 
-hotplug2: hotplug2.o childlist.o mem_utils.o rules.o
-hotplug2-dnode: hotplug2-dnode.o mem_utils.o parser_utils.o
+hotplug2: hotplug2.o hotplug2_utils.o childlist.o mem_utils.o rules.o filemap_utils.o
+hotplug2-dnode: hotplug2-dnode.o hotplug2_utils.o mem_utils.o parser_utils.o
 
 
 include common.mak
diff -urN -x .svn hotplug2-0.9/mem_utils.c hotplug2/mem_utils.c
--- hotplug2-0.9/mem_utils.c	2006-09-25 22:21:45.000000000 +0200
+++ hotplug2/mem_utils.c	2007-07-09 01:17:14.865503750 +0200
@@ -9,6 +9,13 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+/**
+ * A malloc wrapper. Exits if no memory.
+ *
+ * @1 Ammount of memory to allocate
+ *
+ * Returns: Pointer to freshly allocated memory
+ */
 inline void *xmalloc(size_t size) {
 	void *ptr;
 	ptr = malloc(size);
@@ -19,6 +26,14 @@
 	return ptr;
 }
 
+/**
+ * A realloc wrapper. Exits if no memory.
+ *
+ * @1 Old pointer
+ * @2 Ammount of memory to allocate
+ *
+ * Returns: Pointer to reallocated memory
+ */
 inline void *xrealloc(void *inptr, size_t size) {
 	void *ptr;
 	ptr = realloc(inptr, size);
diff -urN -x .svn hotplug2-0.9/parser_utils.c hotplug2/parser_utils.c
--- hotplug2-0.9/parser_utils.c	2006-09-25 22:21:13.000000000 +0200
+++ hotplug2/parser_utils.c	2007-07-09 01:17:14.865503750 +0200
@@ -12,6 +12,16 @@
 #include "mem_utils.h"
 #include "parser_utils.h"
 
+/**
+ * Creates a newly allocated null-terminated string representing line
+ * starting at a given pointer and ending at the closest newline. If
+ * no newline present, returns NULL. TODO, use dup_token
+ *
+ * @1 Starting pointer
+ * @2 Pointer where the end position is returned
+ *
+ * Returns: Newly allocated string containing the line or NULL
+ */
 char *dup_line(char *start, char **nptr) {
 	char *ptr, *rv;
 	
@@ -29,6 +39,15 @@
 	return rv;
 }
 
+/**
+ * Returns a token delimited by the given function.
+ *
+ * @1 Starting pointer
+ * @2 Pointer where the end position is returned
+ * @3 Function that identifies the delimiter characters
+ *
+ * Returns: Newly allocated string containing the token or NULL
+ */
 char *dup_token(char *start, char **nptr, int (*isdelimiter)(int)) {
 	char *ptr, *rv;
 	
@@ -56,6 +75,16 @@
 	return rv;
 }
 
+/**
+ * Returns the last token delimited by the given function.
+ *
+ * @1 Starting pointer of the whole string
+ * @2 Starting position
+ * @3 Pointer where the end position is returned
+ * @4 Function that identifies the delimiter characters
+ *
+ * Returns: Newly allocated string containing the token or NULL
+ */
 char *dup_token_r(char *start, char *start_string, char **nptr, int (*isdelimiter)(int)) {
 	char *ptr, *rv;
 	
diff -urN -x .svn hotplug2-0.9/rules.c hotplug2/rules.c
--- hotplug2-0.9/rules.c	2006-09-29 22:19:31.000000000 +0200
+++ hotplug2/rules.c	2007-07-09 02:01:10.962249500 +0200
@@ -22,11 +22,18 @@
 #include <sys/stat.h>
 
 #include "mem_utils.h"
+#include "filemap_utils.h"
 #include "hotplug2.h"
 #include "rules.h"
 
-#define last_rule return_rules->rules[return_rules->rules_c - 1]
 
+/**
+ * Function supplementing 'mkdir -p'.
+ *
+ * @1 Path to be mkdir'd
+ *
+ * Returns: void
+ */
 static void mkdir_p(char *path) {
 	char *ptr;
 	struct stat statbuf;
@@ -59,6 +66,40 @@
 	free(path);
 }
 
+/**
+ * Function supplementing 'rmdir -p'.
+ *
+ * @1 Path to be rmdir'd
+ *
+ * Returns: void
+ */
+static void rmdir_p(char *path) {
+	char *ptr;
+	
+	path = strdup(path);
+	ptr = path;
+	while (ptr != NULL) {
+		ptr = strrchr(path, '/');
+		if (ptr == NULL)
+			break;
+		
+		*ptr = '\0';
+		
+		if (rmdir(path))
+			break;
+	}
+	free(path);
+}
+
+/**
+ * Replaces all needles by a given value.
+ *
+ * @1 Haystack (which gets free'd in the function)
+ * @2 Needle
+ * @3 Needle replacement
+ *
+ * Returns: Newly allocated haysteck after replacement.
+ */
 static char *replace_str(char *hay, char *needle, char *replacement) {
         char *ptr, *start, *bptr, *buf;
         int occurences, j;
@@ -128,7 +169,15 @@
         return buf;
 }
 
-inline int isescaped(char *hay, char *ptr) {
+/**
+ * Trivial utility, figuring out whether a character is escaped or not.
+ *
+ * @1 Haystack
+ * @2 Pointer to the character in question
+ *
+ * Returns: 1 if escaped, 0 otherwise
+ */
+static inline int isescaped(char *hay, char *ptr) {
 	if (ptr <= hay)
 		return 0;
 	
@@ -138,6 +187,15 @@
 	return 1;
 }
 
+/**
+ * Performs replacement of all keys by their value based on the hotplug
+ * event structure. Keys are identified as strings %KEY%.
+ *
+ * @1 Haystack
+ * @2 Hotplug event structure
+ *
+ * Returns: Newly allocated haystack (old is freed)
+ */
 static char *replace_key_by_value(char *hay, struct hotplug2_event_t *event) {
 	char *sptr = hay, *ptr = hay;
 	char *buf, *replacement;
@@ -171,6 +229,17 @@
 	return hay;
 }
 
+/**
+ * Obtains all information from hotplug event structure about a device node.
+ * Creates the device node at a given path (expandable by keys) and with
+ * given mode.
+ *
+ * @1 Hotplug event structure
+ * @2 Path (may contain keys)
+ * @3 Mode of the file
+ *
+ * Returns: 0 if success, non-zero otherwise
+ */
 static int make_dev_from_event(struct hotplug2_event_t *event, char *path, mode_t devmode) {
 	char *subsystem, *major, *minor, *devpath;
 	int rv = 1;
@@ -196,12 +265,27 @@
 	path = replace_key_by_value(path, event);
 	mkdir_p(path);
 	rv = mknod(path, devmode, makedev(atoi(major), atoi(minor)));
+
+	/*
+	 * Fixes an issue caused by devmode being modified by umask.
+	 */
+	chmod(path, devmode);
+
 	free(path);
 	
 return_value:
 	return rv;
 }
 
+/**
+ * Execute an application without invoking a shell.
+ *
+ * @1 Hotplug event structure
+ * @2 Path to the application, with expandable keys
+ * @3 Argv for the application, with expandable keys
+ *
+ * Returns: Exit status of the application.
+ */
 static int exec_noshell(struct hotplug2_event_t *event, char *application, char **argv) {
 	pid_t p;
 	int i, status;
@@ -211,11 +295,12 @@
 		case -1:
 			return -1;
 		case 0:
+			application = replace_key_by_value(strdup(application), event);
 			for (i = 0; argv[i] != NULL; i++) {
 				argv[i] = replace_key_by_value(argv[i], event);
 			}
 			execvp(application, argv);
-			exit(0);
+			exit(127);
 			break;
 		default:
 			if (waitpid(p, &status, 0) == -1)
@@ -226,6 +311,14 @@
 	}
 }
 
+/**
+ * Execute an application while invoking a shell.
+ *
+ * @1 Hotplug event structure
+ * @2 The application and all its arguments, with expandable keys
+ *
+ * Returns: Exit status of the application.
+ */
 static int exec_shell(struct hotplug2_event_t *event, char *application) {
 	int rv;
 	
@@ -235,6 +328,15 @@
 	return rv;
 }
 
+/**
+ * Create a symlink, with necessary parent directories.
+ *
+ * @1 Hotplug event structure
+ * @2 Link target, with expandable keys
+ * @3 Link name, with expandable keys
+ *
+ * Returns: return value of symlink()
+ */
 static int make_symlink(struct hotplug2_event_t *event, char *target, char *linkname) {
 	int rv;
 	
@@ -250,11 +352,50 @@
 	return rv;
 }
 
-static int chown_chgrp(int action, char *file, char *param) {
+/**
+ * Chmod a given file.
+ *
+ * @1 Hotplug event structure
+ * @2 File name, with expandable keys
+ * @3 Chmod value, with expandable keys
+ *
+ * Returns: return value of chmod()
+ */
+static int chmod_file(struct hotplug2_event_t *event, char *file, char *value) {
+	int rv;
+
+	file = replace_key_by_value(strdup(file), event);
+	value = replace_key_by_value(strdup(value), event);
+
+	rv = chmod(file, strtoul(value, 0, 8));
+
+	free(file);
+	free(value);
+
+	return rv;
+}
+
+
+/**
+ * Change owner or group of a given file.
+ *
+ * @1 Hotplug event structure
+ * @2 Whether we chown or chgrp
+ * @3 Filename, with expandable keys
+ * @4 Group or user name, with expandable keys
+ *
+ * Returns: return value of chown()
+ */
+static int chown_chgrp(struct hotplug2_event_t *event, int action, char *file, char *param) {
 	struct group *grp;
 	struct passwd *pwd;
 	int rv;
-	
+
+	file = replace_key_by_value(strdup(file), event);
+	param = replace_key_by_value(strdup(param), event);
+
+	rv = -1;
+
 	switch (action) {
 		case ACT_CHOWN:
 			pwd = getpwnam(param);
@@ -265,11 +406,37 @@
 			rv = chown(file, -1, grp->gr_gid);
 			break;
 	}
+
+	free(file);
+	free(param);
 	
-	return -1;
+	return rv;
+}
+
+/**
+ * Prints all uevent keys.
+ *
+ * @1 Hotplug event structure
+ *
+ * Returns: void
+ */
+static void print_debug(struct hotplug2_event_t *event) {
+	int i;
+
+	for (i = 0; i < event->env_vars_c; i++)
+		printf("%s=%s\n", event->env_vars[i].key, event->env_vars[i].value);
 }
 
-static int rule_condition_eval(struct hotplug2_event_t *event, struct condition_t *condition) {
+/**
+ * Evaluates a condition according to a given hotplug event structure.
+ *
+ * @1 Hotplug event structure
+ * @2 Condition to be evaluated
+ *
+ * Returns: 1 if match, 0 if no match, EVAL_NOT_AVAILABLE if unable to
+ * perform evaluation
+ */
+int rule_condition_eval(struct hotplug2_event_t *event, struct condition_t *condition) {
 	int rv;
 	char *event_value = NULL;
 	regex_t preg;
@@ -314,6 +481,16 @@
 	return EVAL_NOT_AVAILABLE;
 }
 
+/**
+ * Executes a rule. Contains evaluation of all conditions prior
+ * to execution.
+ *
+ * @1 Hotplug event structure
+ * @2 The rule to be executed
+ *
+ * Returns: 0 if success, -1 if the whole event is to be 
+ * discared, 1 if bail out of this particular rule was required
+ */
 int rule_execute(struct hotplug2_event_t *event, struct rule_t *rule) {
 	int i, last_rv;
 	
@@ -347,11 +524,11 @@
 				last_rv = make_dev_from_event(event, rule->actions[i].parameter[0], strtoul(rule->actions[i].parameter[1], NULL, 0));
 				break;
 			case ACT_CHMOD:
-				last_rv = chmod(rule->actions[i].parameter[0], strtoul(rule->actions[i].parameter[1], NULL, 0));
+				last_rv = chmod_file(event, rule->actions[i].parameter[0], rule->actions[i].parameter[1]);
 				break;
 			case ACT_CHOWN:
 			case ACT_CHGRP:
-				last_rv = chown_chgrp(rule->actions[i].type, rule->actions[i].parameter[0], rule->actions[i].parameter[1]);
+				last_rv = chown_chgrp(event, rule->actions[i].type, rule->actions[i].parameter[0], rule->actions[i].parameter[1]);
 				break;
 			case ACT_SYMLINK:
 				last_rv = make_symlink(event, rule->actions[i].parameter[0], rule->actions[i].parameter[1]);
@@ -365,12 +542,49 @@
 			case ACT_SETENV:
 				last_rv = setenv(rule->actions[i].parameter[0], rule->actions[i].parameter[1], 1);
 				break;
+			case ACT_REMOVE:
+				last_rv = unlink(rule->actions[i].parameter[0]);
+				rmdir_p(rule->actions[i].parameter[0]);
+				break;
+			case ACT_DEBUG:
+				print_debug(event);
+				last_rv = 0;
+				break;
 		}
 	}
 	
 	return 0;
 }
 
+/**
+ * Sets the flags of the given rule.
+ *
+ * @1 Rule structure
+ *
+ * Returns: void
+ */
+void rule_flags(struct rule_t *rule) {
+	int i;
+
+	for (i = 0; i < rule->actions_c; i++) {
+		switch (rule->actions[i].type) {
+			case ACT_FLAG_NOTHROTTLE:
+				rule->flags |= FLAG_NOTHROTTLE;
+				break;
+		}
+	}
+	
+	return;
+}
+
+/**
+ * Checks whether the given character should initiate
+ * further parsing.
+ *
+ * @1 Character to examine
+ *
+ * Returns: 1 if it should, 0 otherwise
+ */
 static inline int isinitiator(int c) {
 	switch (c) {
 		case ',':
@@ -383,6 +597,16 @@
 	return 0;
 }
 
+/**
+ * Appends a character to a buffer. Enlarges if necessary.
+ *
+ * @1 Pointer to the buffer
+ * @2 Pointer to buffer size
+ * @3 Pointer to last buffer character
+ * @4 Appended character
+ *
+ * Returns: void
+ */
 static inline void add_buffer(char **buf, int *blen, int *slen, char c) {
 	if (*slen + 1 >= *blen) {
 		*blen = *blen + 64;
@@ -394,6 +618,14 @@
 	*slen += 1;
 }
 
+/**
+ * Parses a string into a syntactically acceptable value.
+ *
+ * @1 Input string
+ * @2 Pointer to the new position
+ *
+ * Returns: Newly allocated string.
+ */
 static char *rules_get_value(char *input, char **nptr) {
 	int quotes = QUOTES_NONE;
 	char *ptr = input;
@@ -471,6 +703,16 @@
 	return buf;
 }
 
+/**
+ * Releases all memory associated with the ruleset. TODO: Make
+ * the behavior same for all _free() functions, ie. either
+ * release the given pointer itself or keep it, but do it
+ * in all functions!
+ *
+ * @1 The ruleset to be freed
+ *
+ * Returns: void
+ */
 void rules_free(struct rules_t *rules) {
 	int i, j, k;
 	
@@ -492,10 +734,53 @@
 	free(rules->rules);
 }
 
-struct rules_t *rules_from_config(char *input) {
-	int status = STATUS_KEY, terminate;
+/**
+ * Includes a rule file.
+ *
+ * @1 Filename
+ * @2 The ruleset structure
+ *
+ * Returns: 0 if success, -1 otherwise
+ */
+int rules_include(const char *filename, struct rules_t **return_rules) {
+	struct filemap_t filemap;
+	struct rules_t *rules;
+
+	if (map_file(filename, &filemap)) {
+		ERROR("rules parse","Unable to open/mmap rules file.");
+		return -1;
+	}
+	
+	rules = rules_from_config((char*)(filemap.map), *return_rules);
+	if (rules == NULL) {
+		ERROR("rules parse","Unable to parse rules file.");
+		return -1;
+	}
+
+	unmap_file(&filemap);
+
+	return 0;
+}
+
+/**
+ * Parses an entire file of rules.
+ *
+ * @1 The whole file in memory or mmap'd
+ *
+ * Returns: A newly allocated ruleset.
+ */
+struct rules_t *rules_from_config(char *input, struct rules_t *return_rules) {
+	#define last_rule return_rules->rules[return_rules->rules_c - 1]
+	int nested;
+	int status;	
+	int terminate;
 	char *buf;
-	struct rules_t *return_rules;
+
+	/*
+	 * TODO: cleanup
+	 *
+	 * BIIIG cleanup... Use callbacks for actions and for internal actions.
+	 */
 	
 	int i, j;
 	struct key_rec_t conditions[] = {	/*NOTE: We never have parameters for conditions. */
@@ -506,6 +791,7 @@
 		{"!~", 0, COND_NMATCH_RE},
 		{NULL, 0, -1}
 	};
+
 	struct key_rec_t actions[] = {
 		/*one line / one command*/
 		{"run", 1, ACT_RUN_SHELL},
@@ -518,6 +804,9 @@
 		{"chmod", 2, ACT_CHMOD},
 		{"chgrp", 2, ACT_CHGRP},
 		{"setenv", 2, ACT_SETENV},
+		{"remove", 1, ACT_REMOVE},
+		{"nothrottle", 0, ACT_FLAG_NOTHROTTLE},
+		{"printdebug", 0, ACT_DEBUG},
 		/*symlink*/
 		{"symlink", 2, ACT_SYMLINK},
 		{"softlink", 2, ACT_SYMLINK},
@@ -527,9 +816,19 @@
 		{NULL, 0, -1}
 	};
 
-	return_rules = xmalloc(sizeof(struct rules_t));
-	return_rules->rules_c = 1;
-	return_rules->rules = xmalloc(sizeof(struct rule_t) * return_rules->rules_c);
+	/*
+	 * A little trick for inclusion.
+	 */
+	if (return_rules == NULL) {
+		return_rules = xmalloc(sizeof(struct rules_t));
+		return_rules->rules_c = 1;
+		return_rules->rules = xmalloc(sizeof(struct rule_t) * return_rules->rules_c);
+		nested = 0;
+	} else {
+		nested = 1;
+	}
+
+	status = STATUS_KEY;
 	
 	last_rule.actions = NULL;
 	last_rule.actions_c = 0;
@@ -549,9 +848,26 @@
 			/* Skip to next line */
 			while (*input != '\0' && *input != '\n')
 				input++;
+
+			free(buf);
+			continue;
+		} else if (buf[0] == '$') {
+			buf++;
+
+			/*
+			 * Warning, hack ahead...
+			 */
+			if (!strcmp("include", buf)) {
+				buf = rules_get_value(input, &input);
+				if (rules_include(buf, &return_rules)) {
+					ERROR("rules_include", "Unable to include ruleset '%s'!", buf);
+				}
+			}
+
+			free(buf);
 			continue;
 		}
-		
+
 		switch (status) {
 			case STATUS_KEY:
 				last_rule.conditions_c++;
@@ -684,8 +1000,14 @@
 		return_rules->rules_c--;
 		return return_rules;
 	} else {
-		rules_free(return_rules);
-		free(return_rules);
+		/*
+		 * We don't want to cleanup if we're nested.
+		 */
+		if (!nested) {
+			rules_free(return_rules);
+			free(return_rules);
+		}
+
 		return NULL;
 	}
 }
diff -urN -x .svn hotplug2-0.9/rules.h hotplug2/rules.h
--- hotplug2-0.9/rules.h	2006-09-25 13:42:22.000000000 +0200
+++ hotplug2/rules.h	2007-07-09 02:01:10.962249500 +0200
@@ -24,9 +24,12 @@
 #define ACT_CHGRP			6	/* chgrp <...> */
 #define ACT_CHOWN			7	/* chown <...> */
 #define ACT_SYMLINK			8	/* symlink <...> */
-#define ACT_NEXT_EVENT		9	/* next */
+#define ACT_NEXT_EVENT			9	/* next */
 #define ACT_NEXT_IF_FAILED		10	/* next_if_failed */
 #define ACT_SETENV			11	/* setenv <...> */
+#define ACT_REMOVE			12	/* remove <...> */
+#define ACT_DEBUG			13	/* debug */
+#define ACT_FLAG_NOTHROTTLE		14	/* sets 'nothrottle' flag */
 
 #define EVAL_MATCH			1
 #define EVAL_NOT_MATCH			0
@@ -42,6 +45,10 @@
 #define STATUS_INITIATOR		3	/* ',' for next cond, '{' for block*/
 #define STATUS_ACTION			4	/* viz ACT_* and '}' for end of block */
 
+#define FLAG_UNSET			0
+#define FLAG_ALL			0xffffffff
+#define FLAG_NOTHROTTLE			1	/* We want this rule to ignore max_children limit */
+
 struct key_rec_t {
 	char *key;
 	int param;
@@ -65,6 +72,8 @@
 	
 	struct action_t *actions;
 	int actions_c;
+
+	unsigned int flags;
 };
 
 struct rules_t {
@@ -72,8 +81,10 @@
 	int rules_c;
 };
 
+int rule_condition_eval(struct hotplug2_event_t *, struct condition_t *);
 int rule_execute(struct hotplug2_event_t *, struct rule_t *);
+void rule_flags(struct rule_t *);
 void rules_free(struct rules_t *);
-struct rules_t *rules_from_config(char *);
+struct rules_t *rules_from_config(char *, struct rules_t *);
 
 #endif /* ifndef RULES_H*/
Binary files hotplug2-0.9/.swp and hotplug2/.swp differ
diff -urN -x .svn hotplug2-0.9/TODO hotplug2/TODO
--- hotplug2-0.9/TODO	1970-01-01 01:00:00.000000000 +0100
+++ hotplug2/TODO	2007-06-28 14:51:00.012934184 +0200
@@ -0,0 +1 @@
+ - live rules update (via inotify)