gen-tunables.awk 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Generate dl-tunable-list.h from dl-tunables.list
  2. BEGIN {
  3. min_of["STRING"]="0"
  4. max_of["STRING"]="0"
  5. min_of["INT_32"]="INT32_MIN"
  6. max_of["INT_32"]="INT32_MAX"
  7. min_of["UINT_64"]="0"
  8. max_of["UINT_64"]="UINT64_MAX"
  9. min_of["SIZE_T"]="0"
  10. max_of["SIZE_T"]="SIZE_MAX"
  11. tunable=""
  12. ns=""
  13. top_ns=""
  14. }
  15. # Skip over blank lines and comments.
  16. /^#/ {
  17. next
  18. }
  19. /^[ \t]*$/ {
  20. next
  21. }
  22. # Beginning of either a top namespace, tunable namespace or a tunable, decided
  23. # on the current value of TUNABLE, NS or TOP_NS.
  24. $2 == "{" {
  25. if (top_ns == "") {
  26. top_ns = $1
  27. }
  28. else if (ns == "") {
  29. ns = $1
  30. }
  31. else if (tunable == "") {
  32. tunable = $1
  33. }
  34. else {
  35. printf ("Unexpected occurrence of '{': %s:%d\n", FILENAME, FNR)
  36. exit 1
  37. }
  38. next
  39. }
  40. # End of either a top namespace, tunable namespace or a tunable.
  41. $1 == "}" {
  42. if (tunable != "") {
  43. # Tunables definition ended, now fill in default attributes.
  44. if (!types[top_ns,ns,tunable]) {
  45. types[top_ns,ns,tunable] = "STRING"
  46. }
  47. if (!minvals[top_ns,ns,tunable]) {
  48. minvals[top_ns,ns,tunable] = min_of[types[top_ns,ns,tunable]]
  49. }
  50. if (!maxvals[top_ns,ns,tunable]) {
  51. maxvals[top_ns,ns,tunable] = max_of[types[top_ns,ns,tunable]]
  52. }
  53. if (!env_alias[top_ns,ns,tunable]) {
  54. env_alias[top_ns,ns,tunable] = "NULL"
  55. }
  56. if (!security_level[top_ns,ns,tunable]) {
  57. security_level[top_ns,ns,tunable] = "SXID_ERASE"
  58. }
  59. tunable = ""
  60. }
  61. else if (ns != "") {
  62. ns = ""
  63. }
  64. else if (top_ns != "") {
  65. top_ns = ""
  66. }
  67. else {
  68. printf ("syntax error: extra }: %s:%d\n", FILENAME, FNR)
  69. exit 1
  70. }
  71. next
  72. }
  73. # Everything else, which could either be a tunable without any attributes or a
  74. # tunable attribute.
  75. {
  76. if (ns == "") {
  77. printf("Line %d: Invalid tunable outside a namespace: %s\n", NR, $0)
  78. exit 1
  79. }
  80. if (tunable == "") {
  81. # We encountered a tunable without any attributes, so note it with a
  82. # default.
  83. types[top_ns,ns,$1] = "STRING"
  84. next
  85. }
  86. # Otherwise, we have encountered a tunable attribute.
  87. split($0, arr, ":")
  88. attr = gensub(/^[ \t]+|[ \t]+$/, "", "g", arr[1])
  89. val = gensub(/^[ \t]+|[ \t]+$/, "", "g", arr[2])
  90. if (attr == "type") {
  91. types[top_ns,ns,tunable] = val
  92. }
  93. else if (attr == "minval") {
  94. minvals[top_ns,ns,tunable] = val
  95. }
  96. else if (attr == "maxval") {
  97. maxvals[top_ns,ns,tunable] = val
  98. }
  99. else if (attr == "env_alias") {
  100. env_alias[top_ns,ns,tunable] = sprintf("\"%s\"", val)
  101. }
  102. else if (attr == "security_level") {
  103. if (val == "SXID_ERASE" || val == "SXID_IGNORE" || val == "NONE") {
  104. security_level[top_ns,ns,tunable] = val
  105. }
  106. else {
  107. printf("Line %d: Invalid value (%s) for security_level: %s, ", NR, val,
  108. $0)
  109. print("Allowed values are 'SXID_ERASE', 'SXID_IGNORE', or 'NONE'")
  110. exit 1
  111. }
  112. }
  113. else if (attr == "default") {
  114. if (types[top_ns,ns,tunable] == "STRING") {
  115. default_val[top_ns,ns,tunable] = sprintf(".strval = \"%s\"", val);
  116. }
  117. else {
  118. default_val[top_ns,ns,tunable] = sprintf(".numval = %s", val)
  119. }
  120. }
  121. }
  122. END {
  123. if (ns != "") {
  124. print "Unterminated namespace. Is a closing brace missing?"
  125. exit 1
  126. }
  127. print "/* AUTOGENERATED by gen-tunables.awk. */"
  128. print "#ifndef _TUNABLES_H_"
  129. print "# error \"Do not include this file directly.\""
  130. print "# error \"Include tunables.h instead.\""
  131. print "#endif"
  132. print "#include <dl-procinfo.h>\n"
  133. # Now, the enum names
  134. print "\ntypedef enum"
  135. print "{"
  136. for (tnm in types) {
  137. split (tnm, indices, SUBSEP);
  138. t = indices[1];
  139. n = indices[2];
  140. m = indices[3];
  141. printf (" TUNABLE_ENUM_NAME(%s, %s, %s),\n", t, n, m);
  142. }
  143. print "} tunable_id_t;\n"
  144. # Finally, the tunable list.
  145. print "\n#ifdef TUNABLES_INTERNAL"
  146. print "static tunable_t tunable_list[] attribute_relro = {"
  147. for (tnm in types) {
  148. split (tnm, indices, SUBSEP);
  149. t = indices[1];
  150. n = indices[2];
  151. m = indices[3];
  152. printf (" {TUNABLE_NAME_S(%s, %s, %s)", t, n, m)
  153. printf (", {TUNABLE_TYPE_%s, %s, %s}, {%s}, NULL, TUNABLE_SECLEVEL_%s, %s},\n",
  154. types[t,n,m], minvals[t,n,m], maxvals[t,n,m],
  155. default_val[t,n,m], security_level[t,n,m], env_alias[t,n,m]);
  156. }
  157. print "};"
  158. print "#endif"
  159. }