rc-common.aat (2930B)
1 |function service(name, type, deps) { 2 |gsub("[ \t]+", "\n", deps) # Allow whitespace-separated dependencies for readability 3 /{{name}} d m755 4 /{{name}}/type cN {{type}} m644 5 |if(type == "bundle") { # contents file is required even if empty 6 /{{name}}/contents{{ C(deps)"m644" }} 7 |} else { 8 /{{name}}/dependencies{{ deps?C(deps)"m644":"\tr\t" }} 9 |} 10 | return name 11 |} 12 |function with_counter(base_name, suffix) { 13 | suffix = svc_counter[base_name]++ 14 | if(suffix) { return base_name "-" suffix } 15 | return base_name 16 |} 17 |function oneshot(name, deps) { 18 | return service(name, "oneshot", deps) 19 |} 20 |function longrun(name, deps) { 21 | return service(name, "longrun", deps) 22 |} 23 |function runscript_el_cgroup2(svc_name) { 24 /{{svc_name}}/run 25 CN #!/command/execlineb -P 26 getpid SERVICE_PID 27 foreground { 28 importas -i SERVICE_PID SERVICE_PID 29 if { test -d /run/cgroup2 } 30 if { mkdir -p /run/cgroup2/s6-rc/{{svc_name}} } 31 redirfd -w 1 /run/cgroup2/s6-rc/{{svc_name}}/cgroup.procs 32 printf "%s" ${SERVICE_PID} 33 } 34 unexport SERVICE_PID 35 |} 36 |function runscript_simple(svc_name, cmd) { 37 |runscript_el_cgroup2(svc_name) 38 fdmove -c 2 1 39 {{cmd}} 40 m755 41 |} 42 |function longrun_with_logger(name, deps) { 43 | longrun(name "-log", "mount-run-cgroup2\nrootfs") 44 /{{name}}-log/consumer-for cN {{name}} m640 45 |runscript_el_cgroup2(name "-log") 46 if { mkdir -p /var/log/{{name}} } 47 s6-log -b -- n10 s10240000 t /var/log/{{name}} 48 m755 49 | longrun(name, deps) 50 /{{name}}/producer-for cN {{name}}-log m640 51 |} 52 |function list_services_with_counter(base_name, suffix, list) { 53 | for(suffix = 0; suffix < svc_counter[base_name]; suffix++) { 54 | list = list base_name (suffix ? "-" suffix : "") "\n" 55 | } 56 | return list 57 |} 58 |function add_to_bundle(bundle_name, svc_name) { 59 | if(already_in_bundle[bundle_name,svc_name]) { return } 60 | already_in_bundle[bundle_name,svc_name] = 1 61 | if(bundles[bundle_name]) { 62 | bundles[bundle_name] = bundles[bundle_name] "\n" svc_name 63 | } else { 64 | bundles[bundle_name] = svc_name 65 | bundle_names[++bundle_count] = bundle_name 66 | } 67 |} 68 |function declare_bundle(bundle_name, svc_names, svc_array, svc_count, n) { 69 | if(length(svc_names)) { 70 | svc_count = split(svc_names, svc_array, "[ \t\n]+") 71 | for(n=1; n<=svc_count; n++) { 72 | add_to_bundle(bundle_name, svc_array[n]) 73 | } 74 | } else if(!(bundle_name in bundles)) { 75 | bundles[bundle_name] = "" 76 | bundle_names[++bundle_count] = bundle_name 77 | } 78 |} 79 |function print_bundles( n, command) { 80 | if(length(sort_tmpfile) && bundle_count) { 81 | command = "sort >" sort_tmpfile 82 | for(n=1; n<=bundle_count; n++) { 83 | print bundle_names[n] | command 84 | } 85 | close(command) 86 | n = 0 87 | while((getline bundle_names[n+1] < sort_tmpfile) > 0) { n++ } 88 | close(sort_tmpfile) 89 | if(n != bundle_count) { 90 | print "FATAL: error sorting bundles (had "bundle_count", got "n")" >"/dev/stderr" 91 | exit 1 92 | } 93 | } 94 | for(n=1; n<=bundle_count; n++) { 95 | service(bundle_names[n], "bundle", bundles[bundle_names[n]]) 96 97 | } 98 |}