/* Legacy one-liner parser for /etc/finit.conf and the finit.d hierarchy * * NOTE: this parser is frozen at the Finit 4.x feature set. It is * kept, indefinitely, for backwards compatibility -- existing * configurations must keep working. All new directives and * options land in the libconfuse-based format, see conf.c * * Copyright (c) 2012-2025 Joachim Wiberg * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "config.h" /* Generated by configure script */ #include #include #include #include #ifdef _LIBITE_LITE # include # include /* BSD sys/queue.h API */ #else # include # include /* BSD sys/queue.h API */ #endif #include "finit.h" #include "cond.h" #include "conf.h" #include "devmon.h" #include "legacy.h" #include "private.h" #include "service.h" #include "tty.h" #include "helpers.h" #include "util.h" #define BOOTSTRAP (runlevel == INIT_LEVEL) struct env_entry { TAILQ_ENTRY(env_entry) link; char *name; }; static TAILQ_HEAD(, env_entry) env_list = TAILQ_HEAD_INITIALIZER(env_list); static int get_bool(char *arg, int default_value) { if (!arg) goto fallback; if (string_compare(arg, "true") || string_compare(arg, "on") || string_compare(arg, "1")) return 1; if (string_compare(arg, "false") || string_compare(arg, "off") || string_compare(arg, "0")) return 0; fallback: return default_value; } static int validate_arg(char *arg, const char *opt) { if (!arg) { errx(1, "option %s missing argument, skipping.", opt); return 1; } return 0; } /* * finit.cond = foo (=> ) * finit.config = /path/to/etc/alt-finit.conf * finit.debug = [on,off] * finit.fstab = /path/to/etc/fstab.aternative * finit.status = [on,off] (compat finit.show_status) * finit.status_style = [old,classic,modern] */ static void parse_finit_opts(char *opt) { char *arg; arg = strchr(opt, '='); if (arg) *arg++ = 0; if (string_compare(opt, "cond")) { if (validate_arg(arg, "finit.cond")) return; cond_boot_parse(arg); return; } if (string_compare(opt, "config")) { if (validate_arg(arg, "finit.config")) return; if (finit_conf) free(finit_conf); finit_conf = strdup(arg); return; } if (string_compare(opt, "debug")) { debug = get_bool(arg, 1); return; } if (string_compare(opt, "fstab")) { if (validate_arg(arg, "finit.fstab")) return; if (fstab) free(fstab); fstab = strdup(arg); return; } if (string_compare(opt, "status_style")) { if (validate_arg(arg, "finit.status_style")) return; if (string_compare(arg, "old") || string_compare(arg, "classic")) show_progress(PROGRESS_CLASSIC); else show_progress(PROGRESS_MODERN); return; } if (string_compare(opt, "status") || string_compare(opt, "show_status")) { show_progress(get_bool(arg, 1) ? PROGRESS_DEFAULT : PROGRESS_SILENT); return; } } static void parse_fsck_opts(char *opt) { char *arg; arg = strchr(opt, '='); if (arg) *arg++ = 0; if (string_compare(opt, "mode")) { if (validate_arg(arg, "fsck.mode")) return; if (!strcmp(arg, "skip")) fsck_mode = NULL; if (!strcmp(arg, "auto")) fsck_mode = ""; if (!strcmp(arg, "force")) fsck_mode = "-f"; return; } if (string_compare(opt, "repair")) { if (validate_arg(arg, "fsck.repair")) return; if (!strcmp(arg, "no")) fsck_repair = "-n"; if (!strcmp(arg, "preen")) fsck_repair = "-p"; if (!strcmp(arg, "yes")) fsck_repair = "-y"; return; } } /* * When runlevel (single integer) is given on the command line, * it overrides the runlevel in finit.conf and the built-in * default (from configure). It do however have to pass the * same sanity checks. */ static int parse_runlevel(char *arg) { const char *err = NULL; char *ptr = arg; long long num; /* Sanity check the token is just digit(s) */ while (*ptr) { if (!isdigit(*ptr++)) return 0; } num = strtonum(arg, 1, 9, &err); if (err || num == 6) { dbg("Not a valid runlevel (%s), valid levels are [1-9], excluding 6, skipping.", arg); return 0; } return (int)num; } static void parse_arg(char *arg) { if (!strncmp(arg, "finit.", 6)) { parse_finit_opts(&arg[6]); return; } if (!strncmp(arg, "fsck.", 5)) { parse_fsck_opts(&arg[5]); return; } #ifdef RESCUE_MODE if (string_compare(arg, "rescue") || string_compare(arg, "recover")) { rescue = 1; return; } #endif if (string_compare(arg, "single") || string_compare(arg, "S")) { single = 1; return; } /* Put any new command line options before this line. */ cmdlevel = parse_runlevel(arg); } #ifdef KERNEL_CMDLINE /* * Parse /proc/cmdline to find args for init. Don't use this! * * Instead, rely on the kernel to give Finit its arguments as * regular argc + argv[]. Only use this if the system you run * on has a broken initramfs system that cannot forward args * to Finit properly. */ static void parse_kernel_cmdline(void) { char line[LINE_SIZE], *cmdline, *tok; FILE *fp; fp = fopen("/proc/cmdline", "r"); if (!fp) return; if (!fgets(line, sizeof(line), fp)) { fclose(fp); return; } cmdline = chomp(line); dbg("%s", cmdline); while ((tok = strtok(cmdline, " \t"))) { cmdline = NULL; parse_arg(tok); } fclose(fp); } #else #define parse_kernel_cmdline() #endif static void parse_kernel_loglevel(void) { char line[LINE_SIZE], *ptr; FILE *fp; int val; fp = fopen("/proc/sys/kernel/printk", "r"); if (!fp) return; if (!fgets(line, sizeof(line), fp)) { fclose(fp); return; } fclose(fp); ptr = chomp(line); dbg("%s", ptr); val = atoi(ptr); if (val >= 7) kerndebug = 1; } /* * Kernel gives us all non-kernel options on our cmdline */ void conf_parse_cmdline(int argc, char *argv[]) { const char *ptr; /* Set up defaults */ if ((ptr = FINIT_FSTAB)) fstab = strdup(ptr); finit_conf = strdup(FINIT_CONF); finit_rcsd = strdup(FINIT_RCSD); for (int i = 1; i < argc; i++) parse_arg(argv[i]); parse_kernel_cmdline(); parse_kernel_loglevel(); } /* * Clear all environment variables read in parse_env(), they may be * removed now so let the next call to parse_env() restore them. */ void conf_reset_env(void) { struct env_entry *node, *tmp; TAILQ_FOREACH_SAFE(node, &env_list, link, tmp) { TAILQ_REMOVE(&env_list, node, link); if (node->name) { unsetenv(node->name); free(node->name); } free(node); } if (!getenv("PATH")) setenv("PATH", _PATH_STDPATH, 1); if (!getenv("SHELL")) setenv("SHELL", _PATH_BSHELL, 1); setenv("LOGNAME", "root", 1); setenv("USER", "root", 1); } /* * Sourced mainly by initctl and other Finit helper tools */ void conf_saverc(void) { FILE *fp; mkpath(_PATH_VARRUN "finit", 0755); fp = fopen(_PATH_VARRUN "finit/.initrc", "w"); if (!fp) { err(1, "failed creating .finitrc"); return; } fprintf(fp, "FINIT_CONF=%s\n", finit_conf); fprintf(fp, "FINIT_RCSD=%s\n", finit_rcsd); fprintf(fp, "FINIT_CGPATH=%s\n", FINIT_CGPATH); fprintf(fp, "INIT_SOCKET=%s\n", INIT_SOCKET); fprintf(fp, "INIT_MAGIC=0x%08x\n", INIT_MAGIC); fclose(fp); } /* * Called at bootstrap to log execution order (for debug) */ void conf_save_exec_order(svc_t *svc, char *cmdline, int result) { const char *fn = _PATH_VARRUN "finit/exec.order"; const char *ststr = !result ? "[ OK ]" : "[FAIL]"; static char *prepared = NULL; int first = !fexist(fn); FILE *fp; fp = fopen(fn, "a+"); if (!fp) { err(1, "failed writing to %s", fn); return; } if (first) { fprintf(fp, "# Execution order of run/task/services at bootstrap\n"); fprintf(fp, "# ST TYPE COMMAND LINE (DESC)\n"); } if (result == -1) { int len; len = snprintf(NULL, 0, " %-7s %-64s (%s)", svc_typestr(svc), cmdline, svc->desc); if (len > 0) { if (prepared) free(prepared); prepared = malloc(++len); if (!prepared) goto done; snprintf(prepared, len, " %-7s %-64s (%s)", svc_typestr(svc), cmdline, svc->desc); } } else { if (prepared) { memcpy(prepared, ststr, strlen(ststr)); fprintf(fp, "%s\n", prepared); free(prepared); prepared = NULL; } else { fprintf(fp, "%s %-7s %-64s (%s)\n", ststr, svc_typestr(svc), cmdline, svc->desc); } } done: fclose(fp); } /* * Called by plugins and similar that dynamically generate system services */ void conf_save_service(int type, char *cfg, char *file) { char fn[256]; svc_t foo; FILE *fp; mkpath(FINIT_RUNPATH_, 0755); snprintf(fn, sizeof(fn), "%s/%s", FINIT_RUNPATH_, file); if (fexist(fn)) warnx("File %s already exists, overwriting.", fn); fp = fopen(fn, "w"); if (!fp) { err(1, "Failed creating %s", fn); return; } foo.type = type; fprintf(fp, "# Generated by finit:%s()\n", __func__); fprintf(fp, "%s %s\n", svc_typestr(&foo), cfg); fclose(fp); } /** * conf_parse_env - Parse a key=value line * @line: Line buffer without newline * @value: Whitespace trimmed value * * Returns: * %NULL on error, otherwise a whitespace trimmed key. */ char *conf_parse_env(char *line, char **value) { char *key, *val, *end; /* skip any leading whitespace */ key = line; while (isspace(*key)) key++; /* find end of line */ end = key; while (*end) end++; /* strip trailing whitespace */ if (end > key) { end--; while (isspace(*end)) *end-- = 0; } val = strchr(key, '='); if (!val) return NULL; *val++ = 0; /* strip leading whitespace from value */ while (isspace(*val)) val++; /* unquote value, if quoted */ unquote(&val, end); *value = val; /* find end of key */ end = key; while (*end) end++; /* strip trailing whitespace */ if (end > key) { end--; while (isspace(*end)) *end-- = 0; } /* strip any leading 'set ' */ end = key; if (!strncmp(key, "set", 3)) end += 3; /* check key, no spaces allowed */ while (*end && isspace(*end)) end++; key = end; while (*end && !isspace(*end)) end++; if (*end != 0) { warnx("'%s=%s': not a valid identifier", key, val); return NULL; /* invalid key */ } return key; } /* * Sets, and makes a note of, all KEY=VALUE lines in a given .conf line * from finit.conf, or other .conf file. Note, PATH is always reset in * the conf_reset_env() function. */ void legacy_parse_env(char *line) { struct env_entry *node; char *key, *val; key = conf_parse_env(line, &val); if (!key) return; dbg("Global env '%s'='%s'", key, val); setenv(key, val, 1); node = malloc(sizeof(*node)); if (!node) { nomem: err(1, "Out of memory cannot track env vars"); return; } node->name = strdup(key); if (!node->name) { free(node); goto nomem; } TAILQ_INSERT_HEAD(&env_list, node, link); } static int kmod_exists(char *mod) { char buf[256]; int found = 0; FILE *fp; fp = fopen("/proc/modules", "r"); if (!fp) return 0; while (!found && fgets(buf, sizeof(buf), fp)) { char *kmod = strtok(buf, " \t"); if (kmod && !strcmp(kmod, mod)) found = 1; } fclose(fp); return found; } void kmod_load(char *mod) { char module[64] = { 0 }; char cmd[CMD_SIZE]; if (runlevel != INIT_LEVEL) return; /* Strip args for progress below and kmod_exists() */ strlcpy(module, mod, sizeof(module)); if (!strtok(module, " \t")) return; if (kmod_exists(module)) return; strcpy(cmd, "modprobe "); strlcat(cmd, mod, sizeof(cmd)); run_interactive(cmd, "Loading kernel module %s", module); } /* Convert optional "[!123456789S]" string into a bitmask */ int conf_parse_runlevels(char *runlevels) { int i, not = 0, bitmask = 0; if (!runlevels) runlevels = "[234]"; i = 1; while (i) { int level; char lvl = runlevels[i++]; if (']' == lvl || 0 == lvl) break; if ('!' == lvl) { not = 1; bitmask = 0x7FE; continue; } if ('s' == lvl || 'S' == lvl) level = INIT_LEVEL; else level = lvl - '0'; if (level > INIT_LEVEL || level < 0) continue; if (not) CLRBIT(bitmask, level); else SETBIT(bitmask, level); } return bitmask; } void conf_parse_cond(svc_t *svc, char *cond) { size_t i = 0; char *ptr; char *c; if (!svc) { errx(1, "Invalid service pointer"); return; } /* By default we assume UNIX daemons support SIGHUP */ if (svc_is_daemon(svc)) svc->sighup = 1; if (!cond) { memset(svc->cond, 0, sizeof(svc->cond)); return; } /* * First character must be '!' if: * - service: SIGHUP is not supported * - run/task: Do not block bootstrap */ ptr = cond; if (ptr[i] == '!') { ptr++; if (svc_is_runtask(svc)) { /* see service_runtask_clean() */ svc->sighup = 1; svc->once = 1; } else { svc->sighup = 0; } } while (ptr[i] != '>' && ptr[i] != 0) i++; ptr[i] = 0; if (i >= sizeof(svc->cond)) { logit(LOG_WARNING, "%s: too long list of conditions: %s", svc_ident(svc, NULL, 0), ptr); return; } /* * The '~' prefix means a reload of the upstream service is * propagated to this service -- it will be reloaded (SIGHUP) * or restarted (noreload), not just paused and resumed. * Syntax: or <~pid/foo> * * Strip '~' from each condition and set the service-level * flag. This allows '~' on any condition in the list. */ svc->cond[0] = 0; for (i = 0, c = strtok(ptr, ","); c; c = strtok(NULL, ","), i++) { if (c[0] == '~') { c++; svc->flux_reload = 1; } devmon_add_cond(c); if (i) strlcat(svc->cond, ",", sizeof(svc->cond)); strlcat(svc->cond, c, sizeof(svc->cond)); } } struct rlimit_name { char *name; int val; }; static const struct rlimit_name rlimit_names[] = { { "as", RLIMIT_AS }, { "core", RLIMIT_CORE }, { "cpu", RLIMIT_CPU }, { "data", RLIMIT_DATA }, { "fsize", RLIMIT_FSIZE }, { "locks", RLIMIT_LOCKS }, { "memlock", RLIMIT_MEMLOCK }, { "msgqueue", RLIMIT_MSGQUEUE }, { "nice", RLIMIT_NICE }, { "nofile", RLIMIT_NOFILE }, { "nproc", RLIMIT_NPROC }, { "rss", RLIMIT_RSS }, { "rtprio", RLIMIT_RTPRIO }, #ifdef RLIMIT_RTTIME { "rttime", RLIMIT_RTTIME }, #endif { "sigpending", RLIMIT_SIGPENDING }, { "stack", RLIMIT_STACK }, { NULL, 0 } }; int str2rlim(char *str) { const struct rlimit_name *rn; for (rn = rlimit_names; rn->name; rn++) { if (!strcmp(str, rn->name)) return rn->val; } return -1; } char *rlim2str(int rlim) { const struct rlimit_name *rn; for (rn = rlimit_names; rn->name; rn++) { if (rn->val == rlim) return rn->name; } return "unknown"; } char *lim2str(struct rlimit *rlim) { char tmp[25]; static char buf[42]; buf[0] = 0; if (RLIM_INFINITY == rlim->rlim_cur) snprintf(tmp, sizeof(tmp), "unlimited, "); else snprintf(tmp, sizeof(tmp), "%llu, ", (unsigned long long)rlim->rlim_cur); strlcat(buf, tmp, sizeof(buf)); if (RLIM_INFINITY == rlim->rlim_max) snprintf(tmp, sizeof(tmp), "unlimited, "); else snprintf(tmp, sizeof(tmp), "%llu, ", (unsigned long long)rlim->rlim_max); strlcat(buf, tmp, sizeof(buf)); return buf; } /* First form: `rlimit RESOURCE LIMIT` */ void conf_parse_rlimit(char *line, struct rlimit arr[]) { char *level, *limit, *val; int resource = -1; rlim_t cfg; level = strtok(line, " \t"); if (!level) goto error; limit = strtok(NULL, " \t"); if (!limit) goto error; val = strtok(NULL, " \t"); if (!val) { /* Second form: `rlimit RESOURCE LIMIT` */ val = limit; limit = level; level = "both"; } resource = str2rlim(limit); if (resource < 0 || resource > RLIMIT_NLIMITS) goto error; /* Official keyword from v3.1 is `unlimited`, from prlimit(1) */ if (!strcmp(val, "unlimited") || !strcmp(val, "infinity")) { cfg = RLIM_INFINITY; } else { const char *err = NULL; cfg = strtonum(val, 0, (long long)2 << 31, &err); if (err) { logit(LOG_WARNING, "rlimit: invalid %s value: %s", rlim2str(resource), val); return; } } if (!strcmp(level, "soft")) arr[resource].rlim_cur = cfg; else if (!strcmp(level, "hard")) arr[resource].rlim_max = cfg; else if (!strcmp(level, "both")) arr[resource].rlim_max = arr[resource].rlim_cur = cfg; else goto error; return; error: logit(LOG_WARNING, "rlimit: parse error"); } /* cgroup NAME ctrl.prop:value,ctrl.prop:value ... */ static void conf_parse_cgroup(char *line) { char config[strlen(line) + 1]; char *ptr, *name; name = strtok(line, " \t"); if (!name) return; if (strstr(name, "..") || strchr(name, '/')) return; /* illegal */ config[0] = 0; while ((ptr = strtok(NULL, " \t"))) { if (config[0]) strlcat(config, ",", sizeof(config)); strlcat(config, ptr, sizeof(config)); } cgroup_add(name, config, 0); } static int parse_static(char *line, int is_rcsd) { char cmd[CMD_SIZE]; char *x; if (BOOTSTRAP && (MATCH_CMD(line, "host ", x) || MATCH_CMD(line, "hostname ", x))) { if (hostname) free(hostname); hostname = strdup(strip_line(x)); return 0; } if (BOOTSTRAP && MATCH_CMD(line, "mknod ", x)) { char *dev = strip_line(x); strcpy(cmd, "mknod "); strlcat(cmd, dev, sizeof(cmd)); run_interactive(cmd, "Creating device node %s", dev); return 0; } /* Kernel module to load */ if (BOOTSTRAP && MATCH_CMD(line, "module ", x)) { kmod_load(strip_line(x)); return 0; } if (BOOTSTRAP && MATCH_CMD(line, "network ", x)) { if (network) free(network); network = strdup(strip_line(x)); return 0; } if (BOOTSTRAP && MATCH_CMD(line, "rcsd ", x)) { if (finit_rcsd) free(finit_rcsd); finit_rcsd = strdup(strip_line(x)); return 0; } if (BOOTSTRAP && MATCH_CMD(line, "runparts ", x)) { if (runparts) free(runparts); runparts_progress = runparts_sysv = 0; while (x) { if (MATCH_CMD(x, "progress", x)) runparts_progress = 1; else if (MATCH_CMD(x, "sysv", x)) runparts_sysv = 1; else break; } runparts = strdup(strip_line(x)); return 0; } if (BOOTSTRAP && MATCH_CMD(line, "set ", x)) { legacy_parse_env(x); return 0; } if (MATCH_CMD(line, "include ", x)) { char *file = strip_line(x); strlcpy(cmd, file, sizeof(cmd)); if (!fexist(cmd)) { errx(1, "Cannot find include file %s, absolute path required!", x); return 1; } return conf_parse_file(cmd, is_rcsd); } if (MATCH_CMD(line, "log ", x)) { char *tok; static int size = 200000, count = 5; tok = strtok(x, ":= "); while (tok) { if (!strncmp(tok, "size", 4)) size = strtobytes(strtok(NULL, ":= ")); else if (!strncmp(tok, "count", 5)) count = strtobytes(strtok(NULL, ":= ")); tok = strtok(NULL, ":= "); } if (size >= 0) logfile_size_max = size; if (count >= 0) logfile_count_max = count; return 0; } if (MATCH_CMD(line, "shutdown ", x)) { if (sdown) free(sdown); sdown = strdup(strip_line(x)); return 0; } /* * The desired runlevel to start when leaving bootstrap (S). * Finit supports 1-9, but most systems only use 1-6, where * 6 is reserved for reboot and 0 for halt/poweroff. */ if (BOOTSTRAP && MATCH_CMD(line, "runlevel ", x)) { char *token = strip_line(x); const char *err = NULL; cfglevel = strtonum(token, 1, 9, &err); if (err) cfglevel = RUNLEVEL; if (cfglevel < 1 || cfglevel > 9 || cfglevel == 6) cfglevel = 2; /* Fallback */ return 0; } /* * Before Finit 5 the default readiness notification is PID. * This default can be changed by setting 'readiness none' in * the file /etc/finit.conf, only read once at bootstrap. */ if (BOOTSTRAP && MATCH_CMD(line, "readiness ", x)) { char *token = strip_line(x); if (!strcmp(token, "none")) readiness = SVC_NOTIFY_NONE; } if (MATCH_CMD(line, "reboot-delay ", x)) { syncsec = strtonum(strip_line(x), 0, 60, NULL); return 0; } if (MATCH_CMD(line, "reboot-watchdog ", x)) { wdtreboot = get_bool(strip_line(x), 0); return 0; } /* * Periodic check and instability index leveler, seconds */ if (MATCH_CMD(line, "service-interval ", x)) { char *token = strip_line(x); const char *err = NULL; int val; /* 0 min to 1 day, should check at least daily */ val = strtonum(token, 0, 1440, &err); if (!err) { int disabled = !service_interval; service_interval = val * 1000; /* to milliseconds */ if (disabled) service_init(NULL); } return 0; } return 1; } static int parse_dynamic(char *line, struct rlimit rlimit[], char *file) { char *x; /* Monitored daemon, will be respawned on exit */ if (MATCH_CMD(line, "service ", x)) { service_register(SVC_TYPE_SERVICE, x, rlimit, file); return 0; } /* One-shot task, will not be respawned */ if (MATCH_CMD(line, "task ", x)) { service_register(SVC_TYPE_TASK, x, rlimit, file); return 0; } /* Like task but waits for completion, useful w/ [S] */ if (MATCH_CMD(line, "run ", x)) { service_register(SVC_TYPE_RUN, x, rlimit, file); return 0; } /* Similar to task but is treated like a SysV init script */ if (MATCH_CMD(line, "sysv ", x)) { service_register(SVC_TYPE_SYSV, x, rlimit, file); return 0; } /* Read resource limits */ if (MATCH_CMD(line, "rlimit ", x)) { conf_parse_rlimit(x, rlimit); return 0; } /* Read control group limits */ if (MATCH_CMD(line, "cgroup ", x)) { conf_parse_cgroup(x); return 0; } /* Set current cgroup for the following services/run/tasks */ if (MATCH_CMD(line, "cgroup.", x)) { char *group, *token, *saveptr; char settings[128] = {0}; /* Reset cgroup state */ cgroup_settings_current[0] = '\0'; cgroup_delegate_current = 0; /* First token is the group name (system/user/init) */ group = strtok_r(x, ",", &saveptr); if (group) strlcpy(cgroup_current, group, sizeof(cgroup_current)); else strlcpy(cgroup_current, x, sizeof(cgroup_current)); /* Parse any remaining comma-separated options */ while ((token = strtok_r(NULL, ",", &saveptr)) != NULL) { if (strncmp(token, "name:", 5) == 0) { /* Cgroup leaf name override - only valid in per-service directives */ warnx("cgroup %s: name: option not valid in global directive!", group); } else if (strcmp(token, "delegate") == 0) { cgroup_delegate_current = 1; } else { /* Other settings (cpu.weight:500, memory.max:1G, etc.) */ if (settings[0]) strlcat(settings, ",", sizeof(settings)); strlcat(settings, token, sizeof(settings)); } } if (settings[0]) strlcpy(cgroup_settings_current, settings, sizeof(cgroup_settings_current)); return 0; } /* Regular or serial TTYs to run getty */ if (MATCH_CMD(line, "tty ", x)) { service_register(SVC_TYPE_TTY, strip_line(x), rlimit, file); return 0; } return 1; } /* * Very simple and crude implementation, only supports '%i' */ static char *instantiate(char *line, char *name) { char *ptr, *end = strchr(line, 0); char *pos = line; size_t num = 0; if (!name[0] || !end) return line; while ((ptr = strchr(pos, '%'))) { num++; pos++; } ptr = realloc(line, strlen(line) + num * strlen(name) + 1); if (!ptr) return line; pos = line = ptr; while ((ptr = strchr(pos, '%'))) { if (!strncmp(ptr, "%i", 2)) { char *rest = &ptr[2]; char *next = ptr + strlen(name); memmove(next, rest, strlen(rest) + 1); memcpy(ptr, name, strlen(name)); pos += strlen(name); } else pos++; } return line; } static int is_template(const char *file, char *name, size_t len) { char *ptr, *nm; size_t i = 0; ptr = strchr(file, '@'); if (!ptr) return 0; /* not a template */ nm = ptr + 1; ptr = strstr(nm, ".conf"); if (!strcmp(nm, ".conf") || !ptr) return 1; /* template itself or invalid */ while (nm < ptr && i < len - 1) name[i++] = *nm++; name[i] = 0; return 1; /* instantiated template */ } int legacy_parse_conf(char *file, int is_rcsd) { struct rlimit rlimit[RLIMIT_NLIMITS]; char name[65] = { 0 }; FILE *fp; if (is_template(file, name, sizeof(name))) { if (!name[0]) { dbg("*** Skipping template file %s", file); return 0; } dbg("*** instantiating %s from %s ...", name, file); } fp = fopen(file, "r"); if (!fp) return 1; /* Prepare default limits and group for each service in /etc/finit.d/ */ if (is_rcsd) { memcpy(rlimit, global_rlimit, sizeof(rlimit)); cgroup_current[0] = 0; } dbg("*** Parsing %s", file); while (!feof(fp)) { char *line; line = fparseln(fp, NULL, NULL, NULL, FPARSELN_UNESCCOMM); if (!line) continue; tabstospaces(line); // dbg("raw: %s", line); line = instantiate(line, name); // dbg("ins: %s", line); if (!parse_static(line, is_rcsd)) ; else if (!parse_dynamic(line, is_rcsd ? rlimit : global_rlimit, file)) ; else legacy_parse_env(line); free(line); } fclose(fp); return 0; } /** * Local Variables: * indent-tabs-mode: t * c-file-style: "linux" * End: */