rc-builder.include.awk (4486B)
1 function error(message) { 2 print "error: " message >"/dev/stderr" 3 exit 1 4 } 5 function start_service(svc_name, type, variable_enable) { 6 if(length(current_service_name)) { 7 error("start_service(\""svc_name"\", \""type"\", \""deps"\"): service \"" current_service_name "\" not terminated") 8 } 9 if(type !~ /^(oneshot|longrun)$/) { 10 error("start_service(\""svc_name"\", \""type"\", \""deps"\"): invalid service type") 11 } 12 if(length(variable_enable) != 0) { 13 if(!(find(variable_enable)?found:0)) { 14 return 0 15 } 16 } 17 current_service_name = svc_name 18 current_service_type = type 19 return 1 20 } 21 function start_oneshot(svc_name, deps) { 22 start_service(svc_name, "oneshot") 23 oneshot(svc_name, deps) 24 } 25 function start_longrun(svc_name, deps) { 26 start_service(svc_name, "longrun") 27 longrun(svc_name, deps) 28 } 29 function start_longrun_with_logger(svc_name, deps) { 30 start_service(svc_name, "longrun") 31 longrun_with_logger(svc_name, deps) 32 } 33 function enabled_service(svc_name, type, var_name) { 34 var_name = "enable_" current_service_name 35 gsub(/[-. ]/, "_", var_name) 36 return start_service(svc_name, type, var_name) 37 } 38 function enabled_oneshot(svc_name, deps) { 39 if(enabled_service(svc_name, "oneshot", var_name)) { 40 oneshot(svc_name, deps) 41 return 1 42 } 43 return 0 44 } 45 function enabled_longrun(svc_name, deps) { 46 if(enabled_service(svc_name, "longrun", var_name)) { 47 longrun(svc_name, deps) 48 return 1 49 } 50 return 0 51 } 52 function enabled_longrun_with_logger(svc_name, deps) { 53 if(enabled_service(svc_name, "longrun", var_name)) { 54 longrun_with_logger(svc_name, deps) 55 return 1 56 } 57 return 0 58 } 59 function in_bundle(name, variable_enable) { 60 if(!length(current_service_name)) { 61 error("in_bundle(\""name"\"): not in service context") 62 } 63 if(length(variable_enable) != 0) { 64 if(!(find(variable_enable)?found:0)) { 65 return 0 66 } 67 } 68 add_to_bundle(name, current_service_name) 69 return 1 70 } 71 function enabled_bundle(name, var_name) { 72 var_name = "enable_" current_service_name 73 gsub(/[-. ]/, "_", var_name) 74 in_bundle(name, var_name) 75 } 76 function flush_current_file() { 77 if(length(current_file_mode)) { 78 print "m" current_file_mode 79 current_file_mode = "" 80 } 81 } 82 function end() { 83 if(!length(current_service_name)) { 84 error("end(): not in service context") 85 } 86 flush_current_file() 87 current_service_name = "" 88 current_service_type = "" 89 } 90 function file_line(name, content, mode) { 91 if(!length(current_service_name)) { 92 error("start_file(\""name"\", \""mode"\"): not in service context") 93 } 94 flush_current_file() 95 print "/" current_service_name "/" name "\tcN\t" content "\tm" mode 96 } 97 function start_file(name, mode) { 98 if(!length(current_service_name)) { 99 error("start_file(\""name"\", \""mode"\"): not in service context") 100 } 101 flush_current_file() 102 print "/" current_service_name "/" name 103 printf("CN") 104 current_file_mode=mode 105 } 106 function assert_service_type(type, caller_name) { 107 if(!length(current_service_name)) { 108 error("" caller_name ": not in service context") 109 } 110 if(current_service_type != type) { 111 error("" caller_name ": service is of type \"" current_service_type "\" expected \"" type "\"") 112 } 113 } 114 function up(){ 115 assert_service_type("oneshot", "up()") 116 start_file("up", "644") 117 } 118 function down(){ 119 assert_service_type("oneshot", "down()") 120 start_file("down", "644") 121 } 122 function notification_fd(fd){ 123 if(fd !~ /^[0-9]+/ || fd < 3) { 124 error("notification_fd(\"" fd "\"): invalid filedescriptor number") 125 } 126 assert_service_type("longrun", "notification_fd("fd")") 127 file_line("notification-fd", fd, "644") 128 } 129 function consumer_for(name){ 130 if(name ~ /[\t\n ]/) { 131 error("consumer_for(\"" name "\"): invalid service name") 132 } 133 assert_service_type("longrun", "consumer_for(\"" name "\")") 134 file_line("consumer-for", name, "644") 135 } 136 function producer_for(name){ 137 if(name ~ /[\t\n ]/) { 138 error("producer_for(\"" name "\"): invalid service name") 139 } 140 assert_service_type("longrun", "producer_for(\"" name "\")") 141 file_line("producer-for", name, "644") 142 } 143 function run(){ 144 assert_service_type("longrun", "run()") 145 start_file("run", "755") 146 } 147 function finish(){ 148 assert_service_type("longrun", "finish()") 149 start_file("finish", "755") 150 } 151 function finish_el(){ 152 finish() 153 print "\t#!/command/execlineb -P" 154 } 155 function run_arg(cmd){ 156 assert_service_type("longrun", "run_arg(\"" cmd "\")") 157 flush_current_file() 158 runscript_simple(current_service_name, cmd) 159 } 160 function run_el(){ 161 assert_service_type("longrun", "run_el()") 162 flush_current_file() 163 runscript_el_cgroup2(current_service_name) 164 current_file_mode = "755" 165 }